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