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