1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)mkswapconf.c 8.1 (Berkeley) 06/06/93";
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
swapconf()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 *
do_swap(fl)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),\t%d,\t%d },\t/* %s */\n",
77 major(dev), minor(dev), swap->f_swapflag,
78 swap->f_swapsize, swap->f_fn);
79 swap = swap->f_next;
80 } while (swap && swap->f_type == SWAPSPEC);
81 fprintf(fp, "\t{ NODEV, 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
nametodev(name,defunit,defpartition)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; 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 *
devtoname(dev)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; dp = dp->dev_next)
159 if (major(dev) == dp->dev_major)
160 break;
161 if (dp == 0)
162 dp = devtable;
163 (void) sprintf(buf, "%s%d%c", dp->dev_name,
164 minor(dev) >> 3, (minor(dev) & 07) + 'a');
165 return (ns(buf));
166 }
167
initdevtable()168 initdevtable()
169 {
170 char buf[BUFSIZ];
171 int maj;
172 register struct devdescription **dp = &devtable;
173 FILE *fp;
174
175 (void) 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