tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Adding vis-style encoding to stat(1)



stat(1) does not have a way to request encoding of unsafe
characters in user names, group names, or file names.

I propose to make the "#" modified request vis(3)-style
encoding for strings.  See the appended patch.

--apb (Alan Barrett)

Index: usr.bin/stat/stat.1
===================================================================
--- usr.bin/stat/stat.1 16 Sep 2011 14:32:53 -0000      1.33
+++ usr.bin/stat/stat.1 16 Sep 2011 14:37:34 -0000
@@ -202,9 +202,12 @@ examined for the following:
 Any of the following optional flags:
 .Bl -tag -width Ds
 .It Cm #
-Selects an alternate output form for octal and hexadecimal output.
-Non-zero octal output will have a leading zero, and non-zero
-hexadecimal output will have
+Selects an alternate output form for string, octal and hexadecimal output.
+String output will be encoded in
+.Xr vis 3
+style.
+Non-zero octal output will have a leading zero.
+Non-zero hexadecimal output will have
 .Dq 0x
 prepended to it.
 .It Cm +
@@ -582,6 +585,18 @@ Apr 25 11:47:00 2002 /tmp/blah
 Apr 25 10:36:34 2002 /tmp/bar
 Apr 24 16:47:35 2002 /tmp/foo
 .Ed
+.Pp
+User names, group names, and file names that contain spaces
+or other special characters may be encoded in
+.Xr vis 3
+style, using the
+.Cm \&#
+modifier:
+.Bd -literal -offset indent
+\*[Gt] ln -s 'target with spaces' 'link with spaces'
+\*[Gt] stat -f "%#N%#SY" 'link with spaces'
+link\eswith\esspaces -> target\eswith\esspaces
+.Ed
 .Sh SEE ALSO
 .Xr basename 1 ,
 .Xr dirname 1 ,
Index: usr.bin/stat/stat.c
===================================================================
--- usr.bin/stat/stat.c 6 Sep 2011 18:31:22 -0000       1.35
+++ usr.bin/stat/stat.c 16 Sep 2011 14:37:34 -0000
@@ -61,6 +61,7 @@ __RCSID("$NetBSD: stat.c,v 1.35 2011/09/
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <vis.h>
#if HAVE_STRUCT_STAT_ST_FLAGS
 #define DEF_F "%#Xf "
@@ -365,7 +366,12 @@ output(const struct stat *st, const char
     const char *statfmt, int fn, int nonl, int quiet)
 {
        int flags, size, prec, ofmt, hilo, what;
-       char buf[PATH_MAX + 4 + 1];
+       /*
+        * buf size is enough for an item of length PATH_MAX,
+        * multiplied by 4 for vis encoding, plus 4 for symlink
+        * " -> " prefix, plus 1 for \0 terminator.
+        */
+       char buf[PATH_MAX * 4 + 4 + 1];
        const char *subfmt;
        int nl, t, i;
@@ -436,6 +442,7 @@ output(const struct stat *st, const char
                 * the leading " -> " if STRING is explicitly specified.  The
                 * sizerdev datum will generate rdev output for character or
                 * block devices, and size output for all others.
+                * For STRING output, the # format requests vis encoding.
                 */
                flags = 0;
                do {
@@ -564,13 +571,18 @@ format1(const struct stat *st,
        u_int64_t data;
        char *stmp, lfmt[24], tmp[20];
        const char *sdata;
-       char smode[12], sid[12], path[PATH_MAX + 4];
+       char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
        struct passwd *pw;
        struct group *gr;
        struct tm *tm;
        time_t secs;
        long nsecs;
-       int l, small, formats, gottime, shift;
+       int l;
+       int formats;    /* bitmap of allowed formats for this data */
+       int small;      /* true if datum is a small integer */
+       int gottime;    /* true if secs and nsecs are valid */
+       int shift;      /* powers of 2 to scale numbers before printing */
+       size_t prefixlen; /* length of constant prefix for string data */
formats = 0;
        small = 0;
@@ -578,6 +590,7 @@ format1(const struct stat *st,
        secs = 0;
        nsecs = 0;
        shift = 0;
+       prefixlen = 0;
/*
         * First, pick out the data and tweak it based on hilo or
@@ -814,6 +827,7 @@ format1(const struct stat *st,
                                path[0] = '\0';
                        }
                        sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
+                       prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
                }
formats = FMTF_STRING;
@@ -835,6 +849,7 @@ format1(const struct stat *st,
                        }
                        path[l + 4] = '\0';
                        sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
+                       prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
                }
                else {
                        linkfail = 1;
@@ -978,6 +993,18 @@ format1(const struct stat *st,
                errx(1, "%.*s: bad format", (int)flen, fmt);
/*
+        * FLAG_POUND with FMTF_STRING means use vis(3) encoding.
+        * First prefixlen chars are not encoded.
+        */
+       if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) {
+               flags &= !FLAG_POUND;
+               strncpy(visbuf, sdata, prefixlen);
+               strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen,
+                   sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE);
+               sdata = visbuf;
+       }
+
+       /*
         * Assemble the format string for passing to printf(3).
         */
        lfmt[0] = '\0';


Home | Main Index | Thread Index | Old Index