xref: /freebsd/bin/df/df.c (revision 7db423d69273644d3be2a65a48a67b9e0cfb0147)
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 <err.h>
56 #include <getopt.h>
57 #include <libutil.h>
58 #include <locale.h>
59 #include <stdint.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sysexits.h>
64 #include <unistd.h>
65 #include <libxo/xo.h>
66 
67 #define UNITS_SI	1
68 #define UNITS_2		2
69 
70 /* Maximum widths of various fields. */
71 struct maxwidths {
72 	int	mntfrom;
73 	int	fstype;
74 	int	total;
75 	int	used;
76 	int	avail;
77 	int	iused;
78 	int	ifree;
79 };
80 
81 static void	  addstat(struct statfs *, struct statfs *);
82 static char	 *getmntpt(const char *);
83 static const char **makevfslist(char *fslist, int *skip);
84 static int	  checkvfsname(const char *vfsname, const char **vfslist, int skip);
85 static int	  checkvfsselected(char *);
86 static int	  int64width(int64_t);
87 static char	 *makenetvfslist(void);
88 static void	  prthuman(const struct statfs *, int64_t);
89 static void	  prthumanval(const char *, int64_t);
90 static intmax_t	  fsbtoblk(int64_t, uint64_t, u_long);
91 static void	  prtstat(struct statfs *, struct maxwidths *);
92 static size_t	  regetmntinfo(struct statfs **, long);
93 static void	  update_maxwidths(struct maxwidths *, const struct statfs *);
94 static void	  usage(void);
95 
96 static __inline int
97 imax(int a, int b)
98 {
99 	return (a > b ? a : b);
100 }
101 
102 static int	  aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
103 static int	  thousands;
104 static int	  skipvfs_l, skipvfs_t;
105 static const char **vfslist_l, **vfslist_t;
106 
107 static const struct option long_options[] =
108 {
109 	{ "si", no_argument, NULL, 'H' },
110 	{ NULL, no_argument, NULL, 0 },
111 };
112 
113 int
114 main(int argc, char *argv[])
115 {
116 	struct stat stbuf;
117 	struct statfs statfsbuf, totalbuf;
118 	struct maxwidths maxwidths;
119 	struct statfs *mntbuf;
120 	char *mntpt;
121 	int i, mntsize;
122 	int ch, rv;
123 
124 	(void)setlocale(LC_ALL, "");
125 	memset(&maxwidths, 0, sizeof(maxwidths));
126 	memset(&totalbuf, 0, sizeof(totalbuf));
127 	totalbuf.f_bsize = DEV_BSIZE;
128 	strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
129 
130 	argc = xo_parse_args(argc, argv);
131 	if (argc < 0)
132 		exit(1);
133 
134 	while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options,
135 	    NULL)) != -1)
136 		switch (ch) {
137 		case 'a':
138 			aflag = 1;
139 			break;
140 		case 'b':
141 				/* FALLTHROUGH */
142 		case 'P':
143 			/*
144 			 * POSIX specifically discusses the behavior of
145 			 * both -k and -P. It states that the blocksize should
146 			 * be set to 1024. Thus, if this occurs, simply break
147 			 * rather than clobbering the old blocksize.
148 			 */
149 			if (kflag)
150 				break;
151 			setenv("BLOCKSIZE", "512", 1);
152 			hflag = 0;
153 			break;
154 		case 'c':
155 			cflag = 1;
156 			break;
157 		case 'g':
158 			setenv("BLOCKSIZE", "1g", 1);
159 			hflag = 0;
160 			break;
161 		case 'H':
162 			hflag = UNITS_SI;
163 			break;
164 		case 'h':
165 			hflag = UNITS_2;
166 			break;
167 		case 'i':
168 			iflag = 1;
169 			break;
170 		case 'k':
171 			kflag++;
172 			setenv("BLOCKSIZE", "1024", 1);
173 			hflag = 0;
174 			break;
175 		case 'l':
176 			/* Ignore duplicate -l */
177 			if (lflag)
178 				break;
179 			vfslist_l = makevfslist(makenetvfslist(), &skipvfs_l);
180 			lflag = 1;
181 			break;
182 		case 'm':
183 			setenv("BLOCKSIZE", "1m", 1);
184 			hflag = 0;
185 			break;
186 		case 'n':
187 			nflag = 1;
188 			break;
189 		case 't':
190 			if (vfslist_t != NULL)
191 				xo_errx(1, "only one -t option may be specified");
192 			vfslist_t = makevfslist(optarg, &skipvfs_t);
193 			break;
194 		case 'T':
195 			Tflag = 1;
196 			break;
197 		case ',':
198 			thousands = 1;
199 			break;
200 		case '?':
201 		default:
202 			usage();
203 		}
204 	argc -= optind;
205 	argv += optind;
206 
207 	rv = 0;
208 	if (!*argv) {
209 		/* everything (modulo -t) */
210 		mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
211 		mntsize = regetmntinfo(&mntbuf, mntsize);
212 	} else {
213 		/* just the filesystems specified on the command line */
214 		mntbuf = malloc(argc * sizeof(*mntbuf));
215 		if (mntbuf == NULL)
216 			xo_err(1, "malloc()");
217 		mntsize = 0;
218 		/* continued in for loop below */
219 	}
220 
221 	xo_open_container("storage-system-information");
222 	xo_open_list("filesystem");
223 
224 	/* iterate through specified filesystems */
225 	for (; *argv; argv++) {
226 		if (stat(*argv, &stbuf) < 0) {
227 			if ((mntpt = getmntpt(*argv)) == NULL) {
228 				xo_warn("%s", *argv);
229 				rv = 1;
230 				continue;
231 			}
232 		} else if (S_ISCHR(stbuf.st_mode)) {
233 			mntpt = getmntpt(*argv);
234 			if (mntpt == NULL) {
235 				xo_warnx("%s: not mounted", *argv);
236 				rv = 1;
237 				continue;
238 			}
239 		} else {
240 			mntpt = *argv;
241 		}
242 
243 		/*
244 		 * Statfs does not take a `wait' flag, so we cannot
245 		 * implement nflag here.
246 		 */
247 		if (statfs(mntpt, &statfsbuf) < 0) {
248 			xo_warn("%s", mntpt);
249 			rv = 1;
250 			continue;
251 		}
252 
253 		/*
254 		 * Check to make sure the arguments we've been given are
255 		 * satisfied.  Return an error if we have been asked to
256 		 * list a mount point that does not match the other args
257 		 * we've been given (-l, -t, etc.).
258 		 */
259 		if (checkvfsselected(statfsbuf.f_fstypename) != 0) {
260 			rv = 1;
261 			continue;
262 		}
263 
264 		/* the user asked for it, so ignore the ignore flag */
265 		statfsbuf.f_flags &= ~MNT_IGNORE;
266 
267 		/* add to list */
268 		mntbuf[mntsize++] = statfsbuf;
269 	}
270 
271 	memset(&maxwidths, 0, sizeof(maxwidths));
272 	for (i = 0; i < mntsize; i++) {
273 		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
274 			update_maxwidths(&maxwidths, &mntbuf[i]);
275 			if (cflag)
276 				addstat(&totalbuf, &mntbuf[i]);
277 		}
278 	}
279 	for (i = 0; i < mntsize; i++)
280 		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
281 			prtstat(&mntbuf[i], &maxwidths);
282 
283 	xo_close_list("filesystem");
284 
285 	if (cflag)
286 		prtstat(&totalbuf, &maxwidths);
287 
288 	xo_close_container("storage-system-information");
289 	xo_finish();
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 		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 		xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
565 			inodes == 0 ? 100.0 :
566 			(double)used / (double)inodes * 100.0);
567 	} else
568 		xo_emit("  ");
569 	if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
570 		xo_emit("  {:mounted-on}", sfsp->f_mntonname);
571 	xo_emit("\n");
572 	xo_close_instance("filesystem");
573 }
574 
575 static void
576 addstat(struct statfs *totalfsp, struct statfs *statfsp)
577 {
578 	uint64_t bsize;
579 
580 	bsize = statfsp->f_bsize / totalfsp->f_bsize;
581 	totalfsp->f_blocks += statfsp->f_blocks * bsize;
582 	totalfsp->f_bfree += statfsp->f_bfree * bsize;
583 	totalfsp->f_bavail += statfsp->f_bavail * bsize;
584 	totalfsp->f_files += statfsp->f_files;
585 	totalfsp->f_ffree += statfsp->f_ffree;
586 }
587 
588 /*
589  * Update the maximum field-width information in `mwp' based on
590  * the file system specified by `sfsp'.
591  */
592 static void
593 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
594 {
595 	static long blocksize = 0;
596 	int dummy;
597 
598 	if (blocksize == 0)
599 		getbsize(&dummy, &blocksize);
600 
601 	mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
602 	mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
603 	mwp->total = imax(mwp->total, int64width(
604 	    fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
605 	mwp->used = imax(mwp->used,
606 	    int64width(fsbtoblk((int64_t)sfsp->f_blocks -
607 	    (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
608 	mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
609 	    sfsp->f_bsize, blocksize)));
610 	mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
611 	    sfsp->f_ffree));
612 	mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
613 }
614 
615 /* Return the width in characters of the specified value. */
616 static int
617 int64width(int64_t val)
618 {
619 	int len;
620 
621 	len = 0;
622 	/* Negative or zero values require one extra digit. */
623 	if (val <= 0) {
624 		val = -val;
625 		len++;
626 	}
627 	while (val > 0) {
628 		len++;
629 		val /= 10;
630 	}
631 
632 	return (len);
633 }
634 
635 static void
636 usage(void)
637 {
638 
639 	xo_error(
640 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
641 "          [file | filesystem ...]\n");
642 	exit(EX_USAGE);
643 }
644 
645 static char *
646 makenetvfslist(void)
647 {
648 	char *str, *strptr, **listptr;
649 	struct xvfsconf *xvfsp, *keep_xvfsp;
650 	size_t buflen;
651 	int cnt, i, maxvfsconf;
652 
653 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
654 		xo_warn("sysctl(vfs.conflist)");
655 		return (NULL);
656 	}
657 	xvfsp = malloc(buflen);
658 	if (xvfsp == NULL) {
659 		xo_warnx("malloc failed");
660 		return (NULL);
661 	}
662 	keep_xvfsp = xvfsp;
663 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
664 		xo_warn("sysctl(vfs.conflist)");
665 		free(keep_xvfsp);
666 		return (NULL);
667 	}
668 	maxvfsconf = buflen / sizeof(struct xvfsconf);
669 
670 	if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
671 		xo_warnx("malloc failed");
672 		free(keep_xvfsp);
673 		return (NULL);
674 	}
675 
676 	for (cnt = 0, i = 0; i < maxvfsconf; i++) {
677 		if (xvfsp->vfc_flags & VFCF_NETWORK) {
678 			listptr[cnt++] = strdup(xvfsp->vfc_name);
679 			if (listptr[cnt-1] == NULL) {
680 				xo_warnx("malloc failed");
681 				free(listptr);
682 				free(keep_xvfsp);
683 				return (NULL);
684 			}
685 		}
686 		xvfsp++;
687 	}
688 
689 	if (cnt == 0 ||
690 	    (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
691 		if (cnt > 0)
692 			xo_warnx("malloc failed");
693 		free(listptr);
694 		free(keep_xvfsp);
695 		return (NULL);
696 	}
697 
698 	*str = 'n'; *(str + 1) = 'o';
699 	for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
700 		strlcpy(strptr, listptr[i], 32);
701 		strptr += strlen(listptr[i]);
702 		*strptr = ',';
703 		free(listptr[i]);
704 	}
705 	*(--strptr) = '\0';
706 
707 	free(keep_xvfsp);
708 	free(listptr);
709 	return (str);
710 }
711