dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fs.d / ufs / df / df.c
blob86e8a627f19df3818435cf9618eccb9a0230e682
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
41 * df
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/mntent.h>
48 #include <sys/fs/ufs_fs.h>
49 #include <sys/stat.h>
50 #include <sys/vfs.h>
51 #include <sys/file.h>
52 #include <sys/statvfs.h>
53 #include <sys/mnttab.h>
54 #include <sys/mkdev.h>
55 #include <locale.h>
56 #include <stdarg.h>
57 #include <string.h>
58 #include <errno.h>
59 #include <libintl.h>
61 extern char *getenv();
62 extern char *getcwd();
63 extern char *realpath();
64 extern off_t lseek();
67 * Raw name to block device name translation function.
68 * This comes from libadm.
70 extern char *getfullblkname();
72 static void usage(), pheader();
73 static char *mpath(char *);
74 static char *zap_chroot(char *);
75 static char *pathsuffix(char *, char *);
76 static char *xmalloc(unsigned int);
77 static int chroot_stat(char *, int (*)(), char *, char **);
78 static int bread(char *, int, daddr_t, char *, int);
79 static int subpath(char *, char *);
80 static int abspath(char *, char *, char *);
81 static void show_inode_usage();
82 static void dfreedev(char *);
83 static void dfreemnt(char *, struct mnttab *);
84 static void print_totals();
85 static void print_itotals();
86 static void print_statvfs(struct statvfs64 *);
87 static int mdev(char *, struct mnttab **);
88 static struct mntlist *mkmntlist();
89 static struct mnttab *mntdup(struct mnttab *mnt);
90 static struct mntlist *findmntent(char *, struct stat64 *, struct mntlist *);
92 #define bcopy(f, t, n) memcpy(t, f, n)
93 #define bzero(s, n) memset(s, 0, n)
94 #define bcmp(s, d, n) memcmp(s, d, n)
96 #define index(s, r) strchr(s, r)
97 #define rindex(s, r) strrchr(s, r)
99 #define dbtok(x, b) \
100 ((b) < (fsblkcnt64_t)1024 ? \
101 (x) / ((fsblkcnt64_t)1024 / (b)) : (x) * ((b) / (fsblkcnt64_t)1024))
103 int aflag = 0; /* even the uninteresting ones */
104 int bflag = 0; /* print only number of kilobytes free */
105 int eflag = 0; /* print only number of file entries free */
106 int gflag = 0; /* print entire statvfs structure */
107 int hflag = 0; /* don't print header */
108 int iflag = 0; /* information for inodes */
109 int nflag = 0; /* print VFStype name */
110 int tflag = 0; /* print totals */
111 int errflag = 0;
112 int errcode = 0;
113 char *typestr = "ufs";
114 fsblkcnt64_t t_totalblks, t_avail, t_free, t_used, t_reserved;
115 int t_inodes, t_iused, t_ifree;
118 * cached information recording previous chroot history.
120 static char *chrootpath;
122 extern int optind;
123 extern char *optarg;
125 union {
126 struct fs iu_fs;
127 char dummy[SBSIZE];
128 } sb;
129 #define sblock sb.iu_fs
132 * This structure is used to chain mntent structures into a list
133 * and to cache stat information for each member of the list.
135 struct mntlist {
136 struct mnttab *mntl_mnt;
137 struct mntlist *mntl_next;
138 dev_t mntl_dev;
139 int mntl_devvalid;
142 char *subopts [] = {
143 #define A_FLAG 0
144 "a",
145 #define I_FLAG 1
146 "i",
147 NULL
151 main(int argc, char *argv[])
153 struct mnttab mnt;
154 int opt;
155 char *suboptions, *value;
157 (void) setlocale(LC_ALL, "");
158 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
159 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
160 #endif
161 (void) textdomain(TEXT_DOMAIN);
163 while ((opt = getopt(argc, argv, "beghkno:t")) != EOF) {
164 switch (opt) {
166 case 'b': /* print only number of kilobytes free */
167 bflag++;
168 break;
170 case 'e':
171 eflag++; /* print only number of file entries free */
172 iflag++;
173 break;
175 case 'g':
176 gflag++;
177 break;
179 case 'n':
180 nflag++;
181 break;
183 case 'k':
184 break;
186 case 'h':
187 hflag++;
188 break;
190 case 'o':
192 * ufs specific options.
194 suboptions = optarg;
195 while (*suboptions != '\0') {
196 switch (getsubopt(&suboptions,
197 subopts, &value)) {
199 case I_FLAG: /* information for inodes */
200 iflag++;
201 break;
203 default:
204 usage();
207 break;
209 case 't': /* print totals */
210 tflag++;
211 break;
213 case 'V': /* Print command line */
215 char *opt_text;
216 int opt_count;
218 (void) fprintf(stdout, "df -F ufs ");
219 for (opt_count = 1; opt_count < argc;
220 opt_count++) {
221 opt_text = argv[opt_count];
222 if (opt_text)
223 (void) fprintf(stdout, " %s ",
224 opt_text);
226 (void) fprintf(stdout, "\n");
228 break;
230 case '?':
231 errflag++;
234 if (errflag)
235 usage();
236 if (gflag && iflag) {
237 printf(gettext("df: '-g' and '-o i' are mutually exclusive\n"));
238 exit(1);
240 if (bflag || eflag)
241 tflag = 0;
244 * Cache CHROOT information for later use; assume that $CHROOT matches
245 * the cumulative arguments given to chroot calls.
247 chrootpath = getenv("CHROOT");
248 if (chrootpath != NULL && strcmp(chrootpath, "/") == 0)
249 chrootpath = NULL;
251 if (argc <= optind) {
253 * Take this path when "/usr/lib/fs/ufs/df" is specified, and
254 * there are no mountpoints specified.
255 * E.g., these command lines take us down this path
256 * /usr/lib/fs/ufs/df -o i
257 * /usr/lib/fs/ufs/df
259 FILE *mtabp;
261 if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
262 (void) fprintf(stderr, "df: ");
263 perror(MNTTAB);
264 exit(1);
266 pheader();
267 while (getmntent(mtabp, &mnt) == 0) {
268 if (strcmp(typestr, mnt.mnt_fstype) != 0) {
269 continue;
271 dfreemnt(mnt.mnt_mountp, &mnt);
273 if (tflag)
274 if (iflag)
275 print_itotals();
276 else
277 print_totals();
278 (void) fclose(mtabp);
279 } else {
280 int i;
281 struct mntlist *mntl;
282 struct stat64 *argstat;
283 char **devnames;
284 char *cp;
286 /* Arguments are processed till optind, adjust the pointers */
287 argv += optind;
288 argc -= optind;
291 * Obtain stat64 information for each argument before
292 * constructing the list of mounted file systems. This
293 * ordering forces the automounter to establish any
294 * mounts required to access the arguments, so that the
295 * corresponding mount table entries will exist when
296 * we look for them.
298 argstat = (struct stat64 *)xmalloc(argc * sizeof (*argstat));
299 devnames = (char **)xmalloc(argc * sizeof (char *));
300 for (i = 0; i < argc; i++) {
303 * Given a raw device name, get the block device name
305 cp = getfullblkname(argv[i]);
306 if (cp == NULL || *cp == '\0') {
307 free(cp);
308 cp = strdup(argv[i]);
310 if (cp == NULL) {
311 int j;
313 fprintf(stderr, gettext(
314 "df: memory allocation failure\n"));
316 for (j = 0; j < i; j++)
317 free(devnames[j]);
318 free(devnames);
319 free(argstat);
320 exit(1);
323 if (stat64(cp, &argstat[i]) < 0) {
324 errcode = errno;
326 * Mark as no longer interesting.
328 argv[i] = NULL;
329 devnames[i] = NULL;
330 free(cp);
331 } else {
332 devnames[i] = cp;
336 pheader();
337 aflag++;
339 * Construct the list of mounted file systems.
341 mntl = mkmntlist();
344 * Iterate through the argument list, reporting on each one.
346 for (i = 0; i < argc; i++) {
347 struct mntlist *mlp;
348 int isblk;
351 * Skip if we've already determined that we can't
352 * process it.
354 if (argv[i] == NULL)
355 continue;
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
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;
367 if (mdev(cp, &mp))
368 return (1);
369 dfreemnt(mp->mnt_mountp, mp);
370 } else {
371 dfreedev(cp);
373 free(cp);
374 devnames[i] = NULL;
375 continue;
379 * Get this argument's corresponding mount table
380 * entry.
382 mlp = findmntent(cp, &argstat[i], mntl);
383 free(cp);
384 devnames[i] = NULL;
386 if (mlp == NULL) {
387 (void) fprintf(stderr,
388 gettext("Could not find mount point for %s\n"),
389 argv[i]);
390 continue;
393 dfreemnt(mlp->mntl_mnt->mnt_mountp, mlp->mntl_mnt);
395 free(devnames);
396 free(argstat);
398 return (0);
401 void
402 pheader()
404 if (hflag)
405 return;
406 if (nflag)
407 (void) printf(gettext("VFStype name - ufs\n"));
408 if (iflag) {
409 if (eflag)
411 * TRANSLATION_NOTE
412 * Following string is used as a table header.
413 * Translated items should start at the same
414 * columns as the original items.
416 (void) printf(gettext(
417 "Filesystem ifree\n"));
418 else {
420 * TRANSLATION_NOTE
421 * Following string is used as a table header.
422 * Translated items should start at the same
423 * columns as the original items.
425 (void) printf(gettext(
426 "Filesystem iused ifree %%iused Mounted on\n"));
428 } else {
429 if (gflag)
431 * TRANSLATION_NOTE
432 * Following string is used as a table header.
433 * Translated items should start at the same
434 * columns as the original items.
436 (void) printf(gettext(
437 "Filesystem f_type f_fsize f_bfree f_bavail f_files f_ffree "
438 "f_fsid f_flag f_fstr\n"));
439 else
440 if (bflag)
442 * TRANSLATION_NOTE
443 * Following string is used as a table header.
444 * Translated items should start at the same
445 * columns as the original items.
447 (void) printf(gettext(
448 "Filesystem avail\n"));
449 else {
451 * TRANSLATION_NOTE
452 * Following string is used as a table header.
453 * Translated items should start at the same
454 * columns as the original items.
456 (void) printf(gettext(
457 "Filesystem kbytes used avail capacity Mounted on\n"));
463 * Report on a block or character special device. Assumed not to be
464 * mounted. N.B. checks for a valid UFS superblock.
466 void
467 dfreedev(char *file)
469 fsblkcnt64_t totalblks, availblks, avail, free, used;
470 int fi;
472 fi = open64(file, 0);
473 if (fi < 0) {
474 (void) fprintf(stderr, "df: ");
475 perror(file);
476 return;
478 if (bread(file, fi, SBLOCK, (char *)&sblock, SBSIZE) == 0) {
479 (void) close(fi);
480 return;
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;
490 if (sblock.fs_magic == FS_MAGIC &&
491 (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
492 sblock.fs_version != 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;
499 if (sblock.fs_magic == MTB_UFS_MAGIC &&
500 (sblock.fs_version > MTB_UFS_VERSION_1 ||
501 sblock.fs_version < MTB_UFS_VERSION_MIN)) {
502 (void) fprintf(stderr, gettext(
503 "df: %s: unrecognized version of UFS: %d\n"),
504 file, sblock.fs_version);
505 (void) close(fi);
506 return;
508 (void) printf("%-20.20s", file);
509 if (iflag) {
510 if (eflag) {
511 (void) printf("%8ld", sblock.fs_cstotal.cs_nifree);
512 } else {
513 show_inode_usage(
514 (fsfilcnt64_t)sblock.fs_ncg *
515 (fsfilcnt64_t)sblock.fs_ipg,
516 (fsfilcnt64_t)sblock.fs_cstotal.cs_nifree);
518 } else {
519 totalblks = (fsblkcnt64_t)sblock.fs_dsize;
520 free =
521 (fsblkcnt64_t)sblock.fs_cstotal.cs_nbfree *
522 (fsblkcnt64_t)sblock.fs_frag +
523 (fsblkcnt64_t)sblock.fs_cstotal.cs_nffree;
524 used = totalblks - free;
525 availblks = totalblks / (fsblkcnt64_t)100 *
526 ((fsblkcnt64_t)100 - (fsblkcnt64_t)sblock.fs_minfree);
527 avail = availblks > used ? availblks - used : (fsblkcnt64_t)0;
528 if (bflag) {
529 (void) printf("%8lld\n", dbtok(avail,
530 (fsblkcnt64_t)sblock.fs_fsize));
531 } else {
532 (void) printf(" %7lld %7lld %7lld",
533 dbtok(totalblks, (fsblkcnt64_t)sblock.fs_fsize),
534 dbtok(used, (fsblkcnt64_t)sblock.fs_fsize),
535 dbtok(avail, (fsblkcnt64_t)sblock.fs_fsize));
536 (void) printf("%6.0f%%",
537 availblks == 0 ? 0.0 :
538 (double)used / (double)availblks * 100.0);
539 (void) printf(" ");
541 if (tflag) {
542 t_totalblks += dbtok(totalblks,
543 (fsblkcnt64_t)sblock.fs_fsize);
544 t_used += dbtok(used, (fsblkcnt64_t)sblock.fs_fsize);
545 t_avail += dbtok(avail, (fsblkcnt64_t)sblock.fs_fsize);
546 t_free += free;
549 if ((!bflag) && (!eflag))
550 (void) printf(" %s\n", mpath(file));
551 else if (eflag)
552 (void) printf("\n");
553 (void) close(fi);
556 void
557 dfreemnt(char *file, struct mnttab *mnt)
559 struct statvfs64 fs;
561 if (statvfs64(file, &fs) < 0 &&
562 chroot_stat(file, statvfs64, (char *)&fs, &file) < 0) {
563 (void) fprintf(stderr, "df: ");
564 perror(file);
565 return;
568 if (!aflag && fs.f_blocks == 0) {
569 return;
571 if (!isatty(fileno(stdout))) {
572 (void) printf("%s", mnt->mnt_special);
573 } else {
574 if (strlen(mnt->mnt_special) > (size_t)20) {
575 (void) printf("%s\n", mnt->mnt_special);
576 (void) printf(" ");
577 } else {
578 (void) printf("%-20.20s", mnt->mnt_special);
581 if (iflag) {
582 if (eflag) {
583 (void) printf("%8lld", fs.f_ffree);
584 } else {
585 show_inode_usage(fs.f_files, fs.f_ffree);
587 } else {
588 if (gflag) {
589 print_statvfs(&fs);
590 } else {
591 fsblkcnt64_t totalblks, avail, free, used, reserved;
593 totalblks = fs.f_blocks;
594 free = fs.f_bfree;
595 used = totalblks - free;
596 avail = fs.f_bavail;
597 reserved = free - avail;
598 if ((long long)avail < 0)
599 avail = 0;
600 if (bflag) {
601 (void) printf("%8lld\n", dbtok(avail,
602 (fsblkcnt64_t)fs.f_frsize));
603 } else {
604 (void) printf(" %7lld %7lld %7lld",
605 dbtok(totalblks,
606 (fsblkcnt64_t)fs.f_frsize),
607 dbtok(used, (fsblkcnt64_t)fs.f_frsize),
608 dbtok(avail, (fsblkcnt64_t)fs.f_frsize));
609 totalblks -= reserved;
610 (void) printf("%6.0f%%",
611 totalblks == 0 ? 0.0 :
612 (double)used / (double)totalblks * 100.0);
613 (void) printf(" ");
614 if (tflag) {
615 t_totalblks += dbtok(totalblks + reserved,
616 (fsblkcnt64_t)fs.f_bsize);
617 t_reserved += reserved;
618 t_used += dbtok(used,
619 (fsblkcnt64_t)fs.f_frsize);
620 t_avail += dbtok(avail,
621 (fsblkcnt64_t)fs.f_frsize);
622 t_free += free;
627 if ((!bflag) && (!eflag) && (!gflag))
628 (void) printf(" %s\n", mnt->mnt_mountp);
629 else if (eflag)
630 (void) printf("\n");
633 static void
634 show_inode_usage(fsfilcnt64_t total, fsfilcnt64_t free)
636 fsfilcnt64_t used = total - free;
637 int missing_info = ((long long)total == (long long)-1 ||
638 (long long)free == (long long)-1);
640 if (missing_info)
641 (void) printf("%8s", "*");
642 else
643 (void) printf("%8lld", used);
644 if ((long long)free == (long long)-1)
645 (void) printf("%8s", "*");
646 else
647 (void) printf(" %7lld", free);
648 if (missing_info)
649 (void) printf("%6s ", "*");
650 else
651 (void) printf("%6.0f%% ", (double)used / (double)total * 100.0);
655 * Return the suffix of path obtained by stripping off the prefix
656 * that is the value of the CHROOT environment variable. If this
657 * value isn't obtainable or if it's not a prefix of path, return NULL.
659 static char *
660 zap_chroot(char *path)
662 return (pathsuffix(path, chrootpath));
666 * Stat/statfs a file after stripping off leading directory to which we are
667 * chroot'd. Used to find the TFS mount that applies to the current
668 * activated NSE environment.
670 static int
671 chroot_stat(char *dir, int (*statfunc)(), char *statp, char **dirp)
673 if ((dir = zap_chroot(dir)) == NULL)
674 return (-1);
675 if (dirp)
676 *dirp = dir;
677 return (*statfunc)(dir, statp);
681 * Given a name like /dev/dsk/c1d0s2, returns the mounted path, like /usr.
683 char *
684 mpath(char *file)
686 struct mnttab mnt;
687 FILE *mnttab;
688 struct stat64 device_stat, mount_stat;
689 char *mname;
691 mnttab = fopen(MNTTAB, "r");
692 if (mnttab == NULL) {
693 return ("");
695 mname = "";
696 while ((getmntent(mnttab, &mnt)) == 0) {
697 if (strcmp(mnt.mnt_fstype, MNTTYPE_UFS) != 0) {
698 continue;
700 if (strcmp(file, mnt.mnt_special) == 0) {
701 if (stat64(mnt.mnt_mountp, &mount_stat) != 0)
702 continue;
703 if (stat64(mnt.mnt_special, &device_stat) != 0)
704 continue;
706 if (device_stat.st_rdev == mount_stat.st_dev) {
707 mname = mnt.mnt_mountp;
708 break;
712 fclose(mnttab);
713 return (mname);
717 * Given a special device, return mnttab entry
718 * Returns 0 on success
722 mdev(char *spec, struct mnttab **mntbp)
724 FILE *mntp;
725 struct mnttab mnt;
727 if ((mntp = fopen(MNTTAB, "r")) == 0) {
728 (void) fprintf(stderr, "df: ");
729 perror(MNTTAB);
730 return (1);
733 while (getmntent(mntp, &mnt) == 0) {
734 if (strcmp(spec, mnt.mnt_special) == 0) {
735 (void) fclose(mntp);
736 *mntbp = mntdup(&mnt);
737 return (0);
740 (void) fclose(mntp);
741 (void) fprintf(stderr, "df : couldn't find mnttab entry for %s", spec);
742 return (1);
746 * Find the entry in mlist that corresponds to the file named by path
747 * (i.e., that names a mount table entry for the file system in which
748 * path lies). The pstat argument must point to stat information for
749 * path.
751 * Return the entry or NULL if there's no match.
753 * As it becomes necessary to obtain stat information about previously
754 * unexamined mlist entries, gather the information and cache it with the
755 * entries.
757 * The routine's strategy is to convert path into its canonical, symlink-free
758 * representation canon (which will require accessing the file systems on the
759 * branch from the root to path and thus may cause the routine to hang if any
760 * of them are inaccessible) and to use it to search for a mount point whose
761 * name is a substring of canon and whose corresponding device matches that of
762 * canon. This technique avoids accessing unnecessary file system resources
763 * and thus prevents the program from hanging on inaccessible resources unless
764 * those resources are necessary for accessing path.
766 static struct mntlist *
767 findmntent(char *path, struct stat64 *pstat, struct mntlist *mlist)
769 static char cwd[MAXPATHLEN];
770 char canon[MAXPATHLEN];
771 char scratch[MAXPATHLEN];
772 struct mntlist *mlp;
775 * If path is relative and we haven't already determined the current
776 * working directory, do so now. Calculating the working directory
777 * here lets us do the work once, instead of (potentially) repeatedly
778 * in realpath().
780 if (*path != '/' && cwd[0] == '\0') {
781 if (getcwd(cwd, MAXPATHLEN) == NULL) {
782 cwd[0] = '\0';
783 return (NULL);
788 * Find an absolute pathname in the native file system name space that
789 * corresponds to path, stuffing it into canon.
791 * If CHROOT is set in the environment, assume that chroot($CHROOT)
792 * (or an equivalent series of calls) was executed and convert the
793 * path to the equivalent name in the native file system's name space.
794 * Doing so allows direct comparison with the names in mtab entires,
795 * which are assumed to be recorded relative to the native name space.
797 if (abspath(cwd, path, scratch) < 0)
798 return (NULL);
799 if (strcmp(scratch, "/") == 0 && chrootpath != NULL) {
801 * Force canon to be in canonical form; if the result from
802 * abspath was "/" and chrootpath isn't the null string, we
803 * must strip off a trailing slash.
805 scratch[0] = '\0';
807 (void) sprintf(canon, "%s%s", chrootpath ? chrootpath : "", scratch);
809 again:
810 for (mlp = mlist; mlp; mlp = mlp->mntl_next) {
811 struct mnttab *mnt = mlp->mntl_mnt;
814 * Ignore uninteresting mounts.
816 if (strcmp(mnt->mnt_fstype, typestr) != 0)
817 continue;
820 * The mount entry covers some prefix of the file.
821 * See whether it's the entry for the file system
822 * containing the file by comparing device ids.
824 if (mlp->mntl_dev == NODEV) {
825 struct stat64 fs_sb;
827 if (stat64(mnt->mnt_mountp, &fs_sb) < 0 &&
828 chroot_stat(mnt->mnt_mountp, stat64, (char *)&fs_sb,
829 (char **)NULL) < 0) {
830 continue;
832 mlp->mntl_dev = fs_sb.st_dev;
835 if (pstat->st_dev == mlp->mntl_dev)
836 return (mlp);
839 return (NULL);
843 * Convert the path given in raw to canonical, absolute, symlink-free
844 * form, storing the result in the buffer named by canon, which must be
845 * at least MAXPATHLEN bytes long. "wd" contains the current working
846 * directory; accepting this value as an argument lets our caller cache
847 * the value, so that realpath (called from this routine) doesn't have
848 * to recalculate it each time it's given a relative pathname.
850 * Return 0 on success, -1 on failure.
852 static int
853 abspath(char *wd, char *raw, char *canon)
855 char absbuf[MAXPATHLEN];
858 * Preliminary sanity check.
860 if (wd == NULL || raw == NULL || canon == NULL)
861 return (-1);
864 * If the path is relative, convert it to absolute form,
865 * using wd if it's been supplied.
867 if (raw[0] != '/') {
868 char *limit = absbuf + sizeof (absbuf);
869 char *d;
871 /* Fill in working directory. */
872 if (strlcpy(absbuf, wd, sizeof (absbuf)) >= sizeof (absbuf))
873 return (-1);
875 /* Add separating slash. */
876 d = absbuf + strlen(absbuf);
877 if (d < limit)
878 *d++ = '/';
880 /* Glue on the relative part of the path. */
881 while (d < limit && (*d++ = *raw++))
882 continue;
884 raw = absbuf;
888 * Call realpath to canonicalize and resolve symlinks.
890 return (realpath(raw, canon) == NULL ? -1 : 0);
894 * Return a pointer to the trailing suffix of full that follows the prefix
895 * given by pref. If pref isn't a prefix of full, return NULL. Apply
896 * pathname semantics to the prefix test, so that pref must match at a
897 * component boundary.
899 static char *
900 pathsuffix(char *full, char *pref)
902 int preflen;
904 if (full == NULL || pref == NULL)
905 return (NULL);
907 preflen = strlen(pref);
908 if (strncmp(pref, full, preflen) != 0)
909 return (NULL);
912 * pref is a substring of full. To be a subpath, it cannot cover a
913 * partial component of full. The last clause of the test handles the
914 * special case of the root.
916 if (full[preflen] != '\0' && full[preflen] != '/' && preflen > 1)
917 return (NULL);
919 if (preflen == 1 && full[0] == '/')
920 return (full);
921 else
922 return (full + preflen);
926 * Return zero iff the path named by sub is a leading subpath
927 * of the path named by full.
929 * Treat null paths as matching nothing.
931 static int
932 subpath(char *full, char *sub)
934 return (pathsuffix(full, sub) == NULL);
937 offset_t llseek();
940 bread(char *file, int fi, daddr_t bno, char *buf, int cnt)
942 int n;
944 (void) llseek(fi, (offset_t)bno * DEV_BSIZE, 0);
945 if ((n = read(fi, buf, cnt)) < 0) {
946 /* probably a dismounted disk if errno == EIO */
947 if (errno != EIO) {
948 (void) fprintf(stderr, gettext("df: read error on "));
949 perror(file);
950 (void) fprintf(stderr, "bno = %ld\n", bno);
951 } else {
952 (void) fprintf(stderr, gettext(
953 "df: premature EOF on %s\n"), file);
954 (void) fprintf(stderr,
955 "bno = %ld expected = %d count = %d\n", bno, cnt, n);
957 return (0);
959 return (1);
962 char *
963 xmalloc(unsigned int size)
965 char *ret;
966 char *malloc();
968 if ((ret = (char *)malloc(size)) == NULL) {
969 (void) fprintf(stderr, gettext("umount: ran out of memory!\n"));
970 exit(1);
972 return (ret);
975 struct mnttab *
976 mntdup(struct mnttab *mnt)
978 struct mnttab *new;
980 new = (struct mnttab *)xmalloc(sizeof (*new));
982 new->mnt_special =
983 (char *)xmalloc((unsigned)(strlen(mnt->mnt_special) + 1));
984 (void) strcpy(new->mnt_special, mnt->mnt_special);
986 new->mnt_mountp =
987 (char *)xmalloc((unsigned)(strlen(mnt->mnt_mountp) + 1));
988 (void) strcpy(new->mnt_mountp, mnt->mnt_mountp);
990 new->mnt_fstype =
991 (char *)xmalloc((unsigned)(strlen(mnt->mnt_fstype) + 1));
992 (void) strcpy(new->mnt_fstype, mnt->mnt_fstype);
994 if (mnt->mnt_mntopts != NULL) {
995 new->mnt_mntopts =
996 (char *)xmalloc((unsigned)(strlen(mnt->mnt_mntopts) + 1));
997 (void) strcpy(new->mnt_mntopts, mnt->mnt_mntopts);
998 } else {
999 new->mnt_mntopts = NULL;
1002 #ifdef never
1003 new->mnt_freq = mnt->mnt_freq;
1004 new->mnt_passno = mnt->mnt_passno;
1005 #endif /* never */
1007 return (new);
1010 void
1011 usage()
1014 (void) fprintf(stderr, gettext(
1015 "ufs usage: df [generic options] [-o i] [directory | special]\n"));
1016 exit(1);
1019 struct mntlist *
1020 mkmntlist()
1022 FILE *mounted;
1023 struct mntlist *mntl;
1024 struct mntlist *mntst = NULL;
1025 struct extmnttab mnt;
1027 if ((mounted = fopen(MNTTAB, "r")) == NULL) {
1028 (void) fprintf(stderr, "df : ");
1029 perror(MNTTAB);
1030 exit(1);
1032 resetmnttab(mounted);
1033 while (getextmntent(mounted, &mnt, sizeof (struct extmnttab)) == 0) {
1034 mntl = (struct mntlist *)xmalloc(sizeof (*mntl));
1035 mntl->mntl_mnt = mntdup((struct mnttab *)(&mnt));
1036 mntl->mntl_next = mntst;
1037 mntl->mntl_devvalid = 1;
1038 mntl->mntl_dev = makedev(mnt.mnt_major, mnt.mnt_minor);
1039 mntst = mntl;
1041 (void) fclose(mounted);
1042 return (mntst);
1045 void
1046 print_statvfs(struct statvfs64 *fs)
1048 int i;
1050 for (i = 0; i < FSTYPSZ; i++)
1051 (void) printf("%c", fs->f_basetype[i]);
1052 (void) printf(" %7d %7lld %7lld",
1053 fs->f_frsize,
1054 fs->f_blocks,
1055 fs->f_bavail);
1056 (void) printf(" %7lld %7lld %7d",
1057 fs->f_files,
1058 fs->f_ffree,
1059 fs->f_fsid);
1060 (void) printf(" 0x%x ",
1061 fs->f_flag);
1062 for (i = 0; i < 14; i++)
1063 (void) printf("%c",
1064 (fs->f_fstr[i] == '\0') ? ' ' : fs->f_fstr[i]);
1065 printf("\n");
1068 void
1069 print_totals()
1072 * TRANSLATION_NOTE
1073 * Following string is used as a table header.
1074 * Translated items should start at the same
1075 * columns as the original items.
1077 (void) printf(gettext("Totals %8lld %7lld %7lld"),
1078 t_totalblks, t_used, t_avail);
1079 (void) printf("%6.0f%%\n",
1080 (t_totalblks - t_reserved) == (fsblkcnt64_t)0 ?
1081 0.0 :
1082 (double)t_used / (double)(t_totalblks - t_reserved) * 100.0);
1085 void
1086 print_itotals()
1089 * TRANSLATION_NOTE
1090 * Following string is used as a table header.
1091 * Translated items should start at the same
1092 * columns as the original items.
1094 (void) printf(gettext("Totals %8d %7d%6.0f%%\n"),
1095 t_iused,
1096 t_ifree,
1097 t_inodes == 0 ? 0.0 : (double)t_iused / (double)t_inodes * 100.0);