Home | History | Annotate | Line # | Download | only in df
df.c revision 1.23
      1 /*	$NetBSD: df.c,v 1.23 1996/12/11 03:48:42 thorpej 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 #ifndef lint
     42 static char copyright[] =
     43 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
     44 	The Regents of the University of California.  All rights reserved.\n";
     45 #endif /* not lint */
     46 
     47 #ifndef lint
     48 #if 0
     49 static char sccsid[] = "@(#)df.c	8.7 (Berkeley) 4/2/94";
     50 #else
     51 static char rcsid[] = "$NetBSD: df.c,v 1.23 1996/12/11 03:48:42 thorpej Exp $";
     52 #endif
     53 #endif /* not lint */
     54 
     55 #include <sys/param.h>
     56 #include <sys/stat.h>
     57 #include <sys/mount.h>
     58 
     59 #include <err.h>
     60 #include <errno.h>
     61 #include <fcntl.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 int	 bread __P((off_t, void *, int));
     68 char	*getmntpt __P((char *));
     69 void	 prtstat __P((struct statfs *, int));
     70 int	 ufs_df __P((char *, struct statfs *));
     71 int	 selected __P((const char *));
     72 void	 maketypelist __P((char *));
     73 long	 regetmntinfo __P((struct statfs **, long));
     74 void	 usage __P((void));
     75 
     76 int	iflag, kflag, lflag, nflag;
     77 char	**typelist = NULL;
     78 struct	ufs_args mdev;
     79 
     80 int
     81 main(argc, argv)
     82 	int argc;
     83 	char *argv[];
     84 {
     85 	struct stat stbuf;
     86 	struct statfs *mntbuf;
     87 	long mntsize;
     88 	int ch, i, maxwidth, width;
     89 	char *mntpt;
     90 
     91 	while ((ch = getopt(argc, argv, "iklnt:")) != -1)
     92 		switch (ch) {
     93 		case 'i':
     94 			iflag = 1;
     95 			break;
     96 		case 'k':
     97 			kflag = 1;
     98 			break;
     99 		case 'l':
    100 			lflag = 1;
    101 			break;
    102 		case 'n':
    103 			nflag = 1;
    104 			break;
    105 		case 't':
    106 			if (typelist != NULL)
    107 				errx(1, "only one -t option may be specified.");
    108 			maketypelist(optarg);
    109 			break;
    110 		case '?':
    111 		default:
    112 			usage();
    113 		}
    114 	argc -= optind;
    115 	argv += optind;
    116 
    117 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    118 	if (mntsize == 0)
    119 	        err(1, "retrieving information on mounted file systems");
    120 
    121 	if (!*argv) {
    122 		mntsize = regetmntinfo(&mntbuf, mntsize);
    123 	} else {
    124 		mntbuf = malloc(argc * sizeof(struct statfs));
    125 		mntsize = 0;
    126 		for (; *argv; argv++) {
    127 			if (stat(*argv, &stbuf) < 0) {
    128 				if ((mntpt = getmntpt(*argv)) == 0) {
    129 					warn("%s", *argv);
    130 					continue;
    131 				}
    132 			} else if (S_ISCHR(stbuf.st_mode)) {
    133 				if (!ufs_df(*argv, &mntbuf[mntsize]))
    134 					++mntsize;
    135 				continue;
    136 			} else if (S_ISBLK(stbuf.st_mode)) {
    137 				if ((mntpt = getmntpt(*argv)) == 0) {
    138 					mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
    139 					mdev.fspec = *argv;
    140 					if (mkdir(mntpt, DEFFILEMODE) != 0) {
    141 						warn("%s", mntpt);
    142 						continue;
    143 					}
    144 					if (mount(MOUNT_FFS, mntpt, MNT_RDONLY,
    145 					    &mdev) != 0) {
    146 						(void)rmdir(mntpt);
    147 						if (!ufs_df(*argv, &mntbuf[mntsize]))
    148 							++mntsize;
    149 						continue;
    150 					} else if (!statfs(mntpt, &mntbuf[mntsize])) {
    151 						mntbuf[mntsize].f_mntonname[0] = '\0';
    152 						++mntsize;
    153 					} else
    154 						warn("%s", *argv);
    155 					(void)unmount(mntpt, 0);
    156 					(void)rmdir(mntpt);
    157 					continue;
    158 				}
    159 			} else
    160 				mntpt = *argv;
    161 			/*
    162 			 * Statfs does not take a `wait' flag, so we cannot
    163 			 * implement nflag here.
    164 			 */
    165 			if (!statfs(mntpt, &mntbuf[mntsize]))
    166 				if (lflag &&
    167 				    (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
    168 					warnx("Warning: %s is not a local %s",
    169 					    *argv, "file system");
    170 				else if
    171 				    (!selected(mntbuf[mntsize].f_fstypename))
    172 					warnx("Warning: %s mounted as a %s %s",
    173 					    *argv,
    174 					    mntbuf[mntsize].f_fstypename,
    175 					    "file system");
    176 				else
    177 					++mntsize;
    178 			else
    179 				warn("%s", *argv);
    180 		}
    181 	}
    182 
    183 	maxwidth = 0;
    184 	for (i = 0; i < mntsize; i++) {
    185 		width = strlen(mntbuf[i].f_mntfromname);
    186 		if (width > maxwidth)
    187 			maxwidth = width;
    188 	}
    189 	for (i = 0; i < mntsize; i++)
    190 		prtstat(&mntbuf[i], maxwidth);
    191 	exit(0);
    192 }
    193 
    194 char *
    195 getmntpt(name)
    196 	char *name;
    197 {
    198 	long mntsize, i;
    199 	struct statfs *mntbuf;
    200 
    201 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    202 	for (i = 0; i < mntsize; i++) {
    203 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    204 			return (mntbuf[i].f_mntonname);
    205 	}
    206 	return (0);
    207 }
    208 
    209 static enum { IN_LIST, NOT_IN_LIST } which;
    210 
    211 int
    212 selected(type)
    213 	const char *type;
    214 {
    215 	char **av;
    216 
    217 	/* If no type specified, it's always selected. */
    218 	if (typelist == NULL)
    219 		return (1);
    220 	for (av = typelist; *av != NULL; ++av)
    221 		if (!strncmp(type, *av, MFSNAMELEN))
    222 			return (which == IN_LIST ? 1 : 0);
    223 	return (which == IN_LIST ? 0 : 1);
    224 }
    225 
    226 void
    227 maketypelist(fslist)
    228 	char *fslist;
    229 {
    230 	int i;
    231 	char *nextcp, **av;
    232 
    233 	if ((fslist == NULL) || (fslist[0] == '\0'))
    234 		errx(1, "empty type list");
    235 
    236 	/*
    237 	 * XXX
    238 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    239 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    240 	 */
    241 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    242 		fslist += 2;
    243 		which = NOT_IN_LIST;
    244 	} else
    245 		which = IN_LIST;
    246 
    247 	/* Count the number of types. */
    248 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
    249 		++nextcp;
    250 
    251 	/* Build an array of that many types. */
    252 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    253 		err(1, NULL);
    254 	av[0] = fslist;
    255 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
    256 		*nextcp = '\0';
    257 		av[i] = ++nextcp;
    258 	}
    259 	/* Terminate the array. */
    260 	av[i] = NULL;
    261 }
    262 
    263 /*
    264  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    265  * filesystem types not in ``fsmask'' and possibly re-stating to get
    266  * current (not cached) info.  Returns the new count of valid statfs bufs.
    267  */
    268 long
    269 regetmntinfo(mntbufp, mntsize)
    270 	struct statfs **mntbufp;
    271 	long mntsize;
    272 {
    273 	int i, j;
    274 	struct statfs *mntbuf;
    275 
    276 	if (!lflag && typelist == NULL)
    277 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
    278 
    279 	mntbuf = *mntbufp;
    280 	j = 0;
    281 	for (i = 0; i < mntsize; i++) {
    282 		if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
    283 			continue;
    284 		if (!selected(mntbuf[i].f_fstypename))
    285 			continue;
    286 		if (nflag)
    287 			mntbuf[j] = mntbuf[i];
    288 		else
    289 			(void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    290 		j++;
    291 	}
    292 	return (j);
    293 }
    294 
    295 /*
    296  * Convert statfs returned filesystem size into BLOCKSIZE units.
    297  * Attempts to avoid overflow for large filesystems.
    298  */
    299 #define fsbtoblk(num, fsbs, bs) \
    300 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
    301 		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
    302 
    303 /*
    304  * Print out status about a filesystem.
    305  */
    306 void
    307 prtstat(sfsp, maxwidth)
    308 	struct statfs *sfsp;
    309 	int maxwidth;
    310 {
    311 	static long blocksize;
    312 	static int headerlen, timesthrough;
    313 	static char *header;
    314 	long used, availblks, inodes;
    315 
    316 	if (maxwidth < 11)
    317 		maxwidth = 11;
    318 	if (++timesthrough == 1) {
    319 		if (kflag) {
    320 			blocksize = 1024;
    321 			header = "1K-blocks";
    322 			headerlen = strlen(header);
    323 		} else
    324 			header = getbsize(&headerlen, &blocksize);
    325 		(void)printf("%-*.*s %s     Used    Avail Capacity",
    326 		    maxwidth, maxwidth, "Filesystem", header);
    327 		if (iflag)
    328 			(void)printf(" iused   ifree  %%iused");
    329 		(void)printf("  Mounted on\n");
    330 	}
    331 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    332 	used = sfsp->f_blocks - sfsp->f_bfree;
    333 	availblks = sfsp->f_bavail + used;
    334 	(void)printf(" %*ld %8ld %8ld", headerlen,
    335 	    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
    336 	    fsbtoblk(used, sfsp->f_bsize, blocksize),
    337 	    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
    338 	(void)printf(" %5.0f%%",
    339 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
    340 	if (iflag) {
    341 		inodes = sfsp->f_files;
    342 		used = inodes - sfsp->f_ffree;
    343 		(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
    344 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
    345 	} else
    346 		(void)printf("  ");
    347 	(void)printf("  %s\n", sfsp->f_mntonname);
    348 }
    349 
    350 /*
    351  * This code constitutes the pre-system call Berkeley df code for extracting
    352  * information from filesystem superblocks.
    353  */
    354 #include <ufs/ffs/fs.h>
    355 #include <errno.h>
    356 #include <fstab.h>
    357 
    358 union {
    359 	struct fs iu_fs;
    360 	char dummy[SBSIZE];
    361 } sb;
    362 #define sblock sb.iu_fs
    363 
    364 int	rfd;
    365 
    366 int
    367 ufs_df(file, sfsp)
    368 	char *file;
    369 	struct statfs *sfsp;
    370 {
    371 	char *mntpt;
    372 	static int synced;
    373 
    374 	if (synced++ == 0)
    375 		sync();
    376 
    377 	if ((rfd = open(file, O_RDONLY)) < 0) {
    378 		warn("%s", file);
    379 		return (-1);
    380 	}
    381 	if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
    382 		(void)close(rfd);
    383 		return (-1);
    384 	}
    385 	sfsp->f_type = 0;
    386 	sfsp->f_flags = 0;
    387 	sfsp->f_bsize = sblock.fs_fsize;
    388 	sfsp->f_iosize = sblock.fs_bsize;
    389 	sfsp->f_blocks = sblock.fs_dsize;
    390 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
    391 		sblock.fs_cstotal.cs_nffree;
    392 	sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
    393 		(sblock.fs_dsize - sfsp->f_bfree);
    394 	if (sfsp->f_bavail < 0)
    395 		sfsp->f_bavail = 0;
    396 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
    397 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
    398 	sfsp->f_fsid.val[0] = 0;
    399 	sfsp->f_fsid.val[1] = 0;
    400 	if ((mntpt = getmntpt(file)) == 0)
    401 		mntpt = "";
    402 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
    403 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
    404 	strncpy(sfsp->f_fstypename, MOUNT_FFS, MFSNAMELEN);
    405 	(void)close(rfd);
    406 	return (0);
    407 }
    408 
    409 int
    410 bread(off, buf, cnt)
    411 	off_t off;
    412 	void *buf;
    413 	int cnt;
    414 {
    415 	int nr;
    416 
    417 	(void)lseek(rfd, off, SEEK_SET);
    418 	if ((nr = read(rfd, buf, cnt)) != cnt) {
    419 		/* Probably a dismounted disk if errno == EIO. */
    420 		if (errno != EIO)
    421 			(void)fprintf(stderr, "\ndf: %qd: %s\n",
    422 			    off, strerror(nr > 0 ? EIO : errno));
    423 		return (0);
    424 	}
    425 	return (1);
    426 }
    427 
    428 void
    429 usage()
    430 {
    431 	(void)fprintf(stderr, "usage: df [-ikln] [-t type] [file | file_system ...]\n");
    432 	exit(1);
    433 }
    434