aboutsummaryrefslogtreecommitdiff
path: root/tests/env
diff options
context:
space:
mode:
authormax99x <max99x@gmail.com>2011-07-29 22:32:29 +0300
committermax99x <max99x@gmail.com>2011-07-29 22:32:29 +0300
commitb6b5430d73d74671bbae160715eed397b4cecd28 (patch)
tree69187dcd51a826e5fd8b50a3da0b6678b4b5e718 /tests/env
parent1ac86ad03da43b50092ca18b5dcdc6359028d7a5 (diff)
Implemented support for environment variables.
Diffstat (limited to 'tests/env')
-rw-r--r--tests/env/output.txt29
-rw-r--r--tests/env/src.c43
2 files changed, 72 insertions, 0 deletions
diff --git a/tests/env/output.txt b/tests/env/output.txt
new file mode 100644
index 00000000..fa342d48
--- /dev/null
+++ b/tests/env/output.txt
@@ -0,0 +1,29 @@
+List:
+USER=root
+PATH=/
+PWD=/
+HOME=/
+LANG=en_US.UTF-8
+_=./this.program
+
+getenv(PATH): /
+getenv(NONEXISTENT): (null)
+setenv/0(PATH) ret: 0
+getenv(PATH) after setenv/0: /
+setenv/1(PATH) ret: 0
+getenv(PATH) after setenv/1: test2
+setenv(SET_VALUE) ret: 0
+setenv(SET_VALUE2) ret: 0
+getenv(SET_VALUE): test3
+getenv(SET_VALUE2): test4
+putenv(PUT_VALUE) ret: 0
+getenv(PUT_VALUE): test5
+getenv(PUT_VALUE) after alteration: test5
+unsetenv(PUT_VALUE) ret: 0
+getenv(PUT_VALUE) after unsetenv: (null)
+setenv(0) ret: -1
+setenv('') ret: -1
+setenv(X=Y) ret: -1
+unsetenv(0) ret: -1
+unsetenv('') ret: -1
+unsetenv(X=Y) ret: -1
diff --git a/tests/env/src.c b/tests/env/src.c
new file mode 100644
index 00000000..46419495
--- /dev/null
+++ b/tests/env/src.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main() {
+ printf("List:\n");
+ for (char** str = __environ; *str; str++) {
+ printf("%s\n", *str);
+ }
+ printf("\n");
+
+ printf("getenv(PATH): %s\n", getenv("PATH"));
+ printf("getenv(NONEXISTENT): %s\n", getenv("NONEXISTENT"));
+
+ printf("setenv/0(PATH) ret: %d\n", setenv("PATH", "test", 0));
+ printf("getenv(PATH) after setenv/0: %s\n", getenv("PATH"));
+ printf("setenv/1(PATH) ret: %d\n", setenv("PATH", "test2", 1));
+ printf("getenv(PATH) after setenv/1: %s\n", getenv("PATH"));
+
+ printf("setenv(SET_VALUE) ret: %d\n", setenv("SET_VALUE", "test3", 0));
+ printf("setenv(SET_VALUE2) ret: %d\n", setenv("SET_VALUE2", "test4", 1));
+ printf("getenv(SET_VALUE): %s\n", getenv("SET_VALUE"));
+ printf("getenv(SET_VALUE2): %s\n", getenv("SET_VALUE2"));
+
+ char buffer[] = "PUT_VALUE=test5";
+ printf("putenv(PUT_VALUE) ret: %d\n", putenv(buffer));
+ printf("getenv(PUT_VALUE): %s\n", getenv("PUT_VALUE"));
+ // NOTE: This should change the value. In our implementation, it doesn't.
+ buffer[10] = 'Q';
+ printf("getenv(PUT_VALUE) after alteration: %s\n", getenv("PUT_VALUE"));
+
+ printf("unsetenv(PUT_VALUE) ret: %d\n", unsetenv("PUT_VALUE"));
+ printf("getenv(PUT_VALUE) after unsetenv: %s\n", getenv("PUT_VALUE"));
+
+ printf("setenv(0) ret: %d\n", setenv(0, "foo", 1));
+ printf("setenv('') ret: %d\n", setenv("", "foo", 1));
+ printf("setenv(X=Y) ret: %d\n", setenv("X=Y", "foo", 1));
+ printf("unsetenv(0) ret: %d\n", unsetenv(0));
+ printf("unsetenv('') ret: %d\n", unsetenv(""));
+ printf("unsetenv(X=Y) ret: %d\n", unsetenv("X=Y"));
+
+ return 0;
+}