Finally mount works properly. Made debugging work (no more -s ld flag
[oweals/busybox.git] / dmesg.c
1 #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
25 static inline _syscall3(int,klog,int,type,char *,b,int,len)
26 #endif /* __GLIBC__ */
27
28 const char                      dmesg_usage[] = "dmesg";
29
30 int
31 dmesg_main(int argc, char * * argv)
32 {
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:
53          fprintf(stderr, "%s\n", dmesg_usage);
54          exit(1);
55       }
56    }
57    argc -= optind;
58    argv += optind;
59    
60    if (argc > 1) {
61      fprintf(stderr, "%s\n", dmesg_usage);
62       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 }