free.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* getopt not needed */
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include "busybox.h"
  27. int free_main(int argc, char **argv)
  28. {
  29. struct sysinfo info;
  30. sysinfo(&info);
  31. /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
  32. if (info.mem_unit==0) {
  33. info.mem_unit=1;
  34. }
  35. if ( info.mem_unit == 1 ) {
  36. info.mem_unit=1024;
  37. /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
  38. info.totalram/=info.mem_unit;
  39. info.freeram/=info.mem_unit;
  40. #ifndef __uClinux__
  41. info.totalswap/=info.mem_unit;
  42. info.freeswap/=info.mem_unit;
  43. #endif
  44. info.sharedram/=info.mem_unit;
  45. info.bufferram/=info.mem_unit;
  46. } else {
  47. info.mem_unit/=1024;
  48. /* TODO: Make all this stuff not overflow when mem >= 4 Gib */
  49. info.totalram*=info.mem_unit;
  50. info.freeram*=info.mem_unit;
  51. #ifndef __uClinux__
  52. info.totalswap*=info.mem_unit;
  53. info.freeswap*=info.mem_unit;
  54. #endif
  55. info.sharedram*=info.mem_unit;
  56. info.bufferram*=info.mem_unit;
  57. }
  58. if (argc > 1 && **(argv + 1) == '-')
  59. bb_show_usage();
  60. printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
  61. "shared", "buffers");
  62. printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
  63. info.totalram-info.freeram, info.freeram,
  64. info.sharedram, info.bufferram);
  65. #ifndef __uClinux__
  66. printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
  67. info.totalswap-info.freeswap, info.freeswap);
  68. printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
  69. (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
  70. info.freeram+info.freeswap);
  71. #endif
  72. return EXIT_SUCCESS;
  73. }