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