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
|
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define DF_USAGE "Usage: df [-k] [-P] [file...]"
#define FS_NAME_LEN 256
#define HEADER_FMT "Filesystem %u-blocks Used Available Capacity Mounted on\n"
struct fs_results {
const char *name;
unsigned int total;
unsigned int used;
const char *root;
};
static void display_results(struct fs_results results[], size_t results_nb, bool display_in_kb) {
unsigned int block_size = display_in_kb ? 1024 : 512;
printf(HEADER_FMT, block_size);
for (size_t i = 0; i < results_nb; i++) {
struct fs_results cur = results[i];
unsigned int available = cur.total - cur.used;
unsigned int capacity = (cur.used / (float) cur.total) * 100;
printf("%s %d %d %d %d%% %s\n", cur.name, cur.total, cur.used, available, capacity, cur.root);
}
}
int df_main(int argc, char *argv[]) {
bool display_in_kb = false;
int ret = 0;
while ((ret = getopt(argc, argv, "kP")) != -1) {
switch (ret) {
case 'k':
display_in_kb = true;
break;
case 'P':
break;
default:
fputs(DF_USAGE, stderr);
return EXIT_FAILURE;
}
}
struct fs_results results[] = {
{
.name = "test",
.total = 872986112,
.used = 95206402,
.root = "/dev/test",
}
};
display_results(results, 1, false);
return 0;
}
|