Making note of my changes
[oweals/busybox.git] / dmesg.c
1 /* dmesg.c -- Print out the contents of the kernel ring buffer
2  * Created: Sat Oct  9 16:19:47 1993
3  * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
4  * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
5  * This program comes with ABSOLUTELY NO WARRANTY.
6  * Modifications by Rick Sladkey (jrs@world.std.com)
7  * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch
8  * by Peeter Joot.  This was also suggested by John Hudson.
9  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10  * - added Native Language Support
11  *
12  * from util-linux -- adapted for busybox by 
13  * Erik Andersen <andersee@debian.org>. I ripped out Native Language 
14  * Support, replaced getopt, added some gotos for redundant stuff.
15  */
16
17 #include "internal.h"
18 #include <linux/unistd.h>
19 #include <stdio.h>
20 #include <getopt.h>
21 #include <stdlib.h>
22
23 #if __GNU_LIBRARY__ < 5
24
25 #ifndef __alpha__
26 # define __NR_klogctl __NR_syslog
27   static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
28 #else /* __alpha__ */
29 #define klogctl syslog
30 #endif
31
32 #else
33 # include <sys/klog.h>
34 #endif
35
36 static const char dmesg_usage[] = "dmesg [-c] [-n level] [-s bufsize]\n";
37
38 int dmesg_main( int argc, char** argv )
39 {
40    char *buf;
41    int  bufsize=8196;
42    int  i;
43    int  n;
44    int  level = 0;
45    int  lastc;
46    int  cmd = 3;
47    int stopDoingThat;
48
49    argc--;
50    argv++;
51
52     /* Parse any options */
53     while (argc && **argv == '-') {
54         stopDoingThat = FALSE;
55         while (stopDoingThat == FALSE && *++(*argv)) {
56             switch (**argv) {
57             case 'c':
58                 cmd = 4;
59                 break;
60             case 'n':
61                 cmd = 8;
62                 if (--argc == 0)
63                     goto end;
64                 level = atoi (*(++argv));
65                 if (--argc > 0)
66                     ++argv;
67                 stopDoingThat = TRUE;
68                 break;
69             case 's':
70                 if (--argc == 0)
71                     goto end;
72                 bufsize = atoi (*(++argv));
73                 if (--argc > 0)
74                     ++argv;
75                 stopDoingThat = TRUE;
76                 break;
77             default:
78                 goto end;
79             }
80         }
81     }
82    
83    if (argc > 1) {
84         goto end;
85    }
86
87    if (cmd == 8) {
88       n = klogctl( cmd, NULL, level );
89       if (n < 0) {
90           goto klogctl_error;
91       }
92       exit( TRUE );
93    }
94
95    if (bufsize < 4096) bufsize = 4096;
96    buf = (char*)malloc(bufsize);
97    n = klogctl( cmd, buf, bufsize );
98    if (n < 0) {
99        goto klogctl_error;
100    }
101
102    lastc = '\n';
103    for (i = 0; i < n; i++) {
104       if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
105          i++;
106          while (buf[i] >= '0' && buf[i] <= '9')
107             i++;
108          if (buf[i] == '>')
109             i++;
110       }
111       lastc = buf[i];
112       putchar( lastc );
113    }
114    if (lastc != '\n')
115       putchar( '\n' );
116    exit( TRUE);
117 end:
118     usage( dmesg_usage);
119     exit (FALSE);
120 klogctl_error:
121     perror( "klogctl" );
122     exit( FALSE );
123
124 }