/* Compute the free memory on a Linux system with /proc/meminfo */ %{ #include #include static int mem_free = 0, buffers = 0, cached = 0; %} DIGITS [0-9]+ SP [ \t]+ %% ^MemFree:{SP}{DIGITS} { mem_free = atoi(yytext + 8); } ^Buffers:{SP}{DIGITS} { buffers = atoi(yytext + 8); } ^Cached:{SP}{DIGITS} { cached = atoi(yytext + 7); } .|\n { } %% int main(void) { const char meminfo[] = "/proc/meminfo"; yyin = fopen(meminfo, "r"); if (!yyin) { fprintf(stderr, "Cannot open %s\n", meminfo); return 1; } int rc = yylex(); if (rc) return rc; printf("%dm\n", (mem_free + buffers + cached) >> 10); return 0; }