df.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
  25. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  26. *
  27. * Size reduction. Removed floating point dependency. Added error checking
  28. * on output. Output stats on 0-sized filesystems if specifically listed on
  29. * the command line. Properly round *-blocks, Used, and Available quantities.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <mntent.h>
  36. #include <sys/vfs.h>
  37. #include "busybox.h"
  38. #ifndef CONFIG_FEATURE_HUMAN_READABLE
  39. static long kscale(long b, long bs)
  40. {
  41. return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
  42. }
  43. #endif
  44. extern int df_main(int argc, char **argv)
  45. {
  46. long blocks_used;
  47. long blocks_percent_used;
  48. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  49. unsigned long df_disp_hr = KILOBYTE;
  50. #endif
  51. int status = EXIT_SUCCESS;
  52. unsigned long opt;
  53. FILE *mount_table;
  54. struct mntent *mount_entry;
  55. struct statfs s;
  56. static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
  57. const char *disp_units_hdr = hdr_1k;
  58. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  59. bb_opt_complementaly = "h-km:k-hm:m-hk";
  60. opt = bb_getopt_ulflags(argc, argv, "hmk");
  61. if(opt & 1) {
  62. df_disp_hr = 0;
  63. disp_units_hdr = " Size";
  64. }
  65. if(opt & 2) {
  66. df_disp_hr = MEGABYTE;
  67. disp_units_hdr = "1M-blocks";
  68. }
  69. #else
  70. opt = bb_getopt_ulflags(argc, argv, "k");
  71. #endif
  72. bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
  73. "", disp_units_hdr);
  74. mount_table = NULL;
  75. argv += optind;
  76. if (optind >= argc) {
  77. if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
  78. bb_perror_msg_and_die(bb_path_mtab_file);
  79. }
  80. }
  81. do {
  82. const char *device;
  83. const char *mount_point;
  84. if (mount_table) {
  85. if (!(mount_entry = getmntent(mount_table))) {
  86. endmntent(mount_table);
  87. break;
  88. }
  89. } else {
  90. if (!(mount_point = *argv++)) {
  91. break;
  92. }
  93. if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) {
  94. bb_error_msg("%s: can't find mount point.", mount_point);
  95. SET_ERROR:
  96. status = EXIT_FAILURE;
  97. continue;
  98. }
  99. }
  100. device = mount_entry->mnt_fsname;
  101. mount_point = mount_entry->mnt_dir;
  102. if (statfs(mount_point, &s) != 0) {
  103. bb_perror_msg("%s", mount_point);
  104. goto SET_ERROR;
  105. }
  106. if ((s.f_blocks > 0) || !mount_table){
  107. blocks_used = s.f_blocks - s.f_bfree;
  108. blocks_percent_used = 0;
  109. if (blocks_used + s.f_bavail) {
  110. blocks_percent_used = (((long long) blocks_used) * 100
  111. + (blocks_used + s.f_bavail)/2
  112. ) / (blocks_used + s.f_bavail);
  113. }
  114. if (strcmp(device, "rootfs") == 0) {
  115. continue;
  116. } else if (strcmp(device, "/dev/root") == 0) {
  117. /* Adjusts device to be the real root device,
  118. * or leaves device alone if it can't find it */
  119. if ((device = find_real_root_device_name()) == NULL) {
  120. goto SET_ERROR;
  121. }
  122. }
  123. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  124. bb_printf("%-21s%9s ", device,
  125. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  126. bb_printf("%9s ",
  127. make_human_readable_str( (s.f_blocks - s.f_bfree),
  128. s.f_bsize, df_disp_hr));
  129. bb_printf("%9s %3ld%% %s\n",
  130. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  131. blocks_percent_used, mount_point);
  132. #else
  133. bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
  134. device,
  135. kscale(s.f_blocks, s.f_bsize),
  136. kscale(s.f_blocks-s.f_bfree, s.f_bsize),
  137. kscale(s.f_bavail, s.f_bsize),
  138. blocks_percent_used, mount_point);
  139. #endif
  140. }
  141. } while (1);
  142. bb_fflush_stdout_and_exit(status);
  143. }
  144. /*
  145. Local Variables:
  146. c-file-style: "linux"
  147. c-basic-offset: 4
  148. tab-width: 4
  149. End:
  150. */