home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / make3_60.lzh / MAKE3_60 / REMAKE.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  22KB  |  880 lines

  1. /* Copyright (C) 1988-1991 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. #include "make.h"
  19. #include "commands.h"
  20. #include "job.h"
  21. #include "dep.h"
  22. #include "file.h"
  23.  
  24. #ifndef USG
  25. #ifndef    sgi
  26. #include <sys/file.h>
  27. #endif
  28. #else
  29. #include <fcntl.h>
  30. #endif
  31.  
  32.  
  33. #if    !defined(__GNU_LIBRARY__) && !defined(_POSIX_SOURCE)
  34. extern int fstat ();
  35. extern time_t time ();
  36. #endif
  37.  
  38. extern int try_implicit_rule ();
  39.  
  40.  
  41. /* Incremented when a file has been remade.  */
  42. static unsigned int files_remade = 0;
  43.  
  44. static int update_file (), update_file_1 (), check_dep ();
  45. static void remake_file ();
  46. static time_t name_mtime (), library_file_mtime ();
  47. extern time_t f_mtime ();
  48.  
  49. /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
  50.    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
  51.    If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
  52.    be disabled for them unless they were also command-line targets, and we
  53.    should only make one goal at a time and return as soon as one goal whose
  54.    `changed' member is nonzero is successfully made.  */
  55.  
  56. int
  57. update_goal_chain (goals, makefiles)
  58.      register struct dep *goals;
  59.      int makefiles;
  60. {
  61.   int t = touch_flag, q = question_flag, n = just_print_flag;
  62.   unsigned int j = job_slots;
  63.   int status = -1;
  64.  
  65. #define    MTIME(file) (makefiles ? file_mtime_no_search (file) \
  66.              : file_mtime (file))
  67.  
  68.   /* Duplicate the chain so we can remove things from it.  */
  69.  
  70.   goals = copy_dep_chain (goals);
  71.  
  72.   if (makefiles)
  73.     /* Only run one job at a time.  */
  74.     job_slots = 1;
  75.  
  76.   /* Update all the goals until they are all finished.  */
  77.  
  78.   while (goals != 0)
  79.     {
  80.       register struct dep *g, *lastgoal;
  81.  
  82.       /* Wait for a child to die.  */
  83.  
  84.       wait_for_children (1, 0);
  85.  
  86.       lastgoal = 0;
  87.       g = goals;
  88.       while (g != 0)
  89.     {
  90.       int x;
  91.       time_t mtime = MTIME (g->file);
  92.       check_renamed (g->file);
  93.  
  94.       if (makefiles)
  95.         {
  96.           if (g->file->cmd_target)
  97.         {
  98.           touch_flag = t;
  99.           question_flag = q;
  100.           just_print_flag = n;
  101.         }
  102.           else
  103.         touch_flag = question_flag = just_print_flag = 0;
  104.         }
  105.  
  106.       x = update_file (g->file, makefiles ? 1 : 0);
  107.       check_renamed (g->file);
  108.       if (x != 0 || g->file->updated)
  109.         {
  110.           int stop = 0;
  111.           /* If STATUS was not already 1, set it to 1 if
  112.          updating failed, or to 0 if updating succeeded.
  113.          Leave STATUS as it is if no updating was done.  */
  114.           if (status < 1)
  115.         {
  116.           if (g->file->update_status != 0)
  117.             {
  118.               /* Updating failed.  */
  119.               status = 1;
  120.               stop = !keep_going_flag && !makefiles;
  121.             }
  122.           else if (MTIME (g->file) != mtime)
  123.             {
  124.               /* Updating was done.
  125.              If this is a makefile and just_print_flag or
  126.              question_flag is set (meaning -n or -q was given
  127.              and this file was specified as a command-line target),
  128.              don't change STATUS.  If STATUS is changed, we will
  129.              get re-exec'd, and fall into an infinite loop.  */
  130.               if (!makefiles || (!just_print_flag && !question_flag))
  131.             status = 0;
  132.               if (makefiles && g->file->dontcare)
  133.             /* This is a default makefile.  Stop remaking.  */
  134.             stop = 1;
  135.             }
  136.         }
  137.  
  138.           /* This goal is finished.  Remove it from the chain.  */
  139.           if (lastgoal == 0)
  140.         goals = g->next;
  141.           else
  142.         lastgoal->next = g->next;
  143.  
  144.           /* Free the storage.  */
  145.           free ((char *) g);
  146.  
  147.           g = lastgoal == 0 ? goals : lastgoal->next;
  148.  
  149.           if (stop)
  150.         break;
  151.         }
  152.       else
  153.         {
  154.           lastgoal = g;
  155.           g = g->next;
  156.         }
  157.     }
  158.     }
  159.  
  160.   if (makefiles)
  161.     {
  162.       touch_flag = t;
  163.       question_flag = q;
  164.       just_print_flag = n;
  165.       job_slots = j;
  166.     }
  167.  
  168.   return status;
  169. }
  170.  
  171. /* If FILE is not up to date, execute the commands for it.
  172.    Return 0 if successful, 1 if unsuccessful;
  173.    but with some flag settings, just call `exit' if unsuccessful.
  174.  
  175.    DEPTH is the depth in recursions of this function.
  176.    We increment it during the consideration of our dependencies,
  177.    then decrement it again after finding out whether this file
  178.    is out of date.
  179.  
  180.    If there are multiple double-colon entries for FILE,
  181.    each is considered in turn.  */
  182.  
  183. static int
  184. update_file (file, depth)
  185.      struct file *file;
  186.      unsigned int depth;
  187. {
  188.   register int status = 0;
  189.   register struct file *f;
  190.   unsigned int ofiles_remade = files_remade;
  191.   int commands_finished = 0;
  192.  
  193.   for (f = file; f != 0; f = f->prev)
  194.     {
  195.       char not_started = f->command_state == cs_not_started;
  196.       status |= update_file_1 (f, depth);
  197.       check_renamed (f);
  198.       if (status != 0 && !keep_going_flag)
  199.     return status;
  200.       commands_finished |= not_started && f->command_state == cs_finished;
  201.     }
  202.  
  203.   /* For a top level target, if we have found nothing whatever to do for it,
  204.      print a message saying nothing needs doing.  */
  205.  
  206.   if (status == 0 && files_remade == ofiles_remade
  207.       && commands_finished && depth == 0 && !silent_flag && !question_flag)
  208.     {
  209.       if (file->phony || file->cmds == 0)
  210.     message ("Nothing to be done for `%s'.", file->name);
  211.       else
  212.     message ("`%s' is up to date.", file->name);
  213.       fflush (stdout);
  214.     }
  215.  
  216.   return status;
  217. }
  218.  
  219. /* Consider a single `struct file' and update it as appropriate.  */
  220.  
  221. static int
  222. update_file_1 (file, depth)
  223.      struct file *file;
  224.      unsigned int depth;
  225. {
  226.   register time_t this_mtime;
  227.   int noexist, must_make, deps_changed;
  228.   int dep_status = 0;
  229.   register struct dep *d, *lastd;
  230.   char running = 0;
  231.  
  232.   DEBUGPR ("Considering target file `%s'.\n");
  233.  
  234.   if (file->updated)
  235.     {
  236.       if (file->update_status > 0)
  237.     {
  238.       DEBUGPR ("Recently tried and failed to update file `%s'.\n");
  239.       return file->update_status;
  240.     }
  241.  
  242.       DEBUGPR ("File `%s' was considered already.\n");
  243.       return 0;
  244.     }
  245.  
  246.   switch (file->command_state)
  247.     {
  248.     case cs_not_started:
  249.     case cs_deps_running:
  250.       break;
  251.     case cs_running:
  252.       DEBUGPR ("Still updating file `%s'.\n");
  253.       return 0;
  254.     case cs_finished:
  255.       DEBUGPR ("Finished updating file `%s'.\n");
  256.       return file->update_status;
  257.     default:
  258.       abort ();
  259.     }
  260.  
  261.   ++depth;
  262.  
  263.   /* Notice recursive update of the same file.  */
  264.   file->updating = 1;
  265.  
  266.   /* Looking at the file's modtime beforehand allows the possibility
  267.      that its name may be changed by a VPATH search, and thus it may
  268.      not need an implicit rule.  If this were not done, the file
  269.      might get implicit commands that apply to its initial name, only
  270.      to have that name replaced with another found by VPATH search.  */
  271.  
  272.   this_mtime = file_mtime (file);
  273.   check_renamed (file);
  274.   noexist = this_mtime == (time_t) -1;
  275.   if (noexist)
  276.     DEBUGPR ("File `%s' does not exist.\n");
  277.  
  278.   must_make = noexist;
  279.  
  280.   /* If file was specified as a target with no commands,
  281.      come up with some default commands.  */
  282.  
  283.   if (!file->phony && file->cmds == 0 && !file->tried_implicit)
  284.     {
  285.       if (try_implicit_rule (file, depth))
  286.     DEBUGPR ("Found an implicit rule for `%s'.\n");
  287.       else
  288.     {
  289.       DEBUGPR ("No implicit rule found for `%s'.\n");
  290.       if (default_file != 0 && default_file->cmds != 0)
  291.         {
  292.           DEBUGPR ("Using default commands for `%s'.\n");
  293.           file->cmds = default_file->cmds;
  294.         }
  295.       file->also_make = 0;
  296.     }
  297.       file->tried_implicit = 1;
  298.     }
  299.  
  300.   /* Update all non-intermediate files we depend on, if necessary,
  301.      and see whether any of them is more recent than this file.  */
  302.  
  303.   lastd = 0;
  304.   d = file->deps;
  305.   while (d != 0)
  306.     {
  307.       time_t mtime;
  308.  
  309.       mtime = file_mtime (d->file);
  310.       check_renamed (d->file);
  311.  
  312.       if (d->file->updating)
  313.     {
  314.       error ("Circular %s <- %s dependency dropped.",
  315.          file->name, d->file->name);
  316.       if (lastd == 0)
  317.         {
  318.           file->deps = d->next;
  319.           free ((char *) d);
  320.           d = file->deps;
  321.         }
  322.       else
  323.         {
  324.           lastd->next = d->next;
  325.           free ((char *) d);
  326.           d = lastd->next;
  327.         }
  328.       continue;
  329.     }
  330.  
  331.       d->file->parent = file;
  332.       dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
  333.       check_renamed (d->file);
  334.  
  335.       running |= (d->file->command_state == cs_running
  336.           || d->file->command_state == cs_deps_running);
  337.  
  338.       if (dep_status != 0 && !keep_going_flag)
  339.     break;
  340.  
  341.       if (!running)
  342.     d->changed = file_mtime (d->file) != mtime;
  343.  
  344.       lastd = d;
  345.       d = d->next;
  346.     }
  347.  
  348.   /* Now we know whether this target needs updating.
  349.      If it does, update all the intermediate files we depend on.  */
  350.  
  351.   if (must_make)
  352.     {
  353.       for (d = file->deps; d != 0; d = d->next)
  354.     if (d->file->intermediate)
  355.       {
  356.         time_t mtime = file_mtime (d->file);
  357.         check_renamed (d->file);
  358.         d->file->parent = file;
  359.         dep_status |= update_file (d->file, depth);
  360.         check_renamed (d->file);
  361.  
  362.         running |= (d->file->command_state == cs_running
  363.             || d->file->command_state == cs_deps_running);
  364.  
  365.         if (dep_status != 0 && !keep_going_flag)
  366.           break;
  367.  
  368.         if (!running)
  369.           d->changed = ((file->phony && file->cmds != 0)
  370.                 || file_mtime (d->file) != mtime);
  371.       }
  372.     }
  373.  
  374.   file->updating = 0;
  375.  
  376.   DEBUGPR ("Finished dependencies of target file `%s'.\n");
  377.  
  378.   /* If any dependency failed, give up now.  */
  379.  
  380.   if (dep_status != 0)
  381.     {
  382.       file->command_state = cs_finished;
  383.       file->update_status = dep_status;
  384.       file->updated = 1;
  385.  
  386.       depth--;
  387.  
  388.       DEBUGPR ("Giving up on target file `%s'.\n");
  389.  
  390.       if (depth == 0 && keep_going_flag
  391.       && !just_print_flag && !question_flag)
  392.     error ("Target `%s' not remade because of errors.", file->name);
  393.  
  394.       return dep_status;
  395.     }
  396.  
  397.   if (running)
  398.     {
  399.       file->command_state = cs_deps_running;
  400.       --depth;
  401.       DEBUGPR ("The dependencies of `%s' are being made.\n");
  402.       return 0;
  403.     }
  404.  
  405.   file->command_state = cs_not_started;
  406.  
  407.   /* Now record which dependencies are more
  408.      recent than this file, so we can define $?.  */
  409.  
  410.   deps_changed = 0;
  411.   for (d = file->deps; d != 0; d = d->next)
  412.     {
  413.       time_t d_mtime = file_mtime (d->file);
  414.       check_renamed (d->file);
  415.  
  416. #if 1    /* %%% In version 4, remove this code completely to
  417.        implement not remaking deps if their deps are newer
  418.        than their parents.  */
  419.       if (d_mtime == (time_t) -1 && !d->file->intermediate)
  420.     /* We must remake if this dep does not
  421.        exist and is not intermediate.  */
  422.     must_make = 1;
  423. #endif
  424.  
  425.       /* Set DEPS_CHANGED if this dep actually changed.  */
  426.       deps_changed |= d->changed;
  427.  
  428.       /* Set D->changed if either this dep actually changed,
  429.      or its dependent, FILE, is older or does not exist.  */
  430.       d->changed |= noexist || d_mtime > this_mtime;
  431.  
  432.       if (debug_flag && !noexist)
  433.     {
  434.       print_spaces (depth);
  435.       if (d_mtime == (time_t) -1)
  436.         printf ("Dependency `%s' does not exist.\n", dep_name (d));
  437.       else
  438.         printf ("Dependency `%s' is %s than dependent `%s'.\n",
  439.             dep_name (d), d->changed ? "newer" : "older", file->name);
  440.       fflush (stdout);
  441.     }
  442.     }
  443.  
  444.   /* Here depth returns to the value it had when we were called.  */
  445.   depth--;
  446.  
  447.   if (file->double_colon && file->deps == 0)
  448.     {
  449.       must_make = 1;
  450.       DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
  451.     }
  452.   else if (file->is_target && !deps_changed && file->cmds == 0)
  453.     {
  454.       must_make = 0;
  455.       DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
  456.     }
  457.  
  458.   if (!must_make)
  459.     {
  460.       DEBUGPR ("No need to remake target `%s'.\n");
  461.       file->command_state = cs_finished;
  462.       file->update_status = 0;
  463.       file->updated = 1;
  464.       return 0;
  465.     }
  466.  
  467.   DEBUGPR ("Must remake target `%s'.\n");
  468.  
  469.   /* Now, take appropriate actions to remake the file.  */
  470.   remake_file (file);
  471.  
  472.   if (file->command_state != cs_finished)
  473.     {
  474.       DEBUGPR ("Commands of `%s' are being run.\n");
  475.       return 0;
  476.     }
  477.  
  478.   switch (file->update_status)
  479.     {
  480.     case 1:
  481.       DEBUGPR ("Failed to remake target file `%s'.\n");
  482.       break;
  483.     case 0:
  484.       DEBUGPR ("Successfully remade target file `%s'.\n");
  485.       break;
  486.     case -1:
  487.       error ("internal error: `%s' update_status is -1 at cs_finished!",
  488.          file->name);
  489.       abort ();
  490.     default:
  491.       error ("internal error: `%s' update_status invalid!", file->name);
  492.       abort ();
  493.     }
  494.  
  495.   file->updated = 1;
  496.   return file->update_status;
  497. }
  498.  
  499. /* Set FILE's `updated' flag and re-check its mtime and the mtime's
  500.    of all files listed in its `also_make' member.  */
  501.  
  502. void
  503. notice_finished_file (file)
  504.      register struct file *file;
  505. {
  506.   file->updated = 1;
  507.  
  508.   ++files_remade;
  509.  
  510.   if (!file->phony)
  511.     {
  512.       if (just_print_flag || question_flag
  513.       || (file->is_target && file->cmds == 0))
  514.     file->last_mtime = time ((time_t *) 0);
  515.       else
  516.     file->last_mtime = 0;
  517.     }
  518.  
  519.   if (file->also_make != 0)
  520.     {
  521.       register unsigned int i;
  522.       for (i = 0; file->also_make[i] != 0; ++i)
  523.     {
  524.       register struct file *f = enter_file (file->also_make[i]);
  525.       f->updated = 1;
  526.       f->update_status = file->update_status;
  527.       if (just_print_flag || question_flag)
  528.         f->last_mtime = file->last_mtime;
  529.       else
  530.         f->last_mtime = 0;
  531.     }
  532.     }
  533. }
  534.  
  535. /* Check whether another file (whose mtime is THIS_MTIME)
  536.    needs updating on account of a dependency which is file FILE.
  537.    If it does, store 1 in *MUST_MAKE_PTR.
  538.    In the process, update any non-intermediate files
  539.    that FILE depends on (including FILE itself).
  540.    Return nonzero if any updating failed.  */
  541.  
  542. static int
  543. check_dep (file, depth, this_mtime, must_make_ptr)
  544.      struct file *file;
  545.      unsigned int depth;
  546.      time_t this_mtime;
  547.      int *must_make_ptr;
  548. {
  549.   register struct dep *d;
  550.   int dep_status = 0;
  551.  
  552.   ++depth;
  553.   file->updating = 1;
  554.  
  555.   if (!file->intermediate)
  556.     /* If this is a non-intermediate file, update it and record
  557.        whether it is newer than THIS_MTIME.  */
  558.     {
  559.       time_t mtime;
  560.       dep_status = update_file (file, depth);
  561.       check_renamed (file);
  562.       mtime = file_mtime (file);
  563.       check_renamed (file);
  564.       if (mtime == (time_t) -1 || mtime > this_mtime)
  565.     *must_make_ptr = 1;
  566.     }
  567.   else
  568.     {
  569.       /* FILE is an intermediate file.
  570.      Update all non-intermediate files we depend on, if necessary,
  571.      and see whether any of them is more recent than the file
  572.      on whose behalf we are checking.  */
  573.       register struct dep *lastd;
  574.       lastd = 0;
  575.       d = file->deps;
  576.       while (d != 0)
  577.     {
  578.       if (d->file->updating)
  579.         {
  580.           error ("Circular %s <- %s dependency dropped.",
  581.              file->name, d->file->name);
  582.           if (lastd == 0)
  583.         {
  584.           file->deps = d->next;
  585.           free ((char *) d);
  586.           d = file->deps;
  587.         }
  588.           else
  589.         {
  590.           lastd->next = d->next;
  591.           free ((char *) d);
  592.           d = lastd->next;
  593.         }
  594.           continue;
  595.         }
  596.  
  597.       d->file->parent = file;
  598.       dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
  599.       check_renamed (d->file);
  600.       if (dep_status != 0 && !keep_going_flag)
  601.         break;
  602.  
  603.       lastd = d;
  604.       d = d->next;
  605.     }
  606.     }
  607.  
  608.   file->updating = 0;
  609.   return dep_status;
  610. }
  611.  
  612. /* Touch FILE.  Return zero if successful, nonzero if not.  */
  613.  
  614. #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
  615.  
  616. static int
  617. touch_file (file)
  618.      register struct file *file;
  619. {
  620.   if (!silent_flag)
  621.     {
  622.       printf ("touch %s\n", file->name);
  623.       fflush (stdout);
  624.     }
  625.  
  626. #ifndef    NO_ARCHIVES
  627.   if (ar_name (file->name))
  628.     return ar_touch (file->name);
  629.   else
  630. #endif
  631.     {
  632.       int fd = open (file->name, O_RDWR | O_CREAT, 0666);
  633.  
  634.       if (fd < 0)
  635.     TOUCH_ERROR ("touch: open: ");
  636.       else
  637.     {
  638.       struct stat statbuf;
  639.       char buf;
  640.  
  641.       if (fstat (fd, &statbuf) < 0)
  642.         TOUCH_ERROR ("touch: fstat: ");
  643.       /* Rewrite character 0 same as it already is.  */
  644.       if (read (fd, &buf, 1) < 0)
  645.         TOUCH_ERROR ("touch: read: ");
  646.       if (lseek (fd, 0L, 0) < 0L)
  647.         TOUCH_ERROR ("touch: lseek: ");
  648.       if (write (fd, &buf, 1) < 0)
  649.         TOUCH_ERROR ("touch: write: ");
  650.       /* If file length was 0, we just
  651.          changed it, so change it back.  */
  652.       if (statbuf.st_size == 0)
  653.         {
  654.           (void) close (fd);
  655.           fd = open (file->name, O_RDWR | O_TRUNC, 0666);
  656.           if (fd < 0)
  657.         TOUCH_ERROR ("touch: open: ");
  658.         }
  659.       (void) close (fd);
  660.     }
  661.     }
  662.  
  663.   return 0;
  664. }
  665.  
  666. /* Having checked and updated the dependencies of FILE,
  667.    do whatever is appropriate to remake FILE itself.
  668.    Return the status from executing FILE's commands.  */
  669.  
  670. static void
  671. remake_file (file)
  672.      struct file *file;
  673. {
  674.   if (file->cmds == 0)
  675.     {
  676.       if (file->phony)
  677.     /* Phony target.  Pretend it succeeded.  */
  678.     file->update_status = 0;
  679.       else if (file->is_target)
  680.     /* This is a nonexistent target file we cannot make.
  681.        Pretend it was successfully remade.  */
  682.     file->update_status = 0;
  683.       else
  684.     {
  685.       /* This is a dependency file we cannot remake.  Fail.  */
  686.       static char noway[] = "*** No way to make target";
  687.       if (keep_going_flag || file->dontcare)
  688.         {
  689.           if (!file->dontcare)
  690.         error ("%s `%s'.", noway, file->name);
  691.            file->update_status = 1;
  692.         }
  693.       else
  694.         fatal ("%s `%s'", noway, file->name);
  695.     }
  696.     }
  697.   else
  698.     {
  699.       chop_commands (file->cmds);
  700.  
  701.       if (touch_flag && !file->cmds->any_recurse)
  702.     {
  703.       if (file->phony)
  704.         file->update_status = 0;
  705.       else
  706.         /* Should set file's modification date and do nothing else.  */
  707.         file->update_status = touch_file (file);
  708.     }
  709.       else
  710.     {
  711.       execute_file_commands (file);
  712.       return;
  713.     }
  714.     }
  715.  
  716.   file->command_state = cs_finished;
  717.   notice_finished_file (file);
  718. }
  719.  
  720. /* Return the mtime of a file, given a `struct file'.
  721.    Caches the time in the struct file to avoid excess stat calls.  If the file
  722.    is not found, and SEARCH is nonzero, VPATH searching and replacement is
  723.    done.  If that fails, a library (-lLIBNAME) is tried but the library's
  724.    actual name (/lib/libLIBNAME.a, etc.) is not substituted into FILE.  */
  725.  
  726. time_t
  727. f_mtime (file, search)
  728.      register struct file *file;
  729.      int search;
  730. {
  731.   register time_t mtime;
  732.  
  733.   /* File's mtime is not known; must get it from the system.  */
  734.  
  735. #ifndef    NO_ARCHIVES
  736.   if (ar_name (file->name))
  737.     {
  738.       /* This file is an archive-member reference.  */
  739.  
  740.       char *arname, *memname;
  741.       struct file *arfile;
  742.       int arname_used = 0;
  743.  
  744.       /* Find the archive's name.  */
  745.       ar_parse_name (file->name, &arname, &memname);
  746.  
  747.       /* Find the modification time of the archive itself.
  748.      Also allow for its name to be changed via VPATH search.  */
  749.       arfile = lookup_file (arname);
  750.       if (arfile == 0)
  751.     {
  752.       arfile = enter_file (arname);
  753.       arname_used = 1;
  754.     }
  755.       mtime = f_mtime (arfile, search);
  756.       check_renamed (arfile);
  757.       if (search && strcmp (arfile->name, arname))
  758.     {
  759.       /* The archive's name has changed.
  760.          Change the archive-member reference accordingly.  */
  761.  
  762.       unsigned int arlen, memlen;
  763.  
  764.       if (!arname_used)
  765.         {
  766.           free (arname);
  767.           arname_used = 1;
  768.         }
  769.  
  770.       arname = arfile->name;
  771.       arlen = strlen (arname);
  772.       memlen = strlen (memname);
  773.  
  774.       free (file->name);
  775.  
  776.       file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
  777.       bcopy (arname, file->name, arlen);
  778.       file->name[arlen] = '(';
  779.       bcopy (memname, file->name + arlen + 1, memlen);
  780.       file->name[arlen + 1 + memlen] = ')';
  781.       file->name[arlen + 1 + memlen + 1] = '\0';
  782.     }
  783.  
  784.       if (!arname_used)
  785.     free (arname);
  786.       free (memname);
  787.  
  788.       if (mtime == (time_t) -1)
  789.     /* The archive doesn't exist, so it's members don't exist either.  */
  790.     return (time_t) -1;
  791.  
  792.       mtime = ar_member_date (file->name);
  793.     }
  794.   else
  795. #endif
  796.     {
  797.       mtime = name_mtime (file->name);
  798.  
  799.       if (mtime == (time_t) -1 && search)
  800.     {
  801.       /* If name_mtime failed, search VPATH.  */
  802.       char *name = file->name;
  803.       if (vpath_search (&name))
  804.         {
  805.           rename_file (file, name);
  806.           check_renamed (file);
  807.           mtime = name_mtime (name);
  808.         }
  809.       else
  810.         /* Last resort, is it a library (-lxxx)?  */
  811.         if (name[0] == '-' && name[1] == 'l')
  812.           mtime = library_file_mtime (&name[2]);
  813.     }
  814.     }
  815.  
  816.   /* Store the mtime into all the entries for this file.  */
  817.  
  818.   while (file != 0)
  819.     {
  820.       file->last_mtime = mtime;
  821.       file = file->prev;
  822.     }
  823.  
  824.   return mtime;
  825. }
  826.  
  827.  
  828. /* Return the mtime of the file or archive-member reference NAME.  */
  829.  
  830. static time_t
  831. name_mtime (name)
  832.      register char *name;
  833. {
  834.   struct stat st;
  835.  
  836.   if (stat (name, &st) < 0)
  837.     return (time_t) -1;
  838.  
  839.   return (time_t) st.st_mtime;
  840. }
  841.  
  842.  
  843. /* Return the mtime of a library file specified as -lLIBNAME,
  844.    searching for a suitable library file in the system library directories
  845.    and the VPATH directories.  */
  846.  
  847. static time_t
  848. library_file_mtime (lib)
  849.      char *lib;
  850. {
  851.   time_t mtime;
  852.   char *name;
  853.  
  854.   name = concat ("/usr/lib/lib", lib, ".a");
  855.   mtime = name_mtime (name);
  856.   if (mtime == (time_t) -1)
  857.     mtime = name_mtime (name + 4);
  858.   if (mtime == (time_t) -1)
  859.     {
  860.       char *local = concat ("/usr/local/lib/lib", lib,  ".a");
  861.       mtime = name_mtime (local);
  862.       free (local);
  863.     }
  864.   if (mtime == (time_t) -1)
  865.     mtime = name_mtime (name + 9);
  866.   if (mtime == (time_t) -1)
  867.     {
  868.       char *newname = name + 9;
  869.       if (vpath_search (&newname))
  870.     {
  871.       mtime = name_mtime (newname);
  872.       free (newname);
  873.     }
  874.     }
  875.  
  876.   free (name);
  877.  
  878.   return mtime;
  879. }
  880.