Home | History | Annotate | Line # | Download | only in make
arch.c revision 1.4
      1 /*
      2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      3  * Copyright (c) 1988, 1989 by Adam de Boor
      4  * Copyright (c) 1989 by Berkeley Softworks
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 /*static char sccsid[] = "from: @(#)arch.c	5.7 (Berkeley) 12/28/90";*/
     41 static char rcsid[] = "$Id: arch.c,v 1.4 1994/01/13 21:01:40 jtc Exp $";
     42 #endif /* not lint */
     43 
     44 /*-
     45  * arch.c --
     46  *	Functions to manipulate libraries, archives and their members.
     47  *
     48  *	Once again, cacheing/hashing comes into play in the manipulation
     49  * of archives. The first time an archive is referenced, all of its members'
     50  * headers are read and hashed and the archive closed again. All hashed
     51  * archives are kept on a list which is searched each time an archive member
     52  * is referenced.
     53  *
     54  * The interface to this module is:
     55  *	Arch_ParseArchive   	Given an archive specification, return a list
     56  *	    	  	    	of GNode's, one for each member in the spec.
     57  *	    	  	    	FAILURE is returned if the specification is
     58  *	    	  	    	invalid for some reason.
     59  *
     60  *	Arch_Touch	    	Alter the modification time of the archive
     61  *	    	  	    	member described by the given node to be
     62  *	    	  	    	the current time.
     63  *
     64  *	Arch_TouchLib	    	Update the modification time of the library
     65  *	    	  	    	described by the given node. This is special
     66  *	    	  	    	because it also updates the modification time
     67  *	    	  	    	of the library's table of contents.
     68  *
     69  *	Arch_MTime	    	Find the modification time of a member of
     70  *	    	  	    	an archive *in the archive*. The time is also
     71  *	    	  	    	placed in the member's GNode. Returns the
     72  *	    	  	    	modification time.
     73  *
     74  *	Arch_MemTime	    	Find the modification time of a member of
     75  *	    	  	    	an archive. Called when the member doesn't
     76  *	    	  	    	already exist. Looks in the archive for the
     77  *	    	  	    	modification time. Returns the modification
     78  *	    	  	    	time.
     79  *
     80  *	Arch_FindLib	    	Search for a library along a path. The
     81  *	    	  	    	library name in the GNode should be in
     82  *	    	  	    	-l<name> format.
     83  *
     84  *	Arch_LibOODate	    	Special function to decide if a library node
     85  *	    	  	    	is out-of-date.
     86  *
     87  *	Arch_Init 	    	Initialize this module.
     88  */
     89 
     90 #include    <sys/types.h>
     91 #include    <sys/stat.h>
     92 #include    <sys/time.h>
     93 #include    <ctype.h>
     94 #include    <ar.h>
     95 #include    <ranlib.h>
     96 #include    <stdio.h>
     97 #include    <stdlib.h>
     98 #include    "make.h"
     99 #include    "hash.h"
    100 
    101 #ifndef RANLIBMAG
    102 #define RANLIBMAG  "__.SYMDEF"
    103 #endif
    104 
    105 static Lst	  archives;   /* Lst of archives we've already examined */
    106 
    107 typedef struct Arch {
    108     char	  *name;      /* Name of archive */
    109     Hash_Table	  members;    /* All the members of the archive described
    110 			       * by <name, struct ar_hdr *> key/value pairs */
    111 } Arch;
    112 
    113 static FILE *ArchFindMember();
    114 
    115 /*-
    116  *-----------------------------------------------------------------------
    117  * Arch_ParseArchive --
    118  *	Parse the archive specification in the given line and find/create
    119  *	the nodes for the specified archive members, placing their nodes
    120  *	on the given list.
    121  *
    122  * Results:
    123  *	SUCCESS if it was a valid specification. The linePtr is updated
    124  *	to point to the first non-space after the archive spec. The
    125  *	nodes for the members are placed on the given list.
    126  *
    127  * Side Effects:
    128  *	Some nodes may be created. The given list is extended.
    129  *
    130  *-----------------------------------------------------------------------
    131  */
    132 ReturnStatus
    133 Arch_ParseArchive (linePtr, nodeLst, ctxt)
    134     char	    **linePtr;      /* Pointer to start of specification */
    135     Lst	    	    nodeLst;   	    /* Lst on which to place the nodes */
    136     GNode   	    *ctxt;  	    /* Context in which to expand variables */
    137 {
    138     register char   *cp;	    /* Pointer into line */
    139     GNode	    *gn;     	    /* New node */
    140     char	    *libName;  	    /* Library-part of specification */
    141     char	    *memName;  	    /* Member-part of specification */
    142     char	    nameBuf[BSIZE]; /* temporary place for node name */
    143     char	    saveChar;  	    /* Ending delimiter of member-name */
    144     Boolean 	    subLibName;	    /* TRUE if libName should have/had
    145 				     * variable substitution performed on it */
    146 
    147     libName = *linePtr;
    148 
    149     subLibName = FALSE;
    150 
    151     for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
    152 	if (*cp == '$') {
    153 	    /*
    154 	     * Variable spec, so call the Var module to parse the puppy
    155 	     * so we can safely advance beyond it...
    156 	     */
    157 	    int 	length;
    158 	    Boolean	freeIt;
    159 	    char	*result;
    160 
    161 	    result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
    162 	    if (result == var_Error) {
    163 		return(FAILURE);
    164 	    } else {
    165 		subLibName = TRUE;
    166 	    }
    167 
    168 	    if (freeIt) {
    169 		free(result);
    170 	    }
    171 	    cp += length-1;
    172 	}
    173     }
    174 
    175     *cp++ = '\0';
    176     if (subLibName) {
    177 	libName = Var_Subst(libName, ctxt, TRUE);
    178     }
    179 
    180 
    181     while (1) {
    182 	/*
    183 	 * First skip to the start of the member's name, mark that
    184 	 * place and skip to the end of it (either white-space or
    185 	 * a close paren).
    186 	 */
    187 	Boolean	doSubst = FALSE; /* TRUE if need to substitute in memName */
    188 
    189 	while (*cp != '\0' && *cp != ')' && isspace (*cp)) {
    190 	    cp++;
    191 	}
    192 	memName = cp;
    193 	while (*cp != '\0' && *cp != ')' && !isspace (*cp)) {
    194 	    if (*cp == '$') {
    195 		/*
    196 		 * Variable spec, so call the Var module to parse the puppy
    197 		 * so we can safely advance beyond it...
    198 		 */
    199 		int 	length;
    200 		Boolean	freeIt;
    201 		char	*result;
    202 
    203 		result=Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
    204 		if (result == var_Error) {
    205 		    return(FAILURE);
    206 		} else {
    207 		    doSubst = TRUE;
    208 		}
    209 
    210 		if (freeIt) {
    211 		    free(result);
    212 		}
    213 		cp += length;
    214 	    } else {
    215 		cp++;
    216 	    }
    217 	}
    218 
    219 	/*
    220 	 * If the specification ends without a closing parenthesis,
    221 	 * chances are there's something wrong (like a missing backslash),
    222 	 * so it's better to return failure than allow such things to happen
    223 	 */
    224 	if (*cp == '\0') {
    225 	    printf("No closing parenthesis in archive specification\n");
    226 	    return (FAILURE);
    227 	}
    228 
    229 	/*
    230 	 * If we didn't move anywhere, we must be done
    231 	 */
    232 	if (cp == memName) {
    233 	    break;
    234 	}
    235 
    236 	saveChar = *cp;
    237 	*cp = '\0';
    238 
    239 	/*
    240 	 * XXX: This should be taken care of intelligently by
    241 	 * SuffExpandChildren, both for the archive and the member portions.
    242 	 */
    243 	/*
    244 	 * If member contains variables, try and substitute for them.
    245 	 * This will slow down archive specs with dynamic sources, of course,
    246 	 * since we'll be (non-)substituting them three times, but them's
    247 	 * the breaks -- we need to do this since SuffExpandChildren calls
    248 	 * us, otherwise we could assume the thing would be taken care of
    249 	 * later.
    250 	 */
    251 	if (doSubst) {
    252 	    char    *buf;
    253 	    char    *sacrifice;
    254 	    char    *oldMemName = memName;
    255 
    256 	    memName = Var_Subst(memName, ctxt, TRUE);
    257 
    258 	    /*
    259 	     * Now form an archive spec and recurse to deal with nested
    260 	     * variables and multi-word variable values.... The results
    261 	     * are just placed at the end of the nodeLst we're returning.
    262 	     */
    263 	    buf = sacrifice = emalloc(strlen(memName)+strlen(libName)+3);
    264 
    265 	    sprintf(buf, "%s(%s)", libName, memName);
    266 
    267 	    if (index(memName, '$') && strcmp(memName, oldMemName) == 0) {
    268 		/*
    269 		 * Must contain dynamic sources, so we can't deal with it now.
    270 		 * Just create an ARCHV node for the thing and let
    271 		 * SuffExpandChildren handle it...
    272 		 */
    273 		gn = Targ_FindNode(buf, TARG_CREATE);
    274 
    275 		if (gn == NILGNODE) {
    276 		    free(buf);
    277 		    return(FAILURE);
    278 		} else {
    279 		    gn->type |= OP_ARCHV;
    280 		    (void)Lst_AtEnd(nodeLst, (ClientData)gn);
    281 		}
    282 	    } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
    283 		/*
    284 		 * Error in nested call -- free buffer and return FAILURE
    285 		 * ourselves.
    286 		 */
    287 		free(buf);
    288 		return(FAILURE);
    289 	    }
    290 	    /*
    291 	     * Free buffer and continue with our work.
    292 	     */
    293 	    free(buf);
    294 	} else if (Dir_HasWildcards(memName)) {
    295 	    Lst	  members = Lst_Init(FALSE);
    296 	    char  *member;
    297 
    298 	    Dir_Expand(memName, dirSearchPath, members);
    299 	    while (!Lst_IsEmpty(members)) {
    300 		member = (char *)Lst_DeQueue(members);
    301 
    302 		sprintf(nameBuf, "%s(%s)", libName, member);
    303 		free(member);
    304 		gn = Targ_FindNode (nameBuf, TARG_CREATE);
    305 		if (gn == NILGNODE) {
    306 		    return (FAILURE);
    307 		} else {
    308 		    /*
    309 		     * We've found the node, but have to make sure the rest of
    310 		     * the world knows it's an archive member, without having
    311 		     * to constantly check for parentheses, so we type the
    312 		     * thing with the OP_ARCHV bit before we place it on the
    313 		     * end of the provided list.
    314 		     */
    315 		    gn->type |= OP_ARCHV;
    316 		    (void) Lst_AtEnd (nodeLst, (ClientData)gn);
    317 		}
    318 	    }
    319 	    Lst_Destroy(members, NOFREE);
    320 	} else {
    321 	    sprintf(nameBuf, "%s(%s)", libName, memName);
    322 	    gn = Targ_FindNode (nameBuf, TARG_CREATE);
    323 	    if (gn == NILGNODE) {
    324 		return (FAILURE);
    325 	    } else {
    326 		/*
    327 		 * We've found the node, but have to make sure the rest of the
    328 		 * world knows it's an archive member, without having to
    329 		 * constantly check for parentheses, so we type the thing with
    330 		 * the OP_ARCHV bit before we place it on the end of the
    331 		 * provided list.
    332 		 */
    333 		gn->type |= OP_ARCHV;
    334 		(void) Lst_AtEnd (nodeLst, (ClientData)gn);
    335 	    }
    336 	}
    337 	if (doSubst) {
    338 	    free(memName);
    339 	}
    340 
    341 	*cp = saveChar;
    342     }
    343 
    344     /*
    345      * If substituted libName, free it now, since we need it no longer.
    346      */
    347     if (subLibName) {
    348 	free(libName);
    349     }
    350 
    351     /*
    352      * We promised the pointer would be set up at the next non-space, so
    353      * we must advance cp there before setting *linePtr... (note that on
    354      * entrance to the loop, cp is guaranteed to point at a ')')
    355      */
    356     do {
    357 	cp++;
    358     } while (*cp != '\0' && isspace (*cp));
    359 
    360     *linePtr = cp;
    361     return (SUCCESS);
    362 }
    363 
    364 /*-
    365  *-----------------------------------------------------------------------
    366  * ArchFindArchive --
    367  *	See if the given archive is the one we are looking for. Called
    368  *	From ArchStatMember and ArchFindMember via Lst_Find.
    369  *
    370  * Results:
    371  *	0 if it is, non-zero if it isn't.
    372  *
    373  * Side Effects:
    374  *	None.
    375  *
    376  *-----------------------------------------------------------------------
    377  */
    378 static int
    379 ArchFindArchive (ar, archName)
    380     Arch	  *ar;	      	  /* Current list element */
    381     char	  *archName;  	  /* Name we want */
    382 {
    383     return (strcmp (archName, ar->name));
    384 }
    385 
    386 /*-
    387  *-----------------------------------------------------------------------
    388  * ArchStatMember --
    389  *	Locate a member of an archive, given the path of the archive and
    390  *	the path of the desired member.
    391  *
    392  * Results:
    393  *	A pointer to the current struct ar_hdr structure for the member. Note
    394  *	That no position is returned, so this is not useful for touching
    395  *	archive members. This is mostly because we have no assurances that
    396  *	The archive will remain constant after we read all the headers, so
    397  *	there's not much point in remembering the position...
    398  *
    399  * Side Effects:
    400  *
    401  *-----------------------------------------------------------------------
    402  */
    403 static struct ar_hdr *
    404 ArchStatMember (archive, member, hash)
    405     char	  *archive;   /* Path to the archive */
    406     char	  *member;    /* Name of member. If it is a path, only the
    407 			       * last component is used. */
    408     Boolean	  hash;	      /* TRUE if archive should be hashed if not
    409     			       * already so. */
    410 {
    411 #define AR_MAX_NAME_LEN	    (sizeof(arh.ar_name)-1)
    412     FILE *	  arch;	      /* Stream to archive */
    413     int		  size;       /* Size of archive member */
    414     char	  *cp;	      /* Useful character pointer */
    415     char	  magic[SARMAG];
    416     int		  len;
    417     LstNode	  ln;	      /* Lst member containing archive descriptor */
    418     Arch	  *ar;	      /* Archive descriptor */
    419     Hash_Entry	  *he;	      /* Entry containing member's description */
    420     struct ar_hdr arh;        /* archive-member header for reading archive */
    421     char	  memName[AR_MAX_NAME_LEN+1];
    422     	    	    	    /* Current member name while hashing. The name is
    423 			     * truncated to AR_MAX_NAME_LEN bytes, but we need
    424 			     * room for the null byte... */
    425     char    	  copy[AR_MAX_NAME_LEN+1];
    426     	    	    	    /* Holds copy of last path element from member, if
    427 			     * it has to be truncated, so we don't have to
    428 			     * figure it out again once the table is hashed. */
    429 
    430     /*
    431      * Because of space constraints and similar things, files are archived
    432      * using their final path components, not the entire thing, so we need
    433      * to point 'member' to the final component, if there is one, to make
    434      * the comparisons easier...
    435      */
    436     cp = rindex (member, '/');
    437     if (cp != (char *) NULL) {
    438 	member = cp + 1;
    439     }
    440     len = strlen (member);
    441     if (len > AR_MAX_NAME_LEN) {
    442 	len = AR_MAX_NAME_LEN;
    443 	strncpy(copy, member, AR_MAX_NAME_LEN);
    444 	copy[AR_MAX_NAME_LEN] = '\0';
    445 	member = copy;
    446     }
    447 
    448     ln = Lst_Find (archives, (ClientData) archive, ArchFindArchive);
    449     if (ln != NILLNODE) {
    450 	ar = (Arch *) Lst_Datum (ln);
    451 
    452 	he = Hash_FindEntry (&ar->members, member);
    453 
    454 	if (he != (Hash_Entry *) NULL) {
    455 	    return ((struct ar_hdr *) Hash_GetValue (he));
    456 	} else {
    457 	    return ((struct ar_hdr *) NULL);
    458 	}
    459     }
    460 
    461     if (!hash) {
    462 	/*
    463 	 * Caller doesn't want the thing hashed, just use ArchFindMember
    464 	 * to read the header for the member out and close down the stream
    465 	 * again. Since the archive is not to be hashed, we assume there's
    466 	 * no need to allocate extra room for the header we're returning,
    467 	 * so just declare it static.
    468 	 */
    469 	 static struct ar_hdr	sarh;
    470 
    471 	 arch = ArchFindMember(archive, member, &sarh, "r");
    472 
    473 	 if (arch == (FILE *)NULL) {
    474 	    return ((struct ar_hdr *)NULL);
    475 	} else {
    476 	    fclose(arch);
    477 	    return (&sarh);
    478 	}
    479     }
    480 
    481     /*
    482      * We don't have this archive on the list yet, so we want to find out
    483      * everything that's in it and cache it so we can get at it quickly.
    484      */
    485     arch = fopen (archive, "r");
    486     if (arch == (FILE *) NULL) {
    487 	return ((struct ar_hdr *) NULL);
    488     }
    489 
    490     /*
    491      * We use the ARMAG string to make sure this is an archive we
    492      * can handle...
    493      */
    494     if ((fread (magic, SARMAG, 1, arch) != 1) ||
    495     	(strncmp (magic, ARMAG, SARMAG) != 0)) {
    496 	    fclose (arch);
    497 	    return ((struct ar_hdr *) NULL);
    498     }
    499 
    500     ar = (Arch *)emalloc (sizeof (Arch));
    501     ar->name = strdup (archive);
    502     Hash_InitTable (&ar->members, -1);
    503     memName[AR_MAX_NAME_LEN] = '\0';
    504 
    505     while (fread ((char *)&arh, sizeof (struct ar_hdr), 1, arch) == 1) {
    506 	if (strncmp ( arh.ar_fmag, ARFMAG, sizeof (arh.ar_fmag)) != 0) {
    507 				 /*
    508 				  * The header is bogus, so the archive is bad
    509 				  * and there's no way we can recover...
    510 				  */
    511 				 fclose (arch);
    512 				 Hash_DeleteTable (&ar->members);
    513 				 free ((Address)ar);
    514 				 return ((struct ar_hdr *) NULL);
    515 	} else {
    516 	    (void) strncpy (memName, arh.ar_name, sizeof(arh.ar_name));
    517 	    for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
    518 		continue;
    519 	    }
    520 	    cp[1] = '\0';
    521 
    522 	    he = Hash_CreateEntry (&ar->members, strdup (memName),
    523 				   (Boolean *)NULL);
    524 	    Hash_SetValue (he, (ClientData)emalloc (sizeof (struct ar_hdr)));
    525 	    bcopy ((Address)&arh, (Address)Hash_GetValue (he),
    526 		sizeof (struct ar_hdr));
    527 	}
    528 	/*
    529 	 * We need to advance the stream's pointer to the start of the
    530 	 * next header. Files are padded with newlines to an even-byte
    531 	 * boundary, so we need to extract the size of the file from the
    532 	 * 'size' field of the header and round it up during the seek.
    533 	 */
    534 	arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
    535 	(void) sscanf (arh.ar_size, "%10d", &size);
    536 	fseek (arch, (size + 1) & ~1, 1);
    537     }
    538 
    539     fclose (arch);
    540 
    541     (void) Lst_AtEnd (archives, (ClientData) ar);
    542 
    543     /*
    544      * Now that the archive has been read and cached, we can look into
    545      * the hash table to find the desired member's header.
    546      */
    547     he = Hash_FindEntry (&ar->members, member);
    548 
    549     if (he != (Hash_Entry *) NULL) {
    550 	return ((struct ar_hdr *) Hash_GetValue (he));
    551     } else {
    552 	return ((struct ar_hdr *) NULL);
    553     }
    554 }
    555 
    556 /*-
    557  *-----------------------------------------------------------------------
    558  * ArchFindMember --
    559  *	Locate a member of an archive, given the path of the archive and
    560  *	the path of the desired member. If the archive is to be modified,
    561  *	the mode should be "r+", if not, it should be "r".
    562  *
    563  * Results:
    564  *	An FILE *, opened for reading and writing, positioned at the
    565  *	start of the member's struct ar_hdr, or NULL if the member was
    566  *	nonexistent. The current struct ar_hdr for member.
    567  *
    568  * Side Effects:
    569  *	The passed struct ar_hdr structure is filled in.
    570  *
    571  *-----------------------------------------------------------------------
    572  */
    573 static FILE *
    574 ArchFindMember (archive, member, arhPtr, mode)
    575     char	  *archive;   /* Path to the archive */
    576     char	  *member;    /* Name of member. If it is a path, only the
    577 			       * last component is used. */
    578     struct ar_hdr *arhPtr;    /* Pointer to header structure to be filled in */
    579     char	  *mode;      /* The mode for opening the stream */
    580 {
    581     FILE *	  arch;	      /* Stream to archive */
    582     int		  size;       /* Size of archive member */
    583     char	  *cp;	      /* Useful character pointer */
    584     char	  magic[SARMAG];
    585     int		  len;
    586 
    587     arch = fopen (archive, mode);
    588     if (arch == (FILE *) NULL) {
    589 	return ((FILE *) NULL);
    590     }
    591 
    592     /*
    593      * We use the ARMAG string to make sure this is an archive we
    594      * can handle...
    595      */
    596     if ((fread (magic, SARMAG, 1, arch) != 1) ||
    597     	(strncmp (magic, ARMAG, SARMAG) != 0)) {
    598 	    fclose (arch);
    599 	    return ((FILE *) NULL);
    600     }
    601 
    602     /*
    603      * Because of space constraints and similar things, files are archived
    604      * using their final path components, not the entire thing, so we need
    605      * to point 'member' to the final component, if there is one, to make
    606      * the comparisons easier...
    607      */
    608     cp = rindex (member, '/');
    609     if (cp != (char *) NULL) {
    610 	member = cp + 1;
    611     }
    612     len = strlen (member);
    613     if (len > sizeof (arhPtr->ar_name)) {
    614 	len = sizeof (arhPtr->ar_name);
    615     }
    616 
    617     while (fread ((char *)arhPtr, sizeof (struct ar_hdr), 1, arch) == 1) {
    618 	if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof (arhPtr->ar_fmag) ) != 0) {
    619 	     /*
    620 	      * The header is bogus, so the archive is bad
    621 	      * and there's no way we can recover...
    622 	      */
    623 	     fclose (arch);
    624 	     return ((FILE *) NULL);
    625 	} else if (strncmp (member, arhPtr->ar_name, len) == 0) {
    626 	    /*
    627 	     * If the member's name doesn't take up the entire 'name' field,
    628 	     * we have to be careful of matching prefixes. Names are space-
    629 	     * padded to the right, so if the character in 'name' at the end
    630 	     * of the matched string is anything but a space, this isn't the
    631 	     * member we sought.
    632 	     */
    633 	    if (len != sizeof(arhPtr->ar_name) && arhPtr->ar_name[len] != ' '){
    634 		continue;
    635 	    } else {
    636 		/*
    637 		 * To make life easier, we reposition the file at the start
    638 		 * of the header we just read before we return the stream.
    639 		 * In a more general situation, it might be better to leave
    640 		 * the file at the actual member, rather than its header, but
    641 		 * not here...
    642 		 */
    643 		fseek (arch, -sizeof(struct ar_hdr), 1);
    644 		return (arch);
    645 	    }
    646 	} else {
    647 	    /*
    648 	     * This isn't the member we're after, so we need to advance the
    649 	     * stream's pointer to the start of the next header. Files are
    650 	     * padded with newlines to an even-byte boundary, so we need to
    651 	     * extract the size of the file from the 'size' field of the
    652 	     * header and round it up during the seek.
    653 	     */
    654 	    arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
    655 	    (void)sscanf (arhPtr->ar_size, "%10d", &size);
    656 	    fseek (arch, (size + 1) & ~1, 1);
    657 	}
    658     }
    659 
    660     /*
    661      * We've looked everywhere, but the member is not to be found. Close the
    662      * archive and return NULL -- an error.
    663      */
    664     fclose (arch);
    665     return ((FILE *) NULL);
    666 }
    667 
    668 /*-
    669  *-----------------------------------------------------------------------
    670  * Arch_Touch --
    671  *	Touch a member of an archive.
    672  *
    673  * Results:
    674  *	The 'time' field of the member's header is updated.
    675  *
    676  * Side Effects:
    677  *	The modification time of the entire archive is also changed.
    678  *	For a library, this could necessitate the re-ranlib'ing of the
    679  *	whole thing.
    680  *
    681  *-----------------------------------------------------------------------
    682  */
    683 void
    684 Arch_Touch (gn)
    685     GNode	  *gn;	  /* Node of member to touch */
    686 {
    687     FILE *	  arch;	  /* Stream open to archive, positioned properly */
    688     struct ar_hdr arh;	  /* Current header describing member */
    689 
    690     arch = ArchFindMember(Var_Value (ARCHIVE, gn),
    691 			  Var_Value (TARGET, gn),
    692 			  &arh, "r+");
    693     sprintf(arh.ar_date, "%-12d", now);
    694 
    695     if (arch != (FILE *) NULL) {
    696 	(void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
    697 	fclose (arch);
    698     }
    699 }
    700 
    701 /*-
    702  *-----------------------------------------------------------------------
    703  * Arch_TouchLib --
    704  *	Given a node which represents a library, touch the thing, making
    705  *	sure that the table of contents also is touched.
    706  *
    707  * Results:
    708  *	None.
    709  *
    710  * Side Effects:
    711  *	Both the modification time of the library and of the RANLIBMAG
    712  *	member are set to 'now'.
    713  *
    714  *-----------------------------------------------------------------------
    715  */
    716 void
    717 Arch_TouchLib (gn)
    718     GNode	    *gn;      	/* The node of the library to touch */
    719 {
    720     FILE *	    arch;	/* Stream open to archive */
    721     struct ar_hdr   arh;      	/* Header describing table of contents */
    722     struct timeval  times[2];	/* Times for utimes() call */
    723 
    724     arch = ArchFindMember (gn->path, RANLIBMAG, &arh, "r+");
    725     sprintf(arh.ar_date, "%-12d", now);
    726 
    727     if (arch != (FILE *) NULL) {
    728 	(void)fwrite ((char *)&arh, sizeof (struct ar_hdr), 1, arch);
    729 	fclose (arch);
    730 
    731 	times[0].tv_sec = times[1].tv_sec = now;
    732 	times[0].tv_usec = times[1].tv_usec = 0;
    733 	utimes(gn->path, times);
    734     }
    735 }
    736 
    737 /*-
    738  *-----------------------------------------------------------------------
    739  * Arch_MTime --
    740  *	Return the modification time of a member of an archive.
    741  *
    742  * Results:
    743  *	The modification time (seconds).
    744  *
    745  * Side Effects:
    746  *	The mtime field of the given node is filled in with the value
    747  *	returned by the function.
    748  *
    749  *-----------------------------------------------------------------------
    750  */
    751 int
    752 Arch_MTime (gn)
    753     GNode	  *gn;	      /* Node describing archive member */
    754 {
    755     struct ar_hdr *arhPtr;    /* Header of desired member */
    756     int		  modTime;    /* Modification time as an integer */
    757 
    758     arhPtr = ArchStatMember (Var_Value (ARCHIVE, gn),
    759 			     Var_Value (TARGET, gn),
    760 			     TRUE);
    761     if (arhPtr != (struct ar_hdr *) NULL) {
    762 	(void)sscanf (arhPtr->ar_date, "%12d", &modTime);
    763     } else {
    764 	modTime = 0;
    765     }
    766 
    767     gn->mtime = modTime;
    768     return (modTime);
    769 }
    770 
    771 /*-
    772  *-----------------------------------------------------------------------
    773  * Arch_MemMTime --
    774  *	Given a non-existent archive member's node, get its modification
    775  *	time from its archived form, if it exists.
    776  *
    777  * Results:
    778  *	The modification time.
    779  *
    780  * Side Effects:
    781  *	The mtime field is filled in.
    782  *
    783  *-----------------------------------------------------------------------
    784  */
    785 int
    786 Arch_MemMTime (gn)
    787     GNode   	  *gn;
    788 {
    789     LstNode 	  ln;
    790     GNode   	  *pgn;
    791     char    	  *nameStart,
    792 		  *nameEnd;
    793 
    794     if (Lst_Open (gn->parents) != SUCCESS) {
    795 	gn->mtime = 0;
    796 	return (0);
    797     }
    798     while ((ln = Lst_Next (gn->parents)) != NILLNODE) {
    799 	pgn = (GNode *) Lst_Datum (ln);
    800 
    801 	if (pgn->type & OP_ARCHV) {
    802 	    /*
    803 	     * If the parent is an archive specification and is being made
    804 	     * and its member's name matches the name of the node we were
    805 	     * given, record the modification time of the parent in the
    806 	     * child. We keep searching its parents in case some other
    807 	     * parent requires this child to exist...
    808 	     */
    809 	    nameStart = index (pgn->name, '(') + 1;
    810 	    nameEnd = index (nameStart, ')');
    811 
    812 	    if (pgn->make &&
    813 		strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
    814 				     gn->mtime = Arch_MTime(pgn);
    815 	    }
    816 	} else if (pgn->make) {
    817 	    /*
    818 	     * Something which isn't a library depends on the existence of
    819 	     * this target, so it needs to exist.
    820 	     */
    821 	    gn->mtime = 0;
    822 	    break;
    823 	}
    824     }
    825 
    826     Lst_Close (gn->parents);
    827 
    828     return (gn->mtime);
    829 }
    830 
    831 /*-
    832  *-----------------------------------------------------------------------
    833  * Arch_FindLib --
    834  *	Search for a library along the given search path.
    835  *
    836  * Results:
    837  *	None.
    838  *
    839  * Side Effects:
    840  *	The node's 'path' field is set to the found path (including the
    841  *	actual file name, not -l...). If the system can handle the -L
    842  *	flag when linking (or we cannot find the library), we assume that
    843  *	the user has placed the .LIBRARIES variable in the final linking
    844  *	command (or the linker will know where to find it) and set the
    845  *	TARGET variable for this node to be the node's name. Otherwise,
    846  *	we set the TARGET variable to be the full path of the library,
    847  *	as returned by Dir_FindFile.
    848  *
    849  *-----------------------------------------------------------------------
    850  */
    851 void
    852 Arch_FindLib (gn, path)
    853     GNode	    *gn;	      /* Node of library to find */
    854     Lst	    	    path;	      /* Search path */
    855 {
    856     char	    *libName;   /* file name for archive */
    857 
    858     libName = (char *)emalloc (strlen (gn->name) + 6 - 2);
    859     sprintf(libName, "lib%s.a", &gn->name[2]);
    860 
    861     gn->path = Dir_FindFile (libName, path);
    862 
    863     free (libName);
    864 
    865 #ifdef LIBRARIES
    866     Var_Set (TARGET, gn->name, gn);
    867 #else
    868     Var_Set (TARGET, gn->path == (char *) NULL ? gn->name : gn->path, gn);
    869 #endif LIBRARIES
    870 }
    871 
    872 /*-
    873  *-----------------------------------------------------------------------
    874  * Arch_LibOODate --
    875  *	Decide if a node with the OP_LIB attribute is out-of-date. Called
    876  *	from Make_OODate to make its life easier.
    877  *
    878  *	There are several ways for a library to be out-of-date that are
    879  *	not available to ordinary files. In addition, there are ways
    880  *	that are open to regular files that are not available to
    881  *	libraries. A library that is only used as a source is never
    882  *	considered out-of-date by itself. This does not preclude the
    883  *	library's modification time from making its parent be out-of-date.
    884  *	A library will be considered out-of-date for any of these reasons,
    885  *	given that it is a target on a dependency line somewhere:
    886  *	    Its modification time is less than that of one of its
    887  *	    	  sources (gn->mtime < gn->cmtime).
    888  *	    Its modification time is greater than the time at which the
    889  *	    	  make began (i.e. it's been modified in the course
    890  *	    	  of the make, probably by archiving).
    891  *	    Its modification time doesn't agree with the modification
    892  *	    	  time of its RANLIBMAG member (i.e. its table of contents
    893  *	    	  is out-of-date).
    894  *
    895  *
    896  * Results:
    897  *	TRUE if the library is out-of-date. FALSE otherwise.
    898  *
    899  * Side Effects:
    900  *	The library will be hashed if it hasn't been already.
    901  *
    902  *-----------------------------------------------------------------------
    903  */
    904 Boolean
    905 Arch_LibOODate (gn)
    906     GNode   	  *gn;  	/* The library's graph node */
    907 {
    908     Boolean 	  oodate;
    909 
    910     if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
    911 	oodate = FALSE;
    912     } else if ((gn->mtime > now) || (gn->mtime < gn->cmtime)) {
    913 	oodate = TRUE;
    914     } else {
    915 	struct ar_hdr  	*arhPtr;    /* Header for __.SYMDEF */
    916 	int 	  	modTimeTOC; /* The table-of-contents's mod time */
    917 
    918 	arhPtr = ArchStatMember (gn->path, RANLIBMAG, FALSE);
    919 
    920 	if (arhPtr != (struct ar_hdr *)NULL) {
    921 	    (void)sscanf (arhPtr->ar_date, "%12d", &modTimeTOC);
    922 
    923 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
    924 		printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
    925 	    }
    926 	    oodate = (gn->mtime > modTimeTOC);
    927 	} else {
    928 	    /*
    929 	     * A library w/o a table of contents is out-of-date
    930 	     */
    931 	    if (DEBUG(ARCH) || DEBUG(MAKE)) {
    932 		printf("No t.o.c....");
    933 	    }
    934 	    oodate = TRUE;
    935 	}
    936     }
    937     return (oodate);
    938 }
    939 
    940 /*-
    941  *-----------------------------------------------------------------------
    942  * Arch_Init --
    943  *	Initialize things for this module.
    944  *
    945  * Results:
    946  *	None.
    947  *
    948  * Side Effects:
    949  *	The 'archives' list is initialized.
    950  *
    951  *-----------------------------------------------------------------------
    952  */
    953 void
    954 Arch_Init ()
    955 {
    956     archives = Lst_Init (FALSE);
    957 }
    958