stat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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: "[-lt"IF_FEATURE_STAT_FILESYSTEM("f")"] "IF_FEATURE_STAT_FORMAT("[-c FMT] ")"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. char modestr[12];
  324. printfs(pformat, bb_mode_string(modestr, 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_stdout(b);
  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 modestr[12];
  673. char *linkname = NULL;
  674. struct passwd *pw_ent;
  675. struct group *gw_ent;
  676. gw_ent = getgrgid(statbuf.st_gid);
  677. pw_ent = getpwuid(statbuf.st_uid);
  678. if (S_ISLNK(statbuf.st_mode))
  679. linkname = xmalloc_readlink_or_warn(filename);
  680. if (linkname) {
  681. printf(" File: '%s' -> '%s'\n", filename, linkname);
  682. free(linkname);
  683. } else {
  684. printf(" File: '%s'\n", filename);
  685. }
  686. printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
  687. "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
  688. (unsigned long long) statbuf.st_size,
  689. (unsigned long long) statbuf.st_blocks,
  690. (unsigned long) statbuf.st_blksize,
  691. file_type(&statbuf),
  692. (unsigned long long) statbuf.st_dev,
  693. (unsigned long long) statbuf.st_dev,
  694. (unsigned long long) statbuf.st_ino,
  695. (unsigned long) statbuf.st_nlink);
  696. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  697. printf(" Device type: %lx,%lx\n",
  698. (unsigned long) major(statbuf.st_rdev),
  699. (unsigned long) minor(statbuf.st_rdev));
  700. else
  701. bb_putchar('\n');
  702. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
  703. (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  704. bb_mode_string(modestr, statbuf.st_mode),
  705. (unsigned long) statbuf.st_uid,
  706. (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
  707. (unsigned long) statbuf.st_gid,
  708. (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
  709. # if ENABLE_SELINUX
  710. if (option_mask32 & OPT_SELINUX)
  711. printf(" S_Context: %s\n", scontext);
  712. # endif
  713. printf("Access: %s\n", human_time(&statbuf.st_atim));
  714. printf("Modify: %s\n", human_time(&statbuf.st_mtim));
  715. printf("Change: %s\n", human_time(&statbuf.st_ctim));
  716. }
  717. #endif /* FEATURE_STAT_FORMAT */
  718. return 1;
  719. }
  720. int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  721. int stat_main(int argc UNUSED_PARAM, char **argv)
  722. {
  723. IF_FEATURE_STAT_FORMAT(char *format = NULL;)
  724. int i;
  725. int ok;
  726. statfunc_ptr statfunc = do_stat;
  727. #if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
  728. unsigned opts;
  729. opts =
  730. #endif
  731. getopt32(argv, "^"
  732. "tL"
  733. IF_FEATURE_STAT_FILESYSTEM("f")
  734. IF_SELINUX("Z")
  735. IF_FEATURE_STAT_FORMAT("c:")
  736. "\0" "-1" /* min one arg */
  737. IF_FEATURE_STAT_FORMAT(,&format)
  738. );
  739. #if ENABLE_FEATURE_STAT_FILESYSTEM
  740. if (opts & OPT_FILESYS) /* -f */
  741. statfunc = do_statfs;
  742. #endif
  743. #if ENABLE_SELINUX
  744. if (opts & OPT_SELINUX) {
  745. selinux_or_die();
  746. }
  747. #endif
  748. ok = 1;
  749. argv += optind;
  750. for (i = 0; argv[i]; ++i)
  751. ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
  752. return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
  753. }