Mercurial > hg > toybox
comparison toys/pending/stat.c @ 917:b2697351ce6d
Stat cleanup.
Move ftname out of GLOBALS into 'F' handler. Make 'i' zero pad output (zeroes
in middle of ID can get lost).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 02 Jun 2013 00:52:14 -0500 |
parents | b92cb3cc9696 |
children |
comparison
equal
deleted
inserted
replaced
916:b92cb3cc9696 | 917:b2697351ce6d |
---|---|
1 /* stat.c : display file or file system status | 1 /* stat.c : display file or file system status |
2 * anand.sinha85@gmail.com | |
3 * Copyright 2012 <warior.linux@gmail.com> | 2 * Copyright 2012 <warior.linux@gmail.com> |
3 * Copyright 2013 <anand.sinha85@gmail.com> | |
4 | 4 |
5 USE_STAT(NEWTOY(stat, "c:f", TOYFLAG_BIN)) | 5 USE_STAT(NEWTOY(stat, "c:f", TOYFLAG_BIN)) |
6 | 6 |
7 config STAT | 7 config STAT |
8 bool stat | 8 bool stat |
42 struct stat st; | 42 struct stat st; |
43 struct statfs sf; | 43 struct statfs sf; |
44 } stat; | 44 } stat; |
45 struct passwd *user_name; | 45 struct passwd *user_name; |
46 struct group *group_name; | 46 struct group *group_name; |
47 char *ftname; | |
48 ) | 47 ) |
49 | 48 |
50 | 49 |
51 // Note: the atime, mtime, and ctime fields in struct stat are the start | 50 // Note: the atime, mtime, and ctime fields in struct stat are the start |
52 // of embedded struct timespec, but posix won't let them use that | 51 // of embedded struct timespec, but posix won't let them use that |
72 } else if (type == 'b') xprintf("%llu", stat->st_blocks); | 71 } else if (type == 'b') xprintf("%llu", stat->st_blocks); |
73 else if (type == 'B') xprintf("%lu", stat->st_blksize); | 72 else if (type == 'B') xprintf("%lu", stat->st_blksize); |
74 else if (type == 'd') xprintf("%ldd", stat->st_dev); | 73 else if (type == 'd') xprintf("%ldd", stat->st_dev); |
75 else if (type == 'D') xprintf("%llxh", stat->st_dev); | 74 else if (type == 'D') xprintf("%llxh", stat->st_dev); |
76 else if (type == 'f') xprintf("%lx", stat->st_mode); | 75 else if (type == 'f') xprintf("%lx", stat->st_mode); |
77 else if (type == 'F') xprintf("%s", TT.ftname); | 76 else if (type == 'F') { |
78 else if (type == 'g') xprintf("%lu", stat->st_gid); | 77 char *t = "character device\0directory\0block device\0" \ |
78 "regular file\0symbolic link\0socket\0FIFO (named pipe)"; | |
79 int i, filetype = stat->st_mode & S_IFMT; | |
80 | |
81 for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1; | |
82 if (!stat->st_size && filetype == S_IFREG) t = "regular empty file"; | |
83 xprintf("%s", t); | |
84 } else if (type == 'g') xprintf("%lu", stat->st_gid); | |
79 else if (type == 'G') xprintf("%8s", TT.user_name->pw_name); | 85 else if (type == 'G') xprintf("%8s", TT.user_name->pw_name); |
80 else if (type == 'h') xprintf("%lu", stat->st_nlink); | 86 else if (type == 'h') xprintf("%lu", stat->st_nlink); |
81 else if (type == 'i') xprintf("%llu", stat->st_ino); | 87 else if (type == 'i') xprintf("%llu", stat->st_ino); |
82 else if (type == 'N') { | 88 else if (type == 'N') { |
83 xprintf("`%s'", *toys.optargs); | 89 xprintf("`%s'", *toys.optargs); |
106 else if (type == 'd') xprintf("%lu", statfs->f_ffree); | 112 else if (type == 'd') xprintf("%lu", statfs->f_ffree); |
107 else if (type == 'f') xprintf("%lu", statfs->f_bfree); | 113 else if (type == 'f') xprintf("%lu", statfs->f_bfree); |
108 else if (type == 'l') xprintf("%ld", statfs->f_namelen); | 114 else if (type == 'l') xprintf("%ld", statfs->f_namelen); |
109 else if (type == 't') xprintf("%lx", statfs->f_type); | 115 else if (type == 't') xprintf("%lx", statfs->f_type); |
110 else if (type == 'i') | 116 else if (type == 'i') |
111 xprintf("%x%x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]); | 117 xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]); |
112 else if (type == 's') xprintf("%d", statfs->f_frsize); | 118 else if (type == 's') xprintf("%d", statfs->f_frsize); |
113 else if (type == 'S') xprintf("%d", statfs->f_bsize); | 119 else if (type == 'S') xprintf("%d", statfs->f_bsize); |
114 else xprintf("?"); | 120 else xprintf("?"); |
115 } | 121 } |
116 | 122 |
133 char *f; | 139 char *f; |
134 | 140 |
135 if (flagf && !statfs(*toys.optargs, (void *)&TT.stat)); | 141 if (flagf && !statfs(*toys.optargs, (void *)&TT.stat)); |
136 else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) { | 142 else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) { |
137 struct stat *stat = (struct stat*)&TT.stat; | 143 struct stat *stat = (struct stat*)&TT.stat; |
138 char *types = "character device\0directory\0block device\0" \ | |
139 "regular file\0symbolic link\0socket\0FIFO (named pipe)"; | |
140 int i, filetype; | |
141 | |
142 filetype = stat->st_mode & S_IFMT; | |
143 TT.ftname = types; | |
144 for (i = 1; filetype != (i*8192) && i < 7; i++) | |
145 TT.ftname += strlen(TT.ftname)+1; | |
146 if (!stat->st_size && filetype == S_IFREG) | |
147 TT.ftname = "regular empty file"; | |
148 | 144 |
149 // check user and group name | 145 // check user and group name |
150 TT.user_name = getpwuid(stat->st_uid); | 146 TT.user_name = getpwuid(stat->st_uid); |
151 TT.group_name = getgrgid(stat->st_gid); | 147 TT.group_name = getgrgid(stat->st_gid); |
152 } else { | 148 } else { |