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