lsvfs(1): Sync a bit with FreeBSD, as applicable.
[dragonfly.git] / usr.bin / lsvfs / lsvfs.c
1 /*
2  * lsvfs - list loaded VFSes
3  * Garrett A. Wollman, September 1994
4  * This file is in the public domain.
5  *
6  * $FreeBSD: src/usr.bin/lsvfs/lsvfs.c,v 1.13.2.1 2001/07/30 09:59:16 dd Exp $
7  */
8
9 #define _NEW_VFSCONF
10
11 #include <sys/param.h>
12 #include <sys/mount.h>
13
14 #include <err.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #define FMT     "%-32.32s 0x%08x %5d  %s\n"
19 #define HDRFMT  "%-32.32s %10s %5.5s  %s\n"
20 #define DASHES  "-------------------------------- "     \
21                 "---------- -----  ---------------\n"
22
23 static struct flaglist {
24         int             flag;
25         const char      str[32]; /* must be longer than the longest one. */
26 } fl[] = {
27         { .flag = VFCF_STATIC, .str = "static", },
28         { .flag = VFCF_NETWORK, .str = "network", },
29         { .flag = VFCF_READONLY, .str = "read-only", },
30         { .flag = VFCF_SYNTHETIC, .str = "synthetic", },
31         { .flag = VFCF_LOOPBACK, .str = "loopback", },
32         { .flag = VFCF_UNICODE, .str = "unicode", },
33 };
34
35 static const char *fmt_flags(int);
36
37 int
38 main(int argc, char **argv)
39 {
40   int rv = 0;
41   struct vfsconf vfc;
42   struct ovfsconf *ovfcp;
43   argc--, argv++;
44
45   setvfsent(1);
46
47   printf(HDRFMT, "Filesystem", "Num", "Refs", "Flags");
48   fputs(DASHES, stdout);
49
50   if(argc) {
51     for(; argc; argc--, argv++) {
52       if (getvfsbyname(*argv, &vfc) == 0) {
53         printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
54             fmt_flags(vfc.vfc_flags));
55       } else {
56         warnx("VFS %s unknown or not loaded", *argv);
57         rv++;
58       }
59     }
60   } else {
61     while ((ovfcp = getvfsent()) != NULL) {
62       if (getvfsbyname(ovfcp->vfc_name, &vfc) == 0) {
63         printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
64             fmt_flags(vfc.vfc_flags));
65       } else {
66         warnx("VFS %s unknown or not loaded", *argv);
67         rv++;
68       }
69     }
70   }
71
72   endvfsent();
73   return rv;
74 }
75
76 static const char *
77 fmt_flags(int flags)
78 {
79         static char buf[sizeof(struct flaglist) * sizeof(fl)];
80         int i;
81
82         buf[0] = '\0';
83         for (i = 0; i < (int)nitems(fl); i++)
84                 if (flags & fl[i].flag) {
85                         strlcat(buf, fl[i].str, sizeof(buf));
86                         strlcat(buf, ", ", sizeof(buf));
87                 }
88         if (buf[0] != '\0')
89                 buf[strlen(buf) - 2] = '\0';
90         return (buf);
91 }