stat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * stat -- display file or file system status
  4. *
  5. * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
  6. * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
  7. * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
  8. * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
  9. *
  10. * Written by Michael Meskes
  11. * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  12. *
  13. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  14. */
  15. #include "libbb.h"
  16. #define OPT_FILESYS (1 << 0)
  17. #define OPT_TERSE (1 << 1)
  18. #define OPT_DEREFERENCE (1 << 2)
  19. #define OPT_SELINUX (1 << 3)
  20. #if ENABLE_FEATURE_STAT_FORMAT
  21. typedef bool (*statfunc_ptr)(const char *, const char *);
  22. #else
  23. typedef bool (*statfunc_ptr)(const char *);
  24. #endif
  25. static const char *file_type(const struct stat *st)
  26. {
  27. /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
  28. * for some of these formats.
  29. * To keep diagnostics grammatical in English, the
  30. * returned string must start with a consonant.
  31. */
  32. if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
  33. if (S_ISDIR(st->st_mode)) return "directory";
  34. if (S_ISBLK(st->st_mode)) return "block special file";
  35. if (S_ISCHR(st->st_mode)) return "character special file";
  36. if (S_ISFIFO(st->st_mode)) return "fifo";
  37. if (S_ISLNK(st->st_mode)) return "symbolic link";
  38. if (S_ISSOCK(st->st_mode)) return "socket";
  39. if (S_TYPEISMQ(st)) return "message queue";
  40. if (S_TYPEISSEM(st)) return "semaphore";
  41. if (S_TYPEISSHM(st)) return "shared memory object";
  42. #ifdef S_TYPEISTMO
  43. if (S_TYPEISTMO(st)) return "typed memory object";
  44. #endif
  45. return "weird file";
  46. }
  47. static const char *human_time(time_t t)
  48. {
  49. /* Old
  50. static char *str;
  51. str = ctime(&t);
  52. str[strlen(str)-1] = '\0';
  53. return str;
  54. */
  55. /* coreutils 6.3 compat: */
  56. /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
  57. #define buf bb_common_bufsiz1
  58. strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
  59. return buf;
  60. #undef buf
  61. }
  62. /* Return the type of the specified file system.
  63. * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
  64. * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
  65. * Still others have neither and have to get by with f_type (Linux).
  66. */
  67. static const char *human_fstype(uint32_t f_type)
  68. {
  69. static const struct types {
  70. uint32_t type;
  71. const char *const fs;
  72. } humantypes[] = {
  73. { 0xADFF, "affs" },
  74. { 0x1Cd1, "devpts" },
  75. { 0x137D, "ext" },
  76. { 0xEF51, "ext2" },
  77. { 0xEF53, "ext2/ext3" },
  78. { 0x3153464a, "jfs" },
  79. { 0x58465342, "xfs" },
  80. { 0xF995E849, "hpfs" },
  81. { 0x9660, "isofs" },
  82. { 0x4000, "isofs" },
  83. { 0x4004, "isofs" },
  84. { 0x137F, "minix" },
  85. { 0x138F, "minix (30 char.)" },
  86. { 0x2468, "minix v2" },
  87. { 0x2478, "minix v2 (30 char.)" },
  88. { 0x4d44, "msdos" },
  89. { 0x4006, "fat" },
  90. { 0x564c, "novell" },
  91. { 0x6969, "nfs" },
  92. { 0x9fa0, "proc" },
  93. { 0x517B, "smb" },
  94. { 0x012FF7B4, "xenix" },
  95. { 0x012FF7B5, "sysv4" },
  96. { 0x012FF7B6, "sysv2" },
  97. { 0x012FF7B7, "coh" },
  98. { 0x00011954, "ufs" },
  99. { 0x012FD16D, "xia" },
  100. { 0x5346544e, "ntfs" },
  101. { 0x1021994, "tmpfs" },
  102. { 0x52654973, "reiserfs" },
  103. { 0x28cd3d45, "cramfs" },
  104. { 0x7275, "romfs" },
  105. { 0x858458f6, "romfs" },
  106. { 0x73717368, "squashfs" },
  107. { 0x62656572, "sysfs" },
  108. { 0, "UNKNOWN" }
  109. };
  110. int i;
  111. for (i = 0; humantypes[i].type; ++i)
  112. if (humantypes[i].type == f_type)
  113. break;
  114. return humantypes[i].fs;
  115. }
  116. /* "man statfs" says that statfsbuf->f_fsid is a mess */
  117. /* coreutils treats it as an array of ints, most significant first */
  118. static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
  119. {
  120. const unsigned *p = (const void*) &statfsbuf->f_fsid;
  121. unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
  122. unsigned long long r = 0;
  123. do
  124. r = (r << (sizeof(unsigned)*8)) | *p++;
  125. while (--sz > 0);
  126. return r;
  127. }
  128. #if ENABLE_FEATURE_STAT_FORMAT
  129. static void strcatc(char *str, char c)
  130. {
  131. int len = strlen(str);
  132. str[len++] = c;
  133. str[len] = '\0';
  134. }
  135. static void printfs(char *pformat, const char *msg)
  136. {
  137. strcatc(pformat, 's');
  138. printf(pformat, msg);
  139. }
  140. /* print statfs info */
  141. static void FAST_FUNC print_statfs(char *pformat, const char m,
  142. const char *const filename, const void *data
  143. IF_SELINUX(, security_context_t scontext))
  144. {
  145. const struct statfs *statfsbuf = data;
  146. if (m == 'n') {
  147. printfs(pformat, filename);
  148. } else if (m == 'i') {
  149. strcat(pformat, "llx");
  150. printf(pformat, get_f_fsid(statfsbuf));
  151. } else if (m == 'l') {
  152. strcat(pformat, "lu");
  153. printf(pformat, (unsigned long) statfsbuf->f_namelen);
  154. } else if (m == 't') {
  155. strcat(pformat, "lx");
  156. printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
  157. } else if (m == 'T') {
  158. printfs(pformat, human_fstype(statfsbuf->f_type));
  159. } else if (m == 'b') {
  160. strcat(pformat, "llu");
  161. printf(pformat, (unsigned long long) statfsbuf->f_blocks);
  162. } else if (m == 'f') {
  163. strcat(pformat, "llu");
  164. printf(pformat, (unsigned long long) statfsbuf->f_bfree);
  165. } else if (m == 'a') {
  166. strcat(pformat, "llu");
  167. printf(pformat, (unsigned long long) statfsbuf->f_bavail);
  168. } else if (m == 's' || m == 'S') {
  169. strcat(pformat, "lu");
  170. printf(pformat, (unsigned long) statfsbuf->f_bsize);
  171. } else if (m == 'c') {
  172. strcat(pformat, "llu");
  173. printf(pformat, (unsigned long long) statfsbuf->f_files);
  174. } else if (m == 'd') {
  175. strcat(pformat, "llu");
  176. printf(pformat, (unsigned long long) statfsbuf->f_ffree);
  177. # if ENABLE_SELINUX
  178. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  179. printfs(pformat, scontext);
  180. # endif
  181. } else {
  182. strcatc(pformat, 'c');
  183. printf(pformat, m);
  184. }
  185. }
  186. /* print stat info */
  187. static void FAST_FUNC print_stat(char *pformat, const char m,
  188. const char *const filename, const void *data
  189. IF_SELINUX(, security_context_t scontext))
  190. {
  191. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  192. struct stat *statbuf = (struct stat *) data;
  193. struct passwd *pw_ent;
  194. struct group *gw_ent;
  195. if (m == 'n') {
  196. printfs(pformat, filename);
  197. } else if (m == 'N') {
  198. strcatc(pformat, 's');
  199. if (S_ISLNK(statbuf->st_mode)) {
  200. char *linkname = xmalloc_readlink_or_warn(filename);
  201. if (linkname == NULL)
  202. return;
  203. printf("'%s' -> '%s'", filename, linkname);
  204. free(linkname);
  205. } else {
  206. printf(pformat, filename);
  207. }
  208. } else if (m == 'd') {
  209. strcat(pformat, "llu");
  210. printf(pformat, (unsigned long long) statbuf->st_dev);
  211. } else if (m == 'D') {
  212. strcat(pformat, "llx");
  213. printf(pformat, (unsigned long long) statbuf->st_dev);
  214. } else if (m == 'i') {
  215. strcat(pformat, "llu");
  216. printf(pformat, (unsigned long long) statbuf->st_ino);
  217. } else if (m == 'a') {
  218. strcat(pformat, "lo");
  219. printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
  220. } else if (m == 'A') {
  221. printfs(pformat, bb_mode_string(statbuf->st_mode));
  222. } else if (m == 'f') {
  223. strcat(pformat, "lx");
  224. printf(pformat, (unsigned long) statbuf->st_mode);
  225. } else if (m == 'F') {
  226. printfs(pformat, file_type(statbuf));
  227. } else if (m == 'h') {
  228. strcat(pformat, "lu");
  229. printf(pformat, (unsigned long) statbuf->st_nlink);
  230. } else if (m == 'u') {
  231. strcat(pformat, "lu");
  232. printf(pformat, (unsigned long) statbuf->st_uid);
  233. } else if (m == 'U') {
  234. pw_ent = getpwuid(statbuf->st_uid);
  235. printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
  236. } else if (m == 'g') {
  237. strcat(pformat, "lu");
  238. printf(pformat, (unsigned long) statbuf->st_gid);
  239. } else if (m == 'G') {
  240. gw_ent = getgrgid(statbuf->st_gid);
  241. printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  242. } else if (m == 't') {
  243. strcat(pformat, "lx");
  244. printf(pformat, (unsigned long) major(statbuf->st_rdev));
  245. } else if (m == 'T') {
  246. strcat(pformat, "lx");
  247. printf(pformat, (unsigned long) minor(statbuf->st_rdev));
  248. } else if (m == 's') {
  249. strcat(pformat, "llu");
  250. printf(pformat, (unsigned long long) statbuf->st_size);
  251. } else if (m == 'B') {
  252. strcat(pformat, "lu");
  253. printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
  254. } else if (m == 'b') {
  255. strcat(pformat, "llu");
  256. printf(pformat, (unsigned long long) statbuf->st_blocks);
  257. } else if (m == 'o') {
  258. strcat(pformat, "lu");
  259. printf(pformat, (unsigned long) statbuf->st_blksize);
  260. } else if (m == 'x') {
  261. printfs(pformat, human_time(statbuf->st_atime));
  262. } else if (m == 'X') {
  263. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  264. /* note: (unsigned long) would be wrong:
  265. * imagine (unsigned long64)int32 */
  266. printf(pformat, (long) statbuf->st_atime);
  267. } else if (m == 'y') {
  268. printfs(pformat, human_time(statbuf->st_mtime));
  269. } else if (m == 'Y') {
  270. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  271. printf(pformat, (long) statbuf->st_mtime);
  272. } else if (m == 'z') {
  273. printfs(pformat, human_time(statbuf->st_ctime));
  274. } else if (m == 'Z') {
  275. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  276. printf(pformat, (long) statbuf->st_ctime);
  277. # if ENABLE_SELINUX
  278. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  279. printfs(pformat, scontext);
  280. # endif
  281. } else {
  282. strcatc(pformat, 'c');
  283. printf(pformat, m);
  284. }
  285. }
  286. static void print_it(const char *masterformat,
  287. const char *filename,
  288. void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
  289. const void *data
  290. IF_SELINUX(, security_context_t scontext))
  291. {
  292. /* Create a working copy of the format string */
  293. char *format = xstrdup(masterformat);
  294. /* Add 2 to accomodate our conversion of the stat '%s' format string
  295. * to the printf '%llu' one. */
  296. char *dest = xmalloc(strlen(format) + 2 + 1);
  297. char *b;
  298. b = format;
  299. while (b) {
  300. /* Each iteration finds next %spec,
  301. * prints preceding string and handles found %spec
  302. */
  303. size_t len;
  304. char *p = strchr(b, '%');
  305. if (!p) {
  306. /* coreutils 6.3 always prints newline at the end */
  307. /*fputs(b, stdout);*/
  308. puts(b);
  309. break;
  310. }
  311. /* dest = "%<modifiers>" */
  312. len = 1 + strspn(p + 1, "#-+.I 0123456789");
  313. memcpy(dest, p, len);
  314. dest[len] = '\0';
  315. /* print preceding string */
  316. *p = '\0';
  317. fputs(b, stdout);
  318. p += len;
  319. b = p + 1;
  320. switch (*p) {
  321. case '\0':
  322. b = NULL;
  323. /* fall through */
  324. case '%':
  325. bb_putchar('%');
  326. break;
  327. default:
  328. /* Completes "%<modifiers>" with specifier and printfs */
  329. print_func(dest, *p, filename, data IF_SELINUX(,scontext));
  330. break;
  331. }
  332. }
  333. free(format);
  334. free(dest);
  335. }
  336. #endif /* FEATURE_STAT_FORMAT */
  337. /* Stat the file system and print what we find. */
  338. #if !ENABLE_FEATURE_STAT_FORMAT
  339. #define do_statfs(filename, format) do_statfs(filename)
  340. #endif
  341. static bool do_statfs(const char *filename, const char *format)
  342. {
  343. struct statfs statfsbuf;
  344. #if !ENABLE_FEATURE_STAT_FORMAT
  345. const char *format;
  346. #endif
  347. #if ENABLE_SELINUX
  348. security_context_t scontext = NULL;
  349. if (option_mask32 & OPT_SELINUX) {
  350. if ((option_mask32 & OPT_DEREFERENCE
  351. ? lgetfilecon(filename, &scontext)
  352. : getfilecon(filename, &scontext)
  353. ) < 0
  354. ) {
  355. bb_perror_msg(filename);
  356. return 0;
  357. }
  358. }
  359. #endif
  360. if (statfs(filename, &statfsbuf) != 0) {
  361. bb_perror_msg("can't read file system information for '%s'", filename);
  362. return 0;
  363. }
  364. #if ENABLE_FEATURE_STAT_FORMAT
  365. if (format == NULL) {
  366. # if !ENABLE_SELINUX
  367. format = (option_mask32 & OPT_TERSE
  368. ? "%n %i %l %t %s %b %f %a %c %d\n"
  369. : " File: \"%n\"\n"
  370. " ID: %-8i Namelen: %-7l Type: %T\n"
  371. "Block size: %-10s\n"
  372. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  373. "Inodes: Total: %-10c Free: %d");
  374. # else
  375. format = (option_mask32 & OPT_TERSE
  376. ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
  377. "%n %i %l %t %s %b %f %a %c %d\n")
  378. : (option_mask32 & OPT_SELINUX ?
  379. " File: \"%n\"\n"
  380. " ID: %-8i Namelen: %-7l Type: %T\n"
  381. "Block size: %-10s\n"
  382. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  383. "Inodes: Total: %-10c Free: %d"
  384. " S_context: %C\n":
  385. " File: \"%n\"\n"
  386. " ID: %-8i Namelen: %-7l Type: %T\n"
  387. "Block size: %-10s\n"
  388. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  389. "Inodes: Total: %-10c Free: %d\n")
  390. );
  391. # endif /* SELINUX */
  392. }
  393. print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
  394. #else /* FEATURE_STAT_FORMAT */
  395. format = (option_mask32 & OPT_TERSE
  396. ? "%s %llx %lu "
  397. : " File: \"%s\"\n"
  398. " ID: %-8llx Namelen: %-7lu ");
  399. printf(format,
  400. filename,
  401. get_f_fsid(&statfsbuf),
  402. statfsbuf.f_namelen);
  403. if (option_mask32 & OPT_TERSE)
  404. printf("%lx ", (unsigned long) statfsbuf.f_type);
  405. else
  406. printf("Type: %s\n", human_fstype(statfsbuf.f_type));
  407. # if !ENABLE_SELINUX
  408. format = (option_mask32 & OPT_TERSE
  409. ? "%lu %llu %llu %llu %llu %llu\n"
  410. : "Block size: %-10lu\n"
  411. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  412. "Inodes: Total: %-10llu Free: %llu\n");
  413. printf(format,
  414. (unsigned long) statfsbuf.f_bsize,
  415. (unsigned long long) statfsbuf.f_blocks,
  416. (unsigned long long) statfsbuf.f_bfree,
  417. (unsigned long long) statfsbuf.f_bavail,
  418. (unsigned long long) statfsbuf.f_files,
  419. (unsigned long long) statfsbuf.f_ffree);
  420. # else
  421. format = (option_mask32 & OPT_TERSE
  422. ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
  423. : (option_mask32 & OPT_SELINUX
  424. ? "Block size: %-10lu\n"
  425. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  426. "Inodes: Total: %-10llu Free: %llu"
  427. "S_context: %C\n"
  428. : "Block size: %-10lu\n"
  429. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  430. "Inodes: Total: %-10llu Free: %llu\n"
  431. )
  432. );
  433. printf(format,
  434. (unsigned long) statfsbuf.f_bsize,
  435. (unsigned long long) statfsbuf.f_blocks,
  436. (unsigned long long) statfsbuf.f_bfree,
  437. (unsigned long long) statfsbuf.f_bavail,
  438. (unsigned long long) statfsbuf.f_files,
  439. (unsigned long long) statfsbuf.f_ffree,
  440. scontext);
  441. if (scontext)
  442. freecon(scontext);
  443. # endif
  444. #endif /* FEATURE_STAT_FORMAT */
  445. return 1;
  446. }
  447. /* stat the file and print what we find */
  448. #if !ENABLE_FEATURE_STAT_FORMAT
  449. #define do_stat(filename, format) do_stat(filename)
  450. #endif
  451. static bool do_stat(const char *filename, const char *format)
  452. {
  453. struct stat statbuf;
  454. #if ENABLE_SELINUX
  455. security_context_t scontext = NULL;
  456. if (option_mask32 & OPT_SELINUX) {
  457. if ((option_mask32 & OPT_DEREFERENCE
  458. ? lgetfilecon(filename, &scontext)
  459. : getfilecon(filename, &scontext)
  460. ) < 0
  461. ) {
  462. bb_perror_msg(filename);
  463. return 0;
  464. }
  465. }
  466. #endif
  467. if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
  468. bb_perror_msg("can't stat '%s'", filename);
  469. return 0;
  470. }
  471. #if ENABLE_FEATURE_STAT_FORMAT
  472. if (format == NULL) {
  473. # if !ENABLE_SELINUX
  474. if (option_mask32 & OPT_TERSE) {
  475. format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  476. } else {
  477. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  478. format =
  479. " File: %N\n"
  480. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  481. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  482. " Device type: %t,%T\n"
  483. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  484. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  485. } else {
  486. format =
  487. " File: %N\n"
  488. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  489. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  490. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  491. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  492. }
  493. }
  494. # else
  495. if (option_mask32 & OPT_TERSE) {
  496. format = (option_mask32 & OPT_SELINUX ?
  497. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
  498. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
  499. } else {
  500. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  501. format = (option_mask32 & OPT_SELINUX ?
  502. " File: %N\n"
  503. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  504. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  505. " Device type: %t,%T\n"
  506. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  507. " S_Context: %C\n"
  508. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  509. " File: %N\n"
  510. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  511. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  512. " Device type: %t,%T\n"
  513. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  514. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  515. } else {
  516. format = (option_mask32 & OPT_SELINUX ?
  517. " File: %N\n"
  518. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  519. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  520. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  521. "S_Context: %C\n"
  522. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  523. " File: %N\n"
  524. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  525. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  526. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  527. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  528. }
  529. }
  530. # endif
  531. }
  532. print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
  533. #else /* FEATURE_STAT_FORMAT */
  534. if (option_mask32 & OPT_TERSE) {
  535. printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
  536. IF_NOT_SELINUX("\n"),
  537. filename,
  538. (unsigned long long) statbuf.st_size,
  539. (unsigned long long) statbuf.st_blocks,
  540. (unsigned long) statbuf.st_mode,
  541. (unsigned long) statbuf.st_uid,
  542. (unsigned long) statbuf.st_gid,
  543. (unsigned long long) statbuf.st_dev,
  544. (unsigned long long) statbuf.st_ino,
  545. (unsigned long) statbuf.st_nlink,
  546. (unsigned long) major(statbuf.st_rdev),
  547. (unsigned long) minor(statbuf.st_rdev),
  548. (unsigned long) statbuf.st_atime,
  549. (unsigned long) statbuf.st_mtime,
  550. (unsigned long) statbuf.st_ctime,
  551. (unsigned long) statbuf.st_blksize
  552. );
  553. # if ENABLE_SELINUX
  554. if (option_mask32 & OPT_SELINUX)
  555. printf(" %lc\n", *scontext);
  556. else
  557. bb_putchar('\n');
  558. # endif
  559. } else {
  560. char *linkname = NULL;
  561. struct passwd *pw_ent;
  562. struct group *gw_ent;
  563. gw_ent = getgrgid(statbuf.st_gid);
  564. pw_ent = getpwuid(statbuf.st_uid);
  565. if (S_ISLNK(statbuf.st_mode))
  566. linkname = xmalloc_readlink_or_warn(filename);
  567. if (linkname) {
  568. printf(" File: '%s' -> '%s'\n", filename, linkname);
  569. free(linkname);
  570. } else {
  571. printf(" File: '%s'\n", filename);
  572. }
  573. printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
  574. "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
  575. (unsigned long long) statbuf.st_size,
  576. (unsigned long long) statbuf.st_blocks,
  577. (unsigned long) statbuf.st_blksize,
  578. file_type(&statbuf),
  579. (unsigned long long) statbuf.st_dev,
  580. (unsigned long long) statbuf.st_dev,
  581. (unsigned long long) statbuf.st_ino,
  582. (unsigned long) statbuf.st_nlink);
  583. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  584. printf(" Device type: %lx,%lx\n",
  585. (unsigned long) major(statbuf.st_rdev),
  586. (unsigned long) minor(statbuf.st_rdev));
  587. else
  588. bb_putchar('\n');
  589. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
  590. (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  591. bb_mode_string(statbuf.st_mode),
  592. (unsigned long) statbuf.st_uid,
  593. (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
  594. (unsigned long) statbuf.st_gid,
  595. (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  596. # if ENABLE_SELINUX
  597. printf(" S_Context: %lc\n", *scontext);
  598. # endif
  599. printf("Access: %s\n", human_time(statbuf.st_atime));
  600. printf("Modify: %s\n", human_time(statbuf.st_mtime));
  601. printf("Change: %s\n", human_time(statbuf.st_ctime));
  602. }
  603. #endif /* FEATURE_STAT_FORMAT */
  604. return 1;
  605. }
  606. int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  607. int stat_main(int argc UNUSED_PARAM, char **argv)
  608. {
  609. IF_FEATURE_STAT_FORMAT(char *format = NULL;)
  610. int i;
  611. int ok;
  612. unsigned opts;
  613. statfunc_ptr statfunc = do_stat;
  614. opt_complementary = "-1"; /* min one arg */
  615. opts = getopt32(argv, "ftL"
  616. IF_SELINUX("Z")
  617. IF_FEATURE_STAT_FORMAT("c:", &format)
  618. );
  619. if (opts & OPT_FILESYS) /* -f */
  620. statfunc = do_statfs;
  621. #if ENABLE_SELINUX
  622. if (opts & OPT_SELINUX) {
  623. selinux_or_die();
  624. }
  625. #endif
  626. ok = 1;
  627. argv += optind;
  628. for (i = 0; argv[i]; ++i)
  629. ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
  630. return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
  631. }