diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
commit | 2f0649f6fe411d7e07c8d56cf8ea56db53536da8 (patch) | |
tree | 778611fb52176dce1ad06c68e87b2cb348ca0f7b /usr/utils/dmesg.c | |
parent | Initial commit. (diff) | |
download | klibc-upstream/2.0.13.tar.xz klibc-upstream/2.0.13.zip |
Adding upstream version 2.0.13.upstream/2.0.13upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | usr/utils/dmesg.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/usr/utils/dmesg.c b/usr/utils/dmesg.c new file mode 100644 index 0000000..1960713 --- /dev/null +++ b/usr/utils/dmesg.c @@ -0,0 +1,79 @@ +#include <unistd.h> +#include <stdio.h> +#include <ctype.h> +#include <stdlib.h> +#include <sys/klog.h> + +static void usage(char *name) +{ + fprintf(stderr, "usage: %s [-c]\n", name); +} + +int main(int argc, char *argv[]) +{ + char *buf = NULL; + const char *p; + int c; + int bufsz = 0; + int cmd = 3; /* Read all messages remaining in the ring buffer */ + int len = 0; + int opt; + int newline; + + while ((opt = getopt(argc, argv, "c")) != -1) { + switch (opt) { + /* Read and clear all messages remaining in the ring buffer */ + case 'c': + cmd = 4; + break; + case '?': + default: + usage(argv[0]); + exit(1); + } + } + + if (!bufsz) { + len = klogctl(10, NULL, 0); /* Get size of log buffer */ + if (len > 0) + bufsz = len; + } + + if (bufsz) { + int sz = bufsz + 8; + + buf = (char *)malloc(sz); + len = klogctl(cmd, buf, sz); + } + + if (len < 0) { + perror("klogctl"); + exit(1); + } + + newline = 1; + p = buf; + while ((c = *p)) { + switch (c) { + case '\n': + newline = 1; + putchar(c); + p++; + break; + case '<': + if (newline && isdigit(p[1]) && p[2] == '>') { + p += 3; + break; + } + /* else fall through */ + default: + newline = 0; + putchar(c); + p++; + } + } + if (!newline) + putchar('\n'); + + return 0; +} |