From 4fd5a09958db951b00db40aa4464db6dac691948 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 10 Mar 2018 10:08:27 +0100
Subject: [PATCH] lsvfs(1): Sync a bit with FreeBSD, as applicable.

Specifically, merge r185029/r251580 and sync the manual page.

Taken-from: FreeBSD
---
 usr.bin/lsvfs/lsvfs.1 |  8 +++--
 usr.bin/lsvfs/lsvfs.c | 86 +++++++++++++++++++++++----------------------------
 2 files changed, 44 insertions(+), 50 deletions(-)

diff --git a/usr.bin/lsvfs/lsvfs.1 b/usr.bin/lsvfs/lsvfs.1
index e660409624..40edcc6e2d 100644
--- a/usr.bin/lsvfs/lsvfs.1
+++ b/usr.bin/lsvfs/lsvfs.1
@@ -1,4 +1,4 @@
-.\" $FreeBSD: src/usr.bin/lsvfs/lsvfs.1,v 1.7.2.3 2003/02/24 22:37:42 trhodes Exp $
+.\" $FreeBSD: head/usr.bin/lsvfs/lsvfs.1 251580 2013-06-09 16:33:32Z hrs $
 .\" Garrett A. Wollman, September 1994
 .\" This file is in the public domain.
 .\"
@@ -7,7 +7,7 @@
 .Os
 .Sh NAME
 .Nm lsvfs
-.Nd list installed virtual filesystems
+.Nd list installed virtual file systems
 .Sh SYNOPSIS
 .Nm
 .Op Ar vfsname Ar ...
@@ -36,6 +36,8 @@ and the
 .Fl t
 option to
 .Xr mount 8
+.It Num
+the filesystem type number.
 .It Refs
 the number of references to this VFS; i.e., the number of currently
 mounted file systems of this type
@@ -44,6 +46,8 @@ flag bits.
 .El
 .Sh SEE ALSO
 .Xr mount 2 ,
+.Xr getvfsbyname 3 ,
+.Xr getvfsent 3 ,
 .Xr mount 8
 .Sh HISTORY
 A
diff --git a/usr.bin/lsvfs/lsvfs.c b/usr.bin/lsvfs/lsvfs.c
index 687f33967c..c7edb218cd 100644
--- a/usr.bin/lsvfs/lsvfs.c
+++ b/usr.bin/lsvfs/lsvfs.c
@@ -4,7 +4,6 @@
  * This file is in the public domain.
  *
  * $FreeBSD: src/usr.bin/lsvfs/lsvfs.c,v 1.13.2.1 2001/07/30 09:59:16 dd Exp $
- * $DragonFly: src/usr.bin/lsvfs/lsvfs.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
  */
 
 #define _NEW_VFSCONF
@@ -16,9 +15,22 @@
 #include <stdio.h>
 #include <string.h>
 
-#define FMT "%-32.32s %5d %s\n"
-#define HDRFMT "%-32.32s %5.5s %s\n"
-#define DASHES "-------------------------------- ----- ---------------\n"
+#define	FMT	"%-32.32s 0x%08x %5d  %s\n"
+#define	HDRFMT	"%-32.32s %10s %5.5s  %s\n"
+#define	DASHES	"-------------------------------- "	\
+		"---------- -----  ---------------\n"
+
+static struct flaglist {
+	int		flag;
+	const char	str[32]; /* must be longer than the longest one. */
+} fl[] = {
+	{ .flag = VFCF_STATIC, .str = "static", },
+	{ .flag = VFCF_NETWORK, .str = "network", },
+	{ .flag = VFCF_READONLY, .str = "read-only", },
+	{ .flag = VFCF_SYNTHETIC, .str = "synthetic", },
+	{ .flag = VFCF_LOOPBACK, .str = "loopback", },
+	{ .flag = VFCF_UNICODE, .str = "unicode", },
+};
 
 static const char *fmt_flags(int);
 
@@ -32,13 +44,14 @@ main(int argc, char **argv)
 
   setvfsent(1);
 
-  printf(HDRFMT, "Filesystem", "Refs", "Flags");
+  printf(HDRFMT, "Filesystem", "Num", "Refs", "Flags");
   fputs(DASHES, stdout);
 
   if(argc) {
     for(; argc; argc--, argv++) {
       if (getvfsbyname(*argv, &vfc) == 0) {
-        printf(FMT, vfc.vfc_name, vfc.vfc_refcount, fmt_flags(vfc.vfc_flags));
+        printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
+	    fmt_flags(vfc.vfc_flags));
       } else {
 	warnx("VFS %s unknown or not loaded", *argv);
         rv++;
@@ -46,8 +59,13 @@ main(int argc, char **argv)
     }
   } else {
     while ((ovfcp = getvfsent()) != NULL) {
-      printf(FMT, ovfcp->vfc_name, ovfcp->vfc_refcount,
-             fmt_flags(ovfcp->vfc_flags));
+      if (getvfsbyname(ovfcp->vfc_name, &vfc) == 0) {
+        printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
+	    fmt_flags(vfc.vfc_flags));
+      } else {
+	warnx("VFS %s unknown or not loaded", *argv);
+        rv++;
+      }
     }
   }
 
@@ -58,44 +76,16 @@ main(int argc, char **argv)
 static const char *
 fmt_flags(int flags)
 {
-  /*
-   * NB: if you add new flags, don't forget to add them here vvvvvv too.
-   */
-  static char buf[sizeof
-    "static, network, read-only, synthetic, loopback, unicode"];
-  int comma = 0;
-
-  buf[0] = '\0';
-
-  if(flags & VFCF_STATIC) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "static");
-  }
-
-  if(flags & VFCF_NETWORK) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "network");
-  }
-
-  if(flags & VFCF_READONLY) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "read-only");
-  }
-
-  if(flags & VFCF_SYNTHETIC) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "synthetic");
-  }
-
-  if(flags & VFCF_LOOPBACK) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "loopback");
-  }
-
-  if(flags & VFCF_UNICODE) {
-    if(comma++) strcat(buf, ", ");
-    strcat(buf, "unicode");
-  }
-
-  return buf;
+	static char buf[sizeof(struct flaglist) * sizeof(fl)];
+	int i;
+
+	buf[0] = '\0';
+	for (i = 0; i < (int)nitems(fl); i++)
+		if (flags & fl[i].flag) {
+			strlcat(buf, fl[i].str, sizeof(buf));
+			strlcat(buf, ", ", sizeof(buf));
+		}
+	if (buf[0] != '\0')
+		buf[strlen(buf) - 2] = '\0';
+	return (buf);
 }
-- 
2.16.2

