]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/dmesg.c
Imported from util-linux-2.11o tarball.
[thirdparty/util-linux.git] / sys-utils / 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@pld.ORG.PL>
10 * - added Native Language Support
11 *
12 */
13
14 #include <linux/unistd.h>
15 #include <stdio.h>
16 #include <getopt.h>
17 #include <stdlib.h>
18 #include "nls.h"
19
20 #if __GNU_LIBRARY__ < 5
21
22 #ifndef __alpha__
23 # define __NR_klogctl __NR_syslog
24 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
25 #else /* __alpha__ */
26 #define klogctl syslog
27 #endif
28
29 #else
30 # include <sys/klog.h>
31 #endif
32
33 static char *progname;
34
35 static void
36 usage(void) {
37 fprintf( stderr, _("Usage: %s [-c] [-n level] [-s bufsize]\n"), progname );
38 }
39
40 int
41 main( int argc, char *argv[] ) {
42 char *buf;
43 int bufsize = 16392;
44 int i;
45 int n;
46 int c;
47 int level = 0;
48 int lastc;
49 int cmd = 3;
50
51 setlocale(LC_ALL, "");
52 bindtextdomain(PACKAGE, LOCALEDIR);
53 textdomain(PACKAGE);
54
55 progname = argv[0];
56 while ((c = getopt( argc, argv, "cn:s:" )) != -1) {
57 switch (c) {
58 case 'c':
59 cmd = 4;
60 break;
61 case 'n':
62 cmd = 8;
63 level = atoi(optarg);
64 break;
65 case 's':
66 bufsize = atoi(optarg);
67 break;
68 case '?':
69 default:
70 usage();
71 exit(1);
72 }
73 }
74 argc -= optind;
75 argv += optind;
76
77 if (argc > 1) {
78 usage();
79 exit(1);
80 }
81
82 if (cmd == 8) {
83 n = klogctl( cmd, NULL, level );
84 if (n < 0) {
85 perror( "klogctl" );
86 exit( 1 );
87 }
88 exit( 0 );
89 }
90
91 if (bufsize < 4096) bufsize = 4096;
92 buf = (char*)malloc(bufsize);
93 n = klogctl( cmd, buf, bufsize );
94 if (n < 0) {
95 perror( "klogctl" );
96 exit( 1 );
97 }
98
99 lastc = '\n';
100 for (i = 0; i < n; i++) {
101 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
102 i++;
103 while (buf[i] >= '0' && buf[i] <= '9')
104 i++;
105 if (buf[i] == '>')
106 i++;
107 }
108 lastc = buf[i];
109 putchar( lastc );
110 }
111 if (lastc != '\n')
112 putchar( '\n' );
113 return 0;
114 }