df.c revision 1.43 1 /* $NetBSD: df.c,v 1.43 2002/08/02 08:17:12 soren 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.43 2002/08/02 08:17:12 soren 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 <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 extern char *strpct(u_long, u_long, u_int);
69
70 int main(int, char *[]);
71 int bread(off_t, void *, int);
72 char *getmntpt(char *);
73 void prtstat(struct statfs *, int);
74 int selected(const char *);
75 void maketypelist(char *);
76 long regetmntinfo(struct statfs **, long);
77 void usage(void);
78
79 int aflag, iflag, kflag, lflag, mflag, nflag, Pflag;
80 char **typelist = NULL;
81
82 int
83 main(int argc, char *argv[])
84 {
85 struct stat stbuf;
86 struct statfs *mntbuf;
87 long mntsize;
88 int ch, i, maxwidth, width;
89 char *mntpt;
90
91 while ((ch = getopt(argc, argv, "aiklmnPt:")) != -1)
92 switch (ch) {
93 case 'a':
94 aflag = 1;
95 break;
96 case 'i':
97 iflag = 1;
98 break;
99 case 'k':
100 kflag = 1;
101 break;
102 case 'l':
103 lflag = 1;
104 break;
105 case 'm':
106 mflag = 1;
107 break;
108 case 'n':
109 nflag = 1;
110 break;
111 case 'P':
112 Pflag = 1;
113 break;
114 case 't':
115 if (typelist != NULL)
116 errx(1, "only one -t option may be specified.");
117 maketypelist(optarg);
118 break;
119 case '?':
120 default:
121 usage();
122 }
123 argc -= optind;
124 argv += optind;
125
126 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
127 if (mntsize == 0)
128 err(1, "retrieving information on mounted file systems");
129
130 if (*argv == NULL) {
131 mntsize = regetmntinfo(&mntbuf, mntsize);
132 } else {
133 mntbuf = malloc(argc * sizeof(struct statfs));
134 mntsize = 0;
135 for (; *argv != NULL; argv++) {
136 if (stat(*argv, &stbuf) < 0) {
137 if ((mntpt = getmntpt(*argv)) == 0) {
138 warn("%s", *argv);
139 continue;
140 }
141 } else if (S_ISBLK(stbuf.st_mode)) {
142 if ((mntpt = getmntpt(*argv)) == 0)
143 mntpt = *argv;
144 } else
145 mntpt = *argv;
146 /*
147 * Statfs does not take a `wait' flag, so we cannot
148 * implement nflag here.
149 */
150 if (!statfs(mntpt, &mntbuf[mntsize]))
151 if (lflag &&
152 (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
153 warnx("Warning: %s is not a local %s",
154 *argv, "file system");
155 else if
156 (!selected(mntbuf[mntsize].f_fstypename))
157 warnx("Warning: %s mounted as a %s %s",
158 *argv,
159 mntbuf[mntsize].f_fstypename,
160 "file system");
161 else
162 ++mntsize;
163 else
164 warn("%s", *argv);
165 }
166 }
167
168 maxwidth = 0;
169 for (i = 0; i < mntsize; i++) {
170 width = strlen(mntbuf[i].f_mntfromname);
171 if (width > maxwidth)
172 maxwidth = width;
173 }
174 for (i = 0; i < mntsize; i++)
175 prtstat(&mntbuf[i], maxwidth);
176 exit(0);
177 /* NOTREACHED */
178 }
179
180 char *
181 getmntpt(char *name)
182 {
183 long mntsize, i;
184 struct statfs *mntbuf;
185
186 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
187 for (i = 0; i < mntsize; i++) {
188 if (!strcmp(mntbuf[i].f_mntfromname, name))
189 return (mntbuf[i].f_mntonname);
190 }
191 return (0);
192 }
193
194 static enum { IN_LIST, NOT_IN_LIST } which;
195
196 int
197 selected(const char *type)
198 {
199 char **av;
200
201 /* If no type specified, it's always selected. */
202 if (typelist == NULL)
203 return (1);
204 for (av = typelist; *av != NULL; ++av)
205 if (!strncmp(type, *av, MFSNAMELEN))
206 return (which == IN_LIST ? 1 : 0);
207 return (which == IN_LIST ? 0 : 1);
208 }
209
210 void
211 maketypelist(char *fslist)
212 {
213 int i;
214 char *nextcp, **av;
215
216 if ((fslist == NULL) || (fslist[0] == '\0'))
217 errx(1, "empty type list");
218
219 /*
220 * XXX
221 * Note: the syntax is "noxxx,yyy" for no xxx's and
222 * no yyy's, not the more intuitive "noyyy,noyyy".
223 */
224 if (fslist[0] == 'n' && fslist[1] == 'o') {
225 fslist += 2;
226 which = NOT_IN_LIST;
227 } else
228 which = IN_LIST;
229
230 /* Count the number of types. */
231 for (i = 1, nextcp = fslist;
232 (nextcp = strchr(nextcp, ',')) != NULL; i++)
233 ++nextcp;
234
235 /* Build an array of that many types. */
236 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
237 err(1, "can't allocate type array");
238 av[0] = fslist;
239 for (i = 1, nextcp = fslist;
240 (nextcp = strchr(nextcp, ',')) != NULL; i++) {
241 *nextcp = '\0';
242 av[i] = ++nextcp;
243 }
244 /* Terminate the array. */
245 av[i] = NULL;
246 }
247
248 /*
249 * Make a pass over the filesystem info in ``mntbuf'' filtering out
250 * filesystem types not in ``fsmask'' and possibly re-stating to get
251 * current (not cached) info. Returns the new count of valid statfs bufs.
252 */
253 long
254 regetmntinfo(struct statfs **mntbufp, long mntsize)
255 {
256 int i, j;
257 struct statfs *mntbuf;
258
259 if (!lflag && typelist == NULL && aflag)
260 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
261
262 mntbuf = *mntbufp;
263 j = 0;
264 for (i = 0; i < mntsize; i++) {
265 if (!aflag && (mntbuf[i].f_flags & MNT_IGNORE) != 0)
266 continue;
267 if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
268 continue;
269 if (!selected(mntbuf[i].f_fstypename))
270 continue;
271 if (nflag)
272 mntbuf[j] = mntbuf[i];
273 else {
274 struct statfs layerbuf = mntbuf[i];
275 (void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
276 /*
277 * If the FS name changed, then new data is for
278 * a different layer and we don't want it.
279 */
280 if (memcmp(layerbuf.f_mntfromname,
281 mntbuf[j].f_mntfromname, MNAMELEN))
282 mntbuf[j] = layerbuf;
283 }
284 j++;
285 }
286 return (j);
287 }
288
289 /*
290 * Convert statfs returned filesystem size into BLOCKSIZE units.
291 * Attempts to avoid overflow for large filesystems.
292 */
293 #define fsbtoblk(num, fsbs, bs) \
294 (((fsbs) != 0 && (fsbs) < (bs)) ? \
295 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
296
297 /*
298 * Print out status about a filesystem.
299 */
300 void
301 prtstat(struct statfs *sfsp, int maxwidth)
302 {
303 static long blocksize;
304 static int headerlen, timesthrough;
305 static char *header;
306 long used, availblks, inodes;
307 static char *full = "100%";
308
309 if (maxwidth < 11)
310 maxwidth = 11;
311 if (++timesthrough == 1) {
312 if (kflag) {
313 blocksize = 1024;
314 header = Pflag ? "1024-blocks" : "1K-blocks";
315 headerlen = strlen(header);
316 } else if (mflag) {
317 blocksize = 1024 * 1024;
318 header = Pflag ? "1048576-blocks" : "1M-blocks";
319 headerlen = strlen(header);
320 } else
321 header = getbsize(&headerlen, &blocksize);
322 (void)printf("%-*.*s %s Used %9s Capacity",
323 maxwidth, maxwidth, "Filesystem", header,
324 Pflag ? "Available" : "Avail");
325 if (iflag)
326 (void)printf(" iused ifree %%iused");
327 (void)printf(" Mounted on\n");
328 }
329 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
330 used = sfsp->f_blocks - sfsp->f_bfree;
331 availblks = sfsp->f_bavail + used;
332 (void)printf(" %*ld %8ld %9ld", headerlen,
333 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
334 fsbtoblk(used, sfsp->f_bsize, blocksize),
335 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
336 (void)printf("%7s",
337 availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0));
338 if (iflag) {
339 inodes = sfsp->f_files;
340 used = inodes - sfsp->f_ffree;
341 (void)printf(" %7ld %7ld %6s ", used, sfsp->f_ffree,
342 inodes == 0 ? full :
343 strpct((u_long)used, (u_long)inodes, 0));
344 } else
345 (void)printf(" ");
346 (void)printf(" %s\n", sfsp->f_mntonname);
347 }
348
349 void
350 usage(void)
351 {
352
353 (void)fprintf(stderr,
354 "Usage: %s [-aiklmnP] [-t type] [file | file_system ...]\n",
355 getprogname());
356 exit(1);
357 /* NOTREACHED */
358 }
359