Mon Nov 2 18:24:42 2020 UTC ()
make(1): use freeIt pattern in Arch_ParseArchive

This makes the memory management more obvious than before, where the
status of the variable libName depended on subLibName.


(rillig)
diff -r1.151 -r1.152 src/usr.bin/make/arch.c

cvs diff -r1.151 -r1.152 src/usr.bin/make/arch.c (switch to unified diff)

--- src/usr.bin/make/arch.c 2020/10/31 18:41:07 1.151
+++ src/usr.bin/make/arch.c 2020/11/02 18:24:42 1.152
@@ -1,1109 +1,1107 @@ @@ -1,1109 +1,1107 @@
1/* $NetBSD: arch.c,v 1.151 2020/10/31 18:41:07 rillig Exp $ */ 1/* $NetBSD: arch.c,v 1.152 2020/11/02 18:24:42 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors 18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35/* 35/*
36 * Copyright (c) 1989 by Berkeley Softworks 36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved. 37 * All rights reserved.
38 * 38 *
39 * This code is derived from software contributed to Berkeley by 39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor. 40 * Adam de Boor.
41 * 41 *
42 * Redistribution and use in source and binary forms, with or without 42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions 43 * modification, are permitted provided that the following conditions
44 * are met: 44 * are met:
45 * 1. Redistributions of source code must retain the above copyright 45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer. 46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright 47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the 48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution. 49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software 50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement: 51 * must display the following acknowledgement:
52 * This product includes software developed by the University of 52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors. 53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors 54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software 55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission. 56 * without specific prior written permission.
57 * 57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE. 68 * SUCH DAMAGE.
69 */ 69 */
70 70
71/*- 71/*-
72 * arch.c -- 72 * arch.c --
73 * Functions to manipulate libraries, archives and their members. 73 * Functions to manipulate libraries, archives and their members.
74 * 74 *
75 * Once again, cacheing/hashing comes into play in the manipulation 75 * Once again, cacheing/hashing comes into play in the manipulation
76 * of archives. The first time an archive is referenced, all of its members' 76 * of archives. The first time an archive is referenced, all of its members'
77 * headers are read and hashed and the archive closed again. All hashed 77 * headers are read and hashed and the archive closed again. All hashed
78 * archives are kept on a list which is searched each time an archive member 78 * archives are kept on a list which is searched each time an archive member
79 * is referenced. 79 * is referenced.
80 * 80 *
81 * The interface to this module is: 81 * The interface to this module is:
82 * Arch_ParseArchive 82 * Arch_ParseArchive
83 * Given an archive specification, return a list 83 * Given an archive specification, return a list
84 * of GNode's, one for each member in the spec. 84 * of GNode's, one for each member in the spec.
85 * FALSE is returned if the specification is 85 * FALSE is returned if the specification is
86 * invalid for some reason. 86 * invalid for some reason.
87 * 87 *
88 * Arch_Touch Alter the modification time of the archive 88 * Arch_Touch Alter the modification time of the archive
89 * member described by the given node to be 89 * member described by the given node to be
90 * the current time. 90 * the current time.
91 * 91 *
92 * Arch_TouchLib Update the modification time of the library 92 * Arch_TouchLib Update the modification time of the library
93 * described by the given node. This is special 93 * described by the given node. This is special
94 * because it also updates the modification time 94 * because it also updates the modification time
95 * of the library's table of contents. 95 * of the library's table of contents.
96 * 96 *
97 * Arch_MTime Find the modification time of a member of 97 * Arch_MTime Find the modification time of a member of
98 * an archive *in the archive*. The time is also 98 * an archive *in the archive*. The time is also
99 * placed in the member's GNode. Returns the 99 * placed in the member's GNode. Returns the
100 * modification time. 100 * modification time.
101 * 101 *
102 * Arch_MemTime Find the modification time of a member of 102 * Arch_MemTime Find the modification time of a member of
103 * an archive. Called when the member doesn't 103 * an archive. Called when the member doesn't
104 * already exist. Looks in the archive for the 104 * already exist. Looks in the archive for the
105 * modification time. Returns the modification 105 * modification time. Returns the modification
106 * time. 106 * time.
107 * 107 *
108 * Arch_FindLib Search for a library along a path. The 108 * Arch_FindLib Search for a library along a path. The
109 * library name in the GNode should be in 109 * library name in the GNode should be in
110 * -l<name> format. 110 * -l<name> format.
111 * 111 *
112 * Arch_LibOODate Special function to decide if a library node 112 * Arch_LibOODate Special function to decide if a library node
113 * is out-of-date. 113 * is out-of-date.
114 * 114 *
115 * Arch_Init Initialize this module. 115 * Arch_Init Initialize this module.
116 * 116 *
117 * Arch_End Clean up this module. 117 * Arch_End Clean up this module.
118 */ 118 */
119 119
120#include <sys/types.h> 120#include <sys/types.h>
121#include <sys/stat.h> 121#include <sys/stat.h>
122#include <sys/time.h> 122#include <sys/time.h>
123#include <sys/param.h> 123#include <sys/param.h>
124 124
125#include <ar.h> 125#include <ar.h>
126#include <utime.h> 126#include <utime.h>
127 127
128#include "make.h" 128#include "make.h"
129#include "dir.h" 129#include "dir.h"
130#include "config.h" 130#include "config.h"
131 131
132/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */ 132/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
133MAKE_RCSID("$NetBSD: arch.c,v 1.151 2020/10/31 18:41:07 rillig Exp $"); 133MAKE_RCSID("$NetBSD: arch.c,v 1.152 2020/11/02 18:24:42 rillig Exp $");
134 134
135#ifdef TARGET_MACHINE 135#ifdef TARGET_MACHINE
136#undef MAKE_MACHINE 136#undef MAKE_MACHINE
137#define MAKE_MACHINE TARGET_MACHINE 137#define MAKE_MACHINE TARGET_MACHINE
138#endif 138#endif
139#ifdef TARGET_MACHINE_ARCH 139#ifdef TARGET_MACHINE_ARCH
140#undef MAKE_MACHINE_ARCH 140#undef MAKE_MACHINE_ARCH
141#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH 141#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
142#endif 142#endif
143 143
144typedef struct List ArchList; 144typedef struct List ArchList;
145typedef struct ListNode ArchListNode; 145typedef struct ListNode ArchListNode;
146 146
147static ArchList *archives; /* The archives we've already examined */ 147static ArchList *archives; /* The archives we've already examined */
148 148
149typedef struct Arch { 149typedef struct Arch {
150 char *name; /* Name of archive */ 150 char *name; /* Name of archive */
151 HashTable members; /* All the members of the archive described 151 HashTable members; /* All the members of the archive described
152 * by <name, struct ar_hdr *> key/value pairs */ 152 * by <name, struct ar_hdr *> key/value pairs */
153 char *fnametab; /* Extended name table strings */ 153 char *fnametab; /* Extended name table strings */
154 size_t fnamesize; /* Size of the string table */ 154 size_t fnamesize; /* Size of the string table */
155} Arch; 155} Arch;
156 156
157static FILE *ArchFindMember(const char *, const char *, 157static FILE *ArchFindMember(const char *, const char *,
158 struct ar_hdr *, const char *); 158 struct ar_hdr *, const char *);
159#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__) 159#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
160#define SVR4ARCHIVES 160#define SVR4ARCHIVES
161static int ArchSVR4Entry(Arch *, char *, size_t, FILE *); 161static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
162#endif 162#endif
163 163
164#ifdef CLEANUP 164#ifdef CLEANUP
165static void 165static void
166ArchFree(void *ap) 166ArchFree(void *ap)
167{ 167{
168 Arch *a = ap; 168 Arch *a = ap;
169 HashIter hi; 169 HashIter hi;
170 170
171 /* Free memory from hash entries */ 171 /* Free memory from hash entries */
172 HashIter_Init(&hi, &a->members); 172 HashIter_Init(&hi, &a->members);
173 while (HashIter_Next(&hi) != NULL) 173 while (HashIter_Next(&hi) != NULL)
174 free(hi.entry->value); 174 free(hi.entry->value);
175 175
176 free(a->name); 176 free(a->name);
177 free(a->fnametab); 177 free(a->fnametab);
178 HashTable_Done(&a->members); 178 HashTable_Done(&a->members);
179 free(a); 179 free(a);
180} 180}
181#endif 181#endif
182 182
183 183
184/*- 184/*-
185 *----------------------------------------------------------------------- 185 *-----------------------------------------------------------------------
186 * Arch_ParseArchive -- 186 * Arch_ParseArchive --
187 * Parse the archive specification in the given line and find/create 187 * Parse the archive specification in the given line and find/create
188 * the nodes for the specified archive members, placing their nodes 188 * the nodes for the specified archive members, placing their nodes
189 * on the given list. 189 * on the given list.
190 * 190 *
191 * Input: 191 * Input:
192 * linePtr Pointer to start of specification 192 * linePtr Pointer to start of specification
193 * nodeLst Lst on which to place the nodes 193 * nodeLst Lst on which to place the nodes
194 * ctxt Context in which to expand variables 194 * ctxt Context in which to expand variables
195 * 195 *
196 * Results: 196 * Results:
197 * TRUE if it was a valid specification. The linePtr is updated 197 * TRUE if it was a valid specification. The linePtr is updated
198 * to point to the first non-space after the archive spec. The 198 * to point to the first non-space after the archive spec. The
199 * nodes for the members are placed on the given list. 199 * nodes for the members are placed on the given list.
200 *----------------------------------------------------------------------- 200 *-----------------------------------------------------------------------
201 */ 201 */
202Boolean 202Boolean
203Arch_ParseArchive(char **linePtr, GNodeList *nodeLst, GNode *ctxt) 203Arch_ParseArchive(char **linePtr, GNodeList *nodeLst, GNode *ctxt)
204{ 204{
205 char *cp; /* Pointer into line */ 205 char *cp; /* Pointer into line */
206 GNode *gn; /* New node */ 206 GNode *gn; /* New node */
207 char *libName; /* Library-part of specification */ 207 char *libName; /* Library-part of specification */
 208 char *libName_freeIt = NULL;
208 char *memName; /* Member-part of specification */ 209 char *memName; /* Member-part of specification */
209 char saveChar; /* Ending delimiter of member-name */ 210 char saveChar; /* Ending delimiter of member-name */
210 Boolean subLibName; /* TRUE if libName should have/had 211 Boolean subLibName; /* TRUE if libName should have/had
211 * variable substitution performed on it */ 212 * variable substitution performed on it */
212 213
213 libName = *linePtr; 214 libName = *linePtr;
214 215
215 subLibName = FALSE; 216 subLibName = FALSE;
216 217
217 for (cp = libName; *cp != '(' && *cp != '\0';) { 218 for (cp = libName; *cp != '(' && *cp != '\0';) {
218 if (*cp == '$') { 219 if (*cp == '$') {
219 /* 220 /*
220 * Variable spec, so call the Var module to parse the puppy 221 * Variable spec, so call the Var module to parse the puppy
221 * so we can safely advance beyond it... 222 * so we can safely advance beyond it...
222 */ 223 */
223 const char *nested_p = cp; 224 const char *nested_p = cp;
224 void *result_freeIt; 225 void *result_freeIt;
225 const char *result; 226 const char *result;
226 Boolean isError; 227 Boolean isError;
227 228
 229 /* XXX: is expanded twice: once here and once below */
228 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES, 230 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
229 &result, &result_freeIt); 231 &result, &result_freeIt);
230 /* TODO: handle errors */ 232 /* TODO: handle errors */
231 isError = result == var_Error; 233 isError = result == var_Error;
232 free(result_freeIt); 234 free(result_freeIt);
233 if (isError) 235 if (isError)
234 return FALSE; 236 return FALSE;
235 237
236 subLibName = TRUE; 238 subLibName = TRUE;
237 cp += nested_p - cp; 239 cp += nested_p - cp;
238 } else 240 } else
239 cp++; 241 cp++;
240 } 242 }
241 243
242 *cp++ = '\0'; 244 *cp++ = '\0';
243 if (subLibName) { 245 if (subLibName) {
244 (void)Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES, &libName); 246 (void)Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES, &libName);
245 /* TODO: handle errors */ 247 /* TODO: handle errors */
 248 libName_freeIt = libName;
246 } 249 }
247 250
248 251
249 for (;;) { 252 for (;;) {
250 /* 253 /*
251 * First skip to the start of the member's name, mark that 254 * First skip to the start of the member's name, mark that
252 * place and skip to the end of it (either white-space or 255 * place and skip to the end of it (either white-space or
253 * a close paren). 256 * a close paren).
254 */ 257 */
255 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */ 258 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
256 259
257 pp_skip_whitespace(&cp); 260 pp_skip_whitespace(&cp);
258 261
259 memName = cp; 262 memName = cp;
260 while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) { 263 while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
261 if (*cp == '$') { 264 if (*cp == '$') {
262 /* 265 /*
263 * Variable spec, so call the Var module to parse the puppy 266 * Variable spec, so call the Var module to parse the puppy
264 * so we can safely advance beyond it... 267 * so we can safely advance beyond it...
265 */ 268 */
266 void *freeIt; 269 void *freeIt;
267 const char *result; 270 const char *result;
268 Boolean isError; 271 Boolean isError;
269 const char *nested_p = cp; 272 const char *nested_p = cp;
270 273
271 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES, 274 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
272 &result, &freeIt); 275 &result, &freeIt);
273 /* TODO: handle errors */ 276 /* TODO: handle errors */
274 isError = result == var_Error; 277 isError = result == var_Error;
275 free(freeIt); 278 free(freeIt);
276 279
277 if (isError) 280 if (isError)
278 return FALSE; 281 return FALSE;
279 282
280 doSubst = TRUE; 283 doSubst = TRUE;
281 cp += nested_p - cp; 284 cp += nested_p - cp;
282 } else { 285 } else {
283 cp++; 286 cp++;
284 } 287 }
285 } 288 }
286 289
287 /* 290 /*
288 * If the specification ends without a closing parenthesis, 291 * If the specification ends without a closing parenthesis,
289 * chances are there's something wrong (like a missing backslash), 292 * chances are there's something wrong (like a missing backslash),
290 * so it's better to return failure than allow such things to happen 293 * so it's better to return failure than allow such things to happen
291 */ 294 */
292 if (*cp == '\0') { 295 if (*cp == '\0') {
293 printf("No closing parenthesis in archive specification\n"); 296 printf("No closing parenthesis in archive specification\n");
294 return FALSE; 297 return FALSE;
295 } 298 }
296 299
297 /* 300 /*
298 * If we didn't move anywhere, we must be done 301 * If we didn't move anywhere, we must be done
299 */ 302 */
300 if (cp == memName) { 303 if (cp == memName) {
301 break; 304 break;
302 } 305 }
303 306
304 saveChar = *cp; 307 saveChar = *cp;
305 *cp = '\0'; 308 *cp = '\0';
306 309
307 /* 310 /*
308 * XXX: This should be taken care of intelligently by 311 * XXX: This should be taken care of intelligently by
309 * SuffExpandChildren, both for the archive and the member portions. 312 * SuffExpandChildren, both for the archive and the member portions.
310 */ 313 */
311 /* 314 /*
312 * If member contains variables, try and substitute for them. 315 * If member contains variables, try and substitute for them.
313 * This will slow down archive specs with dynamic sources, of course, 316 * This will slow down archive specs with dynamic sources, of course,
314 * since we'll be (non-)substituting them three times, but them's 317 * since we'll be (non-)substituting them three times, but them's
315 * the breaks -- we need to do this since SuffExpandChildren calls 318 * the breaks -- we need to do this since SuffExpandChildren calls
316 * us, otherwise we could assume the thing would be taken care of 319 * us, otherwise we could assume the thing would be taken care of
317 * later. 320 * later.
318 */ 321 */
319 if (doSubst) { 322 if (doSubst) {
320 char *buf; 323 char *buf;
321 char *sacrifice; 324 char *sacrifice;
322 char *oldMemName = memName; 325 char *oldMemName = memName;
323 326
324 (void)Var_Subst(memName, ctxt, VARE_UNDEFERR|VARE_WANTRES, 327 (void)Var_Subst(memName, ctxt, VARE_UNDEFERR|VARE_WANTRES,
325 &memName); 328 &memName);
326 /* TODO: handle errors */ 329 /* TODO: handle errors */
327 330
328 /* 331 /*
329 * Now form an archive spec and recurse to deal with nested 332 * Now form an archive spec and recurse to deal with nested
330 * variables and multi-word variable values.... The results 333 * variables and multi-word variable values.... The results
331 * are just placed at the end of the nodeLst we're returning. 334 * are just placed at the end of the nodeLst we're returning.
332 */ 335 */
333 buf = sacrifice = str_concat4(libName, "(", memName, ")"); 336 buf = sacrifice = str_concat4(libName, "(", memName, ")");
334 337
335 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) { 338 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
336 /* 339 /*
337 * Must contain dynamic sources, so we can't deal with it now. 340 * Must contain dynamic sources, so we can't deal with it now.
338 * Just create an ARCHV node for the thing and let 341 * Just create an ARCHV node for the thing and let
339 * SuffExpandChildren handle it... 342 * SuffExpandChildren handle it...
340 */ 343 */
341 gn = Targ_GetNode(buf); 344 gn = Targ_GetNode(buf);
342 gn->type |= OP_ARCHV; 345 gn->type |= OP_ARCHV;
343 Lst_Append(nodeLst, gn); 346 Lst_Append(nodeLst, gn);
344 347
345 } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) { 348 } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) {
346 /* Error in nested call. */ 349 /* Error in nested call. */
347 free(buf); 350 free(buf);
348 return FALSE; 351 return FALSE;
349 } 352 }
350 free(buf); 353 free(buf);
351 354
352 } else if (Dir_HasWildcards(memName)) { 355 } else if (Dir_HasWildcards(memName)) {
353 StringList *members = Lst_New(); 356 StringList *members = Lst_New();
354 Dir_Expand(memName, dirSearchPath, members); 357 Dir_Expand(memName, dirSearchPath, members);
355 358
356 while (!Lst_IsEmpty(members)) { 359 while (!Lst_IsEmpty(members)) {
357 char *member = Lst_Dequeue(members); 360 char *member = Lst_Dequeue(members);
358 char *fullname = str_concat4(libName, "(", member, ")"); 361 char *fullname = str_concat4(libName, "(", member, ")");
359 free(member); 362 free(member);
360 363
361 gn = Targ_GetNode(fullname); 364 gn = Targ_GetNode(fullname);
362 free(fullname); 365 free(fullname);
363 366
364 gn->type |= OP_ARCHV; 367 gn->type |= OP_ARCHV;
365 Lst_Append(nodeLst, gn); 368 Lst_Append(nodeLst, gn);
366 } 369 }
367 Lst_Free(members); 370 Lst_Free(members);
368 371
369 } else { 372 } else {
370 char *fullname = str_concat4(libName, "(", memName, ")"); 373 char *fullname = str_concat4(libName, "(", memName, ")");
371 gn = Targ_GetNode(fullname); 374 gn = Targ_GetNode(fullname);
372 free(fullname); 375 free(fullname);
373 376
374 /* 377 /*
375 * We've found the node, but have to make sure the rest of the 378 * We've found the node, but have to make sure the rest of the
376 * world knows it's an archive member, without having to 379 * world knows it's an archive member, without having to
377 * constantly check for parentheses, so we type the thing with 380 * constantly check for parentheses, so we type the thing with
378 * the OP_ARCHV bit before we place it on the end of the 381 * the OP_ARCHV bit before we place it on the end of the
379 * provided list. 382 * provided list.
380 */ 383 */
381 gn->type |= OP_ARCHV; 384 gn->type |= OP_ARCHV;
382 Lst_Append(nodeLst, gn); 385 Lst_Append(nodeLst, gn);
383 } 386 }
384 if (doSubst) { 387 if (doSubst) {
385 free(memName); 388 free(memName);
386 } 389 }
387 390
388 *cp = saveChar; 391 *cp = saveChar;
389 } 392 }
390 393
391 /* 394 free(libName_freeIt);
392 * If substituted libName, free it now, since we need it no longer. 
393 */ 
394 if (subLibName) { 
395 free(libName); 
396 } 
397 395
398 cp++; /* skip the ')' */ 396 cp++; /* skip the ')' */
399 /* We promised that linePtr would be set up at the next non-space. */ 397 /* We promised that linePtr would be set up at the next non-space. */
400 pp_skip_whitespace(&cp); 398 pp_skip_whitespace(&cp);
401 *linePtr = cp; 399 *linePtr = cp;
402 return TRUE; 400 return TRUE;
403} 401}
404 402
405/* Locate a member of an archive, given the path of the archive and the path 403/* Locate a member of an archive, given the path of the archive and the path
406 * of the desired member. 404 * of the desired member.
407 * 405 *
408 * Input: 406 * Input:
409 * archive Path to the archive 407 * archive Path to the archive
410 * member Name of member; only its basename is used. 408 * member Name of member; only its basename is used.
411 * hash TRUE if archive should be hashed if not already so. 409 * hash TRUE if archive should be hashed if not already so.
412 * 410 *
413 * Results: 411 * Results:
414 * The ar_hdr for the member. 412 * The ar_hdr for the member.
415 */ 413 */
416static struct ar_hdr * 414static struct ar_hdr *
417ArchStatMember(const char *archive, const char *member, Boolean hash) 415ArchStatMember(const char *archive, const char *member, Boolean hash)
418{ 416{
419#define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1) 417#define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1)
420 FILE *arch; /* Stream to archive */ 418 FILE *arch; /* Stream to archive */
421 size_t size; /* Size of archive member */ 419 size_t size; /* Size of archive member */
422 char magic[SARMAG]; 420 char magic[SARMAG];
423 ArchListNode *ln; 421 ArchListNode *ln;
424 Arch *ar; /* Archive descriptor */ 422 Arch *ar; /* Archive descriptor */
425 struct ar_hdr arh; /* archive-member header for reading archive */ 423 struct ar_hdr arh; /* archive-member header for reading archive */
426 char memName[MAXPATHLEN + 1]; 424 char memName[MAXPATHLEN + 1];
427 /* Current member name while hashing. */ 425 /* Current member name while hashing. */
428 426
429 /* 427 /*
430 * Because of space constraints and similar things, files are archived 428 * Because of space constraints and similar things, files are archived
431 * using their basename, not the entire path. 429 * using their basename, not the entire path.
432 */ 430 */
433 const char *lastSlash = strrchr(member, '/'); 431 const char *lastSlash = strrchr(member, '/');
434 if (lastSlash != NULL) 432 if (lastSlash != NULL)
435 member = lastSlash + 1; 433 member = lastSlash + 1;
436 434
437 for (ln = archives->first; ln != NULL; ln = ln->next) { 435 for (ln = archives->first; ln != NULL; ln = ln->next) {
438 const Arch *archPtr = ln->datum; 436 const Arch *archPtr = ln->datum;
439 if (strcmp(archPtr->name, archive) == 0) 437 if (strcmp(archPtr->name, archive) == 0)
440 break; 438 break;
441 } 439 }
442 440
443 if (ln != NULL) { 441 if (ln != NULL) {
444 struct ar_hdr *hdr; 442 struct ar_hdr *hdr;
445 443
446 ar = ln->datum; 444 ar = ln->datum;
447 hdr = HashTable_FindValue(&ar->members, member); 445 hdr = HashTable_FindValue(&ar->members, member);
448 if (hdr != NULL) 446 if (hdr != NULL)
449 return hdr; 447 return hdr;
450 448
451 { 449 {
452 /* Try truncated name */ 450 /* Try truncated name */
453 char copy[AR_MAX_NAME_LEN + 1]; 451 char copy[AR_MAX_NAME_LEN + 1];
454 size_t len = strlen(member); 452 size_t len = strlen(member);
455 453
456 if (len > AR_MAX_NAME_LEN) { 454 if (len > AR_MAX_NAME_LEN) {
457 len = AR_MAX_NAME_LEN; 455 len = AR_MAX_NAME_LEN;
458 snprintf(copy, sizeof copy, "%s", member); 456 snprintf(copy, sizeof copy, "%s", member);
459 } 457 }
460 hdr = HashTable_FindValue(&ar->members, copy); 458 hdr = HashTable_FindValue(&ar->members, copy);
461 return hdr; 459 return hdr;
462 } 460 }
463 } 461 }
464 462
465 if (!hash) { 463 if (!hash) {
466 /* 464 /*
467 * Caller doesn't want the thing hashed, just use ArchFindMember 465 * Caller doesn't want the thing hashed, just use ArchFindMember
468 * to read the header for the member out and close down the stream 466 * to read the header for the member out and close down the stream
469 * again. Since the archive is not to be hashed, we assume there's 467 * again. Since the archive is not to be hashed, we assume there's
470 * no need to allocate extra room for the header we're returning, 468 * no need to allocate extra room for the header we're returning,
471 * so just declare it static. 469 * so just declare it static.
472 */ 470 */
473 static struct ar_hdr sarh; 471 static struct ar_hdr sarh;
474 472
475 arch = ArchFindMember(archive, member, &sarh, "r"); 473 arch = ArchFindMember(archive, member, &sarh, "r");
476 if (arch == NULL) 474 if (arch == NULL)
477 return NULL; 475 return NULL;
478 476
479 fclose(arch); 477 fclose(arch);
480 return &sarh; 478 return &sarh;
481 } 479 }
482 480
483 /* 481 /*
484 * We don't have this archive on the list yet, so we want to find out 482 * We don't have this archive on the list yet, so we want to find out
485 * everything that's in it and cache it so we can get at it quickly. 483 * everything that's in it and cache it so we can get at it quickly.
486 */ 484 */
487 arch = fopen(archive, "r"); 485 arch = fopen(archive, "r");
488 if (arch == NULL) 486 if (arch == NULL)
489 return NULL; 487 return NULL;
490 488
491 /* 489 /*
492 * We use the ARMAG string to make sure this is an archive we 490 * We use the ARMAG string to make sure this is an archive we
493 * can handle... 491 * can handle...
494 */ 492 */
495 if ((fread(magic, SARMAG, 1, arch) != 1) || 493 if ((fread(magic, SARMAG, 1, arch) != 1) ||
496 (strncmp(magic, ARMAG, SARMAG) != 0)) { 494 (strncmp(magic, ARMAG, SARMAG) != 0)) {
497 fclose(arch); 495 fclose(arch);
498 return NULL; 496 return NULL;
499 } 497 }
500 498
501 ar = bmake_malloc(sizeof(Arch)); 499 ar = bmake_malloc(sizeof(Arch));
502 ar->name = bmake_strdup(archive); 500 ar->name = bmake_strdup(archive);
503 ar->fnametab = NULL; 501 ar->fnametab = NULL;
504 ar->fnamesize = 0; 502 ar->fnamesize = 0;
505 HashTable_Init(&ar->members); 503 HashTable_Init(&ar->members);
506 memName[AR_MAX_NAME_LEN] = '\0'; 504 memName[AR_MAX_NAME_LEN] = '\0';
507 505
508 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) { 506 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
509 if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) { 507 if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
510 /* 508 /*
511 * The header is bogus, so the archive is bad 509 * The header is bogus, so the archive is bad
512 * and there's no way we can recover... 510 * and there's no way we can recover...
513 */ 511 */
514 goto badarch; 512 goto badarch;
515 } else { 513 } else {
516 char *nameend; 514 char *nameend;
517 515
518 /* 516 /*
519 * We need to advance the stream's pointer to the start of the 517 * We need to advance the stream's pointer to the start of the
520 * next header. Files are padded with newlines to an even-byte 518 * next header. Files are padded with newlines to an even-byte
521 * boundary, so we need to extract the size of the file from the 519 * boundary, so we need to extract the size of the file from the
522 * 'size' field of the header and round it up during the seek. 520 * 'size' field of the header and round it up during the seek.
523 */ 521 */
524 arh.ar_size[sizeof(arh.ar_size) - 1] = '\0'; 522 arh.ar_size[sizeof(arh.ar_size) - 1] = '\0';
525 size = (size_t)strtol(arh.ar_size, NULL, 10); 523 size = (size_t)strtol(arh.ar_size, NULL, 10);
526 524
527 memcpy(memName, arh.ar_name, sizeof(arh.ar_name)); 525 memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
528 nameend = memName + AR_MAX_NAME_LEN; 526 nameend = memName + AR_MAX_NAME_LEN;
529 while (*nameend == ' ') { 527 while (*nameend == ' ') {
530 nameend--; 528 nameend--;
531 } 529 }
532 nameend[1] = '\0'; 530 nameend[1] = '\0';
533 531
534#ifdef SVR4ARCHIVES 532#ifdef SVR4ARCHIVES
535 /* 533 /*
536 * svr4 names are slash terminated. Also svr4 extended AR format. 534 * svr4 names are slash terminated. Also svr4 extended AR format.
537 */ 535 */
538 if (memName[0] == '/') { 536 if (memName[0] == '/') {
539 /* 537 /*
540 * svr4 magic mode; handle it 538 * svr4 magic mode; handle it
541 */ 539 */
542 switch (ArchSVR4Entry(ar, memName, size, arch)) { 540 switch (ArchSVR4Entry(ar, memName, size, arch)) {
543 case -1: /* Invalid data */ 541 case -1: /* Invalid data */
544 goto badarch; 542 goto badarch;
545 case 0: /* List of files entry */ 543 case 0: /* List of files entry */
546 continue; 544 continue;
547 default: /* Got the entry */ 545 default: /* Got the entry */
548 break; 546 break;
549 } 547 }
550 } else { 548 } else {
551 if (nameend[0] == '/') 549 if (nameend[0] == '/')
552 nameend[0] = '\0'; 550 nameend[0] = '\0';
553 } 551 }
554#endif 552#endif
555 553
556#ifdef AR_EFMT1 554#ifdef AR_EFMT1
557 /* 555 /*
558 * BSD 4.4 extended AR format: #1/<namelen>, with name as the 556 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
559 * first <namelen> bytes of the file 557 * first <namelen> bytes of the file
560 */ 558 */
561 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && 559 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
562 ch_isdigit(memName[sizeof(AR_EFMT1) - 1])) { 560 ch_isdigit(memName[sizeof(AR_EFMT1) - 1])) {
563 561
564 int elen = atoi(&memName[sizeof(AR_EFMT1) - 1]); 562 int elen = atoi(&memName[sizeof(AR_EFMT1) - 1]);
565 563
566 if ((unsigned int)elen > MAXPATHLEN) 564 if ((unsigned int)elen > MAXPATHLEN)
567 goto badarch; 565 goto badarch;
568 if (fread(memName, (size_t)elen, 1, arch) != 1) 566 if (fread(memName, (size_t)elen, 1, arch) != 1)
569 goto badarch; 567 goto badarch;
570 memName[elen] = '\0'; 568 memName[elen] = '\0';
571 if (fseek(arch, -elen, SEEK_CUR) != 0) 569 if (fseek(arch, -elen, SEEK_CUR) != 0)
572 goto badarch; 570 goto badarch;
573 if (DEBUG(ARCH) || DEBUG(MAKE)) { 571 if (DEBUG(ARCH) || DEBUG(MAKE)) {
574 debug_printf("ArchStat: Extended format entry for %s\n", 572 debug_printf("ArchStat: Extended format entry for %s\n",
575 memName); 573 memName);
576 } 574 }
577 } 575 }
578#endif 576#endif
579 577
580 { 578 {
581 HashEntry *he; 579 HashEntry *he;
582 he = HashTable_CreateEntry(&ar->members, memName, NULL); 580 he = HashTable_CreateEntry(&ar->members, memName, NULL);
583 HashEntry_Set(he, bmake_malloc(sizeof(struct ar_hdr))); 581 HashEntry_Set(he, bmake_malloc(sizeof(struct ar_hdr)));
584 memcpy(HashEntry_Get(he), &arh, sizeof(struct ar_hdr)); 582 memcpy(HashEntry_Get(he), &arh, sizeof(struct ar_hdr));
585 } 583 }
586 } 584 }
587 if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0) 585 if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
588 goto badarch; 586 goto badarch;
589 } 587 }
590 588
591 fclose(arch); 589 fclose(arch);
592 590
593 Lst_Append(archives, ar); 591 Lst_Append(archives, ar);
594 592
595 /* 593 /*
596 * Now that the archive has been read and cached, we can look into 594 * Now that the archive has been read and cached, we can look into
597 * the hash table to find the desired member's header. 595 * the hash table to find the desired member's header.
598 */ 596 */
599 return HashTable_FindValue(&ar->members, member); 597 return HashTable_FindValue(&ar->members, member);
600 598
601badarch: 599badarch:
602 fclose(arch); 600 fclose(arch);
603 HashTable_Done(&ar->members); 601 HashTable_Done(&ar->members);
604 free(ar->fnametab); 602 free(ar->fnametab);
605 free(ar); 603 free(ar);
606 return NULL; 604 return NULL;
607} 605}
608 606
609#ifdef SVR4ARCHIVES 607#ifdef SVR4ARCHIVES
610/*- 608/*-
611 *----------------------------------------------------------------------- 609 *-----------------------------------------------------------------------
612 * ArchSVR4Entry -- 610 * ArchSVR4Entry --
613 * Parse an SVR4 style entry that begins with a slash. 611 * Parse an SVR4 style entry that begins with a slash.
614 * If it is "//", then load the table of filenames 612 * If it is "//", then load the table of filenames
615 * If it is "/<offset>", then try to substitute the long file name 613 * If it is "/<offset>", then try to substitute the long file name
616 * from offset of a table previously read. 614 * from offset of a table previously read.
617 * If a table is read, the file pointer is moved to the next archive 615 * If a table is read, the file pointer is moved to the next archive
618 * member. 616 * member.
619 * 617 *
620 * Results: 618 * Results:
621 * -1: Bad data in archive 619 * -1: Bad data in archive
622 * 0: A table was loaded from the file 620 * 0: A table was loaded from the file
623 * 1: Name was successfully substituted from table 621 * 1: Name was successfully substituted from table
624 * 2: Name was not successfully substituted from table 622 * 2: Name was not successfully substituted from table
625 *----------------------------------------------------------------------- 623 *-----------------------------------------------------------------------
626 */ 624 */
627static int 625static int
628ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch) 626ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
629{ 627{
630#define ARLONGNAMES1 "//" 628#define ARLONGNAMES1 "//"
631#define ARLONGNAMES2 "/ARFILENAMES" 629#define ARLONGNAMES2 "/ARFILENAMES"
632 size_t entry; 630 size_t entry;
633 char *ptr, *eptr; 631 char *ptr, *eptr;
634 632
635 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 || 633 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
636 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) { 634 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
637 635
638 if (ar->fnametab != NULL) { 636 if (ar->fnametab != NULL) {
639 DEBUG0(ARCH, "Attempted to redefine an SVR4 name table\n"); 637 DEBUG0(ARCH, "Attempted to redefine an SVR4 name table\n");
640 return -1; 638 return -1;
641 } 639 }
642 640
643 /* 641 /*
644 * This is a table of archive names, so we build one for 642 * This is a table of archive names, so we build one for
645 * ourselves 643 * ourselves
646 */ 644 */
647 ar->fnametab = bmake_malloc(size); 645 ar->fnametab = bmake_malloc(size);
648 ar->fnamesize = size; 646 ar->fnamesize = size;
649 647
650 if (fread(ar->fnametab, size, 1, arch) != 1) { 648 if (fread(ar->fnametab, size, 1, arch) != 1) {
651 DEBUG0(ARCH, "Reading an SVR4 name table failed\n"); 649 DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
652 return -1; 650 return -1;
653 } 651 }
654 eptr = ar->fnametab + size; 652 eptr = ar->fnametab + size;
655 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++) 653 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
656 if (*ptr == '/') { 654 if (*ptr == '/') {
657 entry++; 655 entry++;
658 *ptr = '\0'; 656 *ptr = '\0';
659 } 657 }
660 DEBUG1(ARCH, "Found svr4 archive name table with %lu entries\n", 658 DEBUG1(ARCH, "Found svr4 archive name table with %lu entries\n",
661 (unsigned long)entry); 659 (unsigned long)entry);
662 return 0; 660 return 0;
663 } 661 }
664 662
665 if (name[1] == ' ' || name[1] == '\0') 663 if (name[1] == ' ' || name[1] == '\0')
666 return 2; 664 return 2;
667 665
668 entry = (size_t)strtol(&name[1], &eptr, 0); 666 entry = (size_t)strtol(&name[1], &eptr, 0);
669 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) { 667 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
670 DEBUG1(ARCH, "Could not parse SVR4 name %s\n", name); 668 DEBUG1(ARCH, "Could not parse SVR4 name %s\n", name);
671 return 2; 669 return 2;
672 } 670 }
673 if (entry >= ar->fnamesize) { 671 if (entry >= ar->fnamesize) {
674 DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n", 672 DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
675 name, (unsigned long)ar->fnamesize); 673 name, (unsigned long)ar->fnamesize);
676 return 2; 674 return 2;
677 } 675 }
678 676
679 DEBUG2(ARCH, "Replaced %s with %s\n", name, &ar->fnametab[entry]); 677 DEBUG2(ARCH, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
680 678
681 snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]); 679 snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
682 return 1; 680 return 1;
683} 681}
684#endif 682#endif
685 683
686 684
687/*- 685/*-
688 *----------------------------------------------------------------------- 686 *-----------------------------------------------------------------------
689 * ArchFindMember -- 687 * ArchFindMember --
690 * Locate a member of an archive, given the path of the archive and 688 * Locate a member of an archive, given the path of the archive and
691 * the path of the desired member. If the archive is to be modified, 689 * the path of the desired member. If the archive is to be modified,
692 * the mode should be "r+", if not, it should be "r". 690 * the mode should be "r+", if not, it should be "r".
693 * The passed struct ar_hdr structure is filled in. 691 * The passed struct ar_hdr structure is filled in.
694 * 692 *
695 * Input: 693 * Input:
696 * archive Path to the archive 694 * archive Path to the archive
697 * member Name of member. If it is a path, only the last 695 * member Name of member. If it is a path, only the last
698 * component is used. 696 * component is used.
699 * arhPtr Pointer to header structure to be filled in 697 * arhPtr Pointer to header structure to be filled in
700 * mode The mode for opening the stream 698 * mode The mode for opening the stream
701 * 699 *
702 * Results: 700 * Results:
703 * An FILE *, opened for reading and writing, positioned at the 701 * An FILE *, opened for reading and writing, positioned at the
704 * start of the member's struct ar_hdr, or NULL if the member was 702 * start of the member's struct ar_hdr, or NULL if the member was
705 * nonexistent. The current struct ar_hdr for member. 703 * nonexistent. The current struct ar_hdr for member.
706 *----------------------------------------------------------------------- 704 *-----------------------------------------------------------------------
707 */ 705 */
708static FILE * 706static FILE *
709ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr, 707ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
710 const char *mode) 708 const char *mode)
711{ 709{
712 FILE *arch; /* Stream to archive */ 710 FILE *arch; /* Stream to archive */
713 int size; /* Size of archive member */ 711 int size; /* Size of archive member */
714 char magic[SARMAG]; 712 char magic[SARMAG];
715 size_t len, tlen; 713 size_t len, tlen;
716 const char *lastSlash; 714 const char *lastSlash;
717 715
718 arch = fopen(archive, mode); 716 arch = fopen(archive, mode);
719 if (arch == NULL) 717 if (arch == NULL)
720 return NULL; 718 return NULL;
721 719
722 /* 720 /*
723 * We use the ARMAG string to make sure this is an archive we 721 * We use the ARMAG string to make sure this is an archive we
724 * can handle... 722 * can handle...
725 */ 723 */
726 if ((fread(magic, SARMAG, 1, arch) != 1) || 724 if ((fread(magic, SARMAG, 1, arch) != 1) ||
727 (strncmp(magic, ARMAG, SARMAG) != 0)) { 725 (strncmp(magic, ARMAG, SARMAG) != 0)) {
728 fclose(arch); 726 fclose(arch);
729 return NULL; 727 return NULL;
730 } 728 }
731 729
732 /* 730 /*
733 * Because of space constraints and similar things, files are archived 731 * Because of space constraints and similar things, files are archived
734 * using their basename, not the entire path. 732 * using their basename, not the entire path.
735 */ 733 */
736 lastSlash = strrchr(member, '/'); 734 lastSlash = strrchr(member, '/');
737 if (lastSlash != NULL) 735 if (lastSlash != NULL)
738 member = lastSlash + 1; 736 member = lastSlash + 1;
739 737
740 len = tlen = strlen(member); 738 len = tlen = strlen(member);
741 if (len > sizeof(arhPtr->ar_name)) { 739 if (len > sizeof(arhPtr->ar_name)) {
742 tlen = sizeof(arhPtr->ar_name); 740 tlen = sizeof(arhPtr->ar_name);
743 } 741 }
744 742
745 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) { 743 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
746 744
747 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag)) != 0) { 745 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag)) != 0) {
748 /* 746 /*
749 * The header is bogus, so the archive is bad 747 * The header is bogus, so the archive is bad
750 * and there's no way we can recover... 748 * and there's no way we can recover...
751 */ 749 */
752 fclose(arch); 750 fclose(arch);
753 return NULL; 751 return NULL;
754 } 752 }
755 753
756 if (strncmp(member, arhPtr->ar_name, tlen) == 0) { 754 if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
757 /* 755 /*
758 * If the member's name doesn't take up the entire 'name' field, 756 * If the member's name doesn't take up the entire 'name' field,
759 * we have to be careful of matching prefixes. Names are space- 757 * we have to be careful of matching prefixes. Names are space-
760 * padded to the right, so if the character in 'name' at the end 758 * padded to the right, so if the character in 'name' at the end
761 * of the matched string is anything but a space, this isn't the 759 * of the matched string is anything but a space, this isn't the
762 * member we sought. 760 * member we sought.
763 */ 761 */
764 if (tlen != sizeof arhPtr->ar_name && arhPtr->ar_name[tlen] != ' ') 762 if (tlen != sizeof arhPtr->ar_name && arhPtr->ar_name[tlen] != ' ')
765 goto skip; 763 goto skip;
766 764
767 /* 765 /*
768 * To make life easier, we reposition the file at the start 766 * To make life easier, we reposition the file at the start
769 * of the header we just read before we return the stream. 767 * of the header we just read before we return the stream.
770 * In a more general situation, it might be better to leave 768 * In a more general situation, it might be better to leave
771 * the file at the actual member, rather than its header, but 769 * the file at the actual member, rather than its header, but
772 * not here... 770 * not here...
773 */ 771 */
774 if (fseek(arch, -(long)sizeof(struct ar_hdr), SEEK_CUR) != 0) { 772 if (fseek(arch, -(long)sizeof(struct ar_hdr), SEEK_CUR) != 0) {
775 fclose(arch); 773 fclose(arch);
776 return NULL; 774 return NULL;
777 } 775 }
778 return arch; 776 return arch;
779 } 777 }
780 778
781#ifdef AR_EFMT1 779#ifdef AR_EFMT1
782 /* 780 /*
783 * BSD 4.4 extended AR format: #1/<namelen>, with name as the 781 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
784 * first <namelen> bytes of the file 782 * first <namelen> bytes of the file
785 */ 783 */
786 if (strncmp(arhPtr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && 784 if (strncmp(arhPtr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
787 ch_isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) 785 ch_isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1]))
788 { 786 {
789 int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1) - 1]); 787 int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1) - 1]);
790 char ename[MAXPATHLEN + 1]; 788 char ename[MAXPATHLEN + 1];
791 789
792 if ((unsigned int)elen > MAXPATHLEN) { 790 if ((unsigned int)elen > MAXPATHLEN) {
793 fclose(arch); 791 fclose(arch);
794 return NULL; 792 return NULL;
795 } 793 }
796 if (fread(ename, (size_t)elen, 1, arch) != 1) { 794 if (fread(ename, (size_t)elen, 1, arch) != 1) {
797 fclose(arch); 795 fclose(arch);
798 return NULL; 796 return NULL;
799 } 797 }
800 ename[elen] = '\0'; 798 ename[elen] = '\0';
801 if (DEBUG(ARCH) || DEBUG(MAKE)) { 799 if (DEBUG(ARCH) || DEBUG(MAKE)) {
802 debug_printf("ArchFind: Extended format entry for %s\n", ename); 800 debug_printf("ArchFind: Extended format entry for %s\n", ename);
803 } 801 }
804 if (strncmp(ename, member, len) == 0) { 802 if (strncmp(ename, member, len) == 0) {
805 /* Found as extended name */ 803 /* Found as extended name */
806 if (fseek(arch, -(long)sizeof(struct ar_hdr) - elen, 804 if (fseek(arch, -(long)sizeof(struct ar_hdr) - elen,
807 SEEK_CUR) != 0) { 805 SEEK_CUR) != 0) {
808 fclose(arch); 806 fclose(arch);
809 return NULL; 807 return NULL;
810 } 808 }
811 return arch; 809 return arch;
812 } 810 }
813 if (fseek(arch, -elen, SEEK_CUR) != 0) { 811 if (fseek(arch, -elen, SEEK_CUR) != 0) {
814 fclose(arch); 812 fclose(arch);
815 return NULL; 813 return NULL;
816 } 814 }
817 } 815 }
818#endif 816#endif
819 817
820skip: 818skip:
821 /* 819 /*
822 * This isn't the member we're after, so we need to advance the 820 * This isn't the member we're after, so we need to advance the
823 * stream's pointer to the start of the next header. Files are 821 * stream's pointer to the start of the next header. Files are
824 * padded with newlines to an even-byte boundary, so we need to 822 * padded with newlines to an even-byte boundary, so we need to
825 * extract the size of the file from the 'size' field of the 823 * extract the size of the file from the 'size' field of the
826 * header and round it up during the seek. 824 * header and round it up during the seek.
827 */ 825 */
828 arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0'; 826 arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0';
829 size = (int)strtol(arhPtr->ar_size, NULL, 10); 827 size = (int)strtol(arhPtr->ar_size, NULL, 10);
830 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) { 828 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
831 fclose(arch); 829 fclose(arch);
832 return NULL; 830 return NULL;
833 } 831 }
834 } 832 }
835 833
836 /* 834 /*
837 * We've looked everywhere, but the member is not to be found. Close the 835 * We've looked everywhere, but the member is not to be found. Close the
838 * archive and return NULL -- an error. 836 * archive and return NULL -- an error.
839 */ 837 */
840 fclose(arch); 838 fclose(arch);
841 return NULL; 839 return NULL;
842} 840}
843 841
844/*- 842/*-
845 *----------------------------------------------------------------------- 843 *-----------------------------------------------------------------------
846 * Arch_Touch -- 844 * Arch_Touch --
847 * Touch a member of an archive. 845 * Touch a member of an archive.
848 * The modification time of the entire archive is also changed. 846 * The modification time of the entire archive is also changed.
849 * For a library, this could necessitate the re-ranlib'ing of the 847 * For a library, this could necessitate the re-ranlib'ing of the
850 * whole thing. 848 * whole thing.
851 * 849 *
852 * Input: 850 * Input:
853 * gn Node of member to touch 851 * gn Node of member to touch
854 * 852 *
855 * Results: 853 * Results:
856 * The 'time' field of the member's header is updated. 854 * The 'time' field of the member's header is updated.
857 *----------------------------------------------------------------------- 855 *-----------------------------------------------------------------------
858 */ 856 */
859void 857void
860Arch_Touch(GNode *gn) 858Arch_Touch(GNode *gn)
861{ 859{
862 FILE *arch; /* Stream open to archive, positioned properly */ 860 FILE *arch; /* Stream open to archive, positioned properly */
863 struct ar_hdr arh; /* Current header describing member */ 861 struct ar_hdr arh; /* Current header describing member */
864 862
865 arch = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn), 863 arch = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn),
866 &arh, "r+"); 864 &arh, "r+");
867 865
868 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now); 866 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now);
869 867
870 if (arch != NULL) { 868 if (arch != NULL) {
871 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 869 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
872 fclose(arch); 870 fclose(arch);
873 } 871 }
874} 872}
875 873
876/* Given a node which represents a library, touch the thing, making sure that 874/* Given a node which represents a library, touch the thing, making sure that
877 * the table of contents also is touched. 875 * the table of contents also is touched.
878 * 876 *
879 * Both the modification time of the library and of the RANLIBMAG member are 877 * Both the modification time of the library and of the RANLIBMAG member are
880 * set to 'now'. 878 * set to 'now'.
881 * 879 *
882 * Input: 880 * Input:
883 * gn The node of the library to touch 881 * gn The node of the library to touch
884 */ 882 */
885void 883void
886Arch_TouchLib(GNode *gn) 884Arch_TouchLib(GNode *gn)
887{ 885{
888#ifdef RANLIBMAG 886#ifdef RANLIBMAG
889 FILE * arch; /* Stream open to archive */ 887 FILE * arch; /* Stream open to archive */
890 struct ar_hdr arh; /* Header describing table of contents */ 888 struct ar_hdr arh; /* Header describing table of contents */
891 struct utimbuf times; /* Times for utime() call */ 889 struct utimbuf times; /* Times for utime() call */
892 890
893 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+"); 891 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
894 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now); 892 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
895 893
896 if (arch != NULL) { 894 if (arch != NULL) {
897 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch); 895 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
898 fclose(arch); 896 fclose(arch);
899 897
900 times.actime = times.modtime = now; 898 times.actime = times.modtime = now;
901 utime(gn->path, &times); 899 utime(gn->path, &times);
902 } 900 }
903#else 901#else
904 (void)gn; 902 (void)gn;
905#endif 903#endif
906} 904}
907 905
908/* Return the modification time of a member of an archive. The mtime field 906/* Return the modification time of a member of an archive. The mtime field
909 * of the given node is filled in with the value returned by the function. 907 * of the given node is filled in with the value returned by the function.
910 * 908 *
911 * Input: 909 * Input:
912 * gn Node describing archive member 910 * gn Node describing archive member
913 */ 911 */
914time_t 912time_t
915Arch_MTime(GNode *gn) 913Arch_MTime(GNode *gn)
916{ 914{
917 struct ar_hdr *arhPtr; /* Header of desired member */ 915 struct ar_hdr *arhPtr; /* Header of desired member */
918 time_t modTime; /* Modification time as an integer */ 916 time_t modTime; /* Modification time as an integer */
919 917
920 arhPtr = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), TRUE); 918 arhPtr = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), TRUE);
921 if (arhPtr != NULL) { 919 if (arhPtr != NULL) {
922 modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10); 920 modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
923 } else { 921 } else {
924 modTime = 0; 922 modTime = 0;
925 } 923 }
926 924
927 gn->mtime = modTime; 925 gn->mtime = modTime;
928 return modTime; 926 return modTime;
929} 927}
930 928
931/* Given a non-existent archive member's node, get its modification time from 929/* Given a non-existent archive member's node, get its modification time from
932 * its archived form, if it exists. gn->mtime is filled in as well. */ 930 * its archived form, if it exists. gn->mtime is filled in as well. */
933time_t 931time_t
934Arch_MemMTime(GNode *gn) 932Arch_MemMTime(GNode *gn)
935{ 933{
936 GNodeListNode *ln; 934 GNodeListNode *ln;
937 935
938 for (ln = gn->parents->first; ln != NULL; ln = ln->next) { 936 for (ln = gn->parents->first; ln != NULL; ln = ln->next) {
939 GNode *pgn = ln->datum; 937 GNode *pgn = ln->datum;
940 938
941 if (pgn->type & OP_ARCHV) { 939 if (pgn->type & OP_ARCHV) {
942 /* 940 /*
943 * If the parent is an archive specification and is being made 941 * If the parent is an archive specification and is being made
944 * and its member's name matches the name of the node we were 942 * and its member's name matches the name of the node we were
945 * given, record the modification time of the parent in the 943 * given, record the modification time of the parent in the
946 * child. We keep searching its parents in case some other 944 * child. We keep searching its parents in case some other
947 * parent requires this child to exist... 945 * parent requires this child to exist...
948 */ 946 */
949 const char *nameStart = strchr(pgn->name, '(') + 1; 947 const char *nameStart = strchr(pgn->name, '(') + 1;
950 const char *nameEnd = strchr(nameStart, ')'); 948 const char *nameEnd = strchr(nameStart, ')');
951 size_t nameLen = (size_t)(nameEnd - nameStart); 949 size_t nameLen = (size_t)(nameEnd - nameStart);
952 950
953 if ((pgn->flags & REMAKE) && 951 if ((pgn->flags & REMAKE) &&
954 strncmp(nameStart, gn->name, nameLen) == 0) { 952 strncmp(nameStart, gn->name, nameLen) == 0) {
955 gn->mtime = Arch_MTime(pgn); 953 gn->mtime = Arch_MTime(pgn);
956 } 954 }
957 } else if (pgn->flags & REMAKE) { 955 } else if (pgn->flags & REMAKE) {
958 /* 956 /*
959 * Something which isn't a library depends on the existence of 957 * Something which isn't a library depends on the existence of
960 * this target, so it needs to exist. 958 * this target, so it needs to exist.
961 */ 959 */
962 gn->mtime = 0; 960 gn->mtime = 0;
963 break; 961 break;
964 } 962 }
965 } 963 }
966 964
967 return gn->mtime; 965 return gn->mtime;
968} 966}
969 967
970/* Search for a library along the given search path. 968/* Search for a library along the given search path.
971 * 969 *
972 * The node's 'path' field is set to the found path (including the 970 * The node's 'path' field is set to the found path (including the
973 * actual file name, not -l...). If the system can handle the -L 971 * actual file name, not -l...). If the system can handle the -L
974 * flag when linking (or we cannot find the library), we assume that 972 * flag when linking (or we cannot find the library), we assume that
975 * the user has placed the .LIBS variable in the final linking 973 * the user has placed the .LIBS variable in the final linking
976 * command (or the linker will know where to find it) and set the 974 * command (or the linker will know where to find it) and set the
977 * TARGET variable for this node to be the node's name. Otherwise, 975 * TARGET variable for this node to be the node's name. Otherwise,
978 * we set the TARGET variable to be the full path of the library, 976 * we set the TARGET variable to be the full path of the library,
979 * as returned by Dir_FindFile. 977 * as returned by Dir_FindFile.
980 * 978 *
981 * Input: 979 * Input:
982 * gn Node of library to find 980 * gn Node of library to find
983 */ 981 */
984void 982void
985Arch_FindLib(GNode *gn, SearchPath *path) 983Arch_FindLib(GNode *gn, SearchPath *path)
986{ 984{
987 char *libName = str_concat3("lib", gn->name + 2, ".a"); 985 char *libName = str_concat3("lib", gn->name + 2, ".a");
988 gn->path = Dir_FindFile(libName, path); 986 gn->path = Dir_FindFile(libName, path);
989 free(libName); 987 free(libName);
990 988
991#ifdef LIBRARIES 989#ifdef LIBRARIES
992 Var_Set(TARGET, gn->name, gn); 990 Var_Set(TARGET, gn->name, gn);
993#else 991#else
994 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn); 992 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
995#endif 993#endif
996} 994}
997 995
998/* Decide if a node with the OP_LIB attribute is out-of-date. Called from 996/* Decide if a node with the OP_LIB attribute is out-of-date. Called from
999 * Make_OODate to make its life easier. 997 * Make_OODate to make its life easier.
1000 * The library will be hashed if it hasn't been already. 998 * The library will be hashed if it hasn't been already.
1001 * 999 *
1002 * There are several ways for a library to be out-of-date that are 1000 * There are several ways for a library to be out-of-date that are
1003 * not available to ordinary files. In addition, there are ways 1001 * not available to ordinary files. In addition, there are ways
1004 * that are open to regular files that are not available to 1002 * that are open to regular files that are not available to
1005 * libraries. A library that is only used as a source is never 1003 * libraries. A library that is only used as a source is never
1006 * considered out-of-date by itself. This does not preclude the 1004 * considered out-of-date by itself. This does not preclude the
1007 * library's modification time from making its parent be out-of-date. 1005 * library's modification time from making its parent be out-of-date.
1008 * A library will be considered out-of-date for any of these reasons, 1006 * A library will be considered out-of-date for any of these reasons,
1009 * given that it is a target on a dependency line somewhere: 1007 * given that it is a target on a dependency line somewhere:
1010 * 1008 *
1011 * Its modification time is less than that of one of its sources 1009 * Its modification time is less than that of one of its sources
1012 * (gn->mtime < gn->youngestChild->mtime). 1010 * (gn->mtime < gn->youngestChild->mtime).
1013 * 1011 *
1014 * Its modification time is greater than the time at which the make 1012 * Its modification time is greater than the time at which the make
1015 * began (i.e. it's been modified in the course of the make, probably 1013 * began (i.e. it's been modified in the course of the make, probably
1016 * by archiving). 1014 * by archiving).
1017 * 1015 *
1018 * The modification time of one of its sources is greater than the one 1016 * The modification time of one of its sources is greater than the one
1019 * of its RANLIBMAG member (i.e. its table of contents is out-of-date). 1017 * of its RANLIBMAG member (i.e. its table of contents is out-of-date).
1020 * We don't compare of the archive time vs. TOC time because they can be 1018 * We don't compare of the archive time vs. TOC time because they can be
1021 * too close. In my opinion we should not bother with the TOC at all 1019 * too close. In my opinion we should not bother with the TOC at all
1022 * since this is used by 'ar' rules that affect the data contents of the 1020 * since this is used by 'ar' rules that affect the data contents of the
1023 * archive, not by ranlib rules, which affect the TOC. 1021 * archive, not by ranlib rules, which affect the TOC.
1024 * 1022 *
1025 * Input: 1023 * Input:
1026 * gn The library's graph node 1024 * gn The library's graph node
1027 * 1025 *
1028 * Results: 1026 * Results:
1029 * TRUE if the library is out-of-date. FALSE otherwise. 1027 * TRUE if the library is out-of-date. FALSE otherwise.
1030 */ 1028 */
1031Boolean 1029Boolean
1032Arch_LibOODate(GNode *gn) 1030Arch_LibOODate(GNode *gn)
1033{ 1031{
1034 Boolean oodate; 1032 Boolean oodate;
1035 1033
1036 if (gn->type & OP_PHONY) { 1034 if (gn->type & OP_PHONY) {
1037 oodate = TRUE; 1035 oodate = TRUE;
1038 } else if (!GNode_IsTarget(gn) && Lst_IsEmpty(gn->children)) { 1036 } else if (!GNode_IsTarget(gn) && Lst_IsEmpty(gn->children)) {
1039 oodate = FALSE; 1037 oodate = FALSE;
1040 } else if ((!Lst_IsEmpty(gn->children) && gn->youngestChild == NULL) || 1038 } else if ((!Lst_IsEmpty(gn->children) && gn->youngestChild == NULL) ||
1041 (gn->mtime > now) || 1039 (gn->mtime > now) ||
1042 (gn->youngestChild != NULL && 1040 (gn->youngestChild != NULL &&
1043 gn->mtime < gn->youngestChild->mtime)) { 1041 gn->mtime < gn->youngestChild->mtime)) {
1044 oodate = TRUE; 1042 oodate = TRUE;
1045 } else { 1043 } else {
1046#ifdef RANLIBMAG 1044#ifdef RANLIBMAG
1047 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */ 1045 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1048 int modTimeTOC; /* The table-of-contents's mod time */ 1046 int modTimeTOC; /* The table-of-contents's mod time */
1049 1047
1050 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE); 1048 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1051 1049
1052 if (arhPtr != NULL) { 1050 if (arhPtr != NULL) {
1053 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10); 1051 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1054 1052
1055 if (DEBUG(ARCH) || DEBUG(MAKE)) { 1053 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1056 debug_printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC)); 1054 debug_printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1057 } 1055 }
1058 oodate = (gn->youngestChild == NULL || gn->youngestChild->mtime > modTimeTOC); 1056 oodate = (gn->youngestChild == NULL || gn->youngestChild->mtime > modTimeTOC);
1059 } else { 1057 } else {
1060 /* 1058 /*
1061 * A library w/o a table of contents is out-of-date 1059 * A library w/o a table of contents is out-of-date
1062 */ 1060 */
1063 if (DEBUG(ARCH) || DEBUG(MAKE)) { 1061 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1064 debug_printf("No t.o.c...."); 1062 debug_printf("No t.o.c....");
1065 } 1063 }
1066 oodate = TRUE; 1064 oodate = TRUE;
1067 } 1065 }
1068#else 1066#else
1069 oodate = FALSE; 1067 oodate = FALSE;
1070#endif 1068#endif
1071 } 1069 }
1072 return oodate; 1070 return oodate;
1073} 1071}
1074 1072
1075/* Initialize the archives module. */ 1073/* Initialize the archives module. */
1076void 1074void
1077Arch_Init(void) 1075Arch_Init(void)
1078{ 1076{
1079 archives = Lst_New(); 1077 archives = Lst_New();
1080} 1078}
1081 1079
1082/* Clean up the archives module. */ 1080/* Clean up the archives module. */
1083void 1081void
1084Arch_End(void) 1082Arch_End(void)
1085{ 1083{
1086#ifdef CLEANUP 1084#ifdef CLEANUP
1087 Lst_Destroy(archives, ArchFree); 1085 Lst_Destroy(archives, ArchFree);
1088#endif 1086#endif
1089} 1087}
1090 1088
1091Boolean 1089Boolean
1092Arch_IsLib(GNode *gn) 1090Arch_IsLib(GNode *gn)
1093{ 1091{
1094 static const char armag[] = "!<arch>\n"; 1092 static const char armag[] = "!<arch>\n";
1095 char buf[sizeof armag - 1]; 1093 char buf[sizeof armag - 1];
1096 int fd; 1094 int fd;
1097 1095
1098 if ((fd = open(gn->path, O_RDONLY)) == -1) 1096 if ((fd = open(gn->path, O_RDONLY)) == -1)
1099 return FALSE; 1097 return FALSE;
1100 1098
1101 if (read(fd, buf, sizeof buf) != sizeof buf) { 1099 if (read(fd, buf, sizeof buf) != sizeof buf) {
1102 (void)close(fd); 1100 (void)close(fd);
1103 return FALSE; 1101 return FALSE;
1104 } 1102 }
1105 1103
1106 (void)close(fd); 1104 (void)close(fd);
1107 1105
1108 return memcmp(buf, armag, sizeof buf) == 0; 1106 return memcmp(buf, armag, sizeof buf) == 0;
1109} 1107}