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