free.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini free implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under the GPL version 2, see the file LICENSE in this tarball.
  8. */
  9. /* getopt not needed */
  10. #include "libbb.h"
  11. int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  12. int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
  13. {
  14. struct sysinfo info;
  15. unsigned mem_unit;
  16. #if ENABLE_DESKTOP
  17. if (argv[1] && argv[1][0] == '-')
  18. bb_show_usage();
  19. #endif
  20. sysinfo(&info);
  21. /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
  22. mem_unit = 1;
  23. if (info.mem_unit != 0) {
  24. mem_unit = info.mem_unit;
  25. }
  26. /* Convert values to kbytes */
  27. if (mem_unit == 1) {
  28. info.totalram >>= 10;
  29. info.freeram >>= 10;
  30. #if BB_MMU
  31. info.totalswap >>= 10;
  32. info.freeswap >>= 10;
  33. #endif
  34. info.sharedram >>= 10;
  35. info.bufferram >>= 10;
  36. } else {
  37. mem_unit >>= 10;
  38. /* TODO: Make all this stuff not overflow when mem >= 4 Tb */
  39. info.totalram *= mem_unit;
  40. info.freeram *= mem_unit;
  41. #if BB_MMU
  42. info.totalswap *= mem_unit;
  43. info.freeswap *= mem_unit;
  44. #endif
  45. info.sharedram *= mem_unit;
  46. info.bufferram *= mem_unit;
  47. }
  48. printf(" %13s%13s%13s%13s%13s\n",
  49. "total",
  50. "used",
  51. "free",
  52. "shared", "buffers" /* swap and total don't have these columns */
  53. );
  54. printf("%6s%13lu%13lu%13lu%13lu%13lu\n", "Mem:",
  55. info.totalram,
  56. info.totalram - info.freeram,
  57. info.freeram,
  58. info.sharedram, info.bufferram
  59. );
  60. #if BB_MMU
  61. printf("%6s%13lu%13lu%13lu\n", "Swap:",
  62. info.totalswap,
  63. info.totalswap - info.freeswap,
  64. info.freeswap
  65. );
  66. printf("%6s%13lu%13lu%13lu\n", "Total:",
  67. info.totalram + info.totalswap,
  68. (info.totalram - info.freeram) + (info.totalswap - info.freeswap),
  69. info.freeram + info.freeswap
  70. );
  71. #endif
  72. return EXIT_SUCCESS;
  73. }