stat.c 24 KB

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