diff options
| author | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 18:10:13 +0100 |
|---|---|---|
| committer | Kacper <kacper@mail.openlinux.dev> | 2025-12-14 19:28:34 +0100 |
| commit | 872cf03f26c2801ae6c3008ce5fa0d7856f5f85d (patch) | |
| tree | 94809812b71ee286eb5b74c70e4d08fc4c8dc057 /bin/free/free.c | |
| parent | ec769a83bde09c76bd6ad9ee7f391036dba5cd97 (diff) | |
libc: implement err/warn functions
Diffstat (limited to 'bin/free/free.c')
| -rw-r--r-- | bin/free/free.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/bin/free/free.c b/bin/free/free.c new file mode 100644 index 00000000..b42ad99a --- /dev/null +++ b/bin/free/free.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <sys/sysinfo.h> +#include <unistd.h> + +int main(int argc, char **argv) +{ + int opt; + struct sysinfo info; + unsigned int shift; + + while ((opt = getopt(argc, argv, "hmtbs")) != -1) { + switch (opt) { + case 'b': /* bytes */ + shift = 0; + break; + case 'k': /* kibibytes */ + shift = 10; + break; + case 'm': /* mebibytes */ + shift = 20; + break; + case 'g': /* gibibytes */ + shift = 30; + break; + default: + goto usage; + } + } + + if (sysinfo(&info) < 0) + perror("sysinfo"); + + printf(" %13s%13s%13s%13s%13s\n", "total", "used", "free", "shared", + "buffers"); + + printf("Mem: %13llu%13llu%13llu%13llu%13llu\n", + (unsigned long long)((info.totalram * info.mem_unit) >> shift), + (unsigned long long)(((info.totalram - info.freeram) * + info.mem_unit) >> + shift), + (unsigned long long)((info.freeram * info.mem_unit) >> shift), + (unsigned long long)((info.sharedram * info.mem_unit) >> shift), + (unsigned long long)((info.bufferram * info.mem_unit) >> shift)); + + printf("-/+ buffers/cache:%13llu%13llu\n", + (unsigned long long)(((info.totalram - info.freeram - + info.bufferram) * + info.mem_unit) >> + shift), + (unsigned long long)(((info.freeram + info.bufferram) * + info.mem_unit) >> + shift)); + printf("Swap:%13llu%13llu%13llu\n", + (unsigned long long)((info.totalswap * info.mem_unit) >> shift), + (unsigned long long)(((info.totalswap - info.freeswap) * + info.mem_unit) >> + shift), + (unsigned long long)((info.freeswap * info.mem_unit) >> shift)); + + return 0; +usage: + write(STDOUT_FILENO, "usage: free [-hmtbs]\n", 21); + return 0; +} |
