2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $
36 * $DragonFly: src/usr.sbin/config/main.c,v 1.5 2003/11/03 19:31:36 eirikn Exp $
39 #include <sys/types.h>
43 #include <sys/param.h>
60 #define CDIR "../../compile/"
63 char destdir[MAXPATHLEN];
64 char srcdir[MAXPATHLEN];
66 static int no_config_clobber = TRUE;
70 static void configfile(void);
71 static void get_srcdir(void);
72 static void usage(void);
75 * Config builds a set of files for building a UNIX
76 * system given a description of the desired system.
88 while ((ch = getopt(argc, argv, "d:gprn")) != -1)
92 strlcpy(destdir, optarg, sizeof(destdir));
94 errx(2, "directory already set");
103 /* no_config_clobber is now true by default, no-op */
105 "*** Using obsolete config option '-n' ***\n");
108 no_config_clobber = FALSE;
120 if (freopen(PREFIX = *argv, "r", stdin) == NULL)
121 err(2, "%s", PREFIX);
123 if (*destdir != '\0') {
124 len = strlen(destdir);
125 while (len > 1 && destdir[len - 1] == '/')
126 destdir[--len] = '\0';
129 strlcpy(destdir, CDIR, sizeof(destdir));
130 strlcat(destdir, PREFIX, sizeof(destdir));
133 p = path((char *)NULL);
138 else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
139 errx(2, "%s isn't a directory", p);
141 else if (!no_config_clobber) {
142 char tmp[strlen(p) + 8];
144 fprintf(stderr, "Removing old directory %s: ", p);
146 snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
148 fprintf(stderr, "Failed!\n");
151 fprintf(stderr, "Done.\n");
164 newbus_ioconf(); /* Print ioconf.c */
168 printf("Specify machine type, e.g. ``machine i386''\n");
172 * make symbolic links in compilation directory
173 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
174 * and similarly for "machine".
177 char xxx[MAXPATHLEN];
179 (void)snprintf(xxx, sizeof(xxx), "../../%s/include",
182 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
183 srcdir, machinename);
184 (void) symlink(xxx, path("machine"));
188 * XXX check directory structure for architecture subdirectories and
189 * create the symlinks automatically XXX
192 char xxx[MAXPATHLEN];
194 (void)snprintf(xxx, sizeof(xxx), "../../../../../net/i4b/include/%s",
197 (void)snprintf(xxx, sizeof(xxx), "%s/net/i4b/include/%s",
198 srcdir, machinename);
199 (void) mkdir(path("net"), 0755);
200 (void) mkdir(path("net/i4b"), 0755);
201 (void) mkdir(path("net/i4b/include"), 0755);
202 (void) symlink(xxx, path("net/i4b/include/machine"));
206 static char *ary[] = { "linux", "svr4" };
207 char xxx[MAXPATHLEN];
211 for (i = 0; i < sizeof(ary)/sizeof(ary[0]); ++i) {
213 snprintf(xxx, sizeof(xxx), "../../emulation/%s/%s",
214 ary[i], machinename);
216 snprintf(xxx, sizeof(xxx), "%s/emulation/%s/%s",
217 srcdir, ary[i], machinename);
219 snprintf(yyy, sizeof(yyy), "arch_%s", ary[i]);
220 symlink(xxx, path(yyy));
224 options(); /* make options .h files */
225 makefile(); /* build Makefile */
226 headers(); /* make a lot of .h files */
227 configfile(); /* put config file into kernel*/
228 printf("Kernel build directory is %s\n", p);
234 * determine the root of the kernel source tree
235 * and save that in srcdir.
240 if (realpath("../..", srcdir) == NULL)
241 errx(2, "Unable to find root of source tree");
247 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
253 * returns EOF on end of file
254 * NULL on end of line
255 * pointer to the word otherwise
261 static char line[80];
267 while ((ch = getc(fp)) != EOF)
268 if (ch != ' ' && ch != '\t')
271 return ((char *)EOF);
286 while ((ch = getc(fp)) != EOF) {
293 return ((char *)EOF);
294 (void) ungetc(ch, fp);
300 * like get_word but will accept something in double or single quotes
301 * (to allow embedded spaces).
307 static char line[256];
313 while ((ch = getc(fp)) != EOF)
314 if (ch != ' ' && ch != '\t')
317 return ((char *)EOF);
331 if (ch == '"' || ch == '\'') {
332 register int quote = ch;
334 while ((ch = getc(fp)) != EOF) {
339 printf("config: missing quote reading `%s'\n",
347 while ((ch = getc(fp)) != EOF) {
353 (void) ungetc(ch, fp);
357 return ((char *)EOF);
362 * prepend the path to a filename
370 cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
371 (void) strcpy(cp, destdir);
373 (void) strcat(cp, "/");
374 (void) strcat(cp, file);
386 fi = fopen(PREFIX, "r");
388 err(2, "%s", PREFIX);
389 fo = fopen(p=path("config.c.new"), "w");
392 fprintf(fo, "#include \"opt_config.h\"\n");
393 fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
394 fprintf(fo, "static const char config[] = \"\\\n");
395 fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
396 while (EOF != (i=getc(fi))) {
398 fprintf(fo, "\\n\\\n___");
399 } else if (i == '\"') {
401 } else if (i == '\\') {
407 fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
408 fprintf(fo, "\";\n");
409 fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
412 moveifchanged(path("config.c.new"), path("config.c"));
417 * compare two files; rename if changed.
420 moveifchanged(const char *from_name, const char *to_name)
425 struct stat from_sb, to_sb;
430 if ((from_fd = open(from_name, O_RDONLY)) < 0)
431 err(EX_OSERR, "moveifchanged open(%s)", from_name);
433 if ((to_fd = open(to_name, O_RDONLY)) < 0)
436 if (!changed && fstat(from_fd, &from_sb) < 0)
437 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
439 if (!changed && fstat(to_fd, &to_sb) < 0)
440 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
442 if (!changed && from_sb.st_size != to_sb.st_size)
445 tsize = (size_t)from_sb.st_size;
448 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
450 #define MAP_FAILED ((caddr_t) -1)
453 err(EX_OSERR, "mmap %s", from_name);
454 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
456 err(EX_OSERR, "mmap %s", to_name);
458 changed = memcmp(p, q, tsize);
463 if (rename(from_name, to_name) < 0)
464 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
466 if (unlink(from_name) < 0)
467 err(EX_OSERR, "unlink(%s)", from_name);