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