- spelling
[oweals/busybox.git] / coreutils / test.c
index 31ac87f346a38a50cf64e141f75bfabeca5cc94a..2b624e308f3ac4d18e74e0c6e673e159536bb633 100644 (file)
@@ -2,16 +2,16 @@
 /*
  * test implementation for busybox
  *
- * Copyright (c) by a whole pile of folks: 
+ * Copyright (c) by a whole pile of folks:
  *
- *     test(1); version 7-like  --  author Erik Baalbergen
- *     modified by Eric Gisin to be used as built-in.
- *     modified by Arnold Robbins to add SVR3 compatibility
- *     (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
- *     modified by J.T. Conklin for NetBSD.
- *     modified by Herbert Xu to be used as built-in in ash.
- *     modified by Erik Andersen <andersee@debian.org> to be used 
- *     in busybox.
+ *     test(1); version 7-like  --  author Erik Baalbergen
+ *     modified by Eric Gisin to be used as built-in.
+ *     modified by Arnold Robbins to add SVR3 compatibility
+ *     (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
+ *     modified by J.T. Conklin for NetBSD.
+ *     modified by Herbert Xu to be used as built-in in ash.
+ *     modified by Erik Andersen <andersen@codepoet.org> to be used
+ *     in busybox.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  * Original copyright notice states:
- *     "This program is in the Public Domain."
+ *     "This program is in the Public Domain."
  */
 
 #include <sys/types.h>
@@ -51,7 +51,7 @@
        unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
                "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
 
-       binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
+       binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
                        "-nt"|"-ot"|"-ef";
        operand ::= <any legal UNIX file name>
 */
@@ -135,6 +135,7 @@ static const struct t_op {
        "-L", FILSYM, UNOP}, {
        "-S", FILSOCK, UNOP}, {
        "=", STREQ, BINOP}, {
+       "==", STREQ, BINOP}, {
        "!=", STRNE, BINOP}, {
        "<", STRLT, BINOP}, {
        ">", STRGT, BINOP}, {
@@ -155,28 +156,33 @@ static const struct t_op {
        0, 0, 0}
 };
 
+#ifdef CONFIG_FEATURE_TEST_64
+typedef int64_t arith_t;
+#else
+typedef int arith_t;
+#endif
+
 static char **t_wp;
 static struct t_op const *t_wp_op;
 static gid_t *group_array = NULL;
 static int ngroups;
 
 static enum token t_lex(char *s);
-static int oexpr(enum token n);
-static int aexpr(enum token n);
-static int nexpr(enum token n);
+static arith_t oexpr(enum token n);
+static arith_t aexpr(enum token n);
+static arith_t nexpr(enum token n);
 static int binop(void);
-static int primary(enum token n);
+static arith_t primary(enum token n);
 static int filstat(char *nm, enum token mode);
-static int getn(const char *s);
+static arith_t getn(const char *s);
 static int newerf(const char *f1, const char *f2);
 static int olderf(const char *f1, const char *f2);
 static int equalf(const char *f1, const char *f2);
-static void syntax(const char *op, const char *msg);
 static int test_eaccess(char *path, int mode);
 static int is_a_group_member(gid_t gid);
 static void initialize_group_array(void);
 
-extern int test_main(int argc, char **argv)
+int test_main(int argc, char **argv)
 {
        int res;
 
@@ -185,6 +191,11 @@ extern int test_main(int argc, char **argv)
                        bb_error_msg_and_die("missing ]");
                argv[argc] = NULL;
        }
+       if (strcmp(bb_applet_name, "[[") == 0) {
+               if (strcmp(argv[--argc], "]]"))
+                       bb_error_msg_and_die("missing ]]");
+               argv[argc] = NULL;
+       }
        /* Implement special cases from POSIX.2, section 4.62.4 */
        switch (argc) {
        case 1:
@@ -218,23 +229,14 @@ extern int test_main(int argc, char **argv)
        res = !oexpr(t_lex(*t_wp));
 
        if (*t_wp != NULL && *++t_wp != NULL)
-               syntax(*t_wp, "unknown operand");
+               bb_error_msg_and_die("%s: unknown operand", *t_wp);
 
        return (res);
 }
 
-static void syntax(const char *op, const char *msg)
-{
-       if (op && *op) {
-               bb_error_msg_and_die("%s: %s", op, msg);
-       } else {
-               bb_error_msg_and_die("%s", msg);
-       }
-}
-
-static int oexpr(enum token n)
+static arith_t oexpr(enum token n)
 {
-       int res;
+       arith_t res;
 
        res = aexpr(n);
        if (t_lex(*++t_wp) == BOR) {
@@ -244,9 +246,9 @@ static int oexpr(enum token n)
        return res;
 }
 
-static int aexpr(enum token n)
+static arith_t aexpr(enum token n)
 {
-       int res;
+       arith_t res;
 
        res = nexpr(n);
        if (t_lex(*++t_wp) == BAND)
@@ -255,30 +257,30 @@ static int aexpr(enum token n)
        return res;
 }
 
-static int nexpr(enum token n)
+static arith_t nexpr(enum token n)
 {
        if (n == UNOT)
                return !nexpr(t_lex(*++t_wp));
        return primary(n);
 }
 
-static int primary(enum token n)
+static arith_t primary(enum token n)
 {
-       int res;
+       arith_t res;
 
        if (n == EOI) {
-               syntax(NULL, "argument expected");
+               bb_error_msg_and_die("argument expected");
        }
        if (n == LPAREN) {
                res = oexpr(t_lex(*++t_wp));
                if (t_lex(*++t_wp) != RPAREN)
-                       syntax(NULL, "closing paren expected");
+                       bb_error_msg_and_die("closing paren expected");
                return res;
        }
        if (t_wp_op && t_wp_op->op_type == UNOP) {
                /* unary expression */
                if (*++t_wp == NULL)
-                       syntax(t_wp_op->op_text, "argument expected");
+                       bb_error_msg_and_die(bb_msg_requires_arg, t_wp_op->op_text);
                switch (n) {
                case STREZ:
                        return strlen(*t_wp) == 0;
@@ -298,7 +300,7 @@ static int primary(enum token n)
        return strlen(*t_wp) > 0;
 }
 
-static int binop()
+static int binop(void)
 {
        const char *opnd1, *opnd2;
        struct t_op const *op;
@@ -308,7 +310,7 @@ static int binop()
        op = t_wp_op;
 
        if ((opnd2 = *++t_wp) == (char *) 0)
-               syntax(op->op_text, "argument expected");
+               bb_error_msg_and_die(bb_msg_requires_arg, op->op_text);
 
        switch (op->op_num) {
        case STREQ:
@@ -441,13 +443,21 @@ static enum token t_lex(char *s)
 }
 
 /* atoi with error detection */
-static int getn(const char *s)
+static arith_t getn(const char *s)
 {
        char *p;
+#ifdef CONFIG_FEATURE_TEST_64
+       long long r;
+#else
        long r;
+#endif
 
        errno = 0;
+#ifdef CONFIG_FEATURE_TEST_64
+       r = strtoll(s, &p, 10);
+#else
        r = strtol(s, &p, 10);
+#endif
 
        if (errno != 0)
                bb_error_msg_and_die("%s: out of range", s);
@@ -456,7 +466,7 @@ static int getn(const char *s)
        if (*(bb_skip_whitespace(p)))
                bb_error_msg_and_die("%s: bad number", s);
 
-       return (int) r;
+       return r;
 }
 
 static int newerf(const char *f1, const char *f2)
@@ -517,7 +527,7 @@ static int test_eaccess(char *path, int mode)
        return (-1);
 }
 
-static void initialize_group_array()
+static void initialize_group_array(void)
 {
        ngroups = getgroups(0, NULL);
        group_array = xrealloc(group_array, ngroups * sizeof(gid_t));