move remaining help text from include/usage.src.h
[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 Reutner-Fischer <rep.nop@aon.at>
8  *
9  * Licensed under GPLv2, see file LICENSE in this source tree.
10  */
11
12 //usage:#define dmesg_trivial_usage
13 //usage:       "[-c] [-n LEVEL] [-s SIZE]"
14 //usage:#define dmesg_full_usage "\n\n"
15 //usage:       "Print or control the kernel ring buffer\n"
16 //usage:     "\nOptions:"
17 //usage:     "\n        -c              Clear ring buffer after printing"
18 //usage:     "\n        -n LEVEL        Set console logging level"
19 //usage:     "\n        -s SIZE         Buffer size"
20
21 #include <sys/klog.h>
22 #include "libbb.h"
23
24 int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25 int dmesg_main(int argc UNUSED_PARAM, char **argv)
26 {
27         int len, level;
28         char *buf;
29         unsigned opts;
30         enum {
31                 OPT_c = 1 << 0,
32                 OPT_s = 1 << 1,
33                 OPT_n = 1 << 2
34         };
35
36         opt_complementary = "s+:n+"; /* numeric */
37         opts = getopt32(argv, "cs:n:", &len, &level);
38         if (opts & OPT_n) {
39                 if (klogctl(8, NULL, (long) level))
40                         bb_perror_msg_and_die("klogctl");
41                 return EXIT_SUCCESS;
42         }
43
44         if (!(opts & OPT_s))
45                 len = klogctl(10, NULL, 0); /* read ring buffer size */
46         if (len < 16*1024)
47                 len = 16*1024;
48         if (len > 16*1024*1024)
49                 len = 16*1024*1024;
50
51         buf = xmalloc(len);
52         len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
53         if (len < 0)
54                 bb_perror_msg_and_die("klogctl");
55         if (len == 0)
56                 return EXIT_SUCCESS;
57
58
59         if (ENABLE_FEATURE_DMESG_PRETTY) {
60                 int last = '\n';
61                 int in = 0;
62
63                 /* Skip <#> at the start of lines */
64                 while (1) {
65                         if (last == '\n' && buf[in] == '<') {
66                                 in += 3;
67                                 if (in >= len)
68                                         break;
69                         }
70                         last = buf[in];
71                         putchar(last);
72                         in++;
73                         if (in >= len)
74                                 break;
75                 }
76                 /* Make sure we end with a newline */
77                 if (last != '\n')
78                         bb_putchar('\n');
79         } else {
80                 full_write(STDOUT_FILENO, buf, len);
81                 if (buf[len-1] != '\n')
82                         bb_putchar('\n');
83         }
84
85         if (ENABLE_FEATURE_CLEAN_UP) free(buf);
86
87         return EXIT_SUCCESS;
88 }