3
0

stat.c 23 KB

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