xref: /original-bsd/sbin/dmesg/dmesg.c (revision ba762ddc)
1 /*-
2  * Copyright (c) 1986, 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 char 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
15 static char sccsid[] = "@(#)dmesg.c	5.8 (Berkeley) 04/16/91";
16 #endif /* not lint */
17 
18 /*
19  *	Suck up system messages
20  *	dmesg
21  *		print current buffer
22  *	dmesg -
23  *		print and update incremental history
24  */
25 
26 #include <sys/param.h>
27 #include <sys/signal.h>
28 #include <sys/file.h>
29 #include <sys/vm.h>
30 #include <sys/msgbuf.h>
31 #include <nlist.h>
32 #include <stdio.h>
33 #include "pathnames.h"
34 
35 struct	msgbuf msgbuf;
36 char	*msgbufp;
37 int	sflg;
38 int	of	= -1;
39 
40 struct	msgbuf omesg;
41 struct	nlist nl[2] = {
42 	{ "_msgbuf" },
43 	{ "" }
44 };
45 
46 main(argc, argv)
47 char **argv;
48 {
49 	int mem;
50 	register char *mp, *omp, *mstart;
51 	int samef, sawnl, ignore = 0;
52 
53 	if (argc>1 && argv[1][0] == '-') {
54 		sflg++;
55 		argc--;
56 		argv++;
57 	}
58 	if (sflg) {
59 		of = open(_PATH_MSGBUF, O_RDWR | O_CREAT, 0644);
60 		if (of < 0)
61 			done("Can't open msgbuf file\n");
62 		read(of, (char *)&omesg, sizeof(omesg));
63 		lseek(of, 0L, 0);
64 	}
65 	sflg = 0;
66 	nlist(argc>2? argv[2]:_PATH_UNIX, nl);
67 	if (nl[0].n_type==0)
68 		done("Can't get kernel namelist\n");
69 	if ((mem = open((argc>1? argv[1]: _PATH_KMEM), 0)) < 0)
70 		done("Can't read kernel memory\n");
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");
75 	if (msgbuf.msg_bufx >= MSG_BSIZE)
76 		msgbuf.msg_bufx = 0;
77 	if (omesg.msg_bufx >= MSG_BSIZE)
78 		omesg.msg_bufx = 0;
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 		}
91 		if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
92 			mp = msgbuf.msg_bufc;
93 		if (omp >= &omesg.msg_bufc[MSG_BSIZE])
94 			omp = omesg.msg_bufc;
95 	} while (mp != mstart);
96 	if (samef && omesg.msg_bufx == msgbuf.msg_bufx)
97 		exit(0);
98 	mp = mstart;
99 	pdate();
100 	sawnl = 1;
101 	do {
102 		if (sawnl && *mp == '<')
103 			ignore = 1;
104 		if (*mp && (*mp & 0200) == 0 && !ignore)
105 			putchar(*mp);
106 		if (ignore && *mp == '>')
107 			ignore = 0;
108 		sawnl = (*mp == '\n');
109 		mp++;
110 		if (mp >= &msgbuf.msg_bufc[MSG_BSIZE])
111 			mp = msgbuf.msg_bufc;
112 	} while (mp != msgbufp);
113 	done((char *)NULL);
114 }
115 
116 done(s)
117 char *s;
118 {
119 	if (s) {
120 		pdate();
121 		printf(s);
122 	} else if (of != -1)
123 		write(of, (char *)&msgbuf, sizeof(msgbuf));
124 	exit(s!=NULL);
125 }
126 
127 pdate()
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 }
139