stat.c 24 KB

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