stat.c 23 KB

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