aboutsummaryrefslogtreecommitdiff
path: root/sys/libc
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-19 10:06:21 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-19 10:06:21 -0700
commit5a25f38de6d3e2506838191c55af94cb56c9f641 (patch)
tree5dc6b2678fc76502c148c87c419c158680c3c4c5 /sys/libc
parentef512a454184bfbcdf3ac6b295e9fb5bf7e26841 (diff)
feat: quality of life scripts to initialize make rules
Diffstat (limited to 'sys/libc')
-rw-r--r--sys/libc/rules.mk41
-rw-r--r--sys/libc/stdio.c59
-rw-r--r--sys/libc/string.c80
3 files changed, 180 insertions, 0 deletions
diff --git a/sys/libc/rules.mk b/sys/libc/rules.mk
index e69de29..6234a1e 100644
--- a/sys/libc/rules.mk
+++ b/sys/libc/rules.mk
@@ -0,0 +1,41 @@
+# ---- Push on stack ----
+SP := $(SP).x
+DIRSTACK_$(SP) := $(d)
+d := $(DIR)
+
+# Iterate through subdirectory tree
+
+# Local sources
+SRCS_$(d) := $(wildcard $(d)/*.c)
+OBJS_$(d) := $(SRCS_$(d):.c=.o)
+OBJS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(OBJS_$(d)))
+DEPS_$(d) := $(OBJS_$(d):.o=.d)
+
+OBJS := $(OBJS) $(OBJS_$(d))
+DEPS := $(DEPS) $(DEPS_$(d))
+
+# Local targets
+LIBS_$(d) :=
+LIBS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(LIBS_$(d)))
+LIBS := $(LIBS) $(LIBS_$(d))
+
+BINS_$(d) :=
+BINS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(BINS_$(d)))
+BINS := $(BINS) $(BINS_$(d))
+
+# Local rules
+# $(LIBS_$(d)) := TGTFLAGS :=
+# $(LIBS_$(d)) := TGTINCS :=
+# $(LIBS_$(d)) := TGTLIBS :=
+
+$(LIBS_$(d)): $(OBJS_$(d))
+ $(ARCHIVE)
+
+$(BINS_$(d)): $(OBJ_DIR)/libn/test.o
+ $(LINK)
+
+# ---- Pop off stack ----
+-include $(DEPS_$(d))
+
+d := $(DIRSTACK_$(SP))
+SP := $(basename $(SP))
diff --git a/sys/libc/stdio.c b/sys/libc/stdio.c
new file mode 100644
index 0000000..f3295b2
--- /dev/null
+++ b/sys/libc/stdio.c
@@ -0,0 +1,59 @@
+#include <u.h>
+#include <libc.h>
+
+int
+printf(byte* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ int nw, rem, peek, len;
+ byte* str;
+
+ while (*fmt) {
+ rem = INT_MAX - nw;
+
+ if (fmt[0] != '%' || fmt[1] == '%') {
+ if (fmt[0] == '%') fmt++;
+
+ for (peek = 1; fmt[peek] && fmt[peek] != '%'; peek++) {
+ ;
+ }
+ if (rem < peek) return -1;
+ // TODO: Print here.
+ fmt += peek;
+ nw += peek;
+ continue;
+ }
+
+ str = fmt++;
+
+ switch (*fmt++) {
+ case 'c':
+ byte c = va_arg(args, int);
+ if (rem < 0) return -1;
+ // TODO: Print here
+ nw++;
+ break;
+
+ case 's':
+ str = va_arg(args, byte*);
+ len = strlen(str);
+ if (rem < len) return -1;
+ // TODO: Print here
+ nw += len;
+ break;
+ default:
+ fmt = str;
+ len = strlen(fmt);
+ if (rem < len) return -1;
+ // TODO: Print here
+ nw += len;
+ fmt += len;
+ break;
+ }
+ }
+
+ va_end(args);
+ return nw;
+}
diff --git a/sys/libc/string.c b/sys/libc/string.c
new file mode 100644
index 0000000..8fcdfd2
--- /dev/null
+++ b/sys/libc/string.c
@@ -0,0 +1,80 @@
+#include <u.h>
+#include <libc.h>
+
+void*
+memcopy(void* dst, void* src, intptr n)
+{
+ byte *e, *s, *d;
+
+ d = dst;
+ e = d + n;
+ for (s = src ; d != e; ++s, ++d) {
+ *d = *s;
+ }
+
+ return dst;
+}
+
+void*
+memmove(void* dst, void* src, intptr n)
+{
+ byte *e, *s, *d;
+ s = src;
+ d = dst;
+
+ if (d < s) {
+ e = d + n;
+ for (; d != e; ++s, ++d)
+ *d = *s;
+
+ } else {
+ e = d;
+ d += n;
+ s += n;
+ for (; d != e; --s, --d)
+ d[-1] = s[-1];
+ }
+
+ return dst;
+}
+
+void*
+memset(void* buf, int val, intptr n)
+{
+ byte *b, *e;
+ b = buf;
+ e = b + n;
+ for (; b != e; b++) {
+ *b = (byte)val;
+ }
+
+ return buf;
+}
+
+int
+memcmp(void* lhs, void* rhs, intptr n)
+{
+ byte *bl, *br, *e;
+
+ br = rhs;
+ e = br + n;
+ for (bl = lhs; br != e; ++bl, ++br) {
+ if (*bl < *br)
+ return -1;
+ else if (*bl > *br)
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+strlen(byte* s)
+{
+ byte* b;
+ for (b = s; *b; b++) {
+ ;
+ }
+
+ return b - s;
+}