df.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini df implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  11. *
  12. * Size reduction. Removed floating point dependency. Added error checking
  13. * on output. Output stats on 0-sized filesystems if specifically listed on
  14. * the command line. Properly round *-blocks, Used, and Available quantities.
  15. *
  16. * Aug 28, 2008 Bernhard Reutner-Fischer
  17. *
  18. * Implement -P and -B; better coreutils compat; cleanup
  19. */
  20. //config:config DF
  21. //config: bool "df"
  22. //config: default y
  23. //config: help
  24. //config: df reports the amount of disk space used and available
  25. //config: on filesystems.
  26. //config:
  27. //config:config FEATURE_DF_FANCY
  28. //config: bool "Enable -a, -i, -B"
  29. //config: default y
  30. //config: depends on DF
  31. //config: help
  32. //config: This option enables -a, -i and -B.
  33. //config:
  34. //config: -a Show all filesystems
  35. //config: -i Inodes
  36. //config: -B <SIZE> Blocksize
  37. //applet:IF_DF(APPLET(df, BB_DIR_BIN, BB_SUID_DROP))
  38. //kbuild:lib-$(CONFIG_DF) += df.o
  39. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
  40. /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
  41. //usage:#define df_trivial_usage
  42. //usage: "[-Pk"
  43. //usage: IF_FEATURE_HUMAN_READABLE("mh")
  44. //usage: "T"
  45. //usage: IF_FEATURE_DF_FANCY("ai] [-B SIZE")
  46. //usage: "] [FILESYSTEM]..."
  47. //usage:#define df_full_usage "\n\n"
  48. //usage: "Print filesystem usage statistics\n"
  49. //usage: "\n -P POSIX output format"
  50. //usage: "\n -k 1024-byte blocks (default)"
  51. //usage: IF_FEATURE_HUMAN_READABLE(
  52. //usage: "\n -m 1M-byte blocks"
  53. //usage: "\n -h Human readable (e.g. 1K 243M 2G)"
  54. //usage: )
  55. //usage: "\n -T Print filesystem type"
  56. //usage: IF_FEATURE_DF_FANCY(
  57. //usage: "\n -a Show all filesystems"
  58. //usage: "\n -i Inodes"
  59. //usage: "\n -B SIZE Blocksize"
  60. //usage: )
  61. //usage:
  62. //usage:#define df_example_usage
  63. //usage: "$ df\n"
  64. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  65. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  66. //usage: "/dev/sda1 64216 36364 27852 57% /boot\n"
  67. //usage: "$ df /dev/sda3\n"
  68. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  69. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  70. //usage: "$ POSIXLY_CORRECT=sure df /dev/sda3\n"
  71. //usage: "Filesystem 512B-blocks Used Available Use% Mounted on\n"
  72. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  73. //usage: "$ POSIXLY_CORRECT=yep df -P /dev/sda3\n"
  74. //usage: "Filesystem 512-blocks Used Available Capacity Mounted on\n"
  75. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  76. #include <mntent.h>
  77. #include <sys/vfs.h>
  78. #include "libbb.h"
  79. #include "unicode.h"
  80. #if !ENABLE_FEATURE_HUMAN_READABLE
  81. static unsigned long kscale(unsigned long b, unsigned long bs)
  82. {
  83. return (b * (unsigned long long) bs + 1024/2) / 1024;
  84. }
  85. #endif
  86. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  87. int df_main(int argc UNUSED_PARAM, char **argv)
  88. {
  89. unsigned long blocks_used;
  90. unsigned blocks_percent_used;
  91. unsigned long df_disp_hr = 1024;
  92. int status = EXIT_SUCCESS;
  93. unsigned opt;
  94. FILE *mount_table;
  95. struct mntent *mount_entry;
  96. struct statfs s;
  97. enum {
  98. OPT_KILO = (1 << 0),
  99. OPT_POSIX = (1 << 1),
  100. OPT_FSTYPE = (1 << 2),
  101. OPT_ALL = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
  102. OPT_INODE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  103. OPT_BSIZE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
  104. OPT_HUMAN = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  105. OPT_MEGA = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  106. };
  107. const char *disp_units_hdr = NULL;
  108. char *chp;
  109. init_unicode();
  110. #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
  111. opt_complementary = "k-mB:m-Bk:B-km";
  112. #elif ENABLE_FEATURE_HUMAN_READABLE
  113. opt_complementary = "k-m:m-k";
  114. #endif
  115. opt = getopt32(argv, "kPT"
  116. IF_FEATURE_DF_FANCY("aiB:")
  117. IF_FEATURE_HUMAN_READABLE("hm")
  118. IF_FEATURE_DF_FANCY(, &chp));
  119. if (opt & OPT_MEGA)
  120. df_disp_hr = 1024*1024;
  121. if (opt & OPT_BSIZE) {
  122. /* GNU coreutils 8.25 accepts "-BMiB" form too */
  123. int i;
  124. for (i = 0; kmg_i_suffixes[i].suffix[0]; i++) {
  125. if (strcmp(kmg_i_suffixes[i].suffix, chp) == 0) {
  126. df_disp_hr = kmg_i_suffixes[i].mult;
  127. goto got_it;
  128. }
  129. }
  130. /* Range used to disallow 0 */
  131. df_disp_hr = xatoul_range_sfx(chp, 1, ULONG_MAX, kmg_i_suffixes);
  132. got_it: ;
  133. }
  134. /* From the manpage of df from coreutils-6.10:
  135. * Disk space is shown in 1K blocks by default, unless the environment
  136. * variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
  137. */
  138. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  139. df_disp_hr = 512;
  140. if (opt & OPT_HUMAN) {
  141. df_disp_hr = 0;
  142. disp_units_hdr = " Size";
  143. }
  144. if (opt & OPT_INODE)
  145. disp_units_hdr = " Inodes";
  146. if (disp_units_hdr == NULL) {
  147. #if ENABLE_FEATURE_HUMAN_READABLE
  148. disp_units_hdr = xasprintf("%s-blocks",
  149. /* print df_disp_hr, show no fractionals,
  150. * use suffixes if OPT_POSIX is set in opt */
  151. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
  152. );
  153. #else
  154. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  155. #endif
  156. }
  157. printf("Filesystem %s%-15sUsed Available %s Mounted on\n",
  158. (opt & OPT_FSTYPE) ? "Type " : "",
  159. disp_units_hdr,
  160. (opt & OPT_POSIX) ? "Capacity" : "Use%");
  161. mount_table = NULL;
  162. argv += optind;
  163. if (!argv[0]) {
  164. mount_table = setmntent(bb_path_mtab_file, "r");
  165. if (!mount_table)
  166. bb_perror_msg_and_die(bb_path_mtab_file);
  167. }
  168. while (1) {
  169. const char *device;
  170. const char *mount_point;
  171. const char *fs_type;
  172. if (mount_table) {
  173. mount_entry = getmntent(mount_table);
  174. if (!mount_entry) {
  175. endmntent(mount_table);
  176. break;
  177. }
  178. } else {
  179. mount_point = *argv++;
  180. if (!mount_point)
  181. break;
  182. mount_entry = find_mount_point(mount_point, 1);
  183. if (!mount_entry) {
  184. bb_error_msg("%s: can't find mount point", mount_point);
  185. set_error:
  186. status = EXIT_FAILURE;
  187. continue;
  188. }
  189. }
  190. device = mount_entry->mnt_fsname;
  191. mount_point = mount_entry->mnt_dir;
  192. fs_type = mount_entry->mnt_type;
  193. if (statfs(mount_point, &s) != 0) {
  194. bb_simple_perror_msg(mount_point);
  195. goto set_error;
  196. }
  197. /* Some uclibc versions were seen to lose f_frsize
  198. * (kernel does return it, but then uclibc does not copy it)
  199. */
  200. if (s.f_frsize == 0)
  201. s.f_frsize = s.f_bsize;
  202. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  203. if (opt & OPT_INODE) {
  204. s.f_blocks = s.f_files;
  205. s.f_bavail = s.f_bfree = s.f_ffree;
  206. s.f_frsize = 1;
  207. if (df_disp_hr)
  208. df_disp_hr = 1;
  209. }
  210. blocks_used = s.f_blocks - s.f_bfree;
  211. blocks_percent_used = 0;
  212. if (blocks_used + s.f_bavail) {
  213. blocks_percent_used = (blocks_used * 100ULL
  214. + (blocks_used + s.f_bavail)/2
  215. ) / (blocks_used + s.f_bavail);
  216. }
  217. /* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
  218. if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
  219. continue;
  220. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  221. if (strcmp(device, "/dev/root") == 0) {
  222. /* Adjusts device to be the real root device,
  223. * or leaves device alone if it can't find it */
  224. device = find_block_device("/");
  225. if (!device) {
  226. goto set_error;
  227. }
  228. }
  229. #endif
  230. #if ENABLE_UNICODE_SUPPORT
  231. {
  232. uni_stat_t uni_stat;
  233. char *uni_dev = unicode_conv_to_printable(&uni_stat, device);
  234. if (uni_stat.unicode_width > 20 && !(opt & OPT_POSIX)) {
  235. printf("%s\n%20s", uni_dev, "");
  236. } else {
  237. printf("%s%*s", uni_dev, 20 - (int)uni_stat.unicode_width, "");
  238. }
  239. free(uni_dev);
  240. if (opt & OPT_FSTYPE) {
  241. char *uni_type = unicode_conv_to_printable(&uni_stat, fs_type);
  242. if (uni_stat.unicode_width > 10 && !(opt & OPT_POSIX))
  243. printf(" %s\n%31s", uni_type, "");
  244. else
  245. printf(" %s%*s", uni_type, 10 - (int)uni_stat.unicode_width, "");
  246. free(uni_type);
  247. }
  248. }
  249. #else
  250. if (printf("\n%-20s" + 1, device) > 20 && !(opt & OPT_POSIX))
  251. printf("\n%-20s", "");
  252. if (opt & OPT_FSTYPE) {
  253. if (printf(" %-10s", fs_type) > 11 && !(opt & OPT_POSIX))
  254. printf("\n%-30s", "");
  255. }
  256. #endif
  257. #if ENABLE_FEATURE_HUMAN_READABLE
  258. printf(" %9s ",
  259. /* f_blocks x f_frsize / df_disp_hr, show one fractional,
  260. * use suffixes if df_disp_hr == 0 */
  261. make_human_readable_str(s.f_blocks, s.f_frsize, df_disp_hr));
  262. printf(" %9s " + 1,
  263. /* EXPR x f_frsize / df_disp_hr, show one fractional,
  264. * use suffixes if df_disp_hr == 0 */
  265. make_human_readable_str((s.f_blocks - s.f_bfree),
  266. s.f_frsize, df_disp_hr));
  267. printf("%9s %3u%% %s\n",
  268. /* f_bavail x f_frsize / df_disp_hr, show one fractional,
  269. * use suffixes if df_disp_hr == 0 */
  270. make_human_readable_str(s.f_bavail, s.f_frsize, df_disp_hr),
  271. blocks_percent_used, mount_point);
  272. #else
  273. printf(" %9lu %9lu %9lu %3u%% %s\n",
  274. kscale(s.f_blocks, s.f_frsize),
  275. kscale(s.f_blocks - s.f_bfree, s.f_frsize),
  276. kscale(s.f_bavail, s.f_frsize),
  277. blocks_percent_used, mount_point);
  278. #endif
  279. }
  280. }
  281. return status;
  282. }