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