It turns out that DODMALLOC was broken when I reorganized busybox.h
[oweals/busybox.git] / util-linux / dmesg.c
1 /* vi: set sw=4 ts=4: */
2 /* dmesg.c -- Print out the contents of the kernel ring buffer
3  * Created: Sat Oct  9 16:19:47 1993
4  * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
5  * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
6  * This program comes with ABSOLUTELY NO WARRANTY.
7  * Modifications by Rick Sladkey (jrs@world.std.com)
8  * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch
9  * by Peeter Joot.  This was also suggested by John Hudson.
10  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
11  * - added Native Language Support
12  *
13  * from util-linux -- adapted for busybox by 
14  * Erik Andersen <andersee@debian.org>. I ripped out Native Language 
15  * Support, replaced getopt, added some gotos for redundant stuff.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <getopt.h>
21
22 #if __GNU_LIBRARY__ < 5
23 #include <sys/syscall.h>
24 #include <linux/unistd.h>
25 #ifndef __alpha__
26 # define __NR_klogctl __NR_syslog
27 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
28 #else                                                   /* __alpha__ */
29 #define klogctl syslog
30 #endif
31
32 #else
33 # include <sys/klog.h>
34 #endif
35 #include "busybox.h"
36
37 int dmesg_main(int argc, char **argv)
38 {
39         char *buf;
40         int c;
41         int bufsize = 8196;
42         int i;
43         int n;
44         int level = 0;
45         int lastc;
46         int cmd = 3;
47
48         while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
49                 switch (c) {
50                 case 'c':
51                         cmd = 4;
52                         break;
53                 case 'n':
54                         cmd = 8;
55                         if (optarg == NULL)
56                                 show_usage();
57                         level = atoi(optarg);
58                         break;
59                 case 's':
60                         if (optarg == NULL)
61                                 show_usage();
62                         bufsize = atoi(optarg);
63                         break;
64                 default:
65                         show_usage();
66                 }
67         }                       
68
69         if (optind < argc) {
70                 show_usage();
71         }
72
73         if (cmd == 8) {
74                 if (klogctl(cmd, NULL, level) < 0)
75                         perror_msg_and_die("klogctl");
76                 return EXIT_SUCCESS;
77         }
78
79         if (bufsize < 4096)
80                 bufsize = 4096;
81         buf = (char *) xmalloc(bufsize);
82         if ((n = klogctl(cmd, buf, bufsize)) < 0)
83                 perror_msg_and_die("klogctl");
84
85         lastc = '\n';
86         for (i = 0; i < n; i++) {
87                 if (lastc == '\n' && buf[i] == '<') {
88                         i++;
89                         while (buf[i] >= '0' && buf[i] <= '9')
90                                 i++;
91                         if (buf[i] == '>')
92                                 i++;
93                 }
94                 lastc = buf[i];
95                 putchar(lastc);
96         }
97         if (lastc != '\n')
98                 putchar('\n');
99         return EXIT_SUCCESS;
100 }