1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)dmesg.c	5.4 (Berkeley) 2/20/86";
   9: #endif not lint
  10: 
  11: /*
  12:  *	Suck up system messages
  13:  *	dmesg
  14:  *		print current buffer
  15:  *	dmesg -
  16:  *		print and update incremental history
  17:  */
  18: 
  19: #include <stdio.h>
  20: #include <sys/param.h>
  21: #include <nlist.h>
  22: #include <signal.h>
  23: #include <sys/file.h>
  24: #include <sys/vm.h>
  25: #include <sys/msgbuf.h>
  26: 
  27: struct  msgbuf msgbuf;
  28: char    *msgbufp;
  29: int sflg;
  30: int of  = -1;
  31: 
  32: struct  msgbuf omesg;
  33: struct  nlist nl[2] = {
  34:     { "_msgbuf" },
  35:     { "" }
  36: };
  37: 
  38: main(argc, argv)
  39: char **argv;
  40: {
  41:     int mem;
  42:     register char *mp, *omp, *mstart;
  43:     int samef, sawnl, ignore;
  44: 
  45:     if (argc>1 && argv[1][0] == '-') {
  46:         sflg++;
  47:         argc--;
  48:         argv++;
  49:     }
  50:     if (sflg) {
  51:         of = open("/usr/adm/msgbuf", O_RDWR | O_CREAT, 0644);
  52:         if (of < 0)
  53:             done("Can't open /usr/adm/msgbuf\n");
  54:         read(of, (char *)&omesg, sizeof(omesg));
  55:         lseek(of, 0L, 0);
  56:     }
  57:     sflg = 0;
  58:     nlist(argc>2? argv[2]:"/vmunix", nl);
  59:     if (nl[0].n_type==0)
  60:         done("Can't get kernel namelist\n");
  61:     if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0)
  62:         done("Can't read kernel memory\n");
  63:     lseek(mem, (long)nl[0].n_value, 0);
  64:     read(mem, &msgbuf, sizeof (msgbuf));
  65:     if (msgbuf.msg_magic != MSG_MAGIC)
  66:         done("Magic number wrong (namelist mismatch?)\n");
  67:     if (msgbuf.msg_bufx >= MSG_BSIZE)
  68:         msgbuf.msg_bufx = 0;
  69:     if (omesg.msg_bufx >= MSG_BSIZE)
  70:         omesg.msg_bufx = 0;
  71:     mstart = &msgbuf.msg_bufc[omesg.msg_bufx];
  72:     omp = &omesg.msg_bufc[msgbuf.msg_bufx];
  73:     mp = msgbufp = &msgbuf.msg_bufc[msgbuf.msg_bufx];
  74:     samef = 1;
  75:     do {
  76:         if (*mp++ != *omp++) {
  77:             mstart = msgbufp;
  78:             samef = 0;
  79:             pdate();
  80:             printf("...\n");
  81:             break;
  82:         }
  83:         if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
  84:             mp = msgbuf.msg_bufc;
  85:         if (omp >= &omesg.msg_bufc[MSG_BSIZE])
  86:             omp = omesg.msg_bufc;
  87:     } while (mp != mstart);
  88:     if (samef && omesg.msg_bufx == msgbuf.msg_bufx)
  89:         exit(0);
  90:     mp = mstart;
  91:     pdate();
  92:     sawnl = 1;
  93:     do {
  94:         if (sawnl && *mp == '<')
  95:             ignore = 1;
  96:         if (*mp && (*mp & 0200) == 0 && !ignore)
  97:             putchar(*mp);
  98:         if (ignore && *mp == '>')
  99:             ignore = 0;
 100:         sawnl = (*mp == '\n');
 101:         mp++;
 102:         if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
 103:             mp = msgbuf.msg_bufc;
 104:     } while (mp != msgbufp);
 105:     done((char *)NULL);
 106: }
 107: 
 108: done(s)
 109: char *s;
 110: {
 111:     register char *p, *q;
 112: 
 113:     if (s) {
 114:         pdate();
 115:         printf(s);
 116:     } else if (of != -1)
 117:         write(of, (char *)&msgbuf, sizeof(msgbuf));
 118:     exit(s!=NULL);
 119: }
 120: 
 121: pdate()
 122: {
 123:     extern char *ctime();
 124:     static firstime;
 125:     time_t tbuf;
 126: 
 127:     if (firstime==0) {
 128:         firstime++;
 129:         time(&tbuf);
 130:         printf("\n%.12s\n", ctime(&tbuf)+4);
 131:     }
 132: }

Defined functions

done defined in line 108; used 5 times
main defined in line 38; never used
pdate defined in line 121; used 3 times

Defined variables

msgbuf defined in line 27; used 16 times
msgbufp defined in line 28; used 3 times
nl defined in line 33; used 3 times
of defined in line 30; used 6 times
omesg defined in line 32; used 9 times
sccsid defined in line 8; never used
sflg defined in line 29; used 3 times
Last modified: 1986-02-20
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1244
Valid CSS Valid XHTML 1.0 Strict