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