findutils-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Findutils-patches] [PATCH 08/19] Move safe_atoi out into a separate mod


From: James Youngman
Subject: [Findutils-patches] [PATCH 08/19] Move safe_atoi out into a separate module.
Date: Fri, 2 Apr 2010 23:47:08 +0100

* find/parser.c: Include "safe-atoi.h".  Remove the body of safe_atoi.
(parse_group): pass options.err_quoting_style to safe_atoi.
(insert_depthspec): Likewise.
(parse_user): Likewise.
* lib/safe-atoi.h: New file.
* lib/safe-atoi.c: New file.  Add parameter for quoting style (to
avoid an external reference to the "options"struct).
* po/POTFILES.in: Add lib/safe-atoi.c.
* lib/Makefile.am (libfind_a_SOURCES): Add safe-atoi.c and
safe-atoi.h.

Signed-off-by: James Youngman <address@hidden>
---
 find/parser.c   |   54 ++-------------------------------
 lib/Makefile.am |    5 ++-
 lib/safe-atoi.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/safe-atoi.h |   25 +++++++++++++++
 po/POTFILES.in  |    3 +-
 5 files changed, 122 insertions(+), 53 deletions(-)
 create mode 100644 lib/safe-atoi.c
 create mode 100644 lib/safe-atoi.h

diff --git a/find/parser.c b/find/parser.c
index eba3839..3406cd6 100644
--- a/find/parser.c
+++ b/find/parser.c
@@ -43,6 +43,7 @@
 #include "getdate.h"
 #include "error.h"
 #include "findutils-version.h"
+#include "safe-atoi.h"
 
 #include <fcntl.h>
 
@@ -1152,53 +1153,6 @@ parse_gid (const struct parser_table* entry, char 
**argv, int *arg_ptr)
 }
 
 
-static int
-safe_atoi (const char *s)
-{
-  long lval;
-  char *end;
-
-  errno = 0;
-  lval = strtol (s, &end, 10);
-  if ( (LONG_MAX == lval) || (LONG_MIN == lval) )
-    {
-      /* max/min possible value, or an error. */
-      if (errno == ERANGE)
-       {
-         /* too big, or too small. */
-         error (EXIT_FAILURE, errno, "%s", s);
-       }
-      else
-       {
-         /* not a valid number */
-         error (EXIT_FAILURE, errno, "%s", s);
-       }
-      /* Otherwise, we do a range chack against INT_MAX and INT_MIN
-       * below.
-       */
-    }
-
-  if (lval > INT_MAX || lval < INT_MIN)
-    {
-      /* The number was in range for long, but not int. */
-      errno = ERANGE;
-      error (EXIT_FAILURE, errno, "%s", s);
-    }
-  else if (*end)
-    {
-      error (EXIT_FAILURE, errno, _("Unexpected suffix %s on %s"),
-            quotearg_n_style (0, options.err_quoting_style, end),
-            quotearg_n_style (1, options.err_quoting_style, s));
-    }
-  else if (end == s)
-    {
-      error (EXIT_FAILURE, errno, _("Expected an integer: %s"),
-            quotearg_n_style (0, options.err_quoting_style, s));
-    }
-  return (int)lval;
-}
-
-
 static boolean
 parse_group (const struct parser_table* entry, char **argv, int *arg_ptr)
 {
@@ -1222,7 +1176,7 @@ parse_group (const struct parser_table* entry, char 
**argv, int *arg_ptr)
            {
              if (groupname[gid_len] == 0)
                {
-                 gid = safe_atoi (groupname);
+                 gid = safe_atoi (groupname, options.err_quoting_style);
                }
              else
                {
@@ -1489,7 +1443,7 @@ insert_depthspec (const struct parser_table* entry, char 
**argv, int *arg_ptr,
       depth_len = strspn (depthstr, "0123456789");
       if ((depth_len > 0) && (depthstr[depth_len] == 0))
        {
-         (*limitptr) = safe_atoi (depthstr);
+         (*limitptr) = safe_atoi (depthstr, options.err_quoting_style);
          if (*limitptr >= 0)
            {
              return parse_noop (entry, argv, arg_ptr);
@@ -2700,7 +2654,7 @@ parse_user (const struct parser_table* entry, char 
**argv, int *arg_ptr)
          const size_t uid_len = strspn (username, "0123456789");
          if (uid_len && (username[uid_len]==0))
            {
-             uid = safe_atoi (username);
+             uid = safe_atoi (username, options.err_quoting_style);
            }
          else
            {
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 57dec25..d9f6d14 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -34,9 +34,10 @@ INCLUDES = -I../gnulib/lib -I$(top_srcdir)/gnulib/lib
 LDADD = ../gnulib/lib/libgnulib.a $(LIBINTL)
 
 libfind_a_SOURCES += modetype.h nextelem.h printquoted.h listfile.h \
-       regextype.h dircallback.h
+       regextype.h dircallback.h safe-atoi.h
 libfind_a_SOURCES += listfile.c nextelem.c extendbuf.c buildcmd.c 
savedirinfo.c \
-       forcefindlib.c qmark.c printquoted.c regextype.c dircallback.c fdleak.c
+       forcefindlib.c qmark.c printquoted.c regextype.c dircallback.c fdleak.c 
\
+       safe-atoi.c
 
 EXTRA_DIST += waitpid.c forcefindlib.c
 TESTS_ENVIRONMENT = REGEXPROPS=regexprops$(EXEEXT)
diff --git a/lib/safe-atoi.c b/lib/safe-atoi.c
new file mode 100644
index 0000000..235d0e9
--- /dev/null
+++ b/lib/safe-atoi.c
@@ -0,0 +1,88 @@
+/* safe-atoi.c -- checked string-to-int conversion.
+   Copyright (C) 2007, 2010 Free Software Foundation, Inc.
+
+   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 3 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "safe-atoi.h"
+#include "quotearg.h"
+#include "error.h"
+
+
+#if ENABLE_NLS
+# include <libintl.h>
+# define _(Text) gettext (Text)
+#else
+# define _(Text) Text
+#endif
+#ifdef gettext_noop
+# define N_(String) gettext_noop (String)
+#else
+/* See locate.c for explanation as to why not use (String) */
+# define N_(String) String
+#endif
+
+
+int
+safe_atoi (const char *s, enum quoting_style style)
+{
+  long lval;
+  char *end;
+
+  errno = 0;
+  lval = strtol (s, &end, 10);
+  if ( (LONG_MAX == lval) || (LONG_MIN == lval) )
+    {
+      /* max/min possible value, or an error. */
+      if (errno == ERANGE)
+       {
+         /* too big, or too small. */
+         error (1, errno, "%s", s);
+       }
+      else
+       {
+         /* not a valid number */
+         error (1, errno, "%s", s);
+       }
+      /* Otherwise, we do a range chack against INT_MAX and INT_MIN
+       * below.
+       */
+    }
+
+  if (lval > INT_MAX || lval < INT_MIN)
+    {
+      /* The number was in range for long, but not int. */
+      errno = ERANGE;
+      error (1, errno, "%s", s);
+    }
+  else if (*end)
+    {
+      error (1, errno, _("Unexpected suffix %s on %s"),
+            quotearg_n_style (0, style, end),
+            quotearg_n_style (1, style, s));
+    }
+  else if (end == s)
+    {
+      error (1, errno, _("Expected an integer: %s"),
+            quotearg_n_style (0, style, s));
+    }
+  return (int)lval;
+}
+
+
diff --git a/lib/safe-atoi.h b/lib/safe-atoi.h
new file mode 100644
index 0000000..56ecd81
--- /dev/null
+++ b/lib/safe-atoi.h
@@ -0,0 +1,25 @@
+/* safe-atoi.h -- checked string-to-int conversion.
+   Copyright (C) 2010, Free Software Foundation, Inc.
+
+   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 3 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef SAFE_ATOI_H
+#define SAFE_ATOI_H 1
+
+#include "quotearg.h"
+
+int safe_atoi (const char *s, enum quoting_style style);
+
+#endif
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 0afb2c7..3ab3107 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -1,5 +1,5 @@
 # List of source files containing translatable strings.
-# Copyright (C) 2000, 2007 Free Software Foundation, Inc.
+# Copyright (C) 2000, 2007, 2010 Free Software Foundation, Inc.
 
 # Copying and distribution of this file, with or without
 # modification, are permitted provided the copyright notice
@@ -32,6 +32,7 @@ lib/buildcmd.c
 lib/dircallback.c
 lib/findutils-version.c
 lib/listfile.c
+lib/safe-atoi.c
 lib/regextype.c
 locate/code.c
 locate/frcode.c
-- 
1.7.0





reply via email to

[Prev in Thread] Current Thread [Next in Thread]