Mercurial > hg > toybox
comparison lib/lib.c @ 885:beb32d780164
Add library function for the file permission formatting in ls and stat
author | Felix Janda <felix.janda@posteo.de> |
---|---|
date | Mon, 22 Apr 2013 22:29:43 +0200 |
parents | aca8323e2690 |
children | 270e5aab9998 |
comparison
equal
deleted
inserted
replaced
884:ac9991f66d0d | 885:beb32d780164 |
---|---|
1174 return mode; | 1174 return mode; |
1175 barf: | 1175 barf: |
1176 error_exit("bad mode '%s'", modestr); | 1176 error_exit("bad mode '%s'", modestr); |
1177 } | 1177 } |
1178 | 1178 |
1179 // Format a mode for ls and stat | |
1180 void format_mode(char (*buf)[11], mode_t mode) | |
1181 { | |
1182 char c, d; | |
1183 int i, bit; | |
1184 | |
1185 (*buf)[10]=0; | |
1186 for (i=0; i<9; i++) { | |
1187 bit = mode & (1<<i); | |
1188 c = i%3; | |
1189 if (!c && (mode & (1<<((d=i/3)+9)))) { | |
1190 c = "tss"[d]; | |
1191 if (!bit) c &= ~0x20; | |
1192 } else c = bit ? "xwr"[c] : '-'; | |
1193 (*buf)[9-i] = c; | |
1194 } | |
1195 | |
1196 if (S_ISDIR(mode)) c = 'd'; | |
1197 else if (S_ISBLK(mode)) c = 'b'; | |
1198 else if (S_ISCHR(mode)) c = 'c'; | |
1199 else if (S_ISLNK(mode)) c = 'l'; | |
1200 else if (S_ISFIFO(mode)) c = 'p'; | |
1201 else if (S_ISSOCK(mode)) c = 's'; | |
1202 else c = '-'; | |
1203 **buf = c; | |
1204 } | |
1179 | 1205 |
1180 char* make_human_readable(unsigned long long size, unsigned long unit) | 1206 char* make_human_readable(unsigned long long size, unsigned long unit) |
1181 { | 1207 { |
1182 unsigned int frac = 0; | 1208 unsigned int frac = 0; |
1183 if(unit) { | 1209 if(unit) { |