df.c revision 1.96 1 /* $NetBSD: df.c,v 1.96 2020/08/21 16:40:02 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.96 2020/08/21 16:40:02 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 8
83 #define WIDTH_BLKSIZE 10
84
85 int
86 main(int argc, char *argv[])
87 {
88 struct stat stbuf;
89 struct statvfs *mntbuf;
90 int ch, maxwidth, width;
91 size_t i, mntcount;
92 char *mntpt;
93
94 setprogname(argv[0]);
95 (void)setlocale(LC_ALL, "");
96
97 while ((ch = getopt(argc, argv, "aGghiklmnPt:W")) != -1)
98 switch (ch) {
99 case 'a':
100 aflag = 1;
101 break;
102 case 'g':
103 hflag = 0;
104 usize = 1024 * 1024 * 1024;
105 break;
106 case 'G':
107 gflag = 1;
108 break;
109 case 'h':
110 hflag = 1;
111 usize = 0;
112 break;
113 case 'i':
114 iflag = 1;
115 break;
116 case 'k':
117 hflag = 0;
118 usize = 1024;
119 break;
120 case 'l':
121 lflag = 1;
122 break;
123 case 'm':
124 hflag = 0;
125 usize = 1024 * 1024;
126 break;
127 case 'n':
128 nflag = 1;
129 break;
130 case 'P':
131 Pflag = 1;
132 break;
133 case 'W':
134 Wflag = 1;
135 break;
136 case 't':
137 if (typelist != NULL)
138 errx(EXIT_FAILURE,
139 "only one -t option may be specified.");
140 maketypelist(optarg);
141 break;
142 case '?':
143 default:
144 usage();
145 }
146
147 if (gflag && (Pflag || iflag))
148 errx(EXIT_FAILURE,
149 "only one of -G and -P or -i may be specified");
150 if (Pflag && iflag)
151 errx(EXIT_FAILURE,
152 "only one of -P and -i may be specified");
153 #if 0
154 /*
155 * The block size cannot be checked until after getbsize() is called.
156 */
157 if (Pflag && (hflag || (usize != 1024 && usize != 512)))
158 errx(EXIT_FAILURE,
159 "non-standard block size incompatible with -P");
160 #endif
161 argc -= optind;
162 argv += optind;
163
164 mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
165 if (mntcount == 0)
166 err(EXIT_FAILURE,
167 "retrieving information on mounted file systems");
168
169 if (*argv == NULL) {
170 mntcount = regetmntinfo(&mntbuf, mntcount);
171 } else {
172 if ((mntbuf = calloc(argc, sizeof(*mntbuf))) == NULL)
173 err(EXIT_FAILURE, "can't allocate statvfs array");
174 mntcount = 0;
175 for (/*EMPTY*/; *argv != NULL; argv++) {
176 if (stat(*argv, &stbuf) < 0) {
177 if ((mntpt = getmntpt(*argv)) == 0) {
178 warn("%s", *argv);
179 continue;
180 }
181 } else if (S_ISBLK(stbuf.st_mode)) {
182 if ((mntpt = getmntpt(*argv)) == 0)
183 mntpt = *argv;
184 } else
185 mntpt = *argv;
186 /*
187 * Statfs does not take a `wait' flag, so we cannot
188 * implement nflag here.
189 */
190 if (!statvfs(mntpt, &mntbuf[mntcount]))
191 if (lflag &&
192 (mntbuf[mntcount].f_flag & MNT_LOCAL) == 0)
193 warnx("Warning: %s is not a local %s",
194 *argv, "file system");
195 else if
196 (!selected(mntbuf[mntcount].f_fstypename,
197 sizeof(mntbuf[mntcount].f_fstypename)))
198 warnx("Warning: %s mounted as a %s %s",
199 *argv,
200 mntbuf[mntcount].f_fstypename,
201 "file system");
202 else
203 ++mntcount;
204 else
205 warn("%s", *argv);
206 }
207 }
208
209 maxwidth = 0;
210 for (i = 0; i < mntcount; i++) {
211 width = (int)strlen(Wflag && mntbuf[i].f_mntfromlabel[0] ?
212 mntbuf[i].f_mntfromlabel : mntbuf[i].f_mntfromname);
213 if (width > maxwidth)
214 maxwidth = width;
215 }
216 for (i = 0; i < mntcount; i++)
217 prtstat(&mntbuf[i], maxwidth);
218 return 0;
219 }
220
221 static char *
222 getmntpt(const char *name)
223 {
224 size_t mntcount, i;
225 struct statvfs *mntbuf;
226
227 mntcount = getmntinfo(&mntbuf, MNT_NOWAIT);
228 if (mntcount == 0)
229 err(EXIT_FAILURE, "Can't get mount information");
230 for (i = 0; i < mntcount; i++) {
231 if (!strcmp(mntbuf[i].f_mntfromname, name))
232 return mntbuf[i].f_mntonname;
233 }
234 return 0;
235 }
236
237 static enum { IN_LIST, NOT_IN_LIST } which;
238
239 static int
240 selected(const char *type, size_t len)
241 {
242 char **av;
243
244 /* If no type specified, it's always selected. */
245 if (typelist == NULL)
246 return 1;
247 for (av = typelist; *av != NULL; ++av)
248 if (!strncmp(type, *av, len))
249 return which == IN_LIST ? 1 : 0;
250 return which == IN_LIST ? 0 : 1;
251 }
252
253 static void
254 maketypelist(char *fslist)
255 {
256 size_t i;
257 char *nextcp, **av;
258
259 if ((fslist == NULL) || (fslist[0] == '\0'))
260 errx(EXIT_FAILURE, "empty type list");
261
262 /*
263 * XXX
264 * Note: the syntax is "noxxx,yyy" for no xxx's and
265 * no yyy's, not the more intuitive "noyyy,noyyy".
266 */
267 if (fslist[0] == 'n' && fslist[1] == 'o') {
268 fslist += 2;
269 which = NOT_IN_LIST;
270 } else
271 which = IN_LIST;
272
273 /* Count the number of types. */
274 for (i = 1, nextcp = fslist;
275 (nextcp = strchr(nextcp, ',')) != NULL; i++)
276 ++nextcp;
277
278 /* Build an array of that many types. */
279 if ((av = typelist = calloc((i + 1), sizeof(*av))) == NULL)
280 err(EXIT_FAILURE, "can't allocate type array");
281 av[0] = fslist;
282 for (i = 1, nextcp = fslist;
283 (nextcp = strchr(nextcp, ',')) != NULL; i++) {
284 *nextcp = '\0';
285 av[i] = ++nextcp;
286 }
287 /* Terminate the array. */
288 av[i] = NULL;
289 }
290
291 /*
292 * Make a pass over the filesystem info in ``mntbuf'' filtering out
293 * filesystem types not in ``fsmask'' and possibly re-stating to get
294 * current (not cached) info. Returns the new count of valid statvfs bufs.
295 */
296 static size_t
297 regetmntinfo(struct statvfs **mntbufp, size_t mntcount)
298 {
299 size_t i, j;
300 struct statvfs *mntbuf;
301
302 if (!lflag && typelist == NULL && aflag)
303 return nflag ? mntcount : (size_t)getmntinfo(mntbufp, MNT_WAIT);
304
305 mntbuf = *mntbufp;
306 j = 0;
307 for (i = 0; i < mntcount; i++) {
308 if (!aflag && (mntbuf[i].f_flag & MNT_IGNORE) != 0)
309 continue;
310 if (lflag && (mntbuf[i].f_flag & MNT_LOCAL) == 0)
311 continue;
312 if (!selected(mntbuf[i].f_fstypename,
313 sizeof(mntbuf[i].f_fstypename)))
314 continue;
315 if (nflag)
316 mntbuf[j] = mntbuf[i];
317 else {
318 struct statvfs layerbuf = mntbuf[i];
319 (void)statvfs(mntbuf[i].f_mntonname, &mntbuf[j]);
320 /*
321 * If the FS name changed, then new data is for
322 * a different layer and we don't want it.
323 */
324 if (memcmp(layerbuf.f_mntfromname,
325 mntbuf[j].f_mntfromname, MNAMELEN))
326 mntbuf[j] = layerbuf;
327 }
328 j++;
329 }
330 return j;
331 }
332
333 static void
334 prthumanval(int64_t bytes, int width)
335 {
336 char buf[6];
337
338 (void)humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
339 bytes, "", HN_AUTOSCALE,
340 HN_B | HN_NOSPACE | HN_DECIMAL);
341
342 (void)printf("%*s", width, buf);
343 }
344
345 static void
346 prthuman(const struct statvfs *sfsp, int64_t used, int64_t bavail)
347 {
348
349 prthumanval((int64_t)(sfsp->f_blocks * sfsp->f_frsize), WIDTH_BLKSIZE);
350 prthumanval((int64_t)(used * sfsp->f_frsize), 1 + WIDTH_BLKSIZE);
351 prthumanval((int64_t)(bavail * sfsp->f_frsize), 1 + WIDTH_BLKSIZE);
352 }
353
354 /*
355 * Convert statvfs returned filesystem size into BLOCKSIZE units.
356 * Attempts to avoid overflow for large filesystems.
357 */
358 #define fsbtoblk(num, fsbs, bs) \
359 (((fsbs) != 0 && (uint64_t)(fsbs) < (uint64_t)(bs)) ? \
360 (int64_t)(num) / (int64_t)((bs) / (fsbs)) : \
361 (int64_t)(num) * (int64_t)((fsbs) / (bs)))
362
363 /*
364 * Print out status about a filesystem.
365 */
366 static void
367 prtstat(const struct statvfs *sfsp, int maxwidth)
368 {
369 static long blocksize;
370 static int headerlen, timesthrough;
371 static const char *header;
372 static const char full[] = "100";
373 static const char empty[] = " 0";
374 int64_t used, availblks, inodes;
375 int64_t bavail;
376 char pb[64];
377 char mntfromname[sizeof(sfsp->f_mntfromname) + 10];
378
379 if (Wflag && sfsp->f_mntfromlabel[0]) {
380 snprintf(mntfromname, sizeof(mntfromname), "NAME=%s",
381 sfsp->f_mntfromlabel);
382 } else {
383 strlcpy(mntfromname, sfsp->f_mntfromname, sizeof(mntfromname));
384 }
385
386 if (gflag) {
387 /*
388 * From SunOS-5.6:
389 *
390 * /var (/dev/dsk/c0t0d0s3 ): 8192 block size 1024 frag size
391 * 984242 total blocks 860692 free blocks 859708 available 249984 total files
392 * 248691 free files 8388611 filesys id
393 * ufs fstype 0x00000004 flag 255 filename length
394 *
395 */
396 (void)printf("%10s (%-12s): %7ld block size %12ld frag size\n",
397 sfsp->f_mntonname, mntfromname,
398 sfsp->f_bsize, /* On UFS/FFS systems this is
399 * also called the "optimal
400 * transfer block size" but it
401 * is of course the file
402 * system's block size too.
403 */
404 sfsp->f_frsize); /* not so surprisingly the
405 * "fundamental file system
406 * block size" is the frag
407 * size.
408 */
409 (void)printf("%10" PRId64 " total blocks %10" PRId64
410 " free blocks %10" PRId64 " available\n",
411 (uint64_t)sfsp->f_blocks, (uint64_t)sfsp->f_bfree,
412 (uint64_t)sfsp->f_bavail);
413 (void)printf("%10" PRId64 " total files %10" PRId64
414 " free files %12lx filesys id\n",
415 (uint64_t)sfsp->f_ffree, (uint64_t)sfsp->f_files,
416 sfsp->f_fsid);
417 (void)printf("%10s fstype %#15lx flag %17ld filename "
418 "length\n", sfsp->f_fstypename, sfsp->f_flag,
419 sfsp->f_namemax);
420 (void)printf("%10lu owner %17" PRId64 " syncwrites %12" PRId64
421 " asyncwrites\n\n", (unsigned long)sfsp->f_owner,
422 sfsp->f_syncwrites, sfsp->f_asyncwrites);
423
424 /*
425 * a concession by the structured programming police to the
426 * indentation police....
427 */
428 return;
429 }
430 if (maxwidth < 12)
431 maxwidth = 12;
432 if (++timesthrough == 1) {
433 switch (blocksize = usize) {
434 case 1024:
435 header = Pflag ? "1024-blocks" : "1K-blocks";
436 headerlen = (int)strlen(header);
437 break;
438 case 1024 * 1024:
439 header = "1M-blocks";
440 headerlen = (int)strlen(header);
441 break;
442 case 1024 * 1024 * 1024:
443 header = "1G-blocks";
444 headerlen = (int)strlen(header);
445 break;
446 default:
447 if (hflag) {
448 header = "Size";
449 headerlen = (int)strlen(header);
450 } else
451 header = getbsize(&headerlen, &blocksize);
452 break;
453 }
454 if (Pflag) {
455 /*
456 * either:
457 * "Filesystem 1024-blocks Used Available Capacity Mounted on\n"
458 * or:
459 * "Filesystem 512-blocks Used Available Capacity Mounted on\n"
460 */
461 if (blocksize != 1024 && blocksize != 512)
462 errx(EXIT_FAILURE,
463 "non-standard block size incompatible with -P");
464 (void)printf("Filesystem %s Used Available Capacity "
465 "Mounted on\n", header);
466 } else {
467 (void)printf("%-*.*s %*s %*s %*s %%Cap",
468 maxwidth, maxwidth, "Filesystem",
469 WIDTH_BLKSIZE, header,
470 WIDTH_BLKSIZE, "Used",
471 WIDTH_BLKSIZE, "Avail");
472 if (iflag) {
473 (void)printf(" %*s %*s %%iCap",
474 WIDTH_INODE, "iUsed",
475 WIDTH_INODE, "iAvail");
476 }
477 (void)printf(" Mounted on\n");
478 }
479 }
480 used = sfsp->f_blocks - sfsp->f_bfree;
481 bavail = sfsp->f_bfree - sfsp->f_bresvd;
482 availblks = bavail + used;
483 if (Pflag) {
484 assert(hflag == 0);
485 assert(blocksize > 0);
486 /*
487 * "%s %d %d %d %s %s\n", <file system name>, <total space>,
488 * <space used>, <space free>, <percentage used>,
489 * <file system root>
490 */
491 (void)printf("%s %" PRId64 " %" PRId64 " %" PRId64 " %s%% %s\n",
492 mntfromname,
493 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
494 fsbtoblk(used, sfsp->f_frsize, blocksize),
495 fsbtoblk(bavail, sfsp->f_frsize, blocksize),
496 availblks == 0 ? full : strspct(pb, sizeof(pb), used,
497 availblks, 0), sfsp->f_mntonname);
498 /*
499 * another concession by the structured programming police to
500 * the indentation police....
501 *
502 * Note iflag cannot be set when Pflag is set.
503 */
504 return;
505 }
506
507 (void)printf("%-*.*s ", maxwidth, maxwidth, mntfromname);
508
509 if (hflag)
510 prthuman(sfsp, used, bavail);
511 else
512 (void)printf("%*" PRId64 " %*" PRId64 " %*" PRId64,
513 WIDTH_BLKSIZE,
514 fsbtoblk(sfsp->f_blocks, sfsp->f_frsize, blocksize),
515 WIDTH_BLKSIZE, fsbtoblk(used, sfsp->f_frsize, blocksize),
516 WIDTH_BLKSIZE, fsbtoblk(bavail, sfsp->f_frsize, blocksize));
517 (void)printf(" %3s%%",
518 availblks == 0 ? full :
519 strspct(pb, sizeof(pb), used, availblks, 0));
520 if (iflag) {
521 inodes = sfsp->f_files;
522 used = inodes - sfsp->f_ffree;
523 (void)printf(" %*jd %*jd %4s%%",
524 WIDTH_INODE, (intmax_t)used,
525 WIDTH_INODE, (intmax_t)sfsp->f_ffree,
526 inodes == 0 ? (used == 0 ? empty : full) :
527 strspct(pb, sizeof(pb), used, inodes, 0));
528 }
529 (void)printf(" %s\n", sfsp->f_mntonname);
530 }
531
532 static void
533 usage(void)
534 {
535
536 (void)fprintf(stderr,
537 "Usage: %s [-aglnW] [-Ghkm|-ihkm|-Pk] [-t type] [file | "
538 "file_system ...]\n",
539 getprogname());
540 exit(1);
541 /* NOTREACHED */
542 }
543