get_mem.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "assert.h"
00005 #include "xnet.h"
00006 
00007 static unsigned long long bytes[2];
00008 
00009 int __GetMemPoint()
00010 {
00011         FILE *F1 = NULL;
00012         char line[1025] = { '\0' };
00013         char *word = NULL;
00014         unsigned long long mem_total = 0;
00015         unsigned long long mem_free = 0;
00016         unsigned long long mem_used = 0;
00017 
00018         F1 = fopen("/proc/meminfo", "r");
00019         if (F1 == NULL) {
00020                 perror ("/proc/meminfo\n");
00021                 return(1);
00022         }
00023 
00024         while (!feof(F1)) {
00025                 fgets(line, 1024, F1);
00026 
00027                 if (feof(F1))
00028                         break;
00029 
00030                 word = strtok(line, ": ");
00031                 if (word != NULL && !strcasecmp(word, "MemTotal")) {
00032                         word = strtok(NULL, " ");
00033                         if (word != NULL) {
00034                                 /* fprintf(stderr, "%s ", word); /**/
00035                                 sscanf(word, "%llu", &mem_total);
00036                                 mem_total *= 1024;
00037                         }
00038                         else {
00039                                 perror("strtok()\n");
00040                         }
00041                 }
00042                 else if (word != NULL && !strcasecmp(word, "MemFree")) {
00043                         word = strtok(NULL, " ");
00044                         if (word != NULL) {
00045                                 /* fprintf(stderr, "%s ", word); /**/
00046                                 sscanf(word, "%llu", &mem_free);
00047                                 mem_free *= 1024;
00048                         }
00049                         else {
00050                                 perror("strtok()\n");
00051                         }
00052                 }
00053         }
00054         fclose(F1);
00055 
00056         bytes[0] = bytes[1];
00057         bytes[1] = mem_total - mem_free;
00058 
00059         /* fprintf(stderr, "%llu %llu %llu %llu\n", mem_total, mem_free, bytes[0], bytes[1]); /**/
00060 
00061         return(0);
00062 }
00063 
00064 void InitMemPoint()
00065 {
00066         bytes[0] = 0;
00067         bytes[1] = 0;
00068         __GetMemPoint();
00069         __GetMemPoint();
00070 }
00071 
00072 void GetMemPoint( w, closure, call_data )
00073      Widget     w;
00074      XtPointer  closure;
00075      XtPointer  call_data;
00076 {
00077         unsigned long long ull_value = 0;
00078         int i_value = 0;
00079 
00080         ASSERT(w != NULL);
00081         ASSERT(call_data != NULL);
00082 
00083         __GetMemPoint();
00084 
00085         ASSERT(w != NULL);
00086 
00087         if (bytes[1] > bytes[0]) {
00088                 ull_value = bytes[1] - bytes[0];
00089         }
00090         else {
00091                 ull_value = bytes[0] - bytes[1];
00092         }
00093         /* fprintf(stderr, "%llu ", ull_value); /**/
00094 
00095         i_value = (int)ull_value;
00096         /* fprintf(stderr, "%d ", i_value); /**/
00097 
00098         /* fprintf(stderr, "\n"); /**/
00099 
00100         *(int *)call_data = i_value;
00101 
00102         ASSERT(w != NULL);
00103 }
00104 

Generated on Mon May 8 16:47:49 2006 for xmonitors by  doxygen 1.4.2