]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config/main.c
config(8): Add compatibility shims for r335998
[FreeBSD/FreeBSD.git] / usr.sbin / config / main.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35         The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c      8.1 (Berkeley) 6/6/93";
41 #endif
42 static const char rcsid[] =
43   "$FreeBSD$";
44 #endif /* not lint */
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/sbuf.h>
49 #include <sys/file.h>
50 #include <sys/mman.h>
51 #include <sys/param.h>
52
53 #include <assert.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <sysexits.h>
59 #include <unistd.h>
60 #include <dirent.h>
61 #include "y.tab.h"
62 #include "config.h"
63 #include "configvers.h"
64
65 #ifndef TRUE
66 #define TRUE    (1)
67 #endif
68
69 #ifndef FALSE
70 #define FALSE   (0)
71 #endif
72
73 #define CDIR    "../compile/"
74
75 char *  PREFIX;
76 char    destdir[MAXPATHLEN];
77 char    srcdir[MAXPATHLEN];
78
79 int     debugging;
80 int     profiling;
81 int     found_defaults;
82 int     incignore;
83
84 /*
85  * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
86  * literally).
87  */
88 int     filebased = 0;
89 int     versreq;
90
91 static void configfile(void);
92 static void get_srcdir(void);
93 static void usage(void);
94 static void cleanheaders(char *);
95 static void kernconfdump(const char *);
96 static void checkversion(void);
97 extern int yyparse(void);
98
99 struct hdr_list {
100         char *h_name;
101         struct hdr_list *h_next;
102 } *htab;
103
104 /*
105  * Config builds a set of files for building a UNIX
106  * system given a description of the desired system.
107  */
108 int
109 main(int argc, char **argv)
110 {
111
112         struct stat buf;
113         int ch, len;
114         char *p;
115         char *kernfile;
116         struct includepath* ipath;
117         int printmachine;
118
119         printmachine = 0;
120         kernfile = NULL;
121         SLIST_INIT(&includepath);
122         while ((ch = getopt(argc, argv, "CI:d:gmps:Vx:")) != -1)
123                 switch (ch) {
124                 case 'C':
125                         filebased = 1;
126                         break;
127                 case 'I':
128                         ipath = (struct includepath *) \
129                                 calloc(1, sizeof (struct includepath));
130                         if (ipath == NULL)
131                                 err(EXIT_FAILURE, "calloc");
132                         ipath->path = optarg;
133                         SLIST_INSERT_HEAD(&includepath, ipath, path_next);
134                         break;
135                 case 'm':
136                         printmachine = 1;
137                         break;
138                 case 'd':
139                         if (*destdir == '\0')
140                                 strlcpy(destdir, optarg, sizeof(destdir));
141                         else
142                                 errx(EXIT_FAILURE, "directory already set");
143                         break;
144                 case 'g':
145                         debugging++;
146                         break;
147                 case 'p':
148                         profiling++;
149                         break;
150                 case 's':
151                         if (*srcdir == '\0')
152                                 strlcpy(srcdir, optarg, sizeof(srcdir));
153                         else
154                                 errx(EXIT_FAILURE, "src directory already set");
155                         break;
156                 case 'V':
157                         printf("%d\n", CONFIGVERS);
158                         exit(0);
159                 case 'x':
160                         kernfile = optarg;
161                         break;
162                 case '?':
163                 default:
164                         usage();
165                 }
166         argc -= optind;
167         argv += optind;
168
169         if (kernfile != NULL) {
170                 kernconfdump(kernfile);
171                 exit(EXIT_SUCCESS);
172         }
173
174         if (argc != 1)
175                 usage();
176
177         PREFIX = *argv;
178         if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
179                 err(2, "%s", PREFIX);
180         if (freopen("DEFAULTS", "r", stdin) != NULL) {
181                 found_defaults = 1;
182                 yyfile = "DEFAULTS";
183         } else {
184                 if (freopen(PREFIX, "r", stdin) == NULL)
185                         err(2, "%s", PREFIX);
186                 yyfile = PREFIX;
187         }
188         if (*destdir != '\0') {
189                 len = strlen(destdir);
190                 while (len > 1 && destdir[len - 1] == '/')
191                         destdir[--len] = '\0';
192                 if (*srcdir == '\0')
193                         get_srcdir();
194         } else {
195                 strlcpy(destdir, CDIR, sizeof(destdir));
196                 strlcat(destdir, PREFIX, sizeof(destdir));
197         }
198
199         SLIST_INIT(&cputype);
200         SLIST_INIT(&mkopt);
201         SLIST_INIT(&opt);
202         SLIST_INIT(&rmopts);
203         STAILQ_INIT(&cfgfiles);
204         STAILQ_INIT(&dtab);
205         STAILQ_INIT(&fntab);
206         STAILQ_INIT(&ftab);
207         STAILQ_INIT(&hints);
208         STAILQ_INIT(&envvars);
209         if (yyparse())
210                 exit(3);
211
212         /*
213          * Ensure that required elements (machine, cpu, ident) are present.
214          */
215         if (machinename == NULL) {
216                 printf("Specify machine type, e.g. ``machine i386''\n");
217                 exit(1);
218         }
219         if (ident == NULL) {
220                 printf("no ident line specified\n");
221                 exit(1);
222         }
223         if (SLIST_EMPTY(&cputype)) {
224                 printf("cpu type must be specified\n");
225                 exit(1);
226         }
227         checkversion();
228
229         if (printmachine) {
230                 printf("%s\t%s\n",machinename,machinearch);
231                 exit(0);
232         }
233
234         /* Make compile directory */
235         p = path((char *)NULL);
236         if (stat(p, &buf)) {
237                 if (mkdir(p, 0777))
238                         err(2, "%s", p);
239         } else if (!S_ISDIR(buf.st_mode))
240                 errx(EXIT_FAILURE, "%s isn't a directory", p);
241
242         configfile();                   /* put config file into kernel*/
243         options();                      /* make options .h files */
244         makefile();                     /* build Makefile */
245         makeenv();                      /* build env.c */
246         makehints();                    /* build hints.c */
247         headers();                      /* make a lot of .h files */
248         cleanheaders(p);
249         printf("Kernel build directory is %s\n", p);
250         printf("Don't forget to do ``make cleandepend && make depend''\n");
251         exit(0);
252 }
253
254 /*
255  * get_srcdir
256  *      determine the root of the kernel source tree
257  *      and save that in srcdir.
258  */
259 static void
260 get_srcdir(void)
261 {
262         struct stat lg, phy;
263         char *p, *pwd;
264         int i;
265
266         if (realpath("../..", srcdir) == NULL)
267                 err(EXIT_FAILURE, "Unable to find root of source tree");
268         if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
269             (pwd = strdup(pwd)) != NULL) {
270                 /* Remove the last two path components. */
271                 for (i = 0; i < 2; i++) {
272                         if ((p = strrchr(pwd, '/')) == NULL) {
273                                 free(pwd);
274                                 return;
275                         }
276                         *p = '\0';
277                 }
278                 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
279                     lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
280                         strlcpy(srcdir, pwd, MAXPATHLEN);
281                 free(pwd);
282         }
283 }
284
285 static void
286 usage(void)
287 {
288
289         fprintf(stderr,
290             "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
291         fprintf(stderr, "       config -x kernel\n");
292         exit(EX_USAGE);
293 }
294
295 /*
296  * get_word
297  *      returns EOF on end of file
298  *      NULL on end of line
299  *      pointer to the word otherwise
300  */
301 char *
302 get_word(FILE *fp)
303 {
304         static char line[80];
305         int ch;
306         char *cp;
307         int escaped_nl = 0;
308
309 begin:
310         while ((ch = getc(fp)) != EOF)
311                 if (ch != ' ' && ch != '\t')
312                         break;
313         if (ch == EOF)
314                 return ((char *)EOF);
315         if (ch == '\\'){
316                 escaped_nl = 1;
317                 goto begin;
318         }
319         if (ch == '\n') {
320                 if (escaped_nl){
321                         escaped_nl = 0;
322                         goto begin;
323                 }
324                 else
325                         return (NULL);
326         }
327         cp = line;
328         *cp++ = ch;
329         /* Negation operator is a word by itself. */
330         if (ch == '!') {
331                 *cp = 0;
332                 return (line);
333         }
334         while ((ch = getc(fp)) != EOF) {
335                 if (isspace(ch))
336                         break;
337                 *cp++ = ch;
338         }
339         *cp = 0;
340         if (ch == EOF)
341                 return ((char *)EOF);
342         (void) ungetc(ch, fp);
343         return (line);
344 }
345
346 /*
347  * get_quoted_word
348  *      like get_word but will accept something in double or single quotes
349  *      (to allow embedded spaces).
350  */
351 char *
352 get_quoted_word(FILE *fp)
353 {
354         static char line[256];
355         int ch;
356         char *cp;
357         int escaped_nl = 0;
358
359 begin:
360         while ((ch = getc(fp)) != EOF)
361                 if (ch != ' ' && ch != '\t')
362                         break;
363         if (ch == EOF)
364                 return ((char *)EOF);
365         if (ch == '\\'){
366                 escaped_nl = 1;
367                 goto begin;
368         }
369         if (ch == '\n') {
370                 if (escaped_nl){
371                         escaped_nl = 0;
372                         goto begin;
373                 }
374                 else
375                         return (NULL);
376         }
377         cp = line;
378         if (ch == '"' || ch == '\'') {
379                 int quote = ch;
380
381                 escaped_nl = 0;
382                 while ((ch = getc(fp)) != EOF) {
383                         if (ch == quote && !escaped_nl)
384                                 break;
385                         if (ch == '\n' && !escaped_nl) {
386                                 *cp = 0;
387                                 printf("config: missing quote reading `%s'\n",
388                                         line);
389                                 exit(2);
390                         }
391                         if (ch == '\\' && !escaped_nl) {
392                                 escaped_nl = 1;
393                                 continue;
394                         }
395                         if (ch != quote && escaped_nl)
396                                 *cp++ = '\\';
397                         *cp++ = ch;
398                         escaped_nl = 0;
399                 }
400         } else {
401                 *cp++ = ch;
402                 while ((ch = getc(fp)) != EOF) {
403                         if (isspace(ch))
404                                 break;
405                         *cp++ = ch;
406                 }
407                 if (ch != EOF)
408                         (void) ungetc(ch, fp);
409         }
410         *cp = 0;
411         if (ch == EOF)
412                 return ((char *)EOF);
413         return (line);
414 }
415
416 /*
417  * prepend the path to a filename
418  */
419 char *
420 path(const char *file)
421 {
422         char *cp = NULL;
423
424         if (file)
425                 asprintf(&cp, "%s/%s", destdir, file);
426         else
427                 cp = strdup(destdir);
428         return (cp);
429 }
430
431 /*
432  * Generate configuration file based on actual settings. With this mode, user
433  * will be able to obtain and build conifguration file with one command.
434  */
435 static void
436 configfile_dynamic(struct sbuf *sb)
437 {
438         struct cputype *cput;
439         struct device *d;
440         struct opt *ol;
441         char *lend;
442         unsigned int i;
443
444         asprintf(&lend, "\\n\\\n");
445         assert(lend != NULL);
446         sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
447         sbuf_printf(sb, "ident\t%s%s", ident, lend);
448         sbuf_printf(sb, "machine\t%s%s", machinename, lend);
449         SLIST_FOREACH(cput, &cputype, cpu_next)
450                 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
451         SLIST_FOREACH(ol, &mkopt, op_next)
452                 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
453                     ol->op_value, lend);
454         SLIST_FOREACH(ol, &opt, op_next) {
455                 if (strncmp(ol->op_name, "DEV_", 4) == 0)
456                         continue;
457                 sbuf_printf(sb, "options\t%s", ol->op_name);
458                 if (ol->op_value != NULL) {
459                         sbuf_putc(sb, '=');
460                         for (i = 0; i < strlen(ol->op_value); i++) {
461                                 if (ol->op_value[i] == '"')
462                                         sbuf_printf(sb, "\\%c",
463                                             ol->op_value[i]);
464                                 else
465                                         sbuf_printf(sb, "%c",
466                                             ol->op_value[i]);
467                         }
468                         sbuf_printf(sb, "%s", lend);
469                 } else {
470                         sbuf_printf(sb, "%s", lend);
471                 }
472         }
473         /*
474          * Mark this file as containing everything we need.
475          */
476         STAILQ_FOREACH(d, &dtab, d_next)
477                 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
478         free(lend);
479 }
480
481 /*
482  * Generate file from the configuration files.
483  */
484 static void
485 configfile_filebased(struct sbuf *sb)
486 {
487         FILE *cff;
488         struct cfgfile *cf;
489         int i;
490
491         /*
492          * Try to read all configuration files. Since those will be present as
493          * C string in the macro, we have to slash their ends then the line
494          * wraps.
495          */
496         STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
497                 cff = fopen(cf->cfg_path, "r");
498                 if (cff == NULL) {
499                         warn("Couldn't open file %s", cf->cfg_path);
500                         continue;
501                 }
502                 while ((i = getc(cff)) != EOF) {
503                         if (i == '\n')
504                                 sbuf_printf(sb, "\\n\\\n");
505                         else if (i == '"' || i == '\'')
506                                 sbuf_printf(sb, "\\%c", i);
507                         else
508                                 sbuf_putc(sb, i);
509                 }
510                 fclose(cff);
511         }
512 }
513
514 static void
515 configfile(void)
516 {
517         FILE *fo;
518         struct sbuf *sb;
519         char *p;
520
521         /* Add main configuration file to the list of files to be included */
522         cfgfile_add(PREFIX);
523         p = path("config.c.new");
524         fo = fopen(p, "w");
525         if (!fo)
526                 err(2, "%s", p);
527         sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
528         assert(sb != NULL);
529         sbuf_clear(sb);
530         if (filebased) {
531                 /* Is needed, can be used for backward compatibility. */
532                 configfile_filebased(sb);
533         } else {
534                 configfile_dynamic(sb);
535         }
536         sbuf_finish(sb);
537         /* 
538          * We print first part of the template, replace our tag with
539          * configuration files content and later continue writing our
540          * template.
541          */
542         p = strstr(kernconfstr, KERNCONFTAG);
543         if (p == NULL)
544                 errx(EXIT_FAILURE, "Something went terribly wrong!");
545         *p = '\0';
546         fprintf(fo, "%s", kernconfstr);
547         fprintf(fo, "%s", sbuf_data(sb));
548         p += strlen(KERNCONFTAG);
549         fprintf(fo, "%s", p);
550         sbuf_delete(sb);
551         fclose(fo);
552         moveifchanged(path("config.c.new"), path("config.c"));
553         cfgfile_removeall();
554 }
555
556 /*
557  * moveifchanged --
558  *      compare two files; rename if changed.
559  */
560 void
561 moveifchanged(const char *from_name, const char *to_name)
562 {
563         char *p, *q;
564         int changed;
565         size_t tsize;
566         struct stat from_sb, to_sb;
567         int from_fd, to_fd;
568
569         changed = 0;
570
571         if ((from_fd = open(from_name, O_RDONLY)) < 0)
572                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
573
574         if ((to_fd = open(to_name, O_RDONLY)) < 0)
575                 changed++;
576
577         if (!changed && fstat(from_fd, &from_sb) < 0)
578                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
579
580         if (!changed && fstat(to_fd, &to_sb) < 0)
581                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
582
583         if (!changed && from_sb.st_size != to_sb.st_size)
584                 changed++;
585
586         tsize = (size_t)from_sb.st_size;
587
588         if (!changed) {
589                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
590                 if (p == MAP_FAILED)
591                         err(EX_OSERR, "mmap %s", from_name);
592                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
593                 if (q == MAP_FAILED)
594                         err(EX_OSERR, "mmap %s", to_name);
595
596                 changed = memcmp(p, q, tsize);
597                 munmap(p, tsize);
598                 munmap(q, tsize);
599         }
600         if (changed) {
601                 if (rename(from_name, to_name) < 0)
602                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
603         } else {
604                 if (unlink(from_name) < 0)
605                         err(EX_OSERR, "unlink(%s)", from_name);
606         }
607 }
608
609 static void
610 cleanheaders(char *p)
611 {
612         DIR *dirp;
613         struct dirent *dp;
614         struct file_list *fl;
615         struct hdr_list *hl;
616         size_t len;
617
618         remember("y.tab.h");
619         remember("setdefs.h");
620         STAILQ_FOREACH(fl, &ftab, f_next)
621                 remember(fl->f_fn);
622
623         /*
624          * Scan the build directory and clean out stuff that looks like
625          * it might have been a leftover NFOO header, etc.
626          */
627         if ((dirp = opendir(p)) == NULL)
628                 err(EX_OSERR, "opendir %s", p);
629         while ((dp = readdir(dirp)) != NULL) {
630                 len = strlen(dp->d_name);
631                 /* Skip non-headers */
632                 if (len < 2 || dp->d_name[len - 2] != '.' ||
633                     dp->d_name[len - 1] != 'h')
634                         continue;
635                 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
636                 if (strchr(dp->d_name, '_') &&
637                     strncmp(dp->d_name, "opt_", 4) != 0)
638                         continue;
639                 /* Check if it is a target file */
640                 for (hl = htab; hl != NULL; hl = hl->h_next) {
641                         if (eq(dp->d_name, hl->h_name)) {
642                                 break;
643                         }
644                 }
645                 if (hl)
646                         continue;
647                 printf("Removing stale header: %s\n", dp->d_name);
648                 if (unlink(path(dp->d_name)) == -1)
649                         warn("unlink %s", dp->d_name);
650         }
651         (void)closedir(dirp);
652 }
653
654 void
655 remember(const char *file)
656 {
657         char *s;
658         struct hdr_list *hl;
659
660         if ((s = strrchr(file, '/')) != NULL)
661                 s = ns(s + 1);
662         else
663                 s = ns(file);
664
665         if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
666                 free(s);
667                 return;
668         }
669         for (hl = htab; hl != NULL; hl = hl->h_next) {
670                 if (eq(s, hl->h_name)) {
671                         free(s);
672                         return;
673                 }
674         }
675         hl = calloc(1, sizeof(*hl));
676         if (hl == NULL)
677                 err(EXIT_FAILURE, "calloc");
678         hl->h_name = s;
679         hl->h_next = htab;
680         htab = hl;
681 }
682
683 /*
684  * This one is quick hack. Will be probably moved to elf(3) interface.
685  * It takes kernel configuration file name, passes it as an argument to
686  * elfdump -a, which output is parsed by some UNIX tools...
687  */
688 static void
689 kernconfdump(const char *file)
690 {
691         struct stat st;
692         FILE *fp, *pp;
693         int error, osz, r;
694         unsigned int i, off, size, t1, t2, align;
695         char *cmd, *o;
696
697         r = open(file, O_RDONLY);
698         if (r == -1)
699                 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
700         error = fstat(r, &st);
701         if (error == -1)
702                 err(EXIT_FAILURE, "fstat() failed");
703         if (S_ISDIR(st.st_mode))
704                 errx(EXIT_FAILURE, "'%s' is a directory", file);
705         fp = fdopen(r, "r");
706         if (fp == NULL)
707                 err(EXIT_FAILURE, "fdopen() failed");
708         osz = 1024;
709         o = calloc(1, osz);
710         if (o == NULL)
711                 err(EXIT_FAILURE, "Couldn't allocate memory");
712         /* ELF note section header. */
713         asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
714             "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
715         if (cmd == NULL)
716                 errx(EXIT_FAILURE, "asprintf() failed");
717         pp = popen(cmd, "r");
718         if (pp == NULL)
719                 errx(EXIT_FAILURE, "popen() failed");
720         free(cmd);
721         (void)fread(o, osz, 1, pp);
722         pclose(pp);
723         r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
724         free(o);
725         if (r != 5)
726                 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
727                     "file. Either unsupported, or not compiled with "
728                     "INCLUDE_CONFIG_FILE", file);
729         r = fseek(fp, off, SEEK_CUR);
730         if (r != 0)
731                 err(EXIT_FAILURE, "fseek() failed");
732         for (i = 0; i < size; i++) {
733                 r = fgetc(fp);
734                 if (r == EOF)
735                         break;
736                 if (r == '\0') {
737                         assert(i == size - 1 &&
738                             ("\\0 found in the middle of a file"));
739                         break;
740                 }
741                 fputc(r, stdout);
742         }
743         fclose(fp);
744 }
745
746 static void 
747 badversion()
748 {
749         fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
750         fprintf(stderr, "config version = %d, ", CONFIGVERS);
751         fprintf(stderr, "version required = %d\n\n", versreq);
752         fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
753         fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
754         fprintf(stderr, "before trying this again.\n\n");
755         fprintf(stderr, "If running the new config fails check your config\n");
756         fprintf(stderr, "file against the GENERIC or LINT config files for\n");
757         fprintf(stderr, "changes in config syntax, or option/device naming\n");
758         fprintf(stderr, "conventions\n\n");
759         exit(1);
760 }
761
762 static void
763 checkversion(void)
764 {
765         FILE *ifp;
766         char line[BUFSIZ];
767
768         ifp = open_makefile_template();
769         while (fgets(line, BUFSIZ, ifp) != 0) {
770                 if (*line != '%')
771                         continue;
772                 if (strncmp(line, "%VERSREQ=", 9) != 0)
773                         continue;
774                 versreq = atoi(line + 9);
775                 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
776                     versreq <= CONFIGVERS)
777                         continue;
778                 badversion();
779         }
780         fclose(ifp);
781 }