summaryrefslogtreecommitdiff
path: root/src/print_disk_info.c
blob: 1ac22d0f3a51d453f9c70f17b1c19a93a3e95197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// vim:ts=8:expandtab
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <sys/param.h>
#include <sys/mount.h>
#endif


#include "i3status.h"

#define TERABYTE (1024ULL * 1024 * 1024 * 1024)
#define GIGABYTE (1024ULL * 1024 * 1024)
#define MEGABYTE (1024ULL * 1024)
#define KILOBYTE (1024ULL)

/*
 * Prints the given amount of bytes in a human readable manner.
 *
 */
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);
        }
}

/*
 * Does a statvfs and prints either free, used or total amounts of bytes in a
 * human readable manner.
 *
 */
void print_disk_info(const char *path, const char *format) {
        const char *walk;

        if (output_format == O_I3BAR)
                printf("{\"name\":\"disk_info\", \"instance\": \"%s\", \"full_text\":\"", path);

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
        struct statfs buf;

        if (statfs(path, &buf) == -1)
                return;
#else
        struct statvfs buf;

        if (statvfs(path, &buf) == -1)
                return;
#endif

        for (walk = format; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        putchar(*walk);
                        continue;
                }

                if (BEGINS_WITH(walk+1, "free")) {
                        print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
                        walk += strlen("free");
                }

                if (BEGINS_WITH(walk+1, "used")) {
                        print_bytes_human((uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree));
                        walk += strlen("used");
                }

                if (BEGINS_WITH(walk+1, "total")) {
                        print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks);
                        walk += strlen("total");
                }

                if (BEGINS_WITH(walk+1, "avail")) {
                        print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail);
                        walk += strlen("avail");
                }
        }

        if (output_format == O_I3BAR)
                printf("\"}");
}