xref: /original-bsd/usr.bin/systat/disks.c (revision 42f60e33)
1 /*-
2  * Copyright (c) 1980, 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)disks.c	5.17 (Berkeley) 05/28/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/buf.h>
14 
15 #include <nlist.h>
16 #include <ctype.h>
17 #include <paths.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "systat.h"
21 #include "extern.h"
22 
23 static void dkselect __P((char *, int, int []));
24 static int read_names __P((void));
25 
26 static struct nlist namelist[] = {
27 #define	X_DK_NDRIVE	0
28 	{ "_dk_ndrive" },
29 #define	X_DK_WPMS	1
30 	{ "_dk_wpms" },
31 #ifdef vax
32 #define	X_MBDINIT	(X_DK_WPMS+1)
33 	{ "_mbdinit" },
34 #define	X_UBDINIT	(X_DK_WPMS+2)
35 	{ "_ubdinit" },
36 #endif
37 #ifdef sun
38 #define	X_MBDINIT	(X_DK_WPMS+1)
39 	{ "_mbdinit" },
40 #endif
41 #ifdef tahoe
42 #define	X_VBDINIT	(X_DK_WPMS+1)
43 	{ "_vbdinit" },
44 #endif
45 #if defined(hp300) || defined(luna68k)
46 #define X_HPDINIT       (X_DK_WPMS+1)
47         { "_hp_dinit" },
48 #endif
49 #ifdef mips
50 #define X_SCSI_DINIT	(X_DK_WPMS+1)
51 	{ "_scsi_dinit" },
52 #endif
53 	{ "" },
54 };
55 
56 float *dk_mspw;
57 int dk_ndrive, *dk_select;
58 char **dr_name;
59 
60 #include "names.c"					/* XXX */
61 
62 int
63 dkinit()
64 {
65 	register int i;
66 	register char *cp;
67 	static int once = 0;
68 	static char buf[1024];
69 
70 	if (once)
71 		return(1);
72 
73 	if (kvm_nlist(kd, namelist)) {
74 		nlisterr(namelist);
75 		return(0);
76 	}
77 	if (namelist[X_DK_NDRIVE].n_value == 0) {
78 		error("dk_ndrive undefined in kernel");
79 		return(0);
80 	}
81 	NREAD(X_DK_NDRIVE, &dk_ndrive, LONG);
82 	if (dk_ndrive <= 0) {
83 		error("dk_ndrive=%d according to %s", dk_ndrive, _PATH_UNIX);
84 		return(0);
85 	}
86 	dk_mspw = (float *)calloc(dk_ndrive, sizeof (float));
87 	{
88 		long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
89 		KREAD(NPTR(X_DK_WPMS), wpms, dk_ndrive * sizeof (long));
90 		for (i = 0; i < dk_ndrive; i++)
91 			*(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
92 			                 (float) 1.0 / *(wpms + i);
93 		free(wpms);
94 	}
95 	dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
96 	dk_select = (int *)calloc(dk_ndrive, sizeof (int));
97 	for (cp = buf, i = 0; i < dk_ndrive; i++) {
98 		dr_name[i] = cp;
99 		sprintf(dr_name[i], "dk%d", i);
100 		cp += strlen(dr_name[i]) + 1;
101 		if (dk_mspw[i] != 0.0)
102 			dk_select[i] = 1;
103 	}
104 	if (!read_names()) {
105 		free(dr_name);
106 		free(dk_select);
107 		free(dk_mspw);
108 		return(0);
109 	}
110 	once = 1;
111 	return(1);
112 }
113 
114 int
115 dkcmd(cmd, args)
116 	char *cmd, *args;
117 {
118 	if (prefix(cmd, "display") || prefix(cmd, "add")) {
119 		dkselect(args, 1, dk_select);
120 		return (1);
121 	}
122 	if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
123 		dkselect(args, 0, dk_select);
124 		return (1);
125 	}
126 	if (prefix(cmd, "drives")) {
127 		register int i;
128 
129 		move(CMDLINE, 0); clrtoeol();
130 		for (i = 0; i < dk_ndrive; i++)
131 			if (dk_mspw[i] != 0.0)
132 				printw("%s ", dr_name[i]);
133 		return (1);
134 	}
135 	return (0);
136 }
137 
138 static void
139 dkselect(args, truefalse, selections)
140 	char *args;
141 	int truefalse, selections[];
142 {
143 	register char *cp;
144 	register int i;
145 	char *index();
146 
147 	cp = index(args, '\n');
148 	if (cp)
149 		*cp = '\0';
150 	for (;;) {
151 		for (cp = args; *cp && isspace(*cp); cp++)
152 			;
153 		args = cp;
154 		for (; *cp && !isspace(*cp); cp++)
155 			;
156 		if (*cp)
157 			*cp++ = '\0';
158 		if (cp - args == 0)
159 			break;
160 		for (i = 0; i < dk_ndrive; i++)
161 			if (strcmp(args, dr_name[i]) == 0) {
162 				if (dk_mspw[i] != 0.0)
163 					selections[i] = truefalse;
164 				else
165 					error("%s: drive not configured",
166 					    dr_name[i]);
167 				break;
168 			}
169 		if (i >= dk_ndrive)
170 			error("%s: unknown drive", args);
171 		args = cp;
172 	}
173 }
174