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