f9401e84d8c0836934182a524839d7db4821320c
[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 length=VAR(___filename___)->___length___;
88   char* filename= (((char *)&VAR(___filename___)->___length___)+sizeof(int));
89   int fd;
90   if ((fd=open(filename, O_RDONLY, 0)) < 0) {
91     printf(">>>\n");
92     perror("open failed");
93     printf("filename is %s\n", filename);
94     system("pwd");
95     printf("<<<\n");
96   }
97   return fd;
98 #endif
99 }
100 #endif
101
102 #ifdef D___FileInputStream______nativeClose____I
103 void CALL11(___FileInputStream______nativeClose____I, int fd, int fd) {
104 #ifdef MULTICORE
105 #else
106   close(fd);
107 #endif
108 }
109 #endif
110
111 #ifdef D___FileInputStream______nativeRead____I__AR_B_I
112 int CALL23(___FileInputStream______nativeRead____I__AR_B_I, int fd, int numBytes, int fd, struct ArrayObject * ___array___, int numBytes) {
113 #ifdef MULTICORE
114   return -1;
115 #else
116   int toread=VAR(___array___)->___length___;
117   char* string= (((char *)&VAR(___array___)->___length___)+sizeof(int));
118   int status;
119
120   if (numBytes<toread)
121     toread=numBytes;
122
123   if ((status=read(fd, string, toread)) < 0) {
124     perror("");
125   }
126   return status;
127 #endif
128 }
129 #endif
130
131 #ifdef D___FileInputStream______nativePeek____I
132 int CALL11(___FileInputStream______nativePeek____I, int fd, int fd) {
133 #ifdef MULTICORE
134   return 0;
135 #else
136   int status;
137   char string[1];
138   status=read(fd, string, 1);
139
140   if( status <= 0 ) {
141     return status;
142   }
143   lseek(fd, -1, SEEK_CUR);
144   return string[0];
145 #endif
146 }
147 #endif
148
149 #ifdef D___File______nativeLength_____AR_B
150 long long CALL01(___File______nativeLength_____AR_B, struct ArrayObject * ___pathname___) {
151 #ifdef MULTICORE
152   return 0;
153 #else
154   int length=VAR(___pathname___)->___length___;
155   char* filename= (((char *)&VAR(___pathname___)->___length___)+sizeof(int));
156   struct stat st;
157   stat(filename, &st);
158   return st.st_size;
159 #endif
160 }
161 #endif
162
163 #ifdef D___FileInputStream______nativeAvailable____I
164 int CALL11(___FileInputStream______nativeAvailable____I, int fd, int fd) {
165 #ifdef MULTICORE
166   return 0;
167 #else
168   int avail;
169   int cur=lseek(fd, 0, SEEK_CUR);
170   int fsize = lseek(fd, 0, SEEK_END);
171   lseek(fd,cur,SEEK_SET); // seek back to the current position
172   avail=fsize-cur;
173   return avail;
174 #endif
175 }
176 #endif