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