bf6c2d3d3aaacd1789883f80ce1bb4d310b77d6d
[IRC.git] / Robust / src / Runtime / file.c
1 #ifndef MULTICORE
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <unistd.h>
5 #endif
6 #include <sys/types.h>
7 #include "structdefs.h"
8 #include "mem.h"
9 #include "runtime.h"
10
11 void CALL34(___FileOutputStream______nativeWrite____I__AR_B_I_I, int fd, int off, int len, int fd, struct ArrayObject * ___array___, int off, int len) {
12 #ifdef MULTICORE
13 #else
14   char * string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
15   int status=write(fd, &string[off], len);
16 #endif
17 }
18
19 void CALL11(___FileOutputStream______nativeClose____I, int fd, int fd) {
20 #ifdef MULTICORE
21 #else
22   close(fd);
23 #endif
24 }
25
26 void CALL11(___FileOutputStream______nativeFlush____I, int fd, int fd) {
27   // not supported in RAW version
28 #ifdef MULTICORE
29 #else
30   fsync(fd);
31 #endif
32 }
33
34 int CALL01(___FileOutputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
35 #ifdef MULTICORE
36   return 0;
37 #else
38   int length=VAR(___filename___)->___length___;
39   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
40   int fd=open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
41   return fd;
42 #endif
43 }
44
45 int CALL01(___FileOutputStream______nativeAppend_____AR_B, struct ArrayObject * ___filename___) {
46 #ifdef MULTICORE
47   return 0;
48 #else  
49   int length=VAR(___filename___)->___length___;
50   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
51   int fd=open(filename, O_WRONLY|O_CREAT|O_APPEND, S_IRWXU);
52   return fd;
53 #endif
54 }
55
56 int CALL01(___FileInputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
57 #ifdef MULTICORE
58   return 0;
59 #else
60   int length=VAR(___filename___)->___length___;
61   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
62   int fd=open(filename, O_RDONLY, 0);
63   return fd;
64 #endif
65 }
66
67 void CALL11(___FileInputStream______nativeClose____I, int fd, int fd) {
68 #ifdef MULTICORE
69 #else
70   close(fd);
71 #endif
72 }
73
74 int CALL23(___FileInputStream______nativeRead____I__AR_B_I, int fd, int numBytes, int fd, struct ArrayObject * ___array___, int numBytes) {
75 #ifdef MULTICORE
76   return -1;
77 #else
78   int toread=VAR(___array___)->___length___;
79   char* string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
80   int status;
81
82   if (numBytes<toread)
83     toread=numBytes;
84
85   status=read(fd, string, toread);
86   return status;
87 #endif
88 }
89
90 int CALL11(___FileInputStream______nativePeek____I, int fd, int fd) {
91 #ifdef MULTICORE
92   return 0;
93 #else
94   int status;
95   char string[1];
96   status=read(fd, string, 1);
97
98   if( status <= 0 ) {
99     return status;
100   }
101   lseek(fd, -1, SEEK_CUR);
102   return string[0];
103 #endif
104 }
105
106 long long CALL01(___File______nativeLength_____AR_B, struct ArrayObject * ___pathname___) {
107 #ifdef MULTICORE
108   return 0;
109 #else
110   int length=VAR(___pathname___)->___length___;
111   char* filename= (((char *)&VAR(___pathname___)->___length___)+sizeof(int));
112   struct stat st;
113   stat(filename, &st);
114   return st.st_size;
115 #endif
116 }