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