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