- 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));
- }
- else if (EQ(a, "-nouser")) {
- return (mk(nouser, (struct anode *)0, (struct anode *)0));
- }
- else if (EQ(a, "-nogroup")) {
- return (mk(nogroup, (struct anode *)0, (struct anode *)0));
- }
- else if (EQ(a, "-ls")) {
- return (mk(ls, (struct anode *)0, (struct anode *)0));
- }
- else if (EQ(a, "-xdev")) {
- Xdev = 0;
- return (mk(dummy, (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=getuid(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=getgid(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 :
- s=='s' ? S_IFSOCK :
- 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;
-{
- if (Nn >= MAXNODES) {
- fprintf(stderr, "find: Too many options\n");
- exit(1);
- }
-
- 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(p)
-struct anode *p;
-{
- 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));
-}
-nouser(p)
-struct anode *p;
-{
- char *getname();
-
- return (getname(Statb.st_uid) == NULL);
-}
-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);
-}
-nogroup(p)
-struct anode *p;
-{
- char *getgroup();
-
- return (getgroup(Statb.st_gid) == NULL);
-}
-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(p)
-struct anode *p;
-{
-#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;