3
0

stat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. *
  9. * Written by Michael Meskes
  10. * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  11. *
  12. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  13. */
  14. #include "busybox.h"
  15. /* vars to control behavior */
  16. #define OPT_TERSE 2
  17. #define OPT_DEREFERENCE 4
  18. static long flags;
  19. static char const *file_type(struct stat const *st)
  20. {
  21. /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
  22. * for some of these formats.
  23. * To keep diagnostics grammatical in English, the
  24. * returned string must start with a consonant.
  25. */
  26. if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
  27. if (S_ISDIR(st->st_mode)) return "directory";
  28. if (S_ISBLK(st->st_mode)) return "block special file";
  29. if (S_ISCHR(st->st_mode)) return "character special file";
  30. if (S_ISFIFO(st->st_mode)) return "fifo";
  31. if (S_ISLNK(st->st_mode)) return "symbolic link";
  32. if (S_ISSOCK(st->st_mode)) return "socket";
  33. if (S_TYPEISMQ(st)) return "message queue";
  34. if (S_TYPEISSEM(st)) return "semaphore";
  35. if (S_TYPEISSHM(st)) return "shared memory object";
  36. #ifdef S_TYPEISTMO
  37. if (S_TYPEISTMO(st)) return "typed memory object";
  38. #endif
  39. return "weird file";
  40. }
  41. static char const *human_time(time_t t)
  42. {
  43. /* Old
  44. static char *str;
  45. str = ctime(&t);
  46. str[strlen(str)-1] = '\0';
  47. return str;
  48. */
  49. /* coreutils 6.3 compat: */
  50. static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
  51. strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
  52. return buf;
  53. }
  54. /* Return the type of the specified file system.
  55. * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
  56. * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
  57. * Still others have neither and have to get by with f_type (Linux).
  58. */
  59. static char const *human_fstype(long f_type)
  60. {
  61. int i;
  62. static const struct types {
  63. long type;
  64. const char *fs;
  65. } humantypes[] = {
  66. { 0xADFF, "affs" },
  67. { 0x1Cd1, "devpts" },
  68. { 0x137D, "ext" },
  69. { 0xEF51, "ext2" },
  70. { 0xEF53, "ext2/ext3" },
  71. { 0x3153464a, "jfs" },
  72. { 0x58465342, "xfs" },
  73. { 0xF995E849, "hpfs" },
  74. { 0x9660, "isofs" },
  75. { 0x4000, "isofs" },
  76. { 0x4004, "isofs" },
  77. { 0x137F, "minix" },
  78. { 0x138F, "minix (30 char.)" },
  79. { 0x2468, "minix v2" },
  80. { 0x2478, "minix v2 (30 char.)" },
  81. { 0x4d44, "msdos" },
  82. { 0x4006, "fat" },
  83. { 0x564c, "novell" },
  84. { 0x6969, "nfs" },
  85. { 0x9fa0, "proc" },
  86. { 0x517B, "smb" },
  87. { 0x012FF7B4, "xenix" },
  88. { 0x012FF7B5, "sysv4" },
  89. { 0x012FF7B6, "sysv2" },
  90. { 0x012FF7B7, "coh" },
  91. { 0x00011954, "ufs" },
  92. { 0x012FD16D, "xia" },
  93. { 0x5346544e, "ntfs" },
  94. { 0x1021994, "tmpfs" },
  95. { 0x52654973, "reiserfs" },
  96. { 0x28cd3d45, "cramfs" },
  97. { 0x7275, "romfs" },
  98. { 0x858458f6, "romfs" },
  99. { 0x73717368, "squashfs" },
  100. { 0x62656572, "sysfs" },
  101. { 0, "UNKNOWN" }
  102. };
  103. for (i=0; humantypes[i].type; ++i)
  104. if (humantypes[i].type == f_type)
  105. break;
  106. return humantypes[i].fs;
  107. }
  108. #ifdef CONFIG_FEATURE_STAT_FORMAT
  109. /* print statfs info */
  110. static void print_statfs(char *pformat, size_t buf_len, char m,
  111. char const *filename, void const *data)
  112. {
  113. struct statfs const *statfsbuf = data;
  114. switch (m) {
  115. case 'n':
  116. strncat(pformat, "s", buf_len);
  117. printf(pformat, filename);
  118. break;
  119. case 'i':
  120. strncat(pformat, "Lx", buf_len);
  121. printf(pformat, statfsbuf->f_fsid);
  122. break;
  123. case 'l':
  124. strncat(pformat, "lu", buf_len);
  125. printf(pformat, statfsbuf->f_namelen);
  126. break;
  127. case 't':
  128. strncat(pformat, "lx", buf_len);
  129. printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
  130. break;
  131. case 'T':
  132. strncat(pformat, "s", buf_len);
  133. printf(pformat, human_fstype(statfsbuf->f_type));
  134. break;
  135. case 'b':
  136. strncat(pformat, "jd", buf_len);
  137. printf(pformat, (intmax_t) (statfsbuf->f_blocks));
  138. break;
  139. case 'f':
  140. strncat(pformat, "jd", buf_len);
  141. printf(pformat, (intmax_t) (statfsbuf->f_bfree));
  142. break;
  143. case 'a':
  144. strncat(pformat, "jd", buf_len);
  145. printf(pformat, (intmax_t) (statfsbuf->f_bavail));
  146. break;
  147. case 'S':
  148. case 's':
  149. strncat(pformat, "lu", buf_len);
  150. printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
  151. break;
  152. case 'c':
  153. strncat(pformat, "jd", buf_len);
  154. printf(pformat, (intmax_t) (statfsbuf->f_files));
  155. break;
  156. case 'd':
  157. strncat(pformat, "jd", buf_len);
  158. printf(pformat, (intmax_t) (statfsbuf->f_ffree));
  159. break;
  160. default:
  161. strncat(pformat, "c", buf_len);
  162. printf(pformat, m);
  163. break;
  164. }
  165. }
  166. /* print stat info */
  167. static void print_stat(char *pformat, size_t buf_len, char m,
  168. char const *filename, void const *data)
  169. {
  170. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  171. struct stat *statbuf = (struct stat *) data;
  172. struct passwd *pw_ent;
  173. struct group *gw_ent;
  174. switch (m) {
  175. case 'n':
  176. strncat(pformat, "s", buf_len);
  177. printf(pformat, filename);
  178. break;
  179. case 'N':
  180. strncat(pformat, "s", buf_len);
  181. if (S_ISLNK(statbuf->st_mode)) {
  182. char *linkname = xreadlink(filename);
  183. if (linkname == NULL) {
  184. bb_perror_msg("cannot read symbolic link '%s'", filename);
  185. return;
  186. }
  187. /*printf("\"%s\" -> \"%s\"", filename, linkname); */
  188. printf(pformat, filename);
  189. printf(" -> ");
  190. printf(pformat, linkname);
  191. } else {
  192. printf(pformat, filename);
  193. }
  194. break;
  195. case 'd':
  196. strncat(pformat, "ju", buf_len);
  197. printf(pformat, (uintmax_t) statbuf->st_dev);
  198. break;
  199. case 'D':
  200. strncat(pformat, "jx", buf_len);
  201. printf(pformat, (uintmax_t) statbuf->st_dev);
  202. break;
  203. case 'i':
  204. strncat(pformat, "ju", buf_len);
  205. printf(pformat, (uintmax_t) statbuf->st_ino);
  206. break;
  207. case 'a':
  208. strncat(pformat, "lo", buf_len);
  209. printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
  210. break;
  211. case 'A':
  212. strncat(pformat, "s", buf_len);
  213. printf(pformat, bb_mode_string(statbuf->st_mode));
  214. break;
  215. case 'f':
  216. strncat(pformat, "lx", buf_len);
  217. printf(pformat, (unsigned long int) statbuf->st_mode);
  218. break;
  219. case 'F':
  220. strncat(pformat, "s", buf_len);
  221. printf(pformat, file_type(statbuf));
  222. break;
  223. case 'h':
  224. strncat(pformat, "lu", buf_len);
  225. printf(pformat, (unsigned long int) statbuf->st_nlink);
  226. break;
  227. case 'u':
  228. strncat(pformat, "lu", buf_len);
  229. printf(pformat, (unsigned long int) statbuf->st_uid);
  230. break;
  231. case 'U':
  232. strncat(pformat, "s", buf_len);
  233. setpwent();
  234. pw_ent = getpwuid(statbuf->st_uid);
  235. printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
  236. break;
  237. case 'g':
  238. strncat(pformat, "lu", buf_len);
  239. printf(pformat, (unsigned long int) statbuf->st_gid);
  240. break;
  241. case 'G':
  242. strncat(pformat, "s", buf_len);
  243. setgrent();
  244. gw_ent = getgrgid(statbuf->st_gid);
  245. printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
  246. break;
  247. case 't':
  248. strncat(pformat, "lx", buf_len);
  249. printf(pformat, (unsigned long int) major(statbuf->st_rdev));
  250. break;
  251. case 'T':
  252. strncat(pformat, "lx", buf_len);
  253. printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
  254. break;
  255. case 's':
  256. strncat(pformat, "ju", buf_len);
  257. printf(pformat, (uintmax_t) (statbuf->st_size));
  258. break;
  259. case 'B':
  260. strncat(pformat, "lu", buf_len);
  261. printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
  262. break;
  263. case 'b':
  264. strncat(pformat, "ju", buf_len);
  265. printf(pformat, (uintmax_t) statbuf->st_blocks);
  266. break;
  267. case 'o':
  268. strncat(pformat, "lu", buf_len);
  269. printf(pformat, (unsigned long int) statbuf->st_blksize);
  270. break;
  271. case 'x':
  272. strncat(pformat, "s", buf_len);
  273. printf(pformat, human_time(statbuf->st_atime));
  274. break;
  275. case 'X':
  276. strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
  277. printf(pformat, (unsigned long int) statbuf->st_atime);
  278. break;
  279. case 'y':
  280. strncat(pformat, "s", buf_len);
  281. printf(pformat, human_time(statbuf->st_mtime));
  282. break;
  283. case 'Y':
  284. strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
  285. printf(pformat, (unsigned long int) statbuf->st_mtime);
  286. break;
  287. case 'z':
  288. strncat(pformat, "s", buf_len);
  289. printf(pformat, human_time(statbuf->st_ctime));
  290. break;
  291. case 'Z':
  292. strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
  293. printf(pformat, (unsigned long int) statbuf->st_ctime);
  294. break;
  295. default:
  296. strncat(pformat, "c", buf_len);
  297. printf(pformat, m);
  298. break;
  299. }
  300. }
  301. static void print_it(char const *masterformat, char const *filename,
  302. void (*print_func) (char *, size_t, char, char const *, void const *),
  303. void const *data)
  304. {
  305. char *b;
  306. /* create a working copy of the format string */
  307. char *format = xstrdup(masterformat);
  308. /* Add 2 to accomodate our conversion of the stat '%s' format string
  309. * to the printf '%llu' one. */
  310. size_t n_alloc = strlen(format) + 2 + 1;
  311. char *dest = xmalloc(n_alloc);
  312. b = format;
  313. while (b) {
  314. size_t len;
  315. char *p = strchr(b, '%');
  316. if (!p) {
  317. /* coreutils 6.3 always print <cr> at the end */
  318. /*fputs(b, stdout);*/
  319. puts(b);
  320. break;
  321. }
  322. *p++ = '\0';
  323. fputs(b, stdout);
  324. len = strspn(p, "#-+.I 0123456789");
  325. dest[0] = '%';
  326. memcpy(dest + 1, p, len);
  327. dest[1 + len] = 0;
  328. p += len;
  329. b = p + 1;
  330. switch (*p) {
  331. case '\0':
  332. b = NULL;
  333. /* fall through */
  334. case '%':
  335. putchar('%');
  336. break;
  337. default:
  338. print_func(dest, n_alloc, *p, filename, data);
  339. break;
  340. }
  341. }
  342. free(format);
  343. free(dest);
  344. }
  345. #endif
  346. /* Stat the file system and print what we find. */
  347. static int do_statfs(char const *filename, char const *format)
  348. {
  349. struct statfs statfsbuf;
  350. if (statfs(filename, &statfsbuf) != 0) {
  351. bb_perror_msg("cannot read file system information for '%s'", filename);
  352. return 0;
  353. }
  354. #ifdef CONFIG_FEATURE_STAT_FORMAT
  355. if (format == NULL)
  356. format = (flags & OPT_TERSE
  357. ? "%n %i %l %t %s %b %f %a %c %d\n"
  358. : " File: \"%n\"\n"
  359. " ID: %-8i Namelen: %-7l Type: %T\n"
  360. "Block size: %-10s\n"
  361. "Blocks: Total: %-10b Free: %-10f Available: %a\n"
  362. "Inodes: Total: %-10c Free: %d");
  363. print_it(format, filename, print_statfs, &statfsbuf);
  364. #else
  365. format = (flags & OPT_TERSE
  366. ? "%s %llx %lu "
  367. : " File: \"%s\"\n"
  368. " ID: %-8Lx Namelen: %-7lu ");
  369. printf(format,
  370. filename,
  371. statfsbuf.f_fsid,
  372. statfsbuf.f_namelen);
  373. if (flags & OPT_TERSE)
  374. printf("%lx ", (unsigned long int) (statfsbuf.f_type));
  375. else
  376. printf("Type: %s\n", human_fstype(statfsbuf.f_type));
  377. format = (flags & OPT_TERSE
  378. ? "%lu %ld %ld %ld %ld %ld\n"
  379. : "Block size: %-10lu\n"
  380. "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
  381. "Inodes: Total: %-10jd Free: %jd\n");
  382. printf(format,
  383. (unsigned long int) (statfsbuf.f_bsize),
  384. (intmax_t) (statfsbuf.f_blocks),
  385. (intmax_t) (statfsbuf.f_bfree),
  386. (intmax_t) (statfsbuf.f_bavail),
  387. (intmax_t) (statfsbuf.f_files),
  388. (intmax_t) (statfsbuf.f_ffree));
  389. #endif
  390. return 1;
  391. }
  392. /* stat the file and print what we find */
  393. static int do_stat(char const *filename, char const *format)
  394. {
  395. struct stat statbuf;
  396. if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
  397. bb_perror_msg("cannot stat '%s'", filename);
  398. return 0;
  399. }
  400. #ifdef CONFIG_FEATURE_STAT_FORMAT
  401. if (format == NULL) {
  402. if (flags & OPT_TERSE) {
  403. format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  404. } else {
  405. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
  406. format =
  407. " File: \"%N\"\n"
  408. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  409. "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
  410. " Device type: %t,%T\n"
  411. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  412. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  413. } else {
  414. format =
  415. " File: \"%N\"\n"
  416. " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
  417. "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
  418. "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
  419. "Access: %x\n" "Modify: %y\n" "Change: %z\n";
  420. }
  421. }
  422. }
  423. print_it(format, filename, print_stat, &statbuf);
  424. #else
  425. if (flags & OPT_TERSE) {
  426. printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
  427. filename,
  428. (uintmax_t) (statbuf.st_size),
  429. (uintmax_t) statbuf.st_blocks,
  430. (unsigned long int) statbuf.st_mode,
  431. (unsigned long int) statbuf.st_uid,
  432. (unsigned long int) statbuf.st_gid,
  433. (uintmax_t) statbuf.st_dev,
  434. (uintmax_t) statbuf.st_ino,
  435. (unsigned long int) statbuf.st_nlink,
  436. (unsigned long int) major(statbuf.st_rdev),
  437. (unsigned long int) minor(statbuf.st_rdev),
  438. (unsigned long int) statbuf.st_atime,
  439. (unsigned long int) statbuf.st_mtime,
  440. (unsigned long int) statbuf.st_ctime,
  441. (unsigned long int) statbuf.st_blksize
  442. );
  443. } else {
  444. char *linkname = NULL;
  445. struct passwd *pw_ent;
  446. struct group *gw_ent;
  447. setgrent();
  448. gw_ent = getgrgid(statbuf.st_gid);
  449. setpwent();
  450. pw_ent = getpwuid(statbuf.st_uid);
  451. if (S_ISLNK(statbuf.st_mode))
  452. linkname = xreadlink(filename);
  453. if (linkname)
  454. printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
  455. else
  456. printf(" File: \"%s\"\n", filename);
  457. printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
  458. "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
  459. (uintmax_t) (statbuf.st_size),
  460. (uintmax_t) statbuf.st_blocks,
  461. (unsigned long int) statbuf.st_blksize,
  462. file_type(&statbuf),
  463. (uintmax_t) statbuf.st_dev,
  464. (uintmax_t) statbuf.st_dev,
  465. (uintmax_t) statbuf.st_ino,
  466. (unsigned long int) statbuf.st_nlink);
  467. if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
  468. printf(" Device type: %lx,%lx\n",
  469. (unsigned long int) major(statbuf.st_rdev),
  470. (unsigned long int) minor(statbuf.st_rdev));
  471. else
  472. putchar('\n');
  473. printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
  474. "Access: %s\n" "Modify: %s\n" "Change: %s\n",
  475. (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
  476. bb_mode_string(statbuf.st_mode),
  477. (unsigned long int) statbuf.st_uid,
  478. (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
  479. (unsigned long int) statbuf.st_gid,
  480. (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
  481. human_time(statbuf.st_atime),
  482. human_time(statbuf.st_mtime),
  483. human_time(statbuf.st_ctime));
  484. }
  485. #endif
  486. return 1;
  487. }
  488. int stat_main(int argc, char **argv)
  489. {
  490. int i;
  491. char *format = NULL;
  492. int ok = 1;
  493. int (*statfunc)(char const *, char const *) = do_stat;
  494. flags = getopt32(argc, argv, "ftL"
  495. USE_FEATURE_STAT_FORMAT("c:", &format)
  496. );
  497. if (flags & 1) /* -f */
  498. statfunc = do_statfs;
  499. if (argc == optind) /* files */
  500. bb_show_usage();
  501. for (i = optind; i < argc; ++i)
  502. ok &= statfunc(argv[i], format);
  503. return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
  504. }