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