df.c 8.2 KB

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