3
0

stat.c 19 KB

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