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