* makeint.h (STOP_SET): [SV 40371] Cast to unsigned char.
[platform/upstream/make.git] / ar.c
1 /* Interface to 'ar' archives for GNU Make.
2 Copyright (C) 1988-2013 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     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 \f
68
69 /* This function is called by 'ar_scan' to find which member to look at.  */
70
71 /* ARGSUSED */
72 static long int
73 ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
74                   long int hdrpos UNUSED, long int datapos UNUSED,
75                   long int size UNUSED, long int date,
76                   int uid UNUSED, int gid UNUSED, int mode UNUSED,
77                   const void *name)
78 {
79   return ar_name_equal (name, mem, truncated) ? date : 0;
80 }
81
82 /* Return the modtime of NAME.  */
83
84 time_t
85 ar_member_date (const char *name)
86 {
87   char *arname;
88   char *memname;
89   long int val;
90
91   ar_parse_name (name, &arname, &memname);
92
93   /* Make sure we know the modtime of the archive itself because we are
94      likely to be called just before commands to remake a member are run,
95      and they will change the archive itself.
96
97      But we must be careful not to enter_file the archive itself if it does
98      not exist, because pattern_search assumes that files found in the data
99      base exist or can be made.  */
100   {
101     struct file *arfile;
102     arfile = lookup_file (arname);
103     if (arfile == 0 && file_exists_p (arname))
104       arfile = enter_file (strcache_add (arname));
105
106     if (arfile != 0)
107       (void) f_mtime (arfile, 0);
108   }
109
110   val = ar_scan (arname, ar_member_date_1, memname);
111
112   free (arname);
113
114   return (val <= 0 ? (time_t) -1 : (time_t) val);
115 }
116 \f
117 /* Set the archive-member NAME's modtime to now.  */
118
119 #ifdef VMS
120 int
121 ar_touch (const char *name)
122 {
123   error (NILF, _("touch archive member is not available on VMS"));
124   return -1;
125 }
126 #else
127 int
128 ar_touch (const char *name)
129 {
130   char *arname, *memname;
131   int val;
132
133   ar_parse_name (name, &arname, &memname);
134
135   /* Make sure we know the modtime of the archive itself before we
136      touch the member, since this will change the archive modtime.  */
137   {
138     struct file *arfile;
139     arfile = enter_file (strcache_add (arname));
140     f_mtime (arfile, 0);
141   }
142
143   val = 1;
144   switch (ar_member_touch (arname, memname))
145     {
146     case -1:
147       error (NILF, _("touch: Archive '%s' does not exist"), arname);
148       break;
149     case -2:
150       error (NILF, _("touch: '%s' is not a valid archive"), arname);
151       break;
152     case -3:
153       perror_with_name ("touch: ", arname);
154       break;
155     case 1:
156       error (NILF,
157              _("touch: Member '%s' does not exist in '%s'"), memname, arname);
158       break;
159     case 0:
160       val = 0;
161       break;
162     default:
163       error (NILF,
164              _("touch: Bad return code from ar_member_touch on '%s'"), name);
165     }
166
167   free (arname);
168
169   return val;
170 }
171 #endif /* !VMS */
172 \f
173 /* State of an 'ar_glob' run, passed to 'ar_glob_match'.  */
174
175 struct ar_glob_state
176   {
177     const char *arname;
178     const char *pattern;
179     unsigned int size;
180     struct nameseq *chain;
181     unsigned int n;
182   };
183
184 /* This function is called by 'ar_scan' to match one archive
185    element against the pattern in STATE.  */
186
187 static long int
188 ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
189                long int hdrpos UNUSED, long int datapos UNUSED,
190                long int size UNUSED, long int date UNUSED, int uid UNUSED,
191                int gid UNUSED, int mode UNUSED, const void *arg)
192 {
193   struct ar_glob_state *state = (struct ar_glob_state *)arg;
194
195   if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
196     {
197       /* We have a match.  Add it to the chain.  */
198       struct nameseq *new = xcalloc (state->size);
199       new->name = strcache_add (concat (4, state->arname, "(", mem, ")"));
200       new->next = state->chain;
201       state->chain = new;
202       ++state->n;
203     }
204
205   return 0L;
206 }
207
208 /* Return nonzero if PATTERN contains any metacharacters.
209    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
210 static int
211 glob_pattern_p (const char *pattern, int quote)
212 {
213   const char *p;
214   int opened = 0;
215
216   for (p = pattern; *p != '\0'; ++p)
217     switch (*p)
218       {
219       case '?':
220       case '*':
221         return 1;
222
223       case '\\':
224         if (quote)
225           ++p;
226         break;
227
228       case '[':
229         opened = 1;
230         break;
231
232       case ']':
233         if (opened)
234           return 1;
235         break;
236       }
237
238   return 0;
239 }
240
241 /* Glob for MEMBER_PATTERN in archive ARNAME.
242    Return a malloc'd chain of matching elements (or nil if none).  */
243
244 struct nameseq *
245 ar_glob (const char *arname, const char *member_pattern, unsigned int size)
246 {
247   struct ar_glob_state state;
248   struct nameseq *n;
249   const char **names;
250   unsigned int i;
251
252   if (! glob_pattern_p (member_pattern, 1))
253     return 0;
254
255   /* Scan the archive for matches.
256      ar_glob_match will accumulate them in STATE.chain.  */
257   state.arname = arname;
258   state.pattern = member_pattern;
259   state.size = size;
260   state.chain = 0;
261   state.n = 0;
262   ar_scan (arname, ar_glob_match, &state);
263
264   if (state.chain == 0)
265     return 0;
266
267   /* Now put the names into a vector for sorting.  */
268   names = alloca (state.n * sizeof (const char *));
269   i = 0;
270   for (n = state.chain; n != 0; n = n->next)
271     names[i++] = n->name;
272
273   /* Sort them alphabetically.  */
274   /* MSVC erroneously warns without a cast here.  */
275   qsort ((void *)names, i, sizeof (*names), alpha_compare);
276
277   /* Put them back into the chain in the sorted order.  */
278   i = 0;
279   for (n = state.chain; n != 0; n = n->next)
280     n->name = names[i++];
281
282   return state.chain;
283 }
284
285 #endif  /* Not NO_ARCHIVES.  */