/** * Cmon (c'mon) is a simple system resource monitor * Copyright (C) 2022 Aaron Ball * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "meminfo.h" int meminfo_init(struct meminfo* m) { int status=0; FILE* fd = fopen("/proc/meminfo", "r"); if (!fd) return 0; status += fscanf(fd, "MemTotal: %ld kB\n", &m->total); status += fscanf(fd, "MemFree: %ld kB\n", &m->free); status += fscanf(fd, "MemAvailable: %ld kB\n", &m->avail); status += fscanf(fd, "Buffers: %ld kB\n", &m->buffers); status += fscanf(fd, "Cached: %ld kB\n", &m->cached); // Custom value m->used = m->total - m->free - m->buffers - m->cached; fclose(fd); if(status > 5) { fprintf(stderr, "ERROR: Somehow read too many values from /proc/meminfo\n"); return 0; } return 1; }