Still annotating the kernel
[akaros.git] / user / apps / parlib / file_io.c
index d9b01be..22ae07e 100644 (file)
@@ -1,3 +1,4 @@
+#include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -5,36 +6,70 @@
 #include <stdio.h>
 #include <unistd.h>
 
-int main(int argc, char** argv)
-{
-       printf("Welcome to the ROS newlib test suite!\nEnter at you're own risk....\n");
+#define IN_BUF_SIZE 1024
 
-       printf("Size of stat struct: %u\n", sizeof(struct stat));
+extern char * readline(const char *prompt);
 
-       int fd = open("./test/matrix", O_RDWR, 0);
-       char buf[100];
-       int read_amt = read(fd, buf, 100);
-       printf("FD: %d\n", fd);
-       printf("read_amt: %d\n", read_amt);
-       printf("read: %s\n", buf);
+void file_io()
+{      
+       printf("Beginning Serial Based File IO Test...\n\n");
+       int in_fd = open("./test/input", O_RDWR, 0);
+       char buf[IN_BUF_SIZE];
+       int read_amt = read(in_fd, buf, IN_BUF_SIZE - 1);
+       buf[read_amt] = '\0';
+       printf("Opened:       input\n");
+       printf("FD:           %d\n", in_fd);
+       printf("Read:         %d bytes\n", read_amt);
+       printf("Data read:    %s", buf);
 
-       char buf2[] = "NANWAN!\n";
 
-       int fd = open("./test/nanwan", O_RDWR | O_CREAT, 
-                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
-       
-       lseek(fd, 0, SEEK_END); 
+       char * readline_result = readline("\nEnter filename for writing: ");
+       if (readline_result == NULL) {
+               printf("Error reading from console.\n");
+               return;
+       }
+
+    unsigned int fname_len = strlen(readline_result) + 1;
+       char * file_name = malloc(fname_len);
+       strncpy(file_name, readline_result, fname_len);
+
+       readline_result = readline("Enter text to write to file: ");
+
+        if (readline_result == NULL) {
+                printf("Error reading from console.\n");
+                return;
+        }
+
+
+       unsigned int buf2_len = strlen(readline_result) + 1;
+       char *buf2 = malloc(buf2_len);
+       strncpy(buf2, readline_result, buf2_len);
 
-       int write_amt = write(fd, buf2, sizeof(buf2) -1);
-        printf("write_amt: %d\n", write_amt);        
-       printf("wrote: %s\n", buf2);
 
-       unlink ("./test/nanwan_link");
+       unsigned int ofp_len = strlen(file_name) + 8;
+       char * output_full_path = malloc(ofp_len);
+       snprintf(output_full_path, ofp_len, "./test/%s", file_name);
+
+       int out_fd = open(output_full_path, O_RDWR | O_CREAT | O_TRUNC,
+                      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+
+        printf("\nOpened:       %s\n", file_name);
+        printf("FD:           %d\n", out_fd);
+
+
+       int write_amt = write(out_fd, buf2, strlen(buf2));
+       
+        printf("Wrote:        %d bytes\n", write_amt);
 
 
-       link("./test/nanwan", "./test/nanwan_link");
-       unlink("./test/to_delete");
+        int in_fd2 = open(output_full_path, O_RDWR, 0);
+        read_amt = read(in_fd2, buf, IN_BUF_SIZE - 1);
+        buf[read_amt] = '\0';
+        printf("Data written: %s\n\n", buf);
 
+       printf("Closing remote file descriptor: %d.... %s\n", in_fd, ((close(in_fd) == 0) ? "successful" : "failure"));
+       printf("Closing remote file descriptor: %d.... %s\n", out_fd, ((close(out_fd) == 0) ? "successful" : "failure"));
+       close(in_fd2);
 
-       return 0;
+       printf("\nTests Complete.\n\n");
 }