summaryrefslogtreecommitdiff
path: root/src/meminfo.c
blob: dc3ead0e43a56fd4043ef3ad1da6a9dd938c38cd (plain)
    1 /**
    2  * Cmon (c'mon) is a simple system resource monitor
    3  * Copyright (C) 2022  Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17  */
   18 #include "meminfo.h"
   19 
   20 int meminfo_init(struct meminfo* m) {
   21   int status=0;
   22 
   23   FILE* fd = fopen("/proc/meminfo", "r");
   24   if (!fd)
   25     return 0;
   26 
   27   status += fscanf(fd, "MemTotal: %ld kB\n",     &m->total);
   28   status += fscanf(fd, "MemFree: %ld kB\n",      &m->free);
   29   status += fscanf(fd, "MemAvailable: %ld kB\n", &m->avail);
   30   status += fscanf(fd, "Buffers: %ld kB\n",      &m->buffers);
   31   status += fscanf(fd, "Cached: %ld kB\n",       &m->cached);
   32   // Custom value
   33   m->used = m->total - m->free - m->buffers - m->cached;
   34   fclose(fd);
   35 
   36   if(status > 5) {
   37     fprintf(stderr, "ERROR: Somehow read too many values from /proc/meminfo\n");
   38     return 0;
   39   }
   40   return 1;
   41 }

Generated by cgit