diff -ur a/ioping.c b/ioping.c --- a/ioping.c 2011-06-02 22:51:03.000000000 +0400 +++ b/ioping.c 2011-06-07 04:01:04.000000000 +0400 @@ -166,6 +166,7 @@ char *path = NULL; char *fstype = ""; char *device = ""; +short strduped = 0; int fd; char *buf; @@ -258,41 +259,43 @@ void parse_device(dev_t dev) { - char *buf = NULL, *ptr; - unsigned major, minor; - struct stat st; - size_t len; FILE *file; + char *buf, *ptr; + unsigned major, minor; + size_t len = (~0); /* since v2.6.26 */ file = fopen("/proc/self/mountinfo", "r"); - if (!file) - goto old; - while (getline(&buf, &len, file) > 0) { + + if (file != NULL) { + buf = calloc(1, PATH_MAX); + while (getline(&buf, &len, file) > 0) { sscanf(buf, "%*d %*d %u:%u", &major, &minor); if (makedev(major, minor) != dev) continue; ptr = strstr(buf, " - ") + 3; fstype = strdup(strsep(&ptr, " ")); - device = strdup(strsep(&ptr, " ")); - goto out; - } -old: - /* for older versions */ - file = fopen("/proc/mounts", "r"); - if (!file) + device = strdup(strsep(&ptr, " ")); + } + } else { + /* for older versions */ + file = fopen("/proc/mounts", "r"); + if (!file) return; - while (getline(&buf, &len, file) > 0) { + + struct stat st; + + while (getline(&buf, &len, file) > 0) { ptr = buf; strsep(&ptr, " "); if (*buf != '/' || stat(buf, &st) || st.st_rdev != dev) continue; strsep(&ptr, " "); fstype = strdup(strsep(&ptr, " ")); - device = strdup(buf); - goto out; + device = strdup(buf); + } } -out: + strduped = 1; free(buf); fclose(file); } @@ -312,6 +315,12 @@ sigaction(signo, &sa, NULL); } +void free_on_exit(void) { + + if (strduped) + free(fstype), free(device); +} + int main (int argc, char **argv) { ssize_t ret_size; @@ -323,6 +332,8 @@ double time_sum, time_sum2, time_mdev, time_avg; double part_sum, part_sum2, part_mdev, part_avg; + atexit(free_on_exit); + parse_options(argc, argv); if (wsize) @@ -379,7 +390,7 @@ if (S_ISDIR(st.st_mode)) { char *tmpl = "/ioping.XXXXXX"; - char *temp = malloc(strlen(path) + strlen(tmpl) + 1); + char *temp = calloc(1, PATH_MAX); if (!temp) err(2, NULL); @@ -498,7 +509,7 @@ usleep(interval); } - + free(buf); time_total = now() - time_total; time_sum += part_sum;
patch -p1 < copy-n-paste.diff;
# valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./ioping -c5 . ==8328== Memcheck, a memory error detector ==8328== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==8328== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==8328== Command: ./ioping -c5 . ==8328== 4096 bytes from . (tmpfs tmpfs): request=1 time=1.7 ms 4096 bytes from . (tmpfs tmpfs): request=2 time=0.1 ms 4096 bytes from . (tmpfs tmpfs): request=3 time=0.1 ms 4096 bytes from . (tmpfs tmpfs): request=4 time=0.1 ms 4096 bytes from . (tmpfs tmpfs): request=5 time=0.1 ms --- . ioping statistics --- 5 requests completed in 4120.7 ms min/avg/max/mdev = 0.1/0.4/1.7/0.7 ms ==8328== ==8328== HEAP SUMMARY: ==8328== in use at exit: 0 bytes in 0 blocks ==8328== total heap usage: 6 allocs, 6 frees, 12,868 bytes allocated ==8328== ==8328== All heap blocks were freed -- no leaks are possible ==8328== ==8328== For counts of detected and suppressed errors, rerun with: -v ==8328== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Йес!!! Я спать! :)
|