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