1: #ifndef lint
   2: static char *sccsid = "@(#)test.c	4.2 (Berkeley) 5/11/86";
   3: #endif
   4: 
   5: /*
   6:  *	test expression
   7:  *	[ expression ]
   8:  */
   9: 
  10: #include <stdio.h>
  11: #include <sys/types.h>
  12: #include <sys/stat.h>
  13: #define EQ(a,b) ((tmp=a)==0?0:(strcmp(tmp,b)==0))
  14: 
  15: #define DIR 1
  16: #define FIL 2
  17: int ap;
  18: int ac;
  19: char    **av;
  20: char    *tmp;
  21: char    *nxtarg();
  22: 
  23: main(argc, argv)
  24: char *argv[];
  25: {
  26:     int status;
  27: 
  28:     ac = argc; av = argv; ap = 1;
  29:     if(EQ(argv[0],"[")) {
  30:         if(!EQ(argv[--ac],"]"))
  31:             synbad("] missing","");
  32:     }
  33:     argv[ac] = 0;
  34:     if (ac<=1) exit(1);
  35:     status = (exp()?0:1);
  36:     if (nxtarg(1)!=0)
  37:         synbad("too many arguments","");
  38:     exit(status);
  39: }
  40: 
  41: char *nxtarg(mt) {
  42: 
  43:     if (ap>=ac) {
  44:         if(mt) {
  45:             ap++;
  46:             return(0);
  47:         }
  48:         synbad("argument expected","");
  49:     }
  50:     return(av[ap++]);
  51: }
  52: 
  53: exp() {
  54:     int p1;
  55: 
  56:     p1 = e1();
  57:     if (EQ(nxtarg(1), "-o")) return(p1 | exp());
  58:     ap--;
  59:     return(p1);
  60: }
  61: 
  62: e1() {
  63:     int p1;
  64: 
  65:     p1 = e2();
  66:     if (EQ(nxtarg(1), "-a")) return (p1 & e1());
  67:     ap--;
  68:     return(p1);
  69: }
  70: 
  71: e2() {
  72:     if (EQ(nxtarg(0), "!"))
  73:         return(!e3());
  74:     ap--;
  75:     return(e3());
  76: }
  77: 
  78: e3() {
  79:     int p1;
  80:     register char *a;
  81:     char *p2;
  82:     int int1;
  83: 
  84:     a=nxtarg(0);
  85:     if(EQ(a, "(")) {
  86:         p1 = exp();
  87:         if(!EQ(nxtarg(0), ")")) synbad(") expected","");
  88:         return(p1);
  89:     }
  90: 
  91:     if(EQ(a, "-r"))
  92:         return(tio(nxtarg(0), 0));
  93: 
  94:     if(EQ(a, "-w"))
  95:         return(tio(nxtarg(0), 1));
  96: 
  97:     if(EQ(a, "-d"))
  98:         return(ftype(nxtarg(0))==DIR);
  99: 
 100:     if(EQ(a, "-f"))
 101:         return(ftype(nxtarg(0))==FIL);
 102: 
 103:     if(EQ(a, "-s"))
 104:         return(fsizep(nxtarg(0)));
 105: 
 106:     if(EQ(a, "-t"))
 107:         if(ap>=ac)
 108:             return(isatty(1));
 109:         else
 110:             return(isatty(atoi(nxtarg(0))));
 111: 
 112:     if(EQ(a, "-n"))
 113:         return(!EQ(nxtarg(0), ""));
 114:     if(EQ(a, "-z"))
 115:         return(EQ(nxtarg(0), ""));
 116: 
 117:     p2 = nxtarg(1);
 118:     if (p2==0)
 119:         return(!EQ(a,""));
 120:     if(EQ(p2, "="))
 121:         return(EQ(nxtarg(0), a));
 122: 
 123:     if(EQ(p2, "!="))
 124:         return(!EQ(nxtarg(0), a));
 125: 
 126:     if(EQ(a, "-l")) {
 127:         int1=length(p2);
 128:         p2=nxtarg(0);
 129:     } else{ int1=atoi(a);
 130:     }
 131:     if(EQ(p2, "-eq"))
 132:         return(int1==atoi(nxtarg(0)));
 133:     if(EQ(p2, "-ne"))
 134:         return(int1!=atoi(nxtarg(0)));
 135:     if(EQ(p2, "-gt"))
 136:         return(int1>atoi(nxtarg(0)));
 137:     if(EQ(p2, "-lt"))
 138:         return(int1<atoi(nxtarg(0)));
 139:     if(EQ(p2, "-ge"))
 140:         return(int1>=atoi(nxtarg(0)));
 141:     if(EQ(p2, "-le"))
 142:         return(int1<=atoi(nxtarg(0)));
 143: 
 144:     --ap;
 145:     return(!EQ(a,""));
 146: }
 147: 
 148: tio(a, f)
 149: char *a;
 150: int f;
 151: {
 152: 
 153:     f = open(a, f);
 154:     if (f>=0) {
 155:         (void) close(f);
 156:         return(1);
 157:     }
 158:     return(0);
 159: }
 160: 
 161: ftype(f)
 162: char *f;
 163: {
 164:     struct stat statb;
 165: 
 166:     if(stat(f,&statb)<0)
 167:         return(0);
 168:     if((statb.st_mode&S_IFMT)==S_IFDIR)
 169:         return(DIR);
 170:     return(FIL);
 171: }
 172: 
 173: fsizep(f)
 174: char *f;
 175: {
 176:     struct stat statb;
 177:     if(stat(f,&statb)<0)
 178:         return(0);
 179:     return(statb.st_size>0);
 180: }
 181: 
 182: synbad(s1,s2)
 183: char *s1, *s2;
 184: {
 185:     (void) write(2, "test: ", 6);
 186:     (void) write(2, s1, strlen(s1));
 187:     (void) write(2, s2, strlen(s2));
 188:     (void) write(2, "\n", 1);
 189:     exit(255);
 190: }
 191: 
 192: length(s)
 193:     char *s;
 194: {
 195:     char *es=s;
 196:     while(*es++);
 197:     return(es-s-1);
 198: }

Defined functions

e1 defined in line 62; used 2 times
e2 defined in line 71; used 1 times
  • in line 65
e3 defined in line 78; used 2 times
exp defined in line 53; used 3 times
fsizep defined in line 173; used 1 times
ftype defined in line 161; used 2 times
length defined in line 192; used 1 times
main defined in line 23; never used
nxtarg defined in line 41; used 25 times
synbad defined in line 182; used 4 times
tio defined in line 148; used 2 times

Defined variables

ac defined in line 18; used 6 times
ap defined in line 17; used 9 times
av defined in line 19; used 2 times
sccsid defined in line 2; never used
tmp defined in line 20; used 2 times
  • in line 13(2)

Defined macros

DIR defined in line 15; used 2 times
EQ defined in line 13; used 30 times
FIL defined in line 16; used 2 times
Last modified: 1986-05-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1221
Valid CSS Valid XHTML 1.0 Strict