Home | History | Annotate | Line # | Download | only in config
mkheaders.c revision 1.9
      1  1.9       dsl /*	$NetBSD: mkheaders.c,v 1.9 2006/10/04 20:34:48 dsl Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright (c) 1992, 1993
      5  1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * This software was developed by the Computer Systems Engineering group
      8  1.1   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1   thorpej  * contributed to Berkeley.
     10  1.1   thorpej  *
     11  1.1   thorpej  * All advertising materials mentioning features or use of this software
     12  1.1   thorpej  * must display the following acknowledgement:
     13  1.1   thorpej  *	This product includes software developed by the University of
     14  1.1   thorpej  *	California, Lawrence Berkeley Laboratories.
     15  1.1   thorpej  *
     16  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     17  1.1   thorpej  * modification, are permitted provided that the following conditions
     18  1.1   thorpej  * are met:
     19  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     20  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     21  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     23  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     24  1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     25  1.1   thorpej  *    may be used to endorse or promote products derived from this software
     26  1.1   thorpej  *    without specific prior written permission.
     27  1.1   thorpej  *
     28  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1   thorpej  * SUCH DAMAGE.
     39  1.1   thorpej  *
     40  1.1   thorpej  *	from: @(#)mkheaders.c	8.1 (Berkeley) 6/6/93
     41  1.1   thorpej  */
     42  1.1   thorpej 
     43  1.1   thorpej #if HAVE_NBTOOL_CONFIG_H
     44  1.1   thorpej #include "nbtool_config.h"
     45  1.1   thorpej #endif
     46  1.1   thorpej 
     47  1.1   thorpej #include <sys/param.h>
     48  1.1   thorpej #include <ctype.h>
     49  1.1   thorpej #include <errno.h>
     50  1.1   thorpej #include <stdio.h>
     51  1.1   thorpej #include <stdlib.h>
     52  1.1   thorpej #include <string.h>
     53  1.1   thorpej #include <time.h>
     54  1.6  christos #include <util.h>
     55  1.1   thorpej #include "defs.h"
     56  1.1   thorpej 
     57  1.8  christos #ifdef notyet
     58  1.7       dsl #include <crc_extern.h>
     59  1.8  christos #else
     60  1.9       dsl #define fprint_global(fp, name, value)
     61  1.8  christos #endif
     62  1.7       dsl 
     63  1.1   thorpej static int emitcnt(struct nvlist *);
     64  1.1   thorpej static int emitlocs(void);
     65  1.1   thorpej static int emitopts(void);
     66  1.1   thorpej static int emitioconfh(void);
     67  1.1   thorpej static int emittime(void);
     68  1.1   thorpej static int herr(const char *, const char *, FILE *);
     69  1.1   thorpej static int defopts_print(const char *, void *, void *);
     70  1.1   thorpej static char *cntname(const char *);
     71  1.1   thorpej 
     72  1.7       dsl #define UNDEFINED ~0u
     73  1.7       dsl 
     74  1.1   thorpej /*
     75  1.1   thorpej  * Make the various config-generated header files.
     76  1.1   thorpej  */
     77  1.1   thorpej int
     78  1.1   thorpej mkheaders(void)
     79  1.1   thorpej {
     80  1.1   thorpej 	struct files *fi;
     81  1.1   thorpej 
     82  1.1   thorpej 	/*
     83  1.1   thorpej 	 * Make headers containing counts, as needed.
     84  1.1   thorpej 	 */
     85  1.1   thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
     86  1.1   thorpej 		if (fi->fi_flags & FI_HIDDEN)
     87  1.1   thorpej 			continue;
     88  1.1   thorpej 		if (fi->fi_flags & (FI_NEEDSCOUNT | FI_NEEDSFLAG) &&
     89  1.1   thorpej 		    emitcnt(fi->fi_optf))
     90  1.1   thorpej 			return (1);
     91  1.1   thorpej 	}
     92  1.1   thorpej 
     93  1.1   thorpej 	if (emitopts() || emitlocs() || emitioconfh() || emittime())
     94  1.1   thorpej 		return (1);
     95  1.1   thorpej 
     96  1.1   thorpej 	return (0);
     97  1.1   thorpej }
     98  1.1   thorpej 
     99  1.8  christos #ifdef notyet
    100  1.9       dsl static void
    101  1.7       dsl fprint_global(FILE *fp, const char *name, unsigned int value)
    102  1.7       dsl {
    103  1.9       dsl 	fprintf(fp, "#ifdef _LOCORE\n"
    104  1.7       dsl 	    " .global _KERNEL_OPT_%s\n"
    105  1.7       dsl 	    " .set _KERNEL_OPT_%s,0x%x\n"
    106  1.7       dsl 	    "#else\n"
    107  1.7       dsl 	    "__asm(\" .global _KERNEL_OPT_%s\\n"
    108  1.7       dsl 	    " .set _KERNEL_OPT_%s,0x%x\");\n"
    109  1.7       dsl 	    "#endif\n",
    110  1.7       dsl 	    name, name, value,
    111  1.7       dsl 	    name, name, value);
    112  1.7       dsl }
    113  1.7       dsl 
    114  1.7       dsl /* Convert the option argument to a 32bit numder */
    115  1.7       dsl static unsigned int
    116  1.7       dsl global_hash(const char *str)
    117  1.7       dsl {
    118  1.7       dsl         unsigned int h;
    119  1.7       dsl 	char *ep;
    120  1.7       dsl 
    121  1.7       dsl 	/* If the value is a valid numeric, just use it */
    122  1.7       dsl 	h = strtoul(str, &ep, 0);
    123  1.7       dsl 	if (*ep != 0)
    124  1.7       dsl 		/* Otherwise shove through a 32bit CRC function */
    125  1.7       dsl 		h = crc_buf(0, str, strlen(str));
    126  1.7       dsl 
    127  1.7       dsl 	/* Avoid colliding with the value used for undefined options. */
    128  1.7       dsl 	/* At least until I stop any options being set to zero */
    129  1.7       dsl 	return h != UNDEFINED ? h : 0xcafebabe;
    130  1.7       dsl }
    131  1.7       dsl #endif
    132  1.1   thorpej 
    133  1.9       dsl static void
    134  1.1   thorpej fprintcnt(FILE *fp, struct nvlist *nv)
    135  1.1   thorpej {
    136  1.7       dsl 	const char *name = cntname(nv->nv_name);
    137  1.7       dsl 
    138  1.9       dsl 	fprintf(fp, "#define\t%s\t%d\n", name, nv->nv_int);
    139  1.9       dsl 	fprint_global(fp, name, nv->nv_int);
    140  1.1   thorpej }
    141  1.1   thorpej 
    142  1.1   thorpej static int
    143  1.1   thorpej emitcnt(struct nvlist *head)
    144  1.1   thorpej {
    145  1.1   thorpej 	char nfname[BUFSIZ], tfname[BUFSIZ];
    146  1.1   thorpej 	struct nvlist *nv;
    147  1.1   thorpej 	FILE *fp;
    148  1.1   thorpej 
    149  1.1   thorpej 	(void)snprintf(nfname, sizeof(nfname), "%s.h", head->nv_name);
    150  1.1   thorpej 	(void)snprintf(tfname, sizeof(tfname), "tmp_%s", nfname);
    151  1.1   thorpej 
    152  1.1   thorpej 	if ((fp = fopen(tfname, "w")) == NULL)
    153  1.1   thorpej 		return (herr("open", tfname, NULL));
    154  1.1   thorpej 
    155  1.1   thorpej 	for (nv = head; nv != NULL; nv = nv->nv_next)
    156  1.9       dsl 		fprintcnt(fp, nv);
    157  1.9       dsl 
    158  1.9       dsl 	fflush(fp);
    159  1.9       dsl 	if (ferror(fp))
    160  1.9       dsl 		return herr("writ", tfname, fp);
    161  1.1   thorpej 
    162  1.1   thorpej 	if (fclose(fp) == EOF)
    163  1.1   thorpej 		return (herr("clos", tfname, NULL));
    164  1.1   thorpej 
    165  1.1   thorpej 	return (moveifchanged(tfname, nfname));
    166  1.1   thorpej }
    167  1.1   thorpej 
    168  1.1   thorpej /*
    169  1.1   thorpej  * Output a string, preceded by a tab and possibly unescaping any quotes.
    170  1.1   thorpej  * The argument will be output as is if it doesn't start with \".
    171  1.1   thorpej  * Otherwise the first backslash in a \? sequence will be dropped.
    172  1.1   thorpej  */
    173  1.9       dsl static void
    174  1.1   thorpej fprintstr(FILE *fp, const char *str)
    175  1.1   thorpej {
    176  1.1   thorpej 
    177  1.9       dsl 	if (strncmp(str, "\\\"", 2)) {
    178  1.9       dsl 		fprintf(fp, "\t%s", str);
    179  1.9       dsl 		return;
    180  1.9       dsl 	}
    181  1.1   thorpej 
    182  1.9       dsl 	fputc('\t', fp);
    183  1.1   thorpej 
    184  1.9       dsl 	for (; *str; str++) {
    185  1.1   thorpej 		switch (*str) {
    186  1.1   thorpej 		case '\\':
    187  1.1   thorpej 			if (!*++str)				/* XXX */
    188  1.1   thorpej 				str--;
    189  1.1   thorpej 		default:
    190  1.9       dsl 			fputc(*str, fp);
    191  1.1   thorpej 			break;
    192  1.1   thorpej 		}
    193  1.1   thorpej 	}
    194  1.1   thorpej }
    195  1.1   thorpej 
    196  1.1   thorpej /*
    197  1.1   thorpej  * Callback function for walking the option file hash table.  We write out
    198  1.1   thorpej  * the options defined for this file.
    199  1.1   thorpej  */
    200  1.1   thorpej static int
    201  1.1   thorpej defopts_print(const char *name, void *value, void *arg)
    202  1.1   thorpej {
    203  1.1   thorpej 	char tfname[BUFSIZ];
    204  1.1   thorpej 	struct nvlist *nv, *option;
    205  1.7       dsl 	const char *opt_value;
    206  1.1   thorpej 	int isfsoption;
    207  1.1   thorpej 	FILE *fp;
    208  1.1   thorpej 
    209  1.1   thorpej 	(void)snprintf(tfname, sizeof(tfname), "tmp_%s", name);
    210  1.1   thorpej 	if ((fp = fopen(tfname, "w")) == NULL)
    211  1.1   thorpej 		return (herr("open", tfname, NULL));
    212  1.1   thorpej 
    213  1.1   thorpej 	for (nv = value; nv != NULL; nv = nv->nv_next) {
    214  1.1   thorpej 		isfsoption = OPT_FSOPT(nv->nv_name);
    215  1.1   thorpej 
    216  1.7       dsl 		if (nv->nv_flags & NV_OBSOLETE) {
    217  1.9       dsl 			fprintf(fp, "/* %s `%s' is obsolete */\n",
    218  1.1   thorpej 			    isfsoption ? "file system" : "option",
    219  1.9       dsl 			    nv->nv_name);
    220  1.9       dsl 			fprint_global(fp, nv->nv_name, 0xdeadbeef);
    221  1.7       dsl 			continue;
    222  1.7       dsl 		}
    223  1.7       dsl 
    224  1.7       dsl 		if (((option = ht_lookup(opttab, nv->nv_name)) == NULL &&
    225  1.7       dsl 		    (option = ht_lookup(fsopttab, nv->nv_name)) == NULL) &&
    226  1.7       dsl 		    (nv->nv_str == NULL)) {
    227  1.9       dsl 			fprintf(fp, "/* %s `%s' not defined */\n",
    228  1.7       dsl 			    isfsoption ? "file system" : "option",
    229  1.9       dsl 			    nv->nv_name);
    230  1.9       dsl 			fprint_global(fp, nv->nv_name, UNDEFINED);
    231  1.7       dsl 			continue;
    232  1.1   thorpej 		}
    233  1.7       dsl 
    234  1.7       dsl 		opt_value = option != NULL ? option->nv_str : nv->nv_str;
    235  1.7       dsl 		if (isfsoption == 1)
    236  1.7       dsl 			/* For filesysteme we'd output the lower case name */
    237  1.7       dsl 			opt_value = NULL;
    238  1.7       dsl 
    239  1.9       dsl 		fprintf(fp, "#define\t%s", nv->nv_name);
    240  1.9       dsl 		if (opt_value != NULL)
    241  1.9       dsl 			fprintstr(fp, opt_value);
    242  1.9       dsl 		fputc('\n', fp);
    243  1.9       dsl 		fprint_global(fp, nv->nv_name,
    244  1.9       dsl 		    opt_value == NULL ? 1 : global_hash(opt_value));
    245  1.1   thorpej 	}
    246  1.1   thorpej 
    247  1.9       dsl 	fflush(fp);
    248  1.9       dsl 	if (ferror(fp))
    249  1.9       dsl 		return herr("writ", tfname, fp);
    250  1.9       dsl 
    251  1.1   thorpej 	if (fclose(fp) == EOF)
    252  1.1   thorpej 		return (herr("clos", tfname, NULL));
    253  1.1   thorpej 
    254  1.1   thorpej 	return (moveifchanged(tfname, name));
    255  1.1   thorpej }
    256  1.1   thorpej 
    257  1.1   thorpej /*
    258  1.1   thorpej  * Emit the option header files.
    259  1.1   thorpej  */
    260  1.1   thorpej static int
    261  1.1   thorpej emitopts(void)
    262  1.1   thorpej {
    263  1.1   thorpej 
    264  1.1   thorpej 	return (ht_enumerate(optfiletab, defopts_print, NULL));
    265  1.1   thorpej }
    266  1.1   thorpej 
    267  1.1   thorpej /*
    268  1.1   thorpej  * A callback function for walking the attribute hash table.
    269  1.1   thorpej  * Emit CPP definitions of manifest constants for the locators on the
    270  1.1   thorpej  * "name" attribute node (passed as the "value" parameter).
    271  1.1   thorpej  */
    272  1.1   thorpej static int
    273  1.1   thorpej locators_print(const char *name, void *value, void *arg)
    274  1.1   thorpej {
    275  1.1   thorpej 	struct attr *a;
    276  1.1   thorpej 	struct nvlist *nv;
    277  1.1   thorpej 	int i;
    278  1.1   thorpej 	char *locdup, *namedup;
    279  1.1   thorpej 	char *cp;
    280  1.1   thorpej 	FILE *fp = arg;
    281  1.1   thorpej 
    282  1.1   thorpej 	a = value;
    283  1.1   thorpej 	if (a->a_locs) {
    284  1.1   thorpej 		if (strchr(name, ' ') != NULL || strchr(name, '\t') != NULL)
    285  1.1   thorpej 			/*
    286  1.1   thorpej 			 * name contains a space; we can't generate
    287  1.1   thorpej 			 * usable defines, so ignore it.
    288  1.1   thorpej 			 */
    289  1.1   thorpej 			return 0;
    290  1.1   thorpej 		locdup = estrdup(name);
    291  1.1   thorpej 		for (cp = locdup; *cp; cp++)
    292  1.1   thorpej 			if (islower((unsigned char)*cp))
    293  1.1   thorpej 				*cp = toupper((unsigned char)*cp);
    294  1.1   thorpej 		for (i = 0, nv = a->a_locs; nv; nv = nv->nv_next, i++) {
    295  1.1   thorpej 			if (strchr(nv->nv_name, ' ') != NULL ||
    296  1.1   thorpej 			    strchr(nv->nv_name, '\t') != NULL)
    297  1.1   thorpej 				/*
    298  1.1   thorpej 				 * name contains a space; we can't generate
    299  1.1   thorpej 				 * usable defines, so ignore it.
    300  1.1   thorpej 				 */
    301  1.1   thorpej 				continue;
    302  1.1   thorpej 			namedup = estrdup(nv->nv_name);
    303  1.1   thorpej 			for (cp = namedup; *cp; cp++)
    304  1.1   thorpej 				if (islower((unsigned char)*cp))
    305  1.1   thorpej 					*cp = toupper((unsigned char)*cp);
    306  1.1   thorpej 				else if (*cp == ARRCHR)
    307  1.1   thorpej 					*cp = '_';
    308  1.9       dsl 			fprintf(fp, "#define %sCF_%s %d\n", locdup, namedup, i);
    309  1.9       dsl 			if (nv->nv_str != NULL)
    310  1.9       dsl 				fprintf(fp, "#define %sCF_%s_DEFAULT %s\n",
    311  1.9       dsl 				    locdup, namedup, nv->nv_str);
    312  1.1   thorpej 			free(namedup);
    313  1.1   thorpej 		}
    314  1.3  drochner 		/* assert(i == a->a_loclen) */
    315  1.9       dsl 		fprintf(fp, "#define %sCF_NLOCS %d\n", locdup, a->a_loclen);
    316  1.1   thorpej 		free(locdup);
    317  1.1   thorpej 	}
    318  1.1   thorpej 	return 0;
    319  1.1   thorpej }
    320  1.1   thorpej 
    321  1.1   thorpej /*
    322  1.1   thorpej  * Build the "locators.h" file with manifest constants for all potential
    323  1.1   thorpej  * locators in the configuration.  Do this by enumerating the attribute
    324  1.1   thorpej  * hash table and emitting all the locators for each attribute.
    325  1.1   thorpej  */
    326  1.1   thorpej static int
    327  1.1   thorpej emitlocs(void)
    328  1.1   thorpej {
    329  1.1   thorpej 	char *tfname;
    330  1.1   thorpej 	int rval;
    331  1.1   thorpej 	FILE *tfp;
    332  1.1   thorpej 
    333  1.1   thorpej 	tfname = "tmp_locators.h";
    334  1.1   thorpej 	if ((tfp = fopen(tfname, "w")) == NULL)
    335  1.1   thorpej 		return (herr("open", tfname, NULL));
    336  1.1   thorpej 
    337  1.1   thorpej 	rval = ht_enumerate(attrtab, locators_print, tfp);
    338  1.9       dsl 
    339  1.9       dsl 	fflush(tfp);
    340  1.9       dsl 	if (ferror(tfp))
    341  1.9       dsl 		return (herr("writ", tfname, NULL));
    342  1.1   thorpej 	if (fclose(tfp) == EOF)
    343  1.1   thorpej 		return (herr("clos", tfname, NULL));
    344  1.1   thorpej 	if (rval)
    345  1.1   thorpej 		return (rval);
    346  1.1   thorpej 	return (moveifchanged(tfname, "locators.h"));
    347  1.1   thorpej }
    348  1.1   thorpej 
    349  1.1   thorpej /*
    350  1.1   thorpej  * Build the "ioconf.h" file with extern declarations for all configured
    351  1.1   thorpej  * cfdrivers.
    352  1.1   thorpej  */
    353  1.1   thorpej static int
    354  1.1   thorpej emitioconfh(void)
    355  1.1   thorpej {
    356  1.1   thorpej 	const char *tfname;
    357  1.1   thorpej 	FILE *tfp;
    358  1.1   thorpej 	struct devbase *d;
    359  1.1   thorpej 
    360  1.1   thorpej 	tfname = "tmp_ioconf.h";
    361  1.1   thorpej 	if ((tfp = fopen(tfname, "w")) == NULL)
    362  1.1   thorpej 		return (herr("open", tfname, NULL));
    363  1.1   thorpej 
    364  1.1   thorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
    365  1.1   thorpej 		if (!devbase_has_instances(d, WILD))
    366  1.1   thorpej 			continue;
    367  1.9       dsl 		fprintf(tfp, "extern struct cfdriver %s_cd;\n", d->d_name);
    368  1.1   thorpej 	}
    369  1.1   thorpej 
    370  1.9       dsl 	fflush(tfp);
    371  1.9       dsl 	if (ferror(tfp))
    372  1.9       dsl 		return herr("writ", tfname, tfp);
    373  1.9       dsl 
    374  1.1   thorpej 	if (fclose(tfp) == EOF)
    375  1.1   thorpej 		return (herr("clos", tfname, NULL));
    376  1.1   thorpej 
    377  1.1   thorpej 	return (moveifchanged(tfname, "ioconf.h"));
    378  1.1   thorpej }
    379  1.1   thorpej 
    380  1.1   thorpej /*
    381  1.1   thorpej  * Make a file that config_time.h can use as a source, if required.
    382  1.1   thorpej  */
    383  1.1   thorpej static int
    384  1.1   thorpej emittime(void)
    385  1.1   thorpej {
    386  1.1   thorpej 	FILE *fp;
    387  1.1   thorpej 	time_t t;
    388  1.1   thorpej 	struct tm *tm;
    389  1.1   thorpej 	char buf[128];
    390  1.1   thorpej 
    391  1.1   thorpej 	t = time(NULL);
    392  1.1   thorpej 	tm = gmtime(&t);
    393  1.1   thorpej 
    394  1.1   thorpej 	if ((fp = fopen("config_time.src", "w")) == NULL)
    395  1.1   thorpej 		return (herr("open", "config_time.src", NULL));
    396  1.1   thorpej 
    397  1.1   thorpej 	if (strftime(buf, sizeof(buf), "%c %Z", tm) == 0)
    398  1.4      cube 		return (herr("strftime", "config_time.src", fp));
    399  1.1   thorpej 
    400  1.9       dsl 	fprintf(fp, "/* %s */\n"
    401  1.1   thorpej 	    "#define CONFIG_TIME\t%2lld\n"
    402  1.1   thorpej 	    "#define CONFIG_YEAR\t%2d\n"
    403  1.1   thorpej 	    "#define CONFIG_MONTH\t%2d\n"
    404  1.1   thorpej 	    "#define CONFIG_DATE\t%2d\n"
    405  1.1   thorpej 	    "#define CONFIG_HOUR\t%2d\n"
    406  1.1   thorpej 	    "#define CONFIG_MINS\t%2d\n"
    407  1.1   thorpej 	    "#define CONFIG_SECS\t%2d\n",
    408  1.1   thorpej 	    buf, (long long)t,
    409  1.1   thorpej 	    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
    410  1.9       dsl 	    tm->tm_hour, tm->tm_min, tm->tm_sec);
    411  1.9       dsl 
    412  1.9       dsl 	fflush(fp);
    413  1.9       dsl 	if (ferror(fp))
    414  1.4      cube 		return (herr("fprintf", "config_time.src", fp));
    415  1.1   thorpej 
    416  1.1   thorpej 	if (fclose(fp) != 0)
    417  1.9       dsl 		return (herr("clos", "config_time.src", NULL));
    418  1.1   thorpej 
    419  1.1   thorpej 	/*
    420  1.1   thorpej 	 * *Don't* moveifchanged this file.  Makefile.kern.inc will
    421  1.1   thorpej 	 * handle that if it determines such a move is necessary.
    422  1.1   thorpej 	 */
    423  1.1   thorpej 	return (0);
    424  1.1   thorpej }
    425  1.1   thorpej 
    426  1.1   thorpej /*
    427  1.1   thorpej  * Compare two files.  If nfname doesn't exist, or is different from
    428  1.1   thorpej  * tfname, move tfname to nfname.  Otherwise, delete tfname.
    429  1.1   thorpej  */
    430  1.1   thorpej int
    431  1.1   thorpej moveifchanged(const char *tfname, const char *nfname)
    432  1.1   thorpej {
    433  1.1   thorpej 	char tbuf[BUFSIZ], nbuf[BUFSIZ];
    434  1.1   thorpej 	FILE *tfp, *nfp;
    435  1.1   thorpej 
    436  1.1   thorpej 	if ((tfp = fopen(tfname, "r")) == NULL)
    437  1.1   thorpej 		return (herr("open", tfname, NULL));
    438  1.1   thorpej 
    439  1.1   thorpej 	if ((nfp = fopen(nfname, "r")) == NULL)
    440  1.1   thorpej 		goto moveit;
    441  1.1   thorpej 
    442  1.1   thorpej 	while (fgets(tbuf, sizeof(tbuf), tfp) != NULL) {
    443  1.1   thorpej 		if (fgets(nbuf, sizeof(nbuf), nfp) == NULL) {
    444  1.1   thorpej 			/*
    445  1.1   thorpej 			 * Old file has fewer lines.
    446  1.1   thorpej 			 */
    447  1.1   thorpej 			goto moveit;
    448  1.1   thorpej 		}
    449  1.1   thorpej 		if (strcmp(tbuf, nbuf) != 0)
    450  1.1   thorpej 			goto moveit;
    451  1.1   thorpej 	}
    452  1.1   thorpej 
    453  1.1   thorpej 	/*
    454  1.1   thorpej 	 * We've reached the end of the new file.  Check to see if new file
    455  1.1   thorpej 	 * has fewer lines than old.
    456  1.1   thorpej 	 */
    457  1.1   thorpej 	if (fgets(nbuf, sizeof(nbuf), nfp) != NULL) {
    458  1.1   thorpej 		/*
    459  1.1   thorpej 		 * New file has fewer lines.
    460  1.1   thorpej 		 */
    461  1.1   thorpej 		goto moveit;
    462  1.1   thorpej 	}
    463  1.1   thorpej 
    464  1.1   thorpej 	(void) fclose(nfp);
    465  1.1   thorpej 	(void) fclose(tfp);
    466  1.1   thorpej 	if (remove(tfname) == -1)
    467  1.1   thorpej 		return(herr("remov", tfname, NULL));
    468  1.1   thorpej 	return (0);
    469  1.1   thorpej 
    470  1.1   thorpej  moveit:
    471  1.1   thorpej 	/*
    472  1.1   thorpej 	 * They're different, or the file doesn't exist.
    473  1.1   thorpej 	 */
    474  1.1   thorpej 	if (nfp)
    475  1.1   thorpej 		(void) fclose(nfp);
    476  1.1   thorpej 	if (tfp)
    477  1.1   thorpej 		(void) fclose(tfp);
    478  1.1   thorpej 	if (rename(tfname, nfname) == -1)
    479  1.1   thorpej 		return (herr("renam", tfname, NULL));
    480  1.1   thorpej 	return (0);
    481  1.1   thorpej }
    482  1.1   thorpej 
    483  1.1   thorpej static int
    484  1.1   thorpej herr(const char *what, const char *fname, FILE *fp)
    485  1.1   thorpej {
    486  1.1   thorpej 
    487  1.1   thorpej 	(void)fprintf(stderr, "%s: error %sing %s: %s\n",
    488  1.1   thorpej 	    getprogname(), what, fname, strerror(errno));
    489  1.1   thorpej 	if (fp)
    490  1.1   thorpej 		(void)fclose(fp);
    491  1.1   thorpej 	return (1);
    492  1.1   thorpej }
    493  1.1   thorpej 
    494  1.1   thorpej static char *
    495  1.1   thorpej cntname(const char *src)
    496  1.1   thorpej {
    497  1.1   thorpej 	char *dst;
    498  1.1   thorpej 	unsigned char c;
    499  1.1   thorpej 	static char buf[100];
    500  1.1   thorpej 
    501  1.1   thorpej 	dst = buf;
    502  1.1   thorpej 	*dst++ = 'N';
    503  1.1   thorpej 	while ((c = *src++) != 0)
    504  1.1   thorpej 		*dst++ = islower(c) ? toupper(c) : c;
    505  1.1   thorpej 	*dst = 0;
    506  1.1   thorpej 	return (buf);
    507  1.1   thorpej }
    508