Merge remote branch 'origin/sparc-dev'
[akaros.git] / user / apps / parlib / run_binary.c
index 5f4e159..36cd8c8 100644 (file)
@@ -10,8 +10,8 @@
 
 extern char * readline(const char *prompt);
 
+#define MALLOC_SIZE     1048576
 #define READ_SIZE       1024
-uint8_t* binary_buf;
 
 static void fd_error() {
        fprintf(stderr, "Error: Unable to run remote binary (fd error): %s\n", strerror(errno));
@@ -22,13 +22,13 @@ static void malloc_error() {
 }
 
 static void read_error(void* buf, int fd) {
-       free(binary_buf);
+       free(buf);
        close(fd);
        fprintf(stderr, "Error: Unable to run remote binary (read error): %s\n", strerror(errno));
 }
 
 static void realloc_error(void* buf, int fd) {
-       free(binary_buf);
+       free(buf);
        close(fd);
        fprintf(stderr, "Error: Unable to run remote binary: No more memory available!\n");
 }
@@ -40,34 +40,39 @@ void run_binary()
                printf("Error reading from console.\n");
                return;
        }
-       char * file_name = malloc(strlen(readline_result) + 8);
-       sprintf(file_name, "./apps/%s", readline_result);
+       char* file_name = readline_result;
+       //char * file_name = malloc(strlen(readline_result) + 8);
+       //sprintf(file_name, "./apps/%s", readline_result);
        int fd = open(file_name, O_RDONLY, 0);
-       free(file_name);
+       //free(file_name);
        if(fd < 0) { fd_error(); return; };
        
-       int iters = 1;
-       binary_buf = malloc(READ_SIZE);
-       if(binary_buf == NULL) { malloc_error(); return; }
-       
        int total_bytes_read = 0;
-       int bytes_read = read(fd, binary_buf, READ_SIZE);
-       if(bytes_read < 0) { read_error(binary_buf, fd); return; }
+       int bytes_read = 0;
+       int bufsz = 0;
+       void* binary_buf = NULL;
        
-       while(bytes_read > 0) {
-               total_bytes_read += bytes_read; 
-               void* temp_buf = realloc(binary_buf, READ_SIZE*(++iters));
-               if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; } 
-               binary_buf = temp_buf;
+       while(1) {
+               if(total_bytes_read+READ_SIZE > bufsz)
+               {
+                       void* temp_buf = realloc(binary_buf,bufsz+MALLOC_SIZE);
+                       if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; }
+                       binary_buf = temp_buf;
+                       bufsz += MALLOC_SIZE;
+               }
+
                bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
+               total_bytes_read += bytes_read;
                if(bytes_read < 0) { read_error(binary_buf, fd); return; }
+               if(bytes_read == 0) break;
        }
        printf("Loading Binary: %s, ROMSIZE: %d\n", readline_result, total_bytes_read);
-       ssize_t error = sys_run_binary(binary_buf, NULL, total_bytes_read);
+       ssize_t error = sys_run_binary(binary_buf, NULL, total_bytes_read, 0);
        if(error < 0) {
                fprintf(stderr, "Error: Unable to run remote binary\n");
        }
        free(binary_buf);
        close(fd);
+       syscall(SYS_yield,0,0,0,0,0);
 }