Fix an issue with PATH_SEPARATOR_CHAR when cross-compiling for Windows.
[make.git] / ar.c
blob5eda266ac29cf7013149ffcd97775bd2d94cc3ef
1 /* Interface to `ar' archives for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 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
11 version.
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/>. */
20 #include "make.h"
22 #ifndef NO_ARCHIVES
24 #include "filedef.h"
25 #include "dep.h"
26 #include <fnmatch.h>
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
30 non-empty string.
31 If a name like `lib((entry))' is used, a fatal error is signaled at
32 the attempt to use this unsupported feature. */
34 int
35 ar_name (const char *name)
37 const char *p = strchr (name, '(');
38 const char *end;
40 if (p == 0 || p == name)
41 return 0;
43 end = p + strlen (p) - 1;
44 if (*end != ')' || end == p + 1)
45 return 0;
47 if (p[1] == '(' && end[-1] == ')')
48 fatal (NILF, _("attempt to use unsupported feature: `%s'"), name);
50 return 1;
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. */
58 void
59 ar_parse_name (const char *name, char **arname_p, char **memname_p)
61 char *p;
63 *arname_p = xstrdup (name);
64 p = strchr (*arname_p, '(');
65 *(p++) = '\0';
66 p[strlen(p) - 1] = '\0';
67 *memname_p = p;
71 /* This function is called by `ar_scan' to find which member to look at. */
73 /* ARGSUSED */
74 static long int
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,
79 const void *name)
81 return ar_name_equal (name, mem, truncated) ? date : 0;
84 /* Return the modtime of NAME. */
86 time_t
87 ar_member_date (const char *name)
89 char *arname;
90 char *memname;
91 long int val;
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. */
103 struct file *arfile;
104 arfile = lookup_file (arname);
105 if (arfile == 0 && file_exists_p (arname))
106 arfile = enter_file (strcache_add (arname));
108 if (arfile != 0)
109 (void) f_mtime (arfile, 0);
112 val = ar_scan (arname, ar_member_date_1, memname);
114 free (arname);
116 return (val <= 0 ? (time_t) -1 : (time_t) val);
119 /* Set the archive-member NAME's modtime to now. */
121 #ifdef VMS
123 ar_touch (const char *name)
125 error (NILF, _("touch archive member is not available on VMS"));
126 return -1;
128 #else
130 ar_touch (const char *name)
132 char *arname, *memname;
133 int val;
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. */
140 struct file *arfile;
141 arfile = enter_file (strcache_add (arname));
142 f_mtime (arfile, 0);
145 val = 1;
146 switch (ar_member_touch (arname, memname))
148 case -1:
149 error (NILF, _("touch: Archive `%s' does not exist"), arname);
150 break;
151 case -2:
152 error (NILF, _("touch: `%s' is not a valid archive"), arname);
153 break;
154 case -3:
155 perror_with_name ("touch: ", arname);
156 break;
157 case 1:
158 error (NILF,
159 _("touch: Member `%s' does not exist in `%s'"), memname, arname);
160 break;
161 case 0:
162 val = 0;
163 break;
164 default:
165 error (NILF,
166 _("touch: Bad return code from ar_member_touch on `%s'"), name);
169 free (arname);
171 return val;
173 #endif /* !VMS */
175 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
177 struct ar_glob_state
179 const char *arname;
180 const char *pattern;
181 unsigned int size;
182 struct nameseq *chain;
183 unsigned int n;
186 /* This function is called by `ar_scan' to match one archive
187 element against the pattern in STATE. */
189 static long int
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;
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 opened = 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 opened = 1;
232 break;
234 case ']':
235 if (opened)
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 unsigned int i;
254 if (! glob_pattern_p (member_pattern, 1))
255 return 0;
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;
261 state.size = size;
262 state.chain = 0;
263 state.n = 0;
264 ar_scan (arname, ar_glob_match, &state);
266 if (state.chain == 0)
267 return 0;
269 /* Now put the names into a vector for sorting. */
270 names = alloca (state.n * sizeof (const char *));
271 i = 0;
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. */
280 i = 0;
281 for (n = state.chain; n != 0; n = n->next)
282 n->name = names[i++];
284 return state.chain;
287 #endif /* Not NO_ARCHIVES. */