1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #pragma ident "%Z%%M% %I% %E% SMI" 41 42 /* 43 * df 44 */ 45 #include <stdio.h> 46 #include <fcntl.h> 47 #include <sys/param.h> 48 #include <sys/types.h> 49 #include <sys/mntent.h> 50 #include <sys/fs/ufs_fs.h> 51 #include <sys/stat.h> 52 #include <sys/vfs.h> 53 #include <sys/file.h> 54 #include <sys/statvfs.h> 55 #include <sys/mnttab.h> 56 #include <sys/mkdev.h> 57 #include <locale.h> 58 #include <stdarg.h> 59 #include <string.h> 60 #include <errno.h> 61 #include <libintl.h> 62 63 extern char *getenv(); 64 extern char *getcwd(); 65 extern char *realpath(); 66 extern off_t lseek(); 67 68 /* 69 * Raw name to block device name translation function. 70 * This comes from libadm. 71 */ 72 extern char *getfullblkname(); 73 74 static void usage(), pheader(); 75 static char *mpath(), *zap_chroot(); 76 static char *pathsuffix(); 77 static char *xmalloc(); 78 static int chroot_stat(); 79 static int bread(); 80 static int abspath(), subpath(); 81 static void show_inode_usage(); 82 static void dfreedev(); 83 static void dfreemnt(); 84 static void print_totals(); 85 static void print_itotals(); 86 static void print_statvfs(); 87 static struct mntlist *mkmntlist(); 88 static struct mnttab *mntdup(), *mdev(char *); 89 static struct mntlist *findmntent(); 90 91 #define bcopy(f, t, n) memcpy(t, f, n) 92 #define bzero(s, n) memset(s, 0, n) 93 #define bcmp(s, d, n) memcmp(s, d, n) 94 95 #define index(s, r) strchr(s, r) 96 #define rindex(s, r) strrchr(s, r) 97 98 #define dbtok(x, b) \ 99 ((b) < (fsblkcnt64_t)1024 ? \ 100 (x) / ((fsblkcnt64_t)1024 / (b)) : (x) * ((b) / (fsblkcnt64_t)1024)) 101 102 int aflag = 0; /* even the uninteresting ones */ 103 int bflag = 0; /* print only number of kilobytes free */ 104 int eflag = 0; /* print only number of file entries free */ 105 int gflag = 0; /* print entire statvfs structure */ 106 int hflag = 0; /* don't print header */ 107 int iflag = 0; /* information for inodes */ 108 int nflag = 0; /* print VFStype name */ 109 int tflag = 0; /* print totals */ 110 int errflag = 0; 111 int errcode = 0; 112 char *typestr = "ufs"; 113 fsblkcnt64_t t_totalblks, t_avail, t_free, t_used, t_reserved; 114 int t_inodes, t_iused, t_ifree; 115 116 /* 117 * cached information recording previous chroot history. 118 */ 119 static char *chrootpath; 120 121 extern int optind; 122 extern char *optarg; 123 124 union { 125 struct fs iu_fs; 126 char dummy[SBSIZE]; 127 } sb; 128 #define sblock sb.iu_fs 129 130 /* 131 * This structure is used to chain mntent structures into a list 132 * and to cache stat information for each member of the list. 133 */ 134 struct mntlist { 135 struct mnttab *mntl_mnt; 136 struct mntlist *mntl_next; 137 dev_t mntl_dev; 138 int mntl_devvalid; 139 }; 140 141 char *subopts [] = { 142 #define A_FLAG 0 143 "a", 144 #define I_FLAG 1 145 "i", 146 NULL 147 }; 148 149 void 150 main(argc, argv) 151 int argc; 152 char *argv[]; 153 { 154 struct mnttab mnt; 155 int opt; 156 char *suboptions, *value; 157 158 (void) setlocale(LC_ALL, ""); 159 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 160 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 161 #endif 162 (void) textdomain(TEXT_DOMAIN); 163 164 while ((opt = getopt(argc, argv, "beghkno:t")) != EOF) { 165 switch (opt) { 166 167 case 'b': /* print only number of kilobytes free */ 168 bflag++; 169 break; 170 171 case 'e': 172 eflag++; /* print only number of file entries free */ 173 iflag++; 174 break; 175 176 case 'g': 177 gflag++; 178 break; 179 180 case 'n': 181 nflag++; 182 break; 183 184 case 'k': 185 break; 186 187 case 'h': 188 hflag++; 189 break; 190 191 case 'o': 192 /* 193 * ufs specific options. 194 */ 195 suboptions = optarg; 196 while (*suboptions != '\0') { 197 switch (getsubopt(&suboptions, 198 subopts, &value)) { 199 200 case I_FLAG: /* information for inodes */ 201 iflag++; 202 break; 203 204 default: 205 usage(); 206 } 207 } 208 break; 209 210 case 't': /* print totals */ 211 tflag++; 212 break; 213 214 case 'V': /* Print command line */ 215 { 216 char *opt_text; 217 int opt_count; 218 219 (void) fprintf(stdout, "df -F ufs "); 220 for (opt_count = 1; opt_count < argc; 221 opt_count++) { 222 opt_text = argv[opt_count]; 223 if (opt_text) 224 (void) fprintf(stdout, " %s ", 225 opt_text); 226 } 227 (void) fprintf(stdout, "\n"); 228 } 229 break; 230 231 case '?': 232 errflag++; 233 } 234 } 235 if (errflag) 236 usage(); 237 if (gflag && iflag) { 238 printf(gettext("df: '-g' and '-o i' are mutually exclusive\n")); 239 exit(1); 240 } 241 if (bflag || eflag) 242 tflag = 0; 243 244 /* 245 * Cache CHROOT information for later use; assume that $CHROOT matches 246 * the cumulative arguments given to chroot calls. 247 */ 248 chrootpath = getenv("CHROOT"); 249 if (chrootpath != NULL && strcmp(chrootpath, "/") == 0) 250 chrootpath = NULL; 251 252 if (argc <= optind) { 253 /* 254 * Take this path when "/usr/lib/fs/ufs/df" is specified, and 255 * there are no mountpoints specified. 256 * E.g., these command lines take us down this path 257 * /usr/lib/fs/ufs/df -o i 258 * /usr/lib/fs/ufs/df 259 */ 260 register FILE *mtabp; 261 262 if ((mtabp = fopen(MNTTAB, "r")) == NULL) { 263 (void) fprintf(stderr, "df: "); 264 perror(MNTTAB); 265 exit(1); 266 } 267 pheader(); 268 while (getmntent(mtabp, &mnt) == 0) { 269 if (strcmp(typestr, mnt.mnt_fstype) != 0) { 270 continue; 271 } 272 dfreemnt(mnt.mnt_mountp, &mnt); 273 } 274 if (tflag) 275 if (iflag) 276 print_itotals(); 277 else 278 print_totals(); 279 (void) fclose(mtabp); 280 } else { 281 int i; 282 struct mntlist *mntl; 283 struct stat64 *argstat; 284 char **devnames; 285 char *cp; 286 287 /* 288 * Obtain stat64 information for each argument before 289 * constructing the list of mounted file systems. This 290 * ordering forces the automounter to establish any 291 * mounts required to access the arguments, so that the 292 * corresponding mount table entries will exist when 293 * we look for them. 294 */ 295 argv++; 296 argc--; 297 argstat = (struct stat64 *)xmalloc(argc * sizeof (*argstat)); 298 devnames = (char **)xmalloc(argc * sizeof (char *)); 299 for (i = 0; i < argc; i++) { 300 301 /* 302 * Given a raw device name, get the block device name 303 */ 304 cp = getfullblkname(argv[i]); 305 if (cp == NULL || *cp == '\0') { 306 if (cp != NULL) 307 free(cp); 308 cp = strdup(argv[i]); 309 310 if (cp == NULL) { 311 int j; 312 313 fprintf(stderr, gettext( 314 "df: memory allocation failure\n")); 315 316 for (j = 0; j < i; j++) 317 free(devnames[j]); 318 free(devnames); 319 free(argstat); 320 exit(1); 321 } 322 } 323 if (stat64(cp, &argstat[i]) < 0) { 324 errcode = errno; 325 /* 326 * Mark as no longer interesting. 327 */ 328 argv[i] = NULL; 329 devnames[i] = NULL; 330 free(cp); 331 } else { 332 devnames[i] = cp; 333 } 334 } 335 336 pheader(); 337 aflag++; 338 /* 339 * Construct the list of mounted file systems. 340 */ 341 mntl = mkmntlist(); 342 343 /* 344 * Iterate through the argument list, reporting on each one. 345 */ 346 for (i = 0; i < argc; i++) { 347 register struct mntlist *mlp; 348 int isblk; 349 350 /* 351 * Skip if we've already determined that we can't 352 * process it. 353 */ 354 if (argv[i] == NULL) 355 continue; 356 357 /* 358 * If the argument names a device, report on the file 359 * system associated with the device rather than on 360 * the one containing the device's directory entry 361 */ 362 cp = devnames[i]; 363 if ((isblk = (argstat[i].st_mode&S_IFMT) == S_IFBLK) || 364 (argstat[i].st_mode & S_IFMT) == S_IFCHR) { 365 if (isblk && strcmp(mpath(cp), "") != 0) { 366 struct mnttab *mp = mdev(cp); 367 dfreemnt(mp->mnt_mountp, mp); 368 } else { 369 dfreedev(cp); 370 } 371 free(cp); 372 devnames[i] = NULL; 373 continue; 374 } 375 376 /* 377 * Get this argument's corresponding mount table 378 * entry. 379 */ 380 mlp = findmntent(cp, &argstat[i], mntl); 381 free(cp); 382 devnames[i] = NULL; 383 384 if (mlp == NULL) { 385 (void) fprintf(stderr, 386 gettext("Could not find mount point for %s\n"), 387 argv[i]); 388 continue; 389 } 390 391 dfreemnt(mlp->mntl_mnt->mnt_mountp, mlp->mntl_mnt); 392 } 393 free(devnames); 394 free(argstat); 395 } 396 exit(0); 397 /*NOTREACHED*/ 398 } 399 400 void 401 pheader() 402 { 403 if (hflag) 404 return; 405 if (nflag) 406 (void) printf(gettext("VFStype name - ufs\n")); 407 if (iflag) { 408 if (eflag) 409 /* 410 * TRANSLATION_NOTE 411 * Following string is used as a table header. 412 * Translated items should start at the same 413 * columns as the original items. 414 */ 415 (void) printf(gettext( 416 "Filesystem ifree\n")); 417 else { 418 /* 419 * TRANSLATION_NOTE 420 * Following string is used as a table header. 421 * Translated items should start at the same 422 * columns as the original items. 423 */ 424 (void) printf(gettext( 425 "Filesystem iused ifree %%iused Mounted on\n")); 426 } 427 } else { 428 if (gflag) 429 /* 430 * TRANSLATION_NOTE 431 * Following string is used as a table header. 432 * Translated items should start at the same 433 * columns as the original items. 434 */ 435 (void) printf(gettext( 436 "Filesystem f_type f_fsize f_bfree f_bavail f_files f_ffree " 437 "f_fsid f_flag f_fstr\n")); 438 else 439 if (bflag) 440 /* 441 * TRANSLATION_NOTE 442 * Following string is used as a table header. 443 * Translated items should start at the same 444 * columns as the original items. 445 */ 446 (void) printf(gettext( 447 "Filesystem avail\n")); 448 else { 449 /* 450 * TRANSLATION_NOTE 451 * Following string is used as a table header. 452 * Translated items should start at the same 453 * columns as the original items. 454 */ 455 (void) printf(gettext( 456 "Filesystem kbytes used avail capacity Mounted on\n")); 457 } 458 } 459 } 460 461 /* 462 * Report on a block or character special device. Assumed not to be 463 * mounted. N.B. checks for a valid UFS superblock. 464 */ 465 void 466 dfreedev(file) 467 char *file; 468 { 469 fsblkcnt64_t totalblks, availblks, avail, free, used; 470 int fi; 471 472 fi = open64(file, 0); 473 if (fi < 0) { 474 (void) fprintf(stderr, "df: "); 475 perror(file); 476 return; 477 } 478 if (bread(file, fi, SBLOCK, (char *)&sblock, SBSIZE) == 0) { 479 (void) close(fi); 480 return; 481 } 482 if ((sblock.fs_magic != FS_MAGIC) && 483 (sblock.fs_magic != MTB_UFS_MAGIC)) { 484 (void) fprintf(stderr, gettext( 485 "df: %s: not a ufs file system\n"), 486 file); 487 (void) close(fi); 488 return; 489 } 490 if (sblock.fs_magic == MTB_UFS_MAGIC && 491 (sblock.fs_version > MTB_UFS_VERSION_1 || 492 sblock.fs_version < MTB_UFS_VERSION_MIN)) { 493 (void) fprintf(stderr, gettext( 494 "df: %s: unrecognized version of UFS: %d\n"), 495 file, sblock.fs_version); 496 (void) close(fi); 497 return; 498 } 499 (void) printf("%-20.20s", file); 500 if (iflag) { 501 if (eflag) { 502 (void) printf("%8ld", sblock.fs_cstotal.cs_nifree); 503 } else { 504 show_inode_usage( 505 (fsfilcnt64_t)sblock.fs_ncg * (fsfilcnt64_t)sblock.fs_ipg, 506 (fsfilcnt64_t)sblock.fs_cstotal.cs_nifree); 507 } 508 } else { 509 totalblks = (fsblkcnt64_t)sblock.fs_dsize; 510 free = 511 (fsblkcnt64_t)sblock.fs_cstotal.cs_nbfree * 512 (fsblkcnt64_t)sblock.fs_frag + 513 (fsblkcnt64_t)sblock.fs_cstotal.cs_nffree; 514 used = totalblks - free; 515 availblks = totalblks / (fsblkcnt64_t)100 * 516 ((fsblkcnt64_t)100 - (fsblkcnt64_t)sblock.fs_minfree); 517 avail = availblks > used ? availblks - used : (fsblkcnt64_t)0; 518 if (bflag) { 519 (void) printf("%8lld\n", dbtok(avail, 520 (fsblkcnt64_t)sblock.fs_fsize)); 521 } else { 522 (void) printf(" %7lld %7lld %7lld", 523 dbtok(totalblks, (fsblkcnt64_t)sblock.fs_fsize), 524 dbtok(used, (fsblkcnt64_t)sblock.fs_fsize), 525 dbtok(avail, (fsblkcnt64_t)sblock.fs_fsize)); 526 (void) printf("%6.0f%%", 527 availblks == 0 ? 0.0 : 528 (double)used / (double)availblks * 100.0); 529 (void) printf(" "); 530 } 531 if (tflag) { 532 t_totalblks += dbtok(totalblks, 533 (fsblkcnt64_t)sblock.fs_fsize); 534 t_used += dbtok(used, (fsblkcnt64_t)sblock.fs_fsize); 535 t_avail += dbtok(avail, (fsblkcnt64_t)sblock.fs_fsize); 536 t_free += free; 537 } 538 } 539 if ((!bflag) && (!eflag)) 540 (void) printf(" %s\n", mpath(file)); 541 else if (eflag) 542 (void) printf("\n"); 543 (void) close(fi); 544 } 545 546 void 547 dfreemnt(file, mnt) 548 char *file; 549 struct mnttab *mnt; 550 { 551 struct statvfs64 fs; 552 553 if (statvfs64(file, &fs) < 0 && 554 chroot_stat(file, statvfs64, (char *)&fs, &file) < 0) { 555 (void) fprintf(stderr, "df: "); 556 perror(file); 557 return; 558 } 559 560 if (!aflag && fs.f_blocks == 0) { 561 return; 562 } 563 if (!isatty(fileno(stdout))) { 564 (void) printf("%s", mnt->mnt_special); 565 } else { 566 if (strlen(mnt->mnt_special) > (size_t)20) { 567 (void) printf("%s\n", mnt->mnt_special); 568 (void) printf(" "); 569 } else { 570 (void) printf("%-20.20s", mnt->mnt_special); 571 } 572 } 573 if (iflag) { 574 if (eflag) { 575 (void) printf("%8lld", fs.f_ffree); 576 } else { 577 show_inode_usage(fs.f_files, fs.f_ffree); 578 } 579 } else { 580 if (gflag) { 581 print_statvfs(&fs); 582 } else { 583 fsblkcnt64_t totalblks, avail, free, used, reserved; 584 585 totalblks = fs.f_blocks; 586 free = fs.f_bfree; 587 used = totalblks - free; 588 avail = fs.f_bavail; 589 reserved = free - avail; 590 if ((long long)avail < 0) 591 avail = 0; 592 if (bflag) { 593 (void) printf("%8lld\n", dbtok(avail, 594 (fsblkcnt64_t)fs.f_frsize)); 595 } else { 596 (void) printf(" %7lld %7lld %7lld", 597 dbtok(totalblks, 598 (fsblkcnt64_t)fs.f_frsize), 599 dbtok(used, (fsblkcnt64_t)fs.f_frsize), 600 dbtok(avail, (fsblkcnt64_t)fs.f_frsize)); 601 totalblks -= reserved; 602 (void) printf("%6.0f%%", 603 totalblks == 0 ? 0.0 : 604 (double)used / (double)totalblks * 100.0); 605 (void) printf(" "); 606 if (tflag) { 607 t_totalblks += dbtok(totalblks + reserved, 608 (fsblkcnt64_t)fs.f_bsize); 609 t_reserved += reserved; 610 t_used += dbtok(used, 611 (fsblkcnt64_t)fs.f_frsize); 612 t_avail += dbtok(avail, 613 (fsblkcnt64_t)fs.f_frsize); 614 t_free += free; 615 } 616 } 617 } 618 } 619 if ((!bflag) && (!eflag) && (!gflag)) 620 (void) printf(" %s\n", mnt->mnt_mountp); 621 else if (eflag) 622 (void) printf("\n"); 623 } 624 625 static void 626 show_inode_usage(fsfilcnt64_t total, fsfilcnt64_t free) 627 { 628 fsfilcnt64_t used = total - free; 629 int missing_info = ((long long)total == (long long)-1 || 630 (long long)free == (long long)-1); 631 632 if (missing_info) 633 (void) printf("%8s", "*"); 634 else 635 (void) printf("%8lld", used); 636 if ((long long)free == (long long)-1) 637 (void) printf("%8s", "*"); 638 else 639 (void) printf(" %7lld", free); 640 if (missing_info) 641 (void) printf("%6s ", "*"); 642 else 643 (void) printf("%6.0f%% ", (double)used / (double)total * 100.0); 644 } 645 646 /* 647 * Return the suffix of path obtained by stripping off the prefix 648 * that is the value of the CHROOT environment variable. If this 649 * value isn't obtainable or if it's not a prefix of path, return NULL. 650 */ 651 static char * 652 zap_chroot(path) 653 char *path; 654 { 655 return (pathsuffix(path, chrootpath)); 656 } 657 658 /* 659 * Stat/statfs a file after stripping off leading directory to which we are 660 * chroot'd. Used to find the TFS mount that applies to the current 661 * activated NSE environment. 662 */ 663 static int 664 chroot_stat(dir, statfunc, statp, dirp) 665 char *dir; 666 int (*statfunc)(); 667 char *statp; 668 char **dirp; 669 { 670 if ((dir = zap_chroot(dir)) == NULL) 671 return (-1); 672 if (dirp) 673 *dirp = dir; 674 return (*statfunc)(dir, statp); 675 } 676 677 /* 678 * Given a name like /dev/dsk/c1d0s2, returns the mounted path, like /usr. 679 */ 680 char * 681 mpath(char *file) 682 { 683 struct mnttab mnt; 684 FILE *mnttab; 685 struct stat64 device_stat, mount_stat; 686 char *mname; 687 688 mnttab = fopen(MNTTAB, "r"); 689 if (mnttab == NULL) { 690 return (""); 691 } 692 mname = ""; 693 while ((getmntent(mnttab, &mnt)) == 0) { 694 if (strcmp(mnt.mnt_fstype, MNTTYPE_UFS) != 0) { 695 continue; 696 } 697 if (strcmp(file, mnt.mnt_special) == 0) { 698 if (stat64(mnt.mnt_mountp, &mount_stat) != 0) 699 continue; 700 if (stat64(mnt.mnt_special, &device_stat) != 0) 701 continue; 702 703 if (device_stat.st_rdev == mount_stat.st_dev) { 704 mname = mnt.mnt_mountp; 705 break; 706 } 707 } 708 } 709 fclose(mnttab); 710 return (mname); 711 } 712 713 /* 714 * Given a special device, return mnttab entry 715 */ 716 717 struct mnttab * 718 mdev(char *spec) 719 { 720 FILE *mntp; 721 struct mnttab mnt; 722 723 if ((mntp = fopen(MNTTAB, "r")) == 0) { 724 (void) fprintf(stderr, "df: "); 725 perror(MNTTAB); 726 exit(1); 727 } 728 729 while (getmntent(mntp, &mnt) == 0) { 730 if (strcmp(spec, mnt.mnt_special) == 0) { 731 (void) fclose(mntp); 732 return (mntdup(&mnt)); 733 } 734 } 735 (void) fclose(mntp); 736 (void) fprintf(stderr, "df : couldn't find mnttab entry for %s", spec); 737 exit(1); 738 } 739 740 /* 741 * Find the entry in mlist that corresponds to the file named by path 742 * (i.e., that names a mount table entry for the file system in which 743 * path lies). The pstat argument must point to stat information for 744 * path. 745 * 746 * Return the entry or NULL if there's no match. 747 * 748 * As it becomes necessary to obtain stat information about previously 749 * unexamined mlist entries, gather the information and cache it with the 750 * entries. 751 * 752 * The routine's strategy is to convert path into its canonical, symlink-free 753 * representation canon (which will require accessing the file systems on the 754 * branch from the root to path and thus may cause the routine to hang if any 755 * of them are inaccessible) and to use it to search for a mount point whose 756 * name is a substring of canon and whose corresponding device matches that of 757 * canon. This technique avoids accessing unnecessary file system resources 758 * and thus prevents the program from hanging on inaccessible resources unless 759 * those resources are necessary for accessing path. 760 */ 761 static struct mntlist * 762 findmntent(path, pstat, mlist) 763 char *path; 764 struct stat64 *pstat; 765 struct mntlist *mlist; 766 { 767 static char cwd[MAXPATHLEN]; 768 char canon[MAXPATHLEN]; 769 char scratch[MAXPATHLEN]; 770 register struct mntlist *mlp; 771 772 /* 773 * If path is relative and we haven't already determined the current 774 * working directory, do so now. Calculating the working directory 775 * here lets us do the work once, instead of (potentially) repeatedly 776 * in realpath(). 777 */ 778 if (*path != '/' && cwd[0] == '\0') { 779 if (getcwd(cwd, MAXPATHLEN) == NULL) { 780 cwd[0] = '\0'; 781 return (NULL); 782 } 783 } 784 785 /* 786 * Find an absolute pathname in the native file system name space that 787 * corresponds to path, stuffing it into canon. 788 * 789 * If CHROOT is set in the environment, assume that chroot($CHROOT) 790 * (or an equivalent series of calls) was executed and convert the 791 * path to the equivalent name in the native file system's name space. 792 * Doing so allows direct comparison with the names in mtab entires, 793 * which are assumed to be recorded relative to the native name space. 794 */ 795 if (abspath(cwd, path, scratch) < 0) 796 return (NULL); 797 if (strcmp(scratch, "/") == 0 && chrootpath != NULL) { 798 /* 799 * Force canon to be in canonical form; if the result from 800 * abspath was "/" and chrootpath isn't the null string, we 801 * must strip off a trailing slash. 802 */ 803 scratch[0] = '\0'; 804 } 805 (void) sprintf(canon, "%s%s", chrootpath ? chrootpath : "", scratch); 806 807 again: 808 for (mlp = mlist; mlp; mlp = mlp->mntl_next) { 809 struct mnttab *mnt = mlp->mntl_mnt; 810 811 /* 812 * Ignore uninteresting mounts. 813 */ 814 if (strcmp(mnt->mnt_fstype, typestr) != 0) 815 continue; 816 817 /* 818 * The mount entry covers some prefix of the file. 819 * See whether it's the entry for the file system 820 * containing the file by comparing device ids. 821 */ 822 if (mlp->mntl_dev == NODEV) { 823 struct stat64 fs_sb; 824 825 if (stat64(mnt->mnt_mountp, &fs_sb) < 0 && 826 chroot_stat(mnt->mnt_mountp, stat64, (char *)&fs_sb, 827 (char **)NULL) < 0) { 828 continue; 829 } 830 mlp->mntl_dev = fs_sb.st_dev; 831 } 832 833 if (pstat->st_dev == mlp->mntl_dev) 834 return (mlp); 835 } 836 837 return (NULL); 838 } 839 840 /* 841 * Convert the path given in raw to canonical, absolute, symlink-free 842 * form, storing the result in the buffer named by canon, which must be 843 * at least MAXPATHLEN bytes long. "wd" contains the current working 844 * directory; accepting this value as an argument lets our caller cache 845 * the value, so that realpath (called from this routine) doesn't have 846 * to recalculate it each time it's given a relative pathname. 847 * 848 * Return 0 on success, -1 on failure. 849 */ 850 static int 851 abspath(wd, raw, canon) 852 char *wd; 853 register char *raw; 854 char *canon; 855 { 856 char absbuf[MAXPATHLEN]; 857 858 /* 859 * Preliminary sanity check. 860 */ 861 if (wd == NULL || raw == NULL || canon == NULL) 862 return (-1); 863 864 /* 865 * If the path is relative, convert it to absolute form, 866 * using wd if it's been supplied. 867 */ 868 if (raw[0] != '/') { 869 register char *limit = absbuf + sizeof (absbuf); 870 register char *d; 871 872 /* Fill in working directory. */ 873 if (strlcpy(absbuf, wd, sizeof (absbuf)) >= sizeof (absbuf)) 874 return (-1); 875 876 /* Add separating slash. */ 877 d = absbuf + strlen(absbuf); 878 if (d < limit) 879 *d++ = '/'; 880 881 /* Glue on the relative part of the path. */ 882 while (d < limit && (*d++ = *raw++)) 883 continue; 884 885 raw = absbuf; 886 } 887 888 /* 889 * Call realpath to canonicalize and resolve symlinks. 890 */ 891 return (realpath(raw, canon) == NULL ? -1 : 0); 892 } 893 894 /* 895 * Return a pointer to the trailing suffix of full that follows the prefix 896 * given by pref. If pref isn't a prefix of full, return NULL. Apply 897 * pathname semantics to the prefix test, so that pref must match at a 898 * component boundary. 899 */ 900 static char * 901 pathsuffix(full, pref) 902 register char *full; 903 register char *pref; 904 { 905 register int preflen; 906 907 if (full == NULL || pref == NULL) 908 return (NULL); 909 910 preflen = strlen(pref); 911 if (strncmp(pref, full, preflen) != 0) 912 return (NULL); 913 914 /* 915 * pref is a substring of full. To be a subpath, it cannot cover a 916 * partial component of full. The last clause of the test handles the 917 * special case of the root. 918 */ 919 if (full[preflen] != '\0' && full[preflen] != '/' && preflen > 1) 920 return (NULL); 921 922 if (preflen == 1 && full[0] == '/') 923 return (full); 924 else 925 return (full + preflen); 926 } 927 928 /* 929 * Return zero iff the path named by sub is a leading subpath 930 * of the path named by full. 931 * 932 * Treat null paths as matching nothing. 933 */ 934 static int 935 subpath(full, sub) 936 register char *full; 937 register char *sub; 938 { 939 return (pathsuffix(full, sub) == NULL); 940 } 941 942 offset_t llseek(); 943 944 int 945 bread(file, fi, bno, buf, cnt) 946 char *file; 947 int fi; 948 daddr_t bno; 949 char *buf; 950 int cnt; 951 { 952 register int n; 953 954 (void) llseek(fi, (offset_t)bno * DEV_BSIZE, 0); 955 if ((n = read(fi, buf, cnt)) < 0) { 956 /* probably a dismounted disk if errno == EIO */ 957 if (errno != EIO) { 958 (void) fprintf(stderr, gettext("df: read error on ")); 959 perror(file); 960 (void) fprintf(stderr, "bno = %ld\n", bno); 961 } else { 962 (void) fprintf(stderr, gettext( 963 "df: premature EOF on %s\n"), file); 964 (void) fprintf(stderr, 965 "bno = %ld expected = %d count = %d\n", bno, cnt, n); 966 } 967 return (0); 968 } 969 return (1); 970 } 971 972 char * 973 xmalloc(size) 974 unsigned int size; 975 { 976 register char *ret; 977 char *malloc(); 978 979 if ((ret = (char *)malloc(size)) == NULL) { 980 (void) fprintf(stderr, gettext("umount: ran out of memory!\n")); 981 exit(1); 982 } 983 return (ret); 984 } 985 986 struct mnttab * 987 mntdup(mnt) 988 register struct mnttab *mnt; 989 { 990 register struct mnttab *new; 991 992 new = (struct mnttab *)xmalloc(sizeof (*new)); 993 994 new->mnt_special = 995 (char *)xmalloc((unsigned)(strlen(mnt->mnt_special) + 1)); 996 (void) strcpy(new->mnt_special, mnt->mnt_special); 997 998 new->mnt_mountp = 999 (char *)xmalloc((unsigned)(strlen(mnt->mnt_mountp) + 1)); 1000 (void) strcpy(new->mnt_mountp, mnt->mnt_mountp); 1001 1002 new->mnt_fstype = 1003 (char *)xmalloc((unsigned)(strlen(mnt->mnt_fstype) + 1)); 1004 (void) strcpy(new->mnt_fstype, mnt->mnt_fstype); 1005 1006 if (mnt->mnt_mntopts != NULL) { 1007 new->mnt_mntopts = 1008 (char *)xmalloc((unsigned)(strlen(mnt->mnt_mntopts) + 1)); 1009 (void) strcpy(new->mnt_mntopts, mnt->mnt_mntopts); 1010 } else { 1011 new->mnt_mntopts = NULL; 1012 } 1013 1014 #ifdef never 1015 new->mnt_freq = mnt->mnt_freq; 1016 new->mnt_passno = mnt->mnt_passno; 1017 #endif /* never */ 1018 1019 return (new); 1020 } 1021 1022 void 1023 usage() 1024 { 1025 1026 (void) fprintf(stderr, gettext( 1027 "ufs usage: df [generic options] [-o i] [directory | special]\n")); 1028 exit(1); 1029 } 1030 1031 struct mntlist * 1032 mkmntlist() 1033 { 1034 FILE *mounted; 1035 struct mntlist *mntl; 1036 struct mntlist *mntst = NULL; 1037 struct extmnttab mnt; 1038 1039 if ((mounted = fopen(MNTTAB, "r")) == NULL) { 1040 (void) fprintf(stderr, "df : "); 1041 perror(MNTTAB); 1042 exit(1); 1043 } 1044 resetmnttab(mounted); 1045 while (getextmntent(mounted, &mnt, sizeof (struct extmnttab)) == NULL) { 1046 mntl = (struct mntlist *)xmalloc(sizeof (*mntl)); 1047 mntl->mntl_mnt = mntdup((struct mnttab *)(&mnt)); 1048 mntl->mntl_next = mntst; 1049 mntl->mntl_devvalid = 1; 1050 mntl->mntl_dev = makedev(mnt.mnt_major, mnt.mnt_minor); 1051 mntst = mntl; 1052 } 1053 (void) fclose(mounted); 1054 return (mntst); 1055 } 1056 1057 void 1058 print_statvfs(fs) 1059 struct statvfs64 *fs; 1060 { 1061 int i; 1062 1063 for (i = 0; i < FSTYPSZ; i++) 1064 (void) printf("%c", fs->f_basetype[i]); 1065 (void) printf(" %7d %7lld %7lld", 1066 fs->f_frsize, 1067 fs->f_blocks, 1068 fs->f_bavail); 1069 (void) printf(" %7lld %7lld %7d", 1070 fs->f_files, 1071 fs->f_ffree, 1072 fs->f_fsid); 1073 (void) printf(" 0x%x ", 1074 fs->f_flag); 1075 for (i = 0; i < 14; i++) 1076 (void) printf("%c", 1077 (fs->f_fstr[i] == '\0') ? ' ' : fs->f_fstr[i]); 1078 printf("\n"); 1079 } 1080 1081 void 1082 print_totals() 1083 { 1084 /* 1085 * TRANSLATION_NOTE 1086 * Following string is used as a table header. 1087 * Translated items should start at the same 1088 * columns as the original items. 1089 */ 1090 (void) printf(gettext("Totals %8lld %7lld %7lld"), 1091 t_totalblks, t_used, t_avail); 1092 (void) printf("%6.0f%%\n", 1093 (t_totalblks - t_reserved) == (fsblkcnt64_t)0 ? 1094 0.0 : 1095 (double)t_used / (double)(t_totalblks - t_reserved) * 100.0); 1096 } 1097 1098 void 1099 print_itotals() 1100 { 1101 /* 1102 * TRANSLATION_NOTE 1103 * Following string is used as a table header. 1104 * Translated items should start at the same 1105 * columns as the original items. 1106 */ 1107 (void) printf(gettext("Totals %8d %7d%6.0f%%\n"), 1108 t_iused, 1109 t_ifree, 1110 t_inodes == 0 ? 0.0 : (double)t_iused / (double)t_inodes * 100.0); 1111 } 1112