Added stubs for additional newlib system calls
[akaros.git] / user / apps / parlib / run_binary_colored.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <parlib.h>
10
11 extern char * readline(const char *prompt);
12
13 #define READ_SIZE       1024
14 uint8_t* binary_buf;
15
16 static void fd_error() {
17         fprintf(stderr, "Error: Unable to run remote binary (fd error): %s\n", 
18                                                               strerror(errno));
19 }
20
21 static void malloc_error() {
22         fprintf(stderr, 
23                 "Error: Unable to run remote binary: No more memory avaialable!\n");
24 }
25
26 static void read_error(void* buf, int fd) {
27         free(binary_buf);
28         close(fd);
29         fprintf(stderr, "Error: Unable to run remote binary (read error): %s\n", 
30                                                                 strerror(errno));
31 }
32
33 static void realloc_error(void* buf, int fd) {
34         free(binary_buf);
35         close(fd);
36         fprintf(stderr, 
37                 "Error: Unable to run remote binary: No more memory available!\n");
38 }
39
40 void run_binary_colored()
41 {       
42         char* name = readline("\nEnter name of binary to execute: ");
43         if (name == NULL) {
44                 printf("Error reading from console.\n");
45                 return;
46         }
47         char * file_name = malloc(strlen(name) + 8);
48         sprintf(file_name, "./apps/%s", name);
49         int fd = open(file_name, O_RDONLY, 0);
50         free(file_name);
51         if(fd < 0) { fd_error(); return; };
52
53         char* colors = readline("\nEnter number of colors: ");
54         if (colors == NULL) {
55                 printf("Error reading from console.\n");
56                 return;
57         }
58         size_t num_colors = atoi(colors);
59         
60         int iters = 1;
61         binary_buf = malloc(READ_SIZE);
62         if(binary_buf == NULL) { malloc_error(); return; }
63         
64         int total_bytes_read = 0;
65         int bytes_read = read(fd, binary_buf, READ_SIZE);
66         if(bytes_read < 0) { read_error(binary_buf, fd); return; }
67         
68         while(bytes_read > 0) {
69                 total_bytes_read += bytes_read; 
70                 void* temp_buf = realloc(binary_buf, READ_SIZE*(++iters));
71                 if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; } 
72                 binary_buf = temp_buf;
73                 bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
74                 if(bytes_read < 0) { read_error(binary_buf, fd); return; }
75         }
76         printf("Loading Binary: %s, ROMSIZE: %d\n", name, total_bytes_read);
77         ssize_t error = sys_run_binary(binary_buf, NULL, 
78                             total_bytes_read, num_colors);
79         if(error < 0) {
80                 fprintf(stderr, "Error: Unable to run remote binary\n");
81         }
82         free(binary_buf);
83         close(fd);
84 }
85