Update copyright and license notices on all files.
[make.git] / ar.c
blob744e1aac988b4fff2c21b73c19e50a57fa1f71f1
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, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18 #include "make.h"
20 #ifndef NO_ARCHIVES
22 #include "filedef.h"
23 #include "dep.h"
24 #include <fnmatch.h>
26 /* Defined in arscan.c. */
27 extern long int ar_scan PARAMS ((char *archive, long int (*function) (), long int arg));
28 extern int ar_name_equal PARAMS ((char *name, char *mem, int truncated));
29 #ifndef VMS
30 extern int ar_member_touch PARAMS ((char *arname, char *memname));
31 #endif
33 /* Return nonzero if NAME is an archive-member reference, zero if not.
34 An archive-member reference is a name like `lib(member)'.
35 If a name like `lib((entry))' is used, a fatal error is signaled at
36 the attempt to use this unsupported feature. */
38 int
39 ar_name (char *name)
41 char *p = strchr (name, '(');
42 char *end;
44 if (p == 0 || p == name)
45 return 0;
47 end = p + strlen (p) - 1;
48 if (*end != ')')
49 return 0;
51 if (p[1] == '(' && end[-1] == ')')
52 fatal (NILF, _("attempt to use unsupported feature: `%s'"), name);
54 return 1;
58 /* Parse the archive-member reference NAME into the archive and member names.
59 Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
60 put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil. */
62 void
63 ar_parse_name (char *name, char **arname_p, char **memname_p)
65 char *p = strchr (name, '('), *end = name + strlen (name) - 1;
67 if (arname_p != 0)
68 *arname_p = savestring (name, p - name);
70 if (memname_p != 0)
71 *memname_p = savestring (p + 1, end - (p + 1));
74 static long int ar_member_date_1 PARAMS ((int desc, char *mem, int truncated, long int hdrpos,
75 long int datapos, long int size, long int date, int uid, int gid, int mode, char *name));
77 /* Return the modtime of NAME. */
79 time_t
80 ar_member_date (char *name)
82 char *arname;
83 int arname_used = 0;
84 char *memname;
85 long int val;
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. */
97 struct file *arfile;
98 arfile = lookup_file (arname);
99 if (arfile == 0 && file_exists_p (arname))
101 arfile = enter_file (arname);
102 arname_used = 1;
105 if (arfile != 0)
106 (void) f_mtime (arfile, 0);
109 val = ar_scan (arname, ar_member_date_1, (long int) memname);
111 if (!arname_used)
112 free (arname);
113 free (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. */
120 /* ARGSUSED */
121 static long int
122 ar_member_date_1 (int desc UNUSED, char *mem, int truncated,
123 long int hdrpos UNUSED, long int datapos UNUSED,
124 long int size UNUSED, long int date,
125 int uid UNUSED, int gid UNUSED, int mode UNUSED, char *name)
127 return ar_name_equal (name, mem, truncated) ? date : 0;
130 /* Set the archive-member NAME's modtime to now. */
132 #ifdef VMS
134 ar_touch (char *name)
136 error (NILF, _("touch archive member is not available on VMS"));
137 return -1;
139 #else
141 ar_touch (char *name)
143 char *arname, *memname;
144 int arname_used = 0;
145 register int val;
147 ar_parse_name (name, &arname, &memname);
149 /* Make sure we know the modtime of the archive itself before we
150 touch the member, since this will change the archive itself. */
152 struct file *arfile;
153 arfile = lookup_file (arname);
154 if (arfile == 0)
156 arfile = enter_file (arname);
157 arname_used = 1;
160 (void) f_mtime (arfile, 0);
163 val = 1;
164 switch (ar_member_touch (arname, memname))
166 case -1:
167 error (NILF, _("touch: Archive `%s' does not exist"), arname);
168 break;
169 case -2:
170 error (NILF, _("touch: `%s' is not a valid archive"), arname);
171 break;
172 case -3:
173 perror_with_name ("touch: ", arname);
174 break;
175 case 1:
176 error (NILF,
177 _("touch: Member `%s' does not exist in `%s'"), memname, arname);
178 break;
179 case 0:
180 val = 0;
181 break;
182 default:
183 error (NILF,
184 _("touch: Bad return code from ar_member_touch on `%s'"), name);
187 if (!arname_used)
188 free (arname);
189 free (memname);
191 return val;
193 #endif /* !VMS */
195 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
197 struct ar_glob_state
199 char *arname;
200 char *pattern;
201 unsigned int size;
202 struct nameseq *chain;
203 unsigned int n;
206 /* This function is called by `ar_scan' to match one archive
207 element against the pattern in STATE. */
209 static long int
210 ar_glob_match (int desc UNUSED, char *mem, int truncated UNUSED,
211 long int hdrpos UNUSED, long int datapos UNUSED,
212 long int size UNUSED, long int date UNUSED, int uid UNUSED,
213 int gid UNUSED, int mode UNUSED, struct ar_glob_state *state)
215 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
217 /* We have a match. Add it to the chain. */
218 struct nameseq *new = (struct nameseq *) xmalloc (state->size);
219 new->name = concat (state->arname, mem, ")");
220 new->next = state->chain;
221 state->chain = new;
222 ++state->n;
225 return 0L;
228 /* Return nonzero if PATTERN contains any metacharacters.
229 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
230 static int
231 glob_pattern_p (const char *pattern, int quote)
233 const char *p;
234 int open = 0;
236 for (p = pattern; *p != '\0'; ++p)
237 switch (*p)
239 case '?':
240 case '*':
241 return 1;
243 case '\\':
244 if (quote)
245 ++p;
246 break;
248 case '[':
249 open = 1;
250 break;
252 case ']':
253 if (open)
254 return 1;
255 break;
258 return 0;
261 /* Glob for MEMBER_PATTERN in archive ARNAME.
262 Return a malloc'd chain of matching elements (or nil if none). */
264 struct nameseq *
265 ar_glob (char *arname, char *member_pattern, unsigned int size)
267 struct ar_glob_state state;
268 char **names;
269 struct nameseq *n;
270 unsigned int i;
272 if (! glob_pattern_p (member_pattern, 1))
273 return 0;
275 /* Scan the archive for matches.
276 ar_glob_match will accumulate them in STATE.chain. */
277 i = strlen (arname);
278 state.arname = (char *) alloca (i + 2);
279 bcopy (arname, state.arname, i);
280 state.arname[i] = '(';
281 state.arname[i + 1] = '\0';
282 state.pattern = member_pattern;
283 state.size = size;
284 state.chain = 0;
285 state.n = 0;
286 (void) ar_scan (arname, ar_glob_match, (long int) &state);
288 if (state.chain == 0)
289 return 0;
291 /* Now put the names into a vector for sorting. */
292 names = (char **) alloca (state.n * sizeof (char *));
293 i = 0;
294 for (n = state.chain; n != 0; n = n->next)
295 names[i++] = n->name;
297 /* Sort them alphabetically. */
298 qsort ((char *) names, i, sizeof (*names), alpha_compare);
300 /* Put them back into the chain in the sorted order. */
301 i = 0;
302 for (n = state.chain; n != 0; n = n->next)
303 n->name = names[i++];
305 return state.chain;
308 #endif /* Not NO_ARCHIVES. */