summaryrefslogtreecommitdiff
path: root/410user/libstdlib/strtoul.c
diff options
context:
space:
mode:
Diffstat (limited to '410user/libstdlib/strtoul.c')
-rw-r--r--410user/libstdlib/strtoul.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/410user/libstdlib/strtoul.c b/410user/libstdlib/strtoul.c
new file mode 100644
index 0000000..59d8f2a
--- /dev/null
+++ b/410user/libstdlib/strtoul.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 1995-1994 The University of Utah and
+ * the Computer Systems Laboratory at the University of Utah (CSL).
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify and distribute this software is hereby
+ * granted provided that (1) source code retains these copyright, permission,
+ * and disclaimer notices, and (2) redistributions including binaries
+ * reproduce the notices in supporting documentation, and (3) all advertising
+ * materials mentioning features or use of this software display the following
+ * acknowledgement: ``This product includes software developed by the
+ * Computer Systems Laboratory at the University of Utah.''
+ *
+ * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
+ * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
+ * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ *
+ * CSL requests users of this software to return to csl-dist@cs.utah.edu any
+ * improvements that they make and grant CSL redistribution rights.
+ */
+
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+unsigned long strtoul(const char *p, char **out_p, int base)
+{
+ unsigned long v = 0;
+
+ while (isspace(*p))
+ p++;
+ if (((base == 16) || (base == 0)) &&
+ ((*p == '0') && ((p[1] == 'x') || (p[1] == 'X'))))
+ {
+ p += 2;
+ base = 16;
+ }
+ if (base == 0)
+ {
+ if (*p == '0')
+ base = 8;
+ else
+ base = 10;
+ }
+ while (1)
+ {
+ char c = *p;
+ if ((c >= '0') && (c <= '9') && (c - '0' < base))
+ v = (v * base) + (c - '0');
+ else if ((c >= 'a') && (c <= 'z') && (c - 'a' + 10 < base))
+ v = (v * base) + (c - 'a' + 10);
+ else if ((c >= 'A') && (c <= 'Z') && (c - 'A' + 10 < base))
+ v = (v * base) + (c - 'A' + 10);
+ else
+ break;
+ p++;
+ }
+
+ if (out_p) *out_p = (char*)p;
+ return v;
+}
+