xref: /csrg-svn/usr.bin/ranlib/ranlib.c (revision 12673)
1*12673Ssam #ifndef lint
2*12673Ssam static	char sccsid[] = "@(#)ranlib.c 4.6 05/22/83";
3*12673Ssam #endif
4625Sbill /*
5625Sbill  * ranlib - create table of contents for archive; string table version
6625Sbill  */
7625Sbill #include <sys/types.h>
8899Sbill #include <ar.h>
9625Sbill #include <ranlib.h>
10899Sbill #include <a.out.h>
11625Sbill #include <stdio.h>
12625Sbill 
13625Sbill struct	ar_hdr	archdr;
14625Sbill #define	OARMAG 0177545
15625Sbill long	arsize;
16625Sbill struct	exec	exp;
17625Sbill FILE	*fi, *fo;
18625Sbill long	off, oldoff;
19625Sbill long	atol(), ftell();
2011152Sshantz #define TABSZ	6000
21625Sbill struct	ranlib tab[TABSZ];
22625Sbill int	tnum;
231753Sbill #define	STRTABSZ	75000
24625Sbill char	tstrtab[STRTABSZ];
25625Sbill int	tssiz;
26625Sbill char	*strtab;
27625Sbill int	ssiz;
28625Sbill int	new;
29625Sbill char	tempnm[] = "__.SYMDEF";
30625Sbill char	firstname[17];
31625Sbill 
32625Sbill main(argc, argv)
33625Sbill char **argv;
34625Sbill {
35625Sbill 	char cmdbuf[BUFSIZ];
369343Srt 	/* magbuf must be an int array so it is aligned on an int-ish
379343Srt 	   boundary, so that we may access its first word as an int! */
389343Srt 	int magbuf[(SARMAG+sizeof(int))/sizeof(int)];
39625Sbill 
40625Sbill 	--argc;
41625Sbill 	while(argc--) {
42625Sbill 		fi = fopen(*++argv,"r");
43625Sbill 		if (fi == NULL) {
44625Sbill 			fprintf(stderr, "ranlib: cannot open %s\n", *argv);
45625Sbill 			continue;
46625Sbill 		}
47625Sbill 		off = SARMAG;
489343Srt 		fread((char *)magbuf, 1, SARMAG, fi);
499343Srt 		if (strncmp((char *)magbuf, ARMAG, SARMAG)) {
509343Srt 			if (magbuf[0] == OARMAG)
51625Sbill 				fprintf(stderr, "old format ");
52625Sbill 			else
53625Sbill 				fprintf(stderr, "not an ");
54625Sbill 			fprintf(stderr, "archive: %s\n", *argv);
55625Sbill 			continue;
56625Sbill 		}
57625Sbill 		fseek(fi, 0L, 0);
58625Sbill 		new = tnum = 0;
59625Sbill 		if (nextel(fi) == 0) {
60625Sbill 			fclose(fi);
61625Sbill 			continue;
62625Sbill 		}
63625Sbill 		do {
64625Sbill 			long o;
65625Sbill 			register n;
66625Sbill 			struct nlist sym;
67625Sbill 
68625Sbill 			fread((char *)&exp, 1, sizeof(struct exec), fi);
69625Sbill 			if (N_BADMAG(exp))
70625Sbill 				continue;
71625Sbill 			if (exp.a_syms == 0) {
72625Sbill 				fprintf(stderr, "ranlib: warning: %s(%s): no symbol table\n", *argv, archdr.ar_name);
733607Ssklower 				continue;
74625Sbill 			}
75625Sbill 			o = N_STROFF(exp) - sizeof (struct exec);
76625Sbill 			if (ftell(fi)+o+sizeof(ssiz) >= off) {
77625Sbill 				fprintf(stderr, "ranlib: %s(%s): old format .o file\n", *argv, archdr.ar_name);
78625Sbill 				exit(1);
79625Sbill 			}
80625Sbill 			fseek(fi, o, 1);
81625Sbill 			fread((char *)&ssiz, 1, sizeof (ssiz), fi);
829343Srt 			if (ssiz < sizeof ssiz){
839343Srt 				/* sanity check */
849343Srt 				fprintf(stderr, "ranlib: %s(%s): mangled string table\n", *argv, archdr.ar_name);
859343Srt 				exit(1);
869343Srt 			}
87625Sbill 			strtab = (char *)calloc(1, ssiz);
88625Sbill 			if (strtab == 0) {
89625Sbill 				fprintf(stderr, "ranlib: ran out of memory\n");
90625Sbill 				exit(1);
91625Sbill 			}
92625Sbill 			fread(strtab+sizeof(ssiz), ssiz - sizeof(ssiz), 1, fi);
93625Sbill 			fseek(fi, -(exp.a_syms+ssiz), 1);
94625Sbill 			n = exp.a_syms / sizeof(struct nlist);
95625Sbill 			while (--n >= 0) {
96625Sbill 				fread((char *)&sym, 1, sizeof(sym), fi);
97625Sbill 				if (sym.n_un.n_strx == 0)
98625Sbill 					continue;
99625Sbill 				sym.n_un.n_name = strtab + sym.n_un.n_strx;
100625Sbill 				if ((sym.n_type&N_EXT)==0)
101625Sbill 					continue;
102625Sbill 				switch (sym.n_type&N_TYPE) {
103625Sbill 
104625Sbill 				case N_UNDF:
105625Sbill 					if (sym.n_value!=0)
106625Sbill 						stash(&sym);
107625Sbill 					continue;
108625Sbill 
109625Sbill 				default:
110625Sbill 					stash(&sym);
111625Sbill 					continue;
112625Sbill 				}
113625Sbill 			}
114625Sbill 		} while(nextel(fi));
115625Sbill 		new = fixsize();
116625Sbill 		fclose(fi);
117625Sbill 		fo = fopen(tempnm, "w");
118625Sbill 		if(fo == NULL) {
119625Sbill 			fprintf(stderr, "can't create temporary\n");
120625Sbill 			exit(1);
121625Sbill 		}
122625Sbill 		tnum *= sizeof (struct ranlib);
123625Sbill 		fwrite(&tnum, 1, sizeof (tnum), fo);
124625Sbill 		tnum /= sizeof (struct ranlib);
125625Sbill 		fwrite((char *)tab, tnum, sizeof(struct ranlib), fo);
126625Sbill 		fwrite(&tssiz, 1, sizeof (tssiz), fo);
127625Sbill 		fwrite(tstrtab, tssiz, 1, fo);
128625Sbill 		fclose(fo);
129625Sbill 		if(new)
130899Sbill 			sprintf(cmdbuf, "ar rlb %s %s %s\n", firstname, *argv, tempnm);
131625Sbill 		else
132899Sbill 			sprintf(cmdbuf, "ar rl %s %s\n", *argv, tempnm);
133625Sbill 		if(system(cmdbuf))
134625Sbill 			fprintf(stderr, "ranlib: ``%s'' failed\n", cmdbuf);
135625Sbill 		else
136625Sbill 			fixdate(*argv);
137625Sbill 		unlink(tempnm);
138625Sbill 	}
139625Sbill 	exit(0);
140625Sbill }
141625Sbill 
142625Sbill nextel(af)
143625Sbill FILE *af;
144625Sbill {
145625Sbill 	register r;
146625Sbill 	register char *cp;
147625Sbill 
148625Sbill 	oldoff = off;
149625Sbill 	fseek(af, off, 0);
150625Sbill 	r = fread((char *)&archdr, 1, sizeof(struct ar_hdr), af);
151625Sbill 	if (r != sizeof(struct ar_hdr))
152625Sbill 		return(0);
153625Sbill 	for (cp=archdr.ar_name; cp < & archdr.ar_name[sizeof(archdr.ar_name)]; cp++)
154625Sbill 		if (*cp == ' ')
155625Sbill 			*cp = '\0';
156625Sbill 	arsize = atol(archdr.ar_size);
157625Sbill 	if (arsize & 1)
158625Sbill 		arsize++;
159625Sbill 	off = ftell(af) + arsize;
160625Sbill 	return(1);
161625Sbill }
162625Sbill 
163625Sbill stash(s)
164625Sbill 	struct nlist *s;
165625Sbill {
166625Sbill 	int i;
167625Sbill 	register char *cp;
168625Sbill 
169625Sbill 	if(tnum >= TABSZ) {
170625Sbill 		fprintf(stderr, "ranlib: symbol table overflow\n");
171625Sbill 		exit(1);
172625Sbill 	}
173625Sbill 	tab[tnum].ran_un.ran_strx = tssiz;
174625Sbill 	tab[tnum].ran_off = oldoff;
175625Sbill 	for (cp = s->n_un.n_name; tstrtab[tssiz++] = *cp++;)
176625Sbill 		if (tssiz > STRTABSZ) {
177625Sbill 			fprintf(stderr, "ranlib: string table overflow\n");
178625Sbill 			exit(1);
179625Sbill 		}
180625Sbill 	tnum++;
181625Sbill }
182625Sbill 
183625Sbill fixsize()
184625Sbill {
185625Sbill 	int i;
186625Sbill 	off_t offdelta;
187625Sbill 
188625Sbill 	if (tssiz&1)
189625Sbill 		tssiz++;
190625Sbill 	offdelta = sizeof(archdr) + sizeof (tnum) + tnum * sizeof(struct ranlib) +
191625Sbill 	    sizeof (tssiz) + tssiz;
192625Sbill 	off = SARMAG;
193625Sbill 	nextel(fi);
194625Sbill 	if(strncmp(archdr.ar_name, tempnm, sizeof (archdr.ar_name)) == 0) {
195625Sbill 		new = 0;
196625Sbill 		offdelta -= sizeof(archdr) + arsize;
197625Sbill 	} else {
198625Sbill 		new = 1;
199625Sbill 		strncpy(firstname, archdr.ar_name, sizeof(archdr.ar_name));
200625Sbill 	}
201625Sbill 	for(i=0; i<tnum; i++)
202625Sbill 		tab[i].ran_off += offdelta;
203625Sbill 	return(new);
204625Sbill }
205625Sbill 
206625Sbill /* patch time */
207625Sbill fixdate(s)
208625Sbill 	char *s;
209625Sbill {
210625Sbill 	long time();
211625Sbill 	char buf[24];
212625Sbill 	int fd;
213625Sbill 
214625Sbill 	fd = open(s, 1);
215625Sbill 	if(fd < 0) {
216625Sbill 		fprintf(stderr, "ranlib: can't reopen %s\n", s);
217625Sbill 		return;
218625Sbill 	}
219625Sbill 	sprintf(buf, "%-*ld", sizeof(archdr.ar_date), time((long *)NULL)+5);
220625Sbill 	lseek(fd, (long)SARMAG + ((char *)archdr.ar_date-(char *)&archdr), 0);
221625Sbill 	write(fd, buf, sizeof(archdr.ar_date));
222625Sbill 	close(fd);
223625Sbill }
224