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