-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdf.c
More file actions
50 lines (40 loc) · 1017 Bytes
/
df.c
File metadata and controls
50 lines (40 loc) · 1017 Bytes
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
#include <sys/statvfs.h>
#include <unistd.h>
static char num_buf[32];
static inline void write_str(const char *s) {
const char *p = s;
while (*p) p++;
write(1, s, p - s);
}
static inline void write_num(unsigned long n) {
char *p = num_buf + 31;
*p = '\0';
if (n == 0) {
*--p = '0';
} else {
while (n > 0) {
*--p = '0' + (n % 10);
n /= 10;
}
}
write_str(p);
}
int main(int argc, char **argv) {
struct statvfs st;
const char *path = (argc > 1) ? argv[1] : ".";
unsigned long total, free, used;
if (statvfs(path, &st) == -1) return 1;
total = (st.f_blocks * st.f_frsize) / 1024;
free = (st.f_bavail * st.f_frsize) / 1024;
used = total - free;
write_str("Total: ");
write_num(total);
write_str("K\nUsed: ");
write_num(used);
write_str("K\nFree: ");
write_num(free);
write_str("K\nPath: ");
write_str(path);
write_str("\n");
return 0;
}