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