3
0

stat.c 19 KB

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