Debian Codegraph @

src/ar.c

raw symbols SCIP
1 /* Interface to 'ar' archives for GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3
4 This file is part of GNU Make.
5
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 3 of the License, or (at your option) any later
9 version.
10
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "makeint.h"
19
20 #ifndef NO_ARCHIVES
21
22 #include "filedef.h"
23 #include "dep.h"
24 #include <fnmatch.h>
25
26 /* Return nonzero if NAME is an archive-member reference, zero if not. An
27 archive-member reference is a name like 'lib(member)' where member is a
28 non-empty string.
29 If a name like 'lib((entry))' is used, a fatal error is signaled at
30 the attempt to use this unsupported feature. */
31
32 int
33 ar_name (const char *name)
34 {
35 const char *p = strchr (name, '(');
36 const char *end;
37
38 if (p == 0 || p == name)
39 return 0;
40
41 end = p + strlen (p) - 1;
42 if (*end != ')' || end == p + 1)
43 return 0;
44
45 if (p[1] == '(' && end[-1] == ')')
46 OS (fatal, NILF, _("attempt to use unsupported feature: '%s'"), name);
47
48 return 1;
49 }
50
51
52 /* Parse the archive-member reference NAME into the archive and member names.
53 Creates one allocated string containing both names, pointed to by ARNAME_P.
54 MEMNAME_P points to the member. */
55
56 void
57 ar_parse_name (const char *name, char **arname_p, char **memname_p)
58 {
59 char *p;
60
61 *arname_p = xstrdup (name);
62 p = strchr (*arname_p, '(');
63 *(p++) = '\0';
64 p[strlen (p) - 1] = '\0';
65 *memname_p = p;
66 }
67
68
69 /* This function is called by 'ar_scan' to find which member to look at. */
70
71 struct member_date_lookup
72 {
73 const char *name;
74 time_t *member_date;
75 };
76
77 /* ARGSUSED */
78 static long int
79 ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
80 long int hdrpos UNUSED, long int datapos UNUSED,
81 long int size UNUSED, long int date,
82 int uid UNUSED, int gid UNUSED, unsigned int mode UNUSED,
83 const void *data)
84 {
85 const struct member_date_lookup *lookup_data = data;
86 if (ar_name_equal (lookup_data->name, mem, truncated))
87 {
88 *lookup_data->member_date = date;
89 return 1;
90 }
91 return 0;
92 }
93
94 /* Read the modtime of NAME in MEMBER_DATE.
95 Returns 1 if NAME exists, 0 otherwise. */
96 int
97 ar_member_date (const char *name, time_t *member_date)
98 {
99 char *arname;
100 char *memname;
101 int found;
102 struct member_date_lookup lookup_data;
103
104 ar_parse_name (name, &arname, &memname);
105
106 /* Make sure we know the modtime of the archive itself because we are
107 likely to be called just before commands to remake a member are run,
108 and they will change the archive itself.
109
110 But we must be careful not to enter_file the archive itself if it does
111 not exist, because pattern_search assumes that files found in the data
112 base exist or can be made. */
113 {
114 struct file *arfile;
115 arfile = lookup_file (arname);
116 if (arfile == 0 && file_exists_p (arname))
117 arfile = enter_file (strcache_add (arname));
118
119 if (arfile != 0)
120 (void) f_mtime (arfile, 0);
121 }
122
123 lookup_data.name = memname;
124 lookup_data.member_date = member_date;
125 found = ar_scan (arname, ar_member_date_1, &lookup_data);
126
127 free (arname);
128
129 /* return 0 (not found) if the archive does not exist or has invalid format. */
130 return (found == 1) ? 1 : 0;
131 }
132
133 /* Set the archive-member NAME's modtime to now. */
134
135 int
136 ar_touch (const char *name)
137 {
138 char *arname, *memname;
139 int val;
140
141 ar_parse_name (name, &arname, &memname);
142
143 /* Make sure we know the modtime of the archive itself before we
144 touch the member, since this will change the archive modtime. */
145 {
146 struct file *arfile;
147 arfile = enter_file (strcache_add (arname));
148 f_mtime (arfile, 0);
149 }
150
151 val = 1;
152 switch (ar_member_touch (arname, memname))
153 {
154 case -1:
155 OS (error, NILF, _("touch: Archive '%s' does not exist"), arname);
156 break;
157 case -2:
158 OS (error, NILF, _("touch: '%s' is not a valid archive"), arname);
159 break;
160 case -3:
161 perror_with_name ("touch: ", arname);
162 break;
163 case 1:
164 OSS (error, NILF,
165 _("touch: Member '%s' does not exist in '%s'"), memname, arname);
166 break;
167 case 0:
168 val = 0;
169 break;
170 default:
171 OS (error, NILF,
172 _("touch: Bad return code from ar_member_touch on '%s'"), name);
173 }
174
175 free (arname);
176
177 return val;
178 }
179
180 /* State of an 'ar_glob' run, passed to 'ar_glob_match'. */
181
182 struct ar_glob_state
183 {
184 const char *arname;
185 const char *pattern;
186 size_t size;
187 struct nameseq *chain;
188 unsigned int n;
189 };
190
191 /* This function is called by 'ar_scan' to match one archive
192 element against the pattern in STATE. */
193
194 static long int
195 ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
196 long int hdrpos UNUSED, long int datapos UNUSED,
197 long int size UNUSED, long int date UNUSED, int uid UNUSED,
198 int gid UNUSED, unsigned int mode UNUSED, const void *arg)
199 {
200 struct ar_glob_state *state = (struct ar_glob_state *)arg;
201
202 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
203 {
204 /* We have a match. Add it to the chain. */
205 struct nameseq *new = xcalloc (state->size);
206 #ifdef VMS
207 if (state->suffix)
208 new->name = strcache_add(
209 concat(5, state->arname, "(", mem, state->suffix, ")"));
210 else
211 #endif
212 new->name = strcache_add(concat(4, state->arname, "(", mem, ")"));
213 new->next = state->chain;
214 state->chain = new;
215 ++state->n;
216 }
217
218 return 0L;
219 }
220
221 /* Return nonzero if PATTERN contains any metacharacters.
222 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
223 static int
224 ar_glob_pattern_p (const char *pattern, int quote)
225 {
226 const char *p;
227 int opened = 0;
228
229 for (p = pattern; *p != '\0'; ++p)
230 switch (*p)
231 {
232 case '?':
233 case '*':
234 return 1;
235
236 case '\\':
237 if (quote)
238 ++p;
239 break;
240
241 case '[':
242 opened = 1;
243 break;
244
245 case ']':
246 if (opened)
247 return 1;
248 break;
249 }
250
251 return 0;
252 }
253
254 /* Glob for MEMBER_PATTERN in archive ARNAME.
255 Return a malloc'd chain of matching elements (or nil if none). */
256
257 struct nameseq *
258 ar_glob (const char *arname, const char *member_pattern, size_t size)
259 {
260 struct ar_glob_state state;
261 struct nameseq *n;
262 const char **names;
263 unsigned int i;
264 if (! ar_glob_pattern_p (member_pattern, 1))
265 return 0;
266
267 /* Scan the archive for matches.
268 ar_glob_match will accumulate them in STATE.chain. */
269 state.arname = arname;
270 state.pattern = member_pattern;
271 state.size = size;
272 state.chain = 0;
273 state.n = 0;
274 ar_scan (arname, ar_glob_match, &state);
275
276 if (state.chain == 0)
277 return 0;
278
279 /* Now put the names into a vector for sorting. */
280 names = alloca (state.n * sizeof (const char *));
281 i = 0;
282 for (n = state.chain; n != 0; n = n->next)
283 names[i++] = n->name;
284
285 /* Sort them alphabetically. */
286 /* MSVC erroneously warns without a cast here. */
287 qsort ((void *)names, i, sizeof (*names), alpha_compare);
288
289 /* Put them back into the chain in the sorted order. */
290 i = 0;
291 for (n = state.chain; n != 0; n = n->next)
292 n->name = names[i++];
293
294 return state.chain;
295 }
296
297 #endif /* Not NO_ARCHIVES. */