added depend label
[unix-history] / usr / src / sbin / dmesg / dmesg.c
CommitLineData
76797561
DF
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
43a11e0e 8static char sccsid[] = "@(#)dmesg.c 5.4 (Berkeley) %G%";
76797561
DF
9#endif not lint
10
34a7981f
BJ
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>
3b294534 23#include <sys/file.h>
34a7981f
BJ
24#include <sys/vm.h>
25#include <sys/msgbuf.h>
26
27struct msgbuf msgbuf;
28char *msgbufp;
29int sflg;
30int of = -1;
31
32struct msgbuf omesg;
33struct nlist nl[2] = {
34 { "_msgbuf" },
c10ca4d6 35 { "" }
34a7981f
BJ
36};
37
38main(argc, argv)
39char **argv;
40{
41 int mem;
42 register char *mp, *omp, *mstart;
3b294534 43 int samef, sawnl, ignore;
34a7981f 44
34a7981f
BJ
45 if (argc>1 && argv[1][0] == '-') {
46 sflg++;
47 argc--;
48 argv++;
49 }
50 if (sflg) {
3b294534 51 of = open("/usr/adm/msgbuf", O_RDWR | O_CREAT, 0644);
c118b033
S
52 if (of < 0)
53 done("Can't open /usr/adm/msgbuf\n");
34a7981f
BJ
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)
c118b033 60 done("Can't get kernel namelist\n");
34a7981f 61 if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0)
c118b033 62 done("Can't read kernel memory\n");
34a7981f
BJ
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");
ba25830c
MK
67 if (msgbuf.msg_bufx >= MSG_BSIZE)
68 msgbuf.msg_bufx = 0;
43a11e0e
MK
69 if (omesg.msg_bufx >= MSG_BSIZE)
70 omesg.msg_bufx = 0;
34a7981f
BJ
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 }
ba25830c 83 if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
34a7981f 84 mp = msgbuf.msg_bufc;
ba25830c 85 if (omp >= &omesg.msg_bufc[MSG_BSIZE])
34a7981f
BJ
86 omp = omesg.msg_bufc;
87 } while (mp != mstart);
88 if (samef && omesg.msg_bufx == msgbuf.msg_bufx)
89 exit(0);
90 mp = mstart;
3b294534
MK
91 pdate();
92 sawnl = 1;
34a7981f 93 do {
3b294534
MK
94 if (sawnl && *mp == '<')
95 ignore = 1;
96 if (*mp && (*mp & 0200) == 0 && !ignore)
34a7981f 97 putchar(*mp);
3b294534
MK
98 if (ignore && *mp == '>')
99 ignore = 0;
100 sawnl = (*mp == '\n');
34a7981f 101 mp++;
ba25830c 102 if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
34a7981f
BJ
103 mp = msgbuf.msg_bufc;
104 } while (mp != msgbufp);
105 done((char *)NULL);
106}
107
108done(s)
109char *s;
110{
111 register char *p, *q;
112
3b294534 113 if (s) {
34a7981f
BJ
114 pdate();
115 printf(s);
3b294534
MK
116 } else if (of != -1)
117 write(of, (char *)&msgbuf, sizeof(msgbuf));
34a7981f
BJ
118 exit(s!=NULL);
119}
120
121pdate()
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}