From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from localhost (fantadrom.bsd.lv [local]); by fantadrom.bsd.lv (OpenSMTPD) with ESMTPA id 1b5ea2bf; for ; Mon, 16 Feb 2015 09:56:52 -0500 (EST) Date: Mon, 16 Feb 2015 09:56:52 -0500 (EST) Message-Id: <3523606115268837161.enqueue@fantadrom.bsd.lv> X-Mailinglist: mdocml-source Reply-To: source@mdocml.bsd.lv MIME-Version: 1.0 From: schwarze@mdocml.bsd.lv To: source@mdocml.bsd.lv Subject: mdocml: strtonum(3) compat glue X-Mailer: activitymail 1.26, http://search.cpan.org/dist/activitymail/ Content-Type: text/plain; charset=utf-8 Log Message: ----------- strtonum(3) compat glue Modified Files: -------------- mdocml: LICENSE Makefile Makefile.depend configure configure.local.example Added Files: ----------- mdocml: compat_strtonum.c test-strtonum.c Revision Data ------------- Index: Makefile.depend =================================================================== RCS file: /home/cvs/mdocml/mdocml/Makefile.depend,v retrieving revision 1.7 retrieving revision 1.8 diff -LMakefile.depend -LMakefile.depend -u -p -r1.7 -r1.8 --- Makefile.depend +++ Makefile.depend @@ -11,6 +11,7 @@ compat_strcasestr.o: compat_strcasestr.c 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 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 --- /dev/null +++ test-strtonum.c @@ -0,0 +1,42 @@ +/* $Id: test-strtonum.c,v 1.1 2015/02/16 14:56:22 schwarze Exp $ */ +/* + * Copyright (c) 2015 Ingo Schwarze + * + * 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 + +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); +} Index: configure =================================================================== RCS file: /home/cvs/mdocml/mdocml/configure,v retrieving revision 1.18 retrieving revision 1.19 diff -Lconfigure -Lconfigure -u -p -r1.18 -r1.19 --- configure +++ configure @@ -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} @@ -342,6 +345,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 */" Index: Makefile =================================================================== RCS file: /home/cvs/mdocml/mdocml/Makefile,v retrieving revision 1.454 retrieving revision 1.455 diff -LMakefile -LMakefile -u -p -r1.454 -r1.455 --- Makefile +++ Makefile @@ -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 \ Index: LICENSE =================================================================== RCS file: /home/cvs/mdocml/mdocml/LICENSE,v retrieving revision 1.6 retrieving revision 1.7 diff -LLICENSE -LLICENSE -u -p -r1.6 -r1.7 --- LICENSE +++ LICENSE @@ -9,8 +9,9 @@ Copyright (c) 2010-2015 Ingo Schwarze Copyright (c) 2013 Franco Fichtner Copyright (c) 1999, 2004 Marc Espie -Copyright (c) 1998, 2010 Todd C. Miller +Copyright (c) 1998, 2004, 2010 Todd C. Miller Copyright (c) 2008 Otto Moerbeek +Copyright (c) 2004 Ted Unangst Copyright (c) 2003, 2007, 2008, 2014 Jason McIntyre See the individual source files for information about who contributed --- /dev/null +++ compat_strtonum.c @@ -0,0 +1,76 @@ +#include "config.h" + +#if HAVE_STRTONUM + +int dummy; + +#else + +/* $Id: compat_strtonum.c,v 1.1 2015/02/16 14:56:22 schwarze Exp $ */ +/* $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 +#include +#include + +#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 */ Index: configure.local.example =================================================================== RCS file: /home/cvs/mdocml/mdocml/configure.local.example,v retrieving revision 1.5 retrieving revision 1.6 diff -Lconfigure.local.example -Lconfigure.local.example -u -p -r1.5 -r1.6 --- configure.local.example +++ 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 -- To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv