# include "../sccshead/sfile.h"
# include "../sccshead/statbuf.h"
# include "../sccshead/sint.h"

char dofile___[] "~|^`dofile.c	3.2";

int	nfiles;
char	had_dir;
char	had_standinp;


do_file(p,func)
register char *p;
int (*func)();
{
	extern char *fatal_file;
	char str[SIZEOFPfile];
	struct Ibufr ibuf;
	struct Statbuf statbuf;
	struct {
		int  inum;
		char name[14];
		char extra;
	} dir;
	register char *s;
	int fd;

	if (p[0] == '-') {
		had_standinp = 1;
		opnl(&ibuf,0,0);
		while (getl(&ibuf) != 1) {
			s = ibuf.Irecptr;
			s[ibuf.Ilen-1] = 0;
			if (sccsfile(s)) {
				fatal_file = s;
				(*func)(s);
				nfiles++;
			}
		}
	}
	else if(stat(p,&statbuf) != -1 &&
		(statbuf.flags&IFMT) == IFDIR) {
		had_dir = 1;
		fatal_file = p;
		if((fd=open(p,0)) < 0)
			fatal("can't read (204)");
		read(fd,&dir,16);   /* skip "."  */
		read(fd,&dir,16);   /* skip ".." */
		while(read(fd,&dir,16) == 16) {
			if(dir.inum == 0) continue;
			dir.extra = '\0';
			s=stringf("%s/%s",p,dir.name);
			if(size(s) <= SIZEOFPfile) copy(s,str);
			else fatal("too long (205)");
			if(sccsfile(str)) {
				fatal_file = str;
				(*func)(str);
				nfiles++;
			}
		}
		close(fd);
	}
	else {
		fatal_file = p;
		(*func)(p);
		nfiles++;
	}
}


sccsfile(file)
register char *file;
{
	register int ff, result;
	int magic[2];

	result = (ff=open(file,0)) > 0
	  && read(ff,magic,4) == 4
	  && magic[1] == MAGICNO;
	close(ff);
	return(result);
}
