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: help
  35. //config: Without this, stat will not support the '-f' option to display
  36. //config: information about filesystem status.
  37. //applet:IF_STAT(APPLET_NOEXEC(stat, stat, BB_DIR_BIN, BB_SUID_DROP, stat))
  38. //kbuild:lib-$(CONFIG_STAT) += stat.o
  39. //usage:#define stat_trivial_usage
  40. //usage: "[OPTIONS] FILE..."
  41. //usage:#define stat_full_usage "\n\n"
  42. //usage: "Display file"
  43. //usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
  44. //usage: " status\n"
  45. //usage: IF_FEATURE_STAT_FORMAT(
  46. //usage: "\n -c FMT Use the specified format"
  47. //usage: )
  48. //usage: IF_FEATURE_STAT_FILESYSTEM(
  49. //usage: "\n -f Display filesystem status"
  50. //usage: )
  51. //usage: "\n -L Follow links"
  52. //usage: "\n -t Terse display"
  53. //usage: IF_SELINUX(
  54. //usage: "\n -Z Print security context"
  55. //usage: )
  56. //usage: IF_FEATURE_STAT_FORMAT(
  57. //usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
  58. //usage: " %a Access rights in octal\n"
  59. //usage: " %A Access rights in human readable form\n"
  60. //usage: " %b Number of blocks allocated (see %B)\n"
  61. //usage: " %B Size in bytes of each block reported by %b\n"
  62. //usage: " %d Device number in decimal\n"
  63. //usage: " %D Device number in hex\n"
  64. //usage: " %f Raw mode in hex\n"
  65. //usage: " %F File type\n"
  66. //usage: " %g Group ID\n"
  67. //usage: " %G Group name\n"
  68. //usage: " %h Number of hard links\n"
  69. //usage: " %i Inode number\n"
  70. //usage: " %n File name\n"
  71. //usage: " %N File name, with -> TARGET if symlink\n"
  72. //usage: " %o I/O block size\n"
  73. //usage: " %s Total size in bytes\n"
  74. //usage: " %t Major device type in hex\n"
  75. //usage: " %T Minor device type in hex\n"
  76. //usage: " %u User ID\n"
  77. //usage: " %U User name\n"
  78. //usage: " %x Time of last access\n"
  79. //usage: " %X Time of last access as seconds since Epoch\n"
  80. //usage: " %y Time of last modification\n"
  81. //usage: " %Y Time of last modification as seconds since Epoch\n"
  82. //usage: " %z Time of last change\n"
  83. //usage: " %Z Time of last change as seconds since Epoch\n"
  84. //usage: IF_FEATURE_STAT_FILESYSTEM(
  85. //usage: "\nFMT sequences for file systems:\n"
  86. //usage: " %a Free blocks available to non-superuser\n"
  87. //usage: " %b Total data blocks\n"
  88. //usage: " %c Total file nodes\n"
  89. //usage: " %d Free file nodes\n"
  90. //usage: " %f Free blocks\n"
  91. //usage: IF_SELINUX(
  92. //usage: " %C Security context in selinux\n"
  93. //usage: )
  94. //usage: " %i File System ID in hex\n"
  95. //usage: " %l Maximum length of filenames\n"
  96. //usage: " %n File name\n"
  97. //usage: " %s Block size (for faster transfer)\n"
  98. //usage: " %S Fundamental block size (for block counts)\n"
  99. //usage: " %t Type in hex\n"
  100. //usage: " %T Type in human readable form"
  101. //usage: )
  102. //usage: )
  103. #include "libbb.h"
  104. #include "common_bufsiz.h"
  105. enum {
  106. OPT_TERSE = (1 << 0),
  107. OPT_DEREFERENCE = (1 << 1),
  108. OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
  109. OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
  110. };
  111. #if ENABLE_FEATURE_STAT_FORMAT
  112. typedef bool (*statfunc_ptr)(const char *, const char *);
  113. #else
  114. typedef bool (*statfunc_ptr)(const char *);
  115. #endif
  116. static const char *file_type(const struct stat *st)
  117. {
  118. /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
  119. * for some of these formats.
  120. * To keep diagnostics grammatical in English, the
  121. * returned string must start with a consonant.
  122. */
  123. if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
  124. if (S_ISDIR(st->st_mode)) return "directory";
  125. if (S_ISBLK(st->st_mode)) return "block special file";
  126. if (S_ISCHR(st->st_mode)) return "character special file";
  127. if (S_ISFIFO(st->st_mode)) return "fifo";
  128. if (S_ISLNK(st->st_mode)) return "symbolic link";
  129. if (S_ISSOCK(st->st_mode)) return "socket";
  130. #ifdef S_TYPEISMQ
  131. if (S_TYPEISMQ(st)) return "message queue";
  132. #endif
  133. #ifdef S_TYPEISSEM
  134. if (S_TYPEISSEM(st)) return "semaphore";
  135. #endif
  136. #ifdef S_TYPEISSHM
  137. if (S_TYPEISSHM(st)) return "shared memory object";
  138. #endif
  139. #ifdef S_TYPEISTMO
  140. if (S_TYPEISTMO(st)) return "typed memory object";
  141. #endif
  142. return "weird file";
  143. }
  144. static const char *human_time(struct timespec *ts)
  145. {
  146. char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + /*paranoia*/ 8];
  147. /* coreutils 6.3 compat */
  148. #define buf bb_common_bufsiz1
  149. setup_common_bufsiz();
  150. sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
  151. strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
  152. return buf;
  153. #undef buf
  154. }
  155. #if ENABLE_FEATURE_STAT_FILESYSTEM
  156. #define FS_TYPE_LIST \
  157. FS_TYPE(0xADFF, "affs") \
  158. FS_TYPE(0x1CD1, "devpts") \
  159. FS_TYPE(0x137D, "ext") \
  160. FS_TYPE(0xEF51, "ext2") \
  161. FS_TYPE(0xEF53, "ext2/ext3") \
  162. FS_TYPE(0x3153464a, "jfs") \
  163. FS_TYPE(0x58465342, "xfs") \
  164. FS_TYPE(0xF995E849, "hpfs") \
  165. FS_TYPE(0x9660, "isofs") \
  166. FS_TYPE(0x4000, "isofs") \
  167. FS_TYPE(0x4004, "isofs") \
  168. FS_TYPE(0x137F, "minix") \
  169. FS_TYPE(0x138F, "minix (30 char.)") \
  170. FS_TYPE(0x2468, "minix v2") \
  171. FS_TYPE(0x2478, "minix v2 (30 char.)") \
  172. FS_TYPE(0x4d44, "msdos") \
  173. FS_TYPE(0x4006, "fat") \
  174. FS_TYPE(0x564c, "novell") \
  175. FS_TYPE(0x6969, "nfs") \
  176. FS_TYPE(0x9fa0, "proc") \
  177. FS_TYPE(0x517B, "smb") \
  178. FS_TYPE(0x012FF7B4, "xenix") \
  179. FS_TYPE(0x012FF7B5, "sysv4") \
  180. FS_TYPE(0x012FF7B6, "sysv2") \
  181. FS_TYPE(0x012FF7B7, "coh") \
  182. FS_TYPE(0x00011954, "ufs") \
  183. FS_TYPE(0x012FD16D, "xia") \
  184. FS_TYPE(0x5346544e, "ntfs") \
  185. FS_TYPE(0x1021994, "tmpfs") \
  186. FS_TYPE(0x52654973, "reiserfs") \
  187. FS_TYPE(0x28cd3d45, "cramfs") \
  188. FS_TYPE(0x7275, "romfs") \
  189. FS_TYPE(0x858458f6, "ramfs") \
  190. FS_TYPE(0x73717368, "squashfs") \
  191. FS_TYPE(0x62656572, "sysfs")
  192. /* Return the type of the specified file system.
  193. * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
  194. * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
  195. * Still others have neither and have to get by with f_type (Linux).
  196. */
  197. static const char *human_fstype(uint32_t f_type)
  198. {
  199. # define FS_TYPE(type, name) type,
  200. static const uint32_t fstype[] ALIGN4 = {
  201. FS_TYPE_LIST
  202. };
  203. # undef FS_TYPE
  204. # define FS_TYPE(type, name) name"\0"
  205. static const char humanname[] ALIGN1 =
  206. FS_TYPE_LIST
  207. "UNKNOWN";
  208. # undef FS_TYPE
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(fstype); ++i)
  211. if (fstype[i] == f_type)
  212. break;
  213. return nth_string(humanname, i);
  214. }
  215. /* "man statfs" says that statfsbuf->f_fsid is a mess */
  216. /* coreutils treats it as an array of ints, most significant first */
  217. static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
  218. {
  219. const unsigned *p = (const void*) &statfsbuf->f_fsid;
  220. unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
  221. unsigned long long r = 0;
  222. do
  223. r = (r << (sizeof(unsigned)*8)) | *p++;
  224. while (--sz > 0);
  225. return r;
  226. }
  227. #endif /* FEATURE_STAT_FILESYSTEM */
  228. #if ENABLE_FEATURE_STAT_FORMAT
  229. static void strcatc(char *str, char c)
  230. {
  231. int len = strlen(str);
  232. str[len++] = c;
  233. str[len] = '\0';
  234. }
  235. static void printfs(char *pformat, const char *msg)
  236. {
  237. strcatc(pformat, 's');
  238. printf(pformat, msg);
  239. }
  240. #if ENABLE_FEATURE_STAT_FILESYSTEM
  241. /* print statfs info */
  242. static void FAST_FUNC print_statfs(char *pformat, const char m,
  243. const char *const filename, const void *data
  244. IF_SELINUX(, security_context_t scontext))
  245. {
  246. const struct statfs *statfsbuf = data;
  247. if (m == 'n') {
  248. printfs(pformat, filename);
  249. } else if (m == 'i') {
  250. strcat(pformat, "llx");
  251. printf(pformat, get_f_fsid(statfsbuf));
  252. } else if (m == 'l') {
  253. strcat(pformat, "lu");
  254. printf(pformat, (unsigned long) statfsbuf->f_namelen);
  255. } else if (m == 't') {
  256. strcat(pformat, "lx");
  257. printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
  258. } else if (m == 'T') {
  259. printfs(pformat, human_fstype(statfsbuf->f_type));
  260. } else if (m == 'b') {
  261. strcat(pformat, "llu");
  262. printf(pformat, (unsigned long long) statfsbuf->f_blocks);
  263. } else if (m == 'f') {
  264. strcat(pformat, "llu");
  265. printf(pformat, (unsigned long long) statfsbuf->f_bfree);
  266. } else if (m == 'a') {
  267. strcat(pformat, "llu");
  268. printf(pformat, (unsigned long long) statfsbuf->f_bavail);
  269. } else if (m == 's' || m == 'S') {
  270. strcat(pformat, "lu");
  271. printf(pformat, (unsigned long) statfsbuf->f_bsize);
  272. } else if (m == 'c') {
  273. strcat(pformat, "llu");
  274. printf(pformat, (unsigned long long) statfsbuf->f_files);
  275. } else if (m == 'd') {
  276. strcat(pformat, "llu");
  277. printf(pformat, (unsigned long long) statfsbuf->f_ffree);
  278. # if ENABLE_SELINUX
  279. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  280. printfs(pformat, scontext);
  281. # endif
  282. } else {
  283. strcatc(pformat, 'c');
  284. printf(pformat, m);
  285. }
  286. }
  287. #endif
  288. /* print stat info */
  289. static void FAST_FUNC print_stat(char *pformat, const char m,
  290. const char *const filename, const void *data
  291. IF_SELINUX(, security_context_t scontext))
  292. {
  293. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  294. struct stat *statbuf = (struct stat *) data;
  295. struct passwd *pw_ent;
  296. struct group *gw_ent;
  297. if (m == 'n') {
  298. printfs(pformat, filename);
  299. } else if (m == 'N') {
  300. strcatc(pformat, 's');
  301. if (S_ISLNK(statbuf->st_mode)) {
  302. char *linkname = xmalloc_readlink_or_warn(filename);
  303. if (linkname == NULL)
  304. return;
  305. printf("'%s' -> '%s'", filename, linkname);
  306. free(linkname);
  307. } else {
  308. printf(pformat, filename);
  309. }
  310. } else if (m == 'd') {
  311. strcat(pformat, "llu");
  312. printf(pformat, (unsigned long long) statbuf->st_dev);
  313. } else if (m == 'D') {
  314. strcat(pformat, "llx");
  315. printf(pformat, (unsigned long long) statbuf->st_dev);
  316. } else if (m == 'i') {
  317. strcat(pformat, "llu");
  318. printf(pformat, (unsigned long long) statbuf->st_ino);
  319. } else if (m == 'a') {
  320. strcat(pformat, "lo");
  321. printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
  322. } else if (m == 'A') {
  323. printfs(pformat, bb_mode_string(statbuf->st_mode));
  324. } else if (m == 'f') {
  325. strcat(pformat, "lx");
  326. printf(pformat, (unsigned long) statbuf->st_mode);
  327. } else if (m == 'F') {
  328. printfs(pformat, file_type(statbuf));
  329. } else if (m == 'h') {
  330. strcat(pformat, "lu");
  331. printf(pformat, (unsigned long) statbuf->st_nlink);
  332. } else if (m == 'u') {
  333. strcat(pformat, "lu");
  334. printf(pformat, (unsigned long) statbuf->st_uid);
  335. } else if (m == 'U') {
  336. pw_ent = getpwuid(statbuf->st_uid);
  337. printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
  338. } else if (m == 'g') {
  339. strcat(pformat, "lu");
  340. printf(pformat, (unsigned long) statbuf->st_gid);
  341. } else if (m == 'G') {
  342. gw_ent = getgrgid(statbuf->st_gid);
  343. printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  344. } else if (m == 't') {
  345. strcat(pformat, "lx");
  346. printf(pformat, (unsigned long) major(statbuf->st_rdev));
  347. } else if (m == 'T') {
  348. strcat(pformat, "lx");
  349. printf(pformat, (unsigned long) minor(statbuf->st_rdev));
  350. } else if (m == 's') {
  351. strcat(pformat, "llu");
  352. printf(pformat, (unsigned long long) statbuf->st_size);
  353. } else if (m == 'B') {
  354. strcat(pformat, "lu");
  355. printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
  356. } else if (m == 'b') {
  357. strcat(pformat, "llu");
  358. printf(pformat, (unsigned long long) statbuf->st_blocks);
  359. } else if (m == 'o') {
  360. strcat(pformat, "lu");
  361. printf(pformat, (unsigned long) statbuf->st_blksize);
  362. } else if (m == 'x') {
  363. printfs(pformat, human_time(&statbuf->st_atim));
  364. } else if (m == 'X') {
  365. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  366. /* note: (unsigned long) would be wrong:
  367. * imagine (unsigned long64)int32 */
  368. printf(pformat, (long) statbuf->st_atime);
  369. } else if (m == 'y') {
  370. printfs(pformat, human_time(&statbuf->st_mtim));
  371. } else if (m == 'Y') {
  372. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  373. printf(pformat, (long) statbuf->st_mtime);
  374. } else if (m == 'z') {
  375. printfs(pformat, human_time(&statbuf->st_ctim));
  376. } else if (m == 'Z') {
  377. strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
  378. printf(pformat, (long) statbuf->st_ctime);
  379. # if ENABLE_SELINUX
  380. } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
  381. printfs(pformat, scontext);
  382. # endif
  383. } else {
  384. strcatc(pformat, 'c');
  385. printf(pformat, m);
  386. }
  387. }
  388. static void print_it(const char *masterformat,
  389. const char *filename,
  390. void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
  391. const void *data
  392. IF_SELINUX(, security_context_t scontext))
  393. {
  394. /* Create a working copy of the format string */
  395. char *format = xstrdup(masterformat);
  396. /* Add 2 to accommodate our conversion of the stat '%s' format string
  397. * to the printf '%llu' one. */
  398. char *dest = xmalloc(strlen(format) + 2 + 1);
  399. char *b;
  400. b = format;
  401. while (b) {
  402. /* Each iteration finds next %spec,
  403. * prints preceding string and handles found %spec
  404. */
  405. size_t len;
  406. char *p = strchr(b, '%');
  407. if (!p) {
  408. /* coreutils 6.3 always prints newline at the end */
  409. /*fputs(b, stdout);*/
  410. puts(b);
  411. break;
  412. }
  413. /* dest = "%<modifiers>" */
  414. len = 1 + strspn(p + 1, "#-+.I 0123456789");
  415. memcpy(dest, p, len);
  416. dest[len] = '\0';
  417. /* print preceding string */
  418. *p = '\0';
  419. fputs(b, stdout);
  420. p += len;
  421. b = p + 1;
  422. switch (*p) {
  423. case '\0':
  424. b = NULL;
  425. /* fall through */
  426. case '%':
  427. bb_putchar('%');
  428. break;
  429. default:
  430. /* Completes "%<modifiers>" with specifier and printfs */
  431. print_func(dest, *p, filename, data IF_SELINUX(,scontext));
  432. break;
  433. }
  434. }
  435. free(format);
  436. free(dest);
  437. }
  438. #endif /* FEATURE_STAT_FORMAT */
  439. #if ENABLE_FEATURE_STAT_FILESYSTEM
  440. /* Stat the file system and print what we find. */
  441. #if !ENABLE_FEATURE_STAT_FORMAT
  442. #define do_statfs(filename, format) do_statfs(filename)
  443. #endif
  444. static bool do_statfs(const char *filename, const char *format)
  445. {
  446. struct statfs statfsbuf;
  447. #if !ENABLE_FEATURE_STAT_FORMAT
  448. const char *format;
  449. #endif
  450. #if ENABLE_SELINUX
  451. security_context_t scontext = NULL;
  452. if (option_mask32 & OPT_SELINUX) {
  453. if ((option_mask32 & OPT_DEREFERENCE
  454. ? lgetfilecon(filename, &scontext)
  455. : getfilecon(filename, &scontext)
  456. ) < 0
  457. ) {
  458. bb_simple_perror_msg(filename);
  459. return 0;
  460. }
  461. }
  462. #endif
  463. if (statfs(filename, &statfsbuf) != 0) {
  464. bb_perror_msg("can't read file system information for '%s'", filename);
  465. return 0;
  466. }
  467. #if ENABLE_FEATURE_STAT_FORMAT
  468. if (format == NULL) {
  469. # if !ENABLE_SELINUX
  470. format = (option_mask32 & OPT_TERSE
  471. ? "%n %i %l %t %s %b %f %a %c %d"
  472. : " File: \"%n\"\n"
  473. " ID: %-8i Namelen: %-7l Type: %T\n"
  474. "Block size: %-10s\n"
  475. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  476. "Inodes: Total: %-10c Free: %d");
  477. # else
  478. format = (option_mask32 & OPT_TERSE
  479. ? (option_mask32 & OPT_SELINUX
  480. ? "%n %i %l %t %s %b %f %a %c %d %C"
  481. : "%n %i %l %t %s %b %f %a %c %d")
  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"
  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")
  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";
  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";
  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"
  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"
  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"
  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"
  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_atim));
  712. printf("Modify: %s\n", human_time(&statbuf.st_mtim));
  713. printf("Change: %s\n", human_time(&statbuf.st_ctim));
  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. }