home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / MAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  36.7 KB  |  1,428 lines

  1. /* Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/make/RCS/main.c'v 3.58.0.7 90/08/27 01:25:08 tho Exp $
  33.  */
  34.  
  35. #include "make.h"
  36. #include "commands.h"
  37. #include "dep.h"
  38. #include "file.h"
  39. #include "variable.h"
  40. #include "job.h"
  41. #include <ctype.h>
  42. #include <time.h>
  43.  
  44. #ifdef MSDOS
  45. #include <process.h>
  46. #include <swaplib.h>
  47. extern char *msdos_version_string;
  48. #endif /* MSDOS */
  49.  
  50. extern char *version_string;
  51.  
  52. extern int fatal_error_signal ();
  53.  
  54. extern struct dep *read_all_makefiles ();
  55.  
  56. extern void print_variable_data_base ();
  57. extern void print_dir_data_base ();
  58. extern void print_rule_data_base ();
  59. extern void print_file_data_base ();
  60. extern void print_vpath_data_base ();
  61.  
  62. #ifdef MSDOS
  63. #include <direct.h>
  64. #include <io.h>
  65. #else /* not MSDOS */
  66. extern int chdir ();
  67. extern void exit ();
  68. extern long int atol ();
  69. extern time_t time ();
  70. extern int mktemp ();
  71. extern double atof ();
  72. #endif /* not MSDOS */
  73.  
  74. #ifdef MSDOS
  75. extern  int main (int argc, char **argv, char **envp);
  76. static  void decode_switches (int argc, char **argv);
  77. static  void decode_env_switches (char *envar, unsigned int len);
  78. static  void define_makeflags (int pf);
  79. static  void print_version (void);
  80. static  void print_data_base (void);
  81. static  void log_working_directory (int entering);
  82. #else /* not MSDOS */
  83. static void log_working_directory ();
  84. static void print_data_base (), print_version ();
  85. static void decode_switches (), decode_env_switches ();
  86. static void define_makeflags ();
  87. #endif /* not MSDOS */
  88.  
  89. #if 0 /* dummy tag */
  90. flags () {}
  91. #endif
  92. /* Flags:
  93.  *    -b ignored for compatability with System V Make
  94.  *    -C change directory
  95.  *    -d debug
  96.  *    -e env_overrides
  97.  *    -f makefile
  98.  *    -i ignore_errors
  99.  *    -j job_slots
  100.  *    -k keep_going
  101.  *    -l max_load_average
  102.  *    -m ignored for compatibility with something or other
  103.  *    -n just_print
  104.  *    -o consider file old
  105.  *    -p print_data_base
  106.  *    -q question
  107.  *    -r no_builtin_rules
  108.  *    -s silent
  109.  *    -S turn off -k
  110.  *    -t touch
  111.  *    -v print version information
  112.  *    -w log working directory
  113.  *    -W consider file new (with -n, `what' if effect)
  114.  */
  115.  
  116. /* The structure that describes an accepted command switch.  */
  117.  
  118. struct command_switch
  119.   {
  120.     char c;        /* The switch character.  */
  121.     enum        /* Type of the value.  */
  122.       {
  123.     flag,        /* Turn int flag on.  */
  124.     flag_off,    /* Turn int flag off.  */
  125.     string,        /* One string per switch, may be in the same word.  */
  126.     positive_int,    /* A positive integer.  */
  127.     floating,    /* A floating-point number (double).  */
  128.     ignore,        /* Ignored.  */
  129.       } type;
  130.  
  131.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  132.     char env;        /* Can come from MAKEFLAGS.  */
  133.     char toenv;        /* Should be put in MAKEFLAGS.  */
  134.  
  135.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  136.     char *default_value;/* Pointer to default value.  */
  137.   };
  138.  
  139.  
  140. /* The structure used to hold the list of strings given
  141.    in command switches of a type that takes string arguments.  */
  142.  
  143. struct stringlist
  144.   {
  145.     char **list;    /* Nil-terminated list of strings.  */
  146.     unsigned int idx;    /* Index into above.  */
  147.     unsigned int max;    /* Number of pointers allocated.  */
  148.   };
  149.  
  150.  
  151. /* The recognized command switches.  */
  152.  
  153. /* Nonzero means do not print commands to be executed (-s).  */
  154.  
  155. int silent_flag;
  156.  
  157. /* Nonzero means just touch the files
  158.    that would appear to need remaking (-t)  */
  159.  
  160. int touch_flag;
  161.  
  162. /* Nonzero means just print what commands would need to be executed,
  163.    don't actually execute them (-n).  */
  164.  
  165. int just_print_flag;
  166.  
  167. /* Print debugging trace info (-d).  */
  168.  
  169. int debug_flag = 0;
  170.  
  171. /* Environment variables override makefile definitions.  */
  172.  
  173. int env_overrides = 0;
  174.  
  175. /* Nonzero means ignore status codes returned by commands
  176.    executed to remake files.  Just treat them all as successful (-i).  */
  177.  
  178. int ignore_errors_flag = 0;
  179.  
  180. /* Nonzero means don't remake anything, just print the data base
  181.    that results from reading the makefile (-p).  */
  182.  
  183. int print_data_base_flag = 0;
  184.  
  185. /* Nonzero means don't remake anything; just return a nonzero status
  186.    if the specified targets are not up to date (-q).  */
  187.  
  188. int question_flag = 0;
  189.  
  190. /* Nonzero means do not use any of the builtin rules (-r).  */
  191.  
  192. int no_builtin_rules_flag = 0;
  193.  
  194. /* Nonzero means keep going even if remaking some file fails (-k).  */
  195.  
  196. int keep_going_flag = 0;
  197.  
  198. /* Nonzero means print working directory
  199.    before starting and after finishing make. */
  200.  
  201. int print_directory_flag = 0;
  202.  
  203. /* Nonzero means print version information.  */
  204.  
  205. int print_version_flag = 0;
  206.  
  207. /* List of makefiles given with -f switches.  */
  208.  
  209. static struct stringlist *makefiles = 0;
  210.  
  211.  
  212. /* Number of job slots (commands that can be run at once).  */
  213.  
  214. unsigned int job_slots = 1;
  215. unsigned int default_job_slots = 1;
  216.  
  217. /* Value of job_slots that means no limit.  */
  218.  
  219. static unsigned int inf_jobs = 0;
  220.  
  221. /* Maximum load average at which multiple jobs will be run.
  222.    Negative values mean unlimited, while zero means limit to
  223.    zero load (which could be useful to start infinite jobs remotely
  224.    but one at a time locally).  */
  225.  
  226. double max_load_average = -1.0;
  227. double default_load_average = -1.0;
  228.  
  229. /* List of directories given with -c switches.  */
  230.  
  231. static struct stringlist *directories = 0;
  232.  
  233. /* List of include directories given with -I switches.  */
  234.  
  235. static struct stringlist *include_directories = 0;
  236.  
  237. /* List of files given with -o switches.  */
  238.  
  239. static struct stringlist *old_files = 0;
  240.  
  241. /* List of files given with -W switches.  */
  242.  
  243. static struct stringlist *new_files = 0;
  244.  
  245. /* The table of command switches.  */
  246.  
  247. static struct command_switch switches[] =
  248.   {
  249.     { 'b', ignore, 0, 0, 0, 0, 0 },
  250.     { 'C', string, (char *) &directories, 0, 0, 0, 0 },
  251.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0 },
  252.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0 },
  253.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0 },
  254.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0 },
  255.     { 'I', string, (char *) &include_directories, 0, 0, 0, 0 },
  256.     { 'j', positive_int, (char *) &job_slots, 1, 1,
  257.     (char *) &inf_jobs, (char *) &default_job_slots },
  258.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0, 0 },
  259.     { 'l', floating, (char *) &max_load_average, 1, 1,
  260.     (char *) &default_load_average, (char *) &default_load_average },
  261.     { 'm', ignore, 0, 0, 0, 0, 0 },
  262.     { 'n', flag, (char *) &just_print_flag, 1, 1, 0, 0 },
  263.     { 'o', string, (char *) &old_files, 0, 0, 0, 0 },
  264.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0 },
  265.     { 'q', flag, (char *) &question_flag, 1, 1, 0, 0 },
  266.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0 },
  267.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0 },
  268.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0, 0 },
  269.     { 't', flag, (char *) &touch_flag, 1, 1, 0, 0 },
  270.     { 'v', flag, (char *) &print_version_flag, 0, 0, 0, 0 },
  271.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0 },
  272.     { 'W', string, (char *) &new_files, 0, 0, 0, 0 },
  273.     { '\0', ignore, 0, 0, 0, 0 }
  274.   };
  275.  
  276. /* List of non-switch arguments.  */
  277.  
  278. struct stringlist *other_args = 0;
  279.  
  280.  
  281. /* The name we were invoked with.  */
  282.  
  283. char *program;
  284.  
  285. /* Value of the MAKELEVEL variable at startup (or 0).  */
  286.  
  287. unsigned int makelevel;
  288.  
  289. /* First file defined in the makefile whose name does not
  290.    start with `.'.  This is the default to remake if the
  291.    command line does not specify.  */
  292.  
  293. struct file *default_goal_file;
  294.  
  295. /* Pointer to structure for the file .DEFAULT
  296.    whose commands are used for any file that has none of its own.
  297.    This is zero if the makefiles do not define .DEFAULT.  */
  298.  
  299. struct file *default_file;
  300.  
  301. #ifdef MSDOS
  302. /* Under MS-DOS the current directory is not local to the process,
  303.    so we have to find means to back up somehow.  */
  304.  
  305. char current_directory[MAXPATHLEN];
  306. #endif /* MSDOS */
  307.  
  308.  
  309. int
  310. main (argc, argv, envp)
  311.      int argc;
  312.      char **argv;
  313.      char **envp;
  314. {
  315. #if    defined(USG) && !defined(HAVE_SIGLIST)
  316.   extern void init_siglist ();
  317. #endif
  318.   extern int child_handler ();
  319.   register struct file *f;
  320.   register unsigned int i;
  321.   int status;
  322.   register char *cmd_defs;
  323.   register unsigned int cmd_defs_len, cmd_defs_idx;
  324.   char **p;
  325.   time_t now;
  326.   struct dep *goals = 0;
  327.   register struct dep *lastgoal;
  328.   struct dep *read_makefiles;
  329. #ifndef MSDOS            /* see above */
  330.   char current_directory[MAXPATHLEN];
  331. #endif /* not MSDOS */
  332.  
  333.  
  334.   default_goal_file = 0;
  335.   reading_filename = 0;
  336.   reading_lineno_ptr = 0;
  337.   
  338. #if    defined(USG) && !defined(HAVE_SIGLIST)
  339.   init_siglist ();
  340. #endif
  341.  
  342. #ifndef MSDOS
  343.   if (signal (SIGHUP, fatal_error_signal) == SIG_IGN)
  344.     (void) signal (SIGHUP, SIG_IGN);
  345.   if (signal (SIGQUIT, fatal_error_signal) == SIG_IGN)
  346.     (void) signal (SIGQUIT, SIG_IGN);
  347. #endif /* ! MSDOS */
  348.   if (signal (SIGINT, fatal_error_signal) == SIG_IGN)
  349.     (void) signal (SIGINT, SIG_IGN);
  350. #ifndef MSDOS
  351.   if (signal (SIGILL, fatal_error_signal) == SIG_IGN)
  352.     (void) signal (SIGILL, SIG_IGN);
  353.   if (signal (SIGTRAP, fatal_error_signal) == SIG_IGN)
  354.     (void) signal (SIGTRAP, SIG_IGN);
  355.   if (signal (SIGIOT, fatal_error_signal) == SIG_IGN)
  356.     (void) signal (SIGIOT, SIG_IGN);
  357. #endif /* ! MSDOS */
  358. #ifdef    SIGEMT
  359.   if (signal (SIGEMT, fatal_error_signal) == SIG_IGN)
  360.     (void) signal (SIGEMT, SIG_IGN);
  361. #endif
  362. #ifdef    SIGDANGER
  363.   if (signal (SIGDANGER, fatal_error_signal) == SIG_IGN)
  364.     (void) signal (SIGDANGER, SIG_IGN);
  365. #endif
  366.   if (signal (SIGFPE, fatal_error_signal) == SIG_IGN)
  367.     (void) signal (SIGFPE, SIG_IGN);
  368. #ifndef MSDOS
  369.   if (signal (SIGBUS, fatal_error_signal) == SIG_IGN)
  370.     (void) signal (SIGBUS, SIG_IGN);
  371.   if (signal (SIGSEGV, fatal_error_signal) == SIG_IGN)
  372.     (void) signal (SIGSEGV, SIG_IGN);
  373.   if (signal (SIGSYS, fatal_error_signal) == SIG_IGN)
  374.     (void) signal (SIGSYS, SIG_IGN);
  375.   if (signal (SIGTERM, fatal_error_signal) == SIG_IGN)
  376.     (void) signal (SIGTERM, SIG_IGN);
  377. #endif /* ! MSDOS */
  378. #ifdef SIGXCPU
  379.   if (signal (SIGXCPU, fatal_error_signal) == SIG_IGN)
  380.     (void) signal (SIGXCPU, SIG_IGN);
  381. #endif
  382. #ifdef SIGXFSZ
  383.   if (signal (SIGXFSZ, fatal_error_signal) == SIG_IGN)
  384.     (void) signal (SIGXFSZ, SIG_IGN);
  385. #endif
  386.  
  387.   /* Make sure stdout is line-buffered.  */
  388.  
  389. #if    defined(USGr3) || defined(HPUX) || defined(hpux) || defined (MSDOS)
  390.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  391. #else    /* Not USGr3 and not HPUX.  */
  392. #ifdef    USG
  393.   setvbuf (stdout, _IOLBF, (char *) 0, BUFSIZ);
  394. #else    /* Not USG.  */
  395.   setlinebuf (stdout);
  396. #endif    /* USG.  */
  397. #endif    /* USGr3.  */
  398.  
  399.   /* Figure out where this program lives.  */
  400.  
  401.   if (argv[0] == 0)
  402.     argv[0] = "";
  403.   if (argv[0][0] == '\0')
  404.     program = "make";
  405.   else 
  406.     {
  407.       program = rindex (argv[0], '/');
  408.       if (program == 0)
  409.     program = argv[0];
  410.       else
  411.     ++program;
  412.     }
  413.  
  414.   /* Figure out where we are.  */
  415.  
  416. #ifdef    USG
  417.   /* In some System V's, `getcwd' spawns a child running /bin/pwd.  */
  418.   block_children ();
  419. #endif
  420.   if (getwd (current_directory) == 0)
  421.     {
  422.       error ("getwd: %s", current_directory);
  423.       current_directory[0] = '\0';
  424.     }
  425. #ifdef    USG
  426.   unblock_children ();
  427. #endif
  428.  
  429.   /* Read in variables from the environment.  It is important that this be
  430.      done before `MAKE' and `MAKEOVERRIDES' are figured out so their
  431.      definitions will not be ones from the environment.  */
  432.  
  433.   for (i = 0; envp[i] != 0; ++i)
  434.     {
  435.       register char *ep = envp[i];
  436.       while (*ep++ != '=')
  437.     ;
  438.       (void) define_variable (envp[i], ep - envp[i] - 1, ep, o_env, 1);
  439.     }
  440.  
  441.   /* Decode the switches.  */
  442.  
  443.   decode_switches (argc, argv);
  444.  
  445.   /* Print version information.  */
  446.  
  447.   if (print_version_flag || print_data_base_flag || debug_flag)
  448.     print_version ();
  449.  
  450.   /* Construct the list of include directories to search.  */
  451.  
  452.   construct_include_path (include_directories == 0 ? (char **) 0
  453.               : include_directories->list);
  454.  
  455.   /* Search for command line arguments that define variables,
  456.      and do the definitions.  Also save up the text of these
  457.      arguments in CMD_DEFS so we can put them into the values
  458.      of $(MAKEOVERRIDES) and $(MAKE).  */
  459.  
  460.   cmd_defs_len = 200;
  461.   cmd_defs = (char *) xmalloc (cmd_defs_len);
  462.   cmd_defs_idx = 0;
  463.  
  464.   for (i = 1; other_args->list[i] != 0; ++i)
  465.     /* Try a variable definition.  */
  466.     if (try_variable_definition (other_args->list[i], o_command))
  467.       {
  468.     /* It succeeded.  The variable is already defined.
  469.        Backslash-quotify it and append it to CMD_DEFS, then clobber it
  470.        to 0 in the list so that it won't be taken for a goal target.  */
  471.     register char *p = other_args->list[i];
  472.     unsigned int l = strlen (p);
  473.     if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
  474.       {
  475.         if (l * 2 > cmd_defs_len)
  476.           cmd_defs_len += l * 2;
  477.         else
  478.           cmd_defs_len *= 2;
  479.         cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
  480.       }
  481.  
  482.     while (*p != '\0')
  483.       {
  484.         if (index ("'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
  485.           cmd_defs[cmd_defs_idx++] = '\\';
  486.         cmd_defs[cmd_defs_idx++] = *p++;
  487.       }
  488.     cmd_defs[cmd_defs_idx++] = ' ';
  489.       }
  490.   else
  491.     {
  492.       /* It was not a variable definition, so it is a target to be made.
  493.      Enter it as a file and add it to the dep chain of goals.  */
  494.       f = enter_file (other_args->list[i]);
  495.       f->cmd_target = 1;
  496.  
  497.       if (goals == 0)
  498.     {
  499.       goals = (struct dep *) xmalloc (sizeof (struct dep));
  500.       lastgoal = goals;
  501.     }
  502.       else
  503.     {
  504.       lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
  505.       lastgoal = lastgoal->next;
  506.     }
  507.       lastgoal->name = 0;
  508.       lastgoal->file = f;
  509.     }
  510.  
  511.   if (cmd_defs_idx > 0)
  512.     {
  513.       cmd_defs[cmd_defs_idx - 1] = '\0';
  514.       (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_override, 0);
  515.     }
  516.  
  517.   /* Set the "MAKE" variable to the name we were invoked with.
  518.      (If it is a relative pathname with a slash, prepend our directory name
  519.      so the result will run the same program regardless of the current dir.
  520.      If it is a name with no slash, we can only hope that PATH did not
  521.      find it in the current directory.)
  522.  
  523.      Append the command-line variable definitions gathered above
  524.      so sub-makes will get them as command-line definitions.  */
  525.  
  526. #ifdef MSDOS
  527.   if (current_directory[0] != '\0' && argv[0] != 0
  528.   && argv[0][1] != ':' && argv[0][0] != '/' && index (argv[0], '/') != 0)
  529. #else /* not MSDOS */
  530.   if (current_directory[0] != '\0'
  531.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  532. #endif /* not MSDOS */
  533.     argv[0] = concat (current_directory, "/", argv[0]);
  534.  
  535.   if (cmd_defs_idx > 0)
  536.     {
  537.       char *str = concat (argv[0], " ", cmd_defs);
  538.       (void) define_variable ("MAKE", 4, str, o_env, 0);
  539.       free (str);
  540.     }
  541.   else
  542.     (void) define_variable ("MAKE", 4, argv[0], o_env, 0);
  543.  
  544.   free (cmd_defs);
  545.  
  546.   /* If there were -c flags, move ourselves about.  */
  547.  
  548.   if (directories != 0)
  549.     for (i = 0; directories->list[i] != 0; ++i)
  550.       {
  551.     char *dir = directories->list[i];
  552.     if (chdir (dir) < 0)
  553.       pfatal_with_name (dir);
  554.       }
  555.  
  556.   /* Figure out the level of recursion.  */
  557.   {
  558.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  559.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  560.       makelevel = (unsigned int) atoi (v->value);
  561.     else
  562.       makelevel = 0;
  563.   }
  564.  
  565.   /* Tell the user where he is.  */
  566.  
  567.   if (print_directory_flag)
  568.     log_working_directory (1);
  569.  
  570.   /* Read any stdin makefiles into temporary files.  */
  571.  
  572.   if (makefiles != 0)
  573.     {
  574.       register unsigned int i;
  575.       for (i = 0; i < makefiles->idx; ++i)
  576.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  577.       {
  578.         /* This makefile is standard input.  Since we may re-exec
  579.            and thus re-read the makefiles, we read standard input
  580.            into a temporary file and read from that.  */
  581. #ifdef MSDOS        /* Can't use $TMP, sice it may contain a `:'  */
  582.         static char name[] = "./GmXXXXXX";
  583. #else /* not MSDOS */
  584.         static char name[] = "/tmp/GmXXXXXX";
  585. #endif /* not MSDOS */
  586.         FILE *outfile;
  587.  
  588.         /* Free the storage allocated for "-".  */
  589.         free (makefiles->list[i]);
  590.  
  591.         /* Make a unique filename.  */
  592.         (void) mktemp (name);
  593.  
  594.         outfile = fopen (name, "w");
  595.         if (outfile == 0)
  596.           pfatal_with_name ("fopen (temporary file)");
  597.         while (!feof (stdin))
  598.           {
  599.         char buf[2048];
  600.         int n = fread (buf, 1, sizeof(buf), stdin);
  601.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  602.           pfatal_with_name ("fwrite (temporary file)");
  603.           }
  604.         /* Try to make sure we won't remake the temporary
  605.            file when we are re-exec'd.  Kludge-o-matic!  */
  606.         fprintf (outfile, "%s:;\n", name);
  607.         (void) fclose (outfile);
  608.  
  609.         /* Replace the name that read_all_makefiles with
  610.            see with the name of the temporary file.  */
  611.         makefiles->list[i] = savestring (name, sizeof name - 1);
  612.  
  613.         /* Make sure the temporary file will not be remade.  */
  614.         f = enter_file (savestring (name, sizeof name - 1));
  615.         f->updated = 1;
  616.         f->update_status = 0;
  617.         /* Let it be removed when we're done.  */
  618.         f->intermediate = 1;
  619.         /* But don't mention it.  */
  620.         f->dontcare = 1;
  621.       }
  622.     }
  623.  
  624.   /* Set up to handle children dying.  This must be done before
  625.      reading in the makefiles so that `shell' function calls will work.  */
  626.  
  627. #ifndef MSDOS
  628. #ifdef SIGCHLD
  629.   (void) signal (SIGCHLD, child_handler);
  630. #else
  631.   (void) signal (SIGCLD, child_handler);
  632. #endif
  633. #endif /* ! MSDOS */
  634.  
  635.   /* Install the default implicit rules.  */
  636.  
  637.   install_default_implicit_rules ();
  638.  
  639.   /* Define the initial list of suffixes for old-style rules.  */
  640.  
  641.   set_default_suffixes ();
  642.  
  643.   /* Define some internal and special variables.  */
  644.  
  645.   define_automatic_variables ();
  646.  
  647.   /* Set up the MAKEFLAGS and MFLAGS variables
  648.      so makefiles can look at them.  */
  649.  
  650.   define_makeflags (0);
  651.  
  652.   /* Read all the makefiles.  */
  653.  
  654.   default_file = enter_file (".DEFAULT");
  655.  
  656.   read_makefiles
  657.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  658.  
  659.   decode_env_switches ("MAKEFLAGS", 9);
  660.   decode_env_switches ("MFLAGS", 6);
  661.  
  662.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  663.  
  664.   define_makeflags (1);
  665.  
  666.   ignore_errors_flag |= lookup_file (".IGNORE") != 0;
  667.  
  668.   silent_flag |= lookup_file (".SILENT") != 0;
  669.  
  670.   /* Make each `struct dep' point at the
  671.      `struct file' for the file depended on.  */
  672.  
  673.   snap_deps ();
  674.  
  675.   /* Convert old-style suffix rules to pattern rules.  */
  676.  
  677.   convert_to_pattern ();
  678.  
  679.   /* Recompute the implicit rule limits.  */
  680.  
  681.   count_implicit_rule_limits ();
  682.  
  683.   /* Construct the listings of directories in VPATH lists.  */
  684.  
  685.   build_vpath_lists ();
  686.  
  687.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  688.      and as having been updated already, and files given with -W flags
  689.      as brand new (time-stamp of now).  */
  690.  
  691.   if (old_files != 0)
  692.     for (p = old_files->list; *p != 0; ++p)
  693.       {
  694.     f = enter_file (*p);
  695.     f->last_mtime = (time_t) 1;
  696.     f->updated = 1;
  697.     f->update_status = 0;
  698.       }
  699.  
  700.   if (new_files != 0)
  701.     {
  702.       now = time ((time_t *) 0);
  703.       for (p = new_files->list; *p != 0; ++p)
  704.     {
  705.       f = enter_file (*p);
  706.       f->last_mtime = now;
  707.     }
  708.     }
  709.  
  710.   if (read_makefiles != 0)
  711.     {
  712.       /* Update any makefiles if necessary.  */
  713.  
  714.       time_t *makefile_mtimes = 0;
  715.       unsigned int mm_idx = 0;
  716.  
  717.       if (debug_flag)
  718.     puts ("Updating makefiles....");
  719.  
  720.       /* Remove any makefiles we don't want to try to update.
  721.      Also record the current modtimes so we can compare them later.  */
  722.       {
  723.     register struct dep *d, *last;
  724.     last = 0;
  725.     d = read_makefiles;
  726.     while (d != 0)
  727.       {
  728.         register struct file *f = d->file;
  729.         if (f->is_target && f->deps == 0)
  730.           {
  731.         /* This makefile is a target, but has no dependencies.
  732.            So, it will always be remade.  This might well cause
  733.            an infinite loop, so don't try to remake it.
  734.            (This will only happen if your makefiles are written
  735.            exceptionally stupidly; but if you work for Athena,
  736.            that is how you write makefiles.)  */
  737.  
  738.         if (debug_flag)
  739.           printf ("Makefile `%s' might loop; not remaking it.",
  740.               f->name);
  741.  
  742.         if (last == 0)
  743.           read_makefiles = d->next;
  744.         else
  745.           last->next = d->next;
  746.  
  747.         /* Free the storage.  */
  748.         free ((char *) d);
  749.  
  750.         d = last == 0 ? 0 : last->next;
  751.           }
  752.         else
  753.           {
  754.         if (makefile_mtimes == 0)
  755.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  756.         else
  757.           makefile_mtimes = (time_t *)
  758.             xrealloc ((char *) makefile_mtimes,
  759.                   (mm_idx + 1) * sizeof (time_t));
  760.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  761.         last = d;
  762.         d = d->next;
  763.           }
  764.       }
  765.       }    
  766.  
  767.       switch (update_goal_chain (read_makefiles, 1))
  768.     {
  769.     default:
  770.       abort ();
  771.       
  772.     case -1:
  773.       /* Did nothing.  */
  774.       break;
  775.       
  776.     case 1:
  777.       /* Failed to update.  Figure out if we care.  */
  778.       {
  779.         /* Nonzero if any makefile was successfully remade.  */
  780.         int any_remade = 0;
  781.         /* Nonzero if any makefile we care about failed
  782.            in updating or could not be found at all.  */
  783.         int any_failed = 0;
  784.         register unsigned int i;
  785.  
  786.         for (i = 0; read_makefiles != 0; ++i)
  787.           {
  788.         struct dep *d = read_makefiles;
  789.         read_makefiles = d->next;
  790.         if (d->file->updated)
  791.           {
  792.             /* This makefile was updated.  */
  793.             if (d->file->update_status == 0)
  794.               {
  795.             /* It was successfully updated.  */
  796.             any_remade |= (file_mtime_no_search (d->file)
  797.                        != makefile_mtimes[i]);
  798.               }
  799.             else if (d->changed != 1)
  800.               {
  801.             time_t mtime;
  802.             /* The update failed and this makefile was not
  803.                from the MAKEFILES variable, so we care.  */
  804.             error ("Failed to remake makefile `%s'.",
  805.                    d->file->name);
  806.             mtime = file_mtime_no_search (d->file);
  807.             any_remade |= (mtime != (time_t) -1
  808.                        && mtime != makefile_mtimes[i]);
  809.               }
  810.           }
  811.         else
  812.           /* This makefile was not found at all.  */
  813.           switch (d->changed)
  814.             {
  815.             case 0:
  816.               /* A normal makefile.  We must die later.  */
  817.               error ("Makefile `%s' was not found", dep_name (d));
  818.               any_failed = 1;
  819.               break;
  820.             case 1:
  821.               /* A makefile from the MAKEFILES variable.
  822.              We don't care.  */
  823.               break;
  824.             case 2:
  825.               /* An included makefile.  We don't need
  826.              to die, but we do want to complain.  */
  827.               error ("Included makefile `%s' was not found.",
  828.                  dep_name (d));
  829.               break;
  830.             }
  831.  
  832.         free ((char *) d);
  833.           }
  834.  
  835.         if (any_remade)
  836.           goto re_exec;
  837.         else if (any_failed)
  838.           die (1);
  839.         else
  840.           break;
  841.       }
  842.  
  843.     case 0:
  844.     re_exec:;
  845.       /* Updated successfully.  Re-exec ourselves.  */
  846.       if (print_directory_flag)
  847.         log_working_directory (0);
  848.       if (debug_flag)
  849.         puts ("Re-execing myself....");
  850.       if (makefiles != 0)
  851.         {
  852.           /* These names might have changed.  */
  853.           register unsigned int i, j = 0;
  854.           for (i = 1; i < argc; ++i)
  855.         if (!strcmp (argv[i], "-f"))
  856.             {
  857.               char *p = &argv[i][2];
  858.               if (*p == '\0')
  859.             argv[++i] = makefiles->list[j];
  860.               else
  861.             argv[i] = concat ("-f", makefiles->list[j], "");
  862.               ++j;
  863.             }
  864.         }
  865.       if (directories != 0 && directories->idx > 0)
  866.         {
  867.           char bad;
  868.           if (current_directory[0] != '\0')
  869.         {
  870.           if (chdir (current_directory) < 0)
  871.             {
  872.               perror_with_name ("chdir", "");
  873.               bad = 1;
  874.             }
  875.           else
  876.             bad = 0;
  877.         }
  878.           else
  879.         bad = 1;
  880.           if (bad)
  881.         fatal ("Couldn't change back to original directory.");
  882.         }
  883.       fflush (stdout);
  884.       fflush (stderr);
  885.       for (p = environ; *p != 0; ++p)
  886.         if (!strncmp (*p, "MAKELEVEL=", 10))
  887.           {
  888.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  889.         break;
  890.           }
  891. #ifdef MSDOS        /* MSC'c execve () will fail with our spawn(). */
  892.       exit (swap_spawnvpe (argv[0], argv, environ));
  893. #else /* not MSDOS */
  894.       exec_command (argv, environ,
  895.             allocated_variable_expand ("$(PATH)"),
  896.             allocated_variable_expand ("$(SHELL)"));
  897. #endif /* not MSDOS */
  898.       /* NOTREACHED */
  899.     }
  900.     }
  901.  
  902.   {
  903.     int status;
  904.  
  905.     /* If there were no command-line goals, use the default.  */
  906.     if (goals == 0)
  907.       {
  908.     if (default_goal_file != 0)
  909.       {
  910.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  911.         goals->next = 0;
  912.         goals->name = 0;
  913.         goals->file = default_goal_file;
  914.       }
  915.       }
  916.     else
  917.       lastgoal->next = 0;
  918.  
  919.     if (goals != 0)
  920.       {
  921.     /* Update the goals.  */
  922.  
  923.     if (debug_flag)
  924.       puts ("Updating goal targets....");
  925.  
  926.     switch (update_goal_chain (goals, 0))
  927.       {
  928.       case -1:
  929.         /* Nothing happened.  */
  930.       case 0:
  931.         /* Updated successfully.  */
  932.         status = 0;
  933.         break;
  934.       case 1:
  935.         /* Updating failed.  */
  936.         status = 1;
  937.         break;
  938.       default:
  939.         abort ();
  940.       }
  941.       }
  942.  
  943.     /* Print the data base under -p.  */
  944.     if (print_data_base_flag)
  945.       print_data_base ();
  946.  
  947.     if (goals == 0)
  948.       fatal ("No targets");
  949.  
  950.     /* Exit.  */
  951.     die (status);
  952.   }
  953.  
  954.   return 0;
  955. }
  956.  
  957. static void
  958. decode_switches (argc, argv)
  959.      int argc;
  960.      char **argv;
  961. {
  962.   char bad = 0;
  963.   register unsigned int i;
  964.   register char *sw;
  965.   register struct command_switch *cs;
  966.   register struct stringlist *sl;
  967.   char *arg;
  968.  
  969.   decode_env_switches ("MAKEFLAGS", 9);
  970.   decode_env_switches ("MFLAGS", 6);
  971.  
  972.   other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
  973.   other_args->max = 5;
  974.   other_args->list = (char **) xmalloc (5 * sizeof (char *));
  975.   other_args->idx = 1;
  976.   other_args->list[0] = savestring (argv[0], strlen (argv[0]));
  977.  
  978.   for (i = 1; i < argc; i++)
  979.     {
  980.       sw = argv[i];
  981.       if (*sw++ == '-')
  982.     while (*sw != '\0')
  983.       {
  984.         for (cs = switches; cs->c != '\0'; ++cs)
  985.           if (cs->c == *sw)
  986.         {
  987.           ++sw;
  988.           switch (cs->type)
  989.             {
  990.             default:
  991.               abort ();
  992.             case ignore:
  993.               break;
  994.             case flag:
  995.             case flag_off:
  996.               *(int *) cs->value_ptr = cs->type == flag;
  997.               break;
  998.             case string:
  999.               if (*sw == '\0')
  1000.             {
  1001.               arg = argv[++i];
  1002.               if (arg == 0)
  1003.                 {
  1004.                   arg = cs->noarg_value;
  1005.                   if (arg == 0)
  1006.                 {
  1007.                 missing_arg:;
  1008.                   error ("argument required for `-%c' option",
  1009.                      cs->c);
  1010.                   bad = 1;
  1011.                   break;
  1012.                 }
  1013.                 }
  1014.             }
  1015.               else
  1016.             arg = sw;
  1017.  
  1018.               sl = *(struct stringlist **) cs->value_ptr;
  1019.               if (sl == 0)
  1020.             {
  1021.               sl = (struct stringlist *)
  1022.                 xmalloc (sizeof (struct stringlist));
  1023.               sl->max = 5;
  1024.               sl->idx = 0;
  1025.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1026.               *(struct stringlist **) cs->value_ptr = sl;
  1027.             }
  1028.               else if (sl->idx == sl->max - 1)
  1029.             {
  1030.               sl->max += 5;
  1031.               sl->list = (char **)
  1032.                 xrealloc ((char *) sl->list,
  1033.                       sl->max * sizeof (char *));
  1034.             }
  1035.               sl->list[sl->idx++] = savestring (arg, strlen (arg));
  1036.               sl->list[sl->idx] = 0;
  1037.               sw = "";
  1038.               break;
  1039.  
  1040.             case positive_int:
  1041.               if (*sw == '\0')
  1042.             arg = argv[++i];
  1043.               else
  1044.             arg = sw;
  1045.               if (arg != 0 && isdigit (*arg))
  1046.             {
  1047.               int i = atoi (arg);
  1048.               while (isdigit (*arg))
  1049.                 ++arg;
  1050.               if (i < 1)
  1051.                 error ("the `-%c' option requires a \
  1052. positive integral argument",
  1053.                    cs->c);
  1054.               else
  1055.                 *(unsigned int *) cs->value_ptr = i;
  1056.             }
  1057.               else
  1058.             {
  1059.               if (cs->noarg_value != 0)
  1060.                 *(unsigned int *) cs->value_ptr
  1061.                   = *(unsigned int *) cs->noarg_value;
  1062.               else
  1063.                 {
  1064.                   error ("the `-%c' option requires an argument",
  1065.                      cs->c);
  1066.                   bad = 1;
  1067.                 }
  1068.  
  1069.               if (arg == argv[i])
  1070.                 /* We moved to the next arg, so move back.  */
  1071.                 --i;
  1072.             }
  1073.               sw = "";
  1074.               break;
  1075.  
  1076.             case floating:
  1077.               if (*sw == '\0')
  1078.             arg = argv[++i];
  1079.               else
  1080.             arg = sw;
  1081.               if (arg != 0 && (*arg == '.' || isdigit (*arg)))
  1082.             {
  1083.               *(double *) cs->value_ptr = atof (arg);
  1084.               while (*arg == '.' || isdigit (*arg))
  1085.                 ++arg;
  1086.             }
  1087.               else
  1088.             {
  1089.               if (cs->noarg_value != 0)
  1090.                 *(double *) cs->value_ptr
  1091.                   = *(double *) cs->noarg_value;
  1092.               else
  1093.                 {
  1094.                   error ("the `-%c' option requires an argument",
  1095.                      cs->c);
  1096.                   bad = 1;
  1097.                 }
  1098.  
  1099.               if (arg == argv[i])
  1100.                 /* We moved to the next arg, so move back.  */
  1101.                 --i;
  1102.             }
  1103.               sw = "";
  1104.               break;
  1105.             }
  1106.  
  1107.           /* We've found the switch.  Stop looking.  */
  1108.           break;
  1109.         }
  1110.           
  1111.         if (cs->c == '\0')
  1112.           {
  1113.         error ("unknown option `-%c'", *sw++);
  1114.         bad = 1;
  1115.           }
  1116.       }
  1117.       else
  1118.     {
  1119.       if (other_args->idx == other_args->max - 1)
  1120.         {
  1121.           other_args->max += 5;
  1122.           other_args->list = (char **)
  1123.         xrealloc ((char *) other_args->list,
  1124.               other_args->max * sizeof (char *));
  1125.         }
  1126.       other_args->list[other_args->idx++] = argv[i];
  1127.     }
  1128.     }
  1129.  
  1130.   if (other_args != 0)
  1131.     other_args->list[other_args->idx] = 0;
  1132.  
  1133.   if (bad)
  1134.     die (1);
  1135. }
  1136.  
  1137. static void
  1138. decode_env_switches (envar, len)
  1139.      char *envar;
  1140.      unsigned int len;
  1141. {
  1142.   struct variable *v;
  1143.   register char *args;
  1144.   register struct command_switch *cs;
  1145.  
  1146.   v = lookup_variable (envar, len);
  1147.   if (v == 0 || *v->value == '\0')
  1148.     return;
  1149.  
  1150.   for (args = v->value; *args != '\0'; ++args)
  1151.     for (cs = switches; cs->c != '\0'; ++cs)
  1152.       if (cs->c == *args)
  1153.     if (cs->env)
  1154.       switch (cs->type)
  1155.         {
  1156.         case string:
  1157.           /* None of these allow environment changes.  */
  1158.         default:
  1159.           abort ();
  1160.         case flag:
  1161.         case flag_off:
  1162.           *(int *) cs->value_ptr = cs->type == flag;
  1163.           break;
  1164.         case positive_int:
  1165.           while (isspace (args[1]))
  1166.         ++args;
  1167.           if (isdigit(args[1]))
  1168.         {
  1169.           int i = atoi (&args[1]);
  1170.           while (isdigit (args[1]))
  1171.             ++args;
  1172.           if (i >= 1)
  1173.             *(unsigned int *) cs->value_ptr = i;
  1174.         }
  1175.           else
  1176.         *(unsigned int *) cs->value_ptr
  1177.           = *(unsigned int *) cs->noarg_value;
  1178.           break;
  1179.         case floating:
  1180.           while (isspace (args[1]))
  1181.         ++args;
  1182.           if (args[1] == '.' || isdigit (args[1]))
  1183.         {
  1184.           *(double *) cs->value_ptr = atof (&args[1]);
  1185.           while (args[1] == '.' || isdigit (args[1]))
  1186.             ++args;
  1187.         }
  1188.           else
  1189.         *(double *) cs->value_ptr = *(double *) cs->noarg_value;
  1190.           break;
  1191.         }
  1192. }
  1193.  
  1194. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1195.    command switches.  Include positive_int and floating options if PF.  */
  1196.  
  1197. static void
  1198. define_makeflags (pf)
  1199.      int pf;
  1200. {
  1201.   register struct command_switch *cs;
  1202.   char flags[200];
  1203.   register unsigned int i;
  1204.  
  1205.   i = 0;
  1206.   for (cs = switches; cs->c != '\0'; ++cs)
  1207.     if (cs->toenv)
  1208.       {
  1209.     if (i == 0 || flags[i - 1] == ' ')
  1210.       flags[i++] = '-';
  1211.     switch (cs->type)
  1212.       {
  1213.       default:
  1214.         abort ();
  1215.  
  1216.       case ignore:
  1217.         break;
  1218.  
  1219.       case flag:
  1220.       case flag_off:
  1221.         if ((cs->default_value != 0
  1222.          && *(int *) cs->value_ptr != *(int *) cs->default_value)
  1223.         || !*(int *) cs->value_ptr == (cs->type == flag_off))
  1224.           flags[i++] = cs->c;
  1225.         break;
  1226.  
  1227.       case positive_int:
  1228.         if (pf)
  1229.           {
  1230.         if ((cs->default_value != 0
  1231.              && (*(unsigned int *) cs->value_ptr
  1232.              == *(unsigned int *) cs->default_value)))
  1233.           break;
  1234.         else if (cs->noarg_value != 0
  1235.              && (*(unsigned int *) cs->value_ptr ==
  1236.                  *(unsigned int *) cs->noarg_value))
  1237.           flags[i++] = cs->c;
  1238.         else
  1239.           {
  1240.             unsigned int value;
  1241.             if (cs->c == 'j')
  1242.               value = 1;
  1243.             else
  1244.               value = *(unsigned int *) cs->value_ptr;
  1245.             sprintf (&flags[i], "%c%u ", cs->c, value);
  1246.             i += strlen (&flags[i]);
  1247.           }
  1248.           }
  1249.         break;
  1250.  
  1251.       case floating:
  1252.         if (pf)
  1253.           {
  1254.         if (cs->default_value != 0
  1255.             && (*(double *) cs->value_ptr
  1256.             == *(double *) cs->default_value))
  1257.           break;
  1258.         else if (cs->noarg_value != 0
  1259.              && (*(double *) cs->value_ptr
  1260.                  == *(double *) cs->noarg_value))
  1261.           flags[i++] = cs->c;
  1262.         else
  1263.           {
  1264.             sprintf (&flags[i], "%c%f ",
  1265.                  cs->c, *(double *) cs->value_ptr);
  1266.             i += strlen (&flags[i]);
  1267.           }
  1268.           }
  1269.         break;
  1270.       }
  1271.       }
  1272.  
  1273.   if (i == 0)
  1274.     flags[0] = flags[1] = '\0';
  1275.   else if (flags[i - 1] == ' ' || flags[i - 1] == '-')
  1276.     flags[i - 1] = '\0';
  1277.   else
  1278.     flags[i] = '\0';
  1279.  
  1280.   /* On Sun, the value of MFLAGS starts with a `-' but the
  1281.      value of MAKEFLAGS lacks the `-'.  Be compatible.  */
  1282.   (void) define_variable ("MAKEFLAGS", 9, &flags[1], o_env, 0);
  1283.   (void) define_variable ("MFLAGS", 6, flags, o_env, 0);
  1284. }
  1285.  
  1286. static int printed_version = 0;
  1287.  
  1288. /* Print version information.  */
  1289.  
  1290. static void
  1291. print_version ()
  1292. {
  1293.   extern char *remote_description;
  1294.   char *precede = print_data_base_flag ? "# " : "";
  1295.  
  1296.   printf ("%sGNU Make version %s", precede, version_string);
  1297.   if (remote_description != 0 && *remote_description != '\0')
  1298.     printf ("-%s", remote_description);
  1299.  
  1300.   printf (", by Richard Stallman and Roland McGrath.\n\
  1301. %sCopyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.\n\
  1302. %sThis is free software; see the source for copying conditions.\n\
  1303. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1304. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1305.  
  1306. #ifdef MSDOS
  1307.   printf ("%sMS-DOS port (C) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>\n\
  1308. %sthis is a beta version, compiled %s.\n\n",
  1309.       precede, precede, msdos_version_string);
  1310. #endif /* MSDOS */
  1311.  
  1312.   printed_version = 1;
  1313.  
  1314.   /* Flush stdout so the user doesn't have to wait to see the
  1315.      version information while things are thought about.  */
  1316.   fflush (stdout);
  1317. }
  1318.  
  1319. /* Print a bunch of information about this and that.  */
  1320.  
  1321. static void
  1322. print_data_base ()
  1323. {
  1324.   extern char *ctime ();
  1325.   time_t when;
  1326.  
  1327.   when = time ((time_t *) 0);
  1328.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1329.  
  1330.   print_variable_data_base ();
  1331.   print_dir_data_base ();
  1332.   print_rule_data_base ();
  1333.   print_file_data_base ();
  1334.   print_vpath_data_base ();
  1335.  
  1336.   when = time ((time_t *) 0);
  1337.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1338. }
  1339.  
  1340. /* Exit with STATUS, cleaning up as necessary.  */
  1341.  
  1342. void
  1343. die (status)
  1344.      int status;
  1345. {
  1346.   static char dying = 0;
  1347.  
  1348.   if (!dying)
  1349.     {
  1350.       dying = 1;
  1351.  
  1352.       if (print_version_flag && !printed_version)
  1353.     print_version ();
  1354.  
  1355.       /* Wait for children to die.  */
  1356.       wait_for_children (0, status != 0);
  1357.  
  1358.       /* Remove the intermediate files.  */
  1359.  
  1360.       remove_intermediates (0);
  1361.  
  1362.       if (print_directory_flag)
  1363.     log_working_directory (0);
  1364.     }
  1365.  
  1366. #ifdef MSDOS
  1367.   if (directories != 0 && directories->idx > 0)
  1368.     {
  1369.       char bad;
  1370.       if (current_directory[0] != '\0')
  1371.     {
  1372.       if (chdir (current_directory) < 0)
  1373.         {
  1374.           perror_with_name ("chdir", "");
  1375.           bad = 1;
  1376.         }
  1377.       else
  1378.         bad = 0;
  1379.     }
  1380.       else
  1381.     bad = 1;
  1382.       if (bad)
  1383.     fatal ("Couldn't change back to original directory.");
  1384.     }
  1385. #endif / MSDOS */
  1386.  
  1387.   exit (status);
  1388. }
  1389.  
  1390. /* Write a message indicating that we've just entered or
  1391.    left (according to ENTERING) the current directory.  */
  1392.  
  1393. static void
  1394. log_working_directory (entering)
  1395.      int entering;
  1396. {
  1397.   static int entered = 0;
  1398.   char pwdbuf[MAXPATHLEN];
  1399.   char *message = entering ? "Entering" : "Leaving";
  1400.  
  1401.   if (entered && entering)
  1402.     /* Don't print the leaving message if we
  1403.        haven't printed the entering message.  */
  1404.     return;
  1405.   else
  1406.     entered = 1;
  1407.  
  1408.   if (print_data_base_flag)
  1409.     fputs ("# ", stdout);
  1410.  
  1411.   if (makelevel == 0)
  1412.     printf ("%s: %s ", program, message);
  1413.   else
  1414.     printf ("%s[%u]: %s ", program, makelevel, message);
  1415.  
  1416. #ifdef    USG
  1417.   /* In some System V's, `getcwd' spawns a child running /bin/pwd.  */
  1418.   block_children ();
  1419. #endif
  1420.   if (getwd (pwdbuf) == 0)
  1421.     printf ("an unknown directory (getwd: %s)\n", pwdbuf);
  1422.   else
  1423.     printf ("directory `%s'\n", pwdbuf);
  1424. #ifdef    USG
  1425.   unblock_children ();
  1426. #endif
  1427. }
  1428.