xref: /freebsd/bin/df/df.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
38 #ifndef lint
39 static const char 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 static char sccsid[] = "@(#)df.c	8.9 (Berkeley) 5/8/95";
46 #endif /* not lint */
47 #endif
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <sys/mount.h>
54 #include <sys/sysctl.h>
55 #ifdef MOUNT_CHAR_DEVS
56 #include <ufs/ufs/ufsmount.h>
57 #endif
58 #include <err.h>
59 #include <getopt.h>
60 #include <libutil.h>
61 #include <locale.h>
62 #ifdef MOUNT_CHAR_DEVS
63 #include <mntopts.h>
64 #endif
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <sysexits.h>
70 #include <unistd.h>
71 #include <libxo/xo.h>
72 
73 #include "extern.h"
74 
75 #define UNITS_SI	1
76 #define UNITS_2		2
77 
78 /* Maximum widths of various fields. */
79 struct maxwidths {
80 	int	mntfrom;
81 	int	fstype;
82 	int	total;
83 	int	used;
84 	int	avail;
85 	int	iused;
86 	int	ifree;
87 };
88 
89 static void	  addstat(struct statfs *, struct statfs *);
90 static char	 *getmntpt(const char *);
91 static int	  int64width(int64_t);
92 static char	 *makenetvfslist(void);
93 static void	  prthuman(const struct statfs *, int64_t);
94 static void	  prthumanval(const char *, int64_t);
95 static intmax_t	  fsbtoblk(int64_t, uint64_t, u_long);
96 static void	  prtstat(struct statfs *, struct maxwidths *);
97 static size_t	  regetmntinfo(struct statfs **, long, const char **);
98 static void	  update_maxwidths(struct maxwidths *, const struct statfs *);
99 static void	  usage(void);
100 
101 static __inline int
102 imax(int a, int b)
103 {
104 	return (a > b ? a : b);
105 }
106 
107 static int	aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
108 static int	thousands;
109 #ifdef MOUNT_CHAR_DEVS
110 static struct	ufs_args mdev;
111 #endif
112 
113 static const struct option long_options[] =
114 {
115 	{ "si", no_argument, NULL, 'H' },
116 	{ NULL, no_argument, NULL, 0 },
117 };
118 
119 int
120 main(int argc, char *argv[])
121 {
122 	struct stat stbuf;
123 	struct statfs statfsbuf, totalbuf;
124 	struct maxwidths maxwidths;
125 	struct statfs *mntbuf;
126 #ifdef MOUNT_CHAR_DEVS
127 	struct iovec *iov = NULL;
128 #endif
129 	const char *fstype;
130 #ifdef MOUNT_CHAR_DEVS
131 	char *mntpath;
132 	char errmsg[255] = {0};
133 #endif
134 	char *mntpt;
135 	const char **vfslist;
136 	int i, mntsize;
137 	int ch, rv;
138 #ifdef MOUNT_CHAR_DEVS
139 	int iovlen = 0;
140 #endif
141 
142 	fstype = "ufs";
143 	(void)setlocale(LC_ALL, "");
144 	memset(&maxwidths, 0, sizeof(maxwidths));
145 	memset(&totalbuf, 0, sizeof(totalbuf));
146 	totalbuf.f_bsize = DEV_BSIZE;
147 	strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
148 	vfslist = NULL;
149 
150 	argc = xo_parse_args(argc, argv);
151 	if (argc < 0)
152 		exit(1);
153 
154 	while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options,
155 	    NULL)) != -1)
156 		switch (ch) {
157 		case 'a':
158 			aflag = 1;
159 			break;
160 		case 'b':
161 				/* FALLTHROUGH */
162 		case 'P':
163 			/*
164 			 * POSIX specifically discusses the behavior of
165 			 * both -k and -P. It states that the blocksize should
166 			 * be set to 1024. Thus, if this occurs, simply break
167 			 * rather than clobbering the old blocksize.
168 			 */
169 			if (kflag)
170 				break;
171 			setenv("BLOCKSIZE", "512", 1);
172 			hflag = 0;
173 			break;
174 		case 'c':
175 			cflag = 1;
176 			break;
177 		case 'g':
178 			setenv("BLOCKSIZE", "1g", 1);
179 			hflag = 0;
180 			break;
181 		case 'H':
182 			hflag = UNITS_SI;
183 			break;
184 		case 'h':
185 			hflag = UNITS_2;
186 			break;
187 		case 'i':
188 			iflag = 1;
189 			break;
190 		case 'k':
191 			kflag++;
192 			setenv("BLOCKSIZE", "1024", 1);
193 			hflag = 0;
194 			break;
195 		case 'l':
196 			/* Ignore duplicate -l */
197 			if (lflag)
198 				break;
199 			if (vfslist != NULL)
200 				xo_errx(1, "-l and -t are mutually exclusive.");
201 			vfslist = makevfslist(makenetvfslist());
202 			lflag = 1;
203 			break;
204 		case 'm':
205 			setenv("BLOCKSIZE", "1m", 1);
206 			hflag = 0;
207 			break;
208 		case 'n':
209 			nflag = 1;
210 			break;
211 		case 't':
212 			if (lflag)
213 				xo_errx(1, "-l and -t are mutually exclusive.");
214 			if (vfslist != NULL)
215 				xo_errx(1, "only one -t option may be specified");
216 			fstype = optarg;
217 			vfslist = makevfslist(optarg);
218 			break;
219 		case 'T':
220 			Tflag = 1;
221 			break;
222 		case ',':
223 			thousands = 1;
224 			break;
225 		case '?':
226 		default:
227 			usage();
228 		}
229 	argc -= optind;
230 	argv += optind;
231 
232 	rv = 0;
233 	if (!*argv) {
234 		/* everything (modulo -t) */
235 		mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
236 		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
237 	} else {
238 		/* just the filesystems specified on the command line */
239 		mntbuf = malloc(argc * sizeof(*mntbuf));
240 		if (mntbuf == NULL)
241 			xo_err(1, "malloc()");
242 		mntsize = 0;
243 		/* continued in for loop below */
244 	}
245 
246 	xo_open_container("storage-system-information");
247 	xo_open_list("filesystem");
248 
249 	/* iterate through specified filesystems */
250 	for (; *argv; argv++) {
251 		if (stat(*argv, &stbuf) < 0) {
252 			if ((mntpt = getmntpt(*argv)) == NULL) {
253 				xo_warn("%s", *argv);
254 				rv = 1;
255 				continue;
256 			}
257 		} else if (S_ISCHR(stbuf.st_mode)) {
258 			if ((mntpt = getmntpt(*argv)) == NULL) {
259 #ifdef MOUNT_CHAR_DEVS
260 				xo_warnx(
261 				    "df on unmounted devices is deprecated");
262 				mdev.fspec = *argv;
263 				mntpath = strdup("/tmp/df.XXXXXX");
264 				if (mntpath == NULL) {
265 					xo_warn("strdup failed");
266 					rv = 1;
267 					continue;
268 				}
269 				mntpt = mkdtemp(mntpath);
270 				if (mntpt == NULL) {
271 					xo_warn("mkdtemp(\"%s\") failed", mntpath);
272 					rv = 1;
273 					free(mntpath);
274 					continue;
275 				}
276 				if (iov != NULL)
277 					free_iovec(&iov, &iovlen);
278 				build_iovec_argf(&iov, &iovlen, "fstype", "%s",
279 				    fstype);
280 				build_iovec_argf(&iov, &iovlen, "fspath", "%s",
281 				    mntpath);
282 				build_iovec_argf(&iov, &iovlen, "from", "%s",
283 				    *argv);
284 				build_iovec(&iov, &iovlen, "errmsg", errmsg,
285 				    sizeof(errmsg));
286 				if (nmount(iov, iovlen,
287 				    MNT_RDONLY|MNT_NOEXEC) < 0) {
288 					if (errmsg[0])
289 						xo_warn("%s: %s", *argv,
290 						    errmsg);
291 					else
292 						xo_warn("%s", *argv);
293 					rv = 1;
294 					(void)rmdir(mntpt);
295 					free(mntpath);
296 					continue;
297 				} else if (statfs(mntpt, &statfsbuf) == 0) {
298 					statfsbuf.f_mntonname[0] = '\0';
299 					prtstat(&statfsbuf, &maxwidths);
300 					if (cflag)
301 						addstat(&totalbuf, &statfsbuf);
302 				} else {
303 					xo_warn("%s", *argv);
304 					rv = 1;
305 				}
306 				(void)unmount(mntpt, 0);
307 				(void)rmdir(mntpt);
308 				free(mntpath);
309 				continue;
310 #else
311 				xo_warnx("%s: not mounted", *argv);
312 				rv = 1;
313 				continue;
314 #endif
315 			}
316 		} else
317 			mntpt = *argv;
318 
319 		/*
320 		 * Statfs does not take a `wait' flag, so we cannot
321 		 * implement nflag here.
322 		 */
323 		if (statfs(mntpt, &statfsbuf) < 0) {
324 			xo_warn("%s", mntpt);
325 			rv = 1;
326 			continue;
327 		}
328 
329 		/*
330 		 * Check to make sure the arguments we've been given are
331 		 * satisfied.  Return an error if we have been asked to
332 		 * list a mount point that does not match the other args
333 		 * we've been given (-l, -t, etc.).
334 		 */
335 		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
336 			rv = 1;
337 			continue;
338 		}
339 
340 		/* the user asked for it, so ignore the ignore flag */
341 		statfsbuf.f_flags &= ~MNT_IGNORE;
342 
343 		/* add to list */
344 		mntbuf[mntsize++] = statfsbuf;
345 	}
346 
347 	memset(&maxwidths, 0, sizeof(maxwidths));
348 	for (i = 0; i < mntsize; i++) {
349 		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
350 			update_maxwidths(&maxwidths, &mntbuf[i]);
351 			if (cflag)
352 				addstat(&totalbuf, &mntbuf[i]);
353 		}
354 	}
355 	for (i = 0; i < mntsize; i++)
356 		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
357 			prtstat(&mntbuf[i], &maxwidths);
358 
359 	xo_close_list("filesystem");
360 
361 	if (cflag)
362 		prtstat(&totalbuf, &maxwidths);
363 
364 	xo_close_container("storage-system-information");
365 	xo_finish();
366 	exit(rv);
367 }
368 
369 static char *
370 getmntpt(const char *name)
371 {
372 	size_t mntsize, i;
373 	struct statfs *mntbuf;
374 
375 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
376 	for (i = 0; i < mntsize; i++) {
377 		if (!strcmp(mntbuf[i].f_mntfromname, name))
378 			return (mntbuf[i].f_mntonname);
379 	}
380 	return (NULL);
381 }
382 
383 /*
384  * Make a pass over the file system info in ``mntbuf'' filtering out
385  * file system types not in vfslist and possibly re-stating to get
386  * current (not cached) info.  Returns the new count of valid statfs bufs.
387  */
388 static size_t
389 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
390 {
391 	int error, i, j;
392 	struct statfs *mntbuf;
393 
394 	if (vfslist == NULL)
395 		return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
396 
397 	mntbuf = *mntbufp;
398 	for (j = 0, i = 0; i < mntsize; i++) {
399 		if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
400 			continue;
401 		/*
402 		 * XXX statfs(2) can fail for various reasons. It may be
403 		 * possible that the user does not have access to the
404 		 * pathname, if this happens, we will fall back on
405 		 * "stale" filesystem statistics.
406 		 */
407 		error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
408 		if (nflag || error < 0)
409 			if (i != j) {
410 				if (error < 0)
411 					xo_warnx("%s stats possibly stale",
412 					    mntbuf[i].f_mntonname);
413 				mntbuf[j] = mntbuf[i];
414 			}
415 		j++;
416 	}
417 	return (j);
418 }
419 
420 static void
421 prthuman(const struct statfs *sfsp, int64_t used)
422 {
423 
424 	prthumanval("  {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize);
425 	prthumanval("  {:used/%6s}", used * sfsp->f_bsize);
426 	prthumanval("  {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize);
427 }
428 
429 static void
430 prthumanval(const char *fmt, int64_t bytes)
431 {
432 	char buf[6];
433 	int flags;
434 
435 	flags = HN_B | HN_NOSPACE | HN_DECIMAL;
436 	if (hflag == UNITS_SI)
437 		flags |= HN_DIVISOR_1000;
438 
439 	humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
440 	    bytes, "", HN_AUTOSCALE, flags);
441 
442 	xo_attr("value", "%lld", (long long) bytes);
443 	xo_emit(fmt, buf);
444 }
445 
446 /*
447  * Print an inode count in "human-readable" format.
448  */
449 static void
450 prthumanvalinode(const char *fmt, int64_t bytes)
451 {
452 	char buf[6];
453 	int flags;
454 
455 	flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
456 
457 	humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
458 	    bytes, "", HN_AUTOSCALE, flags);
459 
460 	xo_attr("value", "%lld", (long long) bytes);
461 	xo_emit(fmt, buf);
462 }
463 
464 /*
465  * Convert statfs returned file system size into BLOCKSIZE units.
466  */
467 static intmax_t
468 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
469 {
470 	return (num * (intmax_t) fsbs / (int64_t) bs);
471 }
472 
473 /*
474  * Print out status about a file system.
475  */
476 static void
477 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
478 {
479 	static long blocksize;
480 	static int headerlen, timesthrough = 0;
481 	static const char *header;
482 	int64_t used, availblks, inodes;
483 	const char *format;
484 
485 	if (++timesthrough == 1) {
486 		mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
487 		mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
488 		if (thousands) {		/* make space for commas */
489 		    mwp->total += (mwp->total - 1) / 3;
490 		    mwp->used  += (mwp->used - 1) / 3;
491 		    mwp->avail += (mwp->avail - 1) / 3;
492 		    mwp->iused += (mwp->iused - 1) / 3;
493 		    mwp->ifree += (mwp->ifree - 1) / 3;
494 		}
495 		if (hflag) {
496 			header = "   Size";
497 			mwp->total = mwp->used = mwp->avail =
498 			    (int)strlen(header);
499 		} else {
500 			header = getbsize(&headerlen, &blocksize);
501 			mwp->total = imax(mwp->total, headerlen);
502 		}
503 		mwp->used = imax(mwp->used, (int)strlen("Used"));
504 		mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
505 
506 		xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem");
507 		if (Tflag)
508 			xo_emit("  {T:/%-*s}", mwp->fstype, "Type");
509 		xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} {T:Capacity}",
510 			mwp->total, header,
511 			mwp->used, "Used", mwp->avail, "Avail");
512 		if (iflag) {
513 			mwp->iused = imax(hflag ? 0 : mwp->iused,
514 			    (int)strlen("  iused"));
515 			mwp->ifree = imax(hflag ? 0 : mwp->ifree,
516 			    (int)strlen("ifree"));
517 			xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}",
518 			    mwp->iused - 2, "iused", mwp->ifree, "ifree");
519 		}
520 		xo_emit("  {T:Mounted on}\n");
521 	}
522 
523 	xo_open_instance("filesystem");
524 	/* Check for 0 block size.  Can this happen? */
525 	if (sfsp->f_bsize == 0) {
526 		xo_warnx ("File system %s does not have a block size, assuming 512.",
527 		    sfsp->f_mntonname);
528 		sfsp->f_bsize = 512;
529 	}
530 	xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname);
531 	if (Tflag)
532 		xo_emit("  {:type/%-*s}", mwp->fstype, sfsp->f_fstypename);
533 	used = sfsp->f_blocks - sfsp->f_bfree;
534 	availblks = sfsp->f_bavail + used;
535 	if (hflag) {
536 		prthuman(sfsp, used);
537 	} else {
538 		if (thousands)
539 		    format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} "
540 			"{t:available-blocks/%*j'd}";
541 		else
542 		    format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} "
543 			"{t:available-blocks/%*jd}";
544 		xo_emit(format,
545 		    mwp->total, fsbtoblk(sfsp->f_blocks,
546 		    sfsp->f_bsize, blocksize),
547 		    mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
548 		    mwp->avail, fsbtoblk(sfsp->f_bavail,
549 		    sfsp->f_bsize, blocksize));
550 	}
551 	xo_emit(" {:used-percent/%5.0f}{U:%%}",
552 	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
553 	if (iflag) {
554 		inodes = sfsp->f_files;
555 		used = inodes - sfsp->f_ffree;
556 		if (hflag) {
557 			xo_emit("  ");
558 			prthumanvalinode(" {:inodes-used/%5s}", used);
559 			prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree);
560 		} else {
561 			if (thousands)
562 			    format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}";
563 			else
564 			    format = " {:inodes-used/%*jd} {:inodes-free/%*jd}";
565 			xo_emit(format, mwp->iused, (intmax_t)used,
566 			    mwp->ifree, (intmax_t)sfsp->f_ffree);
567 		}
568 		xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
569 			inodes == 0 ? 100.0 :
570 			(double)used / (double)inodes * 100.0);
571 	} else
572 		xo_emit("  ");
573 	if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
574 		xo_emit("  {:mounted-on}", sfsp->f_mntonname);
575 	xo_emit("\n");
576 	xo_close_instance("filesystem");
577 }
578 
579 static void
580 addstat(struct statfs *totalfsp, struct statfs *statfsp)
581 {
582 	uint64_t bsize;
583 
584 	bsize = statfsp->f_bsize / totalfsp->f_bsize;
585 	totalfsp->f_blocks += statfsp->f_blocks * bsize;
586 	totalfsp->f_bfree += statfsp->f_bfree * bsize;
587 	totalfsp->f_bavail += statfsp->f_bavail * bsize;
588 	totalfsp->f_files += statfsp->f_files;
589 	totalfsp->f_ffree += statfsp->f_ffree;
590 }
591 
592 /*
593  * Update the maximum field-width information in `mwp' based on
594  * the file system specified by `sfsp'.
595  */
596 static void
597 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
598 {
599 	static long blocksize = 0;
600 	int dummy;
601 
602 	if (blocksize == 0)
603 		getbsize(&dummy, &blocksize);
604 
605 	mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
606 	mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
607 	mwp->total = imax(mwp->total, int64width(
608 	    fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
609 	mwp->used = imax(mwp->used,
610 	    int64width(fsbtoblk((int64_t)sfsp->f_blocks -
611 	    (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
612 	mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
613 	    sfsp->f_bsize, blocksize)));
614 	mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
615 	    sfsp->f_ffree));
616 	mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
617 }
618 
619 /* Return the width in characters of the specified value. */
620 static int
621 int64width(int64_t val)
622 {
623 	int len;
624 
625 	len = 0;
626 	/* Negative or zero values require one extra digit. */
627 	if (val <= 0) {
628 		val = -val;
629 		len++;
630 	}
631 	while (val > 0) {
632 		len++;
633 		val /= 10;
634 	}
635 
636 	return (len);
637 }
638 
639 static void
640 usage(void)
641 {
642 
643 	xo_error(
644 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
645 "          [file | filesystem ...]\n");
646 	exit(EX_USAGE);
647 }
648 
649 static char *
650 makenetvfslist(void)
651 {
652 	char *str, *strptr, **listptr;
653 	struct xvfsconf *xvfsp, *keep_xvfsp;
654 	size_t buflen;
655 	int cnt, i, maxvfsconf;
656 
657 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
658 		xo_warn("sysctl(vfs.conflist)");
659 		return (NULL);
660 	}
661 	xvfsp = malloc(buflen);
662 	if (xvfsp == NULL) {
663 		xo_warnx("malloc failed");
664 		return (NULL);
665 	}
666 	keep_xvfsp = xvfsp;
667 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
668 		xo_warn("sysctl(vfs.conflist)");
669 		free(keep_xvfsp);
670 		return (NULL);
671 	}
672 	maxvfsconf = buflen / sizeof(struct xvfsconf);
673 
674 	if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
675 		xo_warnx("malloc failed");
676 		free(keep_xvfsp);
677 		return (NULL);
678 	}
679 
680 	for (cnt = 0, i = 0; i < maxvfsconf; i++) {
681 		if (xvfsp->vfc_flags & VFCF_NETWORK) {
682 			listptr[cnt++] = strdup(xvfsp->vfc_name);
683 			if (listptr[cnt-1] == NULL) {
684 				xo_warnx("malloc failed");
685 				free(listptr);
686 				free(keep_xvfsp);
687 				return (NULL);
688 			}
689 		}
690 		xvfsp++;
691 	}
692 
693 	if (cnt == 0 ||
694 	    (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
695 		if (cnt > 0)
696 			xo_warnx("malloc failed");
697 		free(listptr);
698 		free(keep_xvfsp);
699 		return (NULL);
700 	}
701 
702 	*str = 'n'; *(str + 1) = 'o';
703 	for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
704 		strlcpy(strptr, listptr[i], 32);
705 		strptr += strlen(listptr[i]);
706 		*strptr = ',';
707 		free(listptr[i]);
708 	}
709 	*(--strptr) = '\0';
710 
711 	free(keep_xvfsp);
712 	free(listptr);
713 	return (str);
714 }
715