stat.c 24 KB

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