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