Paul Sajna 7 years ago
parent
commit
5cc52e4b19
11 changed files with 85 additions and 18 deletions
  1. 5 1
      platform/src/linux/mod.rs
  2. 0 7
      src/stdio/src/lib.rs
  3. 0 10
      src/unistd/src/lib.rs
  4. 6 0
      tests/.gitignore
  5. 5 0
      tests/Makefile
  6. 8 0
      tests/alloc.c
  7. 8 0
      tests/brk.c
  8. 15 0
      tests/chdir.c
  9. 16 0
      tests/dup.c
  10. 11 0
      tests/fchdir.c
  11. 11 0
      tests/fsync.c

+ 5 - 1
platform/src/linux/mod.rs

@@ -4,7 +4,11 @@ const AT_FDCWD: c_int = -100;
 
 pub fn brk(addr: *const c_void) -> c_int {
     unsafe {
-        syscall!(BRK, addr) as c_int
+        let newbrk = syscall!(BRK, addr);
+        if newbrk < addr as usize {
+            return -1
+        }
+        0
     }
 }
 

+ 0 - 7
src/stdio/src/lib.rs

@@ -190,13 +190,6 @@ pub extern "C" fn getchar_unlocked() -> c_int {
     unimplemented!();
 }
 
-#[no_mangle]
-pub extern "C" fn getopt(argc: c_int,
-                  argv: *const *const c_char,
-                  optstring: c_char) -> c_int {
-    unimplemented!();
-}
-
 #[no_mangle]
 pub extern "C" fn gets(s: *mut c_char)
      -> *mut c_char {

+ 0 - 10
src/unistd/src/lib.rs

@@ -74,16 +74,6 @@ pub extern "C" fn crypt(key: *const c_char, salt: *const c_char) -> *mut c_char
     unimplemented!();
 }
 
-#[no_mangle]
-pub extern "C" fn ctermid(s: *mut c_char) -> *mut c_char {
-    unimplemented!();
-}
-
-#[no_mangle]
-pub extern "C" fn cuserid(s: *mut c_char) -> *mut c_char {
-    unimplemented!();
-}
-
 #[no_mangle]
 pub extern "C" fn dup(fildes: c_int) -> c_int {
     platform::dup(fildes)

+ 6 - 0
tests/.gitignore

@@ -1,7 +1,13 @@
 /alloc
 /args
+/brk
+/chdir
 /create
 /create.out
+/dup
+/dup.out
+/fchdir
+/fsync
 /math
 /printf
 /write

+ 5 - 0
tests/Makefile

@@ -1,7 +1,12 @@
 BINS=\
 	alloc \
+	brk \
 	args \
+	chdir \
 	create \
+	dup \
+	fchdir \
+	fsync \
 	math \
 	printf \
 	write

+ 8 - 0
tests/alloc.c

@@ -9,4 +9,12 @@ int main(int argc, char ** argv) {
         ptr[i] = (char)i;
     }
     free(ptr);
+
+    char * ptrc = (char *)calloc(256,1);
+    printf("calloc %p\n", ptrc);
+    for(int i = 0; i < 256; i++) {
+        ptrc[i] = (char)i;
+    }
+    free(ptrc);
+
 }

+ 8 - 0
tests/brk.c

@@ -0,0 +1,8 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+    int status = brk((void*)100);
+    printf("brk exited with status code %d\n", status);
+}
+

+ 15 - 0
tests/chdir.c

@@ -0,0 +1,15 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char** argv) {
+    char* cwd1 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
+    getcwd(cwd1, 4096);
+    printf("initial cwd: %s\n", cwd1);
+    free(cwd1);
+    chdir("..");
+    char* cwd2 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
+    getcwd(cwd2, 4096);
+    printf("final cwd: %s\n", cwd2);
+    free(cwd2);
+}

+ 16 - 0
tests/dup.c

@@ -0,0 +1,16 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+    creat("dup.out", 0777);
+    int fd1 = open("dup.out", 0, 0);
+    int fd2 = dup(fd1);
+    printf("fd %d duped into fd %d\n", fd1, fd2);
+    close(fd1);
+    close(fd2);
+    int fd3 = open("dup.out", 0x0002, 0x1000);
+    dup2(fd3, 1);
+    printf("hello fd %d", fd3);
+    close(fd3);
+}

+ 11 - 0
tests/fchdir.c

@@ -0,0 +1,11 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+int main (int argc, char** argv) {
+    int fd = open("..", 0, 0);
+    int status;
+    status = fchdir(fd);
+    printf("fchdir exited with status code %d\n", status);
+    close(fd);
+}

+ 11 - 0
tests/fsync.c

@@ -0,0 +1,11 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+int main (int argc, char** argv) {
+    int fd = open(".", 0, 0);
+    int status;
+    status = fsync(fd);
+    printf("fsync exited with status code %d\n", status);
+    close(fd);
+}