From 5a221e6b84e9eab02e24ca541530e28eb7cfddac Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Mon, 22 Apr 2013 22:29:43 +0200 Subject: Add library function for the file permission formatting in ls and stat --- lib/lib.c | 26 ++++++++++++++++++++++++++ lib/lib.h | 1 + toys/pending/stat.c | 23 ++--------------------- toys/posix/ls.c | 23 ++--------------------- 4 files changed, 31 insertions(+), 42 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 4d8e23a4..b89f2db8 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -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< 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<st_mtime)); strftime(thyme, sizeof(thyme), "%F %H:%M", tm); -- cgit v1.2.3