df.c revision 1.97 1 /* $NetBSD: df.c,v 1.97 2020/08/21 16:41:06 ryo Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT(
40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\
41 The Regents of the University of California. All rights reserved.");
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94";
47 #else
48 __RCSID("$NetBSD: df.c,v 1.97 2020/08/21 16:41:06 ryo Exp $");
49 #endif
50 #endif /* not lint */
51
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55
56 #include <assert.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <stdbool.h>
60 #include <fcntl.h>
61 #include <locale.h>
62 #include <util.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <util.h>
68
69 static char *getmntpt(const char *);
70 static void prtstat(const struct statvfs *, int);
71 static int selected(const char *, size_t);
72 static void maketypelist(char *);
73 static size_t regetmntinfo(struct statvfs **, size_t);
74 __dead static void usage(void);
75 static void prthumanval(int64_t, int);
76 static void prthuman(const struct statvfs *, int64_t, int64_t);
77
78 static int aflag, gflag, hflag, iflag, lflag, nflag, Pflag, Wflag;
79 static long usize;
80 static char **typelist;
81
82 #define WIDTH_INODE 10
83 #define WIDTH_BLKSIZE 12
84 static int blksize_width = WIDTH_BLKSIZE;
85
86 int
87 main(int argc, char *argv[])
88 {
89 struct stat stbuf;
90 struct statvfs *mntbuf;
91 int ch, maxwidth, width;
92 size_t i, mntcount;
93 char *mntpt;
94
95 setprogname(argv[0]);
96 (void)setlocale(LC_ALL, "");
97
98 while ((ch = getopt(argc, argv, "aGghiklmnPt:W")) != -1)
99 switch (ch) {
100 case 'a':
101 aflag = 1;
102 break;
103 case 'g':
104 hflag = 0;
105 usize = 1024 * 1024 * 1024;
106 break;
107 case 'G':
108 gflag = 1;
109 break;
110 case 'h':
111 hflag = 1;
112 usize = 0;
113 break;
114 case 'i':
115 iflag = 1;
116 break;
117 case 'k':
118 hflag = 0;
119 usize = 1024;
120 break;
121 case 'l':
122 lflag = 1;
123 break;
124 case 'm':
125 hflag = 0;
126 usize = 1024 * 1024;
127 break;
128 case 'n':
129 nflag = 1;
130 break;
131 case 'P':
132 Pflag = 1;
133 break;
134 case 'W':
135 Wflag = 1;
136 break;
137 case 't':
138 if (typelist != NULL)
139 errx(EXIT_FAILURE,
140 "only one -t option may be specified.");
141 maketypelist(optarg);
142 break;
143 case '?':
144 default:
145 usage();
146 }
147
148 if (gflag && (Pflag || iflag))
149 errx(EXIT_FAILURE,
150 "only one of -G and -P or -i may be specified");
151 if (Pflag && iflag)
152 errx(EXIT_FAILURE,
153 "only one of -P and -i may be specified");
154 #if 0
155 /*
156 * The block size cannot be checked until after getbsize() is called.
157 */
158 if (Pflag && (hflag || (usize != 1024 && usize != 512)))
159 errx(EXIT_FAILURE,
160 "non-standard block size incompatible with -P");
161 #endif
162 argc -= optind;
163 argv += optind;
164
165 mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
166 if (mntcount == 0)
167 err(EXIT_FAILURE,
168 "retrieving information on mounted file systems");
169
170 if (*argv == NULL) {
171 mntcount = regetmntinfo(&mntbuf, mntcount);
172 } else {
173 if ((mntbuf = calloc(argc, sizeof(*mntbuf))) == NULL)
174 err(EXIT_FAILURE, "can't allocate statvfs array");
175 mntcount = 0;
176 for (/*EMPTY*/; *argv != NULL; argv++) {
177 if (stat(*argv, &stbuf) < 0) {
178 if ((mntpt = getmntpt(*argv)) == 0) {
179 warn("%s", *argv);
180 continue;
181 }
182 } else if (S_ISBLK(stbuf.st_mode)) {
183 if ((mntpt = getmntpt(*argv)) == 0)
184 mntpt = *argv;
185 } else
186 mntpt = *argv;
187 /*
188 * Statfs does not take a `wait' flag, so we cannot
189 * implement nflag here.
190 */
191 if (!statvfs(mntpt, &mntbuf[mntcount]))
192 if (lflag &&
193 (mntbuf[mntcount].f_flag & MNT_LOCAL) == 0)
194 warnx("Warning: %s is not a local %s",
195 *argv, "file system");
196 else if
197 (!selected(mntbuf[mntcount].f_fstypename,
198 sizeof(mntbuf[mntcount].f_fstypename)))
199 warnx("Warning: %s mounted as a %s %s",
200 *argv,
201 mntbuf[mntcount].f_fstypename,
202 "file system");
203 else
204 ++mntcount;
205 else
206 warn("%s", *argv);
207 }
208 }
209
210 maxwidth = 0;
211 for (i = 0; i < mntcount; i++) {
212 width = (int)strlen(Wflag && mntbuf[i].f_mntfromlabel[0] ?
213 mntbuf[i].f_mntfromlabel : mntbuf[i].f_mntfromname);
214 if (width > maxwidth)
215 maxwidth = width;
216 }
217 for (i = 0; i < mntcount; i++)
218 prtstat(&mntbuf[i], maxwidth);
219 return 0;
220 }
221
222 static char *
223 getmntpt(const char *name)
224 {
225 size_t mntcount, i;
226 struct statvfs *mntbuf;
227
228 mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
229 if (mntcount == 0)
230 err(EXIT_FAILURE, "Can't get mount information");
231 for (i = 0; i < mntcount; i++) {
232 if (!strcmp(mntbuf[i].f_mntfromname, name))
233 return mntbuf[i].f_mntonname;
234 }
235 return 0;
236 }
237
238 static enum { IN_LIST, NOT_IN_LIST } which;
239
240 static int
241 selected(const char *type, size_t len)
242 {
243 char **av;
244
245 /* If no type specified, it's always selected. */
246 if (typelist == NULL)
247 return 1;
248 for (av = typelist; *av != NULL; ++av)
249 if (!strncmp(type, *av, len))
250 return which == IN_LIST ? 1 : 0;
251 return which == IN_LIST ? 0 : 1;
252 }
253
254 static void
255 maketypelist(char *fslist)
256 {
257 size_t i;
258 char *nextcp, **av;
259
260 if ((fslist == NULL) || (fslist[0] == '\0'))
261 errx(EXIT_FAILURE, "empty type list");
262
263 /*
264 * XXX
265 * Note: the syntax is "noxxx,yyy" for no xxx's and
266 * no yyy's, not the more intuitive "noyyy,noyyy".
267 */
268 if (fslist[0] == 'n' && fslist[1] == 'o') {
269 fslist += 2;
270 which = NOT_IN_LIST;
271 } else
272 which = IN_LIST;
273
274 /* Count the number of types. */
275 for (i = 1, nextcp = fslist;
276 (nextcp = strchr(nextcp, ',')) != NULL; i++)
277 ++nextcp;
278
279 /* Build an array of that many types. */
280 if ((av = typelist = calloc((i + 1), sizeof(*av))) == NULL)
281 err(EXIT_FAILURE, "can't allocate type array");
282 av[0] = fslist;
283 for (i = 1, nextcp = fslist;
284 (nextcp = strchr(nextcp, ',')) != NULL; i++) {
285 *nextcp = '\0';
286 av[i] = ++nextcp;
287 }
288 /* Terminate the array. */
289 av[i] = NULL;
290 }
291
292 /*
293 * Make a pass over the filesystem info in ``mntbuf'' filtering out
294 * filesystem types not in ``fsmask'' and possibly re-stating to get
295 * current (not cached) info. Returns the new count of valid statvfs bufs.
296 */
297 static size_t
298 regetmntinfo(struct statvfs **mntbufp, size_t mntcount)
299 {
300 size_t i, j;
301 struct statvfs *mntbuf;
302
303 if (!lflag && typelist == NULL && aflag)
304 return nflag ? mntcount : (size_t)getmntinfo(mntbufp, MNT_WAIT);
305
306 mntbuf = *mntbufp;
307 j = 0;
308 for (i = 0; i < mntcount; i++) {
309 if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0)
310 continue;
311 if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0)
312 continue;
313 if (!selected(mntbuf[i].f_fstypename,
314 sizeof(mntbuf[i].f_fstypename)))
315 continue;
316 if (nflag)
317 mntbuf[j] = mntbuf[i];
318 else {
319 struct statvfs layerbuf = mntbuf[i];
320 (void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]);
321 /*
322 * If the FS name changed, then new data is for
323 * a different layer and we don't want it.
324 */
325 if (memcmp(layerbuf.f_mntfromname,
326 mntbuf[j].f_mntfromname, MNAMELEN))
327 mntbuf[j] = layerbuf;
328 }
329 j++;
330 }
331 return j;
332 }
333
334 static void
335 prthumanval(int64_t bytes, int width)
336 {
337 char buf[6];
338
339 (void)humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
340 bytes, "", HN_AUTOSCALE,
341 HN_B | HN_NOSPACE | HN_DECIMAL);
342
343 (void)printf("%*s", width, buf);
344 }
345
346 static void
347 prthuman(const struct statvfs *sfsp, int64_t used, int64_t bavail)
348 {
349
350 prthumanval((int64_t)(sfsp->f_blocks * sfsp->f_frsize), blksize_width);
351 prthumanval((int64_t)(used * sfsp->f_frsize), 1 + blksize_width);
352 prthumanval((int64_t)(bavail * sfsp->f_frsize), 1 + blksize_width);
353 }
354
355 /*
356 * Convert statvfs returned filesystem size into BLOCKSIZE units.
357 * Attempts to avoid overflow for large filesystems.
358 */
359 #define fsbtoblk(num, fsbs, bs) \
360 (((fsbs) != 0 && (uint64_t)(fsbs) < (uint64_t)(bs)) ? \
361 (int64_t)(num) / (int64_t)((bs) / (fsbs)) : \
362 (int64_t)(num) * (int64_t)((fsbs) / (bs)))
363
364 /*
365 * Print out status about a filesystem.
366 */
367 static void
368 prtstat(const struct statvfs *sfsp, int maxwidth)
369 {
370 static long blocksize;
371 static int headerlen, timesthrough;
372 static const char *header;
373 static const char full[] = "100";
374 static const char empty[] = " 0";
375 int64_t used, availblks, inodes;
376 int64_t bavail;
377 char pb[64];
378 char mntfromname[sizeof(sfsp->f_mntfromname) + 10];
379
380 if (Wflag && sfsp->f_mntfromlabel[0]) {
381 snprintf(mntfromname, sizeof(mntfromname), "NAME=%s",
382 sfsp->f_mntfromlabel);
383 } else {
384 strlcpy(mntfromname, sfsp->f_mntfromname, sizeof(mntfromname));
385 }
386
387 if (gflag) {
388 /*
389 * From SunOS-5.6:
390 *
391 * /var (/dev/dsk/c0t0d0s3 ): 8192 block size 1024 frag size
392 * 984242 total blocks 860692 free blocks 859708 available 249984 total files
393 * 248691 free files 8388611 filesys id
394 * ufs fstype 0x00000004 flag 255 filename length
395 *
396 */
397 (void)printf("%10s (%-12s): %7ld block size %12ld frag size\n",
398 sfsp->f_mntonname, mntfromname,
399 sfsp->f_bsize, /* On UFS/FFS systems this is
400 * also called the "optimal
401 * transfer block size" but it
402 * is of course the file
403 * system's block size too.
404 */
405 sfsp->f_frsize); /* not so surprisingly the
406 * "fundamental file system
407 * block size" is the frag
408 * size.
409 */
410 (void)printf("%10" PRId64 " total blocks %10" PRId64
411 " free blocks %10" PRId64 " available\n",
412 (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree,
413 (uint64_t)sfsp->f_bavail);
414 (void)printf("%10" PRId64 " total files %10" PRId64
415 " free files %12lx filesys id\n",
416 (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files,
417 sfsp->f_fsid);
418 (void)printf("%10s fstype %#15lx flag %17ld filename "
419 "length\n", sfsp->f_fstypename, sfsp->f_flag,
420 sfsp->f_namemax);
421 (void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64
422 " asyncwrites\n\n", (unsigned long)sfsp->f_owner,
423 sfsp->f_syncwrites, sfsp->f_asyncwrites);
424
425 /*
426 * a concession by the structured programming police to the
427 * indentation police....
428 */
429 return;
430 }
431 if (maxwidth < 12)
432 maxwidth = 12;
433 if (++timesthrough == 1) {
434 switch (blocksize = usize) {
435 case 1024:
436 header = Pflag ? "1024-blocks" : "1K-blocks";
437 headerlen = (int)strlen(header);
438 break;
439 case 1024 * 1024:
440 header = "1M-blocks";
441 headerlen = (int)strlen(header);
442 break;
443 case 1024 * 1024 * 1024:
444 header = "1G-blocks";
445 headerlen = (int)strlen(header);
446 break;
447 default:
448 if (hflag) {
449 header = "Size";
450 headerlen = (int)strlen(header);
451 blksize_width = 6;
452 } else
453 header = getbsize(&headerlen, &blocksize);
454 break;
455 }
456
457 if (blocksize >= 1024 * 1024)
458 blksize_width -= 3;
459 if (blocksize >= 1024 * 1024 * 1024)
460 blksize_width -= 3;
461 if (blksize_width < headerlen)
462 blksize_width = headerlen;
463
464 if (Pflag) {
465 /*
466 * either:
467 * "Filesystem 1024-blocks Used Available Capacity Mounted on\n"
468 * or:
469 * "Filesystem 512-blocks Used Available Capacity Mounted on\n"
470 */
471 if (blocksize != 1024 && blocksize != 512)
472 errx(EXIT_FAILURE,
473 "non-standard block size incompatible with -P");
474 (void)printf("Filesystem %s Used Available Capacity "
475 "Mounted on\n", header);
476 } else {
477 (void)printf("%-*.*s %*s %*s %*s %%Cap",
478 maxwidth, maxwidth, "Filesystem",
479 blksize_width, header,
480 blksize_width, "Used",
481 blksize_width, "Avail");
482 if (iflag) {
483 (void)printf(" %*s %*s %%iCap",
484 WIDTH_INODE, "iUsed",
485 WIDTH_INODE, "iAvail");
486 }
487 (void)printf(" Mounted on\n");
488 }
489 }
490 used = sfsp->f_blocks - sfsp->f_bfree;
491 bavail = sfsp->f_bfree - sfsp->f_bresvd;
492 availblks = bavail + used;
493 if (Pflag) {
494 assert(hflag == 0);
495 assert(blocksize > 0);
496 /*
497 * "%s %d %d %d %s %s\n", <file system name>, <total space>,
498 * <space used>, <space free>, <percentage used>,
499 * <file system root>
500 */
501 (void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s%% %s\n",
502 mntfromname,
503 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
504 fsbtoblk(used, sfsp->f_frsize, blocksize),
505 fsbtoblk(bavail, sfsp->f_frsize, blocksize),
506 availblks == 0 ? full : strspct(pb, sizeof(pb), used,
507 availblks, 0), sfsp->f_mntonname);
508 /*
509 * another concession by the structured programming police to
510 * the indentation police....
511 *
512 * Note iflag cannot be set when Pflag is set.
513 */
514 return;
515 }
516
517 (void)printf("%-*.*s ", maxwidth, maxwidth, mntfromname);
518
519 if (hflag)
520 prthuman(sfsp, used, bavail);
521 else
522 (void)printf("%*" PRId64 " %*" PRId64 " %*" PRId64,
523 blksize_width,
524 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
525 blksize_width, fsbtoblk(used, sfsp->f_frsize, blocksize),
526 blksize_width, fsbtoblk(bavail, sfsp->f_frsize, blocksize));
527 (void)printf(" %3s%%",
528 availblks == 0 ? full :
529 strspct(pb, sizeof(pb), used, availblks, 0));
530 if (iflag) {
531 inodes = sfsp->f_files;
532 used = inodes - sfsp->f_ffree;
533 (void)printf(" %*jd %*jd %4s%%",
534 WIDTH_INODE, (intmax_t)used,
535 WIDTH_INODE, (intmax_t)sfsp->f_ffree,
536 inodes == 0 ? (used == 0 ? empty : full) :
537 strspct(pb, sizeof(pb), used, inodes, 0));
538 }
539 (void)printf(" %s\n", sfsp->f_mntonname);
540 }
541
542 static void
543 usage(void)
544 {
545
546 (void)fprintf(stderr,
547 "Usage: %s [-aglnW] [-Ghkm|-ihkm|-Pk] [-t type] [file | "
548 "file_system ...]\n",
549 getprogname());
550 exit(1);
551 /* NOTREACHED */
552 }
553