Remove trailing whitespace. Update copyright to include 2004.
[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 <andersen@codepoet.org>. I ripped out Native Language
15  * Support, replaced getopt, added some gotos for redundant stuff.
16  *
17  * Audited and cleaned up on 7 March 2003 to reduce size of
18  * check error handling by Erik Andersen <andersen@codepoet.org>
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24 #include <errno.h>
25 #include <sys/klog.h>
26
27 #include "busybox.h"
28
29 int dmesg_main(int argc, char **argv)
30 {
31         char *buf
32 #ifdef CONFIG_FEATURE_CLEAN_UP
33                 = NULL
34 #endif
35                 ;
36         int bufsize = 8196;
37         int i, n;
38         int level = 0;
39         int lastc;
40         int cmd = 3;
41
42         while ((i = getopt(argc, argv, "cn:s:")) > 0) {
43                 switch (i) {
44                         case 'c':
45                                 cmd = 4;
46                                 break;
47                         case 'n':
48                                 cmd = 8;
49                                 level = bb_xgetlarg(optarg, 10, 0, 10);
50                                 break;
51                         case 's':
52                                 /* I think a 512k max kernel ring buffer is big enough for
53                                  * anybody, as the default is 16k...  Could be wrong though.
54                                  * If so I'm sure I'll hear about it by the enraged masses*/
55                                 bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
56                                 break;
57                         default:
58                                 bb_show_usage();
59                 }
60         }
61
62         if (optind < argc) {
63                 bb_show_usage();
64         }
65
66         if (cmd == 8) {
67                 if (klogctl(cmd, NULL, level) < 0)
68                         goto die_the_death;
69                 goto all_done;
70         }
71
72         buf = xmalloc(bufsize);
73         if ((n = klogctl(cmd, buf, bufsize)) < 0)
74                 goto die_the_death;
75
76         lastc = '\n';
77         for (i = 0; i < n; i++) {
78                 if (lastc == '\n' && buf[i] == '<') {
79                         i++;
80                         while (buf[i] >= '0' && buf[i] <= '9')
81                                 i++;
82                         if (buf[i] == '>')
83                                 i++;
84                 }
85                 lastc = buf[i];
86                 putchar(lastc);
87         }
88         if (lastc != '\n')
89                 putchar('\n');
90 all_done:
91 #ifdef CONFIG_FEATURE_CLEAN_UP
92         if (buf) {
93                 free(buf);
94         }
95 #endif
96         return EXIT_SUCCESS;
97 die_the_death:
98         bb_perror_nomsg_and_die();
99 }