]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/df/df.c
Import tzdata 2018c
[FreeBSD/FreeBSD.git] / bin / df / df.c
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 #ifdef MOUNT_CHAR_DEVS
258                 } else if (S_ISCHR(stbuf.st_mode)) {
259                         if ((mntpt = getmntpt(*argv)) == NULL) {
260                                 mdev.fspec = *argv;
261                                 mntpath = strdup("/tmp/df.XXXXXX");
262                                 if (mntpath == NULL) {
263                                         xo_warn("strdup failed");
264                                         rv = 1;
265                                         continue;
266                                 }
267                                 mntpt = mkdtemp(mntpath);
268                                 if (mntpt == NULL) {
269                                         xo_warn("mkdtemp(\"%s\") failed", mntpath);
270                                         rv = 1;
271                                         free(mntpath);
272                                         continue;
273                                 }
274                                 if (iov != NULL)
275                                         free_iovec(&iov, &iovlen);
276                                 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
277                                     fstype);
278                                 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
279                                     mntpath);
280                                 build_iovec_argf(&iov, &iovlen, "from", "%s",
281                                     *argv);
282                                 build_iovec(&iov, &iovlen, "errmsg", errmsg,
283                                     sizeof(errmsg));
284                                 if (nmount(iov, iovlen,
285                                     MNT_RDONLY|MNT_NOEXEC) < 0) {
286                                         if (errmsg[0])
287                                                 xo_warn("%s: %s", *argv,
288                                                     errmsg);
289                                         else
290                                                 xo_warn("%s", *argv);
291                                         rv = 1;
292                                         (void)rmdir(mntpt);
293                                         free(mntpath);
294                                         continue;
295                                 } else if (statfs(mntpt, &statfsbuf) == 0) {
296                                         statfsbuf.f_mntonname[0] = '\0';
297                                         prtstat(&statfsbuf, &maxwidths);
298                                         if (cflag)
299                                                 addstat(&totalbuf, &statfsbuf);
300                                 } else {
301                                         xo_warn("%s", *argv);
302                                         rv = 1;
303                                 }
304                                 (void)unmount(mntpt, 0);
305                                 (void)rmdir(mntpt);
306                                 free(mntpath);
307                                 continue;
308                         }
309 #endif
310                 } else
311                         mntpt = *argv;
312
313                 /*
314                  * Statfs does not take a `wait' flag, so we cannot
315                  * implement nflag here.
316                  */
317                 if (statfs(mntpt, &statfsbuf) < 0) {
318                         xo_warn("%s", mntpt);
319                         rv = 1;
320                         continue;
321                 }
322
323                 /*
324                  * Check to make sure the arguments we've been given are
325                  * satisfied.  Return an error if we have been asked to
326                  * list a mount point that does not match the other args
327                  * we've been given (-l, -t, etc.).
328                  */
329                 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
330                         rv = 1;
331                         continue;
332                 }
333
334                 /* the user asked for it, so ignore the ignore flag */
335                 statfsbuf.f_flags &= ~MNT_IGNORE;
336
337                 /* add to list */
338                 mntbuf[mntsize++] = statfsbuf;
339         }
340
341         memset(&maxwidths, 0, sizeof(maxwidths));
342         for (i = 0; i < mntsize; i++) {
343                 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
344                         update_maxwidths(&maxwidths, &mntbuf[i]);
345                         if (cflag)
346                                 addstat(&totalbuf, &mntbuf[i]);
347                 }
348         }
349         for (i = 0; i < mntsize; i++)
350                 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
351                         prtstat(&mntbuf[i], &maxwidths);
352
353         xo_close_list("filesystem");
354
355         if (cflag)
356                 prtstat(&totalbuf, &maxwidths);
357
358         xo_close_container("storage-system-information");
359         xo_finish();
360         exit(rv);
361 }
362
363 static char *
364 getmntpt(const char *name)
365 {
366         size_t mntsize, i;
367         struct statfs *mntbuf;
368
369         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
370         for (i = 0; i < mntsize; i++) {
371                 if (!strcmp(mntbuf[i].f_mntfromname, name))
372                         return (mntbuf[i].f_mntonname);
373         }
374         return (NULL);
375 }
376
377 /*
378  * Make a pass over the file system info in ``mntbuf'' filtering out
379  * file system types not in vfslist 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, const char **vfslist)
384 {
385         int error, i, j;
386         struct statfs *mntbuf;
387
388         if (vfslist == NULL)
389                 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
390
391         mntbuf = *mntbufp;
392         for (j = 0, i = 0; i < mntsize; i++) {
393                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
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                 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
563                         inodes == 0 ? 100.0 :
564                         (double)used / (double)inodes * 100.0);
565         } else
566                 xo_emit("  ");
567         if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
568                 xo_emit("  {:mounted-on}", sfsp->f_mntonname);
569         xo_emit("\n");
570         xo_close_instance("filesystem");
571 }
572
573 static void
574 addstat(struct statfs *totalfsp, struct statfs *statfsp)
575 {
576         uint64_t bsize;
577
578         bsize = statfsp->f_bsize / totalfsp->f_bsize;
579         totalfsp->f_blocks += statfsp->f_blocks * bsize;
580         totalfsp->f_bfree += statfsp->f_bfree * bsize;
581         totalfsp->f_bavail += statfsp->f_bavail * bsize;
582         totalfsp->f_files += statfsp->f_files;
583         totalfsp->f_ffree += statfsp->f_ffree;
584 }
585
586 /*
587  * Update the maximum field-width information in `mwp' based on
588  * the file system specified by `sfsp'.
589  */
590 static void
591 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
592 {
593         static long blocksize = 0;
594         int dummy;
595
596         if (blocksize == 0)
597                 getbsize(&dummy, &blocksize);
598
599         mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
600         mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
601         mwp->total = imax(mwp->total, int64width(
602             fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
603         mwp->used = imax(mwp->used,
604             int64width(fsbtoblk((int64_t)sfsp->f_blocks -
605             (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
606         mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
607             sfsp->f_bsize, blocksize)));
608         mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
609             sfsp->f_ffree));
610         mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
611 }
612
613 /* Return the width in characters of the specified value. */
614 static int
615 int64width(int64_t val)
616 {
617         int len;
618
619         len = 0;
620         /* Negative or zero values require one extra digit. */
621         if (val <= 0) {
622                 val = -val;
623                 len++;
624         }
625         while (val > 0) {
626                 len++;
627                 val /= 10;
628         }
629
630         return (len);
631 }
632
633 static void
634 usage(void)
635 {
636
637         xo_error(
638 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
639 "          [file | filesystem ...]\n");
640         exit(EX_USAGE);
641 }
642
643 static char *
644 makenetvfslist(void)
645 {
646         char *str, *strptr, **listptr;
647         struct xvfsconf *xvfsp, *keep_xvfsp;
648         size_t buflen;
649         int cnt, i, maxvfsconf;
650
651         if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
652                 xo_warn("sysctl(vfs.conflist)");
653                 return (NULL);
654         }
655         xvfsp = malloc(buflen);
656         if (xvfsp == NULL) {
657                 xo_warnx("malloc failed");
658                 return (NULL);
659         }
660         keep_xvfsp = xvfsp;
661         if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
662                 xo_warn("sysctl(vfs.conflist)");
663                 free(keep_xvfsp);
664                 return (NULL);
665         }
666         maxvfsconf = buflen / sizeof(struct xvfsconf);
667
668         if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
669                 xo_warnx("malloc failed");
670                 free(keep_xvfsp);
671                 return (NULL);
672         }
673
674         for (cnt = 0, i = 0; i < maxvfsconf; i++) {
675                 if (xvfsp->vfc_flags & VFCF_NETWORK) {
676                         listptr[cnt++] = strdup(xvfsp->vfc_name);
677                         if (listptr[cnt-1] == NULL) {
678                                 xo_warnx("malloc failed");
679                                 free(listptr);
680                                 free(keep_xvfsp);
681                                 return (NULL);
682                         }
683                 }
684                 xvfsp++;
685         }
686
687         if (cnt == 0 ||
688             (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
689                 if (cnt > 0)
690                         xo_warnx("malloc failed");
691                 free(listptr);
692                 free(keep_xvfsp);
693                 return (NULL);
694         }
695
696         *str = 'n'; *(str + 1) = 'o';
697         for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
698                 strlcpy(strptr, listptr[i], 32);
699                 strptr += strlen(listptr[i]);
700                 *strptr = ',';
701                 free(listptr[i]);
702         }
703         *(--strptr) = '\0';
704
705         free(keep_xvfsp);
706         free(listptr);
707         return (str);
708 }