adding a test case
[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 #include "methodheaders.h"
11 #ifdef INPUTFILE
12 #include "InputFileArrays.h"
13 #endif
14
15 #ifdef D___FileOutputStream______nativeWrite____I__AR_B_I_I
16 void CALL34(___FileOutputStream______nativeWrite____I__AR_B_I_I, int fd, int off, int len, int fd, struct ArrayObject * ___array___, int off, int len) {
17 #ifdef MULTICORE
18 #else
19   char * string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
20   int status=write(fd, &string[off], len);
21 #endif
22 }
23 #endif
24
25 #ifdef D___FileOutputStream______nativeClose____I
26 void CALL11(___FileOutputStream______nativeClose____I, int fd, int fd) {
27 #ifdef MULTICORE
28 #else
29   close(fd);
30 #endif
31 }
32 #endif
33
34 #ifdef D___FileOutputStream______nativeFlush____I
35 void CALL11(___FileOutputStream______nativeFlush____I, int fd, int fd) {
36   // not supported in RAW version
37 #ifdef MULTICORE
38 #else
39   fsync(fd);
40 #endif
41 }
42 #endif
43
44 #ifdef D___FileOutputStream______nativeOpen_____AR_B
45 int CALL01(___FileOutputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
46   int length=VAR(___filename___)->___length___;
47   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
48 #ifdef MULTICORE
49 #ifdef INPUTFILE
50   int fd=filename2fd(filename, length);
51   return fd;
52 #else
53   return 0;
54 #endif
55 #else
56   int fd=open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
57   return fd;
58 #endif
59 }
60 #endif
61
62 #ifdef D___FileOutputStream______nativeAppend_____AR_B
63 int CALL01(___FileOutputStream______nativeAppend_____AR_B, struct ArrayObject * ___filename___) {
64 #ifdef MULTICORE
65   return 0;
66 #else
67   int length=VAR(___filename___)->___length___;
68   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
69   int fd=open(filename, O_WRONLY|O_CREAT|O_APPEND, S_IRWXU);
70   return fd;
71 #endif
72 }
73 #endif
74
75 #ifdef D___FileInputStream______nativeOpen_____AR_B
76 int CALL01(___FileInputStream______nativeOpen_____AR_B, struct ArrayObject * ___filename___) {
77   int length=VAR(___filename___)->___length___;
78   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
79 #ifdef MULTICORE
80 #ifdef INPUTFILE
81   int fd=filename2fd(filename, length);
82   return fd;
83 #else
84   return 0;
85 #endif
86 #else
87   int fd;
88   if ((fd=open(filename, O_RDONLY, 0)) < 0) {
89     printf(">>>\n");
90     perror("open failed");
91     printf("filename is %s\n", filename);
92     system("pwd");
93     printf("<<<\n");
94   }
95   return fd;
96 #endif
97 }
98 #endif
99
100 #ifdef D___FileInputStream______nativeClose____I
101 void CALL11(___FileInputStream______nativeClose____I, int fd, int fd) {
102 #ifdef MULTICORE
103 #else
104   close(fd);
105 #endif
106 }
107 #endif
108
109 #ifdef D___FileInputStream______nativeRead____I__AR_B_I
110 int CALL23(___FileInputStream______nativeRead____I__AR_B_I, int fd, int numBytes, int fd, struct ArrayObject * ___array___, int numBytes) {
111 #ifdef MULTICORE
112   return -1;
113 #else
114   int toread=VAR(___array___)->___length___;
115   char* string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
116   int status;
117
118   if (numBytes<toread)
119     toread=numBytes;
120
121   if ((status=read(fd, string, toread)) < 0) {
122     perror("");
123   }
124   return status;
125 #endif
126 }
127 #endif
128
129 #ifdef D___FileInputStream______nativePeek____I
130 int CALL11(___FileInputStream______nativePeek____I, int fd, int fd) {
131 #ifdef MULTICORE
132   return 0;
133 #else
134   int status;
135   char string[1];
136   status=read(fd, string, 1);
137
138   if( status <= 0 ) {
139     return status;
140   }
141   lseek(fd, -1, SEEK_CUR);
142   return string[0];
143 #endif
144 }
145 #endif
146
147 #ifdef D___File______nativeLength_____AR_B
148 long long CALL01(___File______nativeLength_____AR_B, struct ArrayObject * ___pathname___) {
149 #ifdef MULTICORE
150   return 0;
151 #else
152   int length=VAR(___pathname___)->___length___;
153   char* filename= (((char *)&VAR(___pathname___)->___length___)+sizeof(int));
154   struct stat st;
155   stat(filename, &st);
156   return st.st_size;
157 #endif
158 }
159 #endif
160
161 #ifdef D___FileInputStream______nativeAvailable____I
162 int CALL11(___FileInputStream______nativeAvailable____I, int fd, int fd) {
163 #ifdef MULTICORE
164   return 0;
165 #else
166   int avail;
167   int cur=lseek(fd, 0, SEEK_CUR);
168   int fsize = lseek(fd, 0, SEEK_END);
169   lseek(fd,cur,SEEK_SET); // seek back to the current position
170   avail=fsize-cur;
171   return avail;
172 #endif
173 }
174 #endif