We'd documented find %Z but not implemented it. We'd neither documented
nor implemented stat's corresponding %C (they'd already taken %Z for
ctime, which is ironic because %c/%C sounds more obvious than %z/%Z for
that to me).
---
 toys/other/stat.c | 24 +++++++++++++++---------
 toys/posix/find.c |  7 ++++++-
 2 files changed, 21 insertions(+), 10 deletions(-)
From 316cb601a39d76ea85e7484b04a0867a3f50f40d Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 6 Jun 2019 15:29:29 -0700
Subject: [PATCH] Implement stat %C and find %Z.

We'd documented find %Z but not implemented it. We'd neither documented
nor implemented stat's corresponding %C (they'd already taken %Z for
ctime, which is ironic because %c/%C sounds more obvious than %z/%Z for
that to me).
---
 toys/other/stat.c | 24 +++++++++++++++---------
 toys/posix/find.c |  7 ++++++-
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/toys/other/stat.c b/toys/other/stat.c
index 3f1d1765..054e2ee7 100644
--- a/toys/other/stat.c
+++ b/toys/other/stat.c
@@ -20,14 +20,15 @@ config STAT
 
     The valid format escape sequences for files:
     %a  Access bits (octal) |%A  Access bits (flags)|%b  Size/512
-    %B  Bytes per %b (512)  |%d  Device ID (dec)    |%D  Device ID (hex)
-    %f  All mode bits (hex) |%F  File type          |%g  Group ID
-    %G  Group name          |%h  Hard links         |%i  Inode
-    %m  Mount point         |%n  Filename           |%N  Long filename
-    %o  I/O block size      |%s  Size (bytes)       |%t  Devtype major (hex)
-    %T  Devtype minor (hex) |%u  User ID            |%U  User name
-    %x  Access time         |%X  Access unix time   |%y  Modification time
-    %Y  Mod unix time       |%z  Creation time      |%Z  Creation unix time
+    %B  Bytes per %b (512)  |%C  Security context   |%d  Device ID (dec)
+    %D  Device ID (hex)     |%f  All mode bits (hex)|%F  File type
+    %g  Group ID            |%G  Group name         |%h  Hard links
+    %i  Inode               |%m  Mount point        |%n  Filename
+    %N  Long filename       |%o  I/O block size     |%s  Size (bytes)
+    %t  Devtype major (hex) |%T  Devtype minor (hex)|%u  User ID
+    %U  User name           |%x  Access time        |%X  Access unix time
+    %y  Modification time   |%Y  Mod unix time      |%z  Creation time
+    %Z  Creation unix time
 
     The valid format escape sequences for filesystems:
     %a  Available blocks    |%b  Total blocks       |%c  Total inodes
@@ -82,7 +83,12 @@ static void print_stat(char type)
     strout(str);
   } else if (type == 'b') out('u', stat->st_blocks);
   else if (type == 'B') out('d', 512);
-  else if (type == 'd') out('d', stat->st_dev);
+  else if (type == 'C') {
+    char *context = NULL;
+
+    strout(lsm_get_context(TT.file, &context) != -1 ? context : "?");
+    free(context);
+  } else if (type == 'd') out('d', stat->st_dev);
   else if (type == 'D') out('x', stat->st_dev);
   else if (type == 'f') out('x', stat->st_mode);
   else if (type == 'F') {
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 3f24c0dc..fe712c88 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -53,7 +53,7 @@ config FIND
     or "+" (next argument after "{}") to collect and run with multiple files.
 
     -printf FORMAT characters are \ escapes and:
-    %b 512 byte blocks used
+    %b  512 byte blocks used
     %f  basename            %g  textual gid          %G  numeric gid
     %i  decimal inode       %l  target of symlink    %m  octal mode
     %M  ls format type/mode %p  path to file         %P  path to file minus DIR
@@ -600,6 +600,11 @@ static int do_find(struct dirtree *new)
                 sprintf(buf, "%ld.%ld", new->st.st_mtim.tv_sec,
                              new->st.st_mtim.tv_nsec);
                 ll = (long)buf;
+              } else if (ch == 'Z') {
+                char *path = dirtree_path(new, 0);
+
+                ll = (lsm_get_context(path, &ff) != -1) ? (long)ff : (long)"?";
+                free(path);
               } else error_exit("bad -printf %%%c", ch);
 
               printf(next, ll);
-- 
2.22.0.rc1.311.g5d7573a151-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to