df.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 tarball for details.
  9. */
  10. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Size reduction. Removed floating point dependency. Added error checking
  15. * on output. Output stats on 0-sized filesystems if specifically listed on
  16. * the command line. Properly round *-blocks, Used, and Available quantities.
  17. *
  18. * Aug 28, 2008 Bernhard Reutner-Fischer
  19. *
  20. * Implement -P and -B; better coreutils compat; cleanup
  21. */
  22. #include <mntent.h>
  23. #include <sys/vfs.h>
  24. #include "libbb.h"
  25. #include "unicode.h"
  26. #if !ENABLE_FEATURE_HUMAN_READABLE
  27. static unsigned long kscale(unsigned long b, unsigned long bs)
  28. {
  29. return (b * (unsigned long long) bs + 1024/2) / 1024;
  30. }
  31. #endif
  32. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  33. int df_main(int argc UNUSED_PARAM, char **argv)
  34. {
  35. unsigned long blocks_used;
  36. unsigned blocks_percent_used;
  37. unsigned long df_disp_hr = 1024;
  38. int status = EXIT_SUCCESS;
  39. unsigned opt;
  40. FILE *mount_table;
  41. struct mntent *mount_entry;
  42. struct statfs s;
  43. enum {
  44. OPT_KILO = (1 << 0),
  45. OPT_POSIX = (1 << 1),
  46. OPT_ALL = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
  47. OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
  48. OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  49. OPT_HUMAN = (1 << (2 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  50. OPT_MEGA = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  51. };
  52. const char *disp_units_hdr = NULL;
  53. char *chp;
  54. init_unicode();
  55. #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
  56. opt_complementary = "k-mB:m-Bk:B-km";
  57. #elif ENABLE_FEATURE_HUMAN_READABLE
  58. opt_complementary = "k-m:m-k";
  59. #endif
  60. opt = getopt32(argv, "kP"
  61. IF_FEATURE_DF_FANCY("aiB:")
  62. IF_FEATURE_HUMAN_READABLE("hm")
  63. IF_FEATURE_DF_FANCY(, &chp));
  64. if (opt & OPT_MEGA)
  65. df_disp_hr = 1024*1024;
  66. if (opt & OPT_BSIZE)
  67. df_disp_hr = xatoul_range(chp, 1, ULONG_MAX); /* disallow 0 */
  68. /* From the manpage of df from coreutils-6.10:
  69. Disk space is shown in 1K blocks by default, unless the environment
  70. variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
  71. */
  72. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  73. df_disp_hr = 512;
  74. if (opt & OPT_HUMAN) {
  75. df_disp_hr = 0;
  76. disp_units_hdr = " Size";
  77. }
  78. if (opt & OPT_INODE)
  79. disp_units_hdr = " Inodes";
  80. if (disp_units_hdr == NULL) {
  81. #if ENABLE_FEATURE_HUMAN_READABLE
  82. disp_units_hdr = xasprintf("%s-blocks",
  83. /* print df_disp_hr, show no fractionals,
  84. * use suffixes if OPT_POSIX is set in opt */
  85. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
  86. );
  87. #else
  88. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  89. #endif
  90. }
  91. printf("Filesystem %-15sUsed Available %s Mounted on\n",
  92. disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
  93. mount_table = NULL;
  94. argv += optind;
  95. if (!argv[0]) {
  96. mount_table = setmntent(bb_path_mtab_file, "r");
  97. if (!mount_table)
  98. bb_perror_msg_and_die(bb_path_mtab_file);
  99. }
  100. while (1) {
  101. const char *device;
  102. const char *mount_point;
  103. if (mount_table) {
  104. mount_entry = getmntent(mount_table);
  105. if (!mount_entry) {
  106. endmntent(mount_table);
  107. break;
  108. }
  109. } else {
  110. mount_point = *argv++;
  111. if (!mount_point)
  112. break;
  113. mount_entry = find_mount_point(mount_point, 1);
  114. if (!mount_entry) {
  115. bb_error_msg("%s: can't find mount point", mount_point);
  116. set_error:
  117. status = EXIT_FAILURE;
  118. continue;
  119. }
  120. }
  121. device = mount_entry->mnt_fsname;
  122. mount_point = mount_entry->mnt_dir;
  123. if (statfs(mount_point, &s) != 0) {
  124. bb_simple_perror_msg(mount_point);
  125. goto set_error;
  126. }
  127. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  128. if (opt & OPT_INODE) {
  129. s.f_blocks = s.f_files;
  130. s.f_bavail = s.f_bfree = s.f_ffree;
  131. s.f_bsize = 1;
  132. if (df_disp_hr)
  133. df_disp_hr = 1;
  134. }
  135. blocks_used = s.f_blocks - s.f_bfree;
  136. blocks_percent_used = 0;
  137. if (blocks_used + s.f_bavail) {
  138. blocks_percent_used = (blocks_used * 100ULL
  139. + (blocks_used + s.f_bavail)/2
  140. ) / (blocks_used + s.f_bavail);
  141. }
  142. /* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
  143. if (strcmp(device, "rootfs") == 0)
  144. continue;
  145. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  146. if (strcmp(device, "/dev/root") == 0) {
  147. /* Adjusts device to be the real root device,
  148. * or leaves device alone if it can't find it */
  149. device = find_block_device("/");
  150. if (!device) {
  151. goto set_error;
  152. }
  153. }
  154. #endif
  155. #if ENABLE_UNICODE_SUPPORT
  156. {
  157. uni_stat_t uni_stat;
  158. char *uni_dev = unicode_conv_to_printable(&uni_stat, device);
  159. if (uni_stat.unicode_width > 20) {
  160. printf("%s\n%20s", uni_dev, "");
  161. } else {
  162. printf("%s%*s", uni_dev, 20 - (int)uni_stat.unicode_width, "");
  163. }
  164. free(uni_dev);
  165. }
  166. #else
  167. if (printf("\n%-20s" + 1, device) > 20)
  168. printf("\n%-20s", "");
  169. #endif
  170. #if ENABLE_FEATURE_HUMAN_READABLE
  171. printf(" %9s ",
  172. /* f_blocks x f_bsize / df_disp_hr, show one fractional,
  173. * use suffixes if df_disp_hr == 0 */
  174. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  175. printf(" %9s " + 1,
  176. /* EXPR x f_bsize / df_disp_hr, show one fractional,
  177. * use suffixes if df_disp_hr == 0 */
  178. make_human_readable_str((s.f_blocks - s.f_bfree),
  179. s.f_bsize, df_disp_hr));
  180. printf("%9s %3u%% %s\n",
  181. /* f_bavail x f_bsize / df_disp_hr, show one fractional,
  182. * use suffixes if df_disp_hr == 0 */
  183. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  184. blocks_percent_used, mount_point);
  185. #else
  186. printf(" %9lu %9lu %9lu %3u%% %s\n",
  187. kscale(s.f_blocks, s.f_bsize),
  188. kscale(s.f_blocks - s.f_bfree, s.f_bsize),
  189. kscale(s.f_bavail, s.f_bsize),
  190. blocks_percent_used, mount_point);
  191. #endif
  192. }
  193. }
  194. return status;
  195. }