summaryrefslogtreecommitdiff
path: root/lib9
diff options
context:
space:
mode:
Diffstat (limited to 'lib9')
-rw-r--r--lib9/mkfile1
-rw-r--r--lib9/mkfile-Nt1
-rw-r--r--lib9/pread-Nt.c9
-rw-r--r--lib9/strtoull.c96
4 files changed, 107 insertions, 0 deletions
diff --git a/lib9/mkfile b/lib9/mkfile
index 2fd660b2..4166196e 100644
--- a/lib9/mkfile
+++ b/lib9/mkfile
@@ -14,6 +14,7 @@ COMMONFILES=\
qsort.$O\
runestrlen.$O\
strtoll.$O\
+ strtoull.$O\
rune.$O\
#
# files used by most models. these are added to TARGFILES in some
diff --git a/lib9/mkfile-Nt b/lib9/mkfile-Nt
index 28e714ba..f264c479 100644
--- a/lib9/mkfile-Nt
+++ b/lib9/mkfile-Nt
@@ -6,3 +6,4 @@ TARGFILES=\
getwd-Nt.$O\
setbinmode-Nt.$O\
lock-Nt-386.$O\
+ pread-Nt.$O\
diff --git a/lib9/pread-Nt.c b/lib9/pread-Nt.c
new file mode 100644
index 00000000..94fc81d5
--- /dev/null
+++ b/lib9/pread-Nt.c
@@ -0,0 +1,9 @@
+#include "lib9.h"
+
+long
+pread(int fd, void *buf, long n, vlong offset)
+{
+ if(seek(fd, offset, 0) < 0)
+ return -1;
+ return read(fd, buf, n);
+}
diff --git a/lib9/strtoull.c b/lib9/strtoull.c
new file mode 100644
index 00000000..3f08ba61
--- /dev/null
+++ b/lib9/strtoull.c
@@ -0,0 +1,96 @@
+#include "lib9.h"
+
+#define UVLONG_MAX (1LL<<63)
+
+uvlong
+strtoull(char *nptr, char **endptr, int base)
+{
+ char *p;
+ uvlong n, nn, m;
+ int c, ovfl, v, neg, ndig;
+
+ p = nptr;
+ neg = 0;
+ n = 0;
+ ndig = 0;
+ ovfl = 0;
+
+ /*
+ * White space
+ */
+ for(;; p++) {
+ switch(*p) {
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\f':
+ case '\r':
+ case '\v':
+ continue;
+ }
+ break;
+ }
+
+ /*
+ * Sign
+ */
+ if(*p == '-' || *p == '+')
+ if(*p++ == '-')
+ neg = 1;
+
+ /*
+ * Base
+ */
+ if(base == 0) {
+ base = 10;
+ if(*p == '0') {
+ base = 8;
+ if(p[1] == 'x' || p[1] == 'X'){
+ p += 2;
+ base = 16;
+ }
+ }
+ } else
+ if(base == 16 && *p == '0') {
+ if(p[1] == 'x' || p[1] == 'X')
+ p += 2;
+ } else
+ if(base < 0 || 36 < base)
+ goto Return;
+
+ /*
+ * Non-empty sequence of digits
+ */
+ m = UVLONG_MAX/base;
+ for(;; p++,ndig++) {
+ c = *p;
+ v = base;
+ if('0' <= c && c <= '9')
+ v = c - '0';
+ else
+ if('a' <= c && c <= 'z')
+ v = c - 'a' + 10;
+ else
+ if('A' <= c && c <= 'Z')
+ v = c - 'A' + 10;
+ if(v >= base)
+ break;
+ if(n > m)
+ ovfl = 1;
+ nn = n*base + v;
+ if(nn < n)
+ ovfl = 1;
+ n = nn;
+ }
+
+Return:
+ if(ndig == 0)
+ p = nptr;
+ if(endptr)
+ *endptr = p;
+ if(ovfl)
+ return UVLONG_MAX;
+ if(neg)
+ return -n;
+ return n;
+}