On Sat, Nov 22, 2014 at 02:00:39PM -0900, Ken Irving wrote:
> I'd also be interested in having stat(1) -c or --printf formats more
> suited to simple parsing in scripts.  %N is effectively a 'pretty print'
> form, fairly complicated to parse, and the same might be said for %F
> since it uses multiple words for some types.  If new format patterns
> might be considered, I concur with the above suggestion for one that
> produces just the unquoted link value (as readlink(1) would provide);
> and a terse type-showing pattern, e.g., maybe using one-letter values
> like find(1) -type uses.

I've attached a draft patch adding format pattern %p to stat(1) to output
one-letter codes for file type, as used by find(1) -type.  The choice of
%p is arbitrary, but it seems to be available, and could perhaps suggest
'tyPe' as a mnemonic.

    ken@kimball:~/coreutils-hacks/coreutils/
    $ mkfifo fifo
    $ src/stat --printf "%12n%24F\t%p\n" configure src INSTALL fifo 
/dev/{ram0,tty,log}
       configure            regular file    f
             src               directory    d
         INSTALL           symbolic link    l
            fifo                    fifo    p
       /dev/ram0      block special file    b
        /dev/tty  character special file    c
        /dev/log                  socket    s

Ken
>From c38fabab3117ba8bac792012fd99cf368e076cd7 Mon Sep 17 00:00:00 2001
From: Ken Irving <[email protected]>
Date: Sun, 23 Nov 2014 21:58:05 -0900
Subject: [PATCH] stat: added %p format for one-letter types

---
 doc/coreutils.texi |  1 +
 src/stat.c         | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 9c731b5..64c9ee8 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -11928,6 +11928,7 @@ The valid @var{format} directives for files with 
@option{--format} and
 @item %n - File name
 @item %N - Quoted file name with dereference if symbolic link
 @item %o - Optimal I/O transfer size hint
+@item %p - File type as one-letter code (see find(1) -type)
 @item %s - Total size, in bytes
 @item %t - Major device type in hex (see below)
 @item %T - Minor device type in hex (see below)
diff --git a/src/stat.c b/src/stat.c
index 987f579..e4f796a 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -1024,6 +1024,24 @@ print_stat (char *pformat, size_t prefix_len, unsigned 
int m,
     case 'F':
       out_string (pformat, prefix_len, file_type (statbuf));
       break;
+    case 'p':
+      if (S_ISREG (statbuf->st_mode))
+        out_string (pformat, prefix_len,"f");
+      else if (S_ISDIR (statbuf->st_mode))
+        out_string (pformat, prefix_len,"d");
+      else if (S_ISLNK (statbuf->st_mode))
+        out_string (pformat, prefix_len,"l");
+      else if (S_ISFIFO (statbuf->st_mode))
+        out_string (pformat, prefix_len,"p");
+      else if (S_ISCHR (statbuf->st_mode))
+        out_string (pformat, prefix_len,"c");
+      else if (S_ISBLK (statbuf->st_mode))
+        out_string (pformat, prefix_len,"b");
+      else if (S_ISSOCK (statbuf->st_mode))
+        out_string (pformat, prefix_len,"s");
+      else
+        out_string (pformat, prefix_len,"?");
+      break;
     case 'h':
       out_uint (pformat, prefix_len, statbuf->st_nlink);
       break;
@@ -1458,6 +1476,7 @@ The valid format sequences for files (without 
--file-system):\n\
   %D   device number in hex\n\
   %f   raw mode in hex\n\
   %F   file type\n\
+  %p   one-letter file type\n\
   %g   group ID of owner\n\
   %G   group name of owner\n\
 "), stdout);
-- 
1.9.1

Reply via email to