Repository navigation
Expand file tree
/
Copy pathdf.c
More file actions
237 lines (203 loc) · 5.84 KB
/
Copy pathdf.c
File metadata and controls
237 lines (203 loc) · 5.84 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* df - disk free block printout Author: Andy Tanenbaum
*
* 91/04/30 Kees J. Bot (kjb@cs.vu.nl)
* Map filename arguments to the devices they live on.
* Changed output to show percentages.
*
* 92/12/12 Kees J. Bot
* Posixized. (Almost, the normal output is in kilobytes, it should
* be 512-byte units. 'df -P' and 'df -kP' are as it should be.)
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <minix/minlib.h>
struct mtab { /* List of mounted devices from /etc/mtab. */
struct mtab *next;
dev_t device;
char *devname;
char *mountpoint;
} *mtab= NULL;
struct mtab *searchtab(char *name);
static void readmtab(const char *type);
int df(const struct mtab *mt);
int iflag= 0; /* Focus on inodes instead of blocks. */
int Pflag= 0; /* Posix standard output. */
int kflag= 0; /* Output in kilobytes instead of 512 byte units for -P. */
int istty; /* isatty(1) */
void usage(void)
{
fprintf(stderr, "Usage: df [-ikP] [-t type] [device]...\n");
exit(1);
}
int unitsize;
int main(int argc, char *argv[])
{
int i;
struct mtab *mt;
char *type= "dev";
int ex= 0;
while (argc > 1 && argv[1][0] == '-') {
char *opt= argv[1]+1;
while (*opt != 0) {
switch (*opt++) {
case 'i': iflag= 1; break;
case 'k': kflag= 1; break;
case 'P': Pflag= 1; break;
case 't':
if (argc < 3) usage();
type= argv[2];
argv++;
argc--;
break;
default:
usage();
}
}
argc--;
argv++;
}
istty= isatty(1);
readmtab(type);
if(!Pflag || (Pflag && kflag)) unitsize = 1024;
else unitsize = 512;
if (Pflag) {
if (!iflag)
printf("\
Filesystem %4d-blocks Used Available Capacity Mounted on\n",
unitsize);
else
printf("\
Filesystem Inodes IUsed IFree %%IUsed Mounted on\n"
);
} else {
printf("%s\n", !iflag ? "\
Filesystem Size (kB) Free Used % Files% Mounted on" : "\
Filesystem Files Free Used % BUsed% Mounted on"
);
}
if (argc == 1) {
for (mt= mtab; mt != NULL; mt= mt->next) ex |= df(mt);
} else {
for (i = 1; i < argc; i++) ex |= df(searchtab(argv[i]));
}
exit(ex);
}
static void readmtab(const char *type)
/* Turn the mounted file table into a list. */
{
struct mtab **amt= &mtab, *new;
struct stat st;
char devname[PATH_MAX], mountpoint[PATH_MAX], version[MNTNAMELEN],
rw_flag[MNTFLAGLEN];
if (load_mtab("df") < 0) exit(1);
while (get_mtab_entry(devname, mountpoint, version, rw_flag),
devname[0] != 0) {
if (strcmp(type, "dev") != 0 && strcmp(type, version) != 0) continue;
/* Make new list cell. */
if ((new= (struct mtab *) malloc(sizeof(*new))) == NULL
|| (new->devname= (char *) malloc(strlen(devname) + 1)) == NULL
|| (new->mountpoint= (char *) malloc(strlen(mountpoint) + 1)) == NULL
) break;
if (strcmp(devname, "none") != 0 && stat(devname, &st) == 0 &&
S_ISBLK(st.st_mode)) {
new->device= st.st_rdev;
} else if (stat(mountpoint, &st) == 0) {
new->device= st.st_dev;
}
strcpy(new->devname, devname);
strcpy(new->mountpoint, mountpoint);
*amt= new; /* Add the cell to the end. */
amt= &new->next;
*amt= NULL;
}
}
struct mtab *searchtab(char *name)
/* See what we can do with a user supplied name, there are three possibilities:
* 1. It's a device and it is in the mtab: Return mtab entry.
* 2. It's a file and lives on a device in the mtab: Return mtab entry.
* 3. It's anything else: Return something df() will choke on.
*/
{
static struct mtab unknown;
struct mtab *mt;
struct stat st;
unknown.devname= name;
unknown.mountpoint= "";
if (stat(name, &st) < 0) return &unknown; /* Case 3. */
unknown.device= S_ISBLK(st.st_mode) ? st.st_rdev : st.st_dev;
for (mt= mtab; mt != NULL; mt= mt->next) {
if (unknown.device == mt->device)
return mt; /* Case 1 & 2. */
}
return &unknown; /* Case 3. */
}
/* (num / tot) in percentages rounded up. */
#define percent(num, tot) \
((tot > 0) ? ((int) ((100ULL * (num) + ((tot) - 1)) / (tot))) : 0)
int df(const struct mtab *mt)
{
long totblocks, busyblocks, totinodes, busyinodes;
struct statvfs sv;
int n;
if (statvfs(mt->mountpoint, &sv) < 0) {
fprintf(stderr, "df: %s: %s\n", mt->devname, strerror(errno));
return(1);
}
/* Print results. */
printf("%s", mt->devname);
n= strlen(mt->devname);
if (n > 15 && istty) { putchar('\n'); n= 0; }
while (n < 15) { putchar(' '); n++; }
totblocks = sv.f_blocks;
busyblocks = sv.f_blocks - sv.f_bfree;
busyblocks = busyblocks * (sv.f_bsize/512) / (unitsize/512);
totblocks = totblocks * (sv.f_bsize/512) / (unitsize/512);
totinodes = sv.f_files;
busyinodes = sv.f_files - sv.f_ffree;
if (!Pflag && !iflag) {
printf(" %9ld %9ld %9ld %3d%% %3d%% %s\n",
totblocks, /* Blocks */
totblocks - busyblocks, /* free */
busyblocks, /* used */
percent(busyblocks, totblocks), /* % */
percent(busyinodes, totinodes), /* FUsed% */
mt->mountpoint /* Mounted on */
);
}
if (!Pflag && iflag) {
printf(" %9ld %9ld %9ld %3d%% %3d%% %s\n",
totinodes, /* Files */
totinodes - busyinodes, /* free */
busyinodes, /* used */
percent(busyinodes, totinodes), /* % */
percent(busyblocks, totblocks), /* BUsed% */
mt->mountpoint /* Mounted on */
);
}
if (Pflag && !iflag) {
printf(" %9ld %9ld %9ld %4d%% %s\n",
totblocks, /* Blocks */
busyblocks, /* Used */
totblocks - busyblocks, /* Available */
percent(busyblocks, totblocks), /* Capacity */
mt->mountpoint /* Mounted on */
);
}
if (Pflag && iflag) {
printf(" %9ld %9ld %9ld %4d%% %s\n",
totinodes, /* Inodes */
busyinodes, /* IUsed */
totinodes - busyinodes, /* IAvail */
percent(busyinodes, totinodes), /* Capacity */
mt->mountpoint /* Mounted on */
);
}
return(0);
}