More stuff.
[oweals/busybox.git] / util-linux / dmesg.c
1 #include "internal.h"
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5
6 /* dmesg.c -- Print out the contents of the kernel ring buffer
7  * Created: Sat Oct  9 16:19:47 1993
8  * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
9  * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
10  * This program comes with ABSOLUTELY NO WARRANTY.
11  * Modifications by Rick Sladkey (jrs@world.std.com)
12  * from util-linux; adapted for busybox
13  */
14
15 #include <linux/unistd.h>
16 #include <stdio.h>
17
18 #define __NR_klog __NR_syslog
19
20 #if defined(__GLIBC__)
21 #include <sys/klog.h>
22 #define klog klogctl
23 #else
24 static inline _syscall3 (int, klog, int, type, char *, b, int, len)
25 #endif                          /* __GLIBC__ */
26
27
28
29 static const char dmesg_usage[] = "dmesg [-c] [-n level]\n";
30
31 int dmesg_main (int argc, char **argv)
32 {
33
34     char buf[4096];
35     int i;
36     int n;
37     int level = 0;
38     int lastc;
39     int cmd = 3;
40
41     argc--;
42     argv++;
43
44     /* Parse any options */
45     while (argc && **argv == '-') {
46         while (*++(*argv))
47             switch (**argv) {
48             case 'c':
49                 cmd = 4;
50                 break;
51             case 'n':
52                 cmd = 8;
53                 if (--argc == 0)
54                     goto end;
55                 level = atoi (*(++argv));
56                 --argc;
57                 ++argv;
58                 break;
59             default:
60                 goto end;
61             }
62     }
63
64     if (cmd == 8) {
65         n = klog (cmd, NULL, level);
66         if (n < 0) {
67             perror ("klog");
68             exit (FALSE);
69         }
70         exit (TRUE);
71     }
72
73     n = klog (cmd, buf, sizeof (buf));
74     if (n < 0) {
75         perror ("klog");
76         exit (FALSE);
77     }
78
79     lastc = '\n';
80     for (i = 0; i < n; i++) {
81         if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
82             i++;
83             while (buf[i] >= '0' && buf[i] <= '9')
84                 i++;
85             if (buf[i] == '>')
86                 i++;
87         }
88         lastc = buf[i];
89         putchar (lastc);
90     }
91     if (lastc != '\n')
92         putchar ('\n');
93     exit (TRUE);
94
95   end:
96     usage( dmesg_usage);
97     exit (FALSE);
98 }