X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_disk_info.c;h=fb7348019b33f1de40a83f7a576d7c8c8193f4fb;hb=b850f5852d0e455c246827a603ac28577a70428d;hp=629e05b30d041eed235800133810bed05560a0c5;hpb=fad9c8237c63290aa9adde5ba6ffa6759f578645;p=i3%2Fi3status diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 629e05b..fb73480 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -10,6 +10,7 @@ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) #include #include +#elif defined(__NetBSD__) #else #include #endif @@ -109,10 +110,12 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c * human readable manner. * */ -void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) { +void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) { + const char *selected_format = format; const char *walk; char *outwalk = buffer; bool colorful_output = false; + bool mounted = false; INSTANCE(path); @@ -121,40 +124,52 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch if (statfs(path, &buf) == -1) return; + + mounted = true; +#elif defined(__NetBSD__) + struct statvfs buf; + + if (statvfs(path, &buf) == -1) + return; + + mounted = true; #else struct statvfs buf; if (statvfs(path, &buf) == -1) { /* If statvfs errors, e.g., due to the path not existing, - * we use the format for a not mounted device. */ - if (format_not_mounted != NULL) { - format = format_not_mounted; - } - } else if (format_not_mounted != NULL) { + * 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; - bool found = false; while ((m = getmntent(mntentfile)) != NULL) { - if (strcmp(m->mnt_dir, path) == 0) { - found = true; + if (strcmp(m->mnt_dir, sanitized) == 0) { + mounted = true; break; } } endmntent(mntentfile); - - if (!found) { - format = format_not_mounted; - } + free(sanitized); } #endif - if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { + if (!mounted) { + if (format_not_mounted == NULL) + format_not_mounted = ""; + selected_format = format_not_mounted; + } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { START_COLOR("color_bad"); colorful_output = true; + if (format_below_threshold != NULL) + selected_format = format_below_threshold; } - for (walk = format; *walk != '\0'; walk++) { + for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; continue;