PATCH TO CONFIG TO ALLOW COMMENTS IN *.i386
[unix-history] / usr / src / usr.sbin / config / mkswapconf.c
CommitLineData
7a4a8643
WJ
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * 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.
42c89371
JE
32 *
33 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
34 * -------------------- ----- ----------------------
35 * CURRENT PATCH LEVEL: 1 00096
36 * -------------------- ----- ----------------------
37 *
38 * 15 Feb 93 Julian Elischer Allow comments (leading #) in device file
7a4a8643
WJ
39 */
40
41#ifndef lint
42static char sccsid[] = "@(#)mkswapconf.c 5.10 (Berkeley) 7/1/91";
43#endif /* not lint */
44
45/*
46 * Build a swap configuration file.
47 */
48#include "config.h"
49
50#include <stdio.h>
51#include <ctype.h>
52
53swapconf()
54{
55 register struct file_list *fl;
56 struct file_list *do_swap();
57
58 fl = conf_list;
59 while (fl) {
60 if (fl->f_type != SYSTEMSPEC) {
61 fl = fl->f_next;
62 continue;
63 }
64 fl = do_swap(fl);
65 }
66}
67
68struct file_list *
69do_swap(fl)
70 register struct file_list *fl;
71{
72 FILE *fp;
73 char swapname[80];
74 register struct file_list *swap;
75 dev_t dev;
76
77 if (eq(fl->f_fn, "generic")) {
78 fl = fl->f_next;
79 return (fl->f_next);
80 }
81 (void) sprintf(swapname, "swap%s.c", fl->f_fn);
82 fp = fopen(path(swapname), "w");
83 if (fp == 0) {
84 perror(path(swapname));
85 exit(1);
86 }
87 fprintf(fp, "#include \"sys/param.h\"\n");
88 fprintf(fp, "#include \"sys/conf.h\"\n");
89 fprintf(fp, "\n");
90 /*
91 * If there aren't any swap devices
92 * specified, just return, the error
93 * has already been noted.
94 */
95 swap = fl->f_next;
96 if (swap == 0 || swap->f_type != SWAPSPEC) {
97 (void) unlink(path(swapname));
98 fclose(fp);
99 return (swap);
100 }
101 fprintf(fp, "dev_t\trootdev = makedev(%d, %d);\n",
102 major(fl->f_rootdev), minor(fl->f_rootdev));
103 fprintf(fp, "dev_t\tdumpdev = makedev(%d, %d);\n",
104 major(fl->f_dumpdev), minor(fl->f_dumpdev));
105 fprintf(fp, "\n");
106 fprintf(fp, "struct\tswdevt swdevt[] = {\n");
107 do {
108 dev = swap->f_swapdev;
109 fprintf(fp, "\t{ makedev(%d, %d),\t0,\t%d },\t/* %s */\n",
110 major(dev), minor(dev), swap->f_swapsize, swap->f_fn);
111 swap = swap->f_next;
112 } while (swap && swap->f_type == SWAPSPEC);
113 fprintf(fp, "\t{ 0, 0, 0 }\n");
114 fprintf(fp, "};\n");
115 fclose(fp);
116 return (swap);
117}
118
119static int devtablenotread = 1;
120static struct devdescription {
121 char *dev_name;
122 int dev_major;
123 struct devdescription *dev_next;
124} *devtable;
125
126/*
127 * Given a device name specification figure out:
128 * major device number
129 * partition
130 * device name
131 * unit number
132 * This is a hack, but the system still thinks in
133 * terms of major/minor instead of string names.
134 */
135dev_t
136nametodev(name, defunit, defpartition)
137 char *name;
138 int defunit;
139 char defpartition;
140{
141 char *cp, partition;
142 int unit;
143 register struct devdescription *dp;
144
145 cp = name;
146 if (cp == 0) {
147 fprintf(stderr, "config: internal error, nametodev\n");
148 exit(1);
149 }
150 while (*cp && !isdigit(*cp))
151 cp++;
152 unit = *cp ? atoi(cp) : defunit;
153 if (unit < 0 || unit > 31) {
154 fprintf(stderr,
155"config: %s: invalid device specification, unit out of range\n", name);
156 unit = defunit; /* carry on more checking */
157 }
158 if (*cp) {
159 *cp++ = '\0';
160 while (*cp && isdigit(*cp))
161 cp++;
162 }
163 partition = *cp ? *cp : defpartition;
164 if (partition < 'a' || partition > 'h') {
165 fprintf(stderr,
166"config: %c: invalid device specification, bad partition\n", *cp);
167 partition = defpartition; /* carry on */
168 }
169 if (devtablenotread)
170 initdevtable();
171 for (dp = devtable; dp; dp = dp->dev_next)
172 if (eq(name, dp->dev_name))
173 break;
174 if (dp == 0) {
175 fprintf(stderr, "config: %s: unknown device\n", name);
176 return (NODEV);
177 }
178 return (makedev(dp->dev_major, (unit << 3) + (partition - 'a')));
179}
180
181char *
182devtoname(dev)
183 dev_t dev;
184{
185 char buf[80];
186 register struct devdescription *dp;
187
188 if (devtablenotread)
189 initdevtable();
190 for (dp = devtable; dp; dp = dp->dev_next)
191 if (major(dev) == dp->dev_major)
192 break;
193 if (dp == 0)
194 dp = devtable;
195 (void) sprintf(buf, "%s%d%c", dp->dev_name,
196 minor(dev) >> 3, (minor(dev) & 07) + 'a');
197 return (ns(buf));
198}
199
200initdevtable()
201{
42c89371 202 char linebuf[256];
7a4a8643
WJ
203 char buf[BUFSIZ];
204 int maj;
205 register struct devdescription **dp = &devtable;
206 FILE *fp;
207
208 (void) sprintf(buf, "../conf/devices.%s", machinename);
209 fp = fopen(buf, "r");
210 if (fp == NULL) {
211 fprintf(stderr, "config: can't open %s\n", buf);
212 exit(1);
213 }
42c89371
JE
214 while(fgets(linebuf,256,fp))
215 {
216 /*******************************\
217 * Allow a comment *
218 \*******************************/
219 if(linebuf[0] == '#') continue;
220
221 if (sscanf(linebuf, "%s\t%d\n", buf, &maj) == 2)
222 {
223 *dp = (struct devdescription *)malloc(sizeof (**dp));
224 (*dp)->dev_name = ns(buf);
225 (*dp)->dev_major = maj;
226 dp = &(*dp)->dev_next;
227 }
228 else
229 {
230 fprintf(stderr,"illegal line in devices file\n");
231 break;
232 }
7a4a8643
WJ
233 }
234 *dp = 0;
235 fclose(fp);
236 devtablenotread = 0;
237}