xref: /original-bsd/usr.sbin/config/mkswapconf.c (revision 9e0ce84d)
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.10 (Berkeley) 07/01/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\tdumpdev = makedev(%d, %d);\n",
71 		major(fl->f_dumpdev), minor(fl->f_dumpdev));
72 	fprintf(fp, "\n");
73 	fprintf(fp, "struct\tswdevt swdevt[] = {\n");
74 	do {
75 		dev = swap->f_swapdev;
76 		fprintf(fp, "\t{ makedev(%d, %d),\t0,\t%d },\t/* %s */\n",
77 		    major(dev), minor(dev), swap->f_swapsize, swap->f_fn);
78 		swap = swap->f_next;
79 	} while (swap && swap->f_type == SWAPSPEC);
80 	fprintf(fp, "\t{ 0, 0, 0 }\n");
81 	fprintf(fp, "};\n");
82 	fclose(fp);
83 	return (swap);
84 }
85 
86 static	int devtablenotread = 1;
87 static	struct devdescription {
88 	char	*dev_name;
89 	int	dev_major;
90 	struct	devdescription *dev_next;
91 } *devtable;
92 
93 /*
94  * Given a device name specification figure out:
95  *	major device number
96  *	partition
97  *	device name
98  *	unit number
99  * This is a hack, but the system still thinks in
100  * terms of major/minor instead of string names.
101  */
102 dev_t
103 nametodev(name, defunit, defpartition)
104 	char *name;
105 	int defunit;
106 	char defpartition;
107 {
108 	char *cp, partition;
109 	int unit;
110 	register struct devdescription *dp;
111 
112 	cp = name;
113 	if (cp == 0) {
114 		fprintf(stderr, "config: internal error, nametodev\n");
115 		exit(1);
116 	}
117 	while (*cp && !isdigit(*cp))
118 		cp++;
119 	unit = *cp ? atoi(cp) : defunit;
120 	if (unit < 0 || unit > 31) {
121 		fprintf(stderr,
122 "config: %s: invalid device specification, unit out of range\n", name);
123 		unit = defunit;			/* carry on more checking */
124 	}
125 	if (*cp) {
126 		*cp++ = '\0';
127 		while (*cp && isdigit(*cp))
128 			cp++;
129 	}
130 	partition = *cp ? *cp : defpartition;
131 	if (partition < 'a' || partition > 'h') {
132 		fprintf(stderr,
133 "config: %c: invalid device specification, bad partition\n", *cp);
134 		partition = defpartition;	/* carry on */
135 	}
136 	if (devtablenotread)
137 		initdevtable();
138 	for (dp = devtable; dp; dp = dp->dev_next)
139 		if (eq(name, dp->dev_name))
140 			break;
141 	if (dp == 0) {
142 		fprintf(stderr, "config: %s: unknown device\n", name);
143 		return (NODEV);
144 	}
145 	return (makedev(dp->dev_major, (unit << 3) + (partition - 'a')));
146 }
147 
148 char *
149 devtoname(dev)
150 	dev_t dev;
151 {
152 	char buf[80];
153 	register struct devdescription *dp;
154 
155 	if (devtablenotread)
156 		initdevtable();
157 	for (dp = devtable; dp; dp = dp->dev_next)
158 		if (major(dev) == dp->dev_major)
159 			break;
160 	if (dp == 0)
161 		dp = devtable;
162 	(void) sprintf(buf, "%s%d%c", dp->dev_name,
163 		minor(dev) >> 3, (minor(dev) & 07) + 'a');
164 	return (ns(buf));
165 }
166 
167 initdevtable()
168 {
169 	char buf[BUFSIZ];
170 	int maj;
171 	register struct devdescription **dp = &devtable;
172 	FILE *fp;
173 
174 	(void) sprintf(buf, "../conf/devices.%s", machinename);
175 	fp = fopen(buf, "r");
176 	if (fp == NULL) {
177 		fprintf(stderr, "config: can't open %s\n", buf);
178 		exit(1);
179 	}
180 	while (fscanf(fp, "%s\t%d\n", buf, &maj) == 2) {
181 		*dp = (struct devdescription *)malloc(sizeof (**dp));
182 		(*dp)->dev_name = ns(buf);
183 		(*dp)->dev_major = maj;
184 		dp = &(*dp)->dev_next;
185 	}
186 	*dp = 0;
187 	fclose(fp);
188 	devtablenotread = 0;
189 }
190