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