1 /* Interface to `ar' archives for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
26 /* Defined in arscan.c. */
27 extern long int ar_scan
PARAMS ((char *archive
, long int (*function
) (), long int arg
));
28 extern int ar_name_equal
PARAMS ((char *name
, char *mem
, int truncated
));
30 extern int ar_member_touch
PARAMS ((char *arname
, char *memname
));
33 /* Return nonzero if NAME is an archive-member reference, zero if not.
34 An archive-member reference is a name like `lib(member)'.
35 If a name like `lib((entry))' is used, a fatal error is signaled at
36 the attempt to use this unsupported feature. */
41 char *p
= strchr (name
, '(');
44 if (p
== 0 || p
== name
)
47 end
= p
+ strlen (p
) - 1;
51 if (p
[1] == '(' && end
[-1] == ')')
52 fatal (NILF
, _("attempt to use unsupported feature: `%s'"), name
);
58 /* Parse the archive-member reference NAME into the archive and member names.
59 Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
60 put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil. */
63 ar_parse_name (char *name
, char **arname_p
, char **memname_p
)
65 char *p
= strchr (name
, '('), *end
= name
+ strlen (name
) - 1;
68 *arname_p
= savestring (name
, p
- name
);
71 *memname_p
= savestring (p
+ 1, end
- (p
+ 1));
74 static long int ar_member_date_1
PARAMS ((int desc
, char *mem
, int truncated
, long int hdrpos
,
75 long int datapos
, long int size
, long int date
, int uid
, int gid
, int mode
, char *name
));
77 /* Return the modtime of NAME. */
80 ar_member_date (char *name
)
87 ar_parse_name (name
, &arname
, &memname
);
89 /* Make sure we know the modtime of the archive itself because we are
90 likely to be called just before commands to remake a member are run,
91 and they will change the archive itself.
93 But we must be careful not to enter_file the archive itself if it does
94 not exist, because pattern_search assumes that files found in the data
95 base exist or can be made. */
98 arfile
= lookup_file (arname
);
99 if (arfile
== 0 && file_exists_p (arname
))
101 arfile
= enter_file (arname
);
106 (void) f_mtime (arfile
, 0);
109 val
= ar_scan (arname
, ar_member_date_1
, (long int) memname
);
115 return (val
<= 0 ? (time_t) -1 : (time_t) val
);
118 /* This function is called by `ar_scan' to find which member to look at. */
122 ar_member_date_1 (int desc UNUSED
, char *mem
, int truncated
,
123 long int hdrpos UNUSED
, long int datapos UNUSED
,
124 long int size UNUSED
, long int date
,
125 int uid UNUSED
, int gid UNUSED
, int mode UNUSED
, char *name
)
127 return ar_name_equal (name
, mem
, truncated
) ? date
: 0;
130 /* Set the archive-member NAME's modtime to now. */
134 ar_touch (char *name
)
136 error (NILF
, _("touch archive member is not available on VMS"));
141 ar_touch (char *name
)
143 char *arname
, *memname
;
147 ar_parse_name (name
, &arname
, &memname
);
149 /* Make sure we know the modtime of the archive itself before we
150 touch the member, since this will change the archive itself. */
153 arfile
= lookup_file (arname
);
156 arfile
= enter_file (arname
);
160 (void) f_mtime (arfile
, 0);
164 switch (ar_member_touch (arname
, memname
))
167 error (NILF
, _("touch: Archive `%s' does not exist"), arname
);
170 error (NILF
, _("touch: `%s' is not a valid archive"), arname
);
173 perror_with_name ("touch: ", arname
);
177 _("touch: Member `%s' does not exist in `%s'"), memname
, arname
);
184 _("touch: Bad return code from ar_member_touch on `%s'"), name
);
195 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
202 struct nameseq
*chain
;
206 /* This function is called by `ar_scan' to match one archive
207 element against the pattern in STATE. */
210 ar_glob_match (int desc UNUSED
, char *mem
, int truncated UNUSED
,
211 long int hdrpos UNUSED
, long int datapos UNUSED
,
212 long int size UNUSED
, long int date UNUSED
, int uid UNUSED
,
213 int gid UNUSED
, int mode UNUSED
, struct ar_glob_state
*state
)
215 if (fnmatch (state
->pattern
, mem
, FNM_PATHNAME
|FNM_PERIOD
) == 0)
217 /* We have a match. Add it to the chain. */
218 struct nameseq
*new = (struct nameseq
*) xmalloc (state
->size
);
219 new->name
= concat (state
->arname
, mem
, ")");
220 new->next
= state
->chain
;
228 /* Return nonzero if PATTERN contains any metacharacters.
229 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
231 glob_pattern_p (const char *pattern
, int quote
)
236 for (p
= pattern
; *p
!= '\0'; ++p
)
261 /* Glob for MEMBER_PATTERN in archive ARNAME.
262 Return a malloc'd chain of matching elements (or nil if none). */
265 ar_glob (char *arname
, char *member_pattern
, unsigned int size
)
267 struct ar_glob_state state
;
272 if (! glob_pattern_p (member_pattern
, 1))
275 /* Scan the archive for matches.
276 ar_glob_match will accumulate them in STATE.chain. */
278 state
.arname
= (char *) alloca (i
+ 2);
279 bcopy (arname
, state
.arname
, i
);
280 state
.arname
[i
] = '(';
281 state
.arname
[i
+ 1] = '\0';
282 state
.pattern
= member_pattern
;
286 (void) ar_scan (arname
, ar_glob_match
, (long int) &state
);
288 if (state
.chain
== 0)
291 /* Now put the names into a vector for sorting. */
292 names
= (char **) alloca (state
.n
* sizeof (char *));
294 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
295 names
[i
++] = n
->name
;
297 /* Sort them alphabetically. */
298 qsort ((char *) names
, i
, sizeof (*names
), alpha_compare
);
300 /* Put them back into the chain in the sorted order. */
302 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
303 n
->name
= names
[i
++];
308 #endif /* Not NO_ARCHIVES. */