Mercurial > hg > toybox
annotate toys/other/free.c @ 1322:b91284c2e569 draft
Make "losetup /dev/loop0 filename" work.
Sigh. Implement the complex cases and you screw up the simple cases you already tested...
author  Rob Landley <rob@landley.net> 

date  Thu, 29 May 2014 06:29:12 0500 
parents  786841fdb1e0 
children  f0c4b7476118 
rev  line source 

694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

1 /* free.c  Display amount of free and used memory in the system. 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

2 * 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

4 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

5 USE_FREE(NEWTOY(free, "gmkb", TOYFLAG_USRTOYFLAG_BIN)) 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

6 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

7 config FREE 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

8 bool "free" 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

9 default y 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

10 help 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

11 usage: free [bkmg] 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

12 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

13 Display the total, free and used amount of physical memory and 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

14 swap space. 
528
878b94b32866
Comment and whitespace tweaks.
Rob Landley <rob@landley.net>
parents:
468
diff
changeset

15 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

16 bkmg Output in bytes (default), KB, MB or GB 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

17 */ 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

18 
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset

19 #define FOR_free 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

20 #include "toys.h" 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

21 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

22 static unsigned long long convert(unsigned long d, unsigned int iscale, 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

23 unsigned int oscale) 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

24 { 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

25 return ((unsigned long long)d*iscale)>>oscale; 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

26 } 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

27 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

28 void free_main(void) 
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

29 { 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

30 struct sysinfo info; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

31 unsigned int iscale = 1; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

32 unsigned int oscale = 0; 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

33 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

34 sysinfo(&info); 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

35 if (info.mem_unit) iscale = info.mem_unit; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

36 if (toys.optflags & FLAG_b) oscale = 0; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

37 if (toys.optflags & FLAG_k) oscale = 10; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

38 if (toys.optflags & FLAG_m) oscale = 20; 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

39 if (toys.optflags & FLAG_g) oscale = 30; 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

40 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

41 xprintf("\t\ttotal used free shared buffers\n"); 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

42 xprintf("Mem:%17llu%12llu%12llu%12llu%12llu\n", 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

43 convert(info.totalram, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

44 convert(info.totalraminfo.freeram, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

45 convert(info.freeram, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

46 convert(info.sharedram, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

47 convert(info.bufferram, iscale, oscale)); 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

48 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

49 xprintf("/+ buffers/cache:%15llu%12llu\n", 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

50 convert(info.totalram  info.freeram  info.bufferram, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

51 convert(info.freeram + info.bufferram, iscale, oscale)); 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

52 
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

53 xprintf("Swap:%16llu%12llu%12llu\n", 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

54 convert(info.totalswap, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

55 convert(info.totalswap  info.freeswap, iscale, oscale), 
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Rewordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset

56 convert(info.freeswap, iscale, oscale)); 
466
9f1e089262cb
Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff
changeset

57 } 