aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/env/Makefile2
-rw-r--r--lib/env/env.c55
-rw-r--r--lib/io/Makefile2
-rw-r--r--lib/io/io.c22
-rw-r--r--lib/io/io.h7
-rwxr-xr-xlib/io/testbin0 -> 36104 bytes
-rw-r--r--lib/sys/reboot.h3
-rw-r--r--lib/sys/sync.h11
8 files changed, 84 insertions, 18 deletions
diff --git a/lib/env/Makefile b/lib/env/Makefile
index db9b5a8..128490b 100644
--- a/lib/env/Makefile
+++ b/lib/env/Makefile
@@ -1,3 +1,3 @@
unit_test:
- gcc env.c ../io/io.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DENV_UNIT_TEST -g
+ gcc env.c ../io/io.c ../aec/aec.c ../malloc/malloc.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DENV_UNIT_TEST -g
./test
diff --git a/lib/env/env.c b/lib/env/env.c
index d52c3f3..7ceaa15 100644
--- a/lib/env/env.c
+++ b/lib/env/env.c
@@ -1,16 +1,19 @@
#include "env.h"
#include "../malloc/malloc.h"
+#include "../cstr/cstr.h"
-const char **envp;
+char **envp;
+u64 envsize = 0;
+#define ENVSIZEINC 16
-const char **__getenvpair(const char *key);
+char **__getenvpair(const char *key);
u8 __match_env_key(const char *p, const char *key);
const char *getenv(const char *key)
{
- const char **p = __getenvpair(key);
+ char **p = __getenvpair(key);
const char *value;
if (p == 0)
@@ -25,14 +28,44 @@ const char *getenv(const char *key)
void setenv(const char *key, const char *value)
{
- const char **p = __getenvpair(key);
- *p = value;
+ char **p = __getenvpair(key);
+
+ if (p == 0) {
+ p = envp;
+
+ while (*p) ++p;
+
+ if (p - envp >= envsize) {
+ if (envsize == 0) {
+ for (; *(envp + envsize); ++envsize);
+ char **nenvp = malloc(sizeof(char *) * (envsize + ENVSIZEINC));
+ p = nenvp;
+ while (p < nenvp + envsize + ENVSIZEINC) *(p++) = 0;
+ p = nenvp;
+ while (*envp) *(p++) = *(envp++);
+ envp = nenvp;
+ } else {
+ envp = realloc(envp, envsize + ENVSIZEINC);
+ }
+
+ p = envp + envsize;
+ envsize += ENVSIZEINC;
+ }
+ }
+
+ *p = malloc((cstr_length(key) + cstr_length(key)) + 2);
+ char *i = *p;
+
+ while (*key) *(i++) = *(key++);
+ *(i++) = '=';
+ while (*value) *(i++) = *(value++);
+ *i = 0;
}
-const char **__getenvpair(const char *key)
+char **__getenvpair(const char *key)
{
- const char **p = envp;
+ char **p = envp;
while (*p) {
if (__match_env_key(*p, key)) {
@@ -48,7 +81,7 @@ const char **__getenvpair(const char *key)
const char **getenvp()
{
- return envp;
+ return (const char**)envp;
}
@@ -69,7 +102,7 @@ u8 __match_env_key(const char *p, const char *key)
return 1;
}
-void init_env(int argc, const char **argv, const char **envp__)
+void init_env(int argc, const char **argv, char **envp__)
{
envp = envp__;
}
@@ -80,7 +113,9 @@ void init_env(int argc, const char **argv, const char **envp__)
int main(int argc, const char **argv)
{
- wstdf("HOME: %s\n", getenv("HOSTNAME"));
+ wstdf("TEST: %s\n", getenv("TEST"));
+ setenv("TEST", "Hello, World!");
+ wstdf("TEST: %s\n", getenv("TEST"));
}
#endif
diff --git a/lib/io/Makefile b/lib/io/Makefile
index 73aa785..46d1d43 100644
--- a/lib/io/Makefile
+++ b/lib/io/Makefile
@@ -1,3 +1,3 @@
unit_test:
- gcc io.c ../aec/aec.c ../cstr/cstr.c ../env/env.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DIO_LIB_UNIT_TEST -g
+ gcc io.c ../malloc/malloc.c ../aec/aec.c ../cstr/cstr.c ../env/env.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DIO_LIB_UNIT_TEST -g
./test
diff --git a/lib/io/io.c b/lib/io/io.c
index e8384f3..7dee314 100644
--- a/lib/io/io.c
+++ b/lib/io/io.c
@@ -20,36 +20,48 @@ void rstd(char *buf, unsigned long count)
read(STDIN_FD, buf, count);
}
+
+void wf(unsigned int fd, char *buf)
+{
+ write(fd, buf, cstr_length(buf));
+}
+
void wstdf__(const char *buf, const void **args)
{
+ wff__(STDOUT_FD, buf, args);
+}
+
+
+void wff__(unsigned int fd, const char *buf, const void **args)
+{
const char *start = buf;
char stoi_buf[32] = "";
int i;
for (; *buf; ++buf) {
if (*buf == '%') {
if (buf - start > 0)
- __wstdn(start, buf - start);
+ write(fd, start, buf - start);
++buf;
switch (*buf) {
case '%': __wstdn("%", 1); break;
case 's':
- __wstdn(((const char **)args)[0], cstr_length(((const char**)args)[0]));
+ write(fd, ((const char **)args)[0], cstr_length(((const char**)args)[0]));
++args;
break;
case 'i':
for (i = 0; i < 32; ++i)
stoi_buf[i] = 0;
i64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
- __wstdn(stoi_buf, cstr_length(stoi_buf));
+ write(fd, stoi_buf, cstr_length(stoi_buf));
++args;
break;
case 'u':
for (i = 0; i < 32; ++i)
stoi_buf[i] = 0;
u64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
- __wstdn(stoi_buf, cstr_length(stoi_buf));
+ write(fd, stoi_buf, cstr_length(stoi_buf));
++args;
break;
case 'S':
@@ -71,7 +83,7 @@ void wstdf__(const char *buf, const void **args)
}
if (buf - start > 0)
- __wstdn(start, buf - start);
+ write(fd, start, buf - start);
}
#ifdef IO_LIB_UNIT_TEST
diff --git a/lib/io/io.h b/lib/io/io.h
index 06cc541..c373cd0 100644
--- a/lib/io/io.h
+++ b/lib/io/io.h
@@ -5,13 +5,20 @@
void wstd(const char *buf);
void rstd(char *buf, unsigned long count);
+void wf(unsigned int fd, char *buf);
void wstdf__(const char *buf, const void **args);
+void wff__(unsigned int fd, const char *buf, const void **args);
#define wstdf(buf, ...) { \
const void *__wstdf__args__[] = { __VA_ARGS__ }; \
wstdf__(buf, __wstdf__args__); \
}
+#define wff(fd, buf, ...) { \
+ const void *__wstdf__args__[] = { __VA_ARGS__ }; \
+ wff__(fd, buf, __wstdf__args__); \
+}
+
#endif
diff --git a/lib/io/test b/lib/io/test
new file mode 100755
index 0000000..ce7db41
--- /dev/null
+++ b/lib/io/test
Binary files differ
diff --git a/lib/sys/reboot.h b/lib/sys/reboot.h
index 22893ca..824fbc3 100644
--- a/lib/sys/reboot.h
+++ b/lib/sys/reboot.h
@@ -7,9 +7,10 @@
#define REBOOT_HARD_RESET 0x01234567
#define REBOOT_ENABLE_CAD 0x89abcdef
#define REBOOT_DISABLE_CAD 0
-#define REBOOT_POWEROFF 0x4321fedc
+#define REBOOT_RESTART 0x4321fedc
#define REBOOT_SUSPEND 0xd000fce2
#define REBOOT_NEW_KERNEL 0x45584543
+#define REBOOT_POWEROFF 0x01234567
static int reboot(int cmd)
{
diff --git a/lib/sys/sync.h b/lib/sys/sync.h
new file mode 100644
index 0000000..fe61454
--- /dev/null
+++ b/lib/sys/sync.h
@@ -0,0 +1,11 @@
+#ifndef SYS_H
+#define SYS_H
+
+#include "syscalls.h"
+
+void sync()
+{
+ syscall(SYNC);
+}
+
+#endif