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