home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / sys-utils / dmesg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  1.6 KB  |  89 lines

  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.  */
  8.  
  9. #include <linux/unistd.h>
  10. #include <stdio.h>
  11. #include <getopt.h>
  12.  
  13. #define __NR_klog __NR_syslog
  14.  
  15. static inline _syscall3(int,klog,int,type,char *,b,int,len)
  16.  
  17. static char *progname;
  18.  
  19. usage()
  20. {
  21.    fprintf( stderr, "Usage: %s [-c] [-n level]\n", progname );
  22. }
  23.  
  24. int main( int argc, char *argv[] )
  25. {
  26.    char buf[4096];
  27.    int  i;
  28.    int  n;
  29.    int  c;
  30.    int  level;
  31.    int  lastc;
  32.    int  cmd = 3;
  33.  
  34.    progname = argv[0];
  35.    while ((c = getopt( argc, argv, "cn:" )) != EOF) {
  36.       switch (c) {
  37.       case 'c':
  38.      cmd = 4;
  39.      break;
  40.       case 'n':
  41.      cmd = 8;
  42.      level = atoi(optarg);
  43.      break;
  44.       case '?':
  45.       default:
  46.      usage();
  47.      exit(1);
  48.       }
  49.    }
  50.    argc -= optind;
  51.    argv += optind;
  52.    
  53.    if (argc > 1) {
  54.       usage();
  55.       exit(1);
  56.    }
  57.  
  58.    if (cmd == 8) {
  59.       n = klog( cmd, NULL, level );
  60.       if (n < 0) {
  61.      perror( "klog" );
  62.      exit( 1 );
  63.       }
  64.       exit( 0 );
  65.    }
  66.  
  67.    n = klog( cmd, buf, sizeof( buf ) );
  68.    if (n < 0) {
  69.       perror( "klog" );
  70.       exit( 1 );
  71.    }
  72.  
  73.    lastc = '\n';
  74.    for (i = 0; i < n; i++) {
  75.       if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
  76.      i++;
  77.      while (buf[i] >= '0' && buf[i] <= '9')
  78.         i++;
  79.      if (buf[i] == '>')
  80.         i++;
  81.       }
  82.       lastc = buf[i];
  83.       putchar( lastc );
  84.    }
  85.    if (lastc != '\n')
  86.       putchar( '\n' );
  87.    return 0;
  88. }
  89.