sched_setaffinity.2: Small markup fix.
[dragonfly.git] / bin / df / df.c
blob15e5f8c99e1919e6f191533de4275d9e436f7fb4
1 /*
2 * Copyright (c) 1980, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#) Copyright (c) 1980, 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
35 * @(#)df.c 8.9 (Berkeley) 5/8/95
36 * $FreeBSD: src/bin/df/df.c,v 1.23.2.9 2002/07/01 00:14:24 iedowse Exp $
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
42 #include <sys/sysctl.h>
43 #include <sys/statvfs.h>
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/ufsmount.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fstab.h>
52 #include <libutil.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
59 #define UNITS_SI 1
60 #define UNITS_2 2
62 #ifndef HN_FRACTIONAL
63 #define HN_FRACTIONAL HN_DECIMAL
64 #endif
66 /* Maximum widths of various fields. */
67 struct maxwidths {
68 int mntfrom;
69 int fstype;
70 int total;
71 int used;
72 int avail;
73 int iused;
74 int ifree;
77 /* vfslist.c */
78 char **makevfslist(char *);
79 int checkvfsname(const char *, char **);
81 static char *getmntpt(char *);
82 static int quadwidth(int64_t);
83 static char *makenetvfslist(void);
84 static void prthuman(struct statvfs *, int64_t);
85 static void prthumanval(int64_t);
86 static void prtstat(struct statfs *, struct statvfs *, struct maxwidths *);
87 static long regetmntinfo(struct statfs **, struct statvfs **, long, char **);
88 static void update_maxwidths(struct maxwidths *, struct statfs *, struct statvfs *);
89 static void usage(void);
91 int aflag = 0, hflag, iflag, nflag, Tflag;
92 struct ufs_args mdev;
94 static __inline int
95 imax(int a, int b)
97 return (a > b ? a : b);
100 static __inline int64_t
101 qmax(int64_t a, int64_t b)
103 return (a > b ? a : b);
107 main(int argc, char **argv)
109 struct stat stbuf;
110 struct statfs statfsbuf, *mntbuf;
111 struct statvfs statvfsbuf, *mntvbuf;
112 struct maxwidths maxwidths;
113 const char *fstype;
114 char *mntpath, *mntpt, **vfslist;
115 long mntsize;
116 int ch, i, rv;
118 fstype = "ufs";
120 vfslist = NULL;
121 while ((ch = getopt(argc, argv, "abgHhiklmnPt:T")) != -1)
122 switch (ch) {
123 case 'a':
124 aflag = 1;
125 break;
126 case 'b':
127 /* FALLTHROUGH */
128 case 'P':
129 if (setenv("BLOCKSIZE", "512", 1) != 0)
130 warn("setenv: cannot set BLOCKSIZE=512");
131 hflag = 0;
132 break;
133 case 'g':
134 if (setenv("BLOCKSIZE", "1g", 1) != 0)
135 warn("setenv: cannot set BLOCKSIZE=1g");
136 hflag = 0;
137 break;
138 case 'H':
139 hflag = UNITS_SI;
140 break;
141 case 'h':
142 hflag = UNITS_2;
143 break;
144 case 'i':
145 iflag = 1;
146 break;
147 case 'k':
148 if (setenv("BLOCKSIZE", "1k", 1) != 0)
149 warn("setenv: cannot set BLOCKSIZE=1k");
150 hflag = 0;
151 break;
152 case 'l':
153 if (vfslist != NULL)
154 errx(1, "-l and -t are mutually exclusive.");
155 vfslist = makevfslist(makenetvfslist());
156 break;
157 case 'm':
158 if (setenv("BLOCKSIZE", "1m", 1) != 0)
159 warn("setenv: cannot set BLOCKSIZE=1m");
160 hflag = 0;
161 break;
162 case 'n':
163 nflag = 1;
164 break;
165 case 't':
166 if (vfslist != NULL)
167 errx(1, "only one -t option may be specified");
168 fstype = optarg;
169 vfslist = makevfslist(optarg);
170 break;
171 case 'T':
172 Tflag = 1;
173 break;
174 case '?':
175 default:
176 usage();
178 argc -= optind;
179 argv += optind;
181 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
182 bzero(&maxwidths, sizeof(maxwidths));
183 for (i = 0; i < mntsize; i++)
184 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
186 rv = 0;
187 if (!*argv) {
188 mntsize = regetmntinfo(&mntbuf, &mntvbuf, mntsize, vfslist);
189 bzero(&maxwidths, sizeof(maxwidths));
190 for (i = 0; i < mntsize; i++)
191 update_maxwidths(&maxwidths, &mntbuf[i], &mntvbuf[i]);
192 for (i = 0; i < mntsize; i++) {
193 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
194 prtstat(&mntbuf[i], &mntvbuf[i], &maxwidths);
196 exit(rv);
199 for (; *argv; argv++) {
200 if (stat(*argv, &stbuf) < 0) {
201 if ((mntpt = getmntpt(*argv)) == NULL) {
202 warn("%s", *argv);
203 rv = 1;
204 continue;
206 } else if (S_ISCHR(stbuf.st_mode)) {
207 if ((mntpt = getmntpt(*argv)) == NULL) {
208 mdev.fspec = *argv;
209 mntpath = strdup("/tmp/df.XXXXXX");
210 if (mntpath == NULL) {
211 warn("strdup failed");
212 rv = 1;
213 continue;
215 mntpt = mkdtemp(mntpath);
216 if (mntpt == NULL) {
217 warn("mkdtemp(\"%s\") failed", mntpath);
218 rv = 1;
219 free(mntpath);
220 continue;
222 if (mount(fstype, mntpt, MNT_RDONLY,
223 &mdev) != 0) {
224 warn("%s", *argv);
225 rv = 1;
226 rmdir(mntpt);
227 free(mntpath);
228 continue;
229 } else if (statfs(mntpt, &statfsbuf) == 0 &&
230 statvfs(mntpt, &statvfsbuf) == 0) {
231 statfsbuf.f_mntonname[0] = '\0';
232 prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
233 } else {
234 warn("%s", *argv);
235 rv = 1;
237 unmount(mntpt, 0);
238 rmdir(mntpt);
239 free(mntpath);
240 continue;
242 } else
243 mntpt = *argv;
245 * Statfs does not take a `wait' flag, so we cannot
246 * implement nflag here.
248 if (statfs(mntpt, &statfsbuf) < 0) {
249 warn("%s", mntpt);
250 rv = 1;
251 continue;
253 if (statvfs(mntpt, &statvfsbuf) < 0) {
254 warn("%s", mntpt);
255 rv = 1;
256 continue;
259 * Check to make sure the arguments we've been given are
260 * satisfied. Return an error if we have been asked to
261 * list a mount point that does not match the other args
262 * we've been given (-l, -t, etc.).
264 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
265 rv = 1;
266 continue;
269 if (argc == 1) {
270 bzero(&maxwidths, sizeof(maxwidths));
271 update_maxwidths(&maxwidths, &statfsbuf, &statvfsbuf);
273 prtstat(&statfsbuf, &statvfsbuf, &maxwidths);
275 return (rv);
278 static char *
279 getmntpt(char *name)
281 long mntsize, i;
282 struct statfs *mntbuf;
283 struct statvfs *mntvbuf;
285 mntsize = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
286 for (i = 0; i < mntsize; i++) {
287 if (!strcmp(mntbuf[i].f_mntfromname, name))
288 return (mntbuf[i].f_mntonname);
290 return (0);
294 * Make a pass over the filesystem info in ``mntbuf'' filtering out
295 * filesystem types not in vfslist and possibly re-stating to get
296 * current (not cached) info. Returns the new count of valid statfs bufs.
298 static long
299 regetmntinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, long mntsize, char **vfslist)
301 int i, j;
302 struct statfs *mntbuf;
303 struct statvfs *mntvbuf;
305 if (vfslist == NULL)
306 return (nflag ? mntsize : getmntvinfo(mntbufp, mntvbufp, MNT_WAIT));
308 mntbuf = *mntbufp;
309 mntvbuf = *mntvbufp;
310 for (j = 0, i = 0; i < mntsize; i++) {
311 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
312 continue;
313 if (!nflag) {
314 statfs(mntbuf[i].f_mntonname,&mntbuf[j]);
315 statvfs(mntbuf[i].f_mntonname,&mntvbuf[j]);
316 } else if (i != j) {
317 mntbuf[j] = mntbuf[i];
318 mntvbuf[j] = mntvbuf[i];
320 j++;
322 return (j);
325 static void
326 prthuman(struct statvfs *vsfsp, int64_t used)
328 prthumanval(vsfsp->f_blocks * vsfsp->f_bsize);
329 prthumanval(used * vsfsp->f_bsize);
330 prthumanval(vsfsp->f_bavail * vsfsp->f_bsize);
333 static void
334 prthumanval(int64_t bytes)
336 char buf[7];
337 int flags;
339 flags = HN_B | HN_NOSPACE | HN_FRACTIONAL;
340 if (hflag == UNITS_SI)
341 flags |= HN_DIVISOR_1000;
343 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
344 bytes, "", HN_AUTOSCALE, flags);
346 printf(" %6s", buf);
350 * Print an inode count in "human-readable" format.
352 static void
353 prthumanvalinode(int64_t bytes)
355 char buf[6];
356 int flags;
358 flags = HN_NOSPACE | HN_FRACTIONAL | HN_DIVISOR_1000;
360 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
361 bytes, "", HN_AUTOSCALE, flags);
363 printf(" %5s", buf);
367 * Convert statfs returned filesystem size into BLOCKSIZE units.
368 * Attempts to avoid overflow for large filesystems.
370 static intmax_t
371 fsbtoblk(int64_t num, uint64_t bsize, u_long reqbsize)
373 if (bsize != 0 && bsize < reqbsize)
374 return (num / (intmax_t)(reqbsize / bsize));
375 else
376 return (num * (intmax_t)(bsize / reqbsize));
380 * Print out status about a filesystem.
382 static void
383 prtstat(struct statfs *sfsp, struct statvfs *vsfsp, struct maxwidths *mwp)
385 static long blocksize;
386 static int headerlen, timesthrough;
387 static const char *header;
388 int64_t used, availblks, inodes;
390 if (++timesthrough == 1) {
391 mwp->mntfrom = imax(mwp->mntfrom, strlen("Filesystem"));
392 mwp->fstype = imax(mwp->fstype, strlen("Type"));
393 if (hflag) {
394 header = " Size";
395 mwp->total = mwp->used = mwp->avail = strlen(header);
396 } else {
397 header = getbsize(&headerlen, &blocksize);
398 mwp->total = imax(mwp->total, headerlen);
400 mwp->used = imax(mwp->used, strlen("Used"));
401 mwp->avail = imax(mwp->avail, strlen("Avail"));
403 printf("%-*s", mwp->mntfrom, "Filesystem");
404 if (Tflag)
405 printf(" %-*s", mwp->fstype, "Type");
406 printf(" %-*s %*s %*s Capacity", mwp->total, header, mwp->used,
407 "Used", mwp->avail, "Avail");
408 if (iflag) {
409 mwp->iused = imax(hflag ? 0 : mwp->iused,
410 (int)strlen(" iused"));
411 mwp->ifree = imax(hflag ? 0 : mwp->ifree,
412 (int)strlen("ifree"));
413 printf(" %*s %*s %%iused", mwp->iused - 2,
414 "iused", mwp->ifree, "ifree");
416 printf(" Mounted on\n");
418 printf("%-*s", mwp->mntfrom, sfsp->f_mntfromname);
419 if (Tflag)
420 printf(" %-*s", mwp->fstype, sfsp->f_fstypename);
421 used = vsfsp->f_blocks - vsfsp->f_bfree;
422 availblks = vsfsp->f_bavail + used;
423 if (hflag) {
424 prthuman(vsfsp, used);
425 } else {
426 printf(" %*jd %*jd %*jd", mwp->total,
427 fsbtoblk(vsfsp->f_blocks, vsfsp->f_bsize, blocksize),
428 mwp->used, fsbtoblk(used, vsfsp->f_bsize, blocksize),
429 mwp->avail, fsbtoblk(vsfsp->f_bavail, vsfsp->f_bsize,
430 blocksize));
432 printf(" %5.0f%%",
433 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
434 if (iflag) {
435 inodes = vsfsp->f_files;
436 used = inodes - vsfsp->f_ffree;
437 if (hflag) {
438 printf(" ");
439 prthumanvalinode(used);
440 prthumanvalinode(vsfsp->f_ffree);
441 } else {
442 printf(" %*jd %*jd", mwp->iused, (intmax_t)used,
443 mwp->ifree, (intmax_t)vsfsp->f_ffree);
445 printf(" %4.0f%% ", inodes == 0 ? 100.0 :
446 (double)used / (double)inodes * 100.0);
447 } else
448 printf(" ");
449 printf(" %s\n", sfsp->f_mntonname);
453 * Update the maximum field-width information in `mwp' based on
454 * the filesystem specified by `sfsp'.
456 static void
457 update_maxwidths(struct maxwidths *mwp, struct statfs *sfsp, struct statvfs *vsfsp)
459 static long blocksize;
460 int dummy;
462 if (blocksize == 0)
463 getbsize(&dummy, &blocksize);
465 mwp->mntfrom = imax(mwp->mntfrom, strlen(sfsp->f_mntfromname));
466 mwp->fstype = imax(mwp->fstype, strlen(sfsp->f_fstypename));
467 mwp->total = imax(mwp->total, quadwidth(fsbtoblk(vsfsp->f_blocks,
468 vsfsp->f_bsize, blocksize)));
469 mwp->used = imax(mwp->used, quadwidth(fsbtoblk(vsfsp->f_blocks -
470 vsfsp->f_bfree, vsfsp->f_bsize, blocksize)));
471 mwp->avail = imax(mwp->avail, quadwidth(fsbtoblk(vsfsp->f_bavail,
472 vsfsp->f_bsize, blocksize)));
473 mwp->iused = imax(mwp->iused, quadwidth(vsfsp->f_files -
474 vsfsp->f_ffree));
475 mwp->ifree = imax(mwp->ifree, quadwidth(vsfsp->f_ffree));
478 /* Return the width in characters of the specified long. */
479 static int
480 quadwidth(int64_t val)
482 int len;
484 len = 0;
485 /* Negative or zero values require one extra digit. */
486 if (val <= 0) {
487 val = -val;
488 len++;
490 while (val > 0) {
491 len++;
492 val /= 10;
494 return (len);
497 static void
498 usage(void)
501 fprintf(stderr,
502 "usage: df [-b | -H | -h | -k | -m | -P] [-ailnT] [-t type] [file | filesystem ...]\n");
503 exit(EX_USAGE);
506 static char *
507 makenetvfslist(void)
509 char *str, *strptr, **listptr;
510 int mib[3], maxvfsconf, cnt=0, i;
511 size_t miblen;
512 struct ovfsconf *ptr;
514 mib[0] = CTL_VFS; mib[1] = VFS_GENERIC; mib[2] = VFS_MAXTYPENUM;
515 miblen=sizeof(maxvfsconf);
516 if (sysctl(mib, (unsigned int)(sizeof(mib) / sizeof(mib[0])),
517 &maxvfsconf, &miblen, NULL, 0)) {
518 warnx("sysctl failed");
519 return (NULL);
522 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
523 warnx("malloc failed");
524 return (NULL);
527 for (ptr = getvfsent(); ptr; ptr = getvfsent())
528 if (ptr->vfc_flags & VFCF_NETWORK) {
529 listptr[cnt++] = strdup(ptr->vfc_name);
530 if (listptr[cnt-1] == NULL) {
531 warnx("malloc failed");
532 free(listptr);
533 return (NULL);
537 if (cnt == 0 ||
538 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
539 if (cnt > 0)
540 warnx("malloc failed");
541 free(listptr);
542 return (NULL);
545 *str = 'n'; *(str + 1) = 'o';
546 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
547 strncpy(strptr, listptr[i], 32);
548 strptr += strlen(listptr[i]);
549 *strptr = ',';
550 free(listptr[i]);
552 *(--strptr) = '\0';
554 free(listptr);
555 return (str);