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, 2004, 2005, 2006 Free Software
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
27 /* Return nonzero if NAME is an archive-member reference, zero if not.
28 An archive-member reference is a name like `lib(member)'.
29 If a name like `lib((entry))' is used, a fatal error is signaled at
30 the attempt to use this unsupported feature. */
33 ar_name (const char *name
)
35 const char *p
= strchr (name
, '(');
38 if (p
== 0 || p
== name
)
41 end
= p
+ strlen (p
) - 1;
45 if (p
[1] == '(' && end
[-1] == ')')
46 fatal (NILF
, _("attempt to use unsupported feature: `%s'"), name
);
52 /* Parse the archive-member reference NAME into the archive and member names.
53 Creates one allocated string containing both names, pointed to by ARNAME_P.
54 MEMNAME_P points to the member. */
57 ar_parse_name (const char *name
, char **arname_p
, char **memname_p
)
61 *arname_p
= xstrdup (name
);
62 p
= strchr (*arname_p
, '(');
64 p
[strlen(p
) - 1] = '\0';
69 /* This function is called by `ar_scan' to find which member to look at. */
73 ar_member_date_1 (int desc UNUSED
, const char *mem
, int truncated
,
74 long int hdrpos UNUSED
, long int datapos UNUSED
,
75 long int size UNUSED
, long int date
,
76 int uid UNUSED
, int gid UNUSED
, int mode UNUSED
,
79 return ar_name_equal (name
, mem
, truncated
) ? date
: 0;
82 /* Return the modtime of NAME. */
85 ar_member_date (const char *name
)
91 ar_parse_name (name
, &arname
, &memname
);
93 /* Make sure we know the modtime of the archive itself because we are
94 likely to be called just before commands to remake a member are run,
95 and they will change the archive itself.
97 But we must be careful not to enter_file the archive itself if it does
98 not exist, because pattern_search assumes that files found in the data
99 base exist or can be made. */
102 arfile
= lookup_file (arname
);
103 if (arfile
== 0 && file_exists_p (arname
))
104 arfile
= enter_file (strcache_add (arname
));
107 (void) f_mtime (arfile
, 0);
110 val
= ar_scan (arname
, ar_member_date_1
, memname
);
114 return (val
<= 0 ? (time_t) -1 : (time_t) val
);
117 /* Set the archive-member NAME's modtime to now. */
121 ar_touch (const char *name
)
123 error (NILF
, _("touch archive member is not available on VMS"));
128 ar_touch (const char *name
)
130 char *arname
, *memname
;
133 ar_parse_name (name
, &arname
, &memname
);
135 /* Make sure we know the modtime of the archive itself before we
136 touch the member, since this will change the archive modtime. */
139 arfile
= enter_file (strcache_add (arname
));
144 switch (ar_member_touch (arname
, memname
))
147 error (NILF
, _("touch: Archive `%s' does not exist"), arname
);
150 error (NILF
, _("touch: `%s' is not a valid archive"), arname
);
153 perror_with_name ("touch: ", arname
);
157 _("touch: Member `%s' does not exist in `%s'"), memname
, arname
);
164 _("touch: Bad return code from ar_member_touch on `%s'"), name
);
173 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
180 struct nameseq
*chain
;
184 /* This function is called by `ar_scan' to match one archive
185 element against the pattern in STATE. */
188 ar_glob_match (int desc UNUSED
, const char *mem
, int truncated UNUSED
,
189 long int hdrpos UNUSED
, long int datapos UNUSED
,
190 long int size UNUSED
, long int date UNUSED
, int uid UNUSED
,
191 int gid UNUSED
, int mode UNUSED
, const void *arg
)
193 struct ar_glob_state
*state
= (struct ar_glob_state
*)arg
;
195 if (fnmatch (state
->pattern
, mem
, FNM_PATHNAME
|FNM_PERIOD
) == 0)
197 /* We have a match. Add it to the chain. */
198 struct nameseq
*new = xmalloc (state
->size
);
199 new->name
= strcache_add (concat (state
->arname
, mem
, ")"));
200 new->next
= state
->chain
;
208 /* Return nonzero if PATTERN contains any metacharacters.
209 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
211 glob_pattern_p (const char *pattern
, int quote
)
216 for (p
= pattern
; *p
!= '\0'; ++p
)
241 /* Glob for MEMBER_PATTERN in archive ARNAME.
242 Return a malloc'd chain of matching elements (or nil if none). */
245 ar_glob (const char *arname
, const char *member_pattern
, unsigned int size
)
247 struct ar_glob_state state
;
253 if (! glob_pattern_p (member_pattern
, 1))
256 /* Scan the archive for matches.
257 ar_glob_match will accumulate them in STATE.chain. */
259 name
= alloca (i
+ 2);
260 memcpy (name
, arname
, i
);
264 state
.pattern
= member_pattern
;
268 ar_scan (arname
, ar_glob_match
, &state
);
270 if (state
.chain
== 0)
273 /* Now put the names into a vector for sorting. */
274 names
= alloca (state
.n
* sizeof (const char *));
276 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
277 names
[i
++] = n
->name
;
279 /* Sort them alphabetically. */
280 qsort (names
, i
, sizeof (*names
), alpha_compare
);
282 /* Put them back into the chain in the sorted order. */
284 for (n
= state
.chain
; n
!= 0; n
= n
->next
)
285 n
->name
= names
[i
++];
290 #endif /* Not NO_ARCHIVES. */