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