summaryrefslogtreecommitdiffstats
path: root/usr/utils/dmesg.c
blob: 19607132b224b286bafe74ca18fe76f1ed3d1994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
}