summaryrefslogtreecommitdiff
path: root/port/strtoul.c
diff options
context:
space:
mode:
authordron <dron>2005-07-07 16:34:06 +0000
committerdron <dron>2005-07-07 16:34:06 +0000
commitcb56ede7e0fc9cfb3dc15f0dae32f80ae24b0dd1 (patch)
tree646cfc4cc9dba8c7346c08635192a11a5a48d62d /port/strtoul.c
parentdbb6cb3a2efdb857ca03183d0e85cf271661df02 (diff)
downloadlibtiff-cb56ede7e0fc9cfb3dc15f0dae32f80ae24b0dd1.tar.gz
Update modules from the NetBSD source tree (the old 4-clause BSD license
changed to the new 3-clause one).
Diffstat (limited to 'port/strtoul.c')
-rw-r--r--port/strtoul.c70
1 files changed, 36 insertions, 34 deletions
diff --git a/port/strtoul.c b/port/strtoul.c
index ed8c1080..84ab2155 100644
--- a/port/strtoul.c
+++ b/port/strtoul.c
@@ -1,6 +1,8 @@
+/* $Id: strtoul.c,v 1.2 2005-07-07 16:34:06 dron Exp $ */
+
/*
- * Copyright (c) 1990 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -10,11 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@@ -31,13 +29,14 @@
* SUCH DAMAGE.
*/
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)strtoul.c 5.3 (Berkeley) 2/23/91";
-#endif /* LIBC_SCCS and not lint */
+#if 0
+static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
+__RCSID("$NetBSD: strtoul.c,v 1.16 2003/08/07 16:43:45 agc Exp $");
+#endif
-#include <limits.h>
#include <ctype.h>
#include <errno.h>
+#include <limits.h>
#include <stdlib.h>
/*
@@ -47,28 +46,28 @@ static char sccsid[] = "@(#)strtoul.c 5.3 (Berkeley) 2/23/91";
* alphabets and digits are each contiguous.
*/
unsigned long
-strtoul(nptr, endptr, base)
- const char *nptr;
- char **endptr;
- register int base;
+strtoul(const char *nptr, char **endptr, int base)
{
- register const char *s = nptr;
- register unsigned long acc;
- register int c;
- register unsigned long cutoff;
- register int neg = 0, any, cutlim;
+ const char *s;
+ unsigned long acc, cutoff;
+ int c;
+ int neg, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
+ s = nptr;
do {
- c = *s++;
+ c = (unsigned char) *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
- } else if (c == '+')
- c = *s++;
+ } else {
+ neg = 0;
+ if (c == '+')
+ c = *s++;
+ }
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
@@ -77,9 +76,10 @@ strtoul(nptr, endptr, base)
}
if (base == 0)
base = c == '0' ? 8 : 10;
- cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
- cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
- for (acc = 0, any = 0;; c = *s++) {
+
+ cutoff = ULONG_MAX / (unsigned long)base;
+ cutlim = (int)(ULONG_MAX % (unsigned long)base);
+ for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
@@ -88,20 +88,22 @@ strtoul(nptr, endptr, base)
break;
if (c >= base)
break;
- if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
+ if (any < 0)
+ continue;
+ if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
- else {
+ acc = ULONG_MAX;
+ errno = ERANGE;
+ } else {
any = 1;
- acc *= base;
+ acc *= (unsigned long)base;
acc += c;
}
}
- if (any < 0) {
- acc = ULONG_MAX;
- errno = ERANGE;
- } else if (neg)
+ if (neg && any > 0)
acc = -acc;
if (endptr != 0)
- *endptr = any ? s - 1 : (char *)nptr;
+ /* LINTED interface specification */
+ *endptr = (char *)(any ? s - 1 : nptr);
return (acc);
}