Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Jun 2008 11:57:29 GMT
From:      Ighighi <ighighi@gmail.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   bin/124349: [patch]: stat(1) should interpret st_flags as it does with st_mode
Message-ID:  <200806061157.m56BvTCe054695@www.freebsd.org>
Resent-Message-ID: <200806061200.m56C0Bvx014729@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         124349
>Category:       bin
>Synopsis:       [patch]: stat(1) should interpret st_flags as it does with st_mode
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jun 06 12:00:11 UTC 2008
>Closed-Date:
>Last-Modified:
>Originator:     Ighighi
>Release:        6.3-STABLE (RELENG_6)
>Organization:
>Environment:
FreeBSD orion 6.3-STABLE FreeBSD 6.3-STABLE #0: Wed May 28 17:18:08 VET 2008
>Description:
Currently, the only output for st_flags in stat(1) is numeric.
I suggest adding support for string output as it's done with
st_mode, st_uid, st_gid, etc, so instead of just:

$ /usr/bin/stat -f '%f %N' /usr/bin/su
131072 /usr/bin/su

we may also see (without the need to use "ls -ldo"):

$ ./stat -f '%Sf %N' /usr/bin/su
schg /usr/bin/su


Quoting stat(1):

     The special output specifier S may be used to indicate that the
     output, if applicable, should be in string format.  May be used
     in combination with:

     [...]

     gu      Display group or user name.

     p       Display the mode of file as in ls -lTd.


I also patched the manpage to add this line:

     f       Display the flags of file as in ls -lTdo.

>How-To-Repeat:

>Fix:
Available patch built and tested on 6.3-STABLE and -CURRENT

Patch attached with submission follows:

--- src/usr.bin/stat/stat.1.orig	2007-04-27 14:23:32.000000000 -0400
+++ src/usr.bin/stat/stat.1	2008-06-06 07:02:14.415211295 -0430
@@ -239,6 +239,11 @@ Display date in
 format.
 .It Cm dr
 Display actual device name.
+.It Cm f
+Display the flags of
+.Ar file
+as in
+.Nm ls Fl lTdo .
 .It Cm gu
 Display group or user name.
 .It Cm p
--- src/usr.bin/stat/stat.c.orig	2003-10-05 21:55:17.000000000 -0400
+++ src/usr.bin/stat/stat.c	2008-06-06 07:07:07.202401287 -0430
@@ -187,6 +187,7 @@ int	format1(const struct stat *,	/* stat
 	    char *, size_t,		/* a place to put the output */
 	    int, int, int, int,		/* the parsed format */
 	    int, int);
+char   *xfflagstostr(unsigned long);
 
 char *timefmt;
 int linkfail;
@@ -329,6 +330,21 @@ main(int argc, char *argv[])
 	return (am_readlink ? linkfail : errs);
 }
 
+/*
+ * fflagstostr() wrapper that leaks only once
+ */
+char *
+xfflagstostr(unsigned long fflags)
+{
+	static char *str = NULL;
+
+	if (str != NULL)
+		free(str);
+
+	str = fflagstostr(fflags);
+	return (str);
+}
+
 void
 usage(const char *synopsis)
 {
@@ -721,8 +737,9 @@ format1(const struct stat *st,
 	case SHOW_st_flags:
 		small = (sizeof(st->st_flags) == 4);
 		data = st->st_flags;
-		sdata = NULL;
-		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
+		sdata = xfflagstostr(st->st_flags);
+		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
+		    FMTF_STRING;
 		if (ofmt == 0)
 			ofmt = FMTF_UNSIGNED;
 		break;
@@ -979,7 +996,7 @@ format1(const struct stat *st,
 	(void)strcat(lfmt, "ll");
 	switch (ofmt) {
 	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
-	case FMTF_OCTAL:		(void)strcat(lfmt, "o");	break;
+	case FMTF_OCTAL:	(void)strcat(lfmt, "o");	break;
 	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
 	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
 	}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200806061157.m56BvTCe054695>