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