Proposed extension to stat(1)

Thomas Quinot thomas at FreeBSD.ORG
Thu May 8 09:19:08 UTC 2014


* Gleb Kurtsou, 2014-05-08 :

Thanks for the thorough review, Gleb! Much appreciated.

> - why do we care about strlen(file) != sizeof(fh) case?

tcpdump(1) may display handles with additional trailing bytes that are
irrelevant to fhstat(2). It would be inconvenient to require the user to
know about the exact length at which the information needs to be
truncated. It is more helpful to silently discard the excess data.

Attached is a diff vs the committed version (r265591) which I think
addresses all of your remarks. Is there anything else that you
think would need fixing?

Thomas.

-------------- next part --------------
Index: stat.1
===================================================================
--- stat.1	(r?vision 265665)
+++ stat.1	(copie de travail)
@@ -130,6 +130,7 @@
 .Xr fhstat 2
 instead of
 .Xr lstat 2 .
+This requires root privileges.
 .It Fl L
 Use
 .Xr stat 2
Index: stat.c
===================================================================
--- stat.c	(r?vision 265665)
+++ stat.c	(copie de travail)
@@ -186,6 +186,7 @@
 	    char *, size_t,		/* a place to put the output */
 	    int, int, int, int,		/* the parsed format */
 	    int, int);
+int	hex2byte(const char [2]);
 #if HAVE_STRUCT_STAT_ST_FLAGS
 char   *xfflagstostr(unsigned long);
 #endif
@@ -214,7 +215,7 @@
 	lsF = 0;
 	fmtchar = '\0';
 	usestat = 0;
-        nfs_handle = 0;
+	nfs_handle = 0;
 	nonl = 0;
 	quiet = 0;
 	linkfail = 0;
@@ -327,32 +328,27 @@
 			rc = fstat(STDIN_FILENO, &st);
 		} else {
 			int j;
-			char *inval;
 
 			file = argv[0];
 			if (nfs_handle) {
 				rc = 0;
-				bzero (&fhnd, sizeof fhnd);
-				j = MIN(2 * sizeof fhnd, strlen(file));
-				if (j & 1) {
+				bzero(&fhnd, sizeof(fhnd));
+				j = MIN(2 * sizeof(fhnd), strlen(file));
+				if ((j & 1) != 0) {
 					rc = -1;
 				} else {
 					while (j) {
-						((char*) &fhnd)[j / 2 - 1] =
-						    strtol(&file[j - 2],
-						           &inval, 16);
-						if (inval != NULL) {
-							rc = -1;
+						rc = hex2byte(&file[j - 2]);
+						if (rc == -1)
 							break;
-						}
-						argv[0][j - 2] = '\0';
+						((char*) &fhnd)[j / 2 - 1] = rc;
 						j -= 2;
 					}
-					if (!rc)
-						rc = fhstat(&fhnd, &st);
-					else
-						errno = EINVAL;
 				}
+				if (rc == -1)
+					errno = EINVAL;
+				else
+					rc = fhstat(&fhnd, &st);
 
 			} else if (usestat) {
 				/*
@@ -1091,3 +1087,12 @@
 
 	return (snprintf(buf, blen, lfmt, data));
 }
+
+
+#define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
+int
+hex2byte(const char c[2]) {
+	if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
+		return -1;
+	return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20140508/8195d66d/attachment.sig>


More information about the freebsd-arch mailing list