#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: mkswap.c,v 1.10 2015/09/03 13:53:36 uebayasi Exp $");
#include <sys/param.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include "defs.h"
#include "sem.h"
static char *mkdevstr(dev_t);
static int mkoneswap(struct config *);
int
mkswap(void)
{
struct config *cf;
TAILQ_FOREACH(cf, &allcf, cf_next) {
if (mkoneswap(cf))
return (1);
}
return (0);
}
static char *
mkdevstr(dev_t d)
{
static char buf[32];
if (d == NODEV)
(void)snprintf(buf, sizeof(buf), "NODEV");
else
(void)snprintf(buf, sizeof(buf), "makedev(%d, %d)",
major(d), minor(d));
return buf;
}
static int
mkoneswap(struct config *cf)
{
struct nvlist *nv;
FILE *fp;
char fname[200], tname[200];
char specinfo[200];
(void)snprintf(fname, sizeof(fname), "swap%s.c", cf->cf_name);
(void)snprintf(tname, sizeof(tname), "swap%s.c.tmp", cf->cf_name);
if ((fp = fopen(tname, "w")) == NULL) {
warn("cannot open %s", fname);
return (1);
}
autogen_comment(fp, fname);
fputs("#include <sys/param.h>\n"
"#include <sys/conf.h>\n\n", fp);
nv = cf->cf_root;
if (cf->cf_root->nv_str == s_qmark)
strlcpy(specinfo, "NULL", sizeof(specinfo));
else
snprintf(specinfo, sizeof(specinfo), "\"%s\"",
cf->cf_root->nv_str);
fprintf(fp, "const char *rootspec = %s;\n", specinfo);
fprintf(fp, "dev_t\trootdev = %s;\t/* %s */\n\n",
mkdevstr((dev_t)nv->nv_num),
nv->nv_str == s_qmark ? "wildcarded" : nv->nv_str);
nv = cf->cf_dump;
if (cf->cf_dump == NULL)
strlcpy(specinfo, "NULL", sizeof(specinfo));
else
snprintf(specinfo, sizeof(specinfo), "\"%s\"", cf->cf_dump->nv_str);
fprintf(fp, "const char *dumpspec = %s;\n", specinfo);
fprintf(fp, "dev_t\tdumpdev = %s;\t/* %s */\n\n",
nv ? mkdevstr((dev_t)nv->nv_num) : "NODEV",
nv ? nv->nv_str : "unspecified");
fprintf(fp, "const char *rootfstype = \"%s\";\n",
cf->cf_fstype ? cf->cf_fstype : "?");
fflush(fp);
if (ferror(fp))
goto wrerror;
if (fclose(fp)) {
fp = NULL;
goto wrerror;
}
if (moveifchanged(tname, fname) != 0) {
warn("error renaming %s", fname);
return (1);
}
return (0);
wrerror:
warn("error writing %s", fname);
if (fp != NULL)
(void)fclose(fp);
#if 0
(void)unlink(fname);
#endif
return (1);
}