2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#) Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California. All rights reserved.
39 * @(#)main.c 8.3 (Berkeley) 3/19/94
40 * $FreeBSD: src/usr.bin/make/main.c,v 1.118 2005/02/13 13:33:56 harti Exp $
41 * $DragonFly: src/usr.bin/make/main.c,v 1.68 2005/04/07 00:44:18 okumoto Exp $
46 * The main file for this entire program. Exit routines etc
49 * Utility functions defined in this file:
51 * Takes a line of arguments, breaks them and
52 * treats them as if they were given when first
53 * invoked. Used by the parse module to implement
58 #include <sys/utsname.h>
60 #include <sys/param.h>
63 #include <sys/queue.h>
64 #include <sys/resource.h>
83 #include "pathnames.h"
90 #define WANT_ENV_MKLVL 1
91 #define MKLVL_MAXVAL 500
92 #define MKLVL_ENVVAR "__MKLVL__"
94 #define MAKEFLAGS ".MAKEFLAGS"
96 /* Targets to be made */
97 Lst create = Lst_Initializer(create);
99 time_t now; /* Time at start of make */
100 struct GNode *DEFAULT; /* .DEFAULT node */
101 Boolean allPrecious; /* .PRECIOUS given on line by itself */
103 static Boolean noBuiltins; /* -r flag */
105 /* ordered list of makefiles to read */
106 static Lst makefiles = Lst_Initializer(makefiles);
108 static Boolean expandVars; /* fully expand printed variables */
110 /* list of variables to print */
111 static Lst variables = Lst_Initializer(variables);
113 int maxJobs; /* -j argument */
114 static Boolean forceJobs; /* -j argument given */
115 Boolean compatMake; /* -B argument */
116 Boolean debug; /* -d flag */
117 Boolean noExecute; /* -n flag */
118 Boolean keepgoing; /* -k flag */
119 Boolean queryFlag; /* -q flag */
120 Boolean touchFlag; /* -t flag */
121 Boolean usePipes; /* !-P flag */
122 Boolean ignoreErrors; /* -i flag */
123 Boolean beSilent; /* -s flag */
124 Boolean beVerbose; /* -v flag */
125 Boolean oldVars; /* variable substitution style */
126 Boolean checkEnvFirst; /* -e flag */
128 /* (-E) vars to override from env */
129 Lst envFirstVars = Lst_Initializer(envFirstVars);
131 Boolean jobsRunning; /* TRUE if the jobs might be running */
133 static void MainParseArgs(int, char **);
134 char *chdir_verify_path(const char *, char *);
135 static int ReadMakefile(const char *);
136 static void usage(void);
138 static char *curdir; /* startup directory */
139 static char *objdir; /* where we chdir'ed to */
143 * Append a flag with an optional argument to MAKEFLAGS and MFLAGS
146 MFLAGS_append(const char *flag, char *arg)
150 Var_Append(MAKEFLAGS, flag, VAR_GLOBAL);
152 str = MAKEFLAGS_quote(arg);
153 Var_Append(MAKEFLAGS, str, VAR_GLOBAL);
157 Var_Append("MFLAGS", flag, VAR_GLOBAL);
159 str = MAKEFLAGS_quote(arg);
160 Var_Append("MFLAGS", str, VAR_GLOBAL);
167 * Parse a given argument vector. Called from main() and from
168 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
170 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
173 * Various global and local flags will be set depending on the flags
177 MainParseArgs(int argc, char **argv)
182 optind = 1; /* since we're called more than once */
183 #define OPTFLAGS "ABC:D:E:I:PSV:Xd:ef:ij:km:nqrstv"
184 while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
189 MFLAGS_append("-A", NULL);
192 if (chdir(optarg) == -1)
193 err(1, "chdir %s", optarg);
196 Var_Set(optarg, "1", VAR_GLOBAL);
197 MFLAGS_append("-D", optarg);
200 Parse_AddIncludeDir(optarg);
201 MFLAGS_append("-I", optarg);
204 Lst_AtEnd(&variables, estrdup(optarg));
205 MFLAGS_append("-V", optarg);
212 MFLAGS_append("-B", NULL);
213 unsetenv("MAKE_JOBS_FIFO");
217 MFLAGS_append("-P", NULL);
221 MFLAGS_append("-S", NULL);
224 char *modules = optarg;
226 for (; *modules; ++modules)
244 if (modules[1] == '1') {
245 debug |= DEBUG_GRAPH1;
248 else if (modules[1] == '2') {
249 debug |= DEBUG_GRAPH2;
272 warnx("illegal argument to d option "
276 MFLAGS_append("-d", optarg);
280 Lst_AtEnd(&envFirstVars, estrdup(optarg));
281 MFLAGS_append("-E", optarg);
284 checkEnvFirst = TRUE;
285 MFLAGS_append("-e", NULL);
288 Lst_AtEnd(&makefiles, estrdup(optarg));
292 MFLAGS_append("-i", NULL);
298 maxJobs = strtol(optarg, &endptr, 10);
299 if (maxJobs <= 0 || *endptr != '\0') {
300 warnx("illegal number, -j argument -- %s",
304 MFLAGS_append("-j", optarg);
309 MFLAGS_append("-k", NULL);
312 Path_AddDir(&sysIncPath, optarg);
313 MFLAGS_append("-m", optarg);
317 MFLAGS_append("-n", NULL);
321 /* Kind of nonsensical, wot? */
322 MFLAGS_append("-q", NULL);
326 MFLAGS_append("-r", NULL);
330 MFLAGS_append("-s", NULL);
334 MFLAGS_append("-t", NULL);
338 MFLAGS_append("-v", NULL);
349 * See if the rest of the arguments are variable assignments and
350 * perform them if so. Else take them to be targets and stuff them
351 * on the end of the "create" list.
353 for (argv += optind, argc -= optind; *argv; ++argv, --argc)
354 if (Parse_IsVar(*argv)) {
355 char *ptr = MAKEFLAGS_quote(*argv);
357 Var_Append(MAKEFLAGS, ptr, VAR_GLOBAL);
360 Parse_DoVar(*argv, VAR_CMD);
363 Punt("illegal (null) argument.");
371 Lst_AtEnd(&create, estrdup(*argv));
377 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
378 * is encountered and by main() when reading the .MAKEFLAGS envariable.
379 * Takes a line of arguments and breaks it into its
380 * component words and passes those words and the number of them to the
381 * MainParseArgs function.
382 * The line should have all its leading whitespace removed.
385 * Only those that come from the various arguments.
388 Main_ParseArgLine(char *line, int mflags)
390 char **argv; /* Manufactured argument vector */
391 int argc; /* Number of arguments in argv */
395 for (; *line == ' '; ++line)
401 argv = MAKEFLAGS_break(line, &argc);
403 argv = brk_string(line, &argc, TRUE);
405 MainParseArgs(argc, argv);
409 chdir_verify_path(const char *path, char *obpath)
413 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
414 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
415 warn("warning: %s", path);
425 catch_child(int sig __unused)
430 * In lieu of a good way to prevent every possible looping in
431 * make(1), stop there from being more than MKLVL_MAXVAL processes forked
432 * by make(1), to prevent a forkbomb from happening, in a dumb and
436 check_make_level(void)
438 #ifdef WANT_ENV_MKLVL
439 char *value = getenv(MKLVL_ENVVAR);
440 int level = (value == NULL) ? 0 : atoi(value);
443 errc(2, EAGAIN, "Invalid value for recursion level (%d).",
445 } else if (level > MKLVL_MAXVAL) {
446 errc(2, EAGAIN, "Max recursion level (%d) exceeded.",
450 sprintf(new_value, "%d", level + 1);
451 setenv(MKLVL_ENVVAR, new_value, 1);
453 #endif /* WANT_ENV_MKLVL */
458 * The main function, for obvious reasons. Initializes variables
459 * and a few modules, then parses the arguments give it in the
460 * environment and on the command line. Reads the system makefile
461 * followed by either Makefile, makefile or the file given by the
462 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
463 * flags it has received by then uses either the Make or the Compat
464 * module to create the initial list of targets.
467 * If -q was given, exits -1 if anything was out-of-date. Else it exits
471 * The program exits when done. Targets are created. etc. etc. etc.
474 main(int argc, char **argv)
476 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
480 char mdpath[MAXPATHLEN];
481 char obpath[MAXPATHLEN];
482 char cdpath[MAXPATHLEN];
483 const char *machine = getenv("MACHINE");
484 const char *machine_arch = getenv("MACHINE_ARCH");
485 const char *machine_cpu = getenv("MACHINE_CPU");
486 char *cp = NULL, *start;
488 /* avoid faults on read-only strings */
489 static char syspath[] = PATH_DEFSYSPATH;
493 * Catch SIGCHLD so that we get kicked out of select() when we
494 * need to look at a child. This is only known to matter for the
495 * -j case (perhaps without -P).
497 * XXX this is intentionally misplaced.
501 sigemptyset(&sa.sa_mask);
502 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
503 sa.sa_handler = catch_child;
504 sigaction(SIGCHLD, &sa, NULL);
511 * Turn off ENV to make ksh happier.
518 * get rid of resource limit on file descriptors
522 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
523 rl.rlim_cur != rl.rlim_max) {
524 rl.rlim_cur = rl.rlim_max;
525 setrlimit(RLIMIT_NOFILE, &rl);
530 * Get the name of this type of MACHINE from utsname
531 * so we can share an executable for similar machines.
532 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
534 * Note that while MACHINE is decided at run-time,
535 * MACHINE_ARCH is always known at compile time.
539 static struct utsname utsname;
541 if (uname(&utsname) == -1)
543 machine = utsname.machine;
551 machine_arch = "unknown";
553 machine_arch = MACHINE_ARCH;
558 * Set machine_cpu to the minumum supported CPU revision based
559 * on the target architecture, if not already set.
562 if (!strcmp(machine_arch, "i386"))
563 machine_cpu = "i386";
564 else if (!strcmp(machine_arch, "alpha"))
567 machine_cpu = "unknown";
571 beSilent = FALSE; /* Print commands as executed */
572 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
573 noExecute = FALSE; /* Execute all commands */
574 keepgoing = FALSE; /* Stop on error */
575 allPrecious = FALSE; /* Remove targets when interrupted */
576 queryFlag = FALSE; /* This is not just a check-run */
577 noBuiltins = FALSE; /* Read the built-in rules */
578 touchFlag = FALSE; /* Actually update targets */
579 usePipes = TRUE; /* Catch child output in pipes */
580 debug = 0; /* No debug verbosity, please. */
583 maxJobs = DEFMAXJOBS;
584 forceJobs = FALSE; /* No -j flag */
585 compatMake = FALSE; /* No compat mode */
588 * Initialize the parsing, directory and variable modules to prepare
589 * for the reading of inclusion paths and variable settings on the
592 Dir_Init(); /* Initialize directory structures so -I flags
593 * can be processed correctly */
594 Parse_Init(); /* Need to initialize the paths of #include
596 Var_Init(); /* As well as the lists of variables for
597 * parsing arguments */
601 * Initialize various variables.
602 * MAKE also gets this name, for compatibility
603 * .MAKEFLAGS gets set to the empty string just in case.
604 * MFLAGS also gets initialized empty, for compatibility.
606 Var_Set("MAKE", argv[0], VAR_GLOBAL);
607 Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
608 Var_Set("MFLAGS", "", VAR_GLOBAL);
609 Var_Set("MACHINE", machine, VAR_GLOBAL);
610 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
611 Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL);
613 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
617 * First snag any flags out of the MAKE environment variable.
618 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
619 * in a different format).
621 Main_ParseArgLine(getenv("MAKEFLAGS"), 1);
623 MainParseArgs(argc, argv);
626 * Find where we are...
627 * All this code is so that we know where we are when we start up
628 * on a different machine with pmake.
631 if (getcwd(curdir, MAXPATHLEN) == NULL)
637 if (stat(curdir, &sa) == -1)
638 err(2, "%s", curdir);
642 * The object directory location is determined using the
643 * following order of preference:
645 * 1. MAKEOBJDIRPREFIX`cwd`
647 * 3. PATH_OBJDIR.${MACHINE}
649 * 5. PATH_OBJDIRPREFIX`cwd`
651 * If one of the first two fails, use the current directory.
652 * If the remaining three all fail, use the current directory.
654 * Once things are initted,
655 * have to add the original directory to the search path,
656 * and modify the paths for the Makefiles apropriately. The
657 * current directory is also placed as a variable for make scripts.
659 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
660 if (!(path = getenv("MAKEOBJDIR"))) {
662 pathp = PATH_OBJDIRPREFIX;
663 snprintf(mdpath, MAXPATHLEN, "%s.%s",
665 if (!(objdir = chdir_verify_path(mdpath, obpath)))
666 if (!(objdir=chdir_verify_path(path, obpath))) {
667 snprintf(mdpath, MAXPATHLEN,
668 "%s%s", pathp, curdir);
669 if (!(objdir=chdir_verify_path(mdpath,
674 else if (!(objdir = chdir_verify_path(path, obpath)))
678 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
679 if (!(objdir = chdir_verify_path(mdpath, obpath)))
682 Dir_InitDot(); /* Initialize the "." directory */
683 if (objdir != curdir)
684 Path_AddDir(&dirSearchPath, curdir);
685 Var_Set(".DIRECTIVE_MAKEENV", "YES", VAR_GLOBAL);
686 Var_Set(".CURDIR", curdir, VAR_GLOBAL);
687 Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
689 if (getenv("MAKE_JOBS_FIFO") != NULL)
692 * Be compatible if user did not specify -j and did not explicitly
693 * turned compatibility on
695 if (!compatMake && !forceJobs)
699 * Initialize target and suffix modules in preparation for
700 * parsing the makefile(s)
709 * Set up the .TARGETS variable to contain the list of targets to be
710 * created. If none specified, make the variable empty -- the parser
711 * will fill the thing in with the default or .MAIN target.
713 if (!Lst_IsEmpty(&create)) {
716 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) {
717 char *name = Lst_Datum(ln);
719 Var_Append(".TARGETS", name, VAR_GLOBAL);
722 Var_Set(".TARGETS", "", VAR_GLOBAL);
726 * If no user-supplied system path was given (through the -m option)
727 * add the directories from the DEFSYSPATH (more than one may be given
728 * as dir1:...:dirn) to the system include path.
730 if (TAILQ_EMPTY(&sysIncPath)) {
731 for (start = syspath; *start != '\0'; start = cp) {
732 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
735 Path_AddDir(&sysIncPath, start);
738 Path_AddDir(&sysIncPath, start);
744 * Read in the built-in rules first, followed by the specified
745 * makefile, if it was (makefile != (char *) NULL), or the default
746 * Makefile and makefile, in that order, if it wasn't.
750 Lst sysMkPath = Lst_Initializer(sysMkPath);
753 Path_Expand(PATH_DEFSYSMK, &sysIncPath, &sysMkPath);
754 if (Lst_IsEmpty(&sysMkPath))
755 Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
756 LST_FOREACH(ln, &sysMkPath) {
757 if (!ReadMakefile(Lst_Datum(ln)))
761 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
762 Lst_Destroy(&sysMkPath, free);
765 if (!Lst_IsEmpty(&makefiles)) {
768 LST_FOREACH(ln, &makefiles) {
769 if (!ReadMakefile(Lst_Datum(ln)))
773 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
774 } else if (!ReadMakefile("BSDmakefile"))
775 if (!ReadMakefile("makefile"))
776 ReadMakefile("Makefile");
778 ReadMakefile(".depend");
780 /* Install all the flags into the MAKE envariable. */
781 if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
782 setenv("MAKEFLAGS", p, 1);
786 * For compatibility, look at the directories in the VPATH variable
787 * and add them to the search path, if the variable is defined. The
788 * variable's value is in the same format as the PATH envariable, i.e.
789 * <directory>:<directory>:<directory>...
791 if (Var_Exists("VPATH", VAR_CMD)) {
793 * GCC stores string constants in read-only memory, but
794 * Var_Subst will want to write this thing, so store it
797 static char VPATH[] = "${VPATH}";
803 buf = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
805 vpath = Buf_Data(buf);
807 /* skip to end of directory */
808 for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
811 /* Save terminator character so know when to stop */
815 /* Add directory to search path */
816 Path_AddDir(&dirSearchPath, vpath);
819 } while (savec != '\0');
821 Buf_Destroy(buf, TRUE);
825 * Now that all search paths have been read for suffixes et al, it's
826 * time to add the default search path to their lists...
830 /* print the initial graph, if the user requested it */
834 /* print the values of any variables requested by the user */
835 if (Lst_IsEmpty(&variables)) {
837 * Since the user has not requested that any variables
838 * be printed, we can build targets.
840 * Have read the entire graph and need to make a list of targets
841 * to create. If none was given on the command line, we consult
842 * the parsing module to find the main target(s) to create.
844 Lst targs = Lst_Initializer(targs);
846 if (Lst_IsEmpty(&create))
847 Parse_MainName(&targs);
849 Targ_FindList(&targs, &create, TARG_CREATE);
853 * Compat_Init will take care of creating
854 * all the targets as well as initializing
861 * Initialize job module before traversing
862 * the graph, now that any .BEGIN and .END
863 * targets have been read. This is done
864 * only if the -q flag wasn't given (to
865 * prevent the .BEGIN from being executed
873 /* Traverse the graph, checking on all the targets */
874 outOfDate = Make_Run(&targs);
876 Lst_Destroy(&targs, NOFREE);
880 * Print the values of any variables requested by
885 LST_FOREACH(n, &variables) {
886 const char *name = Lst_Datum(n);
891 v = emalloc(strlen(name) + 1 + 3);
892 sprintf(v, "${%s}", name);
894 value = Buf_Peel(Var_Subst(NULL, v,
896 printf("%s\n", value);
903 value = Var_Value(name, VAR_GLOBAL, &v);
904 printf("%s\n", value != NULL ? value : "");
911 Lst_Destroy(&variables, free);
912 Lst_Destroy(&makefiles, free);
913 Lst_Destroy(&create, free);
915 /* print the graph now it's been processed if the user requested it */
919 if (queryFlag && outOfDate)
927 * Open and parse the given makefile.
930 * TRUE if ok. FALSE if couldn't open file.
936 ReadMakefile(const char *p)
938 char *fname; /* makefile to read */
940 char *name, path[MAXPATHLEN];
944 /* XXX - remove this once constification is done */
947 if (!strcmp(fname, "-")) {
948 Parse_File("(stdin)", stdin);
949 Var_Set("MAKEFILE", "", VAR_GLOBAL);
951 setMAKEFILE = strcmp(fname, ".depend");
953 /* if we've chdir'd, rebuild the path name */
954 if (curdir != objdir && *fname != '/') {
955 snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
957 * XXX The realpath stuff breaks relative includes
958 * XXX in some cases. The problem likely is in
959 * XXX parse.c where it does special things in
960 * XXX ParseDoInclude if the file is relateive
961 * XXX or absolute and not a system file. There
962 * XXX it assumes that if the current file that's
963 * XXX being included is absolute, that any files
964 * XXX that it includes shouldn't do the -I path
965 * XXX stuff, which is inconsistant with historical
966 * XXX behavior. However, I can't pentrate the mists
967 * XXX further, so I'm putting this workaround in
968 * XXX here until such time as the underlying bug
971 #if THIS_BREAKS_THINGS
972 if (realpath(path, path) != NULL &&
973 (stream = fopen(path, "r")) != NULL) {
978 } else if (realpath(fname, path) != NULL) {
981 if ((stream = fopen(fname, "r")) != NULL)
985 if ((stream = fopen(path, "r")) != NULL) {
992 if ((stream = fopen(fname, "r")) != NULL)
996 /* look in -I and system include directories. */
997 name = Path_FindFile(fname, &parseIncPath);
999 name = Path_FindFile(fname, &sysIncPath);
1000 if (!name || !(stream = fopen(name, "r")))
1002 MAKEFILE = fname = name;
1004 * set the MAKEFILE variable desired by System V fans -- the
1005 * placement of the setting here means it gets set to the last
1006 * makefile specified, as it is set by SysV make.
1010 Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL);
1011 Parse_File(fname, stream);
1019 * Execute the command in cmd, and return the output of that command
1023 * A string containing the output of the command, or the empty string
1024 * If error is not NULL, it contains the reason for the command failure
1027 * The string must be freed by the caller.
1030 Cmd_Exec(const char *cmd, const char **error)
1032 int fds[2]; /* Pipe streams */
1033 int cpid; /* Child PID */
1034 int pid; /* PID from wait() */
1035 int status; /* command exit status */
1036 Buffer *buf; /* buffer to store the result */
1042 if (shellPath == NULL)
1045 * Open a pipe for fetching its output
1047 if (pipe(fds) == -1) {
1048 *error = "Couldn't create pipe for \"%s\"";
1055 switch (cpid = vfork()) {
1058 * Close input side of pipe
1063 * Duplicate the output stream to the shell's output, then
1064 * shut the extra thing down. Note we don't fetch the error
1065 * stream...why not? Why?
1073 /* Set up arguments for shell */
1074 args[0] = shellName;
1079 execv(shellPath, args);
1085 *error = "Couldn't exec \"%s\"";
1090 * No need for the writing half
1095 char result[BUFSIZ];
1097 rcnt = read(fds[0], result, sizeof(result));
1099 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result);
1100 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR));
1103 *error = "Error reading shell's output for \"%s\"";
1106 * Close the input side of the pipe.
1111 * Wait for the process to exit.
1113 while (((pid = wait(&status)) != cpid) && (pid >= 0))
1117 *error = "\"%s\" returned non-zero status";
1119 Buf_StripNewlines(buf);
1128 * exit with usage message
1133 fprintf(stderr, "%s\n%s\n%s\n",
1134 "usage: make [-BPSXeiknqrstv] [-C directory] [-D variable] [-d flags]",
1135 " [-E variable] [-f makefile] [-I directory] [-j max_jobs]",
1136 " [-m directory] [-V variable] [variable=value] [target ...]");