aboutsummaryrefslogtreecommitdiff
path: root/djb
diff options
context:
space:
mode:
authorKlaatu <[email protected]>2015-05-17 15:33:21 +1200
committerKlaatu <[email protected]>2015-05-17 15:33:21 +1200
commitb0de699679e8f1e39af847ed172d1ba605b4370c (patch)
tree01dac00471d61f727394e508c613b29cff0ceae5 /djb
bulk upload of source
Diffstat (limited to 'djb')
-rw-r--r--djb/README2
-rw-r--r--djb/atoulong.c7
-rw-r--r--djb/buffer.h16
-rw-r--r--djb/buffer_get.c17
-rw-r--r--djb/buffer_put.c37
-rw-r--r--djb/buffer_putc.c5
-rw-r--r--djb/buffer_puts.c6
-rw-r--r--djb/byte_copy.c7
-rw-r--r--djb/byte_copyr.c8
-rw-r--r--djb/byte_diff.c11
-rw-r--r--djb/byte_set.c7
-rw-r--r--djb/byte_zero.c7
-rw-r--r--djb/env_get.c15
-rw-r--r--djb/fmt_str.c8
-rw-r--r--djb/fmt_ulong.c10
-rw-r--r--djb/scan_8ulong.c5
-rw-r--r--djb/scan_number.h9
-rw-r--r--djb/scan_ulong.c5
-rw-r--r--djb/str_chr.c6
-rw-r--r--djb/str_copy.c5
-rw-r--r--djb/str_copyn.c6
-rw-r--r--djb/str_diff.c11
-rw-r--r--djb/str_diffn.c11
-rw-r--r--djb/str_len.c5
-rw-r--r--djb/str_rchr.c9
25 files changed, 235 insertions, 0 deletions
diff --git a/djb/README b/djb/README
new file mode 100644
index 0000000..de175ef
--- /dev/null
+++ b/djb/README
@@ -0,0 +1,2 @@
+ninit uses small string/buffer operations.
+In most cases they are less than 15 bytes.
diff --git a/djb/atoulong.c b/djb/atoulong.c
new file mode 100644
index 0000000..95c96d4
--- /dev/null
+++ b/djb/atoulong.c
@@ -0,0 +1,7 @@
+unsigned int atoulong(char *s) /*EXTRACT_INCL*/ {
+ register unsigned int dest=0;
+ register unsigned char c;
+
+ while ((c=*s-'0')<10) { ++s; dest=dest*10 + c; }
+ return dest;
+}
diff --git a/djb/buffer.h b/djb/buffer.h
new file mode 100644
index 0000000..dd920f6
--- /dev/null
+++ b/djb/buffer.h
@@ -0,0 +1,16 @@
+#ifndef BUFFER_H
+#define BUFFER_H
+
+typedef struct buffer {
+ char *x; /* actual buffer space */
+ unsigned int p; /* current position */
+ unsigned int n; /* string position */
+ unsigned int a; /* allocated buffer size */
+ int fd;
+ int (*op)();
+} buffer;
+
+#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (int (*)())(op) }
+#include "../buffer_defs.h"
+#include "../byte_defs.h"
+#endif
diff --git a/djb/buffer_get.c b/djb/buffer_get.c
new file mode 100644
index 0000000..ed51707
--- /dev/null
+++ b/djb/buffer_get.c
@@ -0,0 +1,17 @@
+#include "buffer.h"
+#include <errno.h>
+
+int buffer_getc(buffer* b, char *s) /*EXTRACT_INCL*/ {
+ if (b->p >= b->n) {
+ int r;
+ while ((r=b->op(b->fd, b->x, b->a)) <0)
+ if (errno != EINTR) break;
+
+ if (r<=0) return r;
+ b->p = 0;
+ b->n = r;
+ }
+ *s = b->x[b->p];
+ b->p++;
+ return 1;
+}
diff --git a/djb/buffer_put.c b/djb/buffer_put.c
new file mode 100644
index 0000000..f67e98d
--- /dev/null
+++ b/djb/buffer_put.c
@@ -0,0 +1,37 @@
+#include <errno.h>
+#include "buffer.h"
+
+#ifdef USE_BUFFER_LARGE_WRITE
+#define X(a) (a>0xffffff)?0xffffff:a
+#else
+#define X(a) a
+#endif
+
+static int allwrite(buffer *b, const char *buf, unsigned int len) {
+ int w;
+ b->p = 0;
+ while (len) {
+ w = b->op(b->fd, buf, X(len));
+ if (w == -1) {
+ if (errno == EINTR) continue;
+ return -1;
+ }
+ buf += w;
+ len -= w;
+ }
+ return 0;
+}
+
+int buffer_flush(buffer *b) /*EXTRACT_INCL*/ {
+ return allwrite(b,b->x,b->p);
+}
+
+int buffer_put(buffer *b, const char* s, unsigned int len) /*EXTRACT_INCL*/ {
+ if (b->a-b->p < len) {
+ if (buffer_flush(b)==-1) return -1;
+ if (b->a < len) return allwrite(b, s, len);
+ }
+ byte_copy(b->x + b->p, len, s);
+ b->p += len;
+ return 0;
+}
diff --git a/djb/buffer_putc.c b/djb/buffer_putc.c
new file mode 100644
index 0000000..5ed0689
--- /dev/null
+++ b/djb/buffer_putc.c
@@ -0,0 +1,5 @@
+#include "buffer.h"
+
+int buffer_putc(buffer *b, char ch) /*EXTRACT_INCL*/ {
+ return buffer_put(b, &ch, 1);
+}
diff --git a/djb/buffer_puts.c b/djb/buffer_puts.c
new file mode 100644
index 0000000..0a8696e
--- /dev/null
+++ b/djb/buffer_puts.c
@@ -0,0 +1,6 @@
+#include "buffer.h"
+
+int buffer_puts(buffer *b, const char* s) /*EXTRACT_INCL*/ {
+ return buffer_put(b, s, str_len(s));
+}
+
diff --git a/djb/byte_copy.c b/djb/byte_copy.c
new file mode 100644
index 0000000..84d9f45
--- /dev/null
+++ b/djb/byte_copy.c
@@ -0,0 +1,7 @@
+void byte_copy(void *to, unsigned int n, const void *from) /*EXTRACT_INCL*/ {
+ char *d=(char*)to;
+ char *s=(char*)from;
+ unsigned int k=0;
+ for (; k<n; k++) d[k] = s[k];
+}
+
diff --git a/djb/byte_copyr.c b/djb/byte_copyr.c
new file mode 100644
index 0000000..9574ae7
--- /dev/null
+++ b/djb/byte_copyr.c
@@ -0,0 +1,8 @@
+void byte_copyr(void* dst, unsigned int n, const void* src) /*EXTRACT_INCL*/ {
+ char *d=(char*)dst;
+ char *s=(char*)src;
+ while (n) {
+ --n;
+ d[n] = s[n];
+ }
+}
diff --git a/djb/byte_diff.c b/djb/byte_diff.c
new file mode 100644
index 0000000..6ad0a13
--- /dev/null
+++ b/djb/byte_diff.c
@@ -0,0 +1,11 @@
+int byte_diff(const void* a, unsigned int len, const void* b) /*EXTRACT_INCL*/ {
+ char *x=(char *)a;
+ char *y=(char *)b;
+ unsigned int u=0;
+ char ch=0;
+ for (; u<len; u++) {
+ ch = x[u] - y[u];
+ if (ch) break;
+ }
+ return ch;
+}
diff --git a/djb/byte_set.c b/djb/byte_set.c
new file mode 100644
index 0000000..0248efa
--- /dev/null
+++ b/djb/byte_set.c
@@ -0,0 +1,7 @@
+void byte_set(const void* dst, unsigned int len, char ch) /*EXTRACT_INCL*/ {
+ char *d=(char*)dst;
+ while (len) {
+ --len;
+ d[len] = ch;
+ }
+}
diff --git a/djb/byte_zero.c b/djb/byte_zero.c
new file mode 100644
index 0000000..1fba78a
--- /dev/null
+++ b/djb/byte_zero.c
@@ -0,0 +1,7 @@
+void byte_zero(void *to, unsigned int k) /*EXTRACT_INCL*/ {
+ char *d = (char *)to;
+ while (k) {
+ k--;
+ d[k] = 0;
+ }
+}
diff --git a/djb/env_get.c b/djb/env_get.c
new file mode 100644
index 0000000..f84a1fa
--- /dev/null
+++ b/djb/env_get.c
@@ -0,0 +1,15 @@
+#include "../byte_defs.h"
+extern char **environ;
+
+char *env_get(const char *s) /*EXTRACT_INCL*/ {
+ int i;
+ unsigned int slen;
+ char *envi;
+
+ if (environ==0) return 0;
+ slen = str_len(s);
+ for (i = 0; (envi = environ[i]); ++i)
+ if ((!byte_diff(s,slen,envi)) && (envi[slen] == '='))
+ return envi + slen + 1;
+ return 0;
+}
diff --git a/djb/fmt_str.c b/djb/fmt_str.c
new file mode 100644
index 0000000..3843311
--- /dev/null
+++ b/djb/fmt_str.c
@@ -0,0 +1,8 @@
+unsigned int fmt_str(char *s, const char *t) /*EXTRACT_INCL*/ {
+ register unsigned int len;
+ char ch;
+ len = 0;
+ if (s) { while ((ch = t[len])) s[len++] = ch; }
+ else while (t[len]) len++;
+ return len;
+}
diff --git a/djb/fmt_ulong.c b/djb/fmt_ulong.c
new file mode 100644
index 0000000..dfd7f9e
--- /dev/null
+++ b/djb/fmt_ulong.c
@@ -0,0 +1,10 @@
+unsigned int fmt_ulong(char *s, unsigned long u) /*EXTRACT_INCL*/ {
+ register unsigned int len; register unsigned long q;
+ len = 1; q = u;
+ while (q > 9) { ++len; q /= 10; }
+ if (s) {
+ s += len;
+ do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */
+ }
+ return len;
+}
diff --git a/djb/scan_8ulong.c b/djb/scan_8ulong.c
new file mode 100644
index 0000000..1999e29
--- /dev/null
+++ b/djb/scan_8ulong.c
@@ -0,0 +1,5 @@
+#include "scan_number.h"
+SCAN_NUMBER_DEFINE(scan_8ulong, unsigned long, 8)
+#if 0
+unsigned int scan_8ulong(const char *s, unsigned long *u) /*EXTRACT_INCL*/
+#endif
diff --git a/djb/scan_number.h b/djb/scan_number.h
new file mode 100644
index 0000000..17e1586
--- /dev/null
+++ b/djb/scan_number.h
@@ -0,0 +1,9 @@
+#define SCAN_NUMBER_DEFINE(name, type, base) \
+unsigned int name(const char *s, type *u) {\
+ unsigned int pos;\
+ type result, c;\
+ pos = 0; result = 0;\
+ while ((c = (type) (unsigned char) (s[pos] - '0')) < base)\
+ { result = result * base + c; ++pos; }\
+ *u = result; return pos;\
+}
diff --git a/djb/scan_ulong.c b/djb/scan_ulong.c
new file mode 100644
index 0000000..8f240a8
--- /dev/null
+++ b/djb/scan_ulong.c
@@ -0,0 +1,5 @@
+#include "scan_number.h"
+SCAN_NUMBER_DEFINE(scan_ulong, unsigned long, 10)
+#if 0
+unsigned int scan_ulong(const char *s, unsigned long *u) /*EXTRACT_INCL*/
+#endif
diff --git a/djb/str_chr.c b/djb/str_chr.c
new file mode 100644
index 0000000..1506f30
--- /dev/null
+++ b/djb/str_chr.c
@@ -0,0 +1,6 @@
+unsigned int str_chr(const char *in, char needle) /*EXTRACT_INCL*/ {
+ unsigned int u=0;
+ char ch;
+ while ((ch=in[u]) && ch != needle) u++;
+ return u;
+}
diff --git a/djb/str_copy.c b/djb/str_copy.c
new file mode 100644
index 0000000..90d2da6
--- /dev/null
+++ b/djb/str_copy.c
@@ -0,0 +1,5 @@
+unsigned int str_copy(char *out,const char *in) /*EXTRACT_INCL*/ {
+ unsigned int len=0;
+ while ((out[len]=in[len])) len++;
+ return len;
+}
diff --git a/djb/str_copyn.c b/djb/str_copyn.c
new file mode 100644
index 0000000..3dab724
--- /dev/null
+++ b/djb/str_copyn.c
@@ -0,0 +1,6 @@
+unsigned int str_copyn(char *out,const char *in, unsigned int len) /*EXTRACT_INCL*/ {
+ unsigned int k=0;
+ while (k<len && (out[k]=in[k])) k++;
+ if (k<len) out[k] = 0;
+ return k;
+}
diff --git a/djb/str_diff.c b/djb/str_diff.c
new file mode 100644
index 0000000..2c5ba59
--- /dev/null
+++ b/djb/str_diff.c
@@ -0,0 +1,11 @@
+int str_diff(const char* a, const char* b) /*EXTRACT_INCL*/ {
+ unsigned int u=0;
+ char ch=0;
+ for (;; u++) {
+ ch = a[u]-b[u];
+ if (ch) break;
+ if (a[u]==0) break;
+ }
+ return ch;
+}
+
diff --git a/djb/str_diffn.c b/djb/str_diffn.c
new file mode 100644
index 0000000..934040f
--- /dev/null
+++ b/djb/str_diffn.c
@@ -0,0 +1,11 @@
+int str_diffn(const char* a, const char* b, unsigned int limit) /*EXTRACT_INCL*/ {
+ unsigned int u=0;
+ char ch=0;
+
+ for (; u<limit; u++) {
+ ch = a[u] - b[u];
+ if (ch) break;
+ if (a[u]==0) break;
+ }
+ return ch;
+}
diff --git a/djb/str_len.c b/djb/str_len.c
new file mode 100644
index 0000000..9ce761a
--- /dev/null
+++ b/djb/str_len.c
@@ -0,0 +1,5 @@
+unsigned int str_len(const char * s) /*EXTRACT_INCL*/ {
+ unsigned int len=0;
+ while (s[len]) ++len;
+ return len;
+}
diff --git a/djb/str_rchr.c b/djb/str_rchr.c
new file mode 100644
index 0000000..ce7ab37
--- /dev/null
+++ b/djb/str_rchr.c
@@ -0,0 +1,9 @@
+unsigned int str_rchr(const char *in, char needle) /*EXTRACT_INCL*/ {
+ char ch;
+ unsigned int u=0, found = (unsigned int)-1;
+ for (;; u++) {
+ if ((ch=in[u])==0) break;
+ if (ch==needle) found=u;
+ }
+ return (found != (unsigned int)-1) ? found : u;
+}