From 78f92746c46ef1c700d438f1a66bacc7f61bd47c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 13 Feb 2017 16:19:55 -0600 Subject: [PATCH] apps/examples/stat: Improve decoding of file mode. --- examples/stat/stat_main.c | 57 +++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/examples/stat/stat_main.c b/examples/stat/stat_main.c index 2e6110a10..8bc2c745e 100644 --- a/examples/stat/stat_main.c +++ b/examples/stat/stat_main.c @@ -109,48 +109,71 @@ static void endusage(void) static void dump_stat(FAR struct stat *buf) { - FAR const char *typename; + char details[] = "----------"; if (S_ISLNK(buf->st_mode)) { - typename = "Link"; + details[0] = 'l'; /* Takes precedence over type of the target */ } else if (S_ISCHR(buf->st_mode)) { - typename = "Character driver"; + details[0] = 'c'; } else if (S_ISDIR(buf->st_mode)) { - typename = "Directory"; + details[0] = 'd'; } else if (S_ISBLK(buf->st_mode)) { - typename = "Block driver"; + details[0] = 'b'; } - else if (S_ISREG(buf->st_mode)) + else if (!S_ISREG(buf->st_mode)) { - typename = "Regular file"; + details[0] = '?'; } - else if (S_ISMQ(buf->st_mode)) + + if ((buf->st_mode & S_IRUSR) != 0) { - typename = "Message queue"; + details[1]='r'; } - else if (S_ISSEM(buf->st_mode)) + + if ((buf->st_mode & S_IWUSR) != 0) { - typename = "Named semaphore"; + details[2]='w'; } - else if (S_ISSHM(buf->st_mode)) + + if ((buf->st_mode & S_IXUSR) != 0) { - typename = "Shared memory"; + details[3]='x'; } - else + + if ((buf->st_mode & S_IRGRP) != 0) { - typename = "Unknown file type"; + details[4]='r'; + } + + if ((buf->st_mode & S_IWGRP) != 0) + { + details[5]='w'; + } + + if ((buf->st_mode & S_IXGRP) != 0) + { + details[6]='x'; + } + + if ((buf->st_mode & S_IROTH) != 0) + { + details[7]='r'; + } + + if ((buf->st_mode & S_IWOTH) != 0) + { + details[8]='w'; } printf("\nstat:\n"); - printf(" st_mode: %04x\n", buf->st_mode); - printf(" %s\n", typename); + printf(" st_mode: %04x %s\n", buf->st_mode, details); printf(" st_size: %llu\n", (unsigned long long)buf->st_size); printf(" st_blksize: %lu\n", (unsigned long)buf->st_blksize); printf(" st_blocks: %lu\n", (unsigned long)buf->st_blocks);