blob: 64265b473e6049a560f324ae2c70f0067570dbc1 [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>
Eric Andersencc8ed391999-10-05 16:24:54 +000017
18#define __NR_klog __NR_syslog
19
20#if defined(__GLIBC__)
21#include <sys/klog.h>
22#define klog klogctl
23#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000024static inline _syscall3 (int, klog, int, type, char *, b, int, len)
25#endif /* __GLIBC__ */
Eric Andersencc8ed391999-10-05 16:24:54 +000026
Eric Andersencc8ed391999-10-05 16:24:54 +000027
Eric Andersen3cf52d11999-10-12 22:26:06 +000028
29static const char dmesg_usage[] = "dmesg [-c] [-n level]\n";
30
31int dmesg_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000032{
33
Eric Andersen3cf52d11999-10-12 22:26:06 +000034 char buf[4096];
35 int i;
36 int n;
37 int level = 0;
38 int lastc;
39 int cmd = 3;
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Eric Andersen3cf52d11999-10-12 22:26:06 +000041 argc--;
42 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersen3cf52d11999-10-12 22:26:06 +000044 /* Parse any options */
45 while (argc && **argv == '-') {
46 while (*++(*argv))
47 switch (**argv) {
48 case 'c':
49 cmd = 4;
50 break;
51 case 'n':
52 cmd = 8;
53 if (--argc == 0)
54 goto end;
55 level = atoi (*(++argv));
56 --argc;
57 ++argv;
58 break;
59 default:
60 goto end;
61 }
62 }
Eric Andersencc8ed391999-10-05 16:24:54 +000063
Eric Andersen3cf52d11999-10-12 22:26:06 +000064 if (cmd == 8) {
65 n = klog (cmd, NULL, level);
66 if (n < 0) {
67 perror ("klog");
68 exit (FALSE);
69 }
70 exit (TRUE);
71 }
Eric Andersencc8ed391999-10-05 16:24:54 +000072
Eric Andersen3cf52d11999-10-12 22:26:06 +000073 n = klog (cmd, buf, sizeof (buf));
74 if (n < 0) {
75 perror ("klog");
76 exit (FALSE);
77 }
78
79 lastc = '\n';
80 for (i = 0; i < n; i++) {
81 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
Eric Andersencc8ed391999-10-05 16:24:54 +000082 i++;
Eric Andersen3cf52d11999-10-12 22:26:06 +000083 while (buf[i] >= '0' && buf[i] <= '9')
84 i++;
85 if (buf[i] == '>')
86 i++;
87 }
88 lastc = buf[i];
89 putchar (lastc);
90 }
91 if (lastc != '\n')
92 putchar ('\n');
93 exit (TRUE);
94
95 end:
96 fprintf (stderr, "Usage: %s\n", dmesg_usage);
97 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000098}