xref: /original-bsd/usr.sbin/config/mkswapconf.c (revision 54e6d6c7)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)mkswapconf.c	5.7 (Berkeley) 04/25/89";
20 #endif /* not lint */
21 
22 /*
23  * Build a swap configuration file.
24  */
25 #include "config.h"
26 
27 #include <stdio.h>
28 #include <ctype.h>
29 
30 swapconf()
31 {
32 	register struct file_list *fl;
33 	struct file_list *do_swap();
34 
35 	fl = conf_list;
36 	while (fl) {
37 		if (fl->f_type != SYSTEMSPEC) {
38 			fl = fl->f_next;
39 			continue;
40 		}
41 		fl = do_swap(fl);
42 	}
43 }
44 
45 struct file_list *
46 do_swap(fl)
47 	register struct file_list *fl;
48 {
49 	FILE *fp;
50 	char  swapname[80];
51 	register struct file_list *swap;
52 	dev_t dev;
53 
54 	if (eq(fl->f_fn, "generic")) {
55 		fl = fl->f_next;
56 		return (fl->f_next);
57 	}
58 	(void) sprintf(swapname, "swap%s.c", fl->f_fn);
59 	fp = fopen(path(swapname), "w");
60 	if (fp == 0) {
61 		perror(path(swapname));
62 		exit(1);
63 	}
64 	fprintf(fp, "#include \"../sys/param.h\"\n");
65 	fprintf(fp, "#include \"../sys/conf.h\"\n");
66 	fprintf(fp, "\n");
67 	/*
68 	 * If there aren't any swap devices
69 	 * specified, just return, the error
70 	 * has already been noted.
71 	 */
72 	swap = fl->f_next;
73 	if (swap == 0 || swap->f_type != SWAPSPEC) {
74 		(void) unlink(path(swapname));
75 		fclose(fp);
76 		return (swap);
77 	}
78 	fprintf(fp, "dev_t\trootdev = makedev(%d, %d);\n",
79 		major(fl->f_rootdev), minor(fl->f_rootdev));
80 	fprintf(fp, "dev_t\targdev  = makedev(%d, %d);\n",
81 		major(fl->f_argdev), minor(fl->f_argdev));
82 	fprintf(fp, "dev_t\tdumpdev = makedev(%d, %d);\n",
83 		major(fl->f_dumpdev), minor(fl->f_dumpdev));
84 	fprintf(fp, "\n");
85 	fprintf(fp, "struct\tswdevt swdevt[] = {\n");
86 	do {
87 		dev = swap->f_swapdev;
88 		fprintf(fp, "\t{ makedev(%d, %d),\t0,\t%d },\t/* %s */\n",
89 		    major(dev), minor(dev), swap->f_swapsize, swap->f_fn);
90 		swap = swap->f_next;
91 	} while (swap && swap->f_type == SWAPSPEC);
92 	fprintf(fp, "\t{ 0, 0, 0 }\n");
93 	fprintf(fp, "};\n");
94 	fclose(fp);
95 	return (swap);
96 }
97 
98 static	int devtablenotread = 1;
99 static	struct devdescription {
100 	char	*dev_name;
101 	int	dev_major;
102 	struct	devdescription *dev_next;
103 } *devtable;
104 
105 /*
106  * Given a device name specification figure out:
107  *	major device number
108  *	partition
109  *	device name
110  *	unit number
111  * This is a hack, but the system still thinks in
112  * terms of major/minor instead of string names.
113  */
114 dev_t
115 nametodev(name, defunit, defpartition)
116 	char *name;
117 	int defunit;
118 	char defpartition;
119 {
120 	char *cp, partition;
121 	int unit;
122 	register struct devdescription *dp;
123 
124 	cp = name;
125 	if (cp == 0) {
126 		fprintf(stderr, "config: internal error, nametodev\n");
127 		exit(1);
128 	}
129 	while (*cp && !isdigit(*cp))
130 		cp++;
131 	unit = *cp ? atoi(cp) : defunit;
132 	if (unit < 0 || unit > 31) {
133 		fprintf(stderr,
134 "config: %s: invalid device specification, unit out of range\n", name);
135 		unit = defunit;			/* carry on more checking */
136 	}
137 	if (*cp) {
138 		*cp++ = '\0';
139 		while (*cp && isdigit(*cp))
140 			cp++;
141 	}
142 	partition = *cp ? *cp : defpartition;
143 	if (partition < 'a' || partition > 'h') {
144 		fprintf(stderr,
145 "config: %c: invalid device specification, bad partition\n", *cp);
146 		partition = defpartition;	/* carry on */
147 	}
148 	if (devtablenotread)
149 		initdevtable();
150 	for (dp = devtable; dp; dp = dp->dev_next)
151 		if (eq(name, dp->dev_name))
152 			break;
153 	if (dp == 0) {
154 		fprintf(stderr, "config: %s: unknown device\n", name);
155 		return (NODEV);
156 	}
157 	return (makedev(dp->dev_major, (unit << 3) + (partition - 'a')));
158 }
159 
160 char *
161 devtoname(dev)
162 	dev_t dev;
163 {
164 	char buf[80];
165 	register struct devdescription *dp;
166 
167 	if (devtablenotread)
168 		initdevtable();
169 	for (dp = devtable; dp; dp = dp->dev_next)
170 		if (major(dev) == dp->dev_major)
171 			break;
172 	if (dp == 0)
173 		dp = devtable;
174 	(void) sprintf(buf, "%s%d%c", dp->dev_name,
175 		minor(dev) >> 3, (minor(dev) & 07) + 'a');
176 	return (ns(buf));
177 }
178 
179 initdevtable()
180 {
181 	char buf[BUFSIZ];
182 	int maj;
183 	register struct devdescription **dp = &devtable;
184 	FILE *fp;
185 
186 	(void) sprintf(buf, "../conf/devices.%s", machinename);
187 	fp = fopen(buf, "r");
188 	if (fp == NULL) {
189 		fprintf(stderr, "config: can't open %s\n", buf);
190 		exit(1);
191 	}
192 	while (fscanf(fp, "%s\t%d\n", buf, &maj) == 2) {
193 		*dp = (struct devdescription *)malloc(sizeof (**dp));
194 		(*dp)->dev_name = ns(buf);
195 		(*dp)->dev_major = maj;
196 		dp = &(*dp)->dev_next;
197 	}
198 	*dp = 0;
199 	fclose(fp);
200 	devtablenotread = 0;
201 }
202