xref: /original-bsd/sbin/dmesg/dmesg.c (revision d0e3910b)
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.5 (Berkeley) 02/01/88";
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 = 0;
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 	if (s) {
112 		pdate();
113 		printf(s);
114 	} else if (of != -1)
115 		write(of, (char *)&msgbuf, sizeof(msgbuf));
116 	exit(s!=NULL);
117 }
118 
119 pdate()
120 {
121 	extern char *ctime();
122 	static firstime;
123 	time_t tbuf;
124 
125 	if (firstime==0) {
126 		firstime++;
127 		time(&tbuf);
128 		printf("\n%.12s\n", ctime(&tbuf)+4);
129 	}
130 }
131