xref: /netbsd-src/usr.bin/systat/disks.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: disks.c,v 1.2 1995/01/20 08:51:53 jtc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)disks.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: disks.c,v 1.2 1995/01/20 08:51:53 jtc Exp $";
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/buf.h>
45 
46 #include <nlist.h>
47 #include <ctype.h>
48 #include <paths.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include "systat.h"
52 #include "extern.h"
53 
54 static void dkselect __P((char *, int, int []));
55 static int read_names __P((void));
56 
57 static struct nlist namelist[] = {
58 #define	X_DK_NDRIVE	0
59 	{ "_dk_ndrive" },
60 #define	X_DK_WPMS	1
61 	{ "_dk_wpms" },
62 #ifdef vax
63 #define	X_MBDINIT	(X_DK_WPMS+1)
64 	{ "_mbdinit" },
65 #define	X_UBDINIT	(X_DK_WPMS+2)
66 	{ "_ubdinit" },
67 #endif
68 #ifdef sun
69 #define	X_MBDINIT	(X_DK_WPMS+1)
70 	{ "_mbdinit" },
71 #endif
72 #ifdef tahoe
73 #define	X_VBDINIT	(X_DK_WPMS+1)
74 	{ "_vbdinit" },
75 #endif
76 #if defined(hp300) || defined(luna68k)
77 #define X_HPDINIT       (X_DK_WPMS+1)
78         { "_hp_dinit" },
79 #endif
80 #ifdef mips
81 #define X_SCSI_DINIT	(X_DK_WPMS+1)
82 	{ "_scsi_dinit" },
83 #endif
84 	{ "" },
85 };
86 
87 float *dk_mspw;
88 int dk_ndrive, *dk_select;
89 char **dr_name;
90 
91 #include "names.c"					/* XXX */
92 
93 int
94 dkinit()
95 {
96 	register int i;
97 	register char *cp;
98 	static int once = 0;
99 	static char buf[1024];
100 
101 	if (once)
102 		return(1);
103 
104 	if (kvm_nlist(kd, namelist)) {
105 		nlisterr(namelist);
106 		return(0);
107 	}
108 	if (namelist[X_DK_NDRIVE].n_value == 0) {
109 		error("dk_ndrive undefined in kernel");
110 		return(0);
111 	}
112 	NREAD(X_DK_NDRIVE, &dk_ndrive, LONG);
113 	if (dk_ndrive <= 0) {
114 		error("dk_ndrive=%d according to %s", dk_ndrive, _PATH_UNIX);
115 		return(0);
116 	}
117 	dk_mspw = (float *)calloc(dk_ndrive, sizeof (float));
118 	{
119 		long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
120 		KREAD(NPTR(X_DK_WPMS), wpms, dk_ndrive * sizeof (long));
121 		for (i = 0; i < dk_ndrive; i++)
122 			*(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
123 			                 (float) 1.0 / *(wpms + i);
124 		free(wpms);
125 	}
126 	dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
127 	dk_select = (int *)calloc(dk_ndrive, sizeof (int));
128 	for (cp = buf, i = 0; i < dk_ndrive; i++) {
129 		dr_name[i] = cp;
130 		sprintf(dr_name[i], "dk%d", i);
131 		cp += strlen(dr_name[i]) + 1;
132 		if (dk_mspw[i] != 0.0)
133 			dk_select[i] = 1;
134 	}
135 	if (!read_names()) {
136 		free(dr_name);
137 		free(dk_select);
138 		free(dk_mspw);
139 		return(0);
140 	}
141 	once = 1;
142 	return(1);
143 }
144 
145 int
146 dkcmd(cmd, args)
147 	char *cmd, *args;
148 {
149 	if (prefix(cmd, "display") || prefix(cmd, "add")) {
150 		dkselect(args, 1, dk_select);
151 		return (1);
152 	}
153 	if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
154 		dkselect(args, 0, dk_select);
155 		return (1);
156 	}
157 	if (prefix(cmd, "drives")) {
158 		register int i;
159 
160 		move(CMDLINE, 0); clrtoeol();
161 		for (i = 0; i < dk_ndrive; i++)
162 			if (dk_mspw[i] != 0.0)
163 				printw("%s ", dr_name[i]);
164 		return (1);
165 	}
166 	return (0);
167 }
168 
169 static void
170 dkselect(args, truefalse, selections)
171 	char *args;
172 	int truefalse, selections[];
173 {
174 	register char *cp;
175 	register int i;
176 	char *index();
177 
178 	cp = index(args, '\n');
179 	if (cp)
180 		*cp = '\0';
181 	for (;;) {
182 		for (cp = args; *cp && isspace(*cp); cp++)
183 			;
184 		args = cp;
185 		for (; *cp && !isspace(*cp); cp++)
186 			;
187 		if (*cp)
188 			*cp++ = '\0';
189 		if (cp - args == 0)
190 			break;
191 		for (i = 0; i < dk_ndrive; i++)
192 			if (strcmp(args, dr_name[i]) == 0) {
193 				if (dk_mspw[i] != 0.0)
194 					selections[i] = truefalse;
195 				else
196 					error("%s: drive not configured",
197 					    dr_name[i]);
198 				break;
199 			}
200 		if (i >= dk_ndrive)
201 			error("%s: unknown drive", args);
202 		args = cp;
203 	}
204 }
205