suppress warnings about easch <applet>_main() having
[oweals/busybox.git] / util-linux / dmesg.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *
4  * dmesg - display/control kernel ring buffer.
5  *
6  * Copyright 2006 Rob Landley <rob@landley.net>
7  * Copyright 2006 Bernhard Fischer <rep.nop@aon.at>
8  *
9  * Licensed under GPLv2, see file LICENSE in this tarball for details.
10  */
11
12 #include "busybox.h"
13 #include <unistd.h>
14 #include <sys/klog.h>
15
16 int dmesg_main(int argc, char *argv[]);
17 int dmesg_main(int argc, char *argv[])
18 {
19         char *size, *level;
20         int flags = getopt32(argc, argv, "cs:n:", &size, &level);
21
22         if (flags & 4) {
23                 if (klogctl(8, NULL, xatoul_range(level, 0, 10)))
24                         bb_perror_msg_and_die("klogctl");
25         } else {
26                 int len;
27                 char *buf;
28
29                 len = (flags & 2) ? xatoul_range(size, 2, INT_MAX) : 16384;
30                 buf = xmalloc(len);
31                 if (0 > (len = klogctl(3 + (flags & 1), buf, len)))
32                         bb_perror_msg_and_die("klogctl");
33
34                 // Skip <#> at the start of lines, and make sure we end with a newline.
35
36                 if (ENABLE_FEATURE_DMESG_PRETTY) {
37                         int last = '\n';
38                         int in;
39
40                         for (in = 0; in<len;) {
41                                 if (last == '\n' && buf[in] == '<') in += 3;
42                                 else putchar(last = buf[in++]);
43                         }
44                         if (last != '\n') putchar('\n');
45                 } else {
46                         write(1,buf,len);
47                         if (len && buf[len-1]!='\n') putchar('\n');
48                 }
49
50                 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
51         }
52
53         return 0;
54 }