1 /****************************** nat/io.c ***************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Contains the native functions for class java.io.
9 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
10 Mark Probst EMAIL: cacao@complang.tuwien.ac.at
12 Last Change: 1997/10/22
14 *******************************************************************************/
18 #include <sys/types.h>
24 #include "../threads/threadio.h" /* schani */
27 * io is performed through the threaded... functions, since io operations may
28 * block and the corresponding thread must, in this case, be suspended.
33 /********************* java.io.FileDescriptor ********************************/
35 s4 java_io_FileDescriptor_valid (struct java_io_FileDescriptor* this)
37 if (this->fd >= 0) return 1;
41 struct java_io_FileDescriptor* java_io_FileDescriptor_initSystemFD
42 (struct java_io_FileDescriptor* fd, s4 handle)
45 case 0: fd -> fd = fileno(stdin);
47 case 1: fd -> fd = fileno(stdout);
49 case 2: fd -> fd = fileno(stderr);
51 default: panic ("Invalid file descriptor number");
54 threadedFileDescriptor(fd->fd);
60 /************************* java.io.FileInputStream ***************************/
63 void java_io_FileInputStream_open
64 (struct java_io_FileInputStream* this, struct java_lang_String* name)
67 char *fname = javastring_tochar ((java_objectheader*)name);
69 if (!fname) goto fail;
71 fd = open (fname, O_RDONLY, 0);
74 threadedFileDescriptor(fd);
80 exceptionptr = native_new_and_init (class_java_io_IOException);
84 s4 java_io_FileInputStream_read (struct java_io_FileInputStream* this)
88 r = threadedRead (this->fd->fd, (char *) buffer, 1);
90 if (r>0) return buffer[0];
93 exceptionptr = native_new_and_init (class_java_io_IOException);
98 s4 java_io_FileInputStream_readBytes (struct java_io_FileInputStream* this,
99 java_bytearray* buffer, s4 start, s4 len)
101 s4 ret = threadedRead (this->fd->fd, (char *) buffer->data+start, len);
102 if (ret>0) return ret;
103 if (ret==0) return -1;
105 exceptionptr = native_new_and_init (class_java_io_IOException);
110 s8 java_io_FileInputStream_skip
111 (struct java_io_FileInputStream* this, s8 numbytes)
113 s4 ret = lseek (this->fd->fd, builtin_l2i(numbytes), SEEK_CUR);
114 if (ret>0) return builtin_i2l(ret);
115 if (ret==0) return builtin_i2l(-1);
117 exceptionptr = native_new_and_init (class_java_io_IOException);
118 return builtin_i2l(0);
121 s4 java_io_FileInputStream_available (struct java_io_FileInputStream* this)
126 r1 = fstat (this->fd->fd, &buffer);
127 r2 = lseek(this->fd->fd, 0, SEEK_CUR);
129 if ( (r1 >= 0) && (r2 >= 0) )
130 return buffer.st_size - r2;
132 exceptionptr = native_new_and_init (class_java_io_IOException);
136 void java_io_FileInputStream_close (struct java_io_FileInputStream* this)
138 if (this->fd->fd >= 0) {
139 s4 r = close (this->fd->fd);
142 exceptionptr = native_new_and_init (class_java_io_IOException);
150 /*************************** java.io.FileOutputStream ************************/
153 void java_io_FileOutputStream_open (struct java_io_FileOutputStream* this,
154 struct java_lang_String* name)
157 char *fname = javastring_tochar ((java_objectheader*)name);
158 if (!fname) goto fail;
160 fd = creat (fname, 0666);
163 threadedFileDescriptor(fd);
169 exceptionptr = native_new_and_init (class_java_io_IOException);
174 void java_io_FileOutputStream_write
175 (struct java_io_FileOutputStream* this, s4 byte)
181 l = threadedWrite (this->fd->fd, (char *) buffer, 1);
184 exceptionptr = native_new_and_init (class_java_io_IOException);
188 void java_io_FileOutputStream_writeBytes (struct java_io_FileOutputStream* this,
189 java_bytearray* buffer, s4 start, s4 len)
195 o = threadedWrite (this->fd->fd, (char *) buffer->data+start, len);
197 exceptionptr = native_new_and_init (class_java_io_IOException);
200 void java_io_FileOutputStream_close (struct java_io_FileOutputStream* this)
202 if (this->fd->fd >= 0) {
203 s4 r = close (this->fd->fd);
206 exceptionptr = native_new_and_init (class_java_io_IOException);
212 /************************** java.io.File **************************************/
214 s4 java_io_File_exists0 (struct java_io_File* this)
220 path = javastring_tochar( (java_objectheader*) (this->path));
222 err = stat (path, &buffer);
223 if (err==0) return 1;
227 s4 java_io_File_canWrite0 (struct java_io_File* this)
230 err = access (javastring_tochar( (java_objectheader*) (this->path)), W_OK);
231 if (err==0) return 1;
235 s4 java_io_File_canRead0 (struct java_io_File* this)
238 err = access (javastring_tochar( (java_objectheader*) (this->path)), R_OK);
239 if (err==0) return 1;
243 s4 java_io_File_isFile0 (struct java_io_File* this)
249 path = javastring_tochar( (java_objectheader*) (this->path));
251 err = stat (path, &buffer);
252 if (err!=0) return 0;
253 if (S_ISREG(buffer.st_mode)) return 1;
257 s4 java_io_File_isDirectory0 (struct java_io_File* this)
263 path = javastring_tochar( (java_objectheader*) (this->path));
265 err = stat (path, &buffer);
266 if (err!=0) return 0;
267 if (S_ISDIR(buffer.st_mode)) return 1;
271 s8 java_io_File_lastModified0 (struct java_io_File* this)
275 err = stat (javastring_tochar( (java_objectheader*) (this->path)), &buffer);
276 if (err!=0) return builtin_i2l(0);
277 return builtin_lmul (builtin_i2l(buffer.st_mtime), builtin_i2l(1000) );
280 s8 java_io_File_length0 (struct java_io_File* this)
284 err = stat (javastring_tochar( (java_objectheader*) (this->path)), &buffer);
285 if (err!=0) return builtin_i2l(0);
286 return builtin_i2l(buffer.st_size);
289 s4 java_io_File_mkdir0 (struct java_io_File* this)
291 char *name = javastring_tochar ( (java_objectheader*) (this->path) );
292 int err = mkdir (name, 0777);
293 if (err==0) return 1;
297 s4 java_io_File_renameTo0 (struct java_io_File* this, struct java_io_File* new)
299 #define MAXPATHLEN 200
300 char newname[MAXPATHLEN];
301 char *n = javastring_tochar ( (java_objectheader*) (new->path) );
304 if (strlen(n)>=MAXPATHLEN) return 0;
306 n = javastring_tochar ( (java_objectheader*) (this->path) );
307 err = rename (n, newname);
308 if (err==0) return 1;
312 s4 java_io_File_delete0 (struct java_io_File* this)
315 err = remove (javastring_tochar ( (java_objectheader*) (this->path) ) );
316 if (err==0) return 1;
320 java_objectarray* java_io_File_list0 (struct java_io_File* this)
327 struct java_lang_String *n;
330 name = javastring_tochar ( (java_objectheader*) (this->path) );
335 while (readdir(d) != NULL) len++;
338 a = builtin_anewarray (len, class_java_lang_String);
344 for (i=0; i<len; i++) {
345 if ( (ent = readdir(d)) != NULL) {
346 namlen = strlen(ent->d_name);
347 memcpy (entbuffer, ent->d_name, namlen);
348 entbuffer[namlen] = '\0';
350 n = (struct java_lang_String*)
351 javastring_new_char (entbuffer);
353 a -> data[i] = (java_objectheader*) n;
362 s4 java_io_File_isAbsolute (struct java_io_File* this)
364 char *name = javastring_tochar ( (java_objectheader*) (this->path) );
365 if (name[0] == '/') return 1;
372 /********************** java.io.RandomAccessFile *****************************/
374 void java_io_RandomAccessFile_open (struct java_io_RandomAccessFile* this,
375 struct java_lang_String* name, s4 writeable)
378 char *fname = javastring_tochar ((java_objectheader*)name);
380 if (writeable) fd = open (fname, O_RDWR, 0);
381 else fd = open (fname, O_RDONLY, 0);
382 if (fd==-1) goto fail;
384 threadedFileDescriptor(fd);
390 exceptionptr = native_new_and_init (class_java_io_IOException);
394 s4 java_io_RandomAccessFile_read (struct java_io_RandomAccessFile* this)
398 r = threadedRead (this->fd->fd, (char *) buffer, 1);
399 if (r>0) return buffer[1];
401 exceptionptr = native_new_and_init (class_java_io_IOException);
405 s4 java_io_RandomAccessFile_readBytes (struct java_io_RandomAccessFile* this,
406 java_bytearray* buffer, s4 start, s4 len)
408 s4 r = threadedRead (this->fd->fd, (char *) buffer->data+start, len);
411 exceptionptr = native_new_and_init (class_java_io_IOException);
415 void java_io_RandomAccessFile_write
416 (struct java_io_RandomAccessFile* this, s4 byte)
421 r = write (this->fd->fd, buffer, 1);
423 exceptionptr = native_new_and_init (class_java_io_IOException);
427 void java_io_RandomAccessFile_writeBytes (struct java_io_RandomAccessFile* this,
428 java_bytearray* buffer, s4 start, s4 len)
433 o = threadedWrite (this->fd->fd, (char *) buffer->data+start, len);
434 if (o!=len) exceptionptr = native_new_and_init (class_java_io_IOException);
437 s8 java_io_RandomAccessFile_getFilePointer
438 (struct java_io_RandomAccessFile* this)
440 s4 p = lseek (this->fd->fd, 0, SEEK_CUR);
441 if (p>=0) return builtin_i2l(p);
442 exceptionptr = native_new_and_init (class_java_io_IOException);
443 return builtin_i2l(0);
446 void java_io_RandomAccessFile_seek
447 (struct java_io_RandomAccessFile* this, s8 offset)
449 s4 p = lseek (this->fd->fd, builtin_l2i(offset), SEEK_SET);
451 exceptionptr = native_new_and_init (class_java_io_IOException);
455 s8 java_io_RandomAccessFile_length (struct java_io_RandomAccessFile* this)
458 s4 r = fstat(this->fd->fd, &buffer);
459 if (r>=0) return builtin_i2l(buffer.st_size);
460 exceptionptr = native_new_and_init (class_java_io_IOException);
461 return builtin_i2l(0);
464 void java_io_RandomAccessFile_close (struct java_io_RandomAccessFile* this)
466 if (this->fd->fd >= 0) {
467 s4 r = close (this->fd->fd);
470 exceptionptr = native_new_and_init (class_java_io_IOException);