diff options
-rw-r--r-- | lib/lib.c | 26 | ||||
-rw-r--r-- | lib/lib.h | 1 | ||||
-rw-r--r-- | toys/pending/stat.c | 23 | ||||
-rw-r--r-- | toys/posix/ls.c | 23 |
4 files changed, 31 insertions, 42 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) { @@ -178,6 +178,7 @@ int sig_to_num(char *pidstr); char *num_to_sig(int sig); mode_t string_to_mode(char *mode_str, mode_t base); +void format_mode(char (*buf)[11], mode_t mode); // password helper functions int read_password(char * buff, int buflen, char* mesg); diff --git a/toys/pending/stat.c b/toys/pending/stat.c index 4ce2548b..9710acff 100644 --- a/toys/pending/stat.c +++ b/toys/pending/stat.c @@ -50,7 +50,7 @@ config STAT GLOBALS( char *fmt; - char *access_str; + char access_str[11]; char *file_type; struct passwd *user_name; struct group *group_name; @@ -86,25 +86,6 @@ static char * check_type_file(mode_t mode, size_t size) if (S_ISSOCK(mode)) return "socket"; } -static char * get_access_str(unsigned long permission, mode_t mode) -{ - static char access_string[11]; - char *s = access_string; - char *rwx[] = {"---", "--x", "-w-", "-wx", - "r--", "r-x", "rw-", "rwx"}; - - if (S_ISDIR(mode)) *s = 'd'; - else *s = '-'; - - for (s += 7; s > access_string; s-=3) { - memcpy(s, rwx[permission & 7], 3); - permission >>= 3; - } - - access_string[10] = '\0'; - return access_string; -} - static char * date_stat_format(time_t time) { static char buf[36]; @@ -252,7 +233,7 @@ void stat_main(void) TT.user_name = getpwuid(TT.toystat->st_uid); TT.group_name = getgrgid(TT.toystat->st_gid); // function to get access in human readable format - TT.access_str = get_access_str(TT.toystat->st_mode & ~S_IFMT, TT.toystat->st_mode); + format_mode(&TT.access_str, TT.toystat->st_mode); } else do_statfs(*toys.optargs); print_stat_format(fmts[!flag_c*flag_f+flag_c], flag_f); } diff --git a/toys/posix/ls.c b/toys/posix/ls.c index 5982adfe..76fb7947 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -335,28 +335,9 @@ static void listfiles(int dirfd, struct dirtree *indir) if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) { struct tm *tm; - char perm[11], thyme[64], c, d, *usr, *upad, *grp, *grpad; - int i, bit; - - perm[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] : '-'; - perm[9-i] = c; - } + char perm[11], thyme[64], *usr, *upad, *grp, *grpad; - 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 = '-'; - *perm = c; + format_mode(&perm, mode); tm = localtime(&(st->st_mtime)); strftime(thyme, sizeof(thyme), "%F %H:%M", tm); |