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