X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_disk_info.c;h=03c95e5dd93c08638abba90e8d4a620084b50e42;hb=e5455251481d396f11dc87c734a91a319c44773c;hp=d343fb89c1b94abe09271feb02a87da78c3235c1;hpb=9375959b68d7c80dbad53a6eb0df1035bb33c6ce;p=i3%2Fi3status diff --git a/src/print_disk_info.c b/src/print_disk_info.c index d343fb8..03c95e5 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -7,7 +7,7 @@ #include #include #include -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) #include #include #elif defined(__NetBSD__) @@ -141,16 +141,27 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch * we consider the device not mounted. */ mounted = false; } else { + char *sanitized = sstrdup(path); + if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/') + sanitized[strlen(sanitized) - 1] = '\0'; FILE *mntentfile = setmntent("/etc/mtab", "r"); - struct mntent *m; - - while ((m = getmntent(mntentfile)) != NULL) { - if (strcmp(m->mnt_dir, path) == 0) { - mounted = true; - break; + if (mntentfile == NULL) { + mntentfile = setmntent("/proc/mounts", "r"); + } + if (mntentfile == NULL) { + fprintf(stderr, "i3status: files /etc/mtab and /proc/mounts aren't accessible\n"); + } else { + struct mntent *m; + + while ((m = getmntent(mntentfile)) != NULL) { + if (strcmp(m->mnt_dir, sanitized) == 0) { + mounted = true; + break; + } } + endmntent(mntentfile); } - endmntent(mntentfile); + free(sanitized); } #endif @@ -168,47 +179,41 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "free")) { + } else if (BEGINS_WITH(walk + 1, "free")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type); walk += strlen("free"); - } - if (BEGINS_WITH(walk + 1, "used")) { + } else if (BEGINS_WITH(walk + 1, "used")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); walk += strlen("used"); - } - if (BEGINS_WITH(walk + 1, "total")) { + } else if (BEGINS_WITH(walk + 1, "total")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type); walk += strlen("total"); - } - if (BEGINS_WITH(walk + 1, "avail")) { + } else if (BEGINS_WITH(walk + 1, "avail")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type); walk += strlen("avail"); - } - if (BEGINS_WITH(walk + 1, "percentage_free")) { + } else if (BEGINS_WITH(walk + 1, "percentage_free")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_free"); - } - if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) { + } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_used_of_avail"); - } - if (BEGINS_WITH(walk + 1, "percentage_used")) { + } else if (BEGINS_WITH(walk + 1, "percentage_used")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_used"); - } - if (BEGINS_WITH(walk + 1, "percentage_avail")) { + } else if (BEGINS_WITH(walk + 1, "percentage_avail")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_avail"); + + } else { + *(outwalk++) = '%'; } }