df.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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);
  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. #if ENABLE_FEATURE_HUMAN_READABLE
  43. opt_complementary = "h-km:k-hm:m-hk";
  44. opt = getopt32(argv, "hmk");
  45. if (opt & 1) {
  46. df_disp_hr = 0;
  47. disp_units_hdr = " Size";
  48. }
  49. if (opt & 2) {
  50. df_disp_hr = 1024*1024;
  51. disp_units_hdr = "1M-blocks";
  52. }
  53. #else
  54. opt = getopt32(argv, "k");
  55. #endif
  56. printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
  57. "", disp_units_hdr);
  58. mount_table = NULL;
  59. argv += optind;
  60. if (optind >= argc) {
  61. mount_table = setmntent(bb_path_mtab_file, "r");
  62. if (!mount_table) {
  63. bb_perror_msg_and_die(bb_path_mtab_file);
  64. }
  65. }
  66. while (1) {
  67. const char *device;
  68. const char *mount_point;
  69. if (mount_table) {
  70. mount_entry = getmntent(mount_table);
  71. if (!mount_entry) {
  72. endmntent(mount_table);
  73. break;
  74. }
  75. } else {
  76. mount_point = *argv++;
  77. if (!mount_point) {
  78. break;
  79. }
  80. mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
  81. if (!mount_entry) {
  82. bb_error_msg("%s: can't find mount point", mount_point);
  83. SET_ERROR:
  84. status = EXIT_FAILURE;
  85. continue;
  86. }
  87. }
  88. device = mount_entry->mnt_fsname;
  89. mount_point = mount_entry->mnt_dir;
  90. if (statfs(mount_point, &s) != 0) {
  91. bb_perror_msg("%s", mount_point);
  92. goto SET_ERROR;
  93. }
  94. if ((s.f_blocks > 0) || !mount_table){
  95. blocks_used = s.f_blocks - s.f_bfree;
  96. blocks_percent_used = 0;
  97. if (blocks_used + s.f_bavail) {
  98. blocks_percent_used = (blocks_used * 100ULL
  99. + (blocks_used + s.f_bavail)/2
  100. ) / (blocks_used + s.f_bavail);
  101. }
  102. if (strcmp(device, "rootfs") == 0) {
  103. continue;
  104. } else if (strcmp(device, "/dev/root") == 0) {
  105. /* Adjusts device to be the real root device,
  106. * or leaves device alone if it can't find it */
  107. device = find_block_device("/");
  108. if (!device) {
  109. goto SET_ERROR;
  110. }
  111. }
  112. if (printf("\n%-20s" + 1, device) > 20)
  113. printf("\n%-20s", "");
  114. #if ENABLE_FEATURE_HUMAN_READABLE
  115. printf(" %9s ",
  116. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  117. printf(" %9s " + 1,
  118. make_human_readable_str((s.f_blocks - s.f_bfree),
  119. s.f_bsize, df_disp_hr));
  120. printf("%9s %3u%% %s\n",
  121. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  122. blocks_percent_used, mount_point);
  123. #else
  124. printf(" %9lu %9lu %9lu %3u%% %s\n",
  125. kscale(s.f_blocks, s.f_bsize),
  126. kscale(s.f_blocks-s.f_bfree, s.f_bsize),
  127. kscale(s.f_bavail, s.f_bsize),
  128. blocks_percent_used, mount_point);
  129. #endif
  130. }
  131. }
  132. fflush_stdout_and_exit(status);
  133. }