/* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * Neither the name of the nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ /* * parametry * -v delsi vypis * -s simple - usporny vypis * -l[label] vlastni nadpis * -p vypis v procentech */ #include #include #include #include #include #include #define MAX_LABEL_LENGTH 32 #define MEMORY_INFO_PERCENT 1 #define MEMORY_INFO_VERBOSE 2 static void show_help(void) { puts("parametry:"); puts(" -p zobrazi hodnoty v procentech"); puts(" -v podrobnejsi vypis"); puts(" -s kratky vypis"); puts(" -l "); } static int print_output(const char *szlabel, uint_fast32_t params) { FILE *pfile = fopen("/proc/meminfo", "r"); if (NULL == pfile) { printf("ERROR"); return EXIT_FAILURE; } char line[256]; unsigned int mem_total = 0; unsigned int mem_avail = 0; unsigned int swap_total = 0; unsigned int swap_free = 0; unsigned int end = 0; while (fgets(line, sizeof(line), pfile)) { if (sscanf(line, "MemTotal: %u kB", &mem_total) == 1) { end |= 1; continue; } if (sscanf(line, "MemAvailable: %u kB", &mem_avail) == 1) { end |= 2; continue; } if (sscanf(line, "SwapTotal: %u kB", &swap_total) == 1) { end |= 4; continue; } if (sscanf(line, "SwapFree: %u kB", &swap_free) == 1) { end |= 8; continue; } if (0xF == end) break; } fclose(pfile); if (0 == mem_total) return EXIT_FAILURE; char sztext[32]; char outbuff[64 + MAX_LABEL_LENGTH]; *sztext = 0; *outbuff = 0; if ((NULL != szlabel) && (*szlabel != 0)) { strncat(outbuff, szlabel, sizeof(outbuff) - strlen(outbuff) - 1); strncat(outbuff, " ", sizeof(outbuff) - strlen(outbuff) - 1); } if (MEMORY_INFO_PERCENT & params) { sprintf(sztext, "%u", 100*(mem_total - mem_avail) / mem_total); strncat(sztext, "%", sizeof(outbuff) - strlen(outbuff) - 1); } else { sprintf(sztext, (MEMORY_INFO_VERBOSE & params) ? "%.2f GiB" : "%.2f", (double)(mem_total - mem_avail) / (1024 * 1024)); } strncat(outbuff, sztext, sizeof(outbuff) - strlen(outbuff) - 1); if (0 == swap_total) return EXIT_SUCCESS; if (MEMORY_INFO_PERCENT & params) { sprintf(sztext, " %u", 100*(swap_total - swap_free) / swap_total); strncat(sztext, "%", sizeof(outbuff) - strlen(outbuff) - 1); } else sprintf(sztext, (MEMORY_INFO_VERBOSE & params) ? " - %.2f GiB" : " %.2f G", (double)(swap_total - swap_free) / (1024 * 1024)); strncat(outbuff, sztext, sizeof(outbuff) - strlen(outbuff) - 1); puts(outbuff); return EXIT_SUCCESS; } int main(int argc, char **argv) { int opt; uint_fast32_t params = 0; char szlabel[MAX_LABEL_LENGTH]; *szlabel = 0; if (argc > 1) { while ((opt = getopt(argc, argv, "vsphl:")) != -1) { switch (opt) { case 'l': if (strlen(optarg) <= sizeof(szlabel) - 1) strncpy(szlabel, optarg, sizeof(szlabel) - strlen(szlabel) - 1); break; case 'p': params |= MEMORY_INFO_PERCENT; break; case 's': params &= ~MEMORY_INFO_VERBOSE; break; case 'v': params |= MEMORY_INFO_VERBOSE; break; case 'h': show_help(); return EXIT_SUCCESS; break; } } } return print_output(szlabel, params); }