stat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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(struct timespec *ts)
  146. {
  147. char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + /*paranoia*/ 8];
  148. /* coreutils 6.3 compat */
  149. #define buf bb_common_bufsiz1
  150. setup_common_bufsiz();
  151. sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
  152. strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
  153. return buf;
  154. #undef buf
  155. }
  156. #if ENABLE_FEATURE_STAT_FILESYSTEM
  157. #define FS_TYPE_LIST \
  158. FS_TYPE(0xADFF, "affs") \
  159. FS_TYPE(0x1CD1, "devpts") \
  160. FS_TYPE(0x137D, "ext") \
  161. FS_TYPE(0xEF51, "ext2") \
  162. FS_TYPE(0xEF53, "ext2/ext3") \
  163. FS_TYPE(0x3153464a, "jfs") \
  164. FS_TYPE(0x58465342, "xfs") \
  165. FS_TYPE(0xF995E849, "hpfs") \
  166. FS_TYPE(0x9660, "isofs") \
  167. FS_TYPE(0x4000, "isofs") \
  168. FS_TYPE(0x4004, "isofs") \
  169. FS_TYPE(0x137F, "minix") \
  170. FS_TYPE(0x138F, "minix (30 char.)") \
  171. FS_TYPE(0x2468, "minix v2") \
  172. FS_TYPE(0x2478, "minix v2 (30 char.)") \
  173. FS_TYPE(0x4d44, "msdos") \
  174. FS_TYPE(0x4006, "fat") \
  175. FS_TYPE(0x564c, "novell") \
  176. FS_TYPE(0x6969, "nfs") \
  177. FS_TYPE(0x9fa0, "proc") \
  178. FS_TYPE(0x517B, "smb") \
  179. FS_TYPE(0x012FF7B4, "xenix") \
  180. FS_TYPE(0x012FF7B5, "sysv4") \
  181. FS_TYPE(0x012FF7B6, "sysv2") \
  182. FS_TYPE(0x012FF7B7, "coh") \
  183. FS_TYPE(0x00011954, "ufs") \
  184. FS_TYPE(0x012FD16D, "xia") \
  185. FS_TYPE(0x5346544e, "ntfs") \
  186. FS_TYPE(0x1021994, "tmpfs") \
  187. FS_TYPE(0x52654973, "reiserfs") \
  188. FS_TYPE(0x28cd3d45, "cramfs") \
  189. FS_TYPE(0x7275, "romfs") \
  190. FS_TYPE(0x858458f6, "ramfs") \
  191. FS_TYPE(0x73717368, "squashfs") \
  192. FS_TYPE(0x62656572, "sysfs")
  193. /* Return the type of the specified file system.
  194. * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
  195. * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
  196. * Still others have neither and have to get by with f_type (Linux).
  197. */
  198. static const char *human_fstype(uint32_t f_type)
  199. {
  200. # define FS_TYPE(type, name) type,
  201. static const uint32_t fstype[] = {
  202. FS_TYPE_LIST
  203. };
  204. # undef FS_TYPE
  205. # define FS_TYPE(type, name) name"\0"
  206. static const char humanname[] ALIGN1 =
  207. FS_TYPE_LIST
  208. "UNKNOWN";
  209. # undef FS_TYPE
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(fstype); ++i)
  212. if (fstype[i] == f_type)
  213. break;
  214. return nth_string(humanname, i);
  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_atim));
  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_mtim));
  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_ctim));
  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"
  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
  481. ? "%n %i %l %t %s %b %f %a %c %d %C"
  482. : "%n %i %l %t %s %b %f %a %c %d")
  483. : (option_mask32 & OPT_SELINUX
  484. ? " File: \"%n\"\n"
  485. " ID: %-8i Namelen: %-7l Type: %T\n"
  486. "Block size: %-10s\n"
  487. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  488. "Inodes: Total: %-10c Free: %d"
  489. " S_context: %C"
  490. : " File: \"%n\"\n"
  491. " ID: %-8i Namelen: %-7l Type: %T\n"
  492. "Block size: %-10s\n"
  493. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  494. "Inodes: Total: %-10c Free: %d")
  495. );
  496. # endif /* SELINUX */
  497. }
  498. print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
  499. #else /* !FEATURE_STAT_FORMAT */
  500. format = (option_mask32 & OPT_TERSE
  501. ? "%s %llx %lu "
  502. : " File: \"%s\"\n"
  503. " ID: %-8llx Namelen: %-7lu ");
  504. printf(format,
  505. filename,
  506. get_f_fsid(&statfsbuf),
  507. statfsbuf.f_namelen);
  508. if (option_mask32 & OPT_TERSE)
  509. printf("%lx ", (unsigned long) statfsbuf.f_type);
  510. else
  511. printf("Type: %s\n", human_fstype(statfsbuf.f_type));
  512. # if !ENABLE_SELINUX
  513. format = (option_mask32 & OPT_TERSE
  514. ? "%lu %llu %llu %llu %llu %llu\n"
  515. : "Block size: %-10lu\n"
  516. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  517. "Inodes: Total: %-10llu Free: %llu\n");
  518. printf(format,
  519. (unsigned long) statfsbuf.f_bsize,
  520. (unsigned long long) statfsbuf.f_blocks,
  521. (unsigned long long) statfsbuf.f_bfree,
  522. (unsigned long long) statfsbuf.f_bavail,
  523. (unsigned long long) statfsbuf.f_files,
  524. (unsigned long long) statfsbuf.f_ffree);
  525. # else
  526. format = (option_mask32 & OPT_TERSE
  527. ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
  528. : (option_mask32 & OPT_SELINUX
  529. ? "Block size: %-10lu\n"
  530. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  531. "Inodes: Total: %-10llu Free: %llu"
  532. "S_context: %C\n"
  533. : "Block size: %-10lu\n"
  534. "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
  535. "Inodes: Total: %-10llu Free: %llu\n"
  536. )
  537. );
  538. printf(format,
  539. (unsigned long) statfsbuf.f_bsize,
  540. (unsigned long long) statfsbuf.f_blocks,
  541. (unsigned long long) statfsbuf.f_bfree,
  542. (unsigned long long) statfsbuf.f_bavail,
  543. (unsigned long long) statfsbuf.f_files,
  544. (unsigned long long) statfsbuf.f_ffree,
  545. scontext);
  546. if (scontext)
  547. freecon(scontext);
  548. # endif
  549. #endif /* FEATURE_STAT_FORMAT */
  550. return 1;
  551. }
  552. #endif /* FEATURE_STAT_FILESYSTEM */
  553. /* stat the file and print what we find */
  554. #if !ENABLE_FEATURE_STAT_FORMAT
  555. #define do_stat(filename, format) do_stat(filename)
  556. #endif
  557. static bool do_stat(const char *filename, const char *format)
  558. {
  559. struct stat statbuf;
  560. #if ENABLE_SELINUX
  561. security_context_t scontext = NULL;
  562. if (option_mask32 & OPT_SELINUX) {
  563. if ((option_mask32 & OPT_DEREFERENCE
  564. ? lgetfilecon(filename, &scontext)
  565. : getfilecon(filename, &scontext)
  566. ) < 0
  567. ) {
  568. bb_simple_perror_msg(filename);
  569. return 0;
  570. }
  571. }
  572. #endif
  573. if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
  574. bb_perror_msg("can't stat '%s'", filename);
  575. return 0;
  576. }
  577. #if ENABLE_FEATURE_STAT_FORMAT
  578. if (format == NULL) {
  579. # if !ENABLE_SELINUX
  580. if (option_mask32 & OPT_TERSE) {
  581. format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  582. } else {
  583. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  584. format =
  585. " File: %N\n"
  586. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  587. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  588. " Device type: %t,%T\n"
  589. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  590. "Access: %x\n" "Modify: %y\n" "Change: %z";
  591. } else {
  592. format =
  593. " File: %N\n"
  594. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  595. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  596. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  597. "Access: %x\n" "Modify: %y\n" "Change: %z";
  598. }
  599. }
  600. # else
  601. if (option_mask32 & OPT_TERSE) {
  602. format = (option_mask32 & OPT_SELINUX ?
  603. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
  604. :
  605. "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
  606. );
  607. } else {
  608. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  609. format = (option_mask32 & OPT_SELINUX ?
  610. " File: %N\n"
  611. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  612. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  613. " Device type: %t,%T\n"
  614. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  615. " S_Context: %C\n"
  616. "Access: %x\n" "Modify: %y\n" "Change: %z"
  617. :
  618. " File: %N\n"
  619. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  620. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  621. " Device type: %t,%T\n"
  622. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  623. "Access: %x\n" "Modify: %y\n" "Change: %z"
  624. );
  625. } else {
  626. format = (option_mask32 & OPT_SELINUX ?
  627. " File: %N\n"
  628. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  629. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  630. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  631. "S_Context: %C\n"
  632. "Access: %x\n" "Modify: %y\n" "Change: %z"
  633. :
  634. " File: %N\n"
  635. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  636. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  637. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  638. "Access: %x\n" "Modify: %y\n" "Change: %z"
  639. );
  640. }
  641. }
  642. # endif
  643. }
  644. print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
  645. #else /* FEATURE_STAT_FORMAT */
  646. if (option_mask32 & OPT_TERSE) {
  647. printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
  648. IF_NOT_SELINUX("\n"),
  649. filename,
  650. (unsigned long long) statbuf.st_size,
  651. (unsigned long long) statbuf.st_blocks,
  652. (unsigned long) statbuf.st_mode,
  653. (unsigned long) statbuf.st_uid,
  654. (unsigned long) statbuf.st_gid,
  655. (unsigned long long) statbuf.st_dev,
  656. (unsigned long long) statbuf.st_ino,
  657. (unsigned long) statbuf.st_nlink,
  658. (unsigned long) major(statbuf.st_rdev),
  659. (unsigned long) minor(statbuf.st_rdev),
  660. (unsigned long) statbuf.st_atime,
  661. (unsigned long) statbuf.st_mtime,
  662. (unsigned long) statbuf.st_ctime,
  663. (unsigned long) statbuf.st_blksize
  664. );
  665. # if ENABLE_SELINUX
  666. if (option_mask32 & OPT_SELINUX)
  667. printf(" %s\n", scontext);
  668. else
  669. bb_putchar('\n');
  670. # endif
  671. } else {
  672. char *linkname = NULL;
  673. struct passwd *pw_ent;
  674. struct group *gw_ent;
  675. gw_ent = getgrgid(statbuf.st_gid);
  676. pw_ent = getpwuid(statbuf.st_uid);
  677. if (S_ISLNK(statbuf.st_mode))
  678. linkname = xmalloc_readlink_or_warn(filename);
  679. if (linkname) {
  680. printf(" File: '%s' -> '%s'\n", filename, linkname);
  681. free(linkname);
  682. } else {
  683. printf(" File: '%s'\n", filename);
  684. }
  685. printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
  686. "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
  687. (unsigned long long) statbuf.st_size,
  688. (unsigned long long) statbuf.st_blocks,
  689. (unsigned long) statbuf.st_blksize,
  690. file_type(&statbuf),
  691. (unsigned long long) statbuf.st_dev,
  692. (unsigned long long) statbuf.st_dev,
  693. (unsigned long long) statbuf.st_ino,
  694. (unsigned long) statbuf.st_nlink);
  695. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  696. printf(" Device type: %lx,%lx\n",
  697. (unsigned long) major(statbuf.st_rdev),
  698. (unsigned long) minor(statbuf.st_rdev));
  699. else
  700. bb_putchar('\n');
  701. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
  702. (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  703. bb_mode_string(statbuf.st_mode),
  704. (unsigned long) statbuf.st_uid,
  705. (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
  706. (unsigned long) statbuf.st_gid,
  707. (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  708. # if ENABLE_SELINUX
  709. if (option_mask32 & OPT_SELINUX)
  710. printf(" S_Context: %s\n", scontext);
  711. # endif
  712. printf("Access: %s\n", human_time(&statbuf.st_atim));
  713. printf("Modify: %s\n", human_time(&statbuf.st_mtim));
  714. printf("Change: %s\n", human_time(&statbuf.st_ctim));
  715. }
  716. #endif /* FEATURE_STAT_FORMAT */
  717. return 1;
  718. }
  719. int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  720. int stat_main(int argc UNUSED_PARAM, char **argv)
  721. {
  722. IF_FEATURE_STAT_FORMAT(char *format = NULL;)
  723. int i;
  724. int ok;
  725. statfunc_ptr statfunc = do_stat;
  726. #if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
  727. unsigned opts;
  728. opts =
  729. #endif
  730. getopt32(argv, "^"
  731. "tL"
  732. IF_FEATURE_STAT_FILESYSTEM("f")
  733. IF_SELINUX("Z")
  734. IF_FEATURE_STAT_FORMAT("c:")
  735. "\0" "-1" /* min one arg */
  736. IF_FEATURE_STAT_FORMAT(,&format)
  737. );
  738. #if ENABLE_FEATURE_STAT_FILESYSTEM
  739. if (opts & OPT_FILESYS) /* -f */
  740. statfunc = do_statfs;
  741. #endif
  742. #if ENABLE_SELINUX
  743. if (opts & OPT_SELINUX) {
  744. selinux_or_die();
  745. }
  746. #endif
  747. ok = 1;
  748. argv += optind;
  749. for (i = 0; argv[i]; ++i)
  750. ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
  751. return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
  752. }