-static void print_bytes_human(uint64_t bytes) {
- if (bytes > TERABYTE)
- printf("%.02f TB", (double)bytes / TERABYTE);
- else if (bytes > GIGABYTE)
- printf("%.01f GB", (double)bytes / GIGABYTE);
- else if (bytes > MEGABYTE)
- printf("%.01f MB", (double)bytes / MEGABYTE);
- else if (bytes > KILOBYTE)
- printf("%.01f KB", (double)bytes / KILOBYTE);
- else {
- printf("%.01f B", (double)bytes);
+static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
+ if (strcasecmp(prefix_type, "decimal") == 0) {
+ return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
+ } else if (strcasecmp(prefix_type, "custom") == 0) {
+ return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
+ } else {
+ return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
+ }
+}
+
+/*
+ * Determines whether remaining bytes are below given threshold.
+ *
+ */
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
+static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
+#else
+static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
+#endif
+ if (strcasecmp(threshold_type, "percentage_free") == 0) {
+ return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
+ } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
+ return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
+ } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
+ return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
+ } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
+ return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
+ } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
+ uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
+ double factor = 1;
+
+ switch (threshold_type[0]) {
+ case 'T':
+ case 't':
+ factor *= base;
+ case 'G':
+ case 'g':
+ factor *= base;
+ case 'M':
+ case 'm':
+ factor *= base;
+ case 'K':
+ case 'k':
+ factor *= base;
+ break;
+ default:
+ return false;