mirror of
https://https.git.savannah.gnu.org/git/findutils.git
synced 2026-01-26 15:39:06 +00:00
* find/defs.h: Update copyright year. * find/find.c: Update copyright year. * find/finddata.c: Update copyright year. * find/ftsfind.c: Update copyright year. * find/parser.c: Update copyright year. * find/pred.c: Update copyright year. * find/sharefile.c: Update copyright year. * find/sharefile.h: Update copyright year. * find/tree.c: Update copyright year. * find/util.c: Update copyright year. * lib/buildcmd.c: Update copyright year. * lib/buildcmd.h: Update copyright year. * lib/dircallback.c: Update copyright year. * lib/dircallback.h: Update copyright year. * lib/extendbuf.c: Update copyright year. * lib/extendbuf.h: Update copyright year. * lib/fdleak.c: Update copyright year. * lib/fdleak.h: Update copyright year. * lib/findutils-version.c: Update copyright year. * lib/findutils-version.h: Update copyright year. * lib/forcefindlib.c: Update copyright year. * lib/listfile.c: Update copyright year. * lib/listfile.h: Update copyright year. * lib/nextelem.c: Update copyright year. * lib/nextelem.h: Update copyright year. * lib/printquoted.c: Update copyright year. * lib/printquoted.h: Update copyright year. * lib/qmark.c: Update copyright year. * lib/regexprops.c: Update copyright year. (copying): Update copyright year in the output file, too. * lib/regextype.c: Update copyright year. * lib/regextype.h: Update copyright year. * lib/safe-atoi.c: Update copyright year. * lib/safe-atoi.h: Update copyright year. * lib/savedirinfo.c: Update copyright year. * lib/savedirinfo.h: Update copyright year. * lib/unused-result.h: Update copyright year. * lib/waitpid.c: Update copyright year. * locate/bigram.c: Update copyright year. * locate/code.c: Update copyright year. * locate/frcode.c: Update copyright year. * locate/locate.c: Update copyright year. * locate/locatedb.h: Update copyright year. * locate/word_io.c: Update copyright year. * xargs/xargs.c: Update copyright year.
87 lines
2.2 KiB
C
87 lines
2.2 KiB
C
/* safe-atoi.c -- checked string-to-int conversion.
|
|
Copyright (C) 2007, 2010, 2011 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 (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, style, end),
|
|
quotearg_n_style (1, style, s));
|
|
}
|
|
else if (end == s)
|
|
{
|
|
error (EXIT_FAILURE, errno, _("Expected an integer: %s"),
|
|
quotearg_n_style (0, style, s));
|
|
}
|
|
return (int)lval;
|
|
}
|