Extract usage information into a separate file.
[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 "internal.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #if __GNU_LIBRARY__ < 5
23
24 #ifndef __alpha__
25 # define __NR_klogctl __NR_syslog
26 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
27 #else                                                   /* __alpha__ */
28 #define klogctl syslog
29 #endif
30
31 #else
32 # include <sys/klog.h>
33 #endif
34
35 int dmesg_main(int argc, char **argv)
36 {
37         char *buf, c;
38         int bufsize = 8196;
39         int i;
40         int n;
41         int level = 0;
42         int lastc;
43         int cmd = 3;
44
45         while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
46                 switch (c) {
47                 case 'c':
48                         cmd = 4;
49                         break;
50                 case 'n':
51                         cmd = 8;
52                         if (optarg == NULL)
53                                 usage(dmesg_usage);
54                         level = atoi(optarg);
55                         break;
56                 case 's':
57                         if (optarg == NULL)
58                                 usage(dmesg_usage);
59                         bufsize = atoi(optarg);
60                         break;
61                 default:
62                         usage(dmesg_usage);
63                 }
64         }                       
65
66         if (optind < argc) {
67                 goto end;
68         }
69
70         if (cmd == 8) {
71                 n = klogctl(cmd, NULL, level);
72                 if (n < 0) {
73                         goto klogctl_error;
74                 }
75                 exit(TRUE);
76         }
77
78         if (bufsize < 4096)
79                 bufsize = 4096;
80         buf = (char *) xmalloc(bufsize);
81         n = klogctl(cmd, buf, bufsize);
82         if (n < 0) {
83                 goto klogctl_error;
84         }
85
86         lastc = '\n';
87         for (i = 0; i < n; i++) {
88                 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
89                         i++;
90                         while (buf[i] >= '0' && buf[i] <= '9')
91                                 i++;
92                         if (buf[i] == '>')
93                                 i++;
94                 }
95                 lastc = buf[i];
96                 putchar(lastc);
97         }
98         if (lastc != '\n')
99                 putchar('\n');
100         exit(TRUE);
101   end:
102         usage(dmesg_usage);
103         exit(FALSE);
104   klogctl_error:
105         perror("klogctl");
106         return(FALSE);
107 }