3
0

free.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, char **argv)
  13. {
  14. struct sysinfo info;
  15. sysinfo(&info);
  16. /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
  17. if (info.mem_unit == 0) {
  18. info.mem_unit=1;
  19. }
  20. if (info.mem_unit == 1) {
  21. info.mem_unit=1024;
  22. /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
  23. info.totalram/=info.mem_unit;
  24. info.freeram/=info.mem_unit;
  25. #ifndef __uClinux__
  26. info.totalswap/=info.mem_unit;
  27. info.freeswap/=info.mem_unit;
  28. #endif
  29. info.sharedram/=info.mem_unit;
  30. info.bufferram/=info.mem_unit;
  31. } else {
  32. info.mem_unit/=1024;
  33. /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
  34. info.totalram*=info.mem_unit;
  35. info.freeram*=info.mem_unit;
  36. #ifndef __uClinux__
  37. info.totalswap*=info.mem_unit;
  38. info.freeswap*=info.mem_unit;
  39. #endif
  40. info.sharedram*=info.mem_unit;
  41. info.bufferram*=info.mem_unit;
  42. }
  43. if (argc > 1 && *argv[1] == '-')
  44. bb_show_usage();
  45. printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
  46. "shared", "buffers");
  47. printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
  48. info.totalram-info.freeram, info.freeram,
  49. info.sharedram, info.bufferram);
  50. #ifndef __uClinux__
  51. printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
  52. info.totalswap-info.freeswap, info.freeswap);
  53. printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
  54. (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
  55. info.freeram+info.freeswap);
  56. #endif
  57. return EXIT_SUCCESS;
  58. }