Home | History | Annotate | Line # | Download | only in df
df.c revision 1.50
      1  1.50     perry /*	$NetBSD: df.c,v 1.50 2003/12/07 22:42:36 perry Exp $	*/
      2  1.19       cgd 
      3  1.12       cgd /*
      4  1.15   mycroft  * Copyright (c) 1980, 1990, 1993, 1994
      5  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.12       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.12       cgd  * All or some portions of this file are derived from material licensed
      8  1.12       cgd  * to the University of California by American Telephone and Telegraph
      9  1.12       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.12       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.12       cgd  *
     12  1.12       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.12       cgd  * modification, are permitted provided that the following conditions
     14  1.12       cgd  * are met:
     15  1.12       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.12       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.12       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.12       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.12       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.12       cgd  *    may be used to endorse or promote products derived from this software
     22  1.12       cgd  *    without specific prior written permission.
     23  1.12       cgd  *
     24  1.12       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.12       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.12       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.12       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.12       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.12       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.12       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.12       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.12       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.12       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.12       cgd  * SUCH DAMAGE.
     35  1.12       cgd  */
     36  1.12       cgd 
     37  1.24   thorpej #include <sys/cdefs.h>
     38  1.12       cgd #ifndef lint
     39  1.24   thorpej __COPYRIGHT(
     40  1.15   mycroft "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
     41  1.24   thorpej 	The Regents of the University of California.  All rights reserved.\n");
     42  1.12       cgd #endif /* not lint */
     43  1.12       cgd 
     44  1.12       cgd #ifndef lint
     45  1.19       cgd #if 0
     46  1.19       cgd static char sccsid[] = "@(#)df.c	8.7 (Berkeley) 4/2/94";
     47  1.19       cgd #else
     48  1.50     perry __RCSID("$NetBSD: df.c,v 1.50 2003/12/07 22:42:36 perry Exp $");
     49  1.19       cgd #endif
     50  1.12       cgd #endif /* not lint */
     51  1.12       cgd 
     52  1.12       cgd #include <sys/param.h>
     53  1.12       cgd #include <sys/stat.h>
     54  1.12       cgd #include <sys/mount.h>
     55  1.27       mrg 
     56  1.15   mycroft #include <err.h>
     57  1.12       cgd #include <errno.h>
     58  1.12       cgd #include <fcntl.h>
     59  1.44    provos #include <util.h>
     60  1.12       cgd #include <stdio.h>
     61  1.12       cgd #include <stdlib.h>
     62  1.12       cgd #include <string.h>
     63  1.12       cgd #include <unistd.h>
     64  1.12       cgd 
     65  1.41     enami extern char *strpct(u_long, u_long, u_int);
     66  1.26      fair 
     67  1.41     enami int	 main(int, char *[]);
     68  1.41     enami int	 bread(off_t, void *, int);
     69  1.41     enami char	*getmntpt(char *);
     70  1.41     enami void	 prtstat(struct statfs *, int);
     71  1.41     enami int	 selected(const char *);
     72  1.41     enami void	 maketypelist(char *);
     73  1.41     enami long	 regetmntinfo(struct statfs **, long);
     74  1.41     enami void	 usage(void);
     75  1.44    provos void	 prthumanval(int64_t, char *);
     76  1.44    provos void	 prthuman(struct statfs *, long);
     77  1.12       cgd 
     78  1.47     grant int	aflag, gflag, hflag, iflag, kflag, lflag, mflag, nflag, Pflag;
     79  1.17   mycroft char	**typelist = NULL;
     80  1.12       cgd 
     81  1.12       cgd int
     82  1.41     enami main(int argc, char *argv[])
     83  1.12       cgd {
     84  1.12       cgd 	struct stat stbuf;
     85  1.21       jtc 	struct statfs *mntbuf;
     86  1.17   mycroft 	long mntsize;
     87  1.17   mycroft 	int ch, i, maxwidth, width;
     88  1.12       cgd 	char *mntpt;
     89  1.12       cgd 
     90  1.47     grant 	while ((ch = getopt(argc, argv, "aghiklmnPt:")) != -1)
     91  1.15   mycroft 		switch (ch) {
     92  1.34  christos 		case 'a':
     93  1.34  christos 			aflag = 1;
     94  1.34  christos 			break;
     95  1.47     grant 		case 'g':
     96  1.47     grant 			gflag = 1;
     97  1.46     grant 			break;
     98  1.44    provos 		case 'h':
     99  1.44    provos 			hflag = 1;
    100  1.44    provos 			break;
    101  1.12       cgd 		case 'i':
    102  1.12       cgd 			iflag = 1;
    103  1.12       cgd 			break;
    104  1.12       cgd 		case 'k':
    105  1.12       cgd 			kflag = 1;
    106  1.12       cgd 			break;
    107  1.12       cgd 		case 'l':
    108  1.17   mycroft 			lflag = 1;
    109  1.12       cgd 			break;
    110  1.36   hubertf 		case 'm':
    111  1.36   hubertf 			mflag = 1;
    112  1.36   hubertf 			break;
    113  1.12       cgd 		case 'n':
    114  1.12       cgd 			nflag = 1;
    115  1.12       cgd 			break;
    116  1.35    kleink 		case 'P':
    117  1.35    kleink 			Pflag = 1;
    118  1.35    kleink 			break;
    119  1.16   mycroft 		case 't':
    120  1.17   mycroft 			if (typelist != NULL)
    121  1.17   mycroft 				errx(1, "only one -t option may be specified.");
    122  1.17   mycroft 			maketypelist(optarg);
    123  1.16   mycroft 			break;
    124  1.12       cgd 		case '?':
    125  1.12       cgd 		default:
    126  1.12       cgd 			usage();
    127  1.12       cgd 		}
    128  1.12       cgd 	argc -= optind;
    129  1.12       cgd 	argv += optind;
    130  1.12       cgd 
    131  1.17   mycroft 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    132  1.12       cgd 	if (mntsize == 0)
    133  1.41     enami 		err(1, "retrieving information on mounted file systems");
    134  1.15   mycroft 
    135  1.41     enami 	if (*argv == NULL) {
    136  1.17   mycroft 		mntsize = regetmntinfo(&mntbuf, mntsize);
    137  1.17   mycroft 	} else {
    138  1.17   mycroft 		mntbuf = malloc(argc * sizeof(struct statfs));
    139  1.17   mycroft 		mntsize = 0;
    140  1.41     enami 		for (; *argv != NULL; argv++) {
    141  1.17   mycroft 			if (stat(*argv, &stbuf) < 0) {
    142  1.17   mycroft 				if ((mntpt = getmntpt(*argv)) == 0) {
    143  1.17   mycroft 					warn("%s", *argv);
    144  1.12       cgd 					continue;
    145  1.12       cgd 				}
    146  1.18   mycroft 			} else if (S_ISBLK(stbuf.st_mode)) {
    147  1.43     soren 				if ((mntpt = getmntpt(*argv)) == 0)
    148  1.43     soren 					mntpt = *argv;
    149  1.17   mycroft 			} else
    150  1.17   mycroft 				mntpt = *argv;
    151  1.17   mycroft 			/*
    152  1.17   mycroft 			 * Statfs does not take a `wait' flag, so we cannot
    153  1.17   mycroft 			 * implement nflag here.
    154  1.17   mycroft 			 */
    155  1.17   mycroft 			if (!statfs(mntpt, &mntbuf[mntsize]))
    156  1.23   thorpej 				if (lflag &&
    157  1.23   thorpej 				    (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
    158  1.23   thorpej 					warnx("Warning: %s is not a local %s",
    159  1.23   thorpej 					    *argv, "file system");
    160  1.23   thorpej 				else if
    161  1.23   thorpej 				    (!selected(mntbuf[mntsize].f_fstypename))
    162  1.23   thorpej 					warnx("Warning: %s mounted as a %s %s",
    163  1.23   thorpej 					    *argv,
    164  1.23   thorpej 					    mntbuf[mntsize].f_fstypename,
    165  1.23   thorpej 					    "file system");
    166  1.23   thorpej 				else
    167  1.23   thorpej 					++mntsize;
    168  1.17   mycroft 			else
    169  1.17   mycroft 				warn("%s", *argv);
    170  1.12       cgd 		}
    171  1.12       cgd 	}
    172  1.17   mycroft 
    173  1.17   mycroft 	maxwidth = 0;
    174  1.17   mycroft 	for (i = 0; i < mntsize; i++) {
    175  1.17   mycroft 		width = strlen(mntbuf[i].f_mntfromname);
    176  1.17   mycroft 		if (width > maxwidth)
    177  1.17   mycroft 			maxwidth = width;
    178  1.17   mycroft 	}
    179  1.17   mycroft 	for (i = 0; i < mntsize; i++)
    180  1.17   mycroft 		prtstat(&mntbuf[i], maxwidth);
    181  1.17   mycroft 	exit(0);
    182  1.29   mycroft 	/* NOTREACHED */
    183  1.12       cgd }
    184  1.12       cgd 
    185  1.12       cgd char *
    186  1.41     enami getmntpt(char *name)
    187  1.12       cgd {
    188  1.17   mycroft 	long mntsize, i;
    189  1.17   mycroft 	struct statfs *mntbuf;
    190  1.12       cgd 
    191  1.17   mycroft 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    192  1.12       cgd 	for (i = 0; i < mntsize; i++) {
    193  1.12       cgd 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    194  1.12       cgd 			return (mntbuf[i].f_mntonname);
    195  1.12       cgd 	}
    196  1.12       cgd 	return (0);
    197  1.12       cgd }
    198  1.12       cgd 
    199  1.17   mycroft static enum { IN_LIST, NOT_IN_LIST } which;
    200  1.17   mycroft 
    201  1.17   mycroft int
    202  1.41     enami selected(const char *type)
    203  1.17   mycroft {
    204  1.17   mycroft 	char **av;
    205  1.17   mycroft 
    206  1.17   mycroft 	/* If no type specified, it's always selected. */
    207  1.17   mycroft 	if (typelist == NULL)
    208  1.17   mycroft 		return (1);
    209  1.17   mycroft 	for (av = typelist; *av != NULL; ++av)
    210  1.20       cgd 		if (!strncmp(type, *av, MFSNAMELEN))
    211  1.17   mycroft 			return (which == IN_LIST ? 1 : 0);
    212  1.17   mycroft 	return (which == IN_LIST ? 0 : 1);
    213  1.17   mycroft }
    214  1.16   mycroft 
    215  1.16   mycroft void
    216  1.41     enami maketypelist(char *fslist)
    217  1.16   mycroft {
    218  1.17   mycroft 	int i;
    219  1.17   mycroft 	char *nextcp, **av;
    220  1.16   mycroft 
    221  1.17   mycroft 	if ((fslist == NULL) || (fslist[0] == '\0'))
    222  1.17   mycroft 		errx(1, "empty type list");
    223  1.16   mycroft 
    224  1.17   mycroft 	/*
    225  1.17   mycroft 	 * XXX
    226  1.17   mycroft 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    227  1.17   mycroft 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    228  1.17   mycroft 	 */
    229  1.17   mycroft 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    230  1.17   mycroft 		fslist += 2;
    231  1.17   mycroft 		which = NOT_IN_LIST;
    232  1.17   mycroft 	} else
    233  1.17   mycroft 		which = IN_LIST;
    234  1.17   mycroft 
    235  1.17   mycroft 	/* Count the number of types. */
    236  1.24   thorpej 	for (i = 1, nextcp = fslist;
    237  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++)
    238  1.17   mycroft 		++nextcp;
    239  1.17   mycroft 
    240  1.17   mycroft 	/* Build an array of that many types. */
    241  1.17   mycroft 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    242  1.24   thorpej 		err(1, "can't allocate type array");
    243  1.17   mycroft 	av[0] = fslist;
    244  1.24   thorpej 	for (i = 1, nextcp = fslist;
    245  1.24   thorpej 	    (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    246  1.17   mycroft 		*nextcp = '\0';
    247  1.17   mycroft 		av[i] = ++nextcp;
    248  1.16   mycroft 	}
    249  1.17   mycroft 	/* Terminate the array. */
    250  1.17   mycroft 	av[i] = NULL;
    251  1.17   mycroft }
    252  1.16   mycroft 
    253  1.17   mycroft /*
    254  1.17   mycroft  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    255  1.17   mycroft  * filesystem types not in ``fsmask'' and possibly re-stating to get
    256  1.17   mycroft  * current (not cached) info.  Returns the new count of valid statfs bufs.
    257  1.17   mycroft  */
    258  1.17   mycroft long
    259  1.41     enami regetmntinfo(struct statfs **mntbufp, long mntsize)
    260  1.17   mycroft {
    261  1.17   mycroft 	int i, j;
    262  1.17   mycroft 	struct statfs *mntbuf;
    263  1.16   mycroft 
    264  1.42  christos 	if (!lflag && typelist == NULL && aflag)
    265  1.17   mycroft 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
    266  1.16   mycroft 
    267  1.17   mycroft 	mntbuf = *mntbufp;
    268  1.17   mycroft 	j = 0;
    269  1.17   mycroft 	for (i = 0; i < mntsize; i++) {
    270  1.34  christos 		if (!aflag && (mntbuf[i].f_flags & MNT_IGNORE) != 0)
    271  1.34  christos 			continue;
    272  1.17   mycroft 		if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
    273  1.17   mycroft 			continue;
    274  1.17   mycroft 		if (!selected(mntbuf[i].f_fstypename))
    275  1.17   mycroft 			continue;
    276  1.17   mycroft 		if (nflag)
    277  1.17   mycroft 			mntbuf[j] = mntbuf[i];
    278  1.32  sommerfe 		else {
    279  1.32  sommerfe 			struct statfs layerbuf = mntbuf[i];
    280  1.17   mycroft 			(void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    281  1.32  sommerfe 			/*
    282  1.32  sommerfe 			 * If the FS name changed, then new data is for
    283  1.32  sommerfe 			 * a different layer and we don't want it.
    284  1.32  sommerfe 			 */
    285  1.41     enami 			if (memcmp(layerbuf.f_mntfromname,
    286  1.41     enami 			    mntbuf[j].f_mntfromname, MNAMELEN))
    287  1.41     enami 				mntbuf[j] = layerbuf;
    288  1.32  sommerfe 		}
    289  1.17   mycroft 		j++;
    290  1.16   mycroft 	}
    291  1.17   mycroft 	return (j);
    292  1.16   mycroft }
    293  1.16   mycroft 
    294  1.44    provos void
    295  1.44    provos prthumanval(int64_t bytes, char *pad)
    296  1.44    provos {
    297  1.44    provos 	char buf[5];
    298  1.44    provos 
    299  1.44    provos 	humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
    300  1.44    provos 	    HN_B | HN_NOSPACE | HN_DECIMAL);
    301  1.44    provos 
    302  1.44    provos 	(void)printf("%s %6s", pad, buf);
    303  1.44    provos }
    304  1.44    provos 
    305  1.44    provos void
    306  1.44    provos prthuman(struct statfs *sfsp, long used)
    307  1.44    provos {
    308  1.44    provos        prthumanval((int64_t)(sfsp->f_blocks) * (int64_t)(sfsp->f_bsize), "");
    309  1.44    provos        prthumanval((int64_t)(used) * (int64_t)(sfsp->f_bsize), "  ");
    310  1.44    provos        prthumanval((int64_t)(sfsp->f_bavail) * (int64_t)(sfsp->f_bsize), "   ");
    311  1.44    provos }
    312  1.44    provos 
    313  1.44    provos 
    314  1.12       cgd /*
    315  1.15   mycroft  * Convert statfs returned filesystem size into BLOCKSIZE units.
    316  1.15   mycroft  * Attempts to avoid overflow for large filesystems.
    317  1.15   mycroft  */
    318  1.41     enami #define fsbtoblk(num, fsbs, bs)					\
    319  1.41     enami 	(((fsbs) != 0 && (fsbs) < (bs)) ?			\
    320  1.41     enami 	    (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
    321  1.15   mycroft 
    322  1.15   mycroft /*
    323  1.12       cgd  * Print out status about a filesystem.
    324  1.12       cgd  */
    325  1.12       cgd void
    326  1.41     enami prtstat(struct statfs *sfsp, int maxwidth)
    327  1.12       cgd {
    328  1.17   mycroft 	static long blocksize;
    329  1.12       cgd 	static int headerlen, timesthrough;
    330  1.12       cgd 	static char *header;
    331  1.12       cgd 	long used, availblks, inodes;
    332  1.41     enami 	static char *full = "100%";
    333  1.50     perry 	static char *empty = "  0%";
    334  1.12       cgd 
    335  1.12       cgd 	if (maxwidth < 11)
    336  1.12       cgd 		maxwidth = 11;
    337  1.12       cgd 	if (++timesthrough == 1) {
    338  1.12       cgd 		if (kflag) {
    339  1.17   mycroft 			blocksize = 1024;
    340  1.35    kleink 			header = Pflag ? "1024-blocks" : "1K-blocks";
    341  1.12       cgd 			headerlen = strlen(header);
    342  1.36   hubertf 		} else if (mflag) {
    343  1.36   hubertf 			blocksize = 1024 * 1024;
    344  1.36   hubertf 			header = Pflag ? "1048576-blocks" : "1M-blocks";
    345  1.36   hubertf 			headerlen = strlen(header);
    346  1.47     grant 		} else if (gflag) {
    347  1.46     grant 			blocksize = 1024 * 1024 * 1024;
    348  1.46     grant 			header = Pflag ? "1073741824-blocks" : "1G-blocks";
    349  1.46     grant 			headerlen = strlen(header);
    350  1.44    provos 		} else if (hflag) {
    351  1.44    provos 			header = "  Size";
    352  1.44    provos 			headerlen = strlen(header);
    353  1.12       cgd 		} else
    354  1.12       cgd 			header = getbsize(&headerlen, &blocksize);
    355  1.35    kleink 		(void)printf("%-*.*s %s     Used %9s Capacity",
    356  1.35    kleink 		    maxwidth, maxwidth, "Filesystem", header,
    357  1.35    kleink 		    Pflag ? "Available" : "Avail");
    358  1.12       cgd 		if (iflag)
    359  1.48    simonb 			(void)printf("  iused    ifree  %%iused");
    360  1.12       cgd 		(void)printf("  Mounted on\n");
    361  1.12       cgd 	}
    362  1.12       cgd 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    363  1.12       cgd 	used = sfsp->f_blocks - sfsp->f_bfree;
    364  1.12       cgd 	availblks = sfsp->f_bavail + used;
    365  1.44    provos 	if (hflag)
    366  1.44    provos 		prthuman(sfsp, used);
    367  1.44    provos 	else
    368  1.44    provos 		(void)printf(" %*ld %8ld %9ld", headerlen,
    369  1.44    provos 		    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
    370  1.44    provos 		    fsbtoblk(used, sfsp->f_bsize, blocksize),
    371  1.44    provos 		    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
    372  1.39   tsutsui 	(void)printf("%7s",
    373  1.31  wsanchez 	    availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0));
    374  1.12       cgd 	if (iflag) {
    375  1.12       cgd 		inodes = sfsp->f_files;
    376  1.12       cgd 		used = inodes - sfsp->f_ffree;
    377  1.48    simonb 		(void)printf(" %8ld %8ld %6s ", used, sfsp->f_ffree,
    378  1.50     perry 		    inodes == 0 ? (used == 0 ? empty : full ) :
    379  1.41     enami 		    strpct((u_long)used, (u_long)inodes, 0));
    380  1.41     enami 	} else
    381  1.12       cgd 		(void)printf("  ");
    382  1.12       cgd 	(void)printf("  %s\n", sfsp->f_mntonname);
    383  1.12       cgd }
    384  1.12       cgd 
    385  1.12       cgd void
    386  1.41     enami usage(void)
    387  1.12       cgd {
    388  1.41     enami 
    389  1.34  christos 	(void)fprintf(stderr,
    390  1.47     grant 	    "Usage: %s [-aghiklmnP] [-t type] [file | file_system ...]\n",
    391  1.40       cgd 	    getprogname());
    392  1.12       cgd 	exit(1);
    393  1.30   mycroft 	/* NOTREACHED */
    394  1.12       cgd }
    395