df.c revision 1.60 1 /* $NetBSD: df.c,v 1.60 2004/03/26 20:19:03 enami 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT(
40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n");
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static char sccsid[] = "@(#)df.c 8.7 (Berkeley) 4/2/94";
47 #else
48 __RCSID("$NetBSD: df.c,v 1.60 2004/03/26 20:19:03 enami Exp $");
49 #endif
50 #endif /* not lint */
51
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <util.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 extern char *strpct(u_long, u_long, u_int);
66
67 int main(int, char *[]);
68 int bread(off_t, void *, int);
69 char *getmntpt(char *);
70 void prtstat(struct statfs *, int);
71 int selected(const char *);
72 void maketypelist(char *);
73 long regetmntinfo(struct statfs **, long);
74 void usage(void);
75 void prthumanval(int64_t, char *);
76 void prthuman(struct statfs *, int64_t, int64_t);
77
78 int aflag, gflag, hflag, iflag, kflag, lflag, mflag, nflag, Pflag;
79 char **typelist = NULL;
80
81 int
82 main(int argc, char *argv[])
83 {
84 struct stat stbuf;
85 struct statfs *mntbuf;
86 long mntsize;
87 int ch, i, maxwidth, width;
88 char *mntpt;
89
90 while ((ch = getopt(argc, argv, "aghiklmnPt:")) != -1)
91 switch (ch) {
92 case 'a':
93 aflag = 1;
94 break;
95 case 'g':
96 gflag = 1;
97 break;
98 case 'h':
99 hflag = 1;
100 break;
101 case 'i':
102 iflag = 1;
103 break;
104 case 'k':
105 kflag = 1;
106 break;
107 case 'l':
108 lflag = 1;
109 break;
110 case 'm':
111 mflag = 1;
112 break;
113 case 'n':
114 nflag = 1;
115 break;
116 case 'P':
117 Pflag = 1;
118 break;
119 case 't':
120 if (typelist != NULL)
121 errx(1, "only one -t option may be specified.");
122 maketypelist(optarg);
123 break;
124 case '?':
125 default:
126 usage();
127 }
128 argc -= optind;
129 argv += optind;
130
131 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
132 if (mntsize == 0)
133 err(1, "retrieving information on mounted file systems");
134
135 if (*argv == NULL) {
136 mntsize = regetmntinfo(&mntbuf, mntsize);
137 } else {
138 mntbuf = malloc(argc * sizeof(struct statfs));
139 mntsize = 0;
140 for (; *argv != NULL; argv++) {
141 if (stat(*argv, &stbuf) < 0) {
142 if ((mntpt = getmntpt(*argv)) == 0) {
143 warn("%s", *argv);
144 continue;
145 }
146 } else if (S_ISBLK(stbuf.st_mode)) {
147 if ((mntpt = getmntpt(*argv)) == 0)
148 mntpt = *argv;
149 } else
150 mntpt = *argv;
151 /*
152 * Statfs does not take a `wait' flag, so we cannot
153 * implement nflag here.
154 */
155 if (!statfs(mntpt, &mntbuf[mntsize]))
156 if (lflag &&
157 (mntbuf[mntsize].f_flags & MNT_LOCAL) == 0)
158 warnx("Warning: %s is not a local %s",
159 *argv, "file system");
160 else if
161 (!selected(mntbuf[mntsize].f_fstypename))
162 warnx("Warning: %s mounted as a %s %s",
163 *argv,
164 mntbuf[mntsize].f_fstypename,
165 "file system");
166 else
167 ++mntsize;
168 else
169 warn("%s", *argv);
170 }
171 }
172
173 maxwidth = 0;
174 for (i = 0; i < mntsize; i++) {
175 width = strlen(mntbuf[i].f_mntfromname);
176 if (width > maxwidth)
177 maxwidth = width;
178 }
179 for (i = 0; i < mntsize; i++)
180 prtstat(&mntbuf[i], maxwidth);
181 exit(0);
182 /* NOTREACHED */
183 }
184
185 char *
186 getmntpt(char *name)
187 {
188 long mntsize, i;
189 struct statfs *mntbuf;
190
191 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
192 for (i = 0; i < mntsize; i++) {
193 if (!strcmp(mntbuf[i].f_mntfromname, name))
194 return (mntbuf[i].f_mntonname);
195 }
196 return (0);
197 }
198
199 static enum { IN_LIST, NOT_IN_LIST } which;
200
201 int
202 selected(const char *type)
203 {
204 char **av;
205
206 /* If no type specified, it's always selected. */
207 if (typelist == NULL)
208 return (1);
209 for (av = typelist; *av != NULL; ++av)
210 if (!strncmp(type, *av, MFSNAMELEN))
211 return (which == IN_LIST ? 1 : 0);
212 return (which == IN_LIST ? 0 : 1);
213 }
214
215 void
216 maketypelist(char *fslist)
217 {
218 int i;
219 char *nextcp, **av;
220
221 if ((fslist == NULL) || (fslist[0] == '\0'))
222 errx(1, "empty type list");
223
224 /*
225 * XXX
226 * Note: the syntax is "noxxx,yyy" for no xxx's and
227 * no yyy's, not the more intuitive "noyyy,noyyy".
228 */
229 if (fslist[0] == 'n' && fslist[1] == 'o') {
230 fslist += 2;
231 which = NOT_IN_LIST;
232 } else
233 which = IN_LIST;
234
235 /* Count the number of types. */
236 for (i = 1, nextcp = fslist;
237 (nextcp = strchr(nextcp, ',')) != NULL; i++)
238 ++nextcp;
239
240 /* Build an array of that many types. */
241 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
242 err(1, "can't allocate type array");
243 av[0] = fslist;
244 for (i = 1, nextcp = fslist;
245 (nextcp = strchr(nextcp, ',')) != NULL; i++) {
246 *nextcp = '\0';
247 av[i] = ++nextcp;
248 }
249 /* Terminate the array. */
250 av[i] = NULL;
251 }
252
253 /*
254 * Make a pass over the filesystem info in ``mntbuf'' filtering out
255 * filesystem types not in ``fsmask'' and possibly re-stating to get
256 * current (not cached) info. Returns the new count of valid statfs bufs.
257 */
258 long
259 regetmntinfo(struct statfs **mntbufp, long mntsize)
260 {
261 int i, j;
262 struct statfs *mntbuf;
263
264 if (!lflag && typelist == NULL && aflag)
265 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
266
267 mntbuf = *mntbufp;
268 j = 0;
269 for (i = 0; i < mntsize; i++) {
270 if (!aflag && (mntbuf[i].f_flags & MNT_IGNORE) != 0)
271 continue;
272 if (lflag && (mntbuf[i].f_flags & MNT_LOCAL) == 0)
273 continue;
274 if (!selected(mntbuf[i].f_fstypename))
275 continue;
276 if (nflag)
277 mntbuf[j] = mntbuf[i];
278 else {
279 struct statfs layerbuf = mntbuf[i];
280 (void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
281 /*
282 * If the FS name changed, then new data is for
283 * a different layer and we don't want it.
284 */
285 if (memcmp(layerbuf.f_mntfromname,
286 mntbuf[j].f_mntfromname, MNAMELEN))
287 mntbuf[j] = layerbuf;
288 }
289 j++;
290 }
291 return (j);
292 }
293
294 void
295 prthumanval(int64_t bytes, char *pad)
296 {
297 char buf[6];
298
299 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
300 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, int64_t used, int64_t bavail)
308 {
309
310 prthumanval((int64_t)(u_long)sfsp->f_blocks * sfsp->f_bsize, "");
311 prthumanval(used * sfsp->f_bsize, " ");
312 prthumanval(bavail * sfsp->f_bsize, " ");
313 }
314
315
316 /*
317 * Convert statfs returned filesystem size into BLOCKSIZE units.
318 * Attempts to avoid overflow for large filesystems.
319 */
320 #define fsbtoblk(num, fsbs, bs) \
321 (((fsbs) != 0 && (fsbs) < (bs)) ? \
322 (int64_t)(num) / ((bs) / (fsbs)) : \
323 (int64_t)(num) * ((fsbs) / (bs)))
324
325 /*
326 * Print out status about a filesystem.
327 */
328 void
329 prtstat(struct statfs *sfsp, int maxwidth)
330 {
331 static long blocksize;
332 static int headerlen, timesthrough;
333 static char *header;
334 static const char full[] = "100%";
335 static const char empty[] = " 0%";
336 long used, availblks, inodes;
337 int64_t bavail;
338
339 if (maxwidth < 11)
340 maxwidth = 11;
341 if (++timesthrough == 1) {
342 if (kflag) {
343 blocksize = 1024;
344 header = Pflag ? "1024-blocks" : "1K-blocks";
345 headerlen = strlen(header);
346 } else if (mflag) {
347 blocksize = 1024 * 1024;
348 header = Pflag ? "1048576-blocks" : "1M-blocks";
349 headerlen = strlen(header);
350 } else if (gflag) {
351 blocksize = 1024 * 1024 * 1024;
352 header = Pflag ? "1073741824-blocks" : "1G-blocks";
353 headerlen = strlen(header);
354 } else if (hflag) {
355 header = " Size";
356 headerlen = strlen(header);
357 } else
358 header = getbsize(&headerlen, &blocksize);
359 (void)printf("%-*.*s %s Used %9s Capacity",
360 maxwidth, maxwidth, "Filesystem", header,
361 Pflag ? "Available" : "Avail");
362 if (iflag)
363 (void)printf(" iused ifree %%iused");
364 (void)printf(" Mounted on\n");
365 }
366 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
367 used = sfsp->f_blocks - sfsp->f_bfree;
368 availblks = sfsp->f_bavail + used;
369 if ((u_long)availblks > (u_long)used)
370 bavail = (u_long)sfsp->f_bavail;
371 else
372 bavail = sfsp->f_bavail;
373 if (hflag)
374 prthuman(sfsp, (u_long)used, bavail);
375 else {
376 (void)printf(" %*" PRId64 " %8" PRId64, headerlen,
377 fsbtoblk((u_long)sfsp->f_blocks, sfsp->f_bsize, blocksize),
378 fsbtoblk((u_long)used, sfsp->f_bsize, blocksize));
379 (void)printf(" %9" PRId64, fsbtoblk(bavail,
380 sfsp->f_bsize, blocksize));
381 }
382 (void)printf("%7s",
383 availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0));
384 if (iflag) {
385 inodes = sfsp->f_files;
386 used = inodes - sfsp->f_ffree;
387 (void)printf(" %8ld %8ld %6s ", used, sfsp->f_ffree,
388 inodes == 0 ? (used == 0 ? empty : full) :
389 strpct((u_long)used, (u_long)inodes, 0));
390 } else
391 (void)printf(" ");
392 (void)printf(" %s\n", sfsp->f_mntonname);
393 }
394
395 void
396 usage(void)
397 {
398
399 (void)fprintf(stderr,
400 "usage: %s [-aghiklmnP] [-t type] [file | file_system ...]\n",
401 getprogname());
402 exit(1);
403 /* NOTREACHED */
404 }
405