1 /* Interface to 'ar' archives for GNU Make.
2 Copyright (C) 1988-2022 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 3 of the License, or (at your option) any later
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.
15 You should have received a copy of the GNU General Public License along with
16 this program. If not, see <https://www.gnu.org/licenses/>. */
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
30 If a name like 'lib((entry))' is used, a fatal error is signaled at
31 the attempt to use this unsupported feature. */
34 ar_name (const char *name)
36 const char *p = strchr (name, '(');
39 if (p == NULL || p == name)
42 end = p + strlen (p) - 1;
43 if (*end != ')' || end == p + 1)
46 if (p[1] == '(' && end[-1] == ')')
47 OS (fatal, NILF, _("attempt to use unsupported feature: '%s'"), name);
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. */
58 ar_parse_name (const char *name, char **arname_p, char **memname_p)
62 *arname_p = xstrdup (name);
63 p = strchr (*arname_p, '(');
64 /* This is never called unless ar_name() is true so p cannot be NULL. */
66 OS (fatal, NILF, "Internal: ar_parse_name: bad name '%s'", *arname_p);
68 p[strlen (p) - 1] = '\0';
73 /* This function is called by 'ar_scan' to find which member to look at. */
77 ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
78 long int hdrpos UNUSED, long int datapos UNUSED,
79 long int size UNUSED, intmax_t date,
80 int uid UNUSED, int gid UNUSED, unsigned int mode UNUSED,
83 return ar_name_equal (name, mem, truncated) ? date : 0;
86 /* Return the modtime of NAME. */
89 ar_member_date (const char *name)
95 ar_parse_name (name, &arname, &memname);
97 /* Make sure we know the modtime of the archive itself because we are
98 likely to be called just before commands to remake a member are run,
99 and they will change the archive itself.
101 But we must be careful not to enter_file the archive itself if it does
102 not exist, because pattern_search assumes that files found in the data
103 base exist or can be made. */
106 arfile = lookup_file (arname);
107 if (arfile == 0 && file_exists_p (arname))
108 arfile = enter_file (strcache_add (arname));
111 (void) f_mtime (arfile, 0);
114 val = ar_scan (arname, ar_member_date_1, memname);
118 return 0 < val && val <= TYPE_MAXIMUM (time_t) ? val : -1;
121 /* Set the archive-member NAME's modtime to now. */
125 ar_touch (const char *name)
127 O (error, NILF, _("touch archive member is not available on VMS"));
132 ar_touch (const char *name)
134 char *arname, *memname;
137 ar_parse_name (name, &arname, &memname);
139 /* Make sure we know the modtime of the archive itself before we
140 touch the member, since this will change the archive modtime. */
143 arfile = enter_file (strcache_add (arname));
148 switch (ar_member_touch (arname, memname))
151 OS (error, NILF, _("touch: Archive '%s' does not exist"), arname);
154 OS (error, NILF, _("touch: '%s' is not a valid archive"), arname);
157 perror_with_name ("touch: ", arname);
161 _("touch: Member '%s' does not exist in '%s'"), memname, arname);
168 _("touch: Bad return code from ar_member_touch on '%s'"), name);
177 /* State of an 'ar_glob' run, passed to 'ar_glob_match'. */
179 /* On VMS, (object) modules in libraries do not have suffixes. That is, to
180 find a match for a pattern, the pattern must not have any suffix. So the
181 suffix of the pattern is saved and the pattern is stripped (ar_glob).
182 If there is a match and the match, which is a module name, is added to
183 the chain, the saved suffix is added back to construct a source filename
194 struct nameseq *chain;
198 /* This function is called by 'ar_scan' to match one archive
199 element against the pattern in STATE. */
202 ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
203 long int hdrpos UNUSED, long int datapos UNUSED,
204 long int size UNUSED, intmax_t date UNUSED, int uid UNUSED,
205 int gid UNUSED, unsigned int mode UNUSED, const void *arg)
207 struct ar_glob_state *state = (struct ar_glob_state *)arg;
209 if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
211 /* We have a match. Add it to the chain. */
212 struct nameseq *new = xcalloc (state->size);
215 new->name = strcache_add(
216 concat(5, state->arname, "(", mem, state->suffix, ")"));
219 new->name = strcache_add(concat(4, state->arname, "(", mem, ")"));
220 new->next = state->chain;
228 /* Return nonzero if PATTERN contains any metacharacters.
229 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
231 ar_glob_pattern_p (const char *pattern, int quote)
236 for (p = pattern; *p != '\0'; ++p)
261 /* Glob for MEMBER_PATTERN in archive ARNAME.
262 Return a malloc'd chain of matching elements (or nil if none). */
265 ar_glob (const char *arname, const char *member_pattern, size_t size)
267 struct ar_glob_state state;
272 char *vms_member_pattern;
274 if (! ar_glob_pattern_p (member_pattern, 1))
277 /* Scan the archive for matches.
278 ar_glob_match will accumulate them in STATE.chain. */
279 state.arname = arname;
280 state.pattern = member_pattern;
283 /* In a copy of the pattern, find the suffix, save it and remove it from
286 vms_member_pattern = xstrdup(member_pattern);
287 lastdot = strrchr(vms_member_pattern, '.');
288 state.suffix = lastdot;
291 state.suffix = xstrdup(lastdot);
294 state.pattern = vms_member_pattern;
300 ar_scan (arname, ar_glob_match, &state);
303 /* Deallocate any duplicated string */
304 free(vms_member_pattern);
311 if (state.chain == 0)
314 /* Now put the names into a vector for sorting. */
315 names = alloca (state.n * sizeof (const char *));
317 for (n = state.chain; n != 0; n = n->next)
318 names[i++] = n->name;
320 /* Sort them alphabetically. */
321 /* MSVC erroneously warns without a cast here. */
322 qsort ((void *)names, i, sizeof (*names), alpha_compare);
324 /* Put them back into the chain in the sorted order. */
326 for (n = state.chain; n != 0; n = n->next)
327 n->name = names[i++];
332 #endif /* Not NO_ARCHIVES. */