df.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 -- options -P and -t missing. Also blocksize. */
  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. #include <mntent.h>
  19. #include <sys/vfs.h>
  20. #include "libbb.h"
  21. #if !ENABLE_FEATURE_HUMAN_READABLE
  22. static unsigned long kscale(unsigned long b, unsigned long bs)
  23. {
  24. return (b * (unsigned long long) bs + 1024/2) / 1024;
  25. }
  26. #endif
  27. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  28. int df_main(int argc, char **argv)
  29. {
  30. unsigned long blocks_used;
  31. unsigned blocks_percent_used;
  32. #if ENABLE_FEATURE_HUMAN_READABLE
  33. unsigned df_disp_hr = 1024;
  34. #endif
  35. int status = EXIT_SUCCESS;
  36. unsigned opt;
  37. FILE *mount_table;
  38. struct mntent *mount_entry;
  39. struct statfs s;
  40. /* default display is kilobytes */
  41. const char *disp_units_hdr = "1k-blocks";
  42. enum {
  43. OPT_ALL = (1 << 0),
  44. OPT_INODE = (ENABLE_FEATURE_HUMAN_READABLE ? (1 << 4) : (1 << 2))
  45. * ENABLE_FEATURE_DF_INODE
  46. };
  47. #if ENABLE_FEATURE_HUMAN_READABLE
  48. opt_complementary = "h-km:k-hm:m-hk";
  49. opt = getopt32(argv, "ahmk" USE_FEATURE_DF_INODE("i"));
  50. if (opt & (1 << 1)) { // -h
  51. df_disp_hr = 0;
  52. disp_units_hdr = " Size";
  53. }
  54. if (opt & (1 << 2)) { // -m
  55. df_disp_hr = 1024*1024;
  56. disp_units_hdr = "1M-blocks";
  57. }
  58. if (opt & OPT_INODE) {
  59. disp_units_hdr = " Inodes";
  60. }
  61. #else
  62. opt = getopt32(argv, "ak" USE_FEATURE_DF_INODE("i"));
  63. #endif
  64. printf("Filesystem %-15sUsed Available Use%% Mounted on\n",
  65. disp_units_hdr);
  66. mount_table = NULL;
  67. argv += optind;
  68. if (optind >= argc) {
  69. mount_table = setmntent(bb_path_mtab_file, "r");
  70. if (!mount_table) {
  71. bb_perror_msg_and_die(bb_path_mtab_file);
  72. }
  73. }
  74. while (1) {
  75. const char *device;
  76. const char *mount_point;
  77. if (mount_table) {
  78. mount_entry = getmntent(mount_table);
  79. if (!mount_entry) {
  80. endmntent(mount_table);
  81. break;
  82. }
  83. } else {
  84. mount_point = *argv++;
  85. if (!mount_point) {
  86. break;
  87. }
  88. mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
  89. if (!mount_entry) {
  90. bb_error_msg("%s: can't find mount point", mount_point);
  91. SET_ERROR:
  92. status = EXIT_FAILURE;
  93. continue;
  94. }
  95. }
  96. device = mount_entry->mnt_fsname;
  97. mount_point = mount_entry->mnt_dir;
  98. if (statfs(mount_point, &s) != 0) {
  99. bb_simple_perror_msg(mount_point);
  100. goto SET_ERROR;
  101. }
  102. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  103. if (opt & OPT_INODE) {
  104. s.f_blocks = s.f_files;
  105. s.f_bavail = s.f_bfree = s.f_ffree;
  106. s.f_bsize = 1;
  107. #if ENABLE_FEATURE_HUMAN_READABLE
  108. if (df_disp_hr)
  109. df_disp_hr = 1;
  110. #endif
  111. }
  112. blocks_used = s.f_blocks - s.f_bfree;
  113. blocks_percent_used = 0;
  114. if (blocks_used + s.f_bavail) {
  115. blocks_percent_used = (blocks_used * 100ULL
  116. + (blocks_used + s.f_bavail)/2
  117. ) / (blocks_used + s.f_bavail);
  118. }
  119. #ifdef WHY_IT_SHOULD_BE_HIDDEN
  120. if (strcmp(device, "rootfs") == 0) {
  121. continue;
  122. }
  123. #endif
  124. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  125. /* ... and also this is the only user of find_block_device */
  126. if (strcmp(device, "/dev/root") == 0) {
  127. /* Adjusts device to be the real root device,
  128. * or leaves device alone if it can't find it */
  129. device = find_block_device("/");
  130. if (!device) {
  131. goto SET_ERROR;
  132. }
  133. }
  134. #endif
  135. if (printf("\n%-20s" + 1, device) > 20)
  136. printf("\n%-20s", "");
  137. #if ENABLE_FEATURE_HUMAN_READABLE
  138. printf(" %9s ",
  139. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  140. printf(" %9s " + 1,
  141. make_human_readable_str((s.f_blocks - s.f_bfree),
  142. s.f_bsize, df_disp_hr));
  143. printf("%9s %3u%% %s\n",
  144. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  145. blocks_percent_used, mount_point);
  146. #else
  147. printf(" %9lu %9lu %9lu %3u%% %s\n",
  148. kscale(s.f_blocks, s.f_bsize),
  149. kscale(s.f_blocks-s.f_bfree, s.f_bsize),
  150. kscale(s.f_bavail, s.f_bsize),
  151. blocks_percent_used, mount_point);
  152. #endif
  153. }
  154. }
  155. fflush_stdout_and_exit(status);
  156. }