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, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
6 This file is part of GNU Make.
8 GNU Make is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3 of the License, or (at your option) any later
13 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>. */
28 /* Return nonzero if NAME is an archive-member reference, zero if not. An
29 archive-member reference is a name like `lib(member)' where member is a
31 If a name like `lib((entry))' is used, a fatal error is signaled at
32 the attempt to use this unsupported feature. */
35 ar_name (const char *name)
37 const char *p = strchr (name, '(');
40 if (p == 0 || p == name)
43 end = p + strlen (p) - 1;
44 if (*end != ')' || end == p + 1)
47 if (p[1] == '(' && end[-1] == ')')
48 fatal (NILF, _("attempt to use unsupported feature: `%s'"), name);
54 /* Parse the archive-member reference NAME into the archive and member names.
55 Creates one allocated string containing both names, pointed to by ARNAME_P.
56 MEMNAME_P points to the member. */
59 ar_parse_name (const char *name, char **arname_p, char **memname_p)
63 *arname_p = xstrdup (name);
64 p = strchr (*arname_p, '(');
66 p[strlen(p) - 1] = '\0';
71 /* This function is called by `ar_scan' to find which member to look at. */
75 ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
76 long int hdrpos UNUSED, long int datapos UNUSED,
77 long int size UNUSED, long int date,
78 int uid UNUSED, int gid UNUSED, int mode UNUSED,
81 return ar_name_equal (name, mem, truncated) ? date : 0;
84 /* Return the modtime of NAME. */
87 ar_member_date (const char *name)
93 ar_parse_name (name, &arname, &memname);
95 /* Make sure we know the modtime of the archive itself because we are
96 likely to be called just before commands to remake a member are run,
97 and they will change the archive itself.
99 But we must be careful not to enter_file the archive itself if it does
100 not exist, because pattern_search assumes that files found in the data
101 base exist or can be made. */
104 arfile = lookup_file (arname);
105 if (arfile == 0 && file_exists_p (arname))
106 arfile = enter_file (strcache_add (arname));
109 (void) f_mtime (arfile, 0);
112 val = ar_scan (arname, ar_member_date_1, memname);
116 return (val <= 0 ? (time_t) -1 : (time_t) val);
119 /* Set the archive-member NAME's modtime to now. */
123 ar_touch (const char *name)
125 error (NILF, _("touch archive member is not available on VMS"));
130 ar_touch (const char *name)
132 char *arname, *memname;
135 ar_parse_name (name, &arname, &memname);
137 /* Make sure we know the modtime of the archive itself before we
138 touch the member, since this will change the archive modtime. */
141 arfile = enter_file (strcache_add (arname));
146 switch (ar_member_touch (arname, memname))
149 error (NILF, _("touch: Archive `%s' does not exist"), arname);
152 error (NILF, _("touch: `%s' is not a valid archive"), arname);
155 perror_with_name ("touch: ", arname);
159 _("touch: Member `%s' does not exist in `%s'"), memname, arname);
166 _("touch: Bad return code from ar_member_touch on `%s'"), name);
175 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
182 struct nameseq *chain;
186 /* This function is called by `ar_scan' to match one archive
187 element against the pattern in STATE. */
190 ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
191 long int hdrpos UNUSED, long int datapos UNUSED,
192 long int size UNUSED, long int date UNUSED, int uid UNUSED,
193 int gid UNUSED, int mode UNUSED, const void *arg)
195 struct ar_glob_state *state = (struct ar_glob_state *)arg;
197 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
199 /* We have a match. Add it to the chain. */
200 struct nameseq *new = xcalloc (state->size);
201 new->name = strcache_add (concat (4, state->arname, "(", mem, ")"));
202 new->next = state->chain;
210 /* Return nonzero if PATTERN contains any metacharacters.
211 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
213 glob_pattern_p (const char *pattern, int quote)
218 for (p = pattern; *p != '\0'; ++p)
243 /* Glob for MEMBER_PATTERN in archive ARNAME.
244 Return a malloc'd chain of matching elements (or nil if none). */
247 ar_glob (const char *arname, const char *member_pattern, unsigned int size)
249 struct ar_glob_state state;
254 if (! glob_pattern_p (member_pattern, 1))
257 /* Scan the archive for matches.
258 ar_glob_match will accumulate them in STATE.chain. */
259 state.arname = arname;
260 state.pattern = member_pattern;
264 ar_scan (arname, ar_glob_match, &state);
266 if (state.chain == 0)
269 /* Now put the names into a vector for sorting. */
270 names = alloca (state.n * sizeof (const char *));
272 for (n = state.chain; n != 0; n = n->next)
273 names[i++] = n->name;
275 /* Sort them alphabetically. */
276 /* MSVC erroneously warns without a cast here. */
277 qsort ((void *)names, i, sizeof (*names), alpha_compare);
279 /* Put them back into the chain in the sorted order. */
281 for (n = state.chain; n != 0; n = n->next)
282 n->name = names[i++];
287 #endif /* Not NO_ARCHIVES. */