Home | History | Annotate | Line # | Download | only in df
df.c revision 1.14
      1  1.12     cgd /*
      2  1.12     cgd  * Copyright (c) 1980, 1990 The Regents of the University of California.
      3  1.12     cgd  * All rights reserved.
      4  1.12     cgd  * (c) UNIX System Laboratories, Inc.
      5  1.12     cgd  * All or some portions of this file are derived from material licensed
      6  1.12     cgd  * to the University of California by American Telephone and Telegraph
      7  1.12     cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  1.12     cgd  * the permission of UNIX System Laboratories, Inc.
      9  1.12     cgd  *
     10  1.12     cgd  * Redistribution and use in source and binary forms, with or without
     11  1.12     cgd  * modification, are permitted provided that the following conditions
     12  1.12     cgd  * are met:
     13  1.12     cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.12     cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.12     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.12     cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.12     cgd  *    documentation and/or other materials provided with the distribution.
     18  1.12     cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.12     cgd  *    must display the following acknowledgement:
     20  1.12     cgd  *	This product includes software developed by the University of
     21  1.12     cgd  *	California, Berkeley and its contributors.
     22  1.12     cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.12     cgd  *    may be used to endorse or promote products derived from this software
     24  1.12     cgd  *    without specific prior written permission.
     25  1.12     cgd  *
     26  1.12     cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.12     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.12     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.12     cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.12     cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.12     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.12     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.12     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.12     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.12     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.12     cgd  * SUCH DAMAGE.
     37  1.12     cgd  */
     38  1.12     cgd 
     39  1.12     cgd #ifndef lint
     40  1.12     cgd char copyright[] =
     41  1.12     cgd "@(#) Copyright (c) 1980, 1990 The Regents of the University of California.\n\
     42  1.12     cgd  All rights reserved.\n";
     43  1.12     cgd #endif /* not lint */
     44  1.12     cgd 
     45  1.12     cgd #ifndef lint
     46  1.12     cgd /*static char sccsid[] = "from: @(#)df.c	5.30 (Berkeley) 4/23/92";*/
     47  1.14   glass static char rcsid[] = "$Id: df.c,v 1.14 1994/07/12 07:58:27 glass Exp $";
     48  1.12     cgd #endif /* not lint */
     49  1.12     cgd 
     50  1.12     cgd #include <sys/param.h>
     51  1.12     cgd #include <sys/stat.h>
     52  1.12     cgd #include <sys/mount.h>
     53  1.12     cgd #include <errno.h>
     54  1.12     cgd #include <err.h>
     55  1.12     cgd #include <fcntl.h>
     56  1.12     cgd #include <stdio.h>
     57  1.12     cgd #include <stdlib.h>
     58  1.12     cgd #include <string.h>
     59  1.12     cgd 
     60  1.12     cgd #include <unistd.h>
     61  1.12     cgd 
     62  1.12     cgd int	 bread __P((long, char *, int));
     63  1.12     cgd char	*getmntpt __P((char *));
     64  1.12     cgd void	 prtstat __P((struct statfs *, long));
     65  1.12     cgd void	 ufs_df __P((char *, long));
     66  1.12     cgd void	 usage __P((void));
     67  1.12     cgd 
     68  1.12     cgd int	iflag, kflag = 0, nflag, lflag;
     69  1.12     cgd long	blocksize, mntsize;
     70  1.12     cgd struct	statfs *mntbuf;
     71  1.12     cgd struct	ufs_args mdev;
     72  1.12     cgd 
     73  1.12     cgd int
     74  1.12     cgd main(argc, argv)
     75  1.12     cgd 	int argc;
     76  1.12     cgd 	char *argv[];
     77  1.12     cgd {
     78  1.12     cgd 	struct stat stbuf;
     79  1.12     cgd 	struct statfs statfsbuf;
     80  1.12     cgd 	long width, maxwidth;
     81  1.12     cgd 	int ch, i;
     82  1.12     cgd 	char *mntpt;
     83  1.12     cgd 
     84  1.12     cgd 	while ((ch = getopt(argc, argv, "ikln")) != EOF)
     85  1.12     cgd 		switch(ch) {
     86  1.12     cgd 		case 'i':
     87  1.12     cgd 			iflag = 1;
     88  1.12     cgd 			break;
     89  1.12     cgd 		case 'k':
     90  1.12     cgd 			blocksize = 1024;
     91  1.12     cgd 			kflag = 1;
     92  1.12     cgd 			break;
     93  1.12     cgd 		case 'l':
     94  1.12     cgd 			lflag = 1;
     95  1.12     cgd 			break;
     96  1.12     cgd 		case 'n':
     97  1.12     cgd 			nflag = 1;
     98  1.12     cgd 			break;
     99  1.12     cgd 		case '?':
    100  1.12     cgd 		default:
    101  1.12     cgd 			usage();
    102  1.12     cgd 		}
    103  1.12     cgd 	argc -= optind;
    104  1.12     cgd 	argv += optind;
    105  1.12     cgd 
    106  1.12     cgd 	if (*argv && lflag)
    107  1.12     cgd 		errx(1, "-l does not make sense with list of mount points");
    108  1.12     cgd 
    109  1.12     cgd 	mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
    110  1.12     cgd 	if (mntsize == 0)
    111  1.14   glass 	        err(1, "retrieving information on mounted file systems");
    112  1.12     cgd 	maxwidth = 0;
    113  1.12     cgd 	for (i = 0; i < mntsize; i++) {
    114  1.12     cgd 		width = strlen(mntbuf[i].f_mntfromname);
    115  1.12     cgd 		if (width > maxwidth)
    116  1.12     cgd 			maxwidth = width;
    117  1.12     cgd 	}
    118  1.12     cgd 	if (!*argv) {
    119  1.12     cgd 		for (i = 0; i < mntsize; i++)
    120  1.12     cgd 			if (!lflag || mntbuf[i].f_flags & MNT_LOCAL)
    121  1.12     cgd 				prtstat(&mntbuf[i], maxwidth);
    122  1.12     cgd 		exit(0);
    123  1.12     cgd 	}
    124  1.12     cgd 	for (; *argv; argv++) {
    125  1.12     cgd 		if (stat(*argv, &stbuf) < 0) {
    126  1.12     cgd 			if ((mntpt = getmntpt(*argv)) == 0) {
    127  1.12     cgd 				warn("%s", *argv);
    128  1.12     cgd 				continue;
    129  1.12     cgd 			}
    130  1.12     cgd 		} else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
    131  1.12     cgd 			ufs_df(*argv, maxwidth);
    132  1.12     cgd 			continue;
    133  1.12     cgd 		} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
    134  1.12     cgd 			if ((mntpt = getmntpt(*argv)) == 0) {
    135  1.12     cgd 				mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
    136  1.12     cgd 				mdev.fspec = *argv;
    137  1.12     cgd 				if (mkdir(mntpt, DEFFILEMODE) != 0) {
    138  1.12     cgd 					warn("%s", mntpt);
    139  1.12     cgd 					continue;
    140  1.12     cgd 				}
    141  1.12     cgd 				if (mount(MOUNT_UFS, mntpt, MNT_RDONLY,
    142  1.12     cgd 				    &mdev) != 0) {
    143  1.12     cgd 					ufs_df(*argv, maxwidth);
    144  1.12     cgd 					(void)rmdir(mntpt);
    145  1.12     cgd 					continue;
    146  1.12     cgd 				} else if (statfs(mntpt, &statfsbuf)) {
    147  1.12     cgd 					statfsbuf.f_mntonname[0] = '\0';
    148  1.12     cgd 					prtstat(&statfsbuf, maxwidth);
    149  1.12     cgd 				} else
    150  1.12     cgd 					warn("%s", *argv);
    151  1.13  chopps 				(void)unmount(mntpt, 0);
    152  1.12     cgd 				(void)rmdir(mntpt);
    153  1.12     cgd 				continue;
    154  1.12     cgd 			}
    155  1.12     cgd 		} else
    156  1.12     cgd 			mntpt = *argv;
    157  1.12     cgd 		/*
    158  1.12     cgd 		 * Statfs does not take a `wait' flag, so we cannot
    159  1.12     cgd 		 * implement nflag here.
    160  1.12     cgd 		 */
    161  1.12     cgd 		if (statfs(mntpt, &statfsbuf) < 0) {
    162  1.12     cgd 			warn("%s", mntpt);
    163  1.12     cgd 			continue;
    164  1.12     cgd 		}
    165  1.12     cgd 		if (argc == 1)
    166  1.12     cgd 			maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
    167  1.12     cgd 		prtstat(&statfsbuf, maxwidth);
    168  1.12     cgd 	}
    169  1.12     cgd 	return (0);
    170  1.12     cgd }
    171  1.12     cgd 
    172  1.12     cgd char *
    173  1.12     cgd getmntpt(name)
    174  1.12     cgd 	char *name;
    175  1.12     cgd {
    176  1.12     cgd 	long i;
    177  1.12     cgd 
    178  1.12     cgd 	for (i = 0; i < mntsize; i++) {
    179  1.12     cgd 		if (!strcmp(mntbuf[i].f_mntfromname, name))
    180  1.12     cgd 			return (mntbuf[i].f_mntonname);
    181  1.12     cgd 	}
    182  1.12     cgd 	return (0);
    183  1.12     cgd }
    184  1.12     cgd 
    185  1.12     cgd /*
    186  1.12     cgd  * Print out status about a filesystem.
    187  1.12     cgd  */
    188  1.12     cgd void
    189  1.12     cgd prtstat(sfsp, maxwidth)
    190  1.12     cgd 	register struct statfs *sfsp;
    191  1.12     cgd 	long maxwidth;
    192  1.12     cgd {
    193  1.12     cgd 	static int headerlen, timesthrough;
    194  1.12     cgd 	static char *header;
    195  1.12     cgd 	long used, availblks, inodes;
    196  1.12     cgd 
    197  1.12     cgd 	if (maxwidth < 11)
    198  1.12     cgd 		maxwidth = 11;
    199  1.12     cgd 	if (++timesthrough == 1) {
    200  1.12     cgd 		if (kflag) {
    201  1.12     cgd 			header = "1K-blocks";
    202  1.12     cgd 			headerlen = strlen(header);
    203  1.12     cgd 		} else
    204  1.12     cgd 			header = getbsize(&headerlen, &blocksize);
    205  1.12     cgd 		(void)printf("%-*.*s %s    Used   Avail Capacity",
    206  1.12     cgd 		    maxwidth, maxwidth, "Filesystem", header);
    207  1.12     cgd 		if (iflag)
    208  1.12     cgd 			(void)printf(" iused   ifree  %%iused");
    209  1.12     cgd 		(void)printf("  Mounted on\n");
    210  1.12     cgd 	}
    211  1.12     cgd 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
    212  1.12     cgd 	used = sfsp->f_blocks - sfsp->f_bfree;
    213  1.12     cgd 	availblks = sfsp->f_bavail + used;
    214  1.12     cgd 	(void)printf(" %*ld %7ld %7ld", headerlen,
    215  1.12     cgd 	    sfsp->f_blocks * sfsp->f_bsize / blocksize,
    216  1.12     cgd 	    used * sfsp->f_bsize / blocksize,
    217  1.12     cgd 	    sfsp->f_bavail * sfsp->f_bsize / blocksize);
    218  1.12     cgd 	(void)printf(" %5.0f%%",
    219  1.12     cgd 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
    220  1.12     cgd 	if (iflag) {
    221  1.12     cgd 		inodes = sfsp->f_files;
    222  1.12     cgd 		used = inodes - sfsp->f_ffree;
    223  1.12     cgd 		(void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
    224  1.12     cgd 		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
    225  1.12     cgd 	} else
    226  1.12     cgd 		(void)printf("  ");
    227  1.12     cgd 	(void)printf("  %s\n", sfsp->f_mntonname);
    228  1.12     cgd }
    229  1.12     cgd 
    230  1.12     cgd /*
    231  1.12     cgd  * This code constitutes the old df code for extracting
    232  1.12     cgd  * information from filesystem superblocks.
    233  1.12     cgd  */
    234  1.13  chopps #include <ufs/ffs/fs.h>
    235  1.12     cgd #include <errno.h>
    236  1.12     cgd #include <fstab.h>
    237  1.12     cgd 
    238  1.12     cgd union {
    239  1.12     cgd 	struct fs iu_fs;
    240  1.12     cgd 	char dummy[SBSIZE];
    241  1.12     cgd } sb;
    242  1.12     cgd #define sblock sb.iu_fs
    243  1.12     cgd 
    244  1.12     cgd int	fi;
    245  1.12     cgd 
    246  1.12     cgd void
    247  1.12     cgd ufs_df(file, maxwidth)
    248  1.12     cgd 	char *file;
    249  1.12     cgd 	long maxwidth;
    250  1.12     cgd {
    251  1.12     cgd 	struct statfs statfsbuf;
    252  1.12     cgd 	register struct statfs *sfsp;
    253  1.12     cgd 	char *mntpt;
    254  1.12     cgd 	static int synced;
    255  1.12     cgd 
    256  1.12     cgd 	if (synced++ == 0)
    257  1.12     cgd 		sync();
    258  1.12     cgd 
    259  1.12     cgd 	if ((fi = open(file, O_RDONLY)) < 0) {
    260  1.12     cgd 		warn("%s", file);
    261  1.12     cgd 		return;
    262  1.12     cgd 	}
    263  1.12     cgd 	if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) {
    264  1.12     cgd 		(void)close(fi);
    265  1.12     cgd 		return;
    266  1.12     cgd 	}
    267  1.12     cgd 	sfsp = &statfsbuf;
    268  1.12     cgd 	sfsp->f_type = 0;
    269  1.12     cgd 	sfsp->f_flags = 0;
    270  1.12     cgd 	sfsp->f_iosize = sblock.fs_fsize;
    271  1.12     cgd #ifdef notyet
    272  1.12     cgd 	sfsp->f_iosize = sblock.fs_bsize;
    273  1.12     cgd #endif
    274  1.12     cgd 	sfsp->f_blocks = sblock.fs_dsize;
    275  1.12     cgd 	sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
    276  1.12     cgd 		sblock.fs_cstotal.cs_nffree;
    277  1.12     cgd 	sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
    278  1.12     cgd 		(sblock.fs_dsize - sfsp->f_bfree);
    279  1.12     cgd 	if (sfsp->f_bavail < 0)
    280  1.12     cgd 		sfsp->f_bavail = 0;
    281  1.12     cgd 	sfsp->f_files =  sblock.fs_ncg * sblock.fs_ipg;
    282  1.12     cgd 	sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
    283  1.12     cgd 	sfsp->f_fsid.val[0] = 0;
    284  1.12     cgd 	sfsp->f_fsid.val[1] = 0;
    285  1.12     cgd 	if ((mntpt = getmntpt(file)) == 0)
    286  1.12     cgd 		mntpt = "";
    287  1.12     cgd 	bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
    288  1.12     cgd 	bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
    289  1.12     cgd 	strncpy(sfsp->f_fstypename, MOUNT_UFS, MFSNAMELEN);
    290  1.12     cgd 	sfsp->f_fstypename[MFSNAMELEN] = '\0';
    291  1.12     cgd 	prtstat(sfsp, maxwidth);
    292  1.12     cgd 	(void) close(fi);
    293  1.12     cgd }
    294  1.12     cgd 
    295  1.12     cgd int
    296  1.12     cgd bread(off, buf, cnt)
    297  1.12     cgd 	long off;
    298  1.12     cgd 	char *buf;
    299  1.12     cgd 	int cnt;
    300  1.12     cgd {
    301  1.12     cgd 	int n;
    302  1.12     cgd 
    303  1.12     cgd 	(void) lseek(fi, off, SEEK_SET);
    304  1.12     cgd 	if ((n=read(fi, buf, cnt)) != cnt) {
    305  1.12     cgd 		/* probably a dismounted disk if errno == EIO */
    306  1.12     cgd 		if (errno != EIO) {
    307  1.12     cgd 			(void)printf("\nread error off = %ld\n", off);
    308  1.12     cgd 			(void)printf("count = %d: %s\n", n, strerror(errno));
    309  1.12     cgd 		}
    310  1.12     cgd 		return (0);
    311  1.12     cgd 	}
    312  1.12     cgd 	return (1);
    313  1.12     cgd }
    314  1.12     cgd 
    315  1.12     cgd void
    316  1.12     cgd usage()
    317  1.12     cgd {
    318  1.12     cgd 
    319  1.12     cgd 	fprintf(stderr, "usage: df [-ikln] [file | file_system ...]\n");
    320  1.12     cgd 	exit(1);
    321  1.12     cgd }
    322