diff options
author | Felix Janda <felix.janda@posteo.de> | 2013-04-22 22:29:43 +0200 |
---|---|---|
committer | Felix Janda <felix.janda@posteo.de> | 2013-04-22 22:29:43 +0200 |
commit | 5a221e6b84e9eab02e24ca541530e28eb7cfddac (patch) | |
tree | ffa01832e66ea605e40c5e6c730b22b6088b121d /lib/lib.c | |
parent | b4a86ac3ab6a248b5345557a4c9f1efba7695246 (diff) | |
download | toybox-5a221e6b84e9eab02e24ca541530e28eb7cfddac.tar.gz |
Add library function for the file permission formatting in ls and stat
Diffstat (limited to 'lib/lib.c')
-rw-r--r-- | lib/lib.c | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -1176,6 +1176,32 @@ barf: error_exit("bad mode '%s'", modestr); } +// Format a mode for ls and stat +void format_mode(char (*buf)[11], mode_t mode) +{ + char c, d; + int i, bit; + + (*buf)[10]=0; + for (i=0; i<9; i++) { + bit = mode & (1<<i); + c = i%3; + if (!c && (mode & (1<<((d=i/3)+9)))) { + c = "tss"[d]; + if (!bit) c &= ~0x20; + } else c = bit ? "xwr"[c] : '-'; + (*buf)[9-i] = c; + } + + if (S_ISDIR(mode)) c = 'd'; + else if (S_ISBLK(mode)) c = 'b'; + else if (S_ISCHR(mode)) c = 'c'; + else if (S_ISLNK(mode)) c = 'l'; + else if (S_ISFIFO(mode)) c = 'p'; + else if (S_ISSOCK(mode)) c = 's'; + else c = '-'; + **buf = c; +} char* make_human_readable(unsigned long long size, unsigned long unit) { |