summaryrefslogtreecommitdiffstats
path: root/usr/utils/dmesg.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:06:04 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:06:04 +0000
commit2f0649f6fe411d7e07c8d56cf8ea56db53536da8 (patch)
tree778611fb52176dce1ad06c68e87b2cb348ca0f7b /usr/utils/dmesg.c
parentInitial commit. (diff)
downloadklibc-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.c79
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;
+}