]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /* |
2 | * Copyright (c) 1980, 1990, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * (c) UNIX System Laboratories, Inc. | |
5 | * All or some portions of this file are derived from material licensed | |
6 | * to the University of California by American Telephone and Telegraph | |
7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
8 | * the permission of UNIX System Laboratories, Inc. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 3. All advertising materials mentioning features or use of this software | |
19 | * must display the following acknowledgement: | |
20 | * This product includes software developed by the University of | |
21 | * California, Berkeley and its contributors. | |
22 | * 4. Neither the name of the University nor the names of its contributors | |
23 | * may be used to endorse or promote products derived from this software | |
24 | * without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
36 | * SUCH DAMAGE. | |
37 | */ | |
38 | ||
00337e45 | 39 | #include <sys/cdefs.h> |
44a7a5ab | 40 | #ifndef lint |
00337e45 | 41 | __used static const char copyright[] = |
44a7a5ab | 42 | "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ |
6c780a1f | 43 | The Regents of the University of California. All rights reserved.\n"; |
44a7a5ab A |
44 | #endif /* not lint */ |
45 | ||
46 | #ifndef lint | |
47 | #if 0 | |
6c780a1f | 48 | static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; |
44a7a5ab | 49 | #else |
00337e45 | 50 | __used static const char rcsid[] = |
6c780a1f | 51 | "$FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $"; |
44a7a5ab A |
52 | #endif |
53 | #endif /* not lint */ | |
54 | ||
6c780a1f A |
55 | #ifdef __APPLE__ |
56 | #define MNT_IGNORE 0 | |
6c780a1f A |
57 | #endif |
58 | ||
59 | #include <sys/cdefs.h> | |
44a7a5ab A |
60 | #include <sys/param.h> |
61 | #include <sys/stat.h> | |
62 | #include <sys/mount.h> | |
6c780a1f | 63 | #include <sys/sysctl.h> |
44a7a5ab A |
64 | #include <err.h> |
65 | #include <errno.h> | |
66 | #include <fcntl.h> | |
6c780a1f A |
67 | #include <fstab.h> |
68 | #include <math.h> | |
44a7a5ab A |
69 | #include <stdio.h> |
70 | #include <stdlib.h> | |
71 | #include <string.h> | |
6c780a1f | 72 | #include <sysexits.h> |
44a7a5ab | 73 | #include <unistd.h> |
864a4b6e | 74 | #include <libutil.h> |
44a7a5ab | 75 | |
c59d3020 A |
76 | #ifdef __APPLE__ |
77 | #include "get_compat.h" | |
78 | #else | |
79 | #define COMPAT_MODE(func, mode) 1 | |
80 | #endif | |
81 | ||
6c780a1f A |
82 | #define UNITS_SI 1 |
83 | #define UNITS_2 2 | |
84 | ||
85 | #define KILO_SZ(n) (n) | |
86 | #define MEGA_SZ(n) ((n) * (n)) | |
87 | #define GIGA_SZ(n) ((n) * (n) * (n)) | |
88 | #define TERA_SZ(n) ((n) * (n) * (n) * (n)) | |
89 | #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) | |
90 | ||
91 | #define KILO_2_SZ (KILO_SZ(1024ULL)) | |
92 | #define MEGA_2_SZ (MEGA_SZ(1024ULL)) | |
93 | #define GIGA_2_SZ (GIGA_SZ(1024ULL)) | |
94 | #define TERA_2_SZ (TERA_SZ(1024ULL)) | |
95 | #define PETA_2_SZ (PETA_SZ(1024ULL)) | |
96 | ||
97 | #define KILO_SI_SZ (KILO_SZ(1000ULL)) | |
98 | #define MEGA_SI_SZ (MEGA_SZ(1000ULL)) | |
99 | #define GIGA_SI_SZ (GIGA_SZ(1000ULL)) | |
100 | #define TERA_SI_SZ (TERA_SZ(1000ULL)) | |
101 | #define PETA_SI_SZ (PETA_SZ(1000ULL)) | |
102 | ||
103 | /* Maximum widths of various fields. */ | |
104 | struct maxwidths { | |
105 | int mntfrom; | |
106 | int total; | |
107 | int used; | |
108 | int avail; | |
109 | int iused; | |
110 | int ifree; | |
111 | }; | |
112 | ||
113 | unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ}; | |
114 | unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ}; | |
115 | unsigned long long *valp; | |
116 | ||
117 | typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; | |
118 | ||
119 | unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; | |
120 | ||
121 | int bread(off_t, void *, int); | |
122 | int checkvfsname(const char *, char **); | |
123 | char *getmntpt(char *); | |
29341915 | 124 | int int64width(int64_t); |
6c780a1f | 125 | char *makenetvfslist(void); |
864a4b6e A |
126 | char **makevfslist(const char *); |
127 | void prthuman(struct statfs *, uint64_t); | |
128 | void prthumanval(int64_t); | |
6c780a1f A |
129 | void prtstat(struct statfs *, struct maxwidths *); |
130 | long regetmntinfo(struct statfs **, long, char **); | |
6c780a1f A |
131 | unit_t unit_adjust(double *); |
132 | void update_maxwidths(struct maxwidths *, struct statfs *); | |
133 | void usage(void); | |
134 | ||
135 | int aflag = 0, hflag, iflag, nflag; | |
44a7a5ab | 136 | |
6c780a1f A |
137 | static __inline int imax(int a, int b) |
138 | { | |
139 | return (a > b ? a : b); | |
140 | } | |
141 | ||
44a7a5ab | 142 | int |
6c780a1f | 143 | main(int argc, char *argv[]) |
44a7a5ab A |
144 | { |
145 | struct stat stbuf; | |
6c780a1f A |
146 | struct statfs statfsbuf, *mntbuf; |
147 | struct maxwidths maxwidths; | |
864a4b6e | 148 | char *mntpt, **vfslist; |
44a7a5ab | 149 | long mntsize; |
c59d3020 | 150 | int ch, i, rv, tflag = 0, kludge_tflag = 0; |
864a4b6e | 151 | int kflag = 0; |
c59d3020 A |
152 | const char *options = "abgHhiklmnPt:T:"; |
153 | if (COMPAT_MODE("bin/df", "unix2003")) { | |
864a4b6e A |
154 | /* Unix2003 requires -t be "include total capacity". which df |
155 | already does, but it conflicts with the old -t so we need to | |
c59d3020 A |
156 | *not* expect a string after -t (we provide -T in both cases |
157 | to cover the old use of -t) */ | |
158 | options = "abgHhiklmnPtT:"; | |
4d0bb651 | 159 | iflag = 1; |
c59d3020 | 160 | } |
44a7a5ab | 161 | |
6c780a1f | 162 | vfslist = NULL; |
c59d3020 | 163 | while ((ch = getopt(argc, argv, options)) != -1) |
44a7a5ab | 164 | switch (ch) { |
6c780a1f A |
165 | case 'a': |
166 | aflag = 1; | |
167 | break; | |
168 | case 'b': | |
169 | /* FALLTHROUGH */ | |
170 | case 'P': | |
864a4b6e A |
171 | if (COMPAT_MODE("bin/df", "unix2003")) { |
172 | if (!kflag) { | |
173 | /* -k overrides -P */ | |
174 | putenv("BLOCKSIZE=512"); | |
175 | } | |
184968f4 | 176 | iflag = 0; |
864a4b6e A |
177 | } else { |
178 | putenv("BLOCKSIZE=512"); | |
179 | } | |
6c780a1f A |
180 | hflag = 0; |
181 | break; | |
182 | case 'g': | |
183 | putenv("BLOCKSIZE=1g"); | |
184 | hflag = 0; | |
185 | break; | |
186 | case 'H': | |
187 | hflag = UNITS_SI; | |
188 | valp = vals_si; | |
189 | break; | |
190 | case 'h': | |
191 | hflag = UNITS_2; | |
192 | valp = vals_base2; | |
193 | break; | |
44a7a5ab A |
194 | case 'i': |
195 | iflag = 1; | |
196 | break; | |
197 | case 'k': | |
864a4b6e A |
198 | if (COMPAT_MODE("bin/df", "unix2003")) { |
199 | putenv("BLOCKSIZE=1024"); | |
200 | } else { | |
201 | putenv("BLOCKSIZE=1k"); | |
202 | } | |
203 | kflag = 1; | |
6c780a1f | 204 | hflag = 0; |
44a7a5ab A |
205 | break; |
206 | case 'l': | |
6c780a1f | 207 | if (tflag) |
c59d3020 | 208 | errx(1, "-l and -T are mutually exclusive."); |
6c780a1f A |
209 | if (vfslist != NULL) |
210 | break; | |
211 | vfslist = makevfslist(makenetvfslist()); | |
212 | break; | |
213 | case 'm': | |
214 | putenv("BLOCKSIZE=1m"); | |
215 | hflag = 0; | |
44a7a5ab A |
216 | break; |
217 | case 'n': | |
218 | nflag = 1; | |
219 | break; | |
220 | case 't': | |
c59d3020 A |
221 | /* Unix2003 uses -t for something we do by default */ |
222 | if (COMPAT_MODE("bin/df", "unix2003")) { | |
223 | kludge_tflag = 1; | |
224 | break; | |
225 | } | |
226 | case 'T': | |
6c780a1f A |
227 | if (vfslist != NULL) { |
228 | if (tflag) | |
c59d3020 | 229 | errx(1, "only one -%c option may be specified", ch); |
6c780a1f | 230 | else |
c59d3020 | 231 | errx(1, "-l and -%c are mutually exclusive.", ch); |
6c780a1f A |
232 | } |
233 | tflag++; | |
6c780a1f | 234 | vfslist = makevfslist(optarg); |
44a7a5ab A |
235 | break; |
236 | case '?': | |
237 | default: | |
238 | usage(); | |
239 | } | |
240 | argc -= optind; | |
241 | argv += optind; | |
242 | ||
c59d3020 A |
243 | /* If we are in unix2003 mode, have seen a -t but no -T and the first |
244 | non switch arg isn't a file, let's pretend they used -T on it. | |
245 | This makes the Lexmark printer installer happy (PR-3918471) */ | |
246 | if (tflag == 0 && kludge_tflag && *argv && stat(*argv, &stbuf) < 0 | |
247 | && errno == ENOENT) { | |
4d0bb651 | 248 | vfslist = makevfslist(*argv++); |
c59d3020 A |
249 | } |
250 | ||
44a7a5ab | 251 | mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); |
6c780a1f A |
252 | bzero(&maxwidths, sizeof(maxwidths)); |
253 | for (i = 0; i < mntsize; i++) | |
254 | update_maxwidths(&maxwidths, &mntbuf[i]); | |
44a7a5ab | 255 | |
6c780a1f | 256 | rv = 0; |
44a7a5ab | 257 | if (!*argv) { |
6c780a1f A |
258 | mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); |
259 | bzero(&maxwidths, sizeof(maxwidths)); | |
260 | for (i = 0; i < mntsize; i++) | |
261 | update_maxwidths(&maxwidths, &mntbuf[i]); | |
262 | for (i = 0; i < mntsize; i++) { | |
263 | if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) | |
264 | prtstat(&mntbuf[i], &maxwidths); | |
265 | } | |
266 | exit(rv); | |
267 | } | |
268 | ||
269 | for (; *argv; argv++) { | |
270 | if (stat(*argv, &stbuf) < 0) { | |
271 | if ((mntpt = getmntpt(*argv)) == 0) { | |
272 | warn("%s", *argv); | |
273 | rv = 1; | |
274 | continue; | |
275 | } | |
c59d3020 | 276 | } else if (S_ISCHR(stbuf.st_mode) || S_ISBLK(stbuf.st_mode)) { |
864a4b6e A |
277 | warnx("%s: Raw devices not supported", *argv); |
278 | rv = 1; | |
279 | continue; | |
6c780a1f A |
280 | } else |
281 | mntpt = *argv; | |
282 | /* | |
283 | * Statfs does not take a `wait' flag, so we cannot | |
284 | * implement nflag here. | |
285 | */ | |
286 | if (statfs(mntpt, &statfsbuf) < 0) { | |
287 | warn("%s", mntpt); | |
288 | rv = 1; | |
289 | continue; | |
44a7a5ab | 290 | } |
6c780a1f A |
291 | /* Check to make sure the arguments we've been |
292 | * given are satisfied. Return an error if we | |
293 | * have been asked to list a mount point that does | |
294 | * not match the other args we've been given (-l, -t, etc.) | |
295 | */ | |
296 | if (checkvfsname(statfsbuf.f_fstypename, vfslist)) { | |
297 | rv++; | |
298 | continue; | |
299 | } | |
864a4b6e | 300 | |
6c780a1f | 301 | if (argc == 1) { |
864a4b6e | 302 | bzero(&maxwidths, sizeof(maxwidths)); |
6c780a1f A |
303 | update_maxwidths(&maxwidths, &statfsbuf); |
304 | } | |
305 | prtstat(&statfsbuf, &maxwidths); | |
44a7a5ab | 306 | } |
6c780a1f | 307 | return (rv); |
44a7a5ab A |
308 | } |
309 | ||
310 | char * | |
6c780a1f | 311 | getmntpt(char *name) |
44a7a5ab A |
312 | { |
313 | long mntsize, i; | |
314 | struct statfs *mntbuf; | |
315 | ||
316 | mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); | |
317 | for (i = 0; i < mntsize; i++) { | |
318 | if (!strcmp(mntbuf[i].f_mntfromname, name)) | |
319 | return (mntbuf[i].f_mntonname); | |
320 | } | |
321 | return (0); | |
322 | } | |
323 | ||
44a7a5ab A |
324 | /* |
325 | * Make a pass over the filesystem info in ``mntbuf'' filtering out | |
6c780a1f | 326 | * filesystem types not in vfslist and possibly re-stating to get |
44a7a5ab A |
327 | * current (not cached) info. Returns the new count of valid statfs bufs. |
328 | */ | |
329 | long | |
6c780a1f | 330 | regetmntinfo(struct statfs **mntbufp, long mntsize, char **vfslist) |
44a7a5ab A |
331 | { |
332 | int i, j; | |
333 | struct statfs *mntbuf; | |
334 | ||
6c780a1f | 335 | if (vfslist == NULL) |
44a7a5ab A |
336 | return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); |
337 | ||
338 | mntbuf = *mntbufp; | |
6c780a1f A |
339 | for (j = 0, i = 0; i < mntsize; i++) { |
340 | if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) | |
44a7a5ab | 341 | continue; |
6c780a1f A |
342 | if (!nflag) |
343 | (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); | |
344 | else if (i != j) | |
44a7a5ab | 345 | mntbuf[j] = mntbuf[i]; |
44a7a5ab A |
346 | j++; |
347 | } | |
348 | return (j); | |
349 | } | |
350 | ||
6c780a1f A |
351 | /* |
352 | * Output in "human-readable" format. Uses 3 digits max and puts | |
353 | * unit suffixes at the end. Makes output compact and easy to read, | |
354 | * especially on huge disks. | |
355 | * | |
356 | */ | |
357 | unit_t | |
358 | unit_adjust(double *val) | |
359 | { | |
360 | double abval; | |
361 | unit_t unit; | |
362 | unsigned int unit_sz; | |
363 | ||
364 | abval = fabs(*val); | |
365 | ||
366 | unit_sz = abval ? ilogb(abval) / 10 : 0; | |
367 | ||
368 | if (unit_sz >= UNIT_MAX) { | |
369 | unit = NONE; | |
370 | } else { | |
371 | unit = unitp[unit_sz]; | |
372 | *val /= (double)valp[unit_sz]; | |
373 | } | |
374 | ||
375 | return (unit); | |
376 | } | |
377 | ||
378 | void | |
864a4b6e | 379 | prthuman(struct statfs *sfsp, uint64_t used) |
6c780a1f | 380 | { |
864a4b6e A |
381 | int64_t value; |
382 | ||
383 | value = sfsp->f_blocks; | |
384 | value *= sfsp->f_bsize; | |
385 | prthumanval(value); | |
386 | value = used; | |
387 | value *= sfsp->f_bsize; | |
388 | prthumanval(value); | |
389 | value = sfsp->f_bavail; | |
390 | value *= sfsp->f_bsize; | |
391 | prthumanval(value); | |
6c780a1f A |
392 | } |
393 | ||
394 | void | |
864a4b6e | 395 | prthumanval(int64_t bytes) |
6c780a1f | 396 | { |
864a4b6e A |
397 | char buf[6]; |
398 | int flags; | |
6c780a1f | 399 | |
864a4b6e A |
400 | flags = HN_B | HN_NOSPACE | HN_DECIMAL; |
401 | if (hflag == UNITS_SI) | |
402 | flags |= HN_DIVISOR_1000; | |
403 | ||
404 | humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), | |
405 | bytes, "", HN_AUTOSCALE, flags); | |
6c780a1f | 406 | |
864a4b6e A |
407 | if (hflag == UNITS_SI) |
408 | (void)printf(" %6s", buf); | |
6c780a1f | 409 | else |
864a4b6e A |
410 | (void)printf("%6si", buf); |
411 | ||
6c780a1f A |
412 | } |
413 | ||
44a7a5ab A |
414 | /* |
415 | * Convert statfs returned filesystem size into BLOCKSIZE units. | |
416 | * Attempts to avoid overflow for large filesystems. | |
417 | */ | |
864a4b6e A |
418 | static intmax_t fsbtoblk(int64_t num, uint64_t fsbs, u_long bs, char *fs) |
419 | { | |
420 | if (num < 0) { | |
421 | warnx("negative filesystem block count/size from fs %s", fs); | |
422 | return 0; | |
423 | } else if ((fsbs != 0) && (fsbs < bs)) { | |
424 | return (num / (intmax_t) (bs / fsbs)); | |
425 | } else { | |
426 | return (num * (intmax_t) (fsbs / bs)); | |
427 | } | |
428 | } | |
44a7a5ab A |
429 | |
430 | /* | |
431 | * Print out status about a filesystem. | |
432 | */ | |
433 | void | |
6c780a1f | 434 | prtstat(struct statfs *sfsp, struct maxwidths *mwp) |
44a7a5ab A |
435 | { |
436 | static long blocksize; | |
437 | static int headerlen, timesthrough; | |
6c780a1f | 438 | static const char *header; |
864a4b6e A |
439 | uint64_t used, availblks, inodes; |
440 | char * avail_str; | |
44a7a5ab | 441 | |
44a7a5ab | 442 | if (++timesthrough == 1) { |
4d0bb651 | 443 | mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); |
6c780a1f A |
444 | if (hflag) { |
445 | header = " Size"; | |
4d0bb651 | 446 | mwp->total = mwp->used = mwp->avail = (int)strlen(header); |
6c780a1f | 447 | } else { |
44a7a5ab | 448 | header = getbsize(&headerlen, &blocksize); |
6c780a1f A |
449 | mwp->total = imax(mwp->total, headerlen); |
450 | } | |
4d0bb651 | 451 | mwp->used = imax(mwp->used, (int)strlen("Used")); |
864a4b6e A |
452 | if (COMPAT_MODE("bin/df", "unix2003") && !hflag) { |
453 | avail_str = "Available"; | |
454 | } else { | |
455 | avail_str = "Avail"; | |
456 | } | |
4d0bb651 | 457 | mwp->avail = imax(mwp->avail, (int)strlen(avail_str)); |
6c780a1f | 458 | |
864a4b6e | 459 | (void)printf("%-*s %*s %*s %*s Capacity", mwp->mntfrom, |
6c780a1f | 460 | "Filesystem", mwp->total, header, mwp->used, "Used", |
864a4b6e | 461 | mwp->avail, avail_str); |
6c780a1f | 462 | if (iflag) { |
4d0bb651 A |
463 | mwp->iused = imax(mwp->iused, (int)strlen(" iused")); |
464 | mwp->ifree = imax(mwp->ifree, (int)strlen("ifree")); | |
6c780a1f A |
465 | (void)printf(" %*s %*s %%iused", mwp->iused - 2, |
466 | "iused", mwp->ifree, "ifree"); | |
467 | } | |
44a7a5ab A |
468 | (void)printf(" Mounted on\n"); |
469 | } | |
6c780a1f A |
470 | |
471 | (void)printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname); | |
864a4b6e A |
472 | if (sfsp->f_blocks > sfsp->f_bfree) |
473 | used = sfsp->f_blocks - sfsp->f_bfree; | |
474 | else | |
475 | used = 0; | |
44a7a5ab | 476 | availblks = sfsp->f_bavail + used; |
6c780a1f A |
477 | if (hflag) { |
478 | prthuman(sfsp, used); | |
479 | } else { | |
40bf83fe | 480 | (void)printf(" %*jd %*jd %*jd", mwp->total, |
864a4b6e A |
481 | fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize, sfsp->f_mntonname), |
482 | mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize, sfsp->f_mntonname), | |
483 | mwp->avail, fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize, sfsp->f_mntonname)); | |
484 | } | |
485 | if (COMPAT_MODE("bin/df", "unix2003")) { | |
486 | /* Standard says percentage must be rounded UP to next | |
487 | integer value, not truncated */ | |
488 | double value; | |
489 | if (availblks == 0) | |
490 | value = 100.0; | |
491 | else { | |
492 | value = (double)used / (double)availblks * 100.0; | |
493 | if ((value-(int)value) > 0.0) value = value + 1.0; | |
494 | } | |
495 | (void)printf(" %5.0f%%", trunc(value)); | |
496 | } else { | |
497 | (void)printf(" %5.0f%%", | |
498 | availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); | |
6c780a1f | 499 | } |
44a7a5ab | 500 | if (iflag) { |
864a4b6e | 501 | inodes = sfsp->f_files; |
44a7a5ab | 502 | used = inodes - sfsp->f_ffree; |
29341915 A |
503 | (void)printf(" %*llu %*llu %4.0f%% ", mwp->iused, used, |
504 | mwp->ifree, sfsp->f_ffree, inodes == 0 ? 100.0 : | |
6c780a1f A |
505 | (double)used / (double)inodes * 100.0); |
506 | } else | |
44a7a5ab A |
507 | (void)printf(" "); |
508 | (void)printf(" %s\n", sfsp->f_mntonname); | |
509 | } | |
510 | ||
6c780a1f A |
511 | /* |
512 | * Update the maximum field-width information in `mwp' based on | |
513 | * the filesystem specified by `sfsp'. | |
514 | */ | |
515 | void | |
516 | update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp) | |
517 | { | |
518 | static long blocksize; | |
519 | int dummy; | |
520 | ||
521 | if (blocksize == 0) | |
522 | getbsize(&dummy, &blocksize); | |
523 | ||
4d0bb651 | 524 | mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); |
29341915 | 525 | mwp->total = imax(mwp->total, int64width(fsbtoblk(sfsp->f_blocks, |
864a4b6e A |
526 | sfsp->f_bsize, blocksize, sfsp->f_mntonname))); |
527 | if (sfsp->f_blocks >= sfsp->f_bfree) | |
29341915 | 528 | mwp->used = imax(mwp->used, int64width(fsbtoblk(sfsp->f_blocks - |
864a4b6e | 529 | sfsp->f_bfree, sfsp->f_bsize, blocksize, sfsp->f_mntonname))); |
29341915 | 530 | mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, |
864a4b6e | 531 | sfsp->f_bsize, blocksize, sfsp->f_mntonname))); |
29341915 A |
532 | mwp->iused = imax(mwp->iused, int64width(sfsp->f_files - sfsp->f_ffree)); |
533 | mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); | |
6c780a1f A |
534 | } |
535 | ||
536 | /* Return the width in characters of the specified long. */ | |
537 | int | |
29341915 | 538 | int64width(int64_t val) |
6c780a1f A |
539 | { |
540 | int len; | |
541 | ||
542 | len = 0; | |
543 | /* Negative or zero values require one extra digit. */ | |
544 | if (val <= 0) { | |
545 | val = -val; | |
546 | len++; | |
547 | } | |
548 | while (val > 0) { | |
549 | len++; | |
550 | val /= 10; | |
551 | } | |
552 | ||
553 | return (len); | |
554 | } | |
555 | ||
44a7a5ab | 556 | void |
6c780a1f A |
557 | usage(void) |
558 | { | |
559 | ||
c59d3020 | 560 | char *t_flag = COMPAT_MODE("bin/df", "unix2003") ? "[-t]" : "[-t type]"; |
6c780a1f | 561 | (void)fprintf(stderr, |
64d2f73f | 562 | "usage: df [-b | -H | -h | -k | -m | -g | -P] [-ailn] [-T type] %s [filesystem ...]\n", t_flag); |
6c780a1f A |
563 | exit(EX_USAGE); |
564 | } | |
565 | ||
566 | char * | |
567 | makenetvfslist(void) | |
44a7a5ab | 568 | { |
6c780a1f A |
569 | char *str, *strptr, **listptr; |
570 | #ifndef __APPLE__ | |
571 | int mib[3], maxvfsconf, cnt=0, i; | |
572 | size_t miblen; | |
573 | struct ovfsconf *ptr; | |
574 | #else | |
575 | int mib[4], maxvfsconf, cnt=0, i; | |
576 | size_t miblen; | |
577 | struct vfsconf vfc; | |
578 | #endif | |
579 | ||
580 | mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM; | |
581 | miblen=sizeof(maxvfsconf); | |
00337e45 | 582 | if (sysctl(mib, 3, |
6c780a1f | 583 | &maxvfsconf, &miblen, NULL, 0)) { |
00337e45 | 584 | warn("sysctl failed"); |
6c780a1f A |
585 | return (NULL); |
586 | } | |
587 | ||
588 | if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { | |
589 | warnx("malloc failed"); | |
590 | return (NULL); | |
591 | } | |
592 | ||
593 | #ifndef __APPLE__ | |
594 | for (ptr = getvfsent(); ptr; ptr = getvfsent()) | |
595 | if (ptr->vfc_flags & VFCF_NETWORK) { | |
596 | listptr[cnt++] = strdup(ptr->vfc_name); | |
597 | if (listptr[cnt-1] == NULL) { | |
598 | warnx("malloc failed"); | |
599 | return (NULL); | |
600 | } | |
601 | } | |
602 | #else | |
603 | miblen = sizeof (struct vfsconf); | |
604 | mib[2] = VFS_CONF; | |
605 | for (i = 0; i < maxvfsconf; i++) { | |
606 | mib[3] = i; | |
607 | if (sysctl(mib, 4, &vfc, &miblen, NULL, 0) == 0) { | |
608 | if (!(vfc.vfc_flags & MNT_LOCAL)) { | |
609 | listptr[cnt++] = strdup(vfc.vfc_name); | |
610 | if (listptr[cnt-1] == NULL) { | |
00337e45 | 611 | free(listptr); |
6c780a1f A |
612 | warnx("malloc failed"); |
613 | return (NULL); | |
614 | } | |
615 | } | |
616 | } | |
617 | } | |
618 | #endif | |
619 | ||
620 | if (cnt == 0 || | |
621 | (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { | |
622 | if (cnt > 0) | |
623 | warnx("malloc failed"); | |
624 | free(listptr); | |
625 | return (NULL); | |
626 | } | |
627 | ||
628 | *str = 'n'; *(str + 1) = 'o'; | |
629 | for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { | |
630 | strncpy(strptr, listptr[i], 32); | |
631 | strptr += strlen(listptr[i]); | |
632 | *strptr = ','; | |
633 | free(listptr[i]); | |
634 | } | |
c59d3020 | 635 | *(--strptr) = '\0'; |
6c780a1f A |
636 | |
637 | free(listptr); | |
638 | return (str); | |
44a7a5ab | 639 | } |