jfb2 writes in Bug 119:
[oweals/busybox.git] / coreutils / test.c
index 419da5101898fefc3c2f28b3f7e1004bee859495..3da2daecd9a7d6d91b6e44d04c1a67738f6ef665 100644 (file)
@@ -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,19 +156,25 @@ 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);
@@ -185,6 +192,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:
@@ -232,9 +244,9 @@ static void syntax(const char *op, const char *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 +256,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,16 +267,16 @@ 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");
@@ -298,7 +310,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;
@@ -441,13 +453,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 +476,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 +537,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));