df.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 ENABLE_FEATURE_ASSUME_UNICODE
  104. size_t dev_len;
  105. #endif
  106. if (mount_table) {
  107. mount_entry = getmntent(mount_table);
  108. if (!mount_entry) {
  109. endmntent(mount_table);
  110. break;
  111. }
  112. } else {
  113. mount_point = *argv++;
  114. if (!mount_point)
  115. break;
  116. mount_entry = find_mount_point(mount_point, 1);
  117. if (!mount_entry) {
  118. bb_error_msg("%s: can't find mount point", mount_point);
  119. set_error:
  120. status = EXIT_FAILURE;
  121. continue;
  122. }
  123. }
  124. device = mount_entry->mnt_fsname;
  125. mount_point = mount_entry->mnt_dir;
  126. if (statfs(mount_point, &s) != 0) {
  127. bb_simple_perror_msg(mount_point);
  128. goto set_error;
  129. }
  130. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  131. if (opt & OPT_INODE) {
  132. s.f_blocks = s.f_files;
  133. s.f_bavail = s.f_bfree = s.f_ffree;
  134. s.f_bsize = 1;
  135. if (df_disp_hr)
  136. df_disp_hr = 1;
  137. }
  138. blocks_used = s.f_blocks - s.f_bfree;
  139. blocks_percent_used = 0;
  140. if (blocks_used + s.f_bavail) {
  141. blocks_percent_used = (blocks_used * 100ULL
  142. + (blocks_used + s.f_bavail)/2
  143. ) / (blocks_used + s.f_bavail);
  144. }
  145. /* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
  146. if (strcmp(device, "rootfs") == 0)
  147. continue;
  148. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  149. if (strcmp(device, "/dev/root") == 0) {
  150. /* Adjusts device to be the real root device,
  151. * or leaves device alone if it can't find it */
  152. device = find_block_device("/");
  153. if (!device) {
  154. goto set_error;
  155. }
  156. }
  157. #endif
  158. #if ENABLE_FEATURE_ASSUME_UNICODE
  159. dev_len = unicode_strlen(device);
  160. if (dev_len > 20) {
  161. printf("%s\n%20s", device, "");
  162. } else {
  163. printf("%s%*s", device, 20 - (int)dev_len, "");
  164. }
  165. #else
  166. if (printf("\n%-20s" + 1, device) > 20)
  167. printf("\n%-20s", "");
  168. #endif
  169. #if ENABLE_FEATURE_HUMAN_READABLE
  170. printf(" %9s ",
  171. /* f_blocks x f_bsize / df_disp_hr, show one fractional,
  172. * use suffixes if df_disp_hr == 0 */
  173. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  174. printf(" %9s " + 1,
  175. /* EXPR x f_bsize / df_disp_hr, show one fractional,
  176. * use suffixes if df_disp_hr == 0 */
  177. make_human_readable_str((s.f_blocks - s.f_bfree),
  178. s.f_bsize, df_disp_hr));
  179. printf("%9s %3u%% %s\n",
  180. /* f_bavail x f_bsize / df_disp_hr, show one fractional,
  181. * use suffixes if df_disp_hr == 0 */
  182. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  183. blocks_percent_used, mount_point);
  184. #else
  185. printf(" %9lu %9lu %9lu %3u%% %s\n",
  186. kscale(s.f_blocks, s.f_bsize),
  187. kscale(s.f_blocks - s.f_bfree, s.f_bsize),
  188. kscale(s.f_bavail, s.f_bsize),
  189. blocks_percent_used, mount_point);
  190. #endif
  191. }
  192. }
  193. return status;
  194. }