xref: /original-bsd/usr.sbin/config.new/mkswap.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)mkswap.c	8.1 (Berkeley) 06/06/93
17  */
18 
19 #include <sys/param.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "config.h"
25 
26 static int mkoneswap __P((struct config *));
27 
28 /*
29  * Make the various swap*.c files.  Nothing to do for generic swap.
30  */
31 int
32 mkswap()
33 {
34 	register struct config *cf;
35 
36 	for (cf = allcf; cf != NULL; cf = cf->cf_next)
37 		if (cf->cf_root != NULL && mkoneswap(cf))
38 			return (1);
39 	return (0);
40 }
41 
42 static int
43 mkoneswap(cf)
44 	register struct config *cf;
45 {
46 	register struct nvlist *nv;
47 	register FILE *fp;
48 	register char *fname;
49 	char buf[200];
50 
51 	(void)sprintf(buf, "swap%s.c", cf->cf_name);
52 	fname = path(buf);
53 	if ((fp = fopen(fname, "w")) == NULL) {
54 		(void)fprintf(stderr, "config: cannot write %s: %s\n",
55 		    fname, strerror(errno));
56 		return (1);
57 	}
58 	if (fputs("\
59 #include <sys/param.h>\n\
60 #include <sys/conf.h>\n\n", fp) < 0)
61 		goto wrerror;
62 	nv = cf->cf_root;
63 	if (fprintf(fp, "dev_t\trootdev = makedev(%d, %d);\t/* %s */\n",
64 	    major(nv->nv_int), minor(nv->nv_int), nv->nv_str) < 0)
65 		goto wrerror;
66 	nv = cf->cf_dump;
67 	if (fprintf(fp, "dev_t\tdumpdev = makedev(%d, %d);\t/* %s */\n",
68 	    major(nv->nv_int), minor(nv->nv_int), nv->nv_str) < 0)
69 		goto wrerror;
70 	if (fputs("\nstruct\tswdevt swdevt[] = {\n", fp) < 0)
71 		goto wrerror;
72 	for (nv = cf->cf_swap; nv != NULL; nv = nv->nv_next)
73 		if (fprintf(fp, "\t{ makedev(%d, %d),\t0,\t0 },\t/* %s */\n",
74 		    major(nv->nv_int), minor(nv->nv_int), nv->nv_str) < 0)
75 			goto wrerror;
76 	if (fputs("\t{ NODEV, 0, 0 }\n};\n", fp) < 0)
77 		goto wrerror;
78 	if (fclose(fp)) {
79 		fp = NULL;
80 		goto wrerror;
81 	}
82 	free(fname);
83 	return (0);
84 wrerror:
85 	(void)fprintf(stderr, "config: error writing %s: %s\n",
86 	    fname, strerror(errno));
87 	if (fp != NULL)
88 		(void)fclose(fp);
89 	/* (void)unlink(fname); */
90 	free(fname);
91 	return (1);
92 }
93