stat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. #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. setpwent();
  235. pw_ent = getpwuid(statbuf->st_uid);
  236. printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
  237. } else if (m == 'g') {
  238. strcat(pformat, "lu");
  239. printf(pformat, (unsigned long) statbuf->st_gid);
  240. } else if (m == 'G') {
  241. setgrent();
  242. gw_ent = getgrgid(statbuf->st_gid);
  243. printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  244. } else if (m == 't') {
  245. strcat(pformat, "lx");
  246. printf(pformat, (unsigned long) major(statbuf->st_rdev));
  247. } else if (m == 'T') {
  248. strcat(pformat, "lx");
  249. printf(pformat, (unsigned long) minor(statbuf->st_rdev));
  250. } else if (m == 's') {
  251. strcat(pformat, "llu");
  252. printf(pformat, (unsigned long long) statbuf->st_size);
  253. } else if (m == 'B') {
  254. strcat(pformat, "lu");
  255. printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
  256. } else if (m == 'b') {
  257. strcat(pformat, "llu");
  258. printf(pformat, (unsigned long long) statbuf->st_blocks);
  259. } else if (m == 'o') {
  260. strcat(pformat, "lu");
  261. printf(pformat, (unsigned long) statbuf->st_blksize);
  262. } else if (m == 'x') {
  263. printfs(pformat, human_time(statbuf->st_atime));
  264. } else if (m == 'X') {
  265. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  266. /* note: (unsigned long) would be wrong:
  267. * imagine (unsigned long64)int32 */
  268. printf(pformat, (long) statbuf->st_atime);
  269. } else if (m == 'y') {
  270. printfs(pformat, human_time(statbuf->st_mtime));
  271. } else if (m == 'Y') {
  272. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  273. printf(pformat, (long) statbuf->st_mtime);
  274. } else if (m == 'z') {
  275. printfs(pformat, human_time(statbuf->st_ctime));
  276. } else if (m == 'Z') {
  277. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  278. printf(pformat, (long) statbuf->st_ctime);
  279. # if ENABLE_SELINUX
  280. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  281. printfs(pformat, scontext);
  282. # endif
  283. } else {
  284. strcatc(pformat, 'c');
  285. printf(pformat, m);
  286. }
  287. }
  288. static void print_it(const char *masterformat,
  289. const char *filename,
  290. void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
  291. const void *data
  292. IF_SELINUX(, security_context_t scontext))
  293. {
  294. /* Create a working copy of the format string */
  295. char *format = xstrdup(masterformat);
  296. /* Add 2 to accomodate our conversion of the stat '%s' format string
  297. * to the printf '%llu' one. */
  298. char *dest = xmalloc(strlen(format) + 2 + 1);
  299. char *b;
  300. b = format;
  301. while (b) {
  302. /* Each iteration finds next %spec,
  303. * prints preceding string and handles found %spec
  304. */
  305. size_t len;
  306. char *p = strchr(b, '%');
  307. if (!p) {
  308. /* coreutils 6.3 always prints newline at the end */
  309. /*fputs(b, stdout);*/
  310. puts(b);
  311. break;
  312. }
  313. /* dest = "%<modifiers>" */
  314. len = 1 + strspn(p + 1, "#-+.I 0123456789");
  315. memcpy(dest, p, len);
  316. dest[len] = '\0';
  317. /* print preceding string */
  318. *p = '\0';
  319. fputs(b, stdout);
  320. p += len;
  321. b = p + 1;
  322. switch (*p) {
  323. case '\0':
  324. b = NULL;
  325. /* fall through */
  326. case '%':
  327. bb_putchar('%');
  328. break;
  329. default:
  330. /* Completes "%<modifiers>" with specifier and printfs */
  331. print_func(dest, *p, filename, data IF_SELINUX(,scontext));
  332. break;
  333. }
  334. }
  335. free(format);
  336. free(dest);
  337. }
  338. #endif /* FEATURE_STAT_FORMAT */
  339. /* Stat the file system and print what we find. */
  340. #if !ENABLE_FEATURE_STAT_FORMAT
  341. #define do_statfs(filename, format) do_statfs(filename)
  342. #endif
  343. static bool do_statfs(const char *filename, const char *format)
  344. {
  345. struct statfs statfsbuf;
  346. #if !ENABLE_FEATURE_STAT_FORMAT
  347. const char *format;
  348. #endif
  349. #if ENABLE_SELINUX
  350. security_context_t scontext = NULL;
  351. if (option_mask32 & OPT_SELINUX) {
  352. if ((option_mask32 & OPT_DEREFERENCE
  353. ? lgetfilecon(filename, &scontext)
  354. : getfilecon(filename, &scontext)
  355. ) < 0
  356. ) {
  357. bb_perror_msg(filename);
  358. return 0;
  359. }
  360. }
  361. #endif
  362. if (statfs(filename, &statfsbuf) != 0) {
  363. bb_perror_msg("can't read file system information for '%s'", filename);
  364. return 0;
  365. }
  366. #if ENABLE_FEATURE_STAT_FORMAT
  367. if (format == NULL) {
  368. # if !ENABLE_SELINUX
  369. format = (option_mask32 & OPT_TERSE
  370. ? "%n %i %l %t %s %b %f %a %c %d\n"
  371. : " File: \"%n\"\n"
  372. " ID: %-8i Namelen: %-7l Type: %T\n"
  373. "Block size: %-10s\n"
  374. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  375. "Inodes: Total: %-10c Free: %d");
  376. # else
  377. format = (option_mask32 & OPT_TERSE
  378. ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
  379. "%n %i %l %t %s %b %f %a %c %d\n")
  380. : (option_mask32 & OPT_SELINUX ?
  381. " File: \"%n\"\n"
  382. " ID: %-8i Namelen: %-7l Type: %T\n"
  383. "Block size: %-10s\n"
  384. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  385. "Inodes: Total: %-10c Free: %d"
  386. " S_context: %C\n":
  387. " File: \"%n\"\n"
  388. " ID: %-8i Namelen: %-7l Type: %T\n"
  389. "Block size: %-10s\n"
  390. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  391. "Inodes: Total: %-10c Free: %d\n")
  392. );
  393. # endif /* SELINUX */
  394. }
  395. print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
  396. #else /* FEATURE_STAT_FORMAT */
  397. format = (option_mask32 & OPT_TERSE
  398. ? "%s %llx %lu "
  399. : " File: \"%s\"\n"
  400. " ID: %-8llx Namelen: %-7lu ");
  401. printf(format,
  402. filename,
  403. get_f_fsid(&statfsbuf),
  404. statfsbuf.f_namelen);
  405. if (option_mask32 & OPT_TERSE)
  406. printf("%lx ", (unsigned long) statfsbuf.f_type);
  407. else
  408. printf("Type: %s\n", human_fstype(statfsbuf.f_type));
  409. # if !ENABLE_SELINUX
  410. format = (option_mask32 & OPT_TERSE
  411. ? "%lu %llu %llu %llu %llu %llu\n"
  412. : "Block size: %-10lu\n"
  413. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  414. "Inodes: Total: %-10llu Free: %llu\n");
  415. printf(format,
  416. (unsigned long) statfsbuf.f_bsize,
  417. (unsigned long long) statfsbuf.f_blocks,
  418. (unsigned long long) statfsbuf.f_bfree,
  419. (unsigned long long) statfsbuf.f_bavail,
  420. (unsigned long long) statfsbuf.f_files,
  421. (unsigned long long) statfsbuf.f_ffree);
  422. # else
  423. format = (option_mask32 & OPT_TERSE
  424. ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
  425. : (option_mask32 & OPT_SELINUX
  426. ? "Block size: %-10lu\n"
  427. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  428. "Inodes: Total: %-10llu Free: %llu"
  429. "S_context: %C\n"
  430. : "Block size: %-10lu\n"
  431. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  432. "Inodes: Total: %-10llu Free: %llu\n"
  433. )
  434. );
  435. printf(format,
  436. (unsigned long) statfsbuf.f_bsize,
  437. (unsigned long long) statfsbuf.f_blocks,
  438. (unsigned long long) statfsbuf.f_bfree,
  439. (unsigned long long) statfsbuf.f_bavail,
  440. (unsigned long long) statfsbuf.f_files,
  441. (unsigned long long) statfsbuf.f_ffree,
  442. scontext);
  443. if (scontext)
  444. freecon(scontext);
  445. # endif
  446. #endif /* FEATURE_STAT_FORMAT */
  447. return 1;
  448. }
  449. /* stat the file and print what we find */
  450. #if !ENABLE_FEATURE_STAT_FORMAT
  451. #define do_stat(filename, format) do_stat(filename)
  452. #endif
  453. static bool do_stat(const char *filename, const char *format)
  454. {
  455. struct stat statbuf;
  456. #if ENABLE_SELINUX
  457. security_context_t scontext = NULL;
  458. if (option_mask32 & OPT_SELINUX) {
  459. if ((option_mask32 & OPT_DEREFERENCE
  460. ? lgetfilecon(filename, &scontext)
  461. : getfilecon(filename, &scontext)
  462. ) < 0
  463. ) {
  464. bb_perror_msg(filename);
  465. return 0;
  466. }
  467. }
  468. #endif
  469. if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
  470. bb_perror_msg("can't stat '%s'", filename);
  471. return 0;
  472. }
  473. #if ENABLE_FEATURE_STAT_FORMAT
  474. if (format == NULL) {
  475. # if !ENABLE_SELINUX
  476. if (option_mask32 & OPT_TERSE) {
  477. format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  478. } else {
  479. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  480. format =
  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 =
  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. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  494. }
  495. }
  496. # else
  497. if (option_mask32 & OPT_TERSE) {
  498. format = (option_mask32 & OPT_SELINUX ?
  499. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
  500. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
  501. } else {
  502. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  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: %-5h"
  507. " Device type: %t,%T\n"
  508. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  509. " S_Context: %C\n"
  510. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  511. " File: %N\n"
  512. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  513. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  514. " Device type: %t,%T\n"
  515. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  516. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  517. } else {
  518. format = (option_mask32 & OPT_SELINUX ?
  519. " File: %N\n"
  520. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  521. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  522. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  523. "S_Context: %C\n"
  524. "Access: %x\n" "Modify: %y\n" "Change: %z\n":
  525. " File: %N\n"
  526. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  527. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  528. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  529. "Access: %x\n" "Modify: %y\n" "Change: %z\n");
  530. }
  531. }
  532. # endif
  533. }
  534. print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
  535. #else /* FEATURE_STAT_FORMAT */
  536. if (option_mask32 & OPT_TERSE) {
  537. printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
  538. IF_NOT_SELINUX("\n"),
  539. filename,
  540. (unsigned long long) statbuf.st_size,
  541. (unsigned long long) statbuf.st_blocks,
  542. (unsigned long) statbuf.st_mode,
  543. (unsigned long) statbuf.st_uid,
  544. (unsigned long) statbuf.st_gid,
  545. (unsigned long long) statbuf.st_dev,
  546. (unsigned long long) statbuf.st_ino,
  547. (unsigned long) statbuf.st_nlink,
  548. (unsigned long) major(statbuf.st_rdev),
  549. (unsigned long) minor(statbuf.st_rdev),
  550. (unsigned long) statbuf.st_atime,
  551. (unsigned long) statbuf.st_mtime,
  552. (unsigned long) statbuf.st_ctime,
  553. (unsigned long) statbuf.st_blksize
  554. );
  555. # if ENABLE_SELINUX
  556. if (option_mask32 & OPT_SELINUX)
  557. printf(" %lc\n", *scontext);
  558. else
  559. bb_putchar('\n');
  560. # endif
  561. } else {
  562. char *linkname = NULL;
  563. struct passwd *pw_ent;
  564. struct group *gw_ent;
  565. setgrent();
  566. gw_ent = getgrgid(statbuf.st_gid);
  567. setpwent();
  568. pw_ent = getpwuid(statbuf.st_uid);
  569. if (S_ISLNK(statbuf.st_mode))
  570. linkname = xmalloc_readlink_or_warn(filename);
  571. if (linkname)
  572. printf(" File: '%s' -> '%s'\n", filename, linkname);
  573. else
  574. printf(" File: '%s'\n", filename);
  575. printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
  576. "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
  577. (unsigned long long) statbuf.st_size,
  578. (unsigned long long) statbuf.st_blocks,
  579. (unsigned long) statbuf.st_blksize,
  580. file_type(&statbuf),
  581. (unsigned long long) statbuf.st_dev,
  582. (unsigned long long) statbuf.st_dev,
  583. (unsigned long long) statbuf.st_ino,
  584. (unsigned long) statbuf.st_nlink);
  585. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  586. printf(" Device type: %lx,%lx\n",
  587. (unsigned long) major(statbuf.st_rdev),
  588. (unsigned long) minor(statbuf.st_rdev));
  589. else
  590. bb_putchar('\n');
  591. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
  592. (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  593. bb_mode_string(statbuf.st_mode),
  594. (unsigned long) statbuf.st_uid,
  595. (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
  596. (unsigned long) statbuf.st_gid,
  597. (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  598. # if ENABLE_SELINUX
  599. printf(" S_Context: %lc\n", *scontext);
  600. # endif
  601. printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
  602. human_time(statbuf.st_atime),
  603. human_time(statbuf.st_mtime),
  604. human_time(statbuf.st_ctime));
  605. }
  606. #endif /* FEATURE_STAT_FORMAT */
  607. return 1;
  608. }
  609. int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  610. int stat_main(int argc UNUSED_PARAM, char **argv)
  611. {
  612. IF_FEATURE_STAT_FORMAT(char *format = NULL;)
  613. int i;
  614. int ok;
  615. unsigned opts;
  616. statfunc_ptr statfunc = do_stat;
  617. opt_complementary = "-1"; /* min one arg */
  618. opts = getopt32(argv, "ftL"
  619. IF_SELINUX("Z")
  620. IF_FEATURE_STAT_FORMAT("c:", &format)
  621. );
  622. if (opts & OPT_FILESYS) /* -f */
  623. statfunc = do_statfs;
  624. #if ENABLE_SELINUX
  625. if (opts & OPT_SELINUX) {
  626. selinux_or_die();
  627. }
  628. #endif
  629. ok = 1;
  630. argv += optind;
  631. for (i = 0; argv[i]; ++i)
  632. ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
  633. return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
  634. }