* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)ranlib.c 5.3 (Berkeley) %G%";
* ranlib - create table of contents for archive; string table version
char tempnm
[] = "__.SYMDEF";
char *malloc(), *calloc();
* table segment definitions
struct tabsegment
*pnext
;
struct ranlib tab
[TABSZ
];
struct strsegment
*pnext
;
/* magbuf must be an int array so it is aligned on an int-ish
boundary, so that we may access its first word as an int! */
int magbuf
[(SARMAG
+sizeof(int))/sizeof(int)];
register int just_touch
= 0;
register struct tabsegment
*ptab
;
register struct strsegment
*pstr
;
/* check for the "-t" flag" */
if (argc
> 1 && strcmp(argv
[1], "-t") == 0) {
fprintf(stderr
, "ranlib: cannot open %s\n", *argv
);
fread((char *)magbuf
, 1, SARMAG
, fi
);
if (strncmp((char *)magbuf
, ARMAG
, SARMAG
)) {
fprintf(stderr
, "old format ");
fprintf(stderr
, "not an ");
fprintf(stderr
, "archive: %s\n", *argv
);
fseek(fi
, (long) SARMAG
, 0);
if (fread(cmdbuf
, sizeof archdr
.ar_name
, 1, fi
) != 1) {
fprintf(stderr
, "malformed archive: %s\n",
if (bcmp(cmdbuf
, tempnm
, len
) != 0 ||
fprintf(stderr
, "no symbol table: %s\n", *argv
);
fread((char *)&exp
, 1, sizeof(struct exec
), fi
);
if (!strncmp(tempnm
, archdr
.ar_name
, sizeof(archdr
.ar_name
)))
fprintf(stderr
, "ranlib: warning: %s(%s): no symbol table\n", *argv
, archdr
.ar_name
);
o
= N_STROFF(exp
) - sizeof (struct exec
);
if (ftell(fi
)+o
+sizeof(ssiz
) >= off
) {
fprintf(stderr
, "ranlib: warning: %s(%s): old format .o file\n", *argv
, archdr
.ar_name
);
fread((char *)&ssiz
, 1, sizeof (ssiz
), fi
);
fprintf(stderr
, "ranlib: warning: %s(%s): mangled string table\n", *argv
, archdr
.ar_name
);
strtab
= (char *)calloc(1, ssiz
);
fprintf(stderr
, "ranlib: ran out of memory\n");
fread(strtab
+sizeof(ssiz
), ssiz
- sizeof(ssiz
), 1, fi
);
fseek(fi
, -(exp
.a_syms
+ssiz
), 1);
n
= exp
.a_syms
/ sizeof(struct nlist
);
fread((char *)&sym
, 1, sizeof(sym
), fi
);
if (sym
.n_un
.n_strx
== 0)
sym
.n_un
.n_name
= strtab
+ sym
.n_un
.n_strx
;
if ((sym
.n_type
&N_EXT
)==0)
switch (sym
.n_type
&N_TYPE
) {
fprintf(stderr
, "can't create temporary\n");
tnum
*= sizeof (struct ranlib
);
fwrite(&tnum
, 1, sizeof (tnum
), fo
);
tnum
/= sizeof (struct ranlib
);
fwrite((char *)ptab
->tab
, ptab
->nelem
,
sizeof(struct ranlib
), fo
);
} while (ptab
= ptab
->pnext
);
fwrite(&tssiz
, 1, sizeof (tssiz
), fo
);
fwrite(pstr
->stab
, pstr
->nelem
, 1, fo
);
} while (pstr
= pstr
->pnext
);
while (tssiz
--) putc('\0', fo
);
sprintf(cmdbuf
, "ar rlb %s %s %s\n", firstname
, *argv
, tempnm
);
sprintf(cmdbuf
, "ar rl %s %s\n", *argv
, tempnm
);
fprintf(stderr
, "ranlib: ``%s'' failed\n", cmdbuf
);
r
= fread((char *)&archdr
, 1, sizeof(struct ar_hdr
), af
);
if (r
!= sizeof(struct ar_hdr
))
for (cp
=archdr
.ar_name
; cp
< & archdr
.ar_name
[sizeof(archdr
.ar_name
)]; cp
++)
arsize
= atol(archdr
.ar_size
);
off
= ftell(af
) + arsize
;
register struct ranlib
*tab
;
if (ptabseg
->nelem
>= TABSZ
) {
/* allocate a new symbol table segment */
ptabseg
= ptabseg
->pnext
=
(struct tabsegment
*) segalloc(sizeof(struct tabsegment
));
if (pstrseg
->nelem
>= STRTABSZ
) {
/* allocate a new string table segment */
pstrseg
= pstrseg
->pnext
=
(struct strsegment
*) segalloc(sizeof(struct strsegment
));
tab
[tabsiz
].ran_un
.ran_strx
= tssiz
;
tab
[tabsiz
].ran_off
= oldoff
;
for (cp
= s
->n_un
.n_name
; strtab
[strsiz
++] = *cp
++;)
if (strsiz
>= STRTABSZ
) {
/* allocate a new string table segment */
pstrseg
= pstrseg
->pnext
=
(struct strsegment
*) segalloc(sizeof(struct strsegment
));
strsiz
= pstrseg
->nelem
= 0;
tssiz
+= strsiz
- pstrseg
->nelem
; /* length of the string */
/* allocate a zero filled segment of size bytes */
fprintf(stderr
, "ranlib: ran out of memeory\n");
register struct tabsegment
*ptab
;
register struct strsegment
*pstr
;
* The first entry is static.
* The first entry is static.
register struct tabsegment
*ptab
;
offdelta
= sizeof(archdr
) + sizeof (tnum
) + tnum
* sizeof(struct ranlib
) +
if(strncmp(archdr
.ar_name
, tempnm
, sizeof (archdr
.ar_name
)) == 0) {
offdelta
-= sizeof(archdr
) + arsize
;
strncpy(firstname
, archdr
.ar_name
, sizeof(archdr
.ar_name
));
for (i
= 0; i
< ptab
->nelem
; i
++)
ptab
->tab
[i
].ran_off
+= offdelta
;
} while (ptab
= ptab
->pnext
);
fprintf(stderr
, "ranlib: can't reopen %s\n", s
);
sprintf(buf
, "%-*ld", sizeof(archdr
.ar_date
), time((long *)NULL
)+5);
lseek(fd
, (long)SARMAG
+ ((char *)archdr
.ar_date
-(char *)&archdr
), 0);
write(fd
, buf
, sizeof(archdr
.ar_date
));