1#include <stdlib.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <errno.h>
5#include <sys/klog.h>
6#include <string.h>
7
8#define KLOG_BUF_SHIFT	17	/* CONFIG_LOG_BUF_SHIFT from our kernel */
9#define KLOG_BUF_LEN	(1 << KLOG_BUF_SHIFT)
10
11int dmesg_main(int argc, char **argv)
12{
13    char buffer[KLOG_BUF_LEN + 1];
14    char *p = buffer;
15    ssize_t ret;
16    int n, op;
17
18    if((argc == 2) && (!strcmp(argv[1],"-c"))) {
19        op = KLOG_READ_CLEAR;
20    } else {
21        op = KLOG_READ_ALL;
22    }
23
24    n = klogctl(op, buffer, KLOG_BUF_LEN);
25    if (n < 0) {
26        perror("klogctl");
27        return EXIT_FAILURE;
28    }
29    buffer[n] = '\0';
30
31    while((ret = write(STDOUT_FILENO, p, n))) {
32        if (ret == -1) {
33	    if (errno == EINTR)
34                continue;
35	    perror("write");
36	    return EXIT_FAILURE;
37	}
38	p += ret;
39	n -= ret;
40    }
41
42    return 0;
43}
44