1 /* Interface to `ar' archives for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,97 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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 /* Defined in arscan.c. */
29 extern long int ar_scan
PARAMS ((char *archive
, long int (*function
) (), long int arg
));
30 extern int ar_name_equal
PARAMS ((char *name
, char *mem
, int truncated
));
32 extern int ar_member_touch
PARAMS ((char *arname
, char *memname
));
35 /* Return nonzero if NAME is an archive-member reference, zero if not.
36 An archive-member reference is a name like `lib(member)'.
37 If a name like `lib((entry))' is used, a fatal error is signaled at
38 the attempt to use this unsupported feature. */
44 char *p
= index (name
, '('), *end
= name
+ strlen (name
) - 1;
46 if (p
== 0 || p
== name
|| *end
!= ')')
49 if (p
[1] == '(' && end
[-1] == ')')
50 fatal (NILF
, "attempt to use unsupported feature: `%s'", name
);
56 /* Parse the archive-member reference NAME into the archive and member names.
57 Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
58 put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil. */
61 ar_parse_name (name
, arname_p
, memname_p
)
62 char *name
, **arname_p
, **memname_p
;
64 char *p
= index (name
, '('), *end
= name
+ strlen (name
) - 1;
67 *arname_p
= savestring (name
, p
- name
);
70 *memname_p
= savestring (p
+ 1, end
- (p
+ 1));
73 static long int ar_member_date_1
PARAMS ((int desc
, char *mem
, int truncated
, long int hdrpos
,
74 long int datapos
, long int size
, long int date
, int uid
, int gid
, int mode
, char *name
));
76 /* Return the modtime of 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 (desc
, mem
, truncated
,
123 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
, name
)
127 long int hdrpos
, datapos
, size
, date
;
131 return ar_name_equal (name
, mem
, truncated
) ? date
: 0;
134 /* Set the archive-member NAME's modtime to now. */
141 error (NILF
, "touch archive member is not available on VMS");
149 char *arname
, *memname
;
153 ar_parse_name (name
, &arname
, &memname
);
155 /* Make sure we know the modtime of the archive itself before we
156 touch the member, since this will change the archive itself. */
159 arfile
= lookup_file (arname
);
162 arfile
= enter_file (arname
);
166 (void) f_mtime (arfile
, 0);
170 switch (ar_member_touch (arname
, memname
))
173 error (NILF
, "touch: Archive `%s' does not exist", arname
);
176 error (NILF
, "touch: `%s' is not a valid archive", arname
);
179 perror_with_name ("touch: ", arname
);
183 "touch: Member `%s' does not exist in `%s'", memname
, arname
);
190 "touch: Bad return code from ar_member_touch on `%s'", name
);
201 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
208 struct nameseq
*chain
;
212 /* This function is called by `ar_scan' to match one archive
213 element against the pattern in STATE. */
216 ar_glob_match (desc
, mem
, truncated
,
217 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
,
222 long int hdrpos
, datapos
, size
, date
;
224 struct ar_glob_state
*state
;
226 if (fnmatch (state
->pattern
, mem
, FNM_PATHNAME
|FNM_PERIOD
) == 0)
228 /* We have a match. Add it to the chain. */
229 struct nameseq
*new = (struct nameseq
*) xmalloc (state
->size
);
230 new->name
= concat (state
->arname
, mem
, ")");
231 new->next
= state
->chain
;
239 /* Return nonzero if PATTERN contains any metacharacters.
240 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
242 glob_pattern_p (pattern
, quote
)
246 register const char *p
;
249 for (p
= pattern
; *p
!= '\0'; ++p
)
274 /* Glob for MEMBER_PATTERN in archive ARNAME.
275 Return a malloc'd chain of matching elements (or nil if none). */
278 ar_glob (arname
, member_pattern
, size
)
279 char *arname
, *member_pattern
;
282 struct ar_glob_state state
;
287 if (! glob_pattern_p (member_pattern
, 1))
290 /* Scan the archive for matches.
291 ar_glob_match will accumulate them in STATE.chain. */
293 state
.arname
= (char *) alloca (i
+ 2);
294 bcopy (arname
, state
.arname
, i
);
295 state
.arname
[i
] = '(';
296 state
.arname
[i
+ 1] = '\0';
297 state
.pattern
= member_pattern
;
301 (void) ar_scan (arname
, ar_glob_match
, (long int) &state
);
303 if (state
.chain
== 0)
306 /* Now put the names into a vector for sorting. */
307 names
= (char **) alloca (state
.n
* sizeof (char *));
309 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
310 names
[i
++] = n
->name
;
312 /* Sort them alphabetically. */
313 qsort ((char *) names
, i
, sizeof (*names
), alpha_compare
);
315 /* Put them back into the chain in the sorted order. */
317 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
318 n
->name
= names
[i
++];
323 #endif /* Not NO_ARCHIVES. */