Home | History | Annotate | Line # | Download | only in ksh
c_test.c revision 1.2.6.1
      1  1.2.6.1  wrstuden /*	$NetBSD: c_test.c,v 1.2.6.1 1999/12/27 18:27:01 wrstuden Exp $	*/
      2      1.2       tls 
      3      1.1       jtc /*
      4      1.1       jtc  * test(1); version 7-like  --  author Erik Baalbergen
      5      1.1       jtc  * modified by Eric Gisin to be used as built-in.
      6      1.1       jtc  * modified by Arnold Robbins to add SVR3 compatibility
      7      1.1       jtc  * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
      8      1.1       jtc  * modified by Michael Rendell to add Korn's [[ .. ]] expressions.
      9      1.1       jtc  * modified by J.T. Conklin to add POSIX compatibility.
     10      1.1       jtc  */
     11      1.1       jtc 
     12      1.1       jtc #include "sh.h"
     13      1.1       jtc #include "ksh_stat.h"
     14      1.1       jtc #include "c_test.h"
     15      1.1       jtc 
     16      1.1       jtc /* test(1) accepts the following grammar:
     17      1.1       jtc 	oexpr	::= aexpr | aexpr "-o" oexpr ;
     18      1.1       jtc 	aexpr	::= nexpr | nexpr "-a" aexpr ;
     19      1.1       jtc 	nexpr	::= primary | "!" nexpr ;
     20      1.1       jtc 	primary	::= unary-operator operand
     21      1.1       jtc 		| operand binary-operator operand
     22      1.1       jtc 		| operand
     23      1.1       jtc 		| "(" oexpr ")"
     24      1.1       jtc 		;
     25      1.1       jtc 
     26      1.1       jtc 	unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
     27      1.1       jtc 			   "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
     28      1.1       jtc 			   "-L"|"-h"|"-S"|"-H";
     29      1.1       jtc 
     30      1.1       jtc 	binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
     31      1.1       jtc 			    "-nt"|"-ot"|"-ef"|
     32      1.1       jtc 			    "<"|">"	# rules used for [[ .. ]] expressions
     33      1.1       jtc 			    ;
     34      1.1       jtc 	operand ::= <any thing>
     35      1.1       jtc */
     36      1.1       jtc 
     37      1.1       jtc #define T_ERR_EXIT	2	/* POSIX says > 1 for errors */
     38      1.1       jtc 
     39      1.1       jtc struct t_op {
     40      1.1       jtc 	char	op_text[4];
     41      1.1       jtc 	Test_op	op_num;
     42      1.1       jtc };
     43      1.1       jtc static const struct t_op u_ops [] = {
     44      1.1       jtc 	{"-a",	TO_FILAXST },
     45      1.1       jtc 	{"-b",	TO_FILBDEV },
     46      1.1       jtc 	{"-c",	TO_FILCDEV },
     47      1.1       jtc 	{"-d",	TO_FILID },
     48      1.1       jtc 	{"-e",	TO_FILEXST },
     49      1.1       jtc 	{"-f",	TO_FILREG },
     50      1.1       jtc 	{"-G",	TO_FILGID },
     51      1.1       jtc 	{"-g",	TO_FILSETG },
     52      1.1       jtc 	{"-h",	TO_FILSYM },
     53      1.1       jtc 	{"-H",	TO_FILCDF },
     54      1.1       jtc 	{"-k",	TO_FILSTCK },
     55      1.1       jtc 	{"-L",	TO_FILSYM },
     56      1.1       jtc 	{"-n",	TO_STNZE },
     57      1.1       jtc 	{"-O",	TO_FILUID },
     58      1.1       jtc 	{"-o",	TO_OPTION },
     59      1.1       jtc 	{"-p",	TO_FILFIFO },
     60      1.1       jtc 	{"-r",	TO_FILRD },
     61      1.1       jtc 	{"-s",	TO_FILGZ },
     62      1.1       jtc 	{"-S",	TO_FILSOCK },
     63      1.1       jtc 	{"-t",	TO_FILTT },
     64      1.1       jtc 	{"-u",	TO_FILSETU },
     65      1.1       jtc 	{"-w",	TO_FILWR },
     66      1.1       jtc 	{"-x",	TO_FILEX },
     67      1.1       jtc 	{"-z",	TO_STZER },
     68      1.1       jtc 	{"",	TO_NONOP }
     69      1.1       jtc     };
     70      1.1       jtc static const struct t_op b_ops [] = {
     71      1.1       jtc 	{"=",	TO_STEQL },
     72      1.1       jtc #ifdef KSH
     73      1.1       jtc 	{"==",	TO_STEQL },
     74      1.1       jtc #endif /* KSH */
     75      1.1       jtc 	{"!=",	TO_STNEQ },
     76      1.1       jtc 	{"<",	TO_STLT },
     77      1.1       jtc 	{">",	TO_STGT },
     78      1.1       jtc 	{"-eq",	TO_INTEQ },
     79      1.1       jtc 	{"-ne",	TO_INTNE },
     80      1.1       jtc 	{"-gt",	TO_INTGT },
     81      1.1       jtc 	{"-ge",	TO_INTGE },
     82      1.1       jtc 	{"-lt",	TO_INTLT },
     83      1.1       jtc 	{"-le",	TO_INTLE },
     84      1.1       jtc 	{"-ef",	TO_FILEQ },
     85      1.1       jtc 	{"-nt",	TO_FILNT },
     86      1.1       jtc 	{"-ot",	TO_FILOT },
     87      1.1       jtc 	{"",	TO_NONOP }
     88      1.1       jtc     };
     89      1.1       jtc 
     90      1.1       jtc static int	test_stat ARGS((const char *path, struct stat *statb));
     91      1.1       jtc static int	test_eaccess ARGS((const char *path, int mode));
     92      1.1       jtc static int	test_oexpr ARGS((Test_env *te, int do_eval));
     93      1.1       jtc static int	test_aexpr ARGS((Test_env *te, int do_eval));
     94      1.1       jtc static int	test_nexpr ARGS((Test_env *te, int do_eval));
     95      1.1       jtc static int	test_primary ARGS((Test_env *te, int do_eval));
     96      1.1       jtc static int	ptest_isa ARGS((Test_env *te, Test_meta meta));
     97      1.1       jtc static const char *ptest_getopnd ARGS((Test_env *te, Test_op op, int do_eval));
     98      1.1       jtc static int	ptest_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
     99      1.1       jtc 				const char *opnd2, int do_eval));
    100      1.1       jtc static void	ptest_error ARGS((Test_env *te, int offset, const char *msg));
    101      1.1       jtc 
    102      1.1       jtc int
    103      1.1       jtc c_test(wp)
    104      1.1       jtc 	char **wp;
    105      1.1       jtc {
    106      1.1       jtc 	int argc;
    107      1.1       jtc 	int res;
    108      1.1       jtc 	Test_env te;
    109      1.1       jtc 
    110      1.1       jtc 	te.flags = 0;
    111      1.1       jtc 	te.isa = ptest_isa;
    112      1.1       jtc 	te.getopnd = ptest_getopnd;
    113      1.1       jtc 	te.eval = ptest_eval;
    114      1.1       jtc 	te.error = ptest_error;
    115      1.1       jtc 
    116      1.1       jtc 	for (argc = 0; wp[argc]; argc++)
    117      1.1       jtc 		;
    118      1.1       jtc 
    119      1.1       jtc 	if (strcmp(wp[0], "[") == 0) {
    120      1.1       jtc 		if (strcmp(wp[--argc], "]") != 0) {
    121      1.1       jtc 			bi_errorf("missing ]");
    122      1.1       jtc 			return T_ERR_EXIT;
    123      1.1       jtc 		}
    124      1.1       jtc 	}
    125      1.1       jtc 
    126      1.1       jtc 	te.pos.wp = wp + 1;
    127      1.1       jtc 	te.wp_end = wp + argc;
    128      1.1       jtc 
    129      1.1       jtc 	/*
    130      1.1       jtc 	 * Handle the special cases from POSIX.2, section 4.62.4.
    131      1.1       jtc 	 * Implementation of all the rules isn't necessary since
    132      1.1       jtc 	 * our parser does the right thing for the ommited steps.
    133      1.1       jtc 	 */
    134      1.1       jtc 	if (argc <= 5) {
    135      1.1       jtc 		char **owp = wp;
    136      1.1       jtc 		int invert = 0;
    137      1.1       jtc 		Test_op	op;
    138      1.1       jtc 		const char *opnd1, *opnd2;
    139      1.1       jtc 
    140      1.1       jtc 		while (--argc >= 0) {
    141      1.1       jtc 			if ((*te.isa)(&te, TM_END))
    142      1.1       jtc 				return !0;
    143      1.1       jtc 			if (argc == 3) {
    144      1.1       jtc 				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
    145      1.1       jtc 				if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
    146      1.1       jtc 					opnd2 = (*te.getopnd)(&te, op, 1);
    147      1.1       jtc 					res = (*te.eval)(&te, op, opnd1, opnd2,
    148      1.1       jtc 							1);
    149      1.1       jtc 					if (te.flags & TEF_ERROR)
    150      1.1       jtc 						return T_ERR_EXIT;
    151      1.1       jtc 					if (invert & 1)
    152      1.1       jtc 						res = !res;
    153      1.1       jtc 					return !res;
    154      1.1       jtc 				}
    155      1.1       jtc 				/* back up to opnd1 */
    156      1.1       jtc 				te.pos.wp--;
    157      1.1       jtc 			}
    158      1.1       jtc 			if (argc == 1) {
    159      1.1       jtc 				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
    160  1.2.6.1  wrstuden 				/* Historically, -t by itself test if fd 1
    161  1.2.6.1  wrstuden 				 * is a file descriptor, but POSIX says its
    162  1.2.6.1  wrstuden 				 * a string test...
    163  1.2.6.1  wrstuden 				 */
    164  1.2.6.1  wrstuden 				if (!Flag(FPOSIX) && strcmp(opnd1, "-t") == 0)
    165  1.2.6.1  wrstuden 				    break;
    166      1.1       jtc 				res = (*te.eval)(&te, TO_STNZE, opnd1,
    167      1.1       jtc 						(char *) 0, 1);
    168      1.1       jtc 				if (invert & 1)
    169      1.1       jtc 					res = !res;
    170      1.1       jtc 				return !res;
    171      1.1       jtc 			}
    172      1.1       jtc 			if ((*te.isa)(&te, TM_NOT)) {
    173      1.1       jtc 				invert++;
    174      1.1       jtc 			} else
    175      1.1       jtc 				break;
    176      1.1       jtc 		}
    177      1.1       jtc 		te.pos.wp = owp + 1;
    178      1.1       jtc 	}
    179      1.1       jtc 
    180      1.1       jtc 	return test_parse(&te);
    181      1.1       jtc }
    182      1.1       jtc 
    183      1.1       jtc /*
    184      1.1       jtc  * Generic test routines.
    185      1.1       jtc  */
    186      1.1       jtc 
    187      1.1       jtc Test_op
    188      1.1       jtc test_isop(te, meta, s)
    189      1.1       jtc 	Test_env *te;
    190      1.1       jtc 	Test_meta meta;
    191      1.1       jtc 	const char *s;
    192      1.1       jtc {
    193      1.1       jtc 	char sc1;
    194      1.1       jtc 	const struct t_op *otab;
    195      1.1       jtc 
    196      1.1       jtc 	otab = meta == TM_UNOP ? u_ops : b_ops;
    197      1.1       jtc 	if (*s) {
    198      1.1       jtc 		sc1 = s[1];
    199      1.1       jtc 		for (; otab->op_text[0]; otab++)
    200      1.1       jtc 			if (sc1 == otab->op_text[1]
    201      1.1       jtc 			    && strcmp(s, otab->op_text) == 0
    202      1.1       jtc 			    && ((te->flags & TEF_DBRACKET)
    203      1.1       jtc 				|| (otab->op_num != TO_STLT
    204      1.1       jtc 				    && otab->op_num != TO_STGT)))
    205      1.1       jtc 				return otab->op_num;
    206      1.1       jtc 	}
    207      1.1       jtc 	return TO_NONOP;
    208      1.1       jtc }
    209      1.1       jtc 
    210      1.1       jtc int
    211      1.1       jtc test_eval(te, op, opnd1, opnd2, do_eval)
    212      1.1       jtc 	Test_env *te;
    213      1.1       jtc 	Test_op op;
    214      1.1       jtc 	const char *opnd1;
    215      1.1       jtc 	const char *opnd2;
    216      1.1       jtc 	int do_eval;
    217      1.1       jtc {
    218      1.1       jtc 	int res;
    219      1.1       jtc 	int not;
    220      1.1       jtc 	struct stat b1, b2;
    221      1.1       jtc 
    222      1.1       jtc 	if (!do_eval)
    223      1.1       jtc 		return 0;
    224      1.1       jtc 
    225      1.1       jtc 	switch ((int) op) {
    226      1.1       jtc 	/*
    227      1.1       jtc 	 * Unary Operators
    228      1.1       jtc 	 */
    229      1.1       jtc 	  case TO_STNZE: /* -n */
    230      1.1       jtc 		return *opnd1 != '\0';
    231      1.1       jtc 	  case TO_STZER: /* -z */
    232      1.1       jtc 		return *opnd1 == '\0';
    233      1.1       jtc 	  case TO_OPTION: /* -o */
    234      1.1       jtc 		if ((not = *opnd1 == '!'))
    235      1.1       jtc 			opnd1++;
    236      1.1       jtc 		if ((res = option(opnd1)) < 0)
    237      1.1       jtc 			res = 0;
    238      1.1       jtc 		else {
    239      1.1       jtc 			res = Flag(res);
    240      1.1       jtc 			if (not)
    241      1.1       jtc 				res = !res;
    242      1.1       jtc 		}
    243      1.1       jtc 		return res;
    244      1.1       jtc 	  case TO_FILRD: /* -r */
    245      1.1       jtc 		return test_eaccess(opnd1, R_OK) == 0;
    246      1.1       jtc 	  case TO_FILWR: /* -w */
    247      1.1       jtc 		return test_eaccess(opnd1, W_OK) == 0;
    248      1.1       jtc 	  case TO_FILEX: /* -x */
    249      1.1       jtc 		return test_eaccess(opnd1, X_OK) == 0;
    250      1.1       jtc 	  case TO_FILAXST: /* -a */
    251      1.1       jtc 		return test_stat(opnd1, &b1) == 0;
    252      1.1       jtc 	  case TO_FILEXST: /* -e */
    253      1.1       jtc 		/* at&t ksh does not appear to do the /dev/fd/ thing for
    254      1.1       jtc 		 * this (unless the os itself handles it)
    255      1.1       jtc 		 */
    256      1.1       jtc 		return stat(opnd1, &b1) == 0;
    257      1.1       jtc 	  case TO_FILREG: /* -r */
    258      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
    259      1.1       jtc 	  case TO_FILID: /* -d */
    260      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
    261      1.1       jtc 	  case TO_FILCDEV: /* -c */
    262      1.1       jtc #ifdef S_ISCHR
    263      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
    264      1.1       jtc #else
    265      1.1       jtc 		return 0;
    266      1.1       jtc #endif
    267      1.1       jtc 	  case TO_FILBDEV: /* -b */
    268      1.1       jtc #ifdef S_ISBLK
    269      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
    270      1.1       jtc #else
    271      1.1       jtc 		return 0;
    272      1.1       jtc #endif
    273      1.1       jtc 	  case TO_FILFIFO: /* -p */
    274      1.1       jtc #ifdef S_ISFIFO
    275      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
    276      1.1       jtc #else
    277      1.1       jtc 		return 0;
    278      1.1       jtc #endif
    279      1.1       jtc 	  case TO_FILSYM: /* -h -L */
    280      1.1       jtc #ifdef S_ISLNK
    281      1.1       jtc 		return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
    282      1.1       jtc #else
    283      1.1       jtc 		return 0;
    284      1.1       jtc #endif
    285      1.1       jtc 	  case TO_FILSOCK: /* -S */
    286      1.1       jtc #ifdef S_ISSOCK
    287      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
    288      1.1       jtc #else
    289      1.1       jtc 		return 0;
    290      1.1       jtc #endif
    291      1.1       jtc 	  case TO_FILCDF:/* -H HP context dependent files (directories) */
    292      1.1       jtc #ifdef S_ISCDF
    293      1.1       jtc 	  {
    294      1.1       jtc 		/* Append a + to filename and check to see if result is a
    295      1.1       jtc 		 * setuid directory.  CDF stuff in general is hookey, since
    296      1.1       jtc 		 * it breaks for the following sequence: echo hi > foo+;
    297      1.1       jtc 		 * mkdir foo; echo bye > foo/default; chmod u+s foo
    298      1.1       jtc 		 * (foo+ refers to the file with hi in it, there is no way
    299      1.1       jtc 		 * to get at the file with bye in it - please correct me if
    300      1.1       jtc 		 * I'm wrong about this).
    301      1.1       jtc 		 */
    302      1.1       jtc 		int len = strlen(opnd1);
    303      1.1       jtc 		char *p = str_nsave(opnd1, len + 1, ATEMP);
    304      1.1       jtc 
    305      1.1       jtc 		p[len++] = '+';
    306      1.1       jtc 		p[len] = '\0';
    307      1.1       jtc 		return stat(p, &b1) == 0 && S_ISCDF(b1.st_mode);
    308      1.1       jtc 	  }
    309      1.1       jtc #else
    310      1.1       jtc 		return 0;
    311      1.1       jtc #endif
    312      1.1       jtc 	  case TO_FILSETU: /* -u */
    313      1.1       jtc #ifdef S_ISUID
    314      1.1       jtc 		return test_stat(opnd1, &b1) == 0
    315      1.1       jtc 			&& (b1.st_mode & S_ISUID) == S_ISUID;
    316      1.1       jtc #else
    317      1.1       jtc 		return 0;
    318      1.1       jtc #endif
    319      1.1       jtc 	  case TO_FILSETG: /* -g */
    320      1.1       jtc #ifdef S_ISGID
    321      1.1       jtc 		return test_stat(opnd1, &b1) == 0
    322      1.1       jtc 			&& (b1.st_mode & S_ISGID) == S_ISGID;
    323      1.1       jtc #else
    324      1.1       jtc 		return 0;
    325      1.1       jtc #endif
    326      1.1       jtc 	  case TO_FILSTCK: /* -k */
    327      1.1       jtc 		return test_stat(opnd1, &b1) == 0
    328      1.1       jtc 			&& (b1.st_mode & S_ISVTX) == S_ISVTX;
    329      1.1       jtc 	  case TO_FILGZ: /* -s */
    330      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L;
    331      1.1       jtc 	  case TO_FILTT: /* -t */
    332      1.1       jtc 		if (opnd1 && !bi_getn(opnd1, &res)) {
    333      1.1       jtc 			te->flags |= TEF_ERROR;
    334      1.1       jtc 			res = 0;
    335  1.2.6.1  wrstuden 		} else {
    336  1.2.6.1  wrstuden 			/* generate error if in FPOSIX mode? */
    337      1.1       jtc 			res = isatty(opnd1 ? res : 0);
    338  1.2.6.1  wrstuden 		}
    339      1.1       jtc 		return res;
    340      1.1       jtc 	  case TO_FILUID: /* -O */
    341  1.2.6.1  wrstuden 		return test_stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
    342      1.1       jtc 	  case TO_FILGID: /* -G */
    343      1.1       jtc 		return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
    344      1.1       jtc 	/*
    345      1.1       jtc 	 * Binary Operators
    346      1.1       jtc 	 */
    347      1.1       jtc 	  case TO_STEQL: /* = */
    348      1.1       jtc 		if (te->flags & TEF_DBRACKET)
    349      1.1       jtc 			return gmatch(opnd1, opnd2, FALSE);
    350      1.1       jtc 		return strcmp(opnd1, opnd2) == 0;
    351      1.1       jtc 	  case TO_STNEQ: /* != */
    352      1.1       jtc 		if (te->flags & TEF_DBRACKET)
    353      1.1       jtc 			return !gmatch(opnd1, opnd2, FALSE);
    354      1.1       jtc 		return strcmp(opnd1, opnd2) != 0;
    355      1.1       jtc 	  case TO_STLT: /* < */
    356      1.1       jtc 		return strcmp(opnd1, opnd2) < 0;
    357      1.1       jtc 	  case TO_STGT: /* > */
    358      1.1       jtc 		return strcmp(opnd1, opnd2) > 0;
    359      1.1       jtc 	  case TO_INTEQ: /* -eq */
    360      1.1       jtc 	  case TO_INTNE: /* -ne */
    361      1.1       jtc 	  case TO_INTGE: /* -ge */
    362      1.1       jtc 	  case TO_INTGT: /* -gt */
    363      1.1       jtc 	  case TO_INTLE: /* -le */
    364      1.1       jtc 	  case TO_INTLT: /* -lt */
    365      1.1       jtc 		{
    366      1.1       jtc 			long v1, v2;
    367      1.1       jtc 
    368  1.2.6.1  wrstuden 			if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR)
    369  1.2.6.1  wrstuden 			    || !evaluate(opnd2, &v2, KSH_RETURN_ERROR))
    370      1.1       jtc 			{
    371      1.1       jtc 				/* error already printed.. */
    372      1.1       jtc 				te->flags |= TEF_ERROR;
    373      1.1       jtc 				return 1;
    374      1.1       jtc 			}
    375      1.1       jtc 			switch ((int) op) {
    376      1.1       jtc 			  case TO_INTEQ:
    377      1.1       jtc 				return v1 == v2;
    378      1.1       jtc 			  case TO_INTNE:
    379      1.1       jtc 				return v1 != v2;
    380      1.1       jtc 			  case TO_INTGE:
    381      1.1       jtc 				return v1 >= v2;
    382      1.1       jtc 			  case TO_INTGT:
    383      1.1       jtc 				return v1 > v2;
    384      1.1       jtc 			  case TO_INTLE:
    385      1.1       jtc 				return v1 <= v2;
    386      1.1       jtc 			  case TO_INTLT:
    387      1.1       jtc 				return v1 < v2;
    388      1.1       jtc 			}
    389      1.1       jtc 		}
    390      1.1       jtc 	  case TO_FILNT: /* -nt */
    391  1.2.6.1  wrstuden 		{
    392  1.2.6.1  wrstuden 			int s2;
    393  1.2.6.1  wrstuden 			/* ksh88/ksh93 succeed if file2 can't be stated
    394  1.2.6.1  wrstuden 			 * (subtly different from `does not exist').
    395  1.2.6.1  wrstuden 			 */
    396  1.2.6.1  wrstuden 			return stat(opnd1, &b1) == 0
    397  1.2.6.1  wrstuden 				&& (((s2 = stat(opnd2, &b2)) == 0
    398  1.2.6.1  wrstuden 				      && b1.st_mtime > b2.st_mtime) || s2 < 0);
    399  1.2.6.1  wrstuden 		}
    400      1.1       jtc 	  case TO_FILOT: /* -ot */
    401  1.2.6.1  wrstuden 		{
    402  1.2.6.1  wrstuden 			int s1;
    403  1.2.6.1  wrstuden 			/* ksh88/ksh93 succeed if file1 can't be stated
    404  1.2.6.1  wrstuden 			 * (subtly different from `does not exist').
    405  1.2.6.1  wrstuden 			 */
    406  1.2.6.1  wrstuden 			return stat(opnd2, &b2) == 0
    407  1.2.6.1  wrstuden 				&& (((s1 = stat(opnd1, &b1)) == 0
    408  1.2.6.1  wrstuden 				      && b1.st_mtime < b2.st_mtime) || s1 < 0);
    409  1.2.6.1  wrstuden 		}
    410      1.1       jtc 	  case TO_FILEQ: /* -ef */
    411      1.1       jtc 		return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0
    412      1.1       jtc 		       && b1.st_dev == b2.st_dev
    413      1.1       jtc 		       && b1.st_ino == b2.st_ino;
    414      1.1       jtc 	}
    415      1.1       jtc 	(*te->error)(te, 0, "internal error: unknown op");
    416      1.1       jtc 	return 1;
    417      1.1       jtc }
    418      1.1       jtc 
    419      1.1       jtc /* Nasty kludge to handle Korn's bizarre /dev/fd hack */
    420      1.1       jtc static int
    421      1.1       jtc test_stat(path, statb)
    422      1.1       jtc 	const char *path;
    423      1.1       jtc 	struct stat *statb;
    424      1.1       jtc {
    425      1.1       jtc #if !defined(HAVE_DEV_FD)
    426      1.1       jtc 	int fd;
    427      1.1       jtc 
    428      1.1       jtc 	if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd))
    429      1.1       jtc 		return fstat(fd, statb);
    430      1.1       jtc #endif /* !HAVE_DEV_FD */
    431      1.1       jtc 
    432      1.1       jtc 	return stat(path, statb);
    433      1.1       jtc }
    434      1.1       jtc 
    435  1.2.6.1  wrstuden /* Routine to handle Korn's /dev/fd hack, and to deal with X_OK on
    436  1.2.6.1  wrstuden  * non-directories when running as root.
    437  1.2.6.1  wrstuden  */
    438      1.1       jtc static int
    439      1.1       jtc test_eaccess(path, mode)
    440      1.1       jtc 	const char *path;
    441      1.1       jtc 	int mode;
    442      1.1       jtc {
    443  1.2.6.1  wrstuden 	int res;
    444  1.2.6.1  wrstuden 
    445      1.1       jtc #if !defined(HAVE_DEV_FD)
    446      1.1       jtc 	int fd;
    447      1.1       jtc 
    448  1.2.6.1  wrstuden 	/* Note: doesn't handle //dev/fd, etc.. (this is ok) */
    449      1.1       jtc 	if (strncmp(path, "/dev/fd/", 8) == 0 && getn(path + 8, &fd)) {
    450      1.1       jtc 		int flags;
    451      1.1       jtc 
    452      1.1       jtc 		if ((flags = fcntl(fd, F_GETFL, 0)) < 0
    453      1.1       jtc 		    || (mode & X_OK)
    454      1.1       jtc 		    || ((mode & W_OK) && (flags & O_ACCMODE) == O_RDONLY)
    455      1.1       jtc 		    || ((mode & R_OK) && (flags & O_ACCMODE) == O_WRONLY))
    456      1.1       jtc 			return -1;
    457      1.1       jtc 		return 0;
    458      1.1       jtc 	}
    459      1.1       jtc #endif /* !HAVE_DEV_FD */
    460      1.1       jtc 
    461  1.2.6.1  wrstuden 	/* On most (all?) unixes, access() says everything is executable for
    462  1.2.6.1  wrstuden 	 * root - avoid this on files by using stat().
    463  1.2.6.1  wrstuden 	 */
    464  1.2.6.1  wrstuden 	if ((mode & X_OK) && ksheuid == 0) {
    465  1.2.6.1  wrstuden 		struct stat statb;
    466  1.2.6.1  wrstuden 
    467  1.2.6.1  wrstuden 		if (stat(path, &statb) < 0)
    468  1.2.6.1  wrstuden 			res = -1;
    469  1.2.6.1  wrstuden 		else if (S_ISDIR(statb.st_mode))
    470  1.2.6.1  wrstuden 			res = 0;
    471  1.2.6.1  wrstuden 		else
    472  1.2.6.1  wrstuden 			res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
    473  1.2.6.1  wrstuden 				? 0 : -1;
    474  1.2.6.1  wrstuden 		/* Need to check other permissions?  If so, use access() as
    475  1.2.6.1  wrstuden 		 * this will deal with root on NFS.
    476  1.2.6.1  wrstuden 		 */
    477  1.2.6.1  wrstuden 		if (res == 0 && (mode & (R_OK|W_OK)))
    478  1.2.6.1  wrstuden 			res = eaccess(path, mode);
    479  1.2.6.1  wrstuden 	} else
    480  1.2.6.1  wrstuden 		res = eaccess(path, mode);
    481  1.2.6.1  wrstuden 
    482  1.2.6.1  wrstuden 	return res;
    483      1.1       jtc }
    484      1.1       jtc 
    485      1.1       jtc int
    486      1.1       jtc test_parse(te)
    487      1.1       jtc 	Test_env *te;
    488      1.1       jtc {
    489      1.1       jtc 	int res;
    490      1.1       jtc 
    491      1.1       jtc 	res = test_oexpr(te, 1);
    492      1.1       jtc 
    493      1.1       jtc 	if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
    494      1.1       jtc 		(*te->error)(te, 0, "unexpected operator/operand");
    495      1.1       jtc 
    496      1.1       jtc 	return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
    497      1.1       jtc }
    498      1.1       jtc 
    499      1.1       jtc static int
    500      1.1       jtc test_oexpr(te, do_eval)
    501      1.1       jtc 	Test_env *te;
    502      1.1       jtc 	int do_eval;
    503      1.1       jtc {
    504      1.1       jtc 	int res;
    505      1.1       jtc 
    506      1.1       jtc 	res = test_aexpr(te, do_eval);
    507      1.1       jtc 	if (res)
    508      1.1       jtc 		do_eval = 0;
    509      1.1       jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
    510      1.1       jtc 		return test_oexpr(te, do_eval) || res;
    511      1.1       jtc 	return res;
    512      1.1       jtc }
    513      1.1       jtc 
    514      1.1       jtc static int
    515      1.1       jtc test_aexpr(te, do_eval)
    516      1.1       jtc 	Test_env *te;
    517      1.1       jtc 	int do_eval;
    518      1.1       jtc {
    519      1.1       jtc 	int res;
    520      1.1       jtc 
    521      1.1       jtc 	res = test_nexpr(te, do_eval);
    522      1.1       jtc 	if (!res)
    523      1.1       jtc 		do_eval = 0;
    524      1.1       jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
    525      1.1       jtc 		return test_aexpr(te, do_eval) && res;
    526      1.1       jtc 	return res;
    527      1.1       jtc }
    528      1.1       jtc 
    529      1.1       jtc static int
    530      1.1       jtc test_nexpr(te, do_eval)
    531      1.1       jtc 	Test_env *te;
    532      1.1       jtc 	int do_eval;
    533      1.1       jtc {
    534      1.1       jtc 	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
    535      1.1       jtc 		return !test_nexpr(te, do_eval);
    536      1.1       jtc 	return test_primary(te, do_eval);
    537      1.1       jtc }
    538      1.1       jtc 
    539      1.1       jtc static int
    540      1.1       jtc test_primary(te, do_eval)
    541      1.1       jtc 	Test_env *te;
    542      1.1       jtc 	int do_eval;
    543      1.1       jtc {
    544      1.1       jtc 	const char *opnd1, *opnd2;
    545      1.1       jtc 	int res;
    546      1.1       jtc 	Test_op op;
    547      1.1       jtc 
    548      1.1       jtc 	if (te->flags & TEF_ERROR)
    549      1.1       jtc 		return 0;
    550      1.1       jtc 	if ((*te->isa)(te, TM_OPAREN)) {
    551      1.1       jtc 		res = test_oexpr(te, do_eval);
    552      1.1       jtc 		if (te->flags & TEF_ERROR)
    553      1.1       jtc 			return 0;
    554      1.1       jtc 		if (!(*te->isa)(te, TM_CPAREN)) {
    555      1.1       jtc 			(*te->error)(te, 0, "missing closing paren");
    556      1.1       jtc 			return 0;
    557      1.1       jtc 		}
    558      1.1       jtc 		return res;
    559      1.1       jtc 	}
    560      1.1       jtc 	if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
    561      1.1       jtc 		/* unary expression */
    562      1.1       jtc 		opnd1 = (*te->getopnd)(te, op, do_eval);
    563      1.1       jtc 		if (!opnd1) {
    564      1.1       jtc 			(*te->error)(te, -1, "missing argument");
    565      1.1       jtc 			return 0;
    566      1.1       jtc 		}
    567      1.1       jtc 
    568      1.1       jtc 		return (*te->eval)(te, op, opnd1, (const char *) 0, do_eval);
    569      1.1       jtc 	}
    570      1.1       jtc 	opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
    571      1.1       jtc 	if (!opnd1) {
    572      1.1       jtc 		(*te->error)(te, 0, "expression expected");
    573      1.1       jtc 		return 0;
    574      1.1       jtc 	}
    575      1.1       jtc 	if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
    576      1.1       jtc 		/* binary expression */
    577      1.1       jtc 		opnd2 = (*te->getopnd)(te, op, do_eval);
    578      1.1       jtc 		if (!opnd2) {
    579      1.1       jtc 			(*te->error)(te, -1, "missing second argument");
    580      1.1       jtc 			return 0;
    581      1.1       jtc 		}
    582      1.1       jtc 
    583      1.1       jtc 		return (*te->eval)(te, op, opnd1, opnd2, do_eval);
    584      1.1       jtc 	}
    585      1.1       jtc 	if (te->flags & TEF_DBRACKET) {
    586      1.1       jtc 		(*te->error)(te, -1, "missing expression operator");
    587      1.1       jtc 		return 0;
    588      1.1       jtc 	}
    589      1.1       jtc 	return (*te->eval)(te, TO_STNZE, opnd1, (const char *) 0, do_eval);
    590      1.1       jtc }
    591      1.1       jtc 
    592      1.1       jtc /*
    593      1.1       jtc  * Plain test (test and [ .. ]) specific routines.
    594      1.1       jtc  */
    595      1.1       jtc 
    596      1.1       jtc /* Test if the current token is a whatever.  Accepts the current token if
    597      1.1       jtc  * it is.  Returns 0 if it is not, non-zero if it is (in the case of
    598      1.1       jtc  * TM_UNOP and TM_BINOP, the returned value is a Test_op).
    599      1.1       jtc  */
    600      1.1       jtc static int
    601      1.1       jtc ptest_isa(te, meta)
    602      1.1       jtc 	Test_env *te;
    603      1.1       jtc 	Test_meta meta;
    604      1.1       jtc {
    605      1.1       jtc 	/* Order important - indexed by Test_meta values */
    606      1.1       jtc 	static const char *const tokens[] = {
    607      1.1       jtc 				"-o", "-a", "!", "(", ")"
    608      1.1       jtc 			};
    609      1.1       jtc 	int ret;
    610      1.1       jtc 
    611      1.1       jtc 	if (te->pos.wp >= te->wp_end)
    612      1.1       jtc 		return meta == TM_END;
    613      1.1       jtc 
    614      1.1       jtc 	if (meta == TM_UNOP || meta == TM_BINOP)
    615      1.1       jtc 		ret = (int) test_isop(te, meta, *te->pos.wp);
    616      1.1       jtc 	else if (meta == TM_END)
    617      1.1       jtc 		ret = 0;
    618      1.1       jtc 	else
    619      1.1       jtc 		ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
    620      1.1       jtc 
    621      1.1       jtc 	/* Accept the token? */
    622      1.1       jtc 	if (ret)
    623      1.1       jtc 		te->pos.wp++;
    624      1.1       jtc 
    625      1.1       jtc 	return ret;
    626      1.1       jtc }
    627      1.1       jtc 
    628      1.1       jtc static const char *
    629      1.1       jtc ptest_getopnd(te, op, do_eval)
    630      1.1       jtc 	Test_env *te;
    631      1.1       jtc 	Test_op op;
    632      1.1       jtc 	int do_eval;
    633      1.1       jtc {
    634      1.1       jtc 	if (te->pos.wp >= te->wp_end)
    635      1.1       jtc 		return op == TO_FILTT ? "1" : (const char *) 0;
    636      1.1       jtc 	return *te->pos.wp++;
    637      1.1       jtc }
    638      1.1       jtc 
    639      1.1       jtc static int
    640      1.1       jtc ptest_eval(te, op, opnd1, opnd2, do_eval)
    641      1.1       jtc 	Test_env *te;
    642      1.1       jtc 	Test_op op;
    643      1.1       jtc 	const char *opnd1;
    644      1.1       jtc 	const char *opnd2;
    645      1.1       jtc 	int do_eval;
    646      1.1       jtc {
    647      1.1       jtc 	return test_eval(te, op, opnd1, opnd2, do_eval);
    648      1.1       jtc }
    649      1.1       jtc 
    650      1.1       jtc static void
    651      1.1       jtc ptest_error(te, offset, msg)
    652      1.1       jtc 	Test_env *te;
    653      1.1       jtc 	int offset;
    654      1.1       jtc 	const char *msg;
    655      1.1       jtc {
    656      1.1       jtc 	const char *op = te->pos.wp + offset >= te->wp_end ?
    657      1.1       jtc 				(const char *) 0 : te->pos.wp[offset];
    658      1.1       jtc 
    659      1.1       jtc 	te->flags |= TEF_ERROR;
    660      1.1       jtc 	if (op)
    661      1.1       jtc 		bi_errorf("%s: %s", op, msg);
    662      1.1       jtc 	else
    663      1.1       jtc 		bi_errorf("%s", msg);
    664      1.1       jtc }
    665