.
[make.git] / ar.c
blob63fcd34f6f0e84d05a1c0959ee9f07df1a76ec70
1 /* Interface to `ar' archives for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
21 #ifndef NO_ARCHIVES
23 #include "file.h"
24 #include "dep.h"
25 #include <fnmatch.h>
27 /* Defined in arscan.c. */
28 extern long int ar_scan ();
29 extern int ar_member_touch ();
30 extern int ar_name_equal ();
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 (name)
40 char *name;
42 char *p = index (name, '('), *end = name + strlen (name) - 1;
44 if (p == 0 || p == name || *end != ')')
45 return 0;
47 if (p[1] == '(' && end[-1] == ')')
48 fatal ("attempt to use unsupported feature: `%s'", name);
50 return 1;
54 /* Parse the archive-member reference NAME into the archive and member names.
55 Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
56 put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil. */
58 void
59 ar_parse_name (name, arname_p, memname_p)
60 char *name, **arname_p, **memname_p;
62 char *p = index (name, '('), *end = name + strlen (name) - 1;
64 if (arname_p != 0)
65 *arname_p = savestring (name, p - name);
67 if (memname_p != 0)
68 *memname_p = savestring (p + 1, end - (p + 1));
71 static long int ar_member_date_1 ();
73 /* Return the modtime of NAME. */
75 time_t
76 ar_member_date (name)
77 char *name;
79 char *arname;
80 int arname_used = 0;
81 char *memname;
82 long int val;
84 ar_parse_name (name, &arname, &memname);
86 /* Make sure we know the modtime of the archive itself because we are
87 likely to be called just before commands to remake a member are run,
88 and they will change the archive itself.
90 But we must be careful not to enter_file the archive itself if it does
91 not exist, because pattern_search assumes that files found in the data
92 base exist or can be made. */
94 struct file *arfile;
95 arfile = lookup_file (arname);
96 if (arfile == 0 && file_exists_p (arname))
98 arfile = enter_file (arname);
99 arname_used = 1;
102 if (arfile != 0)
103 (void) f_mtime (arfile, 0);
106 val = ar_scan (arname, ar_member_date_1, (long int) memname);
108 if (!arname_used)
109 free (arname);
110 free (memname);
112 return (val <= 0 ? (time_t) -1 : (time_t) val);
115 /* This function is called by `ar_scan' to find which member to look at. */
117 /* ARGSUSED */
118 static long int
119 ar_member_date_1 (desc, mem, truncated,
120 hdrpos, datapos, size, date, uid, gid, mode, name)
121 int desc;
122 char *mem;
123 int truncated;
124 long int hdrpos, datapos, size, date;
125 int uid, gid, mode;
126 char *name;
128 return ar_name_equal (name, mem, truncated) ? date : 0;
131 /* Set the archive-member NAME's modtime to now. */
134 ar_touch (name)
135 char *name;
137 char *arname, *memname;
138 int arname_used = 0;
139 register int val;
141 ar_parse_name (name, &arname, &memname);
143 /* Make sure we know the modtime of the archive itself before we
144 touch the member, since this will change the archive itself. */
146 struct file *arfile;
147 arfile = lookup_file (arname);
148 if (arfile == 0)
150 arfile = enter_file (arname);
151 arname_used = 1;
154 (void) f_mtime (arfile, 0);
157 val = 1;
158 switch (ar_member_touch (arname, memname))
160 case -1:
161 error ("touch: Archive `%s' does not exist", arname);
162 break;
163 case -2:
164 error ("touch: `%s' is not a valid archive", arname);
165 break;
166 case -3:
167 perror_with_name ("touch: ", arname);
168 break;
169 case 1:
170 error ("touch: Member `%s' does not exist in `%s'", memname, arname);
171 break;
172 case 0:
173 val = 0;
174 break;
175 default:
176 error ("touch: Bad return code from ar_member_touch on `%s'", name);
179 if (!arname_used)
180 free (arname);
181 free (memname);
183 return val;
186 /* State of an `ar_glob' run, passed to `ar_glob_match'. */
188 struct ar_glob_state
190 char *arname;
191 char *pattern;
192 unsigned int size;
193 struct nameseq *chain;
194 unsigned int n;
197 /* This function is called by `ar_scan' to match one archive
198 element against the pattern in STATE. */
200 static long int
201 ar_glob_match (desc, mem, truncated,
202 hdrpos, datapos, size, date, uid, gid, mode,
203 state)
204 int desc;
205 char *mem;
206 int truncated;
207 long int hdrpos, datapos, size, date;
208 int uid, gid, mode;
209 struct ar_glob_state *state;
211 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
213 /* We have a match. Add it to the chain. */
214 struct nameseq *new = (struct nameseq *) xmalloc (state->size);
215 new->name = concat (state->arname, mem, ")");
216 new->next = state->chain;
217 state->chain = new;
218 ++state->n;
221 return 0L;
224 /* Alphabetic sorting function for `qsort'. */
226 static int
227 ar_glob_alphacompare (a, b)
228 char **a, **b;
230 return strcmp (*a, *b);
233 /* Return nonzero if PATTERN contains any metacharacters.
234 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
235 static int
236 glob_pattern_p (pattern, quote)
237 const char *pattern;
238 const int quote;
240 register const char *p;
241 int open = 0;
243 for (p = pattern; *p != '\0'; ++p)
244 switch (*p)
246 case '?':
247 case '*':
248 return 1;
250 case '\\':
251 if (quote)
252 ++p;
253 break;
255 case '[':
256 open = 1;
257 break;
259 case ']':
260 if (open)
261 return 1;
262 break;
265 return 0;
268 /* Glob for MEMBER_PATTERN in archive ARNAME.
269 Return a malloc'd chain of matching elements (or nil if none). */
271 struct nameseq *
272 ar_glob (arname, member_pattern, size)
273 char *arname, *member_pattern;
274 unsigned int size;
276 struct ar_glob_state state;
277 char **names;
278 struct nameseq *n;
279 unsigned int i;
281 if (! glob_pattern_p (member_pattern, 1))
282 return 0;
284 /* Scan the archive for matches.
285 ar_glob_match will accumulate them in STATE.chain. */
286 i = strlen (arname);
287 state.arname = (char *) alloca (i + 2);
288 bcopy (arname, state.arname, i);
289 state.arname[i] = '(';
290 state.arname[i + 1] = '\0';
291 state.pattern = member_pattern;
292 state.size = size;
293 state.chain = 0;
294 state.n = 0;
295 (void) ar_scan (arname, ar_glob_match, (long int) &state);
297 if (state.chain == 0)
298 return 0;
300 /* Now put the names into a vector for sorting. */
301 names = (char **) alloca (state.n * sizeof (char *));
302 i = 0;
303 for (n = state.chain; n != 0; n = n->next)
304 names[i++] = n->name;
306 /* Sort them alphabetically. */
307 qsort ((char *) names, i, sizeof (*names), ar_glob_alphacompare);
309 /* Put them back into the chain in the sorted order. */
310 i = 0;
311 for (n = state.chain; n != 0; n = n->next)
312 n->name = names[i++];
314 return state.chain;
317 #endif /* Not NO_ARCHIVES. */