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