summaryrefslogtreecommitdiff
path: root/dmake/unix/arlib.c
blob: 3a667f08188fbe483b564b16f00553f34c0a9650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/* $RCSfile: arlib.c,v $
-- $Revision: 1.5 $
-- last change: $Author: hr $ $Date: 2006-04-20 12:18:37 $
--
-- SYNOPSIS
--      Unix archive manipulation code.
--
-- DESCRIPTION
--  Originally this code was provided by Eric Gisin of MKS.  I took
--  his code and completely rewrote it adding cacheing of lib members
--  and other various optimizations.  I kept the overal functional
--  idea of the library routines as they are similar to those in GNU
--  make and felt it advantageous to maintain a similar interface.
--
-- AUTHOR
--      Dennis Vadura, dvadura@dmake.wticorp.com
--
-- WWW
--      http://dmake.wticorp.com/
--
-- COPYRIGHT
--      Copyright (c) 1996,1997 by WTI Corp.  All rights reserved.
--
--      This program is NOT free software; you can redistribute it and/or
--      modify it under the terms of the Software License Agreement Provided
--      in the file <distribution-root>/readme/license.txt.
--
-- LOG
--      Use cvs log to obtain detailed change logs.
*/

/* Sun unix on 386i's has a broken ar.h that does not assume PORTAR format
 * by default, so we fix it here. */
#if defined(i386) || defined(__DGUX__)
#define PORTAR 1
#endif

#if !defined (COHERENT) && !defined(__COHERENT__)
#include <ar.h>
#else
#include <arcoff.h>
#endif /* COHERENT, __COHERENT__ */
#include "extern.h"
#include "sysintf.h"

/* By defining the defines below it is possible to configure the library
 * code for library cacheing/non-cacheing, ASCII archive headers, and a full
 * decode of the ar_hdr fields in the scan_ar function. */

#ifndef ASCARCH
#define ASCARCH         1   /* ASCII time stored in archive */
#endif

#ifndef LC
#define LC          1   /* Turn on library cacheing */
#endif

#ifndef CHECKELF
#define CHECKELF        1   /* Enable Elf long member names */
#endif

#ifndef DECODE_ALL_AR_FIELDS
#define DECODE_ALL_AR_FIELDS    0   /* decode only fields make needs*/
#endif

#ifndef AR_TRUNCATE_MEMBER_NAMES
#define AR_TRUNCATE_MEMBER_NAMES 0  /* truncate member names for */
#endif                  /* comparison.               */

#if LC
#  define FOUND_MEMBER  FALSE
#else
#  define FOUND_MEMBER  TRUE
#  define _cache_member(a, b, c)
#  define _check_cache(a, b, c, d)  FALSE
#endif

#define MAXFNAME      255       /* Max length of member name    */
#define MAXMNAME    8   /* Max module name < MAXFNAME   */


/* This struct is used to pass the library and member inrmation about the
 * routines that perform the library seeking/cacheing */
struct ar_args {
   char   *lib;
   char   *member;
   time_t time;
};


typedef struct AR {
   char    ar_name[MAXFNAME+1];      /* File name */
   long    ar_size;                  /* Size in bytes */
   time_t  ar_time;                  /* Modification time */

#ifdef DOS
   char    ar_modname[MAXMNAME+1];   /* DOS module name */
#endif

#if DECODE_ALL_AR_FIELDS
   uint16  ar_mode;                  /* File mode */
   uint16  ar_uid;                   /* File owner */
   uint16  ar_gid;                   /* File group owner */
#endif
} AR, *ARPTR;


static int ar_scan  ANSI((FILE *,
              int (*) ANSI((FILE *, struct AR *,struct ar_args *)),
              struct ar_args *));
static int ar_touch ANSI(( FILE *, time_t ));
static int time_function  ANSI(( FILE *, struct AR *, struct ar_args * ));
static int touch_function ANSI(( FILE *, struct AR *, struct ar_args * ));
static int ar_name_equal ANSI((char *, char *));

#if LC
static int _cache_member ANSI((char *, char *, time_t));
static int _check_cache  ANSI((char *, char *, time_t *, int));
#endif

/* decoded archive header */
static AR    _ar;
static off_t arhdroffset;  /* member seek offset */


PUBLIC time_t
seek_arch(name, lib)/*
======================
   Look for module 'name' inside 'lib'.  If compiled with cacheing then first
   check to see if the specified lib is cached.  If so then return that time
   stamp instead of looking into the library. */
char    *name;
char    *lib;
{
   FILE   *f;
   int    rv;
   time_t mtime;
   struct ar_args args;

   /* Check the cache first (if there is a cache) */
   if( _check_cache(name, lib, &mtime, FALSE) )  return( mtime );

   /* Open the lib file and perform the scan of the members, looking
    * for our particular member.  If cacheing is enabled it will be
    * taken care of automatically during the scan. */

   args.lib    = lib;
   args.member = name;
   args.time   = (time_t)0L;

   if( (f = fopen(lib, "r")) == NIL(FILE) ) return( (time_t)0L );
   rv = ar_scan(f, time_function, &args );
   fclose( f );

   if( rv < 0 ) Fatal("(%s): Invalid library format", lib);

   return( args.time );
}


PUBLIC int
touch_arch(name, lib)/*
=======================
   Look for module 'name' inside 'lib'.  If compiled with cacheing then first
   check to see if the specified lib is cached.  If so then set that time
   stamp and write it into the library.  Returns 0 on success, non-zero
   on failure. */
char   *name;
char   *lib;
{
   FILE   *f;
   int    rv;
   struct ar_args args;

   /* Open the lib file and perform the scan of the members, looking
    * for our particular member.  If cacheing is enabled it will be
    * taken care of automatically during the scan. */

   args.lib    = lib;
   args.member = name;
   args.time   = (time_t)0L;

   if( (f = fopen(lib, "r+")) == NIL(FILE) ) return( (time_t)1L );
   rv = ar_scan(f, touch_function, &args );
   fclose( f );

   if( rv < 0 ) Fatal("(%s): Invalid library format", lib);

   return( 0 );
}



static int
time_function(f, arp, argp)/*
=============================
   get library member's time, if it matches than return it in argp, if
   cacheing is enabled than cache the library members also. */
FILE           *f;      /* library file          */
struct AR      *arp;    /* library member header */
struct ar_args *argp;
{
   int rv = _cache_member( arp->ar_name, argp->lib, arp->ar_time );

   if( ar_name_equal (argp->member, arp->ar_name)) {
      argp->time = arp->ar_time;

      if( arp->ar_time == 0 && !(Glob_attr & A_SILENT) )
         Warning( "(%s): Can't extract library member timestamp; using EPOCH",
              argp->member);

      return( rv );  /* 1 => no cacheing, 0 => cacheing */
   }

   return( FALSE ); /* continue scan */
}



static int
touch_function(f, arp, argp)/*
==============================
   Update library member's time stamp, and write new time value into cache
   if required. */
FILE           *f;      /* library file */
struct AR      *arp;    /* library member header */
struct ar_args *argp;
{
   extern time_t time ANSI(( time_t * ));
   time_t now = time((time_t*) NULL);  /* Current time.       */

   if( ar_name_equal(argp->member, arp->ar_name) ) {
      _check_cache( argp->member, argp->lib, &now, TRUE );
      ar_touch(f, now );

      return( TRUE );
   }

   return( FALSE ); /* continue scan */
}


static int
ar_name_equal (char * name1, char * name2)
{
  int equal;

#if AR_TRUNCATE_MEMBER_NAMES
  struct ar_hdr hdr;

  equal = !strncmp (name1, name2, sizeof (hdr.ar_name)-1);
#else
  equal = !strcmp (name1, name2);
#endif

  return equal;
}


static int
ar_scan(f, function, arg)/*
===========================
   Scan the opened archive, and call the given function for each member found.
   The function will be called with the file positioned at the beginning of
   the member and it can read up to arp->ar_size bytes of the archive member.
   If the function returns 1, we stop and return 1.  We return 0 at the end
   of the archive, or -1 if the archive has invalid format.  This interface
   is more general than required by "make", but it can be used by other
   utilities.  */
register FILE *f;
int      (*function) ANSI((FILE *, struct AR *, struct ar_args *));
struct   ar_args *arg;
{
   extern long atol ();
   register char *p;
   struct ar_hdr arhdr;   /* archive member header   */
   long      nsize;   /* size of member name     */
   long          arind=0; /* archive index offset    */
   int           process;
#if defined(_AIX)
   struct fl_hdr flhdr;   /* archive file header     */
   char   magic[SAIAMAG]; /* size of magic string    */
#else
#if ASCARCH
   char magic[SARMAG];
#else
   unsigned short word;
#endif
#endif

   fseek( f, 0L, 0 );   /* Start at the beginning of the archive file */

#if ASCARCH
#if defined(_AIX)
   fread( (char *)&flhdr, sizeof(flhdr), 1, f );
   if( strncmp(flhdr.fl_magic,AIAMAG, SAIAMAG) != 0 ) return(-1);
   fseek(f, atol(flhdr.fl_fstmoff), 0 ); /* postition to first member */
#else
   fread( magic, sizeof(magic), 1, f );
   if( strncmp(magic, ARMAG, SARMAG) != 0 ) return( -1 );
#endif
#else
   fread( (char*)&word, sizeof(word), 1, f );
   if( word != ARMAG ) return( -1 );
#endif

   /* scan the library, calling `function' for each member
    */
   while( 1 ) {
      arhdroffset = ftell(f);
#if defined(_AIX)
      if( fread((char*)&arhdr,sizeof(arhdr)-sizeof(arhdr._ar_name),1,f)!=1)
         break;
      nsize = atoi(arhdr.ar_namlen);
      fseek(f, arhdroffset+(unsigned long)(((struct ar_hdr *)0)->_ar_name.ar_name), 0);
      if( fread((char*)_ar.ar_name,nsize,1,f)!=1)
         break;
      _ar.ar_name[nsize]='\0';
#else
      if( fread((char*) &arhdr, sizeof(arhdr), 1, f) != 1 ) break;
      strncpy(_ar.ar_name, arhdr.ar_name, nsize = sizeof(arhdr.ar_name));
#endif

      for( p = &_ar.ar_name[nsize];
           --p >= _ar.ar_name && *p == ' ';);

      p[1] = '\0';
      if( *p == '/' ) *p = 0;   /* SysV has trailing '/' */

      /* check to see if this is an archive index using SsysV Index scheme.
       * see ar(4) man page for more info */
#if CHECKELF
      if( _ar.ar_name[0] == '/' && _ar.ar_name[1] == '\0' ) {
     arind = arhdroffset+sizeof(arhdr);
     process = 0;
      }
      else
#endif
     process = 1;

#if !defined(_AIX)
#if ASCARCH
      if( strncmp(arhdr.ar_fmag, ARFMAG, sizeof(arhdr.ar_fmag)) != 0 )
     return( -1 );
      _ar.ar_time = atol(arhdr.ar_date);
      _ar.ar_size = atol(arhdr.ar_size);
#else
      _ar.ar_time = arhdr.ar_date;
      _ar.ar_size = arhdr.ar_size;
#endif
#if CHECKELF
      /* check for names of the form /xxxx where xxxx is an offset into the
       * name table pointed at by arind. */
      if(arind && _ar.ar_name[0] == '/') {
     long offset = atol(_ar.ar_name+1);
     long here = ftell(f);
     int  c;

     fseek(f, arind+offset, 0);
     p = _ar.ar_name;
     while((c=fgetc(f)) != EOF) {
        *p++ = c;
        if(c == '/') {
           p[-1] = '\0';
           break;
        }
     }

     if (c==EOF) return(-1);          /* 'c' should never be EOF */
     fseek(f, here, 0);
      }
#endif
#else
#if ASCARCH
      _ar.ar_time = atol(arhdr.ar_date);
      _ar.ar_size = atol(arhdr.ar_nxtmem);
#else
      _ar.ar_time = arhdr.ar_date;
      _ar.ar_size = arhdr.ar_nxtmem;
#endif
#endif


#if DECODE_ALL_AR_FIELDS
#if ASCARCH
      _ar.ar_mode = atoi(arhdr.ar_mode);
      _ar.ar_uid  = atoi(arhdr.ar_uid);
      _ar.ar_gid  = atoi(arhdr.ar_gid);
#else
      _ar.ar_mode = arhdr.ar_mode;
      _ar.ar_uid = arhdr.ar_uid;
      _ar.ar_gid = arhdr.ar_gid;
#endif
#endif
      if( process && (*function)(f, &_ar, arg) ) return( 1 );

#if defined(_AIX)
      if( _ar.ar_size == 0L ) break;
      fseek( f, (long) _ar.ar_size, 0 );
#else
      fseek( f, arhdroffset + sizeof(arhdr) + ((_ar.ar_size+1) & ~1L), 0 );
#endif
   }

#if !defined(_AIX)
   if( !feof(f) ) return( -1 );
#endif
   return 0;
}



static int
ar_touch( f, now )/*
====================
   touch module header timestamp. */
FILE   *f;
time_t now;
{

   fseek(f, arhdroffset + (unsigned long)(((struct ar_hdr *)0)->ar_date), 0);

#if ASCARCH
   fprintf(f, "%lu", now);
#else
   fwrite((char *)now, sizeof(now), 1, f);
#endif

   return( ferror(f) ? 0 : 1 );
}


#if LC
typedef struct mem {
   time_t   m_time;     /* modify time of member*/
   struct mem   *m_next;    /* next member in lib   */
   char     m_valid;    /* valid cache entry    */
   char     m_name[1];  /* lib member name  */
} MEM, *MEMPTR;

typedef struct lib {
   struct lib   *lb_next;   /* next library in list */
   struct mem   *lb_members;    /* list of lib members  */
   char     lb_valid;   /* valid cache entry    */
   char     *lb_name;   /* library name     */
} LIB, *LIBPTR;

static LIBPTR _cache = NIL(LIB);
static MEMPTR _find_member ANSI(( LIBPTR, char * ));

static int
_check_cache( name, lib, pmtime, touch )/*
==========================================
   Check to see if we have cached member in lib, if so return time in pmtime
   and return TRUE, otherwise return FALSE, if touch is TRUE then touch
   the archive member instead. */
char   *name;
char   *lib;
time_t *pmtime;
int    touch;
{
   register MEMPTR mp;
   register LIBPTR lp;

   for( lp=_cache; lp != NIL(LIB) && lp->lb_name != lib; lp=lp->lb_next );
   if( lp == NIL(LIB) ) return( FALSE );

   mp = _find_member( lp, name );
   if( mp == NIL(MEM) || !mp->m_valid ) return( FALSE );

   if( touch == TRUE )
   {
      mp->m_time = *pmtime;
      mp->m_valid = 1;
   }
   else
      *pmtime = mp->m_time;

   lp->lb_valid   = 1;
   lp->lb_members = mp;

   return( TRUE );
}



static int
_cache_member( name, lib, mtime )/*
===================================
   Cache name in lib along with it's time */
char   *name;
char   *lib;
time_t mtime;
{
   register MEMPTR mp;
   register LIBPTR lp;

   for( lp=_cache;
    lp != NIL(LIB) && lp->lb_name != NIL(char) && lp->lb_name != lib;
    lp=lp->lb_next);

   if( lp == NIL(LIB) )
   {
      lp = (LIBPTR) malloc(sizeof(LIB));
      if( lp == NIL(LIB) ) No_ram();

      lp->lb_name    = lib;
      lp->lb_members = NIL(MEM);
      lp->lb_next    = _cache;
      lp->lb_valid   = 0;
      _cache = lp;
   }

   /* On UNIX ar does not allow multiple copies of the same .o file to live
    * in the same AR file.  If this is not TRUE then use the commented out
    * version to set the value of mp. */

   /*mp = _find_member(lp, name);*/
   mp = NIL(MEM);

   if( mp == NIL(MEM) )
   {
      mp = (MEMPTR) malloc(sizeof(char)*offsetof(MEM,m_name[strlen(name)+1]));
      if( mp == NIL(MEM) ) No_ram();

      strcpy( mp->m_name, name );
      mp->m_time     = mtime;

      if( lp->lb_members == NIL(MEM) ) {
     mp->m_next     = mp;
     lp->lb_members = mp;
      }
      else {
     mp->m_next = lp->lb_members->m_next;
     lp->lb_members->m_next = mp;
     lp->lb_members = mp;
      }
   }
   else
      mp->m_time = mtime;

   mp->m_valid = 1;

   return( lp->lb_valid );
}


static MEMPTR
_find_member( lp, name )
LIBPTR lp;
char   *name;
{
   register MEMPTR mp = lp->lb_members;

   if( mp == NIL(MEM) ) return(mp);

   do {
      if( !strcmp(mp->m_name, name ) ) return( mp );
      mp = mp->m_next;
   }
   while( mp != lp->lb_members );

   return( NIL(MEM) );
}
#endif



PUBLIC void
void_lcache( lib, member )/*
============================
   Void the library cache for lib.  If member is NIL(char) then nuke all
   of the members, if member is NOT NIL(char) then invalidate only that
   member. */
char *lib;
char *member;
{
#if LC
   register LIBPTR lp;
   register MEMPTR mp;
   register MEMPTR tmp;

   for( lp=_cache; lp != NIL(LIB) && lp->lb_name != lib; lp=lp->lb_next );
   if( lp == NIL(LIB) ) return;

   if( member == NIL(char) ) {
      mp = lp->lb_members;
      do {
     tmp = mp->m_next;
     (void) free( mp );
     mp = tmp;
      } while( mp != lp->lb_members );

      lp->lb_valid   = 0;
      lp->lb_members = NIL(MEM);
      lp->lb_name    = NIL(char);
   }
   else {
      mp=lp->lb_members;
      do {
     if( strcmp( member, mp->m_name) == 0 ) {
        lp->lb_members = mp->m_next;
        mp->m_valid = 0;
     }

     mp=mp->m_next;
      } while( mp != lp->lb_members );
   }
#endif
}