- a = nxtarg();
- if(EQ(a, "(")) {
- Randlast--;
- p1 = exp();
- a = nxtarg();
- if(!EQ(a, ")")) goto err;
- return(p1);
- }
- else if(EQ(a, "-print")) {
- return(mk(print, (struct anode *)0, (struct anode *)0));
- }
- b = nxtarg();
- s = *b;
- if(s=='+') b++;
- if(EQ(a, "-name"))
- return(mk(glob, (struct anode *)b, (struct anode *)0));
- else if(EQ(a, "-mtime"))
- return(mk(mtime, (struct anode *)atoi(b), (struct anode *)s));
- else if(EQ(a, "-atime"))
- return(mk(atime, (struct anode *)atoi(b), (struct anode *)s));
- else if(EQ(a, "-user")) {
- if((i=getunum("/etc/passwd", b)) == -1) {
- if(gmatch(b, "[0-9]*"))
- return mk(user, (struct anode *)atoi(b), (struct anode *)s);
- fprintf(stderr, "find: cannot find -user name\n");
- exit(1);
- }
- return(mk(user, (struct anode *)i, (struct anode *)s));
- }
- else if(EQ(a, "-inum"))
- return(mk(ino, (struct anode *)atoi(b), (struct anode *)s));
- else if(EQ(a, "-group")) {
- if((i=getunum("/etc/group", b)) == -1) {
- if(gmatch(b, "[0-9]*"))
- return mk(group, (struct anode *)atoi(b), (struct anode *)s);
- fprintf(stderr, "find: cannot find -group name\n");
- exit(1);
- }
- return(mk(group, (struct anode *)i, (struct anode *)s));
- } else if(EQ(a, "-size"))
- return(mk(size, (struct anode *)atoi(b), (struct anode *)s));
- else if(EQ(a, "-links"))
- return(mk(links, (struct anode *)atoi(b), (struct anode *)s));
- else if(EQ(a, "-perm")) {
- for(i=0; *b ; ++b) {
- if(*b=='-') continue;
- i <<= 3;
- i = i + (*b - '0');
- }
- return(mk(perm, (struct anode *)i, (struct anode *)s));
- }
- else if(EQ(a, "-type")) {
- i = s=='d' ? S_IFDIR :
- s=='b' ? S_IFBLK :
- s=='c' ? S_IFCHR :
- s=='f' ? S_IFREG :
- s=='l' ? S_IFLNK :
- 0;
- return(mk(type, (struct anode *)i, (struct anode *)0));
- }
- else if (EQ(a, "-exec")) {
- i = Ai - 1;
- while(!EQ(nxtarg(), ";"));
- return(mk(exeq, (struct anode *)i, (struct anode *)0));
- }
- else if (EQ(a, "-ok")) {
- i = Ai - 1;
- while(!EQ(nxtarg(), ";"));
- return(mk(ok, (struct anode *)i, (struct anode *)0));
- }
- else if(EQ(a, "-cpio")) {
- if((Cpio = creat(b, 0666)) < 0) {
- fprintf(stderr, "find: cannot create < %s >\n", s);
- exit(1);
- }
- Buf = (short *)sbrk(512);
- Wp = Dbuf = (short *)sbrk(5120);
- return(mk(cpio, (struct anode *)0, (struct anode *)0));
- }
- else if(EQ(a, "-newer")) {
- if(stat(b, &Statb) < 0) {
- fprintf(stderr, "find: cannot access < %s >\n", b);
- exit(1);
- }
- Newer = Statb.st_mtime;
- return mk(newer, (struct anode *)0, (struct anode *)0);
- }
-err: fprintf(stderr, "find: bad option < %s >\n", a);
- exit(1);
-}
-struct anode *mk(f, l, r)
-int (*f)();
-struct anode *l, *r;
-{
- Node[Nn].F = f;
- Node[Nn].L = l;
- Node[Nn].R = r;
- return(&(Node[Nn++]));
-}
-
-char *nxtarg() { /* get next arg from command line */
- static strikes = 0;
-
- if(strikes==3) {
- fprintf(stderr, "find: incomplete statement\n");
- exit(1);
- }
- if(Ai>=Argc) {
- strikes++;
- Ai = Argc + 1;
- return("");
- }
- return(Argv[Ai++]);
-}
-
-/* execution time functions */
-and(p)
-register struct anode *p;
-{
- return(((*p->L->F)(p->L)) && ((*p->R->F)(p->R))?1:0);
-}
-or(p)
-register struct anode *p;
-{
- return(((*p->L->F)(p->L)) || ((*p->R->F)(p->R))?1:0);
-}
-not(p)
-register struct anode *p;
-{
- return( !((*p->L->F)(p->L)));
-}
-glob(p)
-register struct { int f; char *pat; } *p;
-{
- return(gmatch(Fname, p->pat));
-}
-print()
-{
- puts(Pathname);
- return(1);
-}
-mtime(p)
-register struct { int f, t, s; } *p;
-{
- return(scomp((int)((Now - Statb.st_mtime) / A_DAY), p->t, p->s));
-}
-atime(p)
-register struct { int f, t, s; } *p;
-{
- return(scomp((int)((Now - Statb.st_atime) / A_DAY), p->t, p->s));
-}
-user(p)
-register struct { int f, u, s; } *p;
-{
- return(scomp(Statb.st_uid, p->u, p->s));
-}
-ino(p)
-register struct { int f, u, s; } *p;
-{
- return(scomp((int)Statb.st_ino, p->u, p->s));
-}
-group(p)
-register struct { int f, u; } *p;
-{
- return(p->u == Statb.st_gid);
-}
-links(p)
-register struct { int f, link, s; } *p;
-{
- return(scomp(Statb.st_nlink, p->link, p->s));
-}
-size(p)
-register struct { int f, sz, s; } *p;
-{
- return(scomp((int)((Statb.st_size+511)>>9), p->sz, p->s));
-}
-perm(p)
-register struct { int f, per, s; } *p;
-{
- register i;
- i = (p->s=='-') ? p->per : 07777; /* '-' means only arg bits */
- return((Statb.st_mode & i & 07777) == p->per);
-}
-type(p)
-register struct { int f, per, s; } *p;
-{
- return((Statb.st_mode&S_IFMT)==p->per);
-}
-exeq(p)
-register struct { int f, com; } *p;
-{
- fflush(stdout); /* to flush possible `-print' */
- return(doex(p->com));
-}
-ok(p)
-struct { int f, com; } *p;
-{
- char c; int yes;
- yes = 0;
- fflush(stdout); /* to flush possible `-print' */
- fprintf(stderr, "< %s ... %s > ? ", Argv[p->com], Pathname);
- fflush(stderr);
- if((c=getchar())=='y') yes = 1;
- while(c!='\n') c = getchar();
- if(yes) return(doex(p->com));
- return(0);
-}
-
-#define MKSHORT(v, lv) {U.l=1L;if(U.c[0]) U.l=lv, v[0]=U.s[1], v[1]=U.s[0]; else U.l=lv, v[0]=U.s[0], v[1]=U.s[1];}
-union { long l; short s[2]; char c[4]; } U;
-long mklong(v)
-short v[];
-{
- U.l = 1;
- if(U.c[0] /* VAX */)
- U.s[0] = v[1], U.s[1] = v[0];
- else
- U.s[0] = v[0], U.s[1] = v[1];
- return U.l;
-}
-cpio()
-{
-#define MAGIC 070707
- struct header {
- short h_magic,
- h_dev,
- h_ino,
- h_mode,
- h_uid,
- h_gid,
- h_nlink,
- h_rdev;
- short h_mtime[2];
- short h_namesize;
- short h_filesize[2];
- char h_name[256];
- } hdr;
- register ifile, ct;
- static long fsz;
- register i;
-
- hdr.h_magic = MAGIC;
- strcpy(hdr.h_name, !strncmp(Pathname, "./", 2)? Pathname+2: Pathname);
- hdr.h_namesize = strlen(hdr.h_name) + 1;
- hdr.h_uid = Statb.st_uid;
- hdr.h_gid = Statb.st_gid;
- hdr.h_dev = Statb.st_dev;
- hdr.h_ino = Statb.st_ino;
- hdr.h_mode = Statb.st_mode;
- MKSHORT(hdr.h_mtime, Statb.st_mtime);
- hdr.h_nlink = Statb.st_nlink;
- fsz = hdr.h_mode & S_IFREG? Statb.st_size: 0L;
- MKSHORT(hdr.h_filesize, fsz);
- hdr.h_rdev = Statb.st_rdev;
- if(EQ(hdr.h_name, "TRAILER!!!")) {
- bwrite((short *)&hdr, (sizeof hdr-256)+hdr.h_namesize);
- for(i = 0; i < 10; ++i)
- bwrite(Buf, 512);
- return;
- }
- if(!mklong(hdr.h_filesize))
- return;
- if((ifile = open(Fname, 0)) < 0) {
-cerror:
- fprintf(stderr, "find: cannot copy < %s >\n", hdr.h_name);
- return;
- }
- bwrite((short *)&hdr, (sizeof hdr-256)+hdr.h_namesize);
- for(fsz = mklong(hdr.h_filesize); fsz > 0; fsz -= 512) {
- ct = fsz>512? 512: fsz;
- if(read(ifile, (char *)Buf, ct) < 0)
- goto cerror;
- bwrite(Buf, ct);
- }
- close(ifile);
- return;
-}
-newer()
-{
- return Statb.st_mtime > Newer;
-}
-
-/* support functions */
-scomp(a, b, s) /* funny signed compare */
-register a, b;
-register char s;
-{
- if(s == '+')
- return(a > b);
- if(s == '-')
- return(a < (b * -1));
- return(a == b);
-}
-
-doex(com)
-{
- register np;
- register char *na;
- static char *nargv[50];
- static ccode;
-
- ccode = np = 0;
- while (na=Argv[com++]) {
- if(strcmp(na, ";")==0) break;
- if(strcmp(na, "{}")==0) nargv[np++] = Pathname;
- else nargv[np++] = na;
- }
- nargv[np] = 0;
- if (np==0) return(9);
- if(fork()) /*parent*/ {
-#include <signal.h>
- int (*old)() = signal(SIGINT, SIG_IGN);
- int (*oldq)() = signal(SIGQUIT, SIG_IGN);
- wait(&ccode);
- signal(SIGINT, old);
- signal(SIGQUIT, oldq);
- } else { /*child*/
- chdir(Home);
- execvp(nargv[0], nargv, np);
- exit(1);
- }
- return(ccode ? 0:1);
-}