diff options
author | Jason Downs <downsj@cvs.openbsd.org> | 1997-07-06 03:54:07 +0000 |
---|---|---|
committer | Jason Downs <downsj@cvs.openbsd.org> | 1997-07-06 03:54:07 +0000 |
commit | 23edd72b858f60176cbb3ec7ebd8ab078ecc823c (patch) | |
tree | 1406d7074bf99c30869e74f5e3e7c9921f315d68 /usr.sbin/config/mkheaders.c | |
parent | a392bf7c2557809c971d9fcc9e2dd5dbd7665470 (diff) |
Add defopt support, from NetBSD; thorpej (I think, I don't have the commit
message).
Diffstat (limited to 'usr.sbin/config/mkheaders.c')
-rw-r--r-- | usr.sbin/config/mkheaders.c | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/usr.sbin/config/mkheaders.c b/usr.sbin/config/mkheaders.c index eb327da7a17..18e532deca4 100644 --- a/usr.sbin/config/mkheaders.c +++ b/usr.sbin/config/mkheaders.c @@ -1,5 +1,5 @@ -/* $OpenBSD: mkheaders.c,v 1.4 1996/10/23 22:37:54 niklas Exp $ */ -/* $NetBSD: mkheaders.c,v 1.11 1996/08/31 20:58:22 mycroft Exp $ */ +/* $OpenBSD: mkheaders.c,v 1.5 1997/07/06 03:54:05 downsj Exp $ */ +/* $NetBSD: mkheaders.c,v 1.12 1997/02/02 21:12:34 thorpej Exp $ */ /* * Copyright (c) 1992, 1993 @@ -54,6 +54,7 @@ #include "config.h" static int emitcnt __P((struct nvlist *)); +static int emitopt __P((struct nvlist *)); static int err __P((const char *, char *, FILE *)); static char *cntname __P((const char *)); @@ -64,6 +65,7 @@ int mkheaders() { register struct files *fi; + register struct nvlist *nv; for (fi = allfiles; fi != NULL; fi = fi->fi_next) { if (fi->fi_flags & FI_HIDDEN) @@ -72,6 +74,11 @@ mkheaders() emitcnt(fi->fi_optf)) return (1); } + + for (nv = defoptions; nv != NULL; nv = nv->nv_next) + if (emitopt(nv)) + return (1); + return (0); } @@ -120,6 +127,64 @@ writeit: } static int +emitopt(nv) + struct nvlist *nv; +{ + struct nvlist *option; + char new_contents[BUFSIZ], buf[BUFSIZ]; + char fname[BUFSIZ], *p, c; + const char *n; + int nlines; + FILE *fp; + + /* + * Generate the new contents of the file. + */ + p = new_contents; + if ((option = ht_lookup(opttab, nv->nv_str)) == NULL) + p += sprintf(p, "/* option `%s' not defined */\n", + nv->nv_str); + else { + p += sprintf(p, "#define\t%s", option->nv_name); + if (option->nv_str != NULL) + p += sprintf(p, "\t%s", option->nv_str); + p += sprintf(p, "\n"); + } + + /* + * Compare the new file to the old. + */ + sprintf(fname, "opt_%s.h", nv->nv_name); + if ((fp = fopen(fname, "r")) == NULL) + goto writeit; + nlines = 0; + while (fgets(buf, sizeof(buf), fp) != NULL) { + if (++nlines != 1 || + strcmp(buf, new_contents) != 0) + goto writeit; + } + if (ferror(fp)) + return (err("read", fname, fp)); + (void)fclose(fp); + if (nlines == 1) + return (0); +writeit: + /* + * They're different, or the file doesn't exist. + */ + if ((fp = fopen(fname, "w")) == NULL) { + (void)fprintf(stderr, "config: cannot write %s: %s\n", + fname, strerror(errno)); + return (1); + } + if (fprintf(fp, "%s", new_contents) < 0) + return (err("writ", fname, fp)); + if (fclose(fp)) + return (err("writ", fname, fp)); + return (0); +} + +static int err(what, fname, fp) const char *what; char *fname; @@ -130,7 +195,6 @@ err(what, fname, fp) what, fname, strerror(errno)); if (fp) (void)fclose(fp); - free(fname); return (1); } |