Home | History | Annotate | Line # | Download | only in df
df.c revision 1.19
      1 /*	$NetBSD: df.c,v 1.19 1995/03/21 09:04:17 cgd 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.19 1995/03/21 09:04:17 cgd 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 statfsbuf, *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 	if (*argv && (lflag || typelist != NULL))
    118 		errx(1, "-l or -t does not make sense with list of mount points");
    119 
    120 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    121 	if (mntsize == 0)
    122 	        err(1, "retrieving information on mounted file systems");
    123 
    124 	if (!*argv) {
    125 		mntsize = regetmntinfo(&mntbuf, mntsize);
    126 	} else {
    127 		mntbuf = malloc(argc * sizeof(struct statfs));
    128 		mntsize = 0;
    129 		for (; *argv; argv++) {
    130 			if (stat(*argv, &stbuf) < 0) {
    131 				if ((mntpt = getmntpt(*argv)) == 0) {
    132 					warn("%s", *argv);
    133 					continue;
    134 				}
    135 			} else if (S_ISCHR(stbuf.st_mode)) {
    136 				if (!ufs_df(*argv, &mntbuf[mntsize]))
    137 					++mntsize;
    138 				continue;
    139 			} else if (S_ISBLK(stbuf.st_mode)) {
    140 				if ((mntpt = getmntpt(*argv)) == 0) {
    141 					mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
    142 					mdev.fspec = *argv;
    143 					if (mkdir(mntpt, DEFFILEMODE) != 0) {
    144 						warn("%s", mntpt);
    145 						continue;
    146 					}
    147 					if (mount(MOUNT_UFS, mntpt, MNT_RDONLY,
    148 					    &mdev) != 0) {
    149 						(void)rmdir(mntpt);
    150 						if (!ufs_df(*argv, &mntbuf[mntsize]))
    151 							++mntsize;
    152 						continue;
    153 					} else if (!statfs(mntpt, &mntbuf[mntsize])) {
    154 						mntbuf[mntsize].f_mntonname[0] = '\0';
    155 						++mntsize;
    156 					} else
    157 						warn("%s", *argv);
    158 					(void)unmount(mntpt, 0);
    159 					(void)rmdir(mntpt);
    160 					continue;
    161 				}
    162 			} else
    163 				mntpt = *argv;
    164 			/*
    165 			 * Statfs does not take a `wait' flag, so we cannot
    166 			 * implement nflag here.
    167 			 */
    168 			if (!statfs(mntpt, &mntbuf[mntsize]))
    169 				++mntsize;
    170 			else
    171 				warn("%s", *argv);
    172 		}
    173 	}
    174 
    175 	maxwidth = 0;
    176 	for (i = 0; i < mntsize; i++) {
    177 		width = strlen(mntbuf[i].f_mntfromname);
    178 		if (width > maxwidth)
    179 			maxwidth = width;
    180 	}
    181 	for (i = 0; i < mntsize; i++)
    182 		prtstat(&mntbuf[i], maxwidth);
    183 	exit(0);
    184 }
    185 
    186 char *
    187 getmntpt(name)
    188 	char *name;
    189 {
    190 	long mntsize, i;
    191 	struct statfs *mntbuf;
    192 
    193 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    194 	for (i = 0; i < mntsize; i++) {
    195 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    196 			return (mntbuf[i].f_mntonname);
    197 	}
    198 	return (0);
    199 }
    200 
    201 static enum { IN_LIST, NOT_IN_LIST } which;
    202 
    203 int
    204 selected(type)
    205 	const char *type;
    206 {
    207 	char **av;
    208 
    209 	/* If no type specified, it's always selected. */
    210 	if (typelist == NULL)
    211 		return (1);
    212 	for (av = typelist; *av != NULL; ++av)
    213 		if (!strcmp(type, *av))
    214 			return (which == IN_LIST ? 1 : 0);
    215 	return (which == IN_LIST ? 0 : 1);
    216 }
    217 
    218 void
    219 maketypelist(fslist)
    220 	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; nextcp = strchr(nextcp, ','); i++)
    241 		++nextcp;
    242 
    243 	/* Build an array of that many types. */
    244 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    245 		err(1, NULL);
    246 	av[0] = fslist;
    247 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
    248 		*nextcp = '\0';
    249 		av[i] = ++nextcp;
    250 	}
    251 	/* Terminate the array. */
    252 	av[i] = NULL;
    253 }
    254 
    255 /*
    256  * Make a pass over the filesystem info in ``mntbuf'' filtering out
    257  * filesystem types not in ``fsmask'' and possibly re-stating to get
    258  * current (not cached) info.  Returns the new count of valid statfs bufs.
    259  */
    260 long
    261 regetmntinfo(mntbufp, mntsize)
    262 	struct statfs **mntbufp;
    263 	long mntsize;
    264 {
    265 	int i, j;
    266 	struct statfs *mntbuf;
    267 
    268 	if (!lflag && typelist == NULL)
    269 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
    270 
    271 	mntbuf = *mntbufp;
    272 	j = 0;
    273 	for (i = 0; i < mntsize; i++) {
    274 		if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
    275 			continue;
    276 		if (!selected(mntbuf[i].f_fstypename))
    277 			continue;
    278 		if (nflag)
    279 			mntbuf[j] = mntbuf[i];
    280 		else
    281 			(void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
    282 		j++;
    283 	}
    284 	return (j);
    285 }
    286 
    287 /*
    288  * Convert statfs returned filesystem size into BLOCKSIZE units.
    289  * Attempts to avoid overflow for large filesystems.
    290  */
    291 #define fsbtoblk(num, fsbs, bs) \
    292 	(((fsbs) != 0 && (fsbs) < (bs)) ? \
    293 		(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
    294 
    295 /*
    296  * Print out status about a filesystem.
    297  */
    298 void
    299 prtstat(sfsp, maxwidth)
    300 	struct statfs *sfsp;
    301 	int maxwidth;
    302 {
    303 	static long blocksize;
    304 	static int headerlen, timesthrough;
    305 	static char *header;
    306 	long used, availblks, inodes;
    307 
    308 	if (maxwidth < 11)
    309 		maxwidth = 11;
    310 	if (++timesthrough == 1) {
    311 		if (kflag) {
    312 			blocksize = 1024;
    313 			header = "1K-blocks";
    314 			headerlen = strlen(header);
    315 		} else
    316 			header = getbsize(&headerlen, &blocksize);
    317 		(void)printf("%-*.*s %s     Used    Avail Capacity",
    318 		    maxwidth, maxwidth, "Filesystem", header);
    319 		if (iflag)
    320 			(void)printf(" iused   ifree  %%iused");
    321 		(void)printf("  Mounted on\n");
    322 	}
    323 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    324 	used = sfsp->f_blocks - sfsp->f_bfree;
    325 	availblks = sfsp->f_bavail + used;
    326 	(void)printf(" %*ld %8ld %8ld", headerlen,
    327 	    fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
    328 	    fsbtoblk(used, sfsp->f_bsize, blocksize),
    329 	    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
    330 	(void)printf(" %5.0f%%",
    331 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
    332 	if (iflag) {
    333 		inodes = sfsp->f_files;
    334 		used = inodes - sfsp->f_ffree;
    335 		(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
    336 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
    337 	} else
    338 		(void)printf("  ");
    339 	(void)printf("  %s\n", sfsp->f_mntonname);
    340 }
    341 
    342 /*
    343  * This code constitutes the pre-system call Berkeley df code for extracting
    344  * information from filesystem superblocks.
    345  */
    346 #include <ufs/ffs/fs.h>
    347 #include <errno.h>
    348 #include <fstab.h>
    349 
    350 union {
    351 	struct fs iu_fs;
    352 	char dummy[SBSIZE];
    353 } sb;
    354 #define sblock sb.iu_fs
    355 
    356 int	rfd;
    357 
    358 int
    359 ufs_df(file, sfsp)
    360 	char *file;
    361 	struct statfs *sfsp;
    362 {
    363 	char *mntpt;
    364 	static int synced;
    365 
    366 	if (synced++ == 0)
    367 		sync();
    368 
    369 	if ((rfd = open(file, O_RDONLY)) < 0) {
    370 		warn("%s", file);
    371 		return (-1);
    372 	}
    373 	if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
    374 		(void)close(rfd);
    375 		return (-1);
    376 	}
    377 	sfsp->f_type = 0;
    378 	sfsp->f_flags = 0;
    379 	sfsp->f_bsize = sblock.fs_fsize;
    380 	sfsp->f_iosize = sblock.fs_bsize;
    381 	sfsp->f_blocks = sblock.fs_dsize;
    382 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
    383 		sblock.fs_cstotal.cs_nffree;
    384 	sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
    385 		(sblock.fs_dsize - sfsp->f_bfree);
    386 	if (sfsp->f_bavail < 0)
    387 		sfsp->f_bavail = 0;
    388 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
    389 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
    390 	sfsp->f_fsid.val[0] = 0;
    391 	sfsp->f_fsid.val[1] = 0;
    392 	if ((mntpt = getmntpt(file)) == 0)
    393 		mntpt = "";
    394 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
    395 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
    396 	strncpy(sfsp->f_fstypename, MOUNT_UFS, MFSNAMELEN);
    397 	sfsp->f_fstypename[MFSNAMELEN] = '\0';
    398 	(void)close(rfd);
    399 	return (0);
    400 }
    401 
    402 int
    403 bread(off, buf, cnt)
    404 	off_t off;
    405 	void *buf;
    406 	int cnt;
    407 {
    408 	int nr;
    409 
    410 	(void)lseek(rfd, off, SEEK_SET);
    411 	if ((nr = read(rfd, buf, cnt)) != cnt) {
    412 		/* Probably a dismounted disk if errno == EIO. */
    413 		if (errno != EIO)
    414 			(void)fprintf(stderr, "\ndf: %qd: %s\n",
    415 			    off, strerror(nr > 0 ? EIO : errno));
    416 		return (0);
    417 	}
    418 	return (1);
    419 }
    420 
    421 void
    422 usage()
    423 {
    424 	(void)fprintf(stderr, "usage: df [-ikln] [-t type] [file | file_system ...]\n");
    425 	exit(1);
    426 }
    427