df.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #if !ENABLE_FEATURE_HUMAN_READABLE
  26. static unsigned long kscale(unsigned long b, unsigned long bs)
  27. {
  28. return (b * (unsigned long long) bs + 1024/2) / 1024;
  29. }
  30. #endif
  31. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int df_main(int argc, char **argv)
  33. {
  34. unsigned long blocks_used;
  35. unsigned blocks_percent_used;
  36. unsigned long df_disp_hr = 1024;
  37. int status = EXIT_SUCCESS;
  38. unsigned opt;
  39. FILE *mount_table;
  40. struct mntent *mount_entry;
  41. struct statfs s;
  42. static const char ignored_mounts[] ALIGN1 =
  43. "rootfs\0";
  44. enum {
  45. OPT_KILO = (1 << 0),
  46. OPT_POSIX = (1 << 1),
  47. OPT_ALL = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
  48. OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
  49. OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  50. OPT_HUMAN = (1 << 5) * ENABLE_FEATURE_HUMAN_READABLE,
  51. OPT_MEGA = (1 << 6) * ENABLE_FEATURE_HUMAN_READABLE,
  52. };
  53. const char *disp_units_hdr = NULL;
  54. char *chp;
  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. USE_FEATURE_DF_FANCY("aiB:")
  62. USE_FEATURE_HUMAN_READABLE("hm")
  63. USE_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. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX)));
  84. #else
  85. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  86. #endif
  87. }
  88. printf("Filesystem %-15sUsed Available %s Mounted on\n",
  89. disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
  90. mount_table = NULL;
  91. argv += optind;
  92. if (optind >= argc) {
  93. mount_table = setmntent(bb_path_mtab_file, "r");
  94. if (!mount_table)
  95. bb_perror_msg_and_die(bb_path_mtab_file);
  96. }
  97. while (1) {
  98. const char *device;
  99. const char *mount_point;
  100. if (mount_table) {
  101. mount_entry = getmntent(mount_table);
  102. if (!mount_entry) {
  103. endmntent(mount_table);
  104. break;
  105. }
  106. } else {
  107. mount_point = *argv++;
  108. if (!mount_point)
  109. break;
  110. mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
  111. if (!mount_entry) {
  112. bb_error_msg("%s: can't find mount point", mount_point);
  113. SET_ERROR:
  114. status = EXIT_FAILURE;
  115. continue;
  116. }
  117. }
  118. device = mount_entry->mnt_fsname;
  119. mount_point = mount_entry->mnt_dir;
  120. if (statfs(mount_point, &s) != 0) {
  121. bb_simple_perror_msg(mount_point);
  122. goto SET_ERROR;
  123. }
  124. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  125. if (opt & OPT_INODE) {
  126. s.f_blocks = s.f_files;
  127. s.f_bavail = s.f_bfree = s.f_ffree;
  128. s.f_bsize = 1;
  129. if (df_disp_hr)
  130. df_disp_hr = 1;
  131. }
  132. blocks_used = s.f_blocks - s.f_bfree;
  133. blocks_percent_used = 0;
  134. if (blocks_used + s.f_bavail) {
  135. blocks_percent_used = (blocks_used * 100ULL
  136. + (blocks_used + s.f_bavail)/2
  137. ) / (blocks_used + s.f_bavail);
  138. }
  139. /* GNU coreutils 6.10 skip certain mounts, try to be compatible. */
  140. if (index_in_strings(device, ignored_mounts) != -1)
  141. continue;
  142. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  143. /* ... and also this is the only user of find_block_device */
  144. if (strcmp(device, "/dev/root") == 0) {
  145. /* Adjusts device to be the real root device,
  146. * or leaves device alone if it can't find it */
  147. device = find_block_device("/");
  148. if (!device) {
  149. goto SET_ERROR;
  150. }
  151. }
  152. #endif
  153. if (printf("\n%-20s" + 1, device) > 20)
  154. printf("\n%-20s", "");
  155. #if ENABLE_FEATURE_HUMAN_READABLE
  156. printf(" %9s ",
  157. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  158. printf(" %9s " + 1,
  159. make_human_readable_str((s.f_blocks - s.f_bfree),
  160. s.f_bsize, df_disp_hr));
  161. printf("%9s %3u%% %s\n",
  162. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  163. blocks_percent_used, mount_point);
  164. #else
  165. printf(" %9lu %9lu %9lu %3u%% %s\n",
  166. kscale(s.f_blocks, s.f_bsize),
  167. kscale(s.f_blocks - s.f_bfree, s.f_bsize),
  168. kscale(s.f_bavail, s.f_bsize),
  169. blocks_percent_used, mount_point);
  170. #endif
  171. }
  172. }
  173. return status;
  174. }