df.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 (7.5 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 blocks_used;
  88. unsigned blocks_percent_used;
  89. unsigned long df_disp_hr = 1024;
  90. int status = EXIT_SUCCESS;
  91. unsigned opt;
  92. FILE *mount_table;
  93. struct mntent *mount_entry;
  94. struct statvfs s;
  95. enum {
  96. OPT_KILO = (1 << 0),
  97. OPT_POSIX = (1 << 1),
  98. OPT_FSTYPE = (1 << 2),
  99. OPT_ALL = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
  100. OPT_INODE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  101. OPT_BSIZE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
  102. OPT_HUMAN = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  103. OPT_MEGA = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  104. };
  105. const char *disp_units_hdr = NULL;
  106. char *chp;
  107. init_unicode();
  108. opt = getopt32(argv, "^"
  109. "kPT"
  110. IF_FEATURE_DF_FANCY("aiB:")
  111. IF_FEATURE_HUMAN_READABLE("hm")
  112. "\0"
  113. #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
  114. "k-mB:m-Bk:B-km"
  115. #elif ENABLE_FEATURE_HUMAN_READABLE
  116. "k-m:m-k"
  117. #endif
  118. IF_FEATURE_DF_FANCY(, &chp)
  119. );
  120. if (opt & OPT_MEGA)
  121. df_disp_hr = 1024*1024;
  122. if (opt & OPT_BSIZE) {
  123. /* GNU coreutils 8.25 accepts "-BMiB" form too */
  124. int i;
  125. for (i = 0; kmg_i_suffixes[i].suffix[0]; i++) {
  126. if (strcmp(kmg_i_suffixes[i].suffix, chp) == 0) {
  127. df_disp_hr = kmg_i_suffixes[i].mult;
  128. goto got_it;
  129. }
  130. }
  131. /* Range used to disallow 0 */
  132. df_disp_hr = xatoul_range_sfx(chp, 1, ULONG_MAX, kmg_i_suffixes);
  133. got_it: ;
  134. }
  135. /* From the manpage of df from coreutils-6.10:
  136. * Disk space is shown in 1K blocks by default, unless the environment
  137. * variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
  138. */
  139. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  140. df_disp_hr = 512;
  141. if (opt & OPT_HUMAN) {
  142. df_disp_hr = 0;
  143. disp_units_hdr = " Size";
  144. }
  145. if (opt & OPT_INODE)
  146. disp_units_hdr = " Inodes";
  147. if (disp_units_hdr == NULL) {
  148. #if ENABLE_FEATURE_HUMAN_READABLE
  149. disp_units_hdr = xasprintf("%s-blocks",
  150. /* print df_disp_hr, show no fractionals,
  151. * use suffixes if OPT_POSIX is set in opt */
  152. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
  153. );
  154. #else
  155. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  156. #endif
  157. }
  158. printf("Filesystem %s%-15sUsed Available %s Mounted on\n",
  159. (opt & OPT_FSTYPE) ? "Type " : "",
  160. disp_units_hdr,
  161. (opt & OPT_POSIX) ? "Capacity" : "Use%");
  162. mount_table = NULL;
  163. argv += optind;
  164. if (!argv[0]) {
  165. mount_table = setmntent(bb_path_mtab_file, "r");
  166. if (!mount_table)
  167. bb_perror_msg_and_die(bb_path_mtab_file);
  168. }
  169. while (1) {
  170. const char *device;
  171. const char *mount_point;
  172. const char *fs_type;
  173. if (mount_table) {
  174. mount_entry = getmntent(mount_table);
  175. if (!mount_entry) {
  176. endmntent(mount_table);
  177. break;
  178. }
  179. } else {
  180. mount_point = *argv++;
  181. if (!mount_point)
  182. break;
  183. mount_entry = find_mount_point(mount_point, 1);
  184. if (!mount_entry) {
  185. bb_error_msg("%s: can't find mount point", mount_point);
  186. set_error:
  187. status = EXIT_FAILURE;
  188. continue;
  189. }
  190. }
  191. device = mount_entry->mnt_fsname;
  192. mount_point = mount_entry->mnt_dir;
  193. fs_type = mount_entry->mnt_type;
  194. if (statvfs(mount_point, &s) != 0) {
  195. bb_simple_perror_msg(mount_point);
  196. goto set_error;
  197. }
  198. /* Some uclibc versions were seen to lose f_frsize
  199. * (kernel does return it, but then uclibc does not copy it)
  200. */
  201. if (s.f_frsize == 0)
  202. s.f_frsize = s.f_bsize;
  203. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  204. if (opt & OPT_INODE) {
  205. s.f_blocks = s.f_files;
  206. s.f_bavail = s.f_bfree = s.f_ffree;
  207. s.f_frsize = 1;
  208. if (df_disp_hr)
  209. df_disp_hr = 1;
  210. }
  211. blocks_used = s.f_blocks - s.f_bfree;
  212. blocks_percent_used = 0;
  213. if (blocks_used + s.f_bavail) {
  214. blocks_percent_used = (blocks_used * 100ULL
  215. + (blocks_used + s.f_bavail)/2
  216. ) / (blocks_used + s.f_bavail);
  217. }
  218. /* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
  219. if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
  220. continue;
  221. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  222. if (strcmp(device, "/dev/root") == 0) {
  223. /* Adjusts device to be the real root device,
  224. * or leaves device alone if it can't find it */
  225. device = find_block_device("/");
  226. if (!device) {
  227. goto set_error;
  228. }
  229. }
  230. #endif
  231. #if ENABLE_UNICODE_SUPPORT
  232. {
  233. uni_stat_t uni_stat;
  234. char *uni_dev = unicode_conv_to_printable(&uni_stat, device);
  235. if (uni_stat.unicode_width > 20 && !(opt & OPT_POSIX)) {
  236. printf("%s\n%20s", uni_dev, "");
  237. } else {
  238. printf("%s%*s", uni_dev, 20 - (int)uni_stat.unicode_width, "");
  239. }
  240. free(uni_dev);
  241. if (opt & OPT_FSTYPE) {
  242. char *uni_type = unicode_conv_to_printable(&uni_stat, fs_type);
  243. if (uni_stat.unicode_width > 10 && !(opt & OPT_POSIX))
  244. printf(" %s\n%31s", uni_type, "");
  245. else
  246. printf(" %s%*s", uni_type, 10 - (int)uni_stat.unicode_width, "");
  247. free(uni_type);
  248. }
  249. }
  250. #else
  251. if (printf("\n%-20s" + 1, device) > 20 && !(opt & OPT_POSIX))
  252. printf("\n%-20s", "");
  253. if (opt & OPT_FSTYPE) {
  254. if (printf(" %-10s", fs_type) > 11 && !(opt & OPT_POSIX))
  255. printf("\n%-30s", "");
  256. }
  257. #endif
  258. #if ENABLE_FEATURE_HUMAN_READABLE
  259. printf(" %9s ",
  260. /* f_blocks x f_frsize / df_disp_hr, show one fractional,
  261. * use suffixes if df_disp_hr == 0 */
  262. make_human_readable_str(s.f_blocks, s.f_frsize, df_disp_hr));
  263. printf(" %9s " + 1,
  264. /* EXPR x f_frsize / df_disp_hr, show one fractional,
  265. * use suffixes if df_disp_hr == 0 */
  266. make_human_readable_str((s.f_blocks - s.f_bfree),
  267. s.f_frsize, df_disp_hr));
  268. printf("%9s %3u%% %s\n",
  269. /* f_bavail x f_frsize / df_disp_hr, show one fractional,
  270. * use suffixes if df_disp_hr == 0 */
  271. make_human_readable_str(s.f_bavail, s.f_frsize, df_disp_hr),
  272. blocks_percent_used, mount_point);
  273. #else
  274. printf(" %9lu %9lu %9lu %3u%% %s\n",
  275. kscale(s.f_blocks, s.f_frsize),
  276. kscale(s.f_blocks - s.f_bfree, s.f_frsize),
  277. kscale(s.f_bavail, s.f_frsize),
  278. blocks_percent_used, mount_point);
  279. #endif
  280. }
  281. }
  282. return status;
  283. }