blob: 9096621b03a1908aedb9ef74ea863070420032ad [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include "internal.h"
2#include <stdlib.h>
3#include <unistd.h>
4#include <time.h>
5
6/* dmesg.c -- Print out the contents of the kernel ring buffer
7 * Created: Sat Oct 9 16:19:47 1993
8 * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
9 * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
10 * This program comes with ABSOLUTELY NO WARRANTY.
11 * Modifications by Rick Sladkey (jrs@world.std.com)
12 * from util-linux; adapted for busybox
13 */
14
15#include <linux/unistd.h>
16#include <stdio.h>
17#include <getopt.h>
18
19#define __NR_klog __NR_syslog
20
21#if defined(__GLIBC__)
22#include <sys/klog.h>
23#define klog klogctl
24#else
25static inline _syscall3(int,klog,int,type,char *,b,int,len)
26#endif /* __GLIBC__ */
27
28const char dmesg_usage[] = "dmesg";
29
30int
Eric Andersen17d49ef1999-10-06 20:25:32 +000031dmesg_main(int argc, char * * argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000032{
33
34 char buf[4096];
35 int i;
36 int n;
37 int c;
38 int level = 0;
39 int lastc;
40 int cmd = 3;
41
42 while ((c = getopt( argc, argv, "cn:" )) != EOF) {
43 switch (c) {
44 case 'c':
45 cmd = 4;
46 break;
47 case 'n':
48 cmd = 8;
49 level = atoi(optarg);
50 break;
51 case '?':
52 default:
Eric Andersen17d49ef1999-10-06 20:25:32 +000053 fprintf(stderr, "%s\n", dmesg_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000054 exit(1);
55 }
56 }
57 argc -= optind;
58 argv += optind;
59
60 if (argc > 1) {
Eric Andersen17d49ef1999-10-06 20:25:32 +000061 fprintf(stderr, "%s\n", dmesg_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000062 exit(1);
63 }
64
65 if (cmd == 8) {
66 n = klog( cmd, NULL, level );
67 if (n < 0) {
68 perror( "klog" );
69 exit( 1 );
70 }
71 exit( 0 );
72 }
73
74 n = klog( cmd, buf, sizeof( buf ) );
75 if (n < 0) {
76 perror( "klog" );
77 exit( 1 );
78 }
79
80 lastc = '\n';
81 for (i = 0; i < n; i++) {
82 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
83 i++;
84 while (buf[i] >= '0' && buf[i] <= '9')
85 i++;
86 if (buf[i] == '>')
87 i++;
88 }
89 lastc = buf[i];
90 putchar( lastc );
91 }
92 if (lastc != '\n')
93 putchar( '\n' );
94 return 0;
95}