packaging: remove old options to find hidden files
[platform/upstream/findutils.git] / lib / safe-atoi.c
1 /* safe-atoi.c -- checked string-to-int conversion.
2    Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* config.h must be included first. */
18 #include <config.h>
19
20 /* system headers. */
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24
25 /* gnulib headers. */
26 #include "error.h"
27 #include "gettext.h"
28 #include "quotearg.h"
29
30 /* find headers. */
31 #include "safe-atoi.h"
32
33 #if ENABLE_NLS
34 # include <libintl.h>
35 # define _(Text) gettext (Text)
36 #else
37 # define _(Text) Text
38 #endif
39 #ifdef gettext_noop
40 # define N_(String) gettext_noop (String)
41 #else
42 /* See locate.c for explanation as to why not use (String) */
43 # define N_(String) String
44 #endif
45
46
47 int
48 safe_atoi (const char *s, enum quoting_style style)
49 {
50   long lval;
51   char *end;
52
53   errno = 0;
54   lval = strtol (s, &end, 10);
55   if ( (LONG_MAX == lval) || (LONG_MIN == lval) )
56     {
57       /* max/min possible value, or an error. */
58       if (errno == ERANGE)
59         {
60           /* too big, or too small. */
61           error (EXIT_FAILURE, errno, "%s", s);
62         }
63       else
64         {
65           /* not a valid number */
66           error (EXIT_FAILURE, errno, "%s", s);
67         }
68       /* Otherwise, we do a range chack against INT_MAX and INT_MIN
69        * below.
70        */
71     }
72
73   if (lval > INT_MAX || lval < INT_MIN)
74     {
75       /* The number was in range for long, but not int. */
76       errno = ERANGE;
77       error (EXIT_FAILURE, errno, "%s", s);
78     }
79   else if (*end)
80     {
81       error (EXIT_FAILURE, errno, _("Unexpected suffix %s on %s"),
82              quotearg_n_style (0, style, end),
83              quotearg_n_style (1, style, s));
84     }
85   else if (end == s)
86     {
87       error (EXIT_FAILURE, errno, _("Expected an integer: %s"),
88              quotearg_n_style (0, style, s));
89     }
90   return (int)lval;
91 }