- Remove unnecessary warning from libbb and move bb_wfopen_input near bb_wfopen
[oweals/busybox.git] / coreutils / expr.c
index fdb4e3997c99cb22e8ead5fae940d5158bf9dc5f..3f052d92a74f301e56206343629d70b9cce525ee 100644 (file)
@@ -5,28 +5,28 @@
  * based on GNU expr Mike Parker.
  * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
  *
- * Busybox modifications 
+ * Busybox modifications
  * Copyright (c) 2000  Edward Betts <edward@debian.org>.
  * Aug 2003  Vladimir Oleynik - reduced 464 bytes.
  *
- * 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
- * the free software foundation; either version 2 of the license, or
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * this program 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 for more details.
+ * This program 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 for more details.
  *
- * you should have received a copy of the gnu general public license
- * along with this program; if not, write to the free software
- * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  */
 
 /* This program evaluates expressions.  Each token (operator, operand,
- * parenthesis) of the expression must be a seperate argument.  The
+ * parenthesis) of the expression must be a separate argument.  The
  * parser used is a reasonably general one, though any incarnation of
  * it is language-specific.  It is especially nice for expressions.
  *
@@ -117,7 +117,7 @@ static VALUE *str_value (char *s)
 
        v = xmalloc (sizeof(VALUE));
        v->type = string;
-       v->u.s = strdup (s);
+       v->u.s = bb_xstrdup (s);
        return v;
 }
 
@@ -245,10 +245,9 @@ static int arithmetic_common (VALUE *l, VALUE *r, int op)
 static VALUE *docolon (VALUE *sv, VALUE *pv)
 {
        VALUE *v;
-       const char *errmsg;
-       struct re_pattern_buffer re_buffer;
-       struct re_registers re_regs;
-       int len;
+       regex_t re_buffer;
+       const int NMATCH = 2;
+       regmatch_t re_regs[NMATCH];
 
        tostring (sv);
        tostring (pv);
@@ -260,27 +259,22 @@ of a basic regular expression is not portable; it is being ignored",
                pv->u.s);
        }
 
-       len = strlen (pv->u.s);
        memset (&re_buffer, 0, sizeof (re_buffer));
-       memset (&re_regs, 0, sizeof (re_regs));
-       re_buffer.allocated = 2 * len;
-       re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
-       re_buffer.translate = 0;
-       re_syntax_options = RE_SYNTAX_POSIX_BASIC;
-       errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
-       if (errmsg) {
-               bb_error_msg_and_die("%s", errmsg);
-       }
-
-       len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
-       if (len >= 0) {
+       memset (re_regs, 0, sizeof (*re_regs));
+       if( regcomp (&re_buffer, pv->u.s, 0) != 0 )
+               bb_error_msg_and_die("Invalid regular expression");
+
+       /* expr uses an anchored pattern match, so check that there was a
+        * match and that the match starts at offset 0. */
+       if (regexec (&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
+                       re_regs[0].rm_so == 0) {
                /* Were \(...\) used? */
-               if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
-                       sv->u.s[re_regs.end[1]] = '\0';
-                       v = str_value (sv->u.s + re_regs.start[1]);
+               if (re_buffer.re_nsub > 0) {
+                       sv->u.s[re_regs[1].rm_eo] = '\0';
+                       v = str_value (sv->u.s + re_regs[1].rm_so);
                }
                else
-                       v = int_value (len);
+                       v = int_value (re_regs[0].rm_eo);
        }
        else {
                /* Match failed -- return the right kind of null.  */
@@ -289,7 +283,6 @@ of a basic regular expression is not portable; it is being ignored",
                else
                        v = int_value (0);
        }
-       free (re_buffer.buffer);
        return v;
 }