3
0

stat.c 24 KB

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