3
0

df.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <mntent.h>
  23. #include <sys/vfs.h>
  24. #include "busybox.h"
  25. #ifndef CONFIG_FEATURE_HUMAN_READABLE
  26. static long kscale(long b, long bs)
  27. {
  28. return ( b * (long long) bs + 1024/2 ) / 1024;
  29. }
  30. #endif
  31. int df_main(int argc, char **argv)
  32. {
  33. long blocks_used;
  34. long blocks_percent_used;
  35. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  36. unsigned long df_disp_hr = 1024;
  37. #endif
  38. int status = EXIT_SUCCESS;
  39. unsigned opt;
  40. FILE *mount_table;
  41. struct mntent *mount_entry;
  42. struct statfs s;
  43. static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
  44. const char *disp_units_hdr = hdr_1k;
  45. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  46. opt_complementary = "h-km:k-hm:m-hk";
  47. opt = getopt32(argc, argv, "hmk");
  48. if (opt & 1) {
  49. df_disp_hr = 0;
  50. disp_units_hdr = " Size";
  51. }
  52. if (opt & 2) {
  53. df_disp_hr = 1024*1024;
  54. disp_units_hdr = "1M-blocks";
  55. }
  56. #else
  57. opt = getopt32(argc, argv, "k");
  58. #endif
  59. printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
  60. "", disp_units_hdr);
  61. mount_table = NULL;
  62. argv += optind;
  63. if (optind >= argc) {
  64. mount_table = setmntent(bb_path_mtab_file, "r");
  65. if (!mount_table) {
  66. bb_perror_msg_and_die(bb_path_mtab_file);
  67. }
  68. }
  69. do {
  70. const char *device;
  71. const char *mount_point;
  72. if (mount_table) {
  73. mount_entry = getmntent(mount_table);
  74. if (!mount_entry) {
  75. endmntent(mount_table);
  76. break;
  77. }
  78. } else {
  79. mount_point = *argv++;
  80. if (!mount_point) {
  81. break;
  82. }
  83. mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
  84. if (!mount_entry) {
  85. bb_error_msg("%s: can't find mount point", mount_point);
  86. SET_ERROR:
  87. status = EXIT_FAILURE;
  88. continue;
  89. }
  90. }
  91. device = mount_entry->mnt_fsname;
  92. mount_point = mount_entry->mnt_dir;
  93. if (statfs(mount_point, &s) != 0) {
  94. bb_perror_msg("%s", mount_point);
  95. goto SET_ERROR;
  96. }
  97. if ((s.f_blocks > 0) || !mount_table){
  98. blocks_used = s.f_blocks - s.f_bfree;
  99. blocks_percent_used = 0;
  100. if (blocks_used + s.f_bavail) {
  101. blocks_percent_used = (((long long) blocks_used) * 100
  102. + (blocks_used + s.f_bavail)/2
  103. ) / (blocks_used + s.f_bavail);
  104. }
  105. if (strcmp(device, "rootfs") == 0) {
  106. continue;
  107. } else if (strcmp(device, "/dev/root") == 0) {
  108. /* Adjusts device to be the real root device,
  109. * or leaves device alone if it can't find it */
  110. device = find_block_device("/");
  111. if (!device) {
  112. goto SET_ERROR;
  113. }
  114. }
  115. #ifdef CONFIG_FEATURE_HUMAN_READABLE
  116. printf("%-20s %9s ", device,
  117. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  118. printf("%9s ",
  119. make_human_readable_str( (s.f_blocks - s.f_bfree),
  120. s.f_bsize, df_disp_hr));
  121. printf("%9s %3ld%% %s\n",
  122. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  123. blocks_percent_used, mount_point);
  124. #else
  125. printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
  126. device,
  127. kscale(s.f_blocks, s.f_bsize),
  128. kscale(s.f_blocks-s.f_bfree, s.f_bsize),
  129. kscale(s.f_bavail, s.f_bsize),
  130. blocks_percent_used, mount_point);
  131. #endif
  132. }
  133. } while (1);
  134. fflush_stdout_and_exit(status);
  135. }