* GNU test program (ksb and mjb)
* Modified to run with the GNU shell Apr 25, 1988 by bfox.
*??? -G file is owned by same gid; the effective gid is checked
* Chet Ramey, CWRU 3/23/89
* Fixed a BSD dependency (_doprnt()) in the port to AIX 2.2
* Chet Ramey, CWRU 5/3/89
/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any later
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
static char *rcsid
= "$Id: gtest.c,v 1.10 88/07/02 13:34:45 afb Exp Locker: afb $";
/* The following few defines control the truth and false output of each stage.
TRUE and FALSE are what we use to compute the final output value.
SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
TRUTH_OR is how to do logical or with TRUE and FALSE.
TRUTH_AND is how to do logical and with TRUE and FALSE..
Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
SHELL_BOOLEAN = (!value). */
#define SHELL_BOOLEAN(value) (!(value))
#define TRUTH_OR(a, b) ((a) | (b))
#define TRUTH_AND(a, b) ((a) & (b))
/* Define STANDALONE to get the /bin/test version. Otherwise, we are
making this for the shell. */
#define test_exit(val) exit (val)
int test_error_return
= 0;
#define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
static int pos
; /* position in list */
static int argc
; /* number of args from command line */
static char **argv
; /* the argument list */
test_syntax_error (format
, arg
)
(void) fprintf (stderr
, "%s: ", argv
[0]);
(void) fprintf (stderr
, format
, arg
);
test_exit (SHELL_BOOLEAN (FALSE
));
fprintf (stderr
, "%s: ", argv
[0]);
test_exit (SHELL_BOOLEAN (FALSE
));
* advance - increment our position in the argument list. Check that
* we're not past the end of the argument list. This check is
* supressed if the argument is FALSE. made a macro for efficiency.
#define advance(f) (++pos, f && (pos < argc ? 0 : beyond()))
#define unary_advance() (advance (1),++pos)
* beyond - call when we're beyond the end of the argument list (an
test_syntax_error ("argument expected\n", (char *)NULL
);
* int_expt_err - when an integer argument was expected, but something else
test_syntax_error ("integer expression expected %s\n", pch
);
* isint - is the argument whose position in the argument vector is 'm' an
* integer? Convert it for me too, returning it's value in 'pl'.
/* Skip leading whitespace characters. */
while (*pch
== '\t' || *pch
== ' ')
/* accept negative numbers but not '-' alone */
* age_of - find the age of the given file. Return YES or NO depending
* on whether the file exists, and if it does, fill in the age with
if (stat (argv
[posit
], &stat_buf
) < 0)
*age
= stat_buf
.st_mtime
;
* term - parse a term and return 1 or 0 depending on whether the term
* evaluates to true or false, respectively.
* '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
* string ('!='|'=') string
* <int> '-'(eq|ne|le|lt|ge|gt) <int>
* file '-'(nt|ot|ef) file
* positive and negative integers
auto struct stat stat_buf
, stat_spare
;
auto int l_is_l
, r_is_l
; /* are the left and right integer
* expressions of the form '-l string'?
/* Deal with leading "not". */
if (pos
< argc
&& '!' == argv
[pos
][0] && '\000' == argv
[pos
][1])
/* This has to be rewritten to handle the TRUTH and FALSE stuff. */
if ('(' == argv
[pos
][0] && '\000' == argv
[pos
][1])
if (')' != argv
[pos
][0] || '\000' != argv
[pos
][1])
test_syntax_error ("argument expected, found %s\n", argv
[pos
]);
return (TRUE
== (value
));
/* are there enough arguments left that this could be dyadic? */
if ('-' == argv
[pos
][0] && 'l' == argv
[pos
][1] &&
if ('-' == argv
[op
+ 1][0] && 'l' == argv
[op
+ 1][1]
&& '\000' == argv
[op
+ 1][2])
/* check for eq, nt, and stuff */
if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -lt");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -lt");
return (TRUE
== (l
< r
));
if ('e' == argv
[op
][2] && '\000' == argv
[op
][3])
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -le");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -le");
return (TRUE
== (l
<= r
));
if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
/* gt integer greater than */
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -gt");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -gt");
return (TRUE
== (l
> r
));
if ('e' == argv
[op
][2] && '\000' == argv
[op
][3])
/* ge - integer greater than or equal to */
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -ge");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -ge");
return (TRUE
== (l
>= r
));
if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
test_syntax_error ("-nt does not accept -l\n",
if (age_of (op
- 1, &l
) && age_of (op
+ 1, &r
))
return (TRUE
== (l
> r
));
if ('e' == argv
[op
][2] && '\000' == argv
[op
][3])
/* ne - integer not equal */
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -ne");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -ne");
return (TRUE
== (l
!= r
));
if ('q' == argv
[op
][2] && '\000' == argv
[op
][3])
l
= strlen (argv
[op
- 1]);
int_expt_err ("before -eq");
r
= strlen (argv
[op
+ 2]);
int_expt_err ("after -eq");
return (TRUE
== (l
== r
));
if ('f' == argv
[op
][2] && '\000' == argv
[op
][3])
test_syntax_error ("-ef does not accept -l\n",
if (stat (argv
[op
- 1], &stat_buf
) < 0)
if (stat (argv
[op
+ 1], &stat_spare
) < 0)
(stat_buf
.st_dev
== stat_spare
.st_dev
&&
stat_buf
.st_ino
== stat_spare
.st_ino
));
if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
test_syntax_error ("-nt does not accept -l\n",
if (age_of (op
- 1, &l
) && age_of (op
+ 1, &r
))
return (TRUE
== (l
< r
));
if ('=' == argv
[op
][0] && '\000' == argv
[op
][1])
value
= (0 == strcmp (argv
[pos
], argv
[pos
+ 2]));
if ('!' == argv
[op
][0] && '=' == argv
[op
][1] && '\000' == argv
[op
][2])
value
= 0 != strcmp (argv
[pos
], argv
[pos
+ 2]);
/* Might be a switch type argument */
if ('-' == argv
[pos
][0] && '\000' == argv
[pos
][2] /* && pos < argc-1 */ )
/* All of the following unary operators use unary_advance (), which
checks to make sure that there is an argument, and then advances
pos right past it. This means that pos - 1 is the location of the
case 'r': /* file is readable? */
value
= -1 != access (argv
[pos
- 1], R_OK
);
case 'w': /* File is writeable? */
value
= -1 != access (argv
[pos
- 1], W_OK
);
case 'x': /* File is executable? */
value
= -1 != access (argv
[pos
- 1], X_OK
);
case 'O': /* File is owned by you? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (geteuid () == stat_buf
.st_uid
));
case 'f': /* File is a file? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
* Under SYSV, -f is true if the given file exists
* and is a regular file. Other places, this checks
* to see if the given file is not a directory.
return (TRUE
== ((S_IFREG
== (stat_buf
.st_mode
& S_IFMT
)) ||
(0 == (stat_buf
.st_mode
& S_IFMT
))));
return (TRUE
== (S_IFDIR
!= (stat_buf
.st_mode
& S_IFMT
)));
case 'd': /* File is a directory? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFDIR
== (stat_buf
.st_mode
& S_IFMT
)));
case 's': /* File has something in it? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (stat_buf
.st_size
> (off_t
) 0));
case 'S': /* File is a socket? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFSOCK
== (stat_buf
.st_mode
& S_IFMT
)));
case 'c': /* File is character special? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFCHR
== (stat_buf
.st_mode
& S_IFMT
)));
case 'b': /* File is block special? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFBLK
== (stat_buf
.st_mode
& S_IFMT
)));
case 'p': /* File is a named pipe? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFIFO
== (stat_buf
.st_mode
& S_IFMT
)));
case 'L': /* Same as -h */
case 'h': /* File is a symbolic link? */
if (lstat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (S_IFLNK
== (stat_buf
.st_mode
& S_IFMT
)));
case 'u': /* File is setuid? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (0 != (stat_buf
.st_mode
& S_ISUID
)));
case 'g': /* File is setgid? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (0 != (stat_buf
.st_mode
& S_ISGID
)));
case 'k': /* File has sticky bit set? */
if (stat (argv
[pos
- 1], &stat_buf
) < 0)
return (TRUE
== (0 != (stat_buf
.st_mode
& S_ISVTX
)));
case 't': /* File (fd) is a terminal? (fd) defaults to stdout. */
if (pos
< argc
&& isint (pos
, &r
))
return (TRUE
== (isatty ((int) r
)));
return (TRUE
== (isatty (1)));
case 'n': /* True if arg has some length. */
return (TRUE
== (0 != strlen (argv
[pos
- 1])));
case 'z': /* True if arg has no length. */
return (TRUE
== (0 == strlen (argv
[pos
- 1])));
value
= 0 != strlen (argv
[pos
]);
while (pos
< argc
&& '-' == argv
[pos
][0] && 'a' == argv
[pos
][1] && '\000' == argv
[pos
][2])
value
= TRUTH_AND (value
, term ());
while (pos
< argc
&& '-' == argv
[pos
][0] && 'o' == argv
[pos
][1] && '\000' == argv
[pos
][2])
value
= TRUTH_OR (value
, and ());
return value
^ or (); /* Same with this. */
test_command (margc
, margv
)
int code
= setjmp (test_exit_buf
);
return (test_error_return
);
if (margv
[0] && strcmp (margv
[0], "[") == 0)
test_exit (SHELL_BOOLEAN (FALSE
));
if (margv
[margc
] && strcmp (margv
[margc
], "]") != 0)
test_syntax_error ("missing `]'\n", (char *)NULL
);
test_exit (SHELL_BOOLEAN (FALSE
));
test_syntax_error ("too many arguments\n", (char *)NULL
);
test_exit (SHELL_BOOLEAN (value
));