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