df.c 9.4 KB

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