diff options
-rw-r--r-- | LICENSE | 3 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | Makefile.depend | 4 | ||||
-rw-r--r-- | compat_strtonum.c | 76 | ||||
-rwxr-xr-x | configure | 6 | ||||
-rw-r--r-- | configure.local.example | 1 | ||||
-rw-r--r-- | test-strtonum.c | 42 |
7 files changed, 134 insertions, 3 deletions
@@ -9,8 +9,9 @@ Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org> Copyright (c) 2009, 2010, 2011, 2012 Joerg Sonnenberger <joerg@netbsd.org> Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de> Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> -Copyright (c) 1998, 2010 Todd C. Miller <Todd.Miller@courtesan.com> +Copyright (c) 1998, 2004, 2010 Todd C. Miller <Todd.Miller@courtesan.com> Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> +Copyright (c) 2004 Ted Unangst <tedu@openbsd.org> Copyright (c) 2003, 2007, 2008, 2014 Jason McIntyre <jmc@openbsd.org> See the individual source files for information about who contributed @@ -31,6 +31,7 @@ TESTSRCS = test-dirent-namlen.c \ test-strlcpy.c \ test-strptime.c \ test-strsep.c \ + test-strtonum.c \ test-wchar.c SRCS = att.c \ @@ -46,6 +47,7 @@ SRCS = att.c \ compat_strlcat.c \ compat_strlcpy.c \ compat_strsep.c \ + compat_strtonum.c \ demandoc.c \ eqn.c \ eqn_html.c \ @@ -189,7 +191,8 @@ COMPAT_OBJS = compat_fgetln.o \ compat_strcasestr.o \ compat_strlcat.o \ compat_strlcpy.o \ - compat_strsep.o + compat_strsep.o \ + compat_strtonum.o MANDOC_HTML_OBJS = eqn_html.o \ html.o \ diff --git a/Makefile.depend b/Makefile.depend index a61de19f..54af8bce 100644 --- a/Makefile.depend +++ b/Makefile.depend @@ -11,6 +11,7 @@ compat_strcasestr.o: compat_strcasestr.c config.h compat_strlcat.o: compat_strlcat.c config.h compat_strlcpy.o: compat_strlcpy.c config.h compat_strsep.o: compat_strsep.c config.h +compat_strtonum.o: compat_strtonum.c config.h demandoc.o: demandoc.c config.h man.h mdoc.h mandoc.h eqn.o: eqn.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h eqn_html.o: eqn_html.c config.h mandoc.h out.h html.h @@ -39,7 +40,7 @@ mdoc_macro.o: mdoc_macro.c config.h mdoc.h mandoc.h libmdoc.h libmandoc.h mdoc_man.o: mdoc_man.c config.h mandoc.h mandoc_aux.h out.h man.h mdoc.h main.h mdoc_term.o: mdoc_term.c config.h mandoc.h mandoc_aux.h out.h term.h mdoc.h main.h mdoc_validate.o: mdoc_validate.c config.h mdoc.h mandoc.h mandoc_aux.h libmdoc.h libmandoc.h -msec.o: msec.c config.h libmandoc.h msec.in +msec.o: msec.c config.h mandoc.h libmandoc.h msec.in out.o: out.c config.h mandoc_aux.h mandoc.h out.h preconv.o: preconv.c config.h mandoc.h libmandoc.h read.o: read.c config.h mandoc.h mandoc_aux.h libmandoc.h mdoc.h man.h @@ -69,4 +70,5 @@ test-strlcat.o: test-strlcat.c test-strlcpy.o: test-strlcpy.c test-strptime.o: test-strptime.c test-strsep.o: test-strsep.c +test-strtonum.o: test-strtonum.c test-wchar.o: test-wchar.c diff --git a/compat_strtonum.c b/compat_strtonum.c new file mode 100644 index 00000000..75351582 --- /dev/null +++ b/compat_strtonum.c @@ -0,0 +1,76 @@ +#include "config.h" + +#if HAVE_STRTONUM + +int dummy; + +#else + +/* $Id$ */ +/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */ + +/* + * Copyright (c) 2004 Ted Unangst and Todd Miller + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <errno.h> +#include <limits.h> +#include <stdlib.h> + +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 + +long long +strtonum(const char *numstr, long long minval, long long maxval, + const char **errstrp) +{ + long long ll = 0; + int error = 0; + char *ep; + struct errval { + const char *errstr; + int err; + } ev[4] = { + { NULL, 0 }, + { "invalid", EINVAL }, + { "too small", ERANGE }, + { "too large", ERANGE }, + }; + + ev[0].err = errno; + errno = 0; + if (minval > maxval) { + error = INVALID; + } else { + ll = strtoll(numstr, &ep, 10); + if (numstr == ep || *ep != '\0') + error = INVALID; + else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) + error = TOOSMALL; + else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) + error = TOOLARGE; + } + if (errstrp != NULL) + *errstrp = ev[error].errstr; + errno = ev[error].err; + if (error) + ll = 0; + + return (ll); +} + +#endif /* !HAVE_STRTONUM */ @@ -56,6 +56,7 @@ HAVE_STRLCAT= HAVE_STRLCPY= HAVE_STRPTIME= HAVE_STRSEP= +HAVE_STRTONUM= HAVE_WCHAR= HAVE_SQLITE3= @@ -177,6 +178,7 @@ runtest strlcat STRLCAT || true runtest strlcpy STRLCPY || true runtest strptime STRPTIME || true runtest strsep STRSEP || true +runtest strtonum STRTONUM || true runtest wchar WCHAR || true # --- sqlite3 --- @@ -291,6 +293,7 @@ cat << __HEREDOC__ #define HAVE_STRLCPY ${HAVE_STRLCPY} #define HAVE_STRPTIME ${HAVE_STRPTIME} #define HAVE_STRSEP ${HAVE_STRSEP} +#define HAVE_STRTONUM ${HAVE_STRTONUM} #define HAVE_WCHAR ${HAVE_WCHAR} #define HAVE_SQLITE3 ${HAVE_SQLITE3} #define HAVE_SQLITE3_ERRSTR ${HAVE_SQLITE3_ERRSTR} @@ -343,6 +346,9 @@ __HEREDOC__ [ ${HAVE_STRSEP} -eq 0 ] && \ echo "extern char *strsep(char **, const char *);" +[ ${HAVE_STRTONUM} -eq 0 ] && \ + echo "extern long long strtonum(const char *, long long, long long, const char **);" + echo echo "#endif /* MANDOC_CONFIG_H */" diff --git a/configure.local.example b/configure.local.example index 4318ff79..b470fb0f 100644 --- a/configure.local.example +++ b/configure.local.example @@ -226,6 +226,7 @@ HAVE_STRLCAT=0 HAVE_STRLCPY=0 HAVE_STRPTIME=0 HAVE_STRSEP=0 +HAVE_STRTONUM=0 HAVE_SQLITE3=0 HAVE_SQLITE3_ERRSTR=0 diff --git a/test-strtonum.c b/test-strtonum.c new file mode 100644 index 00000000..a336ea94 --- /dev/null +++ b/test-strtonum.c @@ -0,0 +1,42 @@ +/* $Id$ */ +/* + * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdlib.h> + +int +main(void) +{ + const char *errstr; + + if (strtonum("1", 0, 2, &errstr) != 1) + return(1); + if (errstr != NULL) + return(2); + if (strtonum("1x", 0, 2, &errstr) != 0) + return(3); + if (errstr == NULL) + return(4); + if (strtonum("2", 0, 1, &errstr) != 0) + return(5); + if (errstr == NULL) + return(6); + if (strtonum("0", 1, 2, &errstr) != 0) + return(7); + if (errstr == NULL) + return(8); + return(0); +} |