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 <stdio.h>
19#include <stdlib.h>
20#include <getopt.h>
21
22#if __GNU_LIBRARY__ < 5
23# ifdef __alpha__
24#   define klogctl syslog
25# endif
26#else
27# include <sys/klog.h>
28#endif
29
30#include "busybox.h"
31
32int dmesg_main(int argc, char **argv)
33{
34	char *buf;
35	int c;
36	int bufsize = 8196;
37	int i;
38	int n;
39	int level = 0;
40	int lastc;
41	int cmd = 3;
42
43	while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
44		switch (c) {
45		case 'c':
46			cmd = 4;
47			break;
48		case 'n':
49			cmd = 8;
50			if (optarg == NULL)
51				show_usage();
52			level = atoi(optarg);
53			break;
54		case 's':
55			if (optarg == NULL)
56				show_usage();
57			bufsize = atoi(optarg);
58			break;
59		default:
60			show_usage();
61		}
62	}
63
64	if (optind < argc) {
65		show_usage();
66	}
67
68	if (cmd == 8) {
69		if (klogctl(cmd, NULL, level) < 0)
70			perror_msg_and_die("klogctl");
71		return EXIT_SUCCESS;
72	}
73
74	if (bufsize < 4096)
75		bufsize = 4096;
76	buf = (char *) xmalloc(bufsize);
77	if ((n = klogctl(cmd, buf, bufsize)) < 0)
78		perror_msg_and_die("klogctl");
79
80	lastc = '\n';
81	for (i = 0; i < n; i++) {
82		if (lastc == '\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 EXIT_SUCCESS;
95}
96