- Fix Savannah bug #27093
[make.git] / ar.c
blob304ee9c9877d5d5552d0cfb7d26269b6e67893f5
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 Free Software
4 Foundation, Inc.
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 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #ifndef NO_ARCHIVES
23 #include "filedef.h"
24 #include "dep.h"
25 #include <fnmatch.h>
27 /* Return nonzero if NAME is an archive-member reference, zero if not. An
28 archive-member reference is a name like `lib(member)' where member is a
29 non-empty string.
30 If a name like `lib((entry))' is used, a fatal error is signaled at
31 the attempt to use this unsupported feature. */
33 int
34 ar_name (const char *name)
36 const char *p = strchr (name, '(');
37 const char *end;
39 if (p == 0 || p == name)
40 return 0;
42 end = p + strlen (p) - 1;
43 if (*end != ')' || end == p + 1)
44 return 0;
46 if (p[1] == '(' && end[-1] == ')')
47 fatal (NILF, _("attempt to use unsupported feature: `%s'"), name);
49 return 1;
53 /* Parse the archive-member reference NAME into the archive and member names.
54 Creates one allocated string containing both names, pointed to by ARNAME_P.
55 MEMNAME_P points to the member. */
57 void
58 ar_parse_name (const char *name, char **arname_p, char **memname_p)
60 char *p;
62 *arname_p = xstrdup (name);
63 p = strchr (*arname_p, '(');
64 *(p++) = '\0';
65 p[strlen(p) - 1] = '\0';
66 *memname_p = p;
70 /* This function is called by `ar_scan' to find which member to look at. */
72 /* ARGSUSED */
73 static long int
74 ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
75 long int hdrpos UNUSED, long int datapos UNUSED,
76 long int size UNUSED, long int date,
77 int uid UNUSED, int gid UNUSED, int mode UNUSED,
78 const void *name)
80 return ar_name_equal (name, mem, truncated) ? date : 0;
83 /* Return the modtime of NAME. */
85 time_t
86 ar_member_date (const char *name)
88 char *arname;
89 char *memname;
90 long int val;
92 ar_parse_name (name, &arname, &memname);
94 /* Make sure we know the modtime of the archive itself because we are
95 likely to be called just before commands to remake a member are run,
96 and they will change the archive itself.
98 But we must be careful not to enter_file the archive itself if it does
99 not exist, because pattern_search assumes that files found in the data
100 base exist or can be made. */
102 struct file *arfile;
103 arfile = lookup_file (arname);
104 if (arfile == 0 && file_exists_p (arname))
105 arfile = enter_file (strcache_add (arname));
107 if (arfile != 0)
108 (void) f_mtime (arfile, 0);
111 val = ar_scan (arname, ar_member_date_1, memname);
113 free (arname);
115 return (val <= 0 ? (time_t) -1 : (time_t) val);
118 /* Set the archive-member NAME's modtime to now. */
120 #ifdef VMS
122 ar_touch (const char *name)
124 error (NILF, _("touch archive member is not available on VMS"));
125 return -1;
127 #else
129 ar_touch (const char *name)
131 char *arname, *memname;
132 int val;
134 ar_parse_name (name, &arname, &memname);
136 /* Make sure we know the modtime of the archive itself before we
137 touch the member, since this will change the archive modtime. */
139 struct file *arfile;
140 arfile = enter_file (strcache_add (arname));
141 f_mtime (arfile, 0);
144 val = 1;
145 switch (ar_member_touch (arname, memname))
147 case -1:
148 error (NILF, _("touch: Archive `%s' does not exist"), arname);
149 break;
150 case -2:
151 error (NILF, _("touch: `%s' is not a valid archive"), arname);
152 break;
153 case -3:
154 perror_with_name ("touch: ", arname);
155 break;
156 case 1:
157 error (NILF,
158 _("touch: Member `%s' does not exist in `%s'"), memname, arname);
159 break;
160 case 0:
161 val = 0;
162 break;
163 default:
164 error (NILF,
165 _("touch: Bad return code from ar_member_touch on `%s'"), name);
168 free (arname);
170 return val;
172 #endif /* !VMS */
174 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
176 struct ar_glob_state
178 const char *arname;
179 const char *pattern;
180 unsigned int size;
181 struct nameseq *chain;
182 unsigned int n;
185 /* This function is called by `ar_scan' to match one archive
186 element against the pattern in STATE. */
188 static long int
189 ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
190 long int hdrpos UNUSED, long int datapos UNUSED,
191 long int size UNUSED, long int date UNUSED, int uid UNUSED,
192 int gid UNUSED, int mode UNUSED, const void *arg)
194 struct ar_glob_state *state = (struct ar_glob_state *)arg;
196 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
198 /* We have a match. Add it to the chain. */
199 struct nameseq *new = xmalloc (state->size);
200 memset (new, '\0', state->size);
201 new->name = strcache_add (concat (state->arname, mem, ")"));
202 new->next = state->chain;
203 state->chain = new;
204 ++state->n;
207 return 0L;
210 /* Return nonzero if PATTERN contains any metacharacters.
211 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
212 static int
213 glob_pattern_p (const char *pattern, int quote)
215 const char *p;
216 int open = 0;
218 for (p = pattern; *p != '\0'; ++p)
219 switch (*p)
221 case '?':
222 case '*':
223 return 1;
225 case '\\':
226 if (quote)
227 ++p;
228 break;
230 case '[':
231 open = 1;
232 break;
234 case ']':
235 if (open)
236 return 1;
237 break;
240 return 0;
243 /* Glob for MEMBER_PATTERN in archive ARNAME.
244 Return a malloc'd chain of matching elements (or nil if none). */
246 struct nameseq *
247 ar_glob (const char *arname, const char *member_pattern, unsigned int size)
249 struct ar_glob_state state;
250 struct nameseq *n;
251 const char **names;
252 char *name;
253 unsigned int i;
255 if (! glob_pattern_p (member_pattern, 1))
256 return 0;
258 /* Scan the archive for matches.
259 ar_glob_match will accumulate them in STATE.chain. */
260 i = strlen (arname);
261 name = alloca (i + 2);
262 memcpy (name, arname, i);
263 name[i] = '(';
264 name[i + 1] = '\0';
265 state.arname = name;
266 state.pattern = member_pattern;
267 state.size = size;
268 state.chain = 0;
269 state.n = 0;
270 ar_scan (arname, ar_glob_match, &state);
272 if (state.chain == 0)
273 return 0;
275 /* Now put the names into a vector for sorting. */
276 names = alloca (state.n * sizeof (const char *));
277 i = 0;
278 for (n = state.chain; n != 0; n = n->next)
279 names[i++] = n->name;
281 /* Sort them alphabetically. */
282 qsort (names, i, sizeof (*names), alpha_compare);
284 /* Put them back into the chain in the sorted order. */
285 i = 0;
286 for (n = state.chain; n != 0; n = n->next)
287 n->name = names[i++];
289 return state.chain;
292 #endif /* Not NO_ARCHIVES. */