depend needs instrs.c2
[unix-history] / usr / src / old / make / files.c
CommitLineData
b46f5ef9 1static char *sccsid = "@(#)files.c 4.21 (Berkeley) 90/03/26";
5ec84434
RC
2#include <fcntl.h>
3
fbf6641c
BJ
4/* UNIX DEPENDENT PROCEDURES */
5
6
7/* DEFAULT RULES FOR UNIX */
8
9char *builtin[] =
10 {
11#ifdef pwb
12 ".SUFFIXES : .L .out .o .c .f .e .r .y .yr .ye .l .s .z .x .t .h .cl",
13#else
14724fce 14 ".SUFFIXES : .out .o .c .F .f .e .r .y .yr .ye .l .s .cl .p .8 .7 .6 .5 .4 .3 .2 .1 .0",
fbf6641c
BJ
15#endif
16 "YACC=yacc",
17 "YACCR=yacc -r",
18 "YACCE=yacc -e",
19 "YFLAGS=",
20 "LEX=lex",
21 "LFLAGS=",
22 "CC=cc",
dc43ff3e 23 "CPP=cpp",
76ed2017 24#if defined(vax) || defined(sun) || defined(tahoe)
fbf6641c
BJ
25 "AS=as",
26#else
27 "AS=as -",
28#endif
29 "PC=pc",
30 "PFLAGS=",
31 "CFLAGS=",
32 "RC=f77",
33 "RFLAGS=",
0609ddad 34 "FC=f77",
fbf6641c
BJ
35 "EFLAGS=",
36 "FFLAGS=",
37 "LOADLIBES=",
14724fce 38 "NROFF=nroff",
fbf6641c
BJ
39#ifdef pwb
40 "SCOMP=scomp",
41 "SCFLAGS=",
42 "CMDICT=cmdict",
43 "CMFLAGS=",
44#endif
45
46 ".c.o :",
47 "\t$(CC) $(CFLAGS) -c $<",
48
49 ".p.o :",
50 "\t$(PC) $(PFLAGS) -c $<",
51
52 ".cl.o :",
53 "\tclass -c $<",
54
16afe48d 55 ".e.o .r.o .F.o .f.o :",
fbf6641c
BJ
56 "\t$(FC) $(RFLAGS) $(EFLAGS) $(FFLAGS) -c $<",
57
58 ".s.o :",
59 "\t$(AS) -o $@ $<",
60
61 ".y.o :",
62 "\t$(YACC) $(YFLAGS) $<",
63 "\t$(CC) $(CFLAGS) -c y.tab.c",
64 "\trm y.tab.c",
65 "\tmv y.tab.o $@",
66
67 ".yr.o:",
68 "\t$(YACCR) $(YFLAGS) $<",
69 "\t$(RC) $(RFLAGS) -c y.tab.r",
70 "\trm y.tab.r",
71 "\tmv y.tab.o $@",
72
73 ".ye.o :",
74 "\t$(YACCE) $(YFLAGS) $<",
75 "\t$(EC) $(RFLAGS) -c y.tab.e",
76 "\trm y.tab.e",
77 "\tmv y.tab.o $@",
78
79 ".l.o :",
80 "\t$(LEX) $(LFLAGS) $<",
81 "\t$(CC) $(CFLAGS) -c lex.yy.c",
82 "\trm lex.yy.c",
83 "\tmv lex.yy.o $@",
84
85 ".y.c :",
86 "\t$(YACC) $(YFLAGS) $<",
87 "\tmv y.tab.c $@",
88
89 ".l.c :",
90 "\t$(LEX) $(LFLAGS) $<",
91 "\tmv lex.yy.c $@",
92
93 ".yr.r:",
94 "\t$(YACCR) $(YFLAGS) $<",
95 "\tmv y.tab.r $@",
96
97 ".ye.e :",
98 "\t$(YACCE) $(YFLAGS) $<",
99 "\tmv y.tab.e $@",
100
101#ifdef pwb
102 ".o.L .c.L .t.L:",
103 "\t$(SCOMP) $(SCFLAGS) $<",
104
105 ".t.o:",
106 "\t$(SCOMP) $(SCFLAGS) -c $<",
107
108 ".t.c:",
109 "\t$(SCOMP) $(SCFLAGS) -t $<",
110
111 ".h.z .t.z:",
112 "\t$(CMDICT) $(CMFLAGS) $<",
113
114 ".h.x .t.x:",
115 "\t$(CMDICT) $(CMFLAGS) -c $<",
116#endif
117
118 ".s.out .c.out .o.out :",
119 "\t$(CC) $(CFLAGS) $< $(LOADLIBES) -o $@",
120
16afe48d 121 ".f.out .F.out .r.out .e.out :",
fbf6641c
BJ
122 "\t$(FC) $(EFLAGS) $(RFLAGS) $(FFLAGS) $< $(LOADLIBES) -o $@",
123 "\t-rm $*.o",
124
125 ".y.out :",
126 "\t$(YACC) $(YFLAGS) $<",
127 "\t$(CC) $(CFLAGS) y.tab.c $(LOADLIBES) -ly -o $@",
128 "\trm y.tab.c",
129
130 ".l.out :",
131 "\t$(LEX) $(LFLAGS) $<",
132 "\t$(CC) $(CFLAGS) lex.yy.c $(LOADLIBES) -ll -o $@",
133 "\trm lex.yy.c",
134
14724fce
KB
135 ".8.0 :",
136 "\t$(NROFF) -man -h $< > $@",
137
138 ".7.0 :",
139 "\t$(NROFF) -man -h $< > $@",
140
141 ".6.0 :",
142 "\t$(NROFF) -man -h $< > $@",
143
144 ".5.0 :",
145 "\t$(NROFF) -man -h $< > $@",
146
147 ".4.0 :",
148 "\t$(NROFF) -man -h $< > $@",
149
150 ".3.0 :",
151 "\t$(NROFF) -man -h $< > $@",
152
153 ".2.0 :",
154 "\t$(NROFF) -man -h $< > $@",
155
156 ".1.0 :",
157 "\t$(NROFF) -man -h $< > $@",
158
fbf6641c
BJ
159 0 };
160\f
161#include "defs"
a10b24d4 162#include <sys/stat.h>
fbf6641c
BJ
163
164
a10b24d4
KM
165
166TIMETYPE
167exists(pname)
168struct nameblock *pname;
fbf6641c 169{
fbf6641c 170struct stat buf;
a10b24d4 171register char *s, *filename;
fbf6641c 172TIMETYPE lookarch();
a10b24d4
KM
173extern char *findfl();
174
175filename = pname->namep;
fbf6641c
BJ
176
177for(s = filename ; *s!='\0' && *s!='(' ; ++s)
178 ;
179
180if(*s == '(')
181 return(lookarch(filename));
182
0e7207aa 183if (stat(filename, &buf) < 0)
a10b24d4
KM
184{
185 s = findfl(filename);
186 if(s != (char *)-1)
187 {
188 pname->alias = copys(s);
189 if(stat(pname->alias, &buf) == 0)
190 return(buf.st_mtime);
191 }
fbf6641c 192 return(0);
a10b24d4 193}
fbf6641c
BJ
194else return(buf.st_mtime);
195}
196
197
198TIMETYPE prestime()
199{
200TIMETYPE t;
201time(&t);
202return(t);
203}
204
205\f
206
b342e078
KM
207FSTATIC char nbuf[MAXNAMLEN + 1];
208FSTATIC char *nbufend = &nbuf[MAXNAMLEN];
fbf6641c
BJ
209
210
211
212struct depblock *srchdir(pat, mkchain, nextdbl)
213register char *pat; /* pattern to be matched in directory */
214int mkchain; /* nonzero if results to be remembered */
215struct depblock *nextdbl; /* final value for chain */
216{
b342e078 217DIR *dirf;
fbf6641c
BJ
218register int i;
219int nread, cldir;
dbf5cc74
JB
220char *dirname, *dirpref, *endir, *filepat, *p, temp[BUFSIZ];
221char fullname[BUFSIZ], *p1, *p2;
fbf6641c
BJ
222struct nameblock *q;
223struct depblock *thisdbl;
b342e078 224struct dirhdr *od;
fbf6641c 225struct pattern *patp;
a10b24d4 226struct varblock *cp, *varptr();
dbf5cc74 227char *path, pth[BUFSIZ], *strcpy();
43215ae4 228struct dirent *dptr;
fbf6641c
BJ
229
230
231thisdbl = 0;
232
233if(mkchain == NO)
234 for(patp=firstpat ; patp ; patp = patp->nxtpattern)
235 if(! unequal(pat, patp->patval)) return(0);
236
237patp = ALLOC(pattern);
238patp->nxtpattern = firstpat;
239firstpat = patp;
240patp->patval = copys(pat);
241
242endir = 0;
243
244for(p=pat; *p!='\0'; ++p)
245 if(*p=='/') endir = p;
246
247if(endir==0)
248 {
fbf6641c
BJ
249 dirpref = "";
250 filepat = pat;
a10b24d4 251 cp = varptr("VPATH");
d85b33c2 252 if (cp->varval == NULL) path = ".";
a10b24d4
KM
253 else {
254 path = pth;
255 *path = '\0';
d14eaed2 256 if (strncmp(cp->varval, ".:", 2) != 0) strcpy(pth,".:");
a10b24d4
KM
257 strcat(pth, cp->varval);
258 }
fbf6641c
BJ
259 }
260else {
fbf6641c 261 *endir = '\0';
a10b24d4
KM
262 path = strcpy(pth, pat);
263 dirpref = concat(pat, "/", temp);
fbf6641c
BJ
264 filepat = endir+1;
265 }
266
a10b24d4
KM
267while (*path) { /* Loop thru each VPATH directory */
268 dirname = path;
269 for (; *path; path++)
270 if (*path == ':') {
271 *path++ = '\0';
272 break;
273 }
274
fbf6641c
BJ
275dirf = NULL;
276cldir = NO;
277
278for(od = firstod; od; od = od->nxtopendir)
279 if(! unequal(dirname, od->dirn) )
280 {
281 dirf = od->dirfc;
b342e078
KM
282 if (dirf != NULL)
283 rewinddir(dirf); /* start over at the beginning */
fbf6641c
BJ
284 break;
285 }
286
287if(dirf == NULL)
288 {
b342e078 289 dirf = opendir(dirname);
fbf6641c
BJ
290 if(nopdir >= MAXDIR)
291 cldir = YES;
292 else {
293 ++nopdir;
b342e078 294 od = ALLOC(dirhdr);
fbf6641c
BJ
295 od->nxtopendir = firstod;
296 firstod = od;
297 od->dirfc = dirf;
298 od->dirn = copys(dirname);
c01bbea7 299 fcntl(dirfd(dirf), F_SETFD, 1);
fbf6641c
BJ
300 }
301 }
302
303if(dirf == NULL)
304 {
305 fprintf(stderr, "Directory %s: ", dirname);
306 fatal("Cannot open");
307 }
308
b342e078 309else for (dptr = readdir(dirf); dptr != NULL; dptr = readdir(dirf))
fbf6641c 310 {
b342e078
KM
311 p1 = dptr->d_name;
312 p2 = nbuf;
313 while( (p2<nbufend) && (*p2++ = *p1++)!='\0' )
314 /* void */;
315 if( amatch(nbuf,filepat) )
316 {
317 concat(dirpref,nbuf,fullname);
318 if( (q=srchname(fullname)) ==0)
319 q = makename(copys(fullname));
320 if(mkchain)
fbf6641c 321 {
b342e078
KM
322 thisdbl = ALLOC(depblock);
323 thisdbl->nxtdepblock = nextdbl;
324 thisdbl->depname = q;
325 nextdbl = thisdbl;
fbf6641c 326 }
b342e078
KM
327 }
328 }
fbf6641c
BJ
329
330if(endir != 0) *endir = '/';
331
b342e078
KM
332if(cldir) {
333 closedir(dirf);
334 dirf = NULL;
335}
a10b24d4 336} /* End of VPATH loop */
fbf6641c
BJ
337return(thisdbl);
338}
339\f
340/* stolen from glob through find */
341
342static amatch(s, p)
343char *s, *p;
344{
345 register int cc, scc, k;
346 int c, lc;
347
348 scc = *s;
349 lc = 077777;
350 switch (c = *p) {
351
352 case '[':
353 k = 0;
354 while (cc = *++p) {
355 switch (cc) {
356
357 case ']':
358 if (k)
359 return(amatch(++s, ++p));
360 else
361 return(0);
362
363 case '-':
364 k |= (lc <= scc) & (scc <= (cc=p[1]) ) ;
365 }
366 if (scc==(lc=cc)) k++;
367 }
368 return(0);
369
370 case '?':
371 caseq:
372 if(scc) return(amatch(++s, ++p));
373 return(0);
374 case '*':
375 return(umatch(s, ++p));
376 case 0:
377 return(!scc);
378 }
379 if (c==scc) goto caseq;
380 return(0);
381}
382
383static umatch(s, p)
384char *s, *p;
385{
386 if(*p==0) return(1);
387 while(*s)
388 if (amatch(s++,p)) return(1);
389 return(0);
390}
391\f
392#ifdef METERFILE
393#include <pwd.h>
394int meteron = 0; /* default: metering off */
395
396meter(file)
397char *file;
398{
399TIMETYPE tvec;
400char *p, *ctime();
401FILE * mout;
402struct passwd *pwd, *getpwuid();
403
404if(file==0 || meteron==0) return;
405
406pwd = getpwuid(getuid());
407
408time(&tvec);
409
410if( (mout=fopen(file,"a")) != NULL )
411 {
412 p = ctime(&tvec);
413 p[16] = '\0';
414 fprintf(mout,"User %s, %s\n",pwd->pw_name,p+4);
415 fclose(mout);
416 }
417}
418#endif
419\f
420
421/* look inside archives for notations a(b) and a((b))
422 a(b) is file member b in archive a
423 a((b)) is entry point _b in object archive a
424*/
425
426#ifdef ASCARCH
427# include <ar.h>
428#else
429# include <ar.h>
430#endif
431#include <a.out.h>
432
433static long arflen;
434static long arfdate;
435static char arfname[16];
436FILE *arfd;
437long int arpos, arlen;
438
439static struct exec objhead;
440
441static struct nlist objentry;
442
443
444TIMETYPE lookarch(filename)
445char *filename;
446{
b342e078 447char *p, *q, *send, s[MAXNAMLEN + 1];
fbf6641c
BJ
448int i, nc, nsym, objarch;
449
450for(p = filename; *p!= '(' ; ++p)
451 ;
452*p = '\0';
453openarch(filename);
454*p++ = '(';
455
456if(*p == '(')
457 {
458 objarch = YES;
459 nc = 8;
460 ++p;
461 }
462else
463 {
464 objarch = NO;
b342e078 465 nc = MAXNAMLEN;
fbf6641c
BJ
466 }
467send = s + nc;
468
469for( q = s ; q<send && *p!='\0' && *p!=')' ; *q++ = *p++ )
470 ;
471while(q < send)
472 *q++ = '\0';
473while(getarch())
474 {
475 if(objarch)
476 {
477 getobj();
478 nsym = objhead.a_syms / sizeof(objentry);
479 for(i = 0; i<nsym ; ++i)
480 {
481 fread( (char *) &objentry, sizeof(objentry),1,arfd);
482 if( (objentry.n_type & N_EXT)
483 && ((objentry.n_type & ~N_EXT) || objentry.n_value)
484 && eqstr(objentry.n_un.n_name,s,nc))
485 {
486 clarch();
487 return(arfdate);
488 }
489 }
490 }
491
492 else if( eqstr(arfname, s, nc))
493 {
494 clarch();
495 return(arfdate);
496 }
497 }
498
499clarch();
500return( 0L);
501}
502
503
504clarch()
505{
506fclose( arfd );
507}
508
509
510openarch(f)
511register char *f;
512{
513#ifdef ASCARCH
514char magic[SARMAG];
515#endif
516int word;
517#include <sys/stat.h>
518struct stat buf;
519
520stat(f, &buf);
521arlen = buf.st_size;
522
523arfd = fopen(f, "r");
524if(arfd == NULL)
525 fatal1("cannot open %s", f);
526
527 fread( (char *) &word, sizeof(word), 1, arfd);
528#ifdef ASCARCH
529 fseek(arfd, 0L, 0);
530 fread(magic, SARMAG, 1, arfd);
531 arpos = SARMAG;
532 if( ! eqstr(magic, ARMAG, SARMAG) )
533#else
534 arpos = sizeof(word);
535 if(word != ARMAG)
536#endif
537 fatal1("%s is not an archive", f);
538
539arflen = 0;
540}
541
542
543
544getarch()
545{
546 struct ar_hdr arhead;
547 long atol();
548
549arpos += (arflen + 1) & ~1L; /* round archived file length up to even */
550if(arpos >= arlen)
551 return(0);
552fseek(arfd, arpos, 0);
553
554 fread( (char *) &arhead, sizeof(arhead), 1, arfd);
555 arpos += sizeof(arhead);
556#ifdef ASCARCH
557 arflen = atol(arhead.ar_size);
558 arfdate = atol(arhead.ar_date);
559#else
560 arflen = arhead.ar_size;
561 arfdate = arhead.ar_date;
562#endif
563 strncpy(arfname, arhead.ar_name, sizeof(arhead.ar_name));
564return(1);
565}
566
567
568getobj()
569{
570long int skip;
571
572fread( (char *) &objhead, sizeof(objhead), 1, arfd);
573if (N_BADMAG(objhead))
574 fatal1("%s is not an object module", arfname);
575skip = objhead.a_text + objhead.a_data;
4b952a85 576#ifndef pdp11
fbf6641c
BJ
577skip += objhead.a_trsize + objhead.a_drsize;
578#else
579if(! objhead.a_flag )
580 skip *= 2;
581#endif
582fseek(arfd, skip, 1);
583}
584
585
586eqstr(a,b,n)
587register char *a, *b;
588int n;
589{
590register int i;
591for(i = 0 ; i < n ; ++i)
592 if(*a++ != *b++)
593 return(NO);
594return(YES);
595}
a10b24d4
KM
596
597
598/*
599 * findfl(name) (like execvp, but does path search and finds files)
600 */
601static char fname[128];
602
603char *execat();
604
605char *findfl(name)
606register char *name;
607{
608 register char *p;
609 register struct varblock *cp;
610 struct stat buf;
eb1efbca 611 struct varblock *varptr();
a10b24d4
KM
612
613 for (p = name; *p; p++)
614 if(*p == '/') return(name);
615
616 cp = varptr("VPATH");
617 if(cp->varval == NULL || *cp->varval == 0)
618 p = ":";
619 else
620 p = cp->varval;
621
622 do
623 {
624 p = execat(p, name, fname);
625 if(stat(fname,&buf) >= 0)
626 return(fname);
627 } while (p);
628 return((char *)-1);
629}
630
631char *execat(s1, s2, si)
632register char *s1, *s2;
633char *si;
634{
635 register char *s;
636
637 s = si;
638 while (*s1 && *s1 != ':' && *s1 != '-')
639 *s++ = *s1++;
640 if (si != s)
641 *s++ = '/';
642 while (*s2)
643 *s++ = *s2++;
644 *s = '\0';
645 return(*s1? ++s1: 0);
646}
647
648
649/* copy s to d, changing file names to file aliases */
650fixname(s, d)
651char *s, *d;
652{
653 register char *r, *q;
654 struct nameblock *pn;
dbf5cc74 655 char name[BUFSIZ];
a10b24d4
KM
656
657 while (*s) {
658 if (isspace(*s)) *d++ = *s++;
659 else {
660 r = name;
661 while (*s) {
662 if (isspace(*s)) break;
663 *r++ = *s++;
664 }
665 *r = '\0';
666
667 if (((pn = srchname(name)) != 0) && (pn->alias))
668 q = pn->alias;
669 else q = name;
670
671 while (*q) *d++ = *q++;
672 }
673 }
674 *d = '\0';
675}