home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / gnumake / source / ar.c
C/C++ Source or Header  |  1992-03-23  |  4KB  |  162 lines

  1. /* ar.c */
  2. /* Copyright (C) 1988-1991 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "file.h"
  21.  
  22.  
  23. /* Defined in arscan.c.  */
  24. extern long int ar_scan ();
  25. extern int ar_member_touch ();
  26. extern int ar_name_equal ();
  27.  
  28.  
  29. /* Return nonzero if NAME is an archive-member reference, zero if not.
  30.    An archive-member reference is a name like `lib(member)'.
  31.    If a name like `lib((entry))' is used, a fatal error is signaled at
  32.    the attempt to use this unsupported feature.  */
  33.  
  34. int ar_name (char *name)
  35. {
  36.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  37.   
  38.   if (p == 0 || p == name || *end != ')')
  39.     return 0;
  40.  
  41.   if (p[1] == '(' && end[-1] == ')')
  42.     fatal ("attempt to use unsupported feature: `%s'", name);
  43.  
  44.   return 1;
  45. }
  46.  
  47.  
  48. /* Parse the archive-member reference NAME into the archive and member names.
  49.    Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
  50.    put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil.  */
  51.  
  52. void ar_parse_name (char *name, char **arname_p, char **memname_p)
  53. {
  54.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  55.  
  56.   if (arname_p != 0)
  57.     *arname_p = savestring (name, p - name);
  58.  
  59.   if (memname_p != 0)
  60.     *memname_p = savestring (p + 1, end - (p + 1));
  61. }  
  62.  
  63. static long int ar_member_date_1 ();
  64.  
  65. /* Return the modtime of NAME.  */
  66.  
  67. time_t ar_member_date (char *name)
  68. {
  69.   char *arname;
  70.   int arname_used = 0;
  71.   char *memname;
  72.   long int val;
  73.  
  74.   ar_parse_name (name, &arname, &memname);
  75.  
  76.   /* Make sure we know the modtime of the archive itself because
  77.      we are likely to be called just before commands to remake a
  78.      member are run, and they will change the archive itself.  */
  79.   {
  80.     struct file *arfile;
  81.     arfile = lookup_file (arname);
  82.     if (arfile == 0)
  83.       {
  84.     arfile = enter_file (arname);
  85.     arname_used = 1;
  86.       }
  87.  
  88.     (void) f_mtime (arfile, 0);
  89.   }
  90.  
  91.   val = ar_scan (arname, ar_member_date_1, (long int) memname);
  92.  
  93.   if (!arname_used)
  94.     xfree (arname);
  95.   xfree (memname);
  96.  
  97.   return (val <= 0 ? (time_t) -1 : (time_t) val);
  98. }
  99.  
  100. /* This function is called by `ar_scan' to find which member to look at.  */
  101.  
  102. /* ARGSUSED */
  103. static long int ar_member_date_1 (int desc, char *mem, long hdrpos
  104.     , long datapos, long size, long date, int uid, int gid, int mode
  105.     , char *name)
  106. {
  107.   return ar_name_equal (name, mem) ? date : 0;
  108. }
  109.  
  110. /* Set the archive-member NAME's modtime to now.  */
  111.  
  112. int ar_touch (char *name)
  113. {
  114.   char *arname, *memname;
  115.   int arname_used = 0;
  116.   register int val;
  117.  
  118.   ar_parse_name (name, &arname, &memname);
  119.  
  120.   /* Make sure we know the modtime of the archive itself before we
  121.      touch the member, since this will change the archive itself.  */
  122.   {
  123.     struct file *arfile;
  124.     arfile = lookup_file (arname);
  125.     if (arfile == 0)
  126.       {
  127.     arfile = enter_file (arname);
  128.     arname_used = 1;
  129.       }
  130.  
  131.     (void) f_mtime (arfile, 0);
  132.   }
  133.  
  134.   val = 1;
  135.   switch (ar_member_touch (arname, memname))
  136.     {
  137.     case -1:
  138.       error ("touch: Archive `%s' does not exist", arname);
  139.       break;
  140.     case -2:
  141.       error ("touch: `%s' is not a valid archive", arname);
  142.       break;
  143.     case -3:
  144.       perror_with_name ("touch: ", arname);
  145.       break;
  146.     case 1:
  147.       error ("touch: Member `%s' does not exist in `%s'", memname, arname);
  148.       break;
  149.     case 0:
  150.       val = 0;
  151.       break;
  152.     default:
  153.       error ("touch: Bad return code from ar_member_touch on `%s'", name);
  154.     }
  155.  
  156.   if (!arname_used)
  157.     xfree (arname);
  158.   xfree (memname);
  159.  
  160.   return val;
  161. }
  162.