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