df.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 source tree.
  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. //usage:#define df_trivial_usage
  23. //usage: "[-Pk"
  24. //usage: IF_FEATURE_HUMAN_READABLE("mh")
  25. //usage: IF_FEATURE_DF_FANCY("ai] [-B SIZE")
  26. //usage: "] [FILESYSTEM]..."
  27. //usage:#define df_full_usage "\n\n"
  28. //usage: "Print filesystem usage statistics\n"
  29. //usage: "\n -P POSIX output format"
  30. //usage: "\n -k 1024-byte blocks (default)"
  31. //usage: IF_FEATURE_HUMAN_READABLE(
  32. //usage: "\n -m 1M-byte blocks"
  33. //usage: "\n -h Human readable (e.g. 1K 243M 2G)"
  34. //usage: )
  35. //usage: IF_FEATURE_DF_FANCY(
  36. //usage: "\n -a Show all filesystems"
  37. //usage: "\n -i Inodes"
  38. //usage: "\n -B SIZE Blocksize"
  39. //usage: )
  40. //usage:
  41. //usage:#define df_example_usage
  42. //usage: "$ df\n"
  43. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  44. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  45. //usage: "/dev/sda1 64216 36364 27852 57% /boot\n"
  46. //usage: "$ df /dev/sda3\n"
  47. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  48. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  49. //usage: "$ POSIXLY_CORRECT=sure df /dev/sda3\n"
  50. //usage: "Filesystem 512B-blocks Used Available Use% Mounted on\n"
  51. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  52. //usage: "$ POSIXLY_CORRECT=yep df -P /dev/sda3\n"
  53. //usage: "Filesystem 512-blocks Used Available Capacity Mounted on\n"
  54. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  55. #include <mntent.h>
  56. #include <sys/vfs.h>
  57. #include "libbb.h"
  58. #include "unicode.h"
  59. #if !ENABLE_FEATURE_HUMAN_READABLE
  60. static unsigned long kscale(unsigned long b, unsigned long bs)
  61. {
  62. return (b * (unsigned long long) bs + 1024/2) / 1024;
  63. }
  64. #endif
  65. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  66. int df_main(int argc UNUSED_PARAM, char **argv)
  67. {
  68. unsigned long blocks_used;
  69. unsigned blocks_percent_used;
  70. unsigned long df_disp_hr = 1024;
  71. int status = EXIT_SUCCESS;
  72. unsigned opt;
  73. FILE *mount_table;
  74. struct mntent *mount_entry;
  75. struct statfs s;
  76. enum {
  77. OPT_KILO = (1 << 0),
  78. OPT_POSIX = (1 << 1),
  79. OPT_ALL = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
  80. OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
  81. OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  82. OPT_HUMAN = (1 << (2 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  83. OPT_MEGA = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  84. };
  85. const char *disp_units_hdr = NULL;
  86. char *chp;
  87. init_unicode();
  88. #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
  89. opt_complementary = "k-mB:m-Bk:B-km";
  90. #elif ENABLE_FEATURE_HUMAN_READABLE
  91. opt_complementary = "k-m:m-k";
  92. #endif
  93. opt = getopt32(argv, "kP"
  94. IF_FEATURE_DF_FANCY("aiB:")
  95. IF_FEATURE_HUMAN_READABLE("hm")
  96. IF_FEATURE_DF_FANCY(, &chp));
  97. if (opt & OPT_MEGA)
  98. df_disp_hr = 1024*1024;
  99. if (opt & OPT_BSIZE)
  100. df_disp_hr = xatoul_range(chp, 1, ULONG_MAX); /* disallow 0 */
  101. /* From the manpage of df from coreutils-6.10:
  102. * Disk space is shown in 1K blocks by default, unless the environment
  103. * variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
  104. */
  105. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  106. df_disp_hr = 512;
  107. if (opt & OPT_HUMAN) {
  108. df_disp_hr = 0;
  109. disp_units_hdr = " Size";
  110. }
  111. if (opt & OPT_INODE)
  112. disp_units_hdr = " Inodes";
  113. if (disp_units_hdr == NULL) {
  114. #if ENABLE_FEATURE_HUMAN_READABLE
  115. disp_units_hdr = xasprintf("%s-blocks",
  116. /* print df_disp_hr, show no fractionals,
  117. * use suffixes if OPT_POSIX is set in opt */
  118. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
  119. );
  120. #else
  121. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  122. #endif
  123. }
  124. printf("Filesystem %-15sUsed Available %s Mounted on\n",
  125. disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
  126. mount_table = NULL;
  127. argv += optind;
  128. if (!argv[0]) {
  129. mount_table = setmntent(bb_path_mtab_file, "r");
  130. if (!mount_table)
  131. bb_perror_msg_and_die(bb_path_mtab_file);
  132. }
  133. while (1) {
  134. const char *device;
  135. const char *mount_point;
  136. if (mount_table) {
  137. mount_entry = getmntent(mount_table);
  138. if (!mount_entry) {
  139. endmntent(mount_table);
  140. break;
  141. }
  142. } else {
  143. mount_point = *argv++;
  144. if (!mount_point)
  145. break;
  146. mount_entry = find_mount_point(mount_point, 1);
  147. if (!mount_entry) {
  148. bb_error_msg("%s: can't find mount point", mount_point);
  149. set_error:
  150. status = EXIT_FAILURE;
  151. continue;
  152. }
  153. }
  154. device = mount_entry->mnt_fsname;
  155. mount_point = mount_entry->mnt_dir;
  156. if (statfs(mount_point, &s) != 0) {
  157. bb_simple_perror_msg(mount_point);
  158. goto set_error;
  159. }
  160. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  161. if (opt & OPT_INODE) {
  162. s.f_blocks = s.f_files;
  163. s.f_bavail = s.f_bfree = s.f_ffree;
  164. s.f_bsize = 1;
  165. if (df_disp_hr)
  166. df_disp_hr = 1;
  167. }
  168. blocks_used = s.f_blocks - s.f_bfree;
  169. blocks_percent_used = 0;
  170. if (blocks_used + s.f_bavail) {
  171. blocks_percent_used = (blocks_used * 100ULL
  172. + (blocks_used + s.f_bavail)/2
  173. ) / (blocks_used + s.f_bavail);
  174. }
  175. /* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
  176. if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
  177. continue;
  178. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  179. if (strcmp(device, "/dev/root") == 0) {
  180. /* Adjusts device to be the real root device,
  181. * or leaves device alone if it can't find it */
  182. device = find_block_device("/");
  183. if (!device) {
  184. goto set_error;
  185. }
  186. }
  187. #endif
  188. #if ENABLE_UNICODE_SUPPORT
  189. {
  190. uni_stat_t uni_stat;
  191. char *uni_dev = unicode_conv_to_printable(&uni_stat, device);
  192. if (uni_stat.unicode_width > 20 && !(opt & OPT_POSIX)) {
  193. printf("%s\n%20s", uni_dev, "");
  194. } else {
  195. printf("%s%*s", uni_dev, 20 - (int)uni_stat.unicode_width, "");
  196. }
  197. free(uni_dev);
  198. }
  199. #else
  200. if (printf("\n%-20s" + 1, device) > 20 && !(opt & OPT_POSIX))
  201. printf("\n%-20s", "");
  202. #endif
  203. #if ENABLE_FEATURE_HUMAN_READABLE
  204. printf(" %9s ",
  205. /* f_blocks x f_bsize / df_disp_hr, show one fractional,
  206. * use suffixes if df_disp_hr == 0 */
  207. make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
  208. printf(" %9s " + 1,
  209. /* EXPR x f_bsize / df_disp_hr, show one fractional,
  210. * use suffixes if df_disp_hr == 0 */
  211. make_human_readable_str((s.f_blocks - s.f_bfree),
  212. s.f_bsize, df_disp_hr));
  213. printf("%9s %3u%% %s\n",
  214. /* f_bavail x f_bsize / df_disp_hr, show one fractional,
  215. * use suffixes if df_disp_hr == 0 */
  216. make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
  217. blocks_percent_used, mount_point);
  218. #else
  219. printf(" %9lu %9lu %9lu %3u%% %s\n",
  220. kscale(s.f_blocks, s.f_bsize),
  221. kscale(s.f_blocks - s.f_bfree, s.f_bsize),
  222. kscale(s.f_bavail, s.f_bsize),
  223. blocks_percent_used, mount_point);
  224. #endif
  225. }
  226. }
  227. return status;
  228. }