Renamed "internal.h" to the more sensible "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 "busybox.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;
38         int c;
39         int bufsize = 8196;
40         int i;
41         int n;
42         int level = 0;
43         int lastc;
44         int cmd = 3;
45
46         while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
47                 switch (c) {
48                 case 'c':
49                         cmd = 4;
50                         break;
51                 case 'n':
52                         cmd = 8;
53                         if (optarg == NULL)
54                                 usage(dmesg_usage);
55                         level = atoi(optarg);
56                         break;
57                 case 's':
58                         if (optarg == NULL)
59                                 usage(dmesg_usage);
60                         bufsize = atoi(optarg);
61                         break;
62                 default:
63                         usage(dmesg_usage);
64                 }
65         }                       
66
67         if (optind < argc) {
68                 goto end;
69         }
70
71         if (cmd == 8) {
72                 n = klogctl(cmd, NULL, level);
73                 if (n < 0) {
74                         goto klogctl_error;
75                 }
76                 exit(TRUE);
77         }
78
79         if (bufsize < 4096)
80                 bufsize = 4096;
81         buf = (char *) xmalloc(bufsize);
82         n = klogctl(cmd, buf, bufsize);
83         if (n < 0) {
84                 goto klogctl_error;
85         }
86
87         lastc = '\n';
88         for (i = 0; i < n; i++) {
89                 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
90                         i++;
91                         while (buf[i] >= '0' && buf[i] <= '9')
92                                 i++;
93                         if (buf[i] == '>')
94                                 i++;
95                 }
96                 lastc = buf[i];
97                 putchar(lastc);
98         }
99         if (lastc != '\n')
100                 putchar('\n');
101         exit(TRUE);
102   end:
103         usage(dmesg_usage);
104         exit(FALSE);
105   klogctl_error:
106         perror("klogctl");
107         return(FALSE);
108 }