-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdf.c
204 lines (182 loc) · 4.07 KB
/
df.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifdef __INTERIX
#define _NO_STATFS
#include <dirent.h>
#endif
#ifdef __NetBSD__
#define _NO_STATFS
#define getfsstat getvfsstat
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <errno.h>
#ifdef _NO_STATFS
#include <sys/statvfs.h>
#define statfs statvfs
#define f_bsize f_frsize
#else
#if defined __GLIBC__ || defined _WIN32
#include <sys/statfs.h>
#else
#include <sys/param.h>
#include <sys/mount.h>
#endif
#endif
static int ok = EXIT_SUCCESS;
static void printsize(long long int n) {
char unit = 'K';
long long int t;
n *= 10;
if(n > 1024 * 1024 * 10) {
n /= 1024;
unit = 'M';
}
if(n > 1024 * 1024 * 10) {
n /= 1024;
unit = 'G';
}
t = (n + 512) / 1024;
#if defined _WIN32 && !defined _WIN32_WNT_NATIVE
printf("%4lu.%1lu%ci", (unsigned long int)(t / 10), (unsigned long int)(t % 10), unit);
#else
printf("%4lld.%1lld%ci", t / 10, t % 10, unit);
#endif
}
static void sdf(const struct statfs *st, const char *s, int always) {
if(st->f_blocks == 0 && !always) return;
#if defined __GNU__ || defined __linux__ || (defined _WIN32 && !defined _WINDOWSNT_NATIVE) || (!defined __NetBSD__ && !defined __INTERIX && defined _NO_STATFS) || defined __sun
printf("%-20s ", s);
#else
printf("%-20s ", st->f_mntfromname);
#endif
printsize((long long int)st->f_blocks * (long long int)st->f_bsize);
printf(" ");
printsize((long long int)(st->f_blocks - (long long int)st->f_bfree) * st->f_bsize);
printf(" ");
printsize((long long int)st->f_bfree * (long long int)st->f_bsize);
printf(" %d\n", (int)st->f_bsize);
}
static void df(const char *s, int always) {
struct statfs st;
if(statfs(s, &st) < 0) {
//fprintf(stderr, "%s: %s\n", s, strerror(errno));
perror(s);
ok = EXIT_FAILURE;
} else {
//#endif
sdf(&st, s, always);
}
}
static void print_header() {
puts(
#if defined __GNU__ || defined __linux__ || (defined _WIN32 && !defined _WINDOWSNT_NATIVE) || (!defined __NetBSD__ && !defined __INTERIX && defined _NO_STATFS) || defined __sun
//"Mounted on"
"File system"
#else
//"Filesystem"
"Device "
#endif
" Size Used Free Block size");
}
int main(int argc, char *argv[]) {
int all = 0;
if(argc > 1 && (strcmp(argv[1], "-a") == 0 || strcmp(argv[1], "--all") == 0)) {
all = 1;
argc--;
argv++;
}
if(argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--human-readable") == 0)) {
argc--;
argv++;
}
if(argc == 1) {
#if defined __GNU__ || defined __linux__
char s[2000];
FILE *f = fopen("/proc/mounts", "r");
if(!f) {
perror("/proc/mounts");
return 1;
}
print_header();
while(fgets(s, 2000, f)) {
char *c, *e = s;
for (c = s; *c; c++) {
if (*c == ' ') {
e = c + 1;
break;
}
}
for (c = e; *c; c++) {
if (*c == ' ') {
*c = 0;
break;
}
}
df(e, all);
}
fclose(f);
#elif defined __SVR4
char s[2000];
FILE *f = fopen("/etc/mnttab", "r");
if(!f) {
perror("/etc/mnttab");
return 1;
}
print_header();
while(fgets(s, sizeof s, f)) {
char *c = s;
while(*c) if(*c++ == ' ') {
char *e = c;
while(*++c) if(*c == ' ') {
*c = 0;
break;
}
df(e, all);
break;
}
}
fclose(f);
#elif defined __INTERIX
DIR *d = opendir("/dev/fs");
if(!d) {
perror("/dev/fs");
return 1;
}
print_header();
struct dirent *de;
while((de = readdir(d))) {
char buffer[10] = "/dev/fs/";
memcpy(buffer + 8, de->d_name, 2);
df(buffer, all);
}
closedir(d);
#elif (!defined _NO_STATFS || defined __NetBSD__) && !defined _WINDOWSNT_NATIVE && !defined _WIN32
int i;
//struct statfs buffer[200];
int len = getfsstat(NULL, 0, MNT_NOWAIT);
if(len < 0) {
perror("getfsstat");
return 1;
}
struct statfs buffer[len];
if(getfsstat(buffer, len * sizeof(struct statfs), MNT_NOWAIT) < 0) {
perror("getfsstat");
return 1;
}
print_header();
for(i = 0; i < len; i++) {
sdf(buffer + i, NULL, all);
}
#else
fprintf(stderr, "df: You need to specify at least one path\n");
return -1;
#endif
} else {
int i;
print_header();
for(i = 1; i < argc; i++) {
df(argv[i], 1);
}
}
return ok;
}