stat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. /* "man statfs" says that statfsbuf->f_fsid is a mess */
  118. /* coreutils treats it as an array of ints, most significant first */
  119. static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
  120. {
  121. const unsigned *p = (const void*) &statfsbuf->f_fsid;
  122. unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
  123. unsigned long long r = 0;
  124. do
  125. r = (r << (sizeof(unsigned)*8)) | *p++;
  126. while (--sz > 0);
  127. return r;
  128. }
  129. #if ENABLE_FEATURE_STAT_FORMAT
  130. static void strcatc(char *str, char c)
  131. {
  132. int len = strlen(str);
  133. str[len++] = c;
  134. str[len] = '\0';
  135. }
  136. static void printfs(char *pformat, const char *msg)
  137. {
  138. strcatc(pformat, 's');
  139. printf(pformat, msg);
  140. }
  141. /* print statfs info */
  142. static void print_statfs(char *pformat, const char m,
  143. const char *const filename, const void *data
  144. IF_SELINUX(, security_context_t scontext))
  145. {
  146. const struct statfs *statfsbuf = data;
  147. if (m == 'n') {
  148. printfs(pformat, filename);
  149. } else if (m == 'i') {
  150. strcat(pformat, "llx");
  151. printf(pformat, get_f_fsid(statfsbuf));
  152. } else if (m == 'l') {
  153. strcat(pformat, "lu");
  154. printf(pformat, (unsigned long) (statfsbuf->f_namelen));
  155. } else if (m == 't') {
  156. strcat(pformat, "lx");
  157. printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
  158. } else if (m == 'T') {
  159. printfs(pformat, human_fstype(statfsbuf->f_type));
  160. } else if (m == 'b') {
  161. strcat(pformat, "jd");
  162. printf(pformat, (intmax_t) (statfsbuf->f_blocks));
  163. } else if (m == 'f') {
  164. strcat(pformat, "jd");
  165. printf(pformat, (intmax_t) (statfsbuf->f_bfree));
  166. } else if (m == 'a') {
  167. strcat(pformat, "jd");
  168. printf(pformat, (intmax_t) (statfsbuf->f_bavail));
  169. } else if (m == 's' || m == 'S') {
  170. strcat(pformat, "lu");
  171. printf(pformat, (unsigned long) (statfsbuf->f_bsize));
  172. } else if (m == 'c') {
  173. strcat(pformat, "jd");
  174. printf(pformat, (intmax_t) (statfsbuf->f_files));
  175. } else if (m == 'd') {
  176. strcat(pformat, "jd");
  177. printf(pformat, (intmax_t) (statfsbuf->f_ffree));
  178. #if ENABLE_SELINUX
  179. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  180. printfs(pformat, scontext);
  181. #endif
  182. } else {
  183. strcatc(pformat, 'c');
  184. printf(pformat, m);
  185. }
  186. }
  187. /* print stat info */
  188. static void print_stat(char *pformat, const char m,
  189. const char *const filename, const void *data
  190. IF_SELINUX(, security_context_t scontext))
  191. {
  192. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  193. struct stat *statbuf = (struct stat *) data;
  194. struct passwd *pw_ent;
  195. struct group *gw_ent;
  196. if (m == 'n') {
  197. printfs(pformat, filename);
  198. } else if (m == 'N') {
  199. strcatc(pformat, 's');
  200. if (S_ISLNK(statbuf->st_mode)) {
  201. char *linkname = xmalloc_readlink_or_warn(filename);
  202. if (linkname == NULL)
  203. return;
  204. /*printf("\"%s\" -> \"%s\"", filename, linkname); */
  205. printf(pformat, filename);
  206. printf(" -> ");
  207. printf(pformat, linkname);
  208. free(linkname);
  209. } else {
  210. printf(pformat, filename);
  211. }
  212. } else if (m == 'd') {
  213. strcat(pformat, "ju");
  214. printf(pformat, (uintmax_t) statbuf->st_dev);
  215. } else if (m == 'D') {
  216. strcat(pformat, "jx");
  217. printf(pformat, (uintmax_t) statbuf->st_dev);
  218. } else if (m == 'i') {
  219. strcat(pformat, "ju");
  220. printf(pformat, (uintmax_t) statbuf->st_ino);
  221. } else if (m == 'a') {
  222. strcat(pformat, "lo");
  223. printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
  224. } else if (m == 'A') {
  225. printfs(pformat, bb_mode_string(statbuf->st_mode));
  226. } else if (m == 'f') {
  227. strcat(pformat, "lx");
  228. printf(pformat, (unsigned long) statbuf->st_mode);
  229. } else if (m == 'F') {
  230. printfs(pformat, file_type(statbuf));
  231. } else if (m == 'h') {
  232. strcat(pformat, "lu");
  233. printf(pformat, (unsigned long) statbuf->st_nlink);
  234. } else if (m == 'u') {
  235. strcat(pformat, "lu");
  236. printf(pformat, (unsigned long) statbuf->st_uid);
  237. } else if (m == 'U') {
  238. setpwent();
  239. pw_ent = getpwuid(statbuf->st_uid);
  240. printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
  241. } else if (m == 'g') {
  242. strcat(pformat, "lu");
  243. printf(pformat, (unsigned long) statbuf->st_gid);
  244. } else if (m == 'G') {
  245. setgrent();
  246. gw_ent = getgrgid(statbuf->st_gid);
  247. printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  248. } else if (m == 't') {
  249. strcat(pformat, "lx");
  250. printf(pformat, (unsigned long) major(statbuf->st_rdev));
  251. } else if (m == 'T') {
  252. strcat(pformat, "lx");
  253. printf(pformat, (unsigned long) minor(statbuf->st_rdev));
  254. } else if (m == 's') {
  255. strcat(pformat, "ju");
  256. printf(pformat, (uintmax_t) (statbuf->st_size));
  257. } else if (m == 'B') {
  258. strcat(pformat, "lu");
  259. printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
  260. } else if (m == 'b') {
  261. strcat(pformat, "ju");
  262. printf(pformat, (uintmax_t) statbuf->st_blocks);
  263. } else if (m == 'o') {
  264. strcat(pformat, "lu");
  265. printf(pformat, (unsigned long) statbuf->st_blksize);
  266. } else if (m == 'x') {
  267. printfs(pformat, human_time(statbuf->st_atime));
  268. } else if (m == 'X') {
  269. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  270. printf(pformat, (unsigned long) statbuf->st_atime);
  271. } else if (m == 'y') {
  272. printfs(pformat, human_time(statbuf->st_mtime));
  273. } else if (m == 'Y') {
  274. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  275. printf(pformat, (unsigned long) statbuf->st_mtime);
  276. } else if (m == 'z') {
  277. printfs(pformat, human_time(statbuf->st_ctime));
  278. } else if (m == 'Z') {
  279. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  280. printf(pformat, (unsigned long) statbuf->st_ctime);
  281. #if ENABLE_SELINUX
  282. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  283. printfs(pformat, scontext);
  284. #endif
  285. } else {
  286. strcatc(pformat, 'c');
  287. printf(pformat, m);
  288. }
  289. }
  290. static void print_it(const char *masterformat, const char *filename,
  291. void (*print_func) (char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
  292. const void *data
  293. IF_SELINUX(, security_context_t scontext) )
  294. {
  295. /* Create a working copy of the format string */
  296. char *format = xstrdup(masterformat);
  297. /* Add 2 to accomodate our conversion of the stat '%s' format string
  298. * to the printf '%llu' one. */
  299. char *dest = xmalloc(strlen(format) + 2 + 1);
  300. char *b;
  301. b = format;
  302. while (b) {
  303. size_t len;
  304. char *p = strchr(b, '%');
  305. if (!p) {
  306. /* coreutils 6.3 always prints <cr> at the end */
  307. /*fputs(b, stdout);*/
  308. puts(b);
  309. break;
  310. }
  311. *p++ = '\0';
  312. fputs(b, stdout);
  313. /* dest = "%<modifiers>" */
  314. len = strspn(p, "#-+.I 0123456789");
  315. dest[0] = '%';
  316. memcpy(dest + 1, p, len);
  317. dest[1 + len] = '\0';
  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
  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 %ld %ld %ld %ld %ld\n"
  410. : "Block size: %-10lu\n"
  411. "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
  412. "Inodes: Total: %-10jd Free: %jd\n");
  413. printf(format,
  414. (unsigned long) (statfsbuf.f_bsize),
  415. (intmax_t) (statfsbuf.f_blocks),
  416. (intmax_t) (statfsbuf.f_bfree),
  417. (intmax_t) (statfsbuf.f_bavail),
  418. (intmax_t) (statfsbuf.f_files),
  419. (intmax_t) (statfsbuf.f_ffree));
  420. #else
  421. format = (option_mask32 & OPT_TERSE
  422. ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
  423. "%lu %ld %ld %ld %ld %ld\n")
  424. : (option_mask32 & OPT_SELINUX ?
  425. "Block size: %-10lu\n"
  426. "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
  427. "Inodes: Total: %-10jd Free: %jd"
  428. "S_context: %C\n":
  429. "Block size: %-10lu\n"
  430. "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
  431. "Inodes: Total: %-10jd Free: %jd\n"));
  432. printf(format,
  433. (unsigned long) (statfsbuf.f_bsize),
  434. (intmax_t) (statfsbuf.f_blocks),
  435. (intmax_t) (statfsbuf.f_bfree),
  436. (intmax_t) (statfsbuf.f_bavail),
  437. (intmax_t) (statfsbuf.f_files),
  438. (intmax_t) (statfsbuf.f_ffree),
  439. scontext);
  440. if (scontext)
  441. freecon(scontext);
  442. #endif
  443. #endif /* FEATURE_STAT_FORMAT */
  444. return 1;
  445. }
  446. /* stat the file and print what we find */
  447. #if !ENABLE_FEATURE_STAT_FORMAT
  448. #define do_stat(filename, format) do_stat(filename)
  449. #endif
  450. static bool do_stat(const char *filename, const char *format)
  451. {
  452. struct stat statbuf;
  453. #if ENABLE_SELINUX
  454. security_context_t scontext = NULL;
  455. if (option_mask32 & OPT_SELINUX) {
  456. if ((option_mask32 & OPT_DEREFERENCE
  457. ? lgetfilecon(filename, &scontext)
  458. : getfilecon(filename, &scontext)
  459. ) < 0
  460. ) {
  461. bb_perror_msg(filename);
  462. return 0;
  463. }
  464. }
  465. #endif
  466. if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
  467. bb_perror_msg("can't stat '%s'", filename);
  468. return 0;
  469. }
  470. #if ENABLE_FEATURE_STAT_FORMAT
  471. if (format == NULL) {
  472. #if !ENABLE_SELINUX
  473. if (option_mask32 & OPT_TERSE) {
  474. format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  475. } else {
  476. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  477. format =
  478. " File: \"%N\"\n"
  479. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  480. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  481. " Device type: %t,%T\n"
  482. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  483. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  484. } else {
  485. format =
  486. " File: \"%N\"\n"
  487. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  488. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  489. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  490. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  491. }
  492. }
  493. #else
  494. if (option_mask32 & OPT_TERSE) {
  495. format = (option_mask32 & OPT_SELINUX ?
  496. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
  497. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
  498. } else {
  499. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  500. format = (option_mask32 & OPT_SELINUX ?
  501. " File: \"%N\"\n"
  502. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  503. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  504. " Device type: %t,%T\n"
  505. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  506. " S_Context: %C\n"
  507. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  508. " File: \"%N\"\n"
  509. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  510. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  511. " Device type: %t,%T\n"
  512. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  513. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  514. } else {
  515. format = (option_mask32 & OPT_SELINUX ?
  516. " File: \"%N\"\n"
  517. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  518. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  519. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  520. "S_Context: %C\n"
  521. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  522. " File: \"%N\"\n"
  523. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  524. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  525. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  526. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  527. }
  528. }
  529. #endif
  530. }
  531. print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
  532. #else /* FEATURE_STAT_FORMAT */
  533. if (option_mask32 & OPT_TERSE) {
  534. printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
  535. IF_NOT_SELINUX("\n"),
  536. filename,
  537. (uintmax_t) (statbuf.st_size),
  538. (uintmax_t) statbuf.st_blocks,
  539. (unsigned long) statbuf.st_mode,
  540. (unsigned long) statbuf.st_uid,
  541. (unsigned long) statbuf.st_gid,
  542. (uintmax_t) statbuf.st_dev,
  543. (uintmax_t) statbuf.st_ino,
  544. (unsigned long) statbuf.st_nlink,
  545. (unsigned long) major(statbuf.st_rdev),
  546. (unsigned long) minor(statbuf.st_rdev),
  547. (unsigned long) statbuf.st_atime,
  548. (unsigned long) statbuf.st_mtime,
  549. (unsigned long) statbuf.st_ctime,
  550. (unsigned long) statbuf.st_blksize
  551. );
  552. #if ENABLE_SELINUX
  553. if (option_mask32 & OPT_SELINUX)
  554. printf(" %lc\n", *scontext);
  555. else
  556. bb_putchar('\n');
  557. #endif
  558. } else {
  559. char *linkname = NULL;
  560. struct passwd *pw_ent;
  561. struct group *gw_ent;
  562. setgrent();
  563. gw_ent = getgrgid(statbuf.st_gid);
  564. setpwent();
  565. pw_ent = getpwuid(statbuf.st_uid);
  566. if (S_ISLNK(statbuf.st_mode))
  567. linkname = xmalloc_readlink_or_warn(filename);
  568. if (linkname)
  569. printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
  570. else
  571. printf(" File: \"%s\"\n", filename);
  572. printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
  573. "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
  574. (uintmax_t) (statbuf.st_size),
  575. (uintmax_t) statbuf.st_blocks,
  576. (unsigned long) statbuf.st_blksize,
  577. file_type(&statbuf),
  578. (uintmax_t) statbuf.st_dev,
  579. (uintmax_t) statbuf.st_dev,
  580. (uintmax_t) statbuf.st_ino,
  581. (unsigned long) statbuf.st_nlink);
  582. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  583. printf(" Device type: %lx,%lx\n",
  584. (unsigned long) major(statbuf.st_rdev),
  585. (unsigned long) minor(statbuf.st_rdev));
  586. else
  587. bb_putchar('\n');
  588. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
  589. (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  590. bb_mode_string(statbuf.st_mode),
  591. (unsigned long) statbuf.st_uid,
  592. (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
  593. (unsigned long) statbuf.st_gid,
  594. (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  595. #if ENABLE_SELINUX
  596. printf(" S_Context: %lc\n", *scontext);
  597. #endif
  598. printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
  599. human_time(statbuf.st_atime),
  600. human_time(statbuf.st_mtime),
  601. 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. }