1 /* Interface to `ar' archives for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 This file is part of GNU Make.
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)
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.
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. */
27 /* Defined in arscan.c. */
28 extern long int ar_scan ();
29 extern int ar_member_touch ();
30 extern int ar_name_equal ();
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. */
42 char *p
= index (name
, '('), *end
= name
+ strlen (name
) - 1;
44 if (p
== 0 || p
== name
|| *end
!= ')')
47 if (p
[1] == '(' && end
[-1] == ')')
48 fatal ("attempt to use unsupported feature: `%s'", name
);
54 /* Parse the archive-member reference NAME into the archive and member names.
55 Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
56 put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil. */
59 ar_parse_name (name
, arname_p
, memname_p
)
60 char *name
, **arname_p
, **memname_p
;
62 char *p
= index (name
, '('), *end
= name
+ strlen (name
) - 1;
65 *arname_p
= savestring (name
, p
- name
);
68 *memname_p
= savestring (p
+ 1, end
- (p
+ 1));
71 static long int ar_member_date_1 ();
73 /* Return the modtime of NAME. */
84 ar_parse_name (name
, &arname
, &memname
);
86 /* Make sure we know the modtime of the archive itself because we are
87 likely to be called just before commands to remake a member are run,
88 and they will change the archive itself.
90 But we must be careful not to enter_file the archive itself if it does
91 not exist, because pattern_search assumes that files found in the data
92 base exist or can be made. */
95 arfile
= lookup_file (arname
);
96 if (arfile
== 0 && file_exists_p (arname
))
98 arfile
= enter_file (arname
);
103 (void) f_mtime (arfile
, 0);
106 val
= ar_scan (arname
, ar_member_date_1
, (long int) memname
);
112 return (val
<= 0 ? (time_t) -1 : (time_t) val
);
115 /* This function is called by `ar_scan' to find which member to look at. */
119 ar_member_date_1 (desc
, mem
, truncated
,
120 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
, name
)
124 long int hdrpos
, datapos
, size
, date
;
128 return ar_name_equal (name
, mem
, truncated
) ? date
: 0;
131 /* Set the archive-member NAME's modtime to now. */
137 char *arname
, *memname
;
141 ar_parse_name (name
, &arname
, &memname
);
143 /* Make sure we know the modtime of the archive itself before we
144 touch the member, since this will change the archive itself. */
147 arfile
= lookup_file (arname
);
150 arfile
= enter_file (arname
);
154 (void) f_mtime (arfile
, 0);
158 switch (ar_member_touch (arname
, memname
))
161 error ("touch: Archive `%s' does not exist", arname
);
164 error ("touch: `%s' is not a valid archive", arname
);
167 perror_with_name ("touch: ", arname
);
170 error ("touch: Member `%s' does not exist in `%s'", memname
, arname
);
176 error ("touch: Bad return code from ar_member_touch on `%s'", name
);
186 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
193 struct nameseq
*chain
;
197 /* This function is called by `ar_scan' to match one archive
198 element against the pattern in STATE. */
201 ar_glob_match (desc
, mem
, truncated
,
202 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
,
207 long int hdrpos
, datapos
, size
, date
;
209 struct ar_glob_state
*state
;
211 if (fnmatch (state
->pattern
, mem
, FNM_PATHNAME
|FNM_PERIOD
) == 0)
213 /* We have a match. Add it to the chain. */
214 struct nameseq
*new = (struct nameseq
*) xmalloc (state
->size
);
215 new->name
= concat (state
->arname
, mem
, ")");
216 new->next
= state
->chain
;
224 /* Alphabetic sorting function for `qsort'. */
227 ar_glob_alphacompare (a
, b
)
230 return strcmp (*a
, *b
);
233 /* Return nonzero if PATTERN contains any metacharacters.
234 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
236 glob_pattern_p (pattern
, quote
)
240 register const char *p
;
243 for (p
= pattern
; *p
!= '\0'; ++p
)
268 /* Glob for MEMBER_PATTERN in archive ARNAME.
269 Return a malloc'd chain of matching elements (or nil if none). */
272 ar_glob (arname
, member_pattern
, size
)
273 char *arname
, *member_pattern
;
276 struct ar_glob_state state
;
281 if (! glob_pattern_p (member_pattern
, 1))
284 /* Scan the archive for matches.
285 ar_glob_match will accumulate them in STATE.chain. */
287 state
.arname
= (char *) alloca (i
+ 2);
288 bcopy (arname
, state
.arname
, i
);
289 state
.arname
[i
] = '(';
290 state
.arname
[i
+ 1] = '\0';
291 state
.pattern
= member_pattern
;
295 (void) ar_scan (arname
, ar_glob_match
, (long int) &state
);
297 if (state
.chain
== 0)
300 /* Now put the names into a vector for sorting. */
301 names
= (char **) alloca (state
.n
* sizeof (char *));
303 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
304 names
[i
++] = n
->name
;
306 /* Sort them alphabetically. */
307 qsort ((char *) names
, i
, sizeof (*names
), ar_glob_alphacompare
);
309 /* Put them back into the chain in the sorted order. */
311 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
312 n
->name
= names
[i
++];
317 #endif /* Not NO_ARCHIVES. */