Move file_io to a matrix-run app. Cleaned up matrix and file_io.
[akaros.git] / user / apps / parlib / file_io.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 <stdio.h>
7 #include <unistd.h>
8
9 #define IN_BUF_SIZE 1024
10
11 extern char * readline(const char *prompt);
12
13 void file_io()
14 {       
15         printf("Beginning Serial Based File IO Test...\n\n");
16         int in_fd = open("./test/input", O_RDWR, 0);
17         char buf[IN_BUF_SIZE];
18         int read_amt = read(in_fd, buf, IN_BUF_SIZE - 1);
19         buf[read_amt] = '\0';
20         printf("Opened:       input\n");
21         printf("FD:           %d\n", in_fd);
22         printf("Read:         %d bytes\n", read_amt);
23         printf("Data read:    %s", buf);
24
25
26         char * readline_result = readline("\nEnter filename for writing: ");
27         if (readline_result == NULL) {
28                 printf("Error reading from console.\n");
29                 return;
30         }
31
32         char * file_name = malloc(strlen(readline_result) + 1);
33         strcpy(file_name, readline_result);
34
35         readline_result = readline("Enter text to write to file: ");
36
37         if (readline_result == NULL) {
38                 printf("Error reading from console.\n");
39                 return;
40         }
41
42
43         char *buf2 = malloc(strlen(readline_result) + 1);
44         strcpy(buf2, readline_result);
45
46
47         char * output_full_path = malloc(strlen(file_name) + 8);
48         sprintf(output_full_path, "./test/%s", file_name);
49
50         int out_fd = open(output_full_path, O_RDWR | O_CREAT | O_TRUNC,
51                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
52
53         printf("\nOpened:       %s\n", file_name);
54         printf("FD:           %d\n", out_fd);
55
56
57         int write_amt = write(out_fd, buf2, strlen(buf2));
58         
59         printf("Wrote:        %d bytes\n", write_amt);
60
61
62         int in_fd2 = open(output_full_path, O_RDWR, 0);
63         read_amt = read(in_fd2, buf, IN_BUF_SIZE - 1);
64         buf[read_amt] = '\0';
65         printf("Data written: %s\n\n", buf);
66
67         printf("Closing remote file descriptor: %d.... %s\n", in_fd, ((close(in_fd) == 0) ? "successful" : "failure"));
68         printf("Closing remote file descriptor: %d.... %s\n", out_fd, ((close(out_fd) == 0) ? "successful" : "failure"));
69         close(in_fd2);
70
71         printf("\nTests Complete.\n\n");
72 }