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