RPAREN,
OPERAND
};
-#define __is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
-#define __is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
-#define __is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
-#define __is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
-#define __is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
-#define __is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
+#define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
+#define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
+#define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
+#define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
+#define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
+#define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
enum token_types {
UNOP,
BINOP,
};
static const struct t_op {
- const char * const op_text;
+ char op_text[4];
unsigned char op_num, op_type;
} ops[] = {
- {
- "-r", FILRD, UNOP}, {
- "-w", FILWR, UNOP}, {
- "-x", FILEX, UNOP}, {
- "-e", FILEXIST, UNOP}, {
- "-f", FILREG, UNOP}, {
- "-d", FILDIR, UNOP}, {
- "-c", FILCDEV, UNOP}, {
- "-b", FILBDEV, UNOP}, {
- "-p", FILFIFO, UNOP}, {
- "-u", FILSUID, UNOP}, {
- "-g", FILSGID, UNOP}, {
- "-k", FILSTCK, UNOP}, {
- "-s", FILGZ, UNOP}, {
- "-t", FILTT, UNOP}, {
- "-z", STREZ, UNOP}, {
- "-n", STRNZ, UNOP}, {
- "-h", FILSYM, UNOP}, /* for backwards compat */
- {
- "-O", FILUID, UNOP}, {
- "-G", FILGID, UNOP}, {
- "-L", FILSYM, UNOP}, {
- "-S", FILSOCK, UNOP}, {
- "=", STREQ, BINOP}, {
- "==", STREQ, BINOP}, {
- "!=", STRNE, BINOP}, {
- "<", STRLT, BINOP}, {
- ">", STRGT, BINOP}, {
- "-eq", INTEQ, BINOP}, {
- "-ne", INTNE, BINOP}, {
- "-ge", INTGE, BINOP}, {
- "-gt", INTGT, BINOP}, {
- "-le", INTLE, BINOP}, {
- "-lt", INTLT, BINOP}, {
- "-nt", FILNT, BINOP}, {
- "-ot", FILOT, BINOP}, {
- "-ef", FILEQ, BINOP}, {
- "!", UNOT, BUNOP}, {
- "-a", BAND, BBINOP}, {
- "-o", BOR, BBINOP}, {
- "(", LPAREN, PAREN}, {
- ")", RPAREN, PAREN}, {
- 0, 0, 0}
+ { "-r", FILRD , UNOP },
+ { "-w", FILWR , UNOP },
+ { "-x", FILEX , UNOP },
+ { "-e", FILEXIST, UNOP },
+ { "-f", FILREG , UNOP },
+ { "-d", FILDIR , UNOP },
+ { "-c", FILCDEV , UNOP },
+ { "-b", FILBDEV , UNOP },
+ { "-p", FILFIFO , UNOP },
+ { "-u", FILSUID , UNOP },
+ { "-g", FILSGID , UNOP },
+ { "-k", FILSTCK , UNOP },
+ { "-s", FILGZ , UNOP },
+ { "-t", FILTT , UNOP },
+ { "-z", STREZ , UNOP },
+ { "-n", STRNZ , UNOP },
+ { "-h", FILSYM , UNOP }, /* for backwards compat */
+
+ { "-O" , FILUID , UNOP },
+ { "-G" , FILGID , UNOP },
+ { "-L" , FILSYM , UNOP },
+ { "-S" , FILSOCK, UNOP },
+ { "=" , STREQ , BINOP },
+ { "==" , STREQ , BINOP },
+ { "!=" , STRNE , BINOP },
+ { "<" , STRLT , BINOP },
+ { ">" , STRGT , BINOP },
+ { "-eq", INTEQ , BINOP },
+ { "-ne", INTNE , BINOP },
+ { "-ge", INTGE , BINOP },
+ { "-gt", INTGT , BINOP },
+ { "-le", INTLE , BINOP },
+ { "-lt", INTLT , BINOP },
+ { "-nt", FILNT , BINOP },
+ { "-ot", FILOT , BINOP },
+ { "-ef", FILEQ , BINOP },
+ { "!" , UNOT , BUNOP },
+ { "-a" , BAND , BBINOP },
+ { "-o" , BOR , BBINOP },
+ { "(" , LPAREN , PAREN },
+ { ")" , RPAREN , PAREN },
};
-#ifdef CONFIG_FEATURE_TEST_64
+enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
+
+#if ENABLE_FEATURE_TEST_64
typedef int64_t arith_t;
#else
typedef int arith_t;
#endif
+/* Cannot eliminate these static data (do the G trick)
+ * because of bb_test usage from other applets */
static char **t_wp;
static struct t_op const *t_wp_op;
static gid_t *group_array;
static int ngroups;
+static jmp_buf leaving;
static enum token t_lex(char *s);
static arith_t oexpr(enum token n);
static int is_a_group_member(gid_t gid);
static void initialize_group_array(void);
-static jmp_buf leaving;
-
int bb_test(int argc, char **argv)
{
int res;
* isn't likely in the case of a shell. paranoia
* prevails...
*/
- ngroups = 0;
+ ngroups = 0;
/* Implement special cases from POSIX.2, section 4.62.4 */
if (argc == 1)
if (argc == 3)
return *argv[2] != '\0';
_off = argc - 4;
- if (t_lex(argv[2+_off]), t_wp_op && t_wp_op->op_type == BINOP) {
- t_wp = &argv[1+_off];
+ t_lex(argv[2 + _off]);
+ if (t_wp_op && t_wp_op->op_type == BINOP) {
+ t_wp = &argv[1 + _off];
return binop() == 0;
}
}
return res;
}
+static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
static void syntax(const char *op, const char *msg)
{
if (op && *op) {
if (*++t_wp == NULL)
syntax(t_wp_op->op_text, "argument expected");
if (n == STREZ)
- return strlen(*t_wp) == 0;
- else if (n == STRNZ)
- return strlen(*t_wp) != 0;
- else if (n == FILTT)
+ return t_wp[0][0] == '\0';
+ if (n == STRNZ)
+ return t_wp[0][0] != '\0';
+ if (n == FILTT)
return isatty(getn(*t_wp));
- else
- return filstat(*t_wp, n);
+ return filstat(*t_wp, n);
}
- if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
+ t_lex(t_wp[1]);
+ if (t_wp_op && t_wp_op->op_type == BINOP) {
return binop();
}
- return strlen(*t_wp) > 0;
+ return t_wp[0][0] != '\0';
}
static int binop(void)
(void) t_lex(*++t_wp);
op = t_wp_op;
- if ((opnd2 = *++t_wp) == (char *) 0)
+ opnd2 = *++t_wp;
+ if (opnd2 == NULL)
syntax(op->op_text, "argument expected");
- if (__is_int_op(op->op_num)) {
+ if (is_int_op(op->op_num)) {
val1 = getn(opnd1);
val2 = getn(opnd2);
if (op->op_num == INTEQ)
if (op->op_num == INTLT)
return val1 < val2;
}
- if (__is_str_op(op->op_num)) {
+ if (is_str_op(op->op_num)) {
val1 = strcmp(opnd1, opnd2);
if (op->op_num == STREQ)
return val1 == 0;
/* We are sure that these three are by now the only binops we didn't check
* yet, so we do not check if the class is correct:
*/
-/* if (__is_file_op(op->op_num)) */
+/* if (is_file_op(op->op_num)) */
{
struct stat b1, b2;
return 0;
if (mode == FILEXIST)
return 1;
- else if (__is_file_access(mode)) {
+ if (is_file_access(mode)) {
if (mode == FILRD)
i = R_OK;
if (mode == FILWR)
i = X_OK;
return test_eaccess(nm, i) == 0;
}
- else if (__is_file_type(mode)) {
+ if (is_file_type(mode)) {
if (mode == FILREG)
i = S_IFREG;
if (mode == FILDIR)
return 0;
#endif
}
-filetype:
+ filetype:
return ((s.st_mode & S_IFMT) == i);
}
- else if (__is_file_bit(mode)) {
+ if (is_file_bit(mode)) {
if (mode == FILSUID)
i = S_ISUID;
if (mode == FILSGID)
i = S_ISVTX;
return ((s.st_mode & i) != 0);
}
- else if (mode == FILGZ)
+ if (mode == FILGZ)
return s.st_size > 0L;
- else if (mode == FILUID)
+ if (mode == FILUID)
return s.st_uid == geteuid();
- else if (mode == FILGID)
+ if (mode == FILGID)
return s.st_gid == getegid();
- else
- return 1; /* NOTREACHED */
-
+ return 1; /* NOTREACHED */
}
static enum token t_lex(char *s)
{
- struct t_op const *op = ops;
+ const struct t_op *op;
- if (s == 0) {
- t_wp_op = (struct t_op *) 0;
+ t_wp_op = NULL;
+ if (s == NULL) {
return EOI;
}
- while (op->op_text) {
+
+ op = ops;
+ do {
if (strcmp(s, op->op_text) == 0) {
t_wp_op = op;
return op->op_num;
}
op++;
- }
- t_wp_op = (struct t_op *) 0;
+ } while (op < ops + NUM_OPS);
+
return OPERAND;
}
static arith_t getn(const char *s)
{
char *p;
-#ifdef CONFIG_FEATURE_TEST_64
+#if ENABLE_FEATURE_TEST_64
long long r;
#else
long r;
#endif
errno = 0;
-#ifdef CONFIG_FEATURE_TEST_64
+#if ENABLE_FEATURE_TEST_64
r = strtoll(s, &p, 10);
#else
r = strtol(s, &p, 10);
{
return bb_test(argc, argv);
}
-
#include "libbb.h"
-static const struct signal_name {
- int number;
- char name[5];
-} signals[] = {
+static const char signals[32][7] = {
// SUSv3 says kill must support these, and specifies the numerical values,
// http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
// TODO: "[SIG]EXIT" shouldn't work for kill, right?
- {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, {6, "ABRT"}, {9, "KILL"},
- {14, "ALRM"}, {15, "TERM"},
+ // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
+ // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
// And Posix adds the following:
- {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
- {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
- {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
- {SIGTTOU, "TTOU"}
+ // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
+ // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
+ // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
+ // {SIGTTOU, "TTOU"}
+ [0] = "EXIT",
+#ifdef SIGHUP
+ [SIGHUP ] = "HUP",
+#endif
+#ifdef SIGINT
+ [SIGINT ] = "INT",
+#endif
+#ifdef SIGQUIT
+ [SIGQUIT ] = "QUIT",
+#endif
+#ifdef SIGILL
+ [SIGILL ] = "ILL",
+#endif
+#ifdef SIGTRAP
+ [SIGTRAP ] = "TRAP",
+#endif
+#ifdef SIGABRT
+ [SIGABRT ] = "ABRT",
+#endif
+#ifdef SIGBUS
+ [SIGBUS ] = "BUS",
+#endif
+#ifdef SIGFPE
+ [SIGFPE ] = "FPE",
+#endif
+#ifdef SIGKILL
+ [SIGKILL ] = "KILL",
+#endif
+#ifdef SIGUSR1
+ [SIGUSR1 ] = "USR1",
+#endif
+#ifdef SIGSEGV
+ [SIGSEGV ] = "SEGV",
+#endif
+#ifdef SIGUSR2
+ [SIGUSR2 ] = "USR2",
+#endif
+#ifdef SIGPIPE
+ [SIGPIPE ] = "PIPE",
+#endif
+#ifdef SIGALRM
+ [SIGALRM ] = "ALRM",
+#endif
+#ifdef SIGTERM
+ [SIGTERM ] = "TERM",
+#endif
+#ifdef SIGSTKFLT
+ [SIGSTKFLT] = "STKFLT",
+#endif
+#ifdef SIGCHLD
+ [SIGCHLD ] = "CHLD",
+#endif
+#ifdef SIGCONT
+ [SIGCONT ] = "CONT",
+#endif
+#ifdef SIGSTOP
+ [SIGSTOP ] = "STOP",
+#endif
+#ifdef SIGTSTP
+ [SIGTSTP ] = "TSTP",
+#endif
+#ifdef SIGTTIN
+ [SIGTTIN ] = "TTIN",
+#endif
+#ifdef SIGTTOU
+ [SIGTTOU ] = "TTOU",
+#endif
+#ifdef SIGURG
+ [SIGURG ] = "URG",
+#endif
+#ifdef SIGXCPU
+ [SIGXCPU ] = "XCPU",
+#endif
+#ifdef SIGXFSZ
+ [SIGXFSZ ] = "XFSZ",
+#endif
+#ifdef SIGVTALRM
+ [SIGVTALRM] = "VTALRM",
+#endif
+#ifdef SIGPROF
+ [SIGPROF ] = "PROF",
+#endif
+#ifdef SIGWINCH
+ [SIGWINCH ] = "WINCH",
+#endif
+#ifdef SIGPOLL
+ [SIGPOLL ] = "POLL",
+#endif
+#ifdef SIGPWR
+ [SIGPWR ] = "PWR",
+#endif
+#ifdef SIGSYS
+ [SIGSYS ] = "SYS",
+#endif
};
// Convert signal name to number.
int i;
i = bb_strtou(name, NULL, 10);
- if (!errno) return i;
- for (i = 0; i < sizeof(signals) / sizeof(struct signal_name); i++)
- if (strcasecmp(name, signals[i].name) == 0
- || (strncasecmp(name, "SIG", 3) == 0
- && strcasecmp(&name[3], signals[i].name) == 0))
- return signals[i].number;
+ if (!errno)
+ return i;
+ if (strncasecmp(name, "SIG", 3) == 0)
+ name += 3;
+ for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
+ if (strcasecmp(name, signals[i]) == 0)
+ return i;
+
+#if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
+ /* These are aliased to other names */
+ if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
+#ifdef SIGIO
+ if (!name[2])
+ return SIGIO;
+#endif
+#ifdef SIGIOT
+ if ((name[2] | 0x20) == 't' && !name[3])
+ return SIGIOT;
+#endif
+ }
+#endif
+
return -1;
}
const char *get_signame(int number)
{
- int i;
-
- for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) {
- if (number == signals[i].number) {
- return signals[i].name;
- }
+ if ((unsigned)number < sizeof(signals) / sizeof(signals[0])) {
+ if (signals[number][0]) /* if it's not an empty str */
+ return signals[number];
}
return itoa(number);