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