df.c revision 1.23 1 1.23 thorpej /* $NetBSD: df.c,v 1.23 1996/12/11 03:48:42 thorpej Exp $ */
2 1.19 cgd
3 1.12 cgd /*
4 1.15 mycroft * Copyright (c) 1980, 1990, 1993, 1994
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.12 cgd * (c) UNIX System Laboratories, Inc.
7 1.12 cgd * All or some portions of this file are derived from material licensed
8 1.12 cgd * to the University of California by American Telephone and Telegraph
9 1.12 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.12 cgd * the permission of UNIX System Laboratories, Inc.
11 1.12 cgd *
12 1.12 cgd * Redistribution and use in source and binary forms, with or without
13 1.12 cgd * modification, are permitted provided that the following conditions
14 1.12 cgd * are met:
15 1.12 cgd * 1. Redistributions of source code must retain the above copyright
16 1.12 cgd * notice, this list of conditions and the following disclaimer.
17 1.12 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.12 cgd * notice, this list of conditions and the following disclaimer in the
19 1.12 cgd * documentation and/or other materials provided with the distribution.
20 1.12 cgd * 3. All advertising materials mentioning features or use of this software
21 1.12 cgd * must display the following acknowledgement:
22 1.12 cgd * This product includes software developed by the University of
23 1.12 cgd * California, Berkeley and its contributors.
24 1.12 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.12 cgd * may be used to endorse or promote products derived from this software
26 1.12 cgd * without specific prior written permission.
27 1.12 cgd *
28 1.12 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.12 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.12 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.12 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.12 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.12 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.12 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.12 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.12 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.12 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.12 cgd * SUCH DAMAGE.
39 1.12 cgd */
40 1.12 cgd
41 1.12 cgd #ifndef lint
42 1.15 mycroft static char copyright[] =
43 1.15 mycroft "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
44 1.15 mycroft The Regents of the University of California. All rights reserved.\n";
45 1.12 cgd #endif /* not lint */
46 1.12 cgd
47 1.12 cgd #ifndef lint
48 1.19 cgd #if 0
49 1.19 cgd static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94";
50 1.19 cgd #else
51 1.23 thorpej static char rcsid[] = "$NetBSD: df.c,v 1.23 1996/12/11 03:48:42 thorpej Exp $";
52 1.19 cgd #endif
53 1.12 cgd #endif /* not lint */
54 1.12 cgd
55 1.12 cgd #include <sys/param.h>
56 1.12 cgd #include <sys/stat.h>
57 1.12 cgd #include <sys/mount.h>
58 1.15 mycroft
59 1.15 mycroft #include <err.h>
60 1.12 cgd #include <errno.h>
61 1.12 cgd #include <fcntl.h>
62 1.12 cgd #include <stdio.h>
63 1.12 cgd #include <stdlib.h>
64 1.12 cgd #include <string.h>
65 1.12 cgd #include <unistd.h>
66 1.12 cgd
67 1.15 mycroft int bread __P((off_t, void *, int));
68 1.12 cgd char *getmntpt __P((char *));
69 1.15 mycroft void prtstat __P((struct statfs *, int));
70 1.17 mycroft int ufs_df __P((char *, struct statfs *));
71 1.17 mycroft int selected __P((const char *));
72 1.17 mycroft void maketypelist __P((char *));
73 1.17 mycroft long regetmntinfo __P((struct statfs **, long));
74 1.12 cgd void usage __P((void));
75 1.12 cgd
76 1.17 mycroft int iflag, kflag, lflag, nflag;
77 1.17 mycroft char **typelist = NULL;
78 1.12 cgd struct ufs_args mdev;
79 1.12 cgd
80 1.12 cgd int
81 1.12 cgd main(argc, argv)
82 1.12 cgd int argc;
83 1.12 cgd char *argv[];
84 1.12 cgd {
85 1.12 cgd struct stat stbuf;
86 1.21 jtc struct statfs *mntbuf;
87 1.17 mycroft long mntsize;
88 1.17 mycroft int ch, i, maxwidth, width;
89 1.12 cgd char *mntpt;
90 1.12 cgd
91 1.16 mycroft while ((ch = getopt(argc, argv, "iklnt:")) != -1)
92 1.15 mycroft switch (ch) {
93 1.12 cgd case 'i':
94 1.12 cgd iflag = 1;
95 1.12 cgd break;
96 1.12 cgd case 'k':
97 1.12 cgd kflag = 1;
98 1.12 cgd break;
99 1.12 cgd case 'l':
100 1.17 mycroft lflag = 1;
101 1.12 cgd break;
102 1.12 cgd case 'n':
103 1.12 cgd nflag = 1;
104 1.12 cgd break;
105 1.16 mycroft case 't':
106 1.17 mycroft if (typelist != NULL)
107 1.17 mycroft errx(1, "only one -t option may be specified.");
108 1.17 mycroft maketypelist(optarg);
109 1.16 mycroft break;
110 1.12 cgd case '?':
111 1.12 cgd default:
112 1.12 cgd usage();
113 1.12 cgd }
114 1.12 cgd argc -= optind;
115 1.12 cgd argv += optind;
116 1.12 cgd
117 1.17 mycroft mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
118 1.12 cgd if (mntsize == 0)
119 1.14 glass err(1, "retrieving information on mounted file systems");
120 1.15 mycroft
121 1.12 cgd if (!*argv) {
122 1.17 mycroft mntsize = regetmntinfo(&mntbuf, mntsize);
123 1.17 mycroft } else {
124 1.17 mycroft mntbuf = malloc(argc * sizeof(struct statfs));
125 1.17 mycroft mntsize = 0;
126 1.17 mycroft for (; *argv; argv++) {
127 1.17 mycroft if (stat(*argv, &stbuf) < 0) {
128 1.17 mycroft if ((mntpt = getmntpt(*argv)) == 0) {
129 1.17 mycroft warn("%s", *argv);
130 1.12 cgd continue;
131 1.12 cgd }
132 1.18 mycroft } else if (S_ISCHR(stbuf.st_mode)) {
133 1.17 mycroft if (!ufs_df(*argv, &mntbuf[mntsize]))
134 1.17 mycroft ++mntsize;
135 1.17 mycroft continue;
136 1.18 mycroft } else if (S_ISBLK(stbuf.st_mode)) {
137 1.17 mycroft if ((mntpt = getmntpt(*argv)) == 0) {
138 1.17 mycroft mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
139 1.17 mycroft mdev.fspec = *argv;
140 1.17 mycroft if (mkdir(mntpt, DEFFILEMODE) != 0) {
141 1.17 mycroft warn("%s", mntpt);
142 1.17 mycroft continue;
143 1.17 mycroft }
144 1.22 jtc if (mount(MOUNT_FFS, mntpt, MNT_RDONLY,
145 1.17 mycroft &mdev) != 0) {
146 1.17 mycroft (void)rmdir(mntpt);
147 1.17 mycroft if (!ufs_df(*argv, &mntbuf[mntsize]))
148 1.17 mycroft ++mntsize;
149 1.17 mycroft continue;
150 1.17 mycroft } else if (!statfs(mntpt, &mntbuf[mntsize])) {
151 1.17 mycroft mntbuf[mntsize].f_mntonname[0] = '\0';
152 1.17 mycroft ++mntsize;
153 1.17 mycroft } else
154 1.17 mycroft warn("%s", *argv);
155 1.17 mycroft (void)unmount(mntpt, 0);
156 1.12 cgd (void)rmdir(mntpt);
157 1.12 cgd continue;
158 1.17 mycroft }
159 1.17 mycroft } else
160 1.17 mycroft mntpt = *argv;
161 1.17 mycroft /*
162 1.17 mycroft * Statfs does not take a `wait' flag, so we cannot
163 1.17 mycroft * implement nflag here.
164 1.17 mycroft */
165 1.17 mycroft if (!statfs(mntpt, &mntbuf[mntsize]))
166 1.23 thorpej if (lflag &&
167 1.23 thorpej (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
168 1.23 thorpej warnx("Warning: %s is not a local %s",
169 1.23 thorpej *argv, "file system");
170 1.23 thorpej else if
171 1.23 thorpej (!selected(mntbuf[mntsize].f_fstypename))
172 1.23 thorpej warnx("Warning: %s mounted as a %s %s",
173 1.23 thorpej *argv,
174 1.23 thorpej mntbuf[mntsize].f_fstypename,
175 1.23 thorpej "file system");
176 1.23 thorpej else
177 1.23 thorpej ++mntsize;
178 1.17 mycroft else
179 1.17 mycroft warn("%s", *argv);
180 1.12 cgd }
181 1.12 cgd }
182 1.17 mycroft
183 1.17 mycroft maxwidth = 0;
184 1.17 mycroft for (i = 0; i < mntsize; i++) {
185 1.17 mycroft width = strlen(mntbuf[i].f_mntfromname);
186 1.17 mycroft if (width > maxwidth)
187 1.17 mycroft maxwidth = width;
188 1.17 mycroft }
189 1.17 mycroft for (i = 0; i < mntsize; i++)
190 1.17 mycroft prtstat(&mntbuf[i], maxwidth);
191 1.17 mycroft exit(0);
192 1.12 cgd }
193 1.12 cgd
194 1.12 cgd char *
195 1.12 cgd getmntpt(name)
196 1.12 cgd char *name;
197 1.12 cgd {
198 1.17 mycroft long mntsize, i;
199 1.17 mycroft struct statfs *mntbuf;
200 1.12 cgd
201 1.17 mycroft mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
202 1.12 cgd for (i = 0; i < mntsize; i++) {
203 1.12 cgd if (!strcmp(mntbuf[i].f_mntfromname, name))
204 1.12 cgd return (mntbuf[i].f_mntonname);
205 1.12 cgd }
206 1.12 cgd return (0);
207 1.12 cgd }
208 1.12 cgd
209 1.17 mycroft static enum { IN_LIST, NOT_IN_LIST } which;
210 1.17 mycroft
211 1.17 mycroft int
212 1.17 mycroft selected(type)
213 1.17 mycroft const char *type;
214 1.17 mycroft {
215 1.17 mycroft char **av;
216 1.17 mycroft
217 1.17 mycroft /* If no type specified, it's always selected. */
218 1.17 mycroft if (typelist == NULL)
219 1.17 mycroft return (1);
220 1.17 mycroft for (av = typelist; *av != NULL; ++av)
221 1.20 cgd if (!strncmp(type, *av, MFSNAMELEN))
222 1.17 mycroft return (which == IN_LIST ? 1 : 0);
223 1.17 mycroft return (which == IN_LIST ? 0 : 1);
224 1.17 mycroft }
225 1.16 mycroft
226 1.16 mycroft void
227 1.17 mycroft maketypelist(fslist)
228 1.17 mycroft char *fslist;
229 1.16 mycroft {
230 1.17 mycroft int i;
231 1.17 mycroft char *nextcp, **av;
232 1.16 mycroft
233 1.17 mycroft if ((fslist == NULL) || (fslist[0] == '\0'))
234 1.17 mycroft errx(1, "empty type list");
235 1.16 mycroft
236 1.17 mycroft /*
237 1.17 mycroft * XXX
238 1.17 mycroft * Note: the syntax is "noxxx,yyy" for no xxx's and
239 1.17 mycroft * no yyy's, not the more intuitive "noyyy,noyyy".
240 1.17 mycroft */
241 1.17 mycroft if (fslist[0] == 'n' && fslist[1] == 'o') {
242 1.17 mycroft fslist += 2;
243 1.17 mycroft which = NOT_IN_LIST;
244 1.17 mycroft } else
245 1.17 mycroft which = IN_LIST;
246 1.17 mycroft
247 1.17 mycroft /* Count the number of types. */
248 1.17 mycroft for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
249 1.17 mycroft ++nextcp;
250 1.17 mycroft
251 1.17 mycroft /* Build an array of that many types. */
252 1.17 mycroft if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
253 1.17 mycroft err(1, NULL);
254 1.17 mycroft av[0] = fslist;
255 1.17 mycroft for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
256 1.17 mycroft *nextcp = '\0';
257 1.17 mycroft av[i] = ++nextcp;
258 1.16 mycroft }
259 1.17 mycroft /* Terminate the array. */
260 1.17 mycroft av[i] = NULL;
261 1.17 mycroft }
262 1.16 mycroft
263 1.17 mycroft /*
264 1.17 mycroft * Make a pass over the filesystem info in ``mntbuf'' filtering out
265 1.17 mycroft * filesystem types not in ``fsmask'' and possibly re-stating to get
266 1.17 mycroft * current (not cached) info. Returns the new count of valid statfs bufs.
267 1.17 mycroft */
268 1.17 mycroft long
269 1.17 mycroft regetmntinfo(mntbufp, mntsize)
270 1.17 mycroft struct statfs **mntbufp;
271 1.17 mycroft long mntsize;
272 1.17 mycroft {
273 1.17 mycroft int i, j;
274 1.17 mycroft struct statfs *mntbuf;
275 1.16 mycroft
276 1.17 mycroft if (!lflag && typelist == NULL)
277 1.17 mycroft return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
278 1.16 mycroft
279 1.17 mycroft mntbuf = *mntbufp;
280 1.17 mycroft j = 0;
281 1.17 mycroft for (i = 0; i < mntsize; i++) {
282 1.17 mycroft if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
283 1.17 mycroft continue;
284 1.17 mycroft if (!selected(mntbuf[i].f_fstypename))
285 1.17 mycroft continue;
286 1.17 mycroft if (nflag)
287 1.17 mycroft mntbuf[j] = mntbuf[i];
288 1.17 mycroft else
289 1.17 mycroft (void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
290 1.17 mycroft j++;
291 1.16 mycroft }
292 1.17 mycroft return (j);
293 1.16 mycroft }
294 1.16 mycroft
295 1.12 cgd /*
296 1.15 mycroft * Convert statfs returned filesystem size into BLOCKSIZE units.
297 1.15 mycroft * Attempts to avoid overflow for large filesystems.
298 1.15 mycroft */
299 1.15 mycroft #define fsbtoblk(num, fsbs, bs) \
300 1.15 mycroft (((fsbs) != 0 && (fsbs) < (bs)) ? \
301 1.15 mycroft (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
302 1.15 mycroft
303 1.15 mycroft /*
304 1.12 cgd * Print out status about a filesystem.
305 1.12 cgd */
306 1.12 cgd void
307 1.12 cgd prtstat(sfsp, maxwidth)
308 1.15 mycroft struct statfs *sfsp;
309 1.15 mycroft int maxwidth;
310 1.12 cgd {
311 1.17 mycroft static long blocksize;
312 1.12 cgd static int headerlen, timesthrough;
313 1.12 cgd static char *header;
314 1.12 cgd long used, availblks, inodes;
315 1.12 cgd
316 1.12 cgd if (maxwidth < 11)
317 1.12 cgd maxwidth = 11;
318 1.12 cgd if (++timesthrough == 1) {
319 1.12 cgd if (kflag) {
320 1.17 mycroft blocksize = 1024;
321 1.12 cgd header = "1K-blocks";
322 1.12 cgd headerlen = strlen(header);
323 1.12 cgd } else
324 1.12 cgd header = getbsize(&headerlen, &blocksize);
325 1.15 mycroft (void)printf("%-*.*s %s Used Avail Capacity",
326 1.12 cgd maxwidth, maxwidth, "Filesystem", header);
327 1.12 cgd if (iflag)
328 1.12 cgd (void)printf(" iused ifree %%iused");
329 1.12 cgd (void)printf(" Mounted on\n");
330 1.12 cgd }
331 1.12 cgd (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
332 1.12 cgd used = sfsp->f_blocks - sfsp->f_bfree;
333 1.12 cgd availblks = sfsp->f_bavail + used;
334 1.15 mycroft (void)printf(" %*ld %8ld %8ld", headerlen,
335 1.15 mycroft fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
336 1.15 mycroft fsbtoblk(used, sfsp->f_bsize, blocksize),
337 1.15 mycroft fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
338 1.12 cgd (void)printf(" %5.0f%%",
339 1.12 cgd availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
340 1.12 cgd if (iflag) {
341 1.12 cgd inodes = sfsp->f_files;
342 1.12 cgd used = inodes - sfsp->f_ffree;
343 1.12 cgd (void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
344 1.12 cgd inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
345 1.12 cgd } else
346 1.12 cgd (void)printf(" ");
347 1.12 cgd (void)printf(" %s\n", sfsp->f_mntonname);
348 1.12 cgd }
349 1.12 cgd
350 1.12 cgd /*
351 1.15 mycroft * This code constitutes the pre-system call Berkeley df code for extracting
352 1.12 cgd * information from filesystem superblocks.
353 1.12 cgd */
354 1.13 chopps #include <ufs/ffs/fs.h>
355 1.12 cgd #include <errno.h>
356 1.12 cgd #include <fstab.h>
357 1.12 cgd
358 1.12 cgd union {
359 1.12 cgd struct fs iu_fs;
360 1.12 cgd char dummy[SBSIZE];
361 1.12 cgd } sb;
362 1.12 cgd #define sblock sb.iu_fs
363 1.12 cgd
364 1.15 mycroft int rfd;
365 1.12 cgd
366 1.17 mycroft int
367 1.17 mycroft ufs_df(file, sfsp)
368 1.12 cgd char *file;
369 1.17 mycroft struct statfs *sfsp;
370 1.12 cgd {
371 1.12 cgd char *mntpt;
372 1.12 cgd static int synced;
373 1.12 cgd
374 1.12 cgd if (synced++ == 0)
375 1.12 cgd sync();
376 1.12 cgd
377 1.15 mycroft if ((rfd = open(file, O_RDONLY)) < 0) {
378 1.12 cgd warn("%s", file);
379 1.17 mycroft return (-1);
380 1.12 cgd }
381 1.15 mycroft if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) {
382 1.15 mycroft (void)close(rfd);
383 1.17 mycroft return (-1);
384 1.12 cgd }
385 1.12 cgd sfsp->f_type = 0;
386 1.12 cgd sfsp->f_flags = 0;
387 1.15 mycroft sfsp->f_bsize = sblock.fs_fsize;
388 1.12 cgd sfsp->f_iosize = sblock.fs_bsize;
389 1.12 cgd sfsp->f_blocks = sblock.fs_dsize;
390 1.12 cgd sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
391 1.12 cgd sblock.fs_cstotal.cs_nffree;
392 1.12 cgd sfsp->f_bavail = (sblock.fs_dsize * (100 - sblock.fs_minfree) / 100) -
393 1.12 cgd (sblock.fs_dsize - sfsp->f_bfree);
394 1.12 cgd if (sfsp->f_bavail < 0)
395 1.12 cgd sfsp->f_bavail = 0;
396 1.12 cgd sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg;
397 1.12 cgd sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
398 1.12 cgd sfsp->f_fsid.val[0] = 0;
399 1.12 cgd sfsp->f_fsid.val[1] = 0;
400 1.12 cgd if ((mntpt = getmntpt(file)) == 0)
401 1.12 cgd mntpt = "";
402 1.15 mycroft memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
403 1.15 mycroft memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
404 1.22 jtc strncpy(sfsp->f_fstypename, MOUNT_FFS, MFSNAMELEN);
405 1.15 mycroft (void)close(rfd);
406 1.17 mycroft return (0);
407 1.12 cgd }
408 1.12 cgd
409 1.12 cgd int
410 1.12 cgd bread(off, buf, cnt)
411 1.15 mycroft off_t off;
412 1.15 mycroft void *buf;
413 1.12 cgd int cnt;
414 1.12 cgd {
415 1.15 mycroft int nr;
416 1.12 cgd
417 1.15 mycroft (void)lseek(rfd, off, SEEK_SET);
418 1.15 mycroft if ((nr = read(rfd, buf, cnt)) != cnt) {
419 1.15 mycroft /* Probably a dismounted disk if errno == EIO. */
420 1.15 mycroft if (errno != EIO)
421 1.15 mycroft (void)fprintf(stderr, "\ndf: %qd: %s\n",
422 1.15 mycroft off, strerror(nr > 0 ? EIO : errno));
423 1.12 cgd return (0);
424 1.12 cgd }
425 1.12 cgd return (1);
426 1.12 cgd }
427 1.12 cgd
428 1.12 cgd void
429 1.12 cgd usage()
430 1.12 cgd {
431 1.16 mycroft (void)fprintf(stderr, "usage: df [-ikln] [-t type] [file | file_system ...]\n");
432 1.12 cgd exit(1);
433 1.12 cgd }
434