Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / config / mkswap.c
blob5dc86f845377442c1ae1ad300612982aeefa686a
1 /* $NetBSD: mkswap.c,v 1.6 2008/12/28 01:23:46 christos Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
40 * from: @(#)mkswap.c 8.1 (Berkeley) 6/6/93
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
47 #include <sys/param.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <err.h>
53 #include "defs.h"
54 #include "sem.h"
56 static char *mkdevstr(dev_t);
57 static int mkoneswap(struct config *);
60 * Make the various swap*.c files. Nothing to do for generic swap.
62 int
63 mkswap(void)
65 struct config *cf;
67 TAILQ_FOREACH(cf, &allcf, cf_next) {
68 if (mkoneswap(cf))
69 return (1);
71 return (0);
74 static char *
75 mkdevstr(dev_t d)
77 static char buf[32];
79 if (d == NODEV)
80 (void)snprintf(buf, sizeof(buf), "NODEV");
81 else
82 (void)snprintf(buf, sizeof(buf), "makedev(%d, %d)",
83 major(d), minor(d));
84 return buf;
87 static int
88 mkoneswap(struct config *cf)
90 struct nvlist *nv;
91 FILE *fp;
92 char fname[200], tname[200];
93 char specinfo[200];
95 (void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
96 (void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
97 if ((fp = fopen(tname, "w")) == NULL) {
98 warn("cannot open %s", fname);
99 return (1);
102 autogen_comment(fp, fname);
104 fputs("#include <sys/param.h>\n"
105 "#include <sys/conf.h>\n\n", fp);
107 * Emit the root device.
109 nv = cf->cf_root;
110 if (cf->cf_root->nv_str == s_qmark)
111 strlcpy(specinfo, "NULL", sizeof(specinfo));
112 else
113 snprintf(specinfo, sizeof(specinfo), "\"%s\"",
114 cf->cf_root->nv_str);
115 fprintf(fp, "const char *rootspec = %s;\n", specinfo);
116 fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
117 mkdevstr(nv->nv_num),
118 nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str);
121 * Emit the dump device.
123 nv = cf->cf_dump;
124 if (cf->cf_dump == NULL)
125 strlcpy(specinfo, "NULL", sizeof(specinfo));
126 else
127 snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
128 fprintf(fp, "const char *dumpspec = %s;\n", specinfo);
129 fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
130 nv ? mkdevstr(nv->nv_num) : "NODEV",
131 nv ? nv->nv_str : "unspecified");
134 * Emit the root file system.
136 fprintf(fp, "const char *rootfstype = \"%s\";\n",
137 cf->cf_fstype ? cf->cf_fstype : "?");
139 fflush(fp);
140 if (ferror(fp))
141 goto wrerror;
143 if (fclose(fp)) {
144 fp = NULL;
145 goto wrerror;
147 if (moveifchanged(tname, fname) != 0) {
148 warn("error renaming %s", fname);
149 return (1);
151 return (0);
153 wrerror:
154 warn("error writing %s", fname);
155 if (fp != NULL)
156 (void)fclose(fp);
157 #if 0
158 (void)unlink(fname);
159 #endif
160 return (1);