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 == 1) /* don't close stderr!!! -- phil. */
205 if (this->fd->fd >= 0) {
206 s4 r = close (this->fd->fd);
209 exceptionptr = native_new_and_init (class_java_io_IOException);
215 /************************** java.io.File **************************************/
217 s4 java_io_File_exists0 (struct java_io_File* this)
223 path = javastring_tochar( (java_objectheader*) (this->path));
225 err = stat (path, &buffer);
226 if (err==0) return 1;
230 s4 java_io_File_canWrite0 (struct java_io_File* this)
233 err = access (javastring_tochar( (java_objectheader*) (this->path)), W_OK);
234 if (err==0) return 1;
238 s4 java_io_File_canRead0 (struct java_io_File* this)
241 err = access (javastring_tochar( (java_objectheader*) (this->path)), R_OK);
242 if (err==0) return 1;
246 s4 java_io_File_isFile0 (struct java_io_File* this)
252 path = javastring_tochar( (java_objectheader*) (this->path));
254 err = stat (path, &buffer);
255 if (err!=0) return 0;
256 if (S_ISREG(buffer.st_mode)) return 1;
260 s4 java_io_File_isDirectory0 (struct java_io_File* this)
266 path = javastring_tochar( (java_objectheader*) (this->path));
268 err = stat (path, &buffer);
269 if (err!=0) return 0;
270 if (S_ISDIR(buffer.st_mode)) return 1;
274 s8 java_io_File_lastModified0 (struct java_io_File* this)
278 err = stat (javastring_tochar( (java_objectheader*) (this->path)), &buffer);
279 if (err!=0) return builtin_i2l(0);
280 return builtin_lmul (builtin_i2l(buffer.st_mtime), builtin_i2l(1000) );
283 s8 java_io_File_length0 (struct java_io_File* this)
287 err = stat (javastring_tochar( (java_objectheader*) (this->path)), &buffer);
288 if (err!=0) return builtin_i2l(0);
289 return builtin_i2l(buffer.st_size);
292 s4 java_io_File_mkdir0 (struct java_io_File* this)
294 char *name = javastring_tochar ( (java_objectheader*) (this->path) );
295 int err = mkdir (name, 0777);
296 if (err==0) return 1;
300 s4 java_io_File_renameTo0 (struct java_io_File* this, struct java_io_File* new)
302 #define MAXPATHLEN 200
303 char newname[MAXPATHLEN];
304 char *n = javastring_tochar ( (java_objectheader*) (new->path) );
307 if (strlen(n)>=MAXPATHLEN) return 0;
309 n = javastring_tochar ( (java_objectheader*) (this->path) );
310 err = rename (n, newname);
311 if (err==0) return 1;
315 s4 java_io_File_delete0 (struct java_io_File* this)
318 err = remove (javastring_tochar ( (java_objectheader*) (this->path) ) );
319 if (err==0) return 1;
323 java_objectarray* java_io_File_list0 (struct java_io_File* this)
330 struct java_lang_String *n;
333 name = javastring_tochar ( (java_objectheader*) (this->path) );
338 while (readdir(d) != NULL) len++;
341 a = builtin_anewarray (len, class_java_lang_String);
347 for (i=0; i<len; i++) {
348 if ( (ent = readdir(d)) != NULL) {
349 namlen = strlen(ent->d_name);
350 memcpy (entbuffer, ent->d_name, namlen);
351 entbuffer[namlen] = '\0';
353 n = (struct java_lang_String*)
354 javastring_new_char (entbuffer);
356 a -> data[i] = (java_objectheader*) n;
365 s4 java_io_File_isAbsolute (struct java_io_File* this)
367 char *name = javastring_tochar ( (java_objectheader*) (this->path) );
368 if (name[0] == '/') return 1;
375 /********************** java.io.RandomAccessFile *****************************/
377 void java_io_RandomAccessFile_open (struct java_io_RandomAccessFile* this,
378 struct java_lang_String* name, s4 writeable)
381 char *fname = javastring_tochar ((java_objectheader*)name);
383 if (writeable) fd = open (fname, O_RDWR, 0);
384 else fd = open (fname, O_RDONLY, 0);
385 if (fd==-1) goto fail;
387 threadedFileDescriptor(fd);
393 exceptionptr = native_new_and_init (class_java_io_IOException);
397 s4 java_io_RandomAccessFile_read (struct java_io_RandomAccessFile* this)
401 r = threadedRead (this->fd->fd, (char *) buffer, 1);
402 if (r>0) return buffer[1];
404 exceptionptr = native_new_and_init (class_java_io_IOException);
408 s4 java_io_RandomAccessFile_readBytes (struct java_io_RandomAccessFile* this,
409 java_bytearray* buffer, s4 start, s4 len)
411 s4 r = threadedRead (this->fd->fd, (char *) buffer->data+start, len);
414 exceptionptr = native_new_and_init (class_java_io_IOException);
418 void java_io_RandomAccessFile_write
419 (struct java_io_RandomAccessFile* this, s4 byte)
424 r = write (this->fd->fd, buffer, 1);
426 exceptionptr = native_new_and_init (class_java_io_IOException);
430 void java_io_RandomAccessFile_writeBytes (struct java_io_RandomAccessFile* this,
431 java_bytearray* buffer, s4 start, s4 len)
436 o = threadedWrite (this->fd->fd, (char *) buffer->data+start, len);
437 if (o!=len) exceptionptr = native_new_and_init (class_java_io_IOException);
440 s8 java_io_RandomAccessFile_getFilePointer
441 (struct java_io_RandomAccessFile* this)
443 s4 p = lseek (this->fd->fd, 0, SEEK_CUR);
444 if (p>=0) return builtin_i2l(p);
445 exceptionptr = native_new_and_init (class_java_io_IOException);
446 return builtin_i2l(0);
449 void java_io_RandomAccessFile_seek
450 (struct java_io_RandomAccessFile* this, s8 offset)
452 s4 p = lseek (this->fd->fd, builtin_l2i(offset), SEEK_SET);
454 exceptionptr = native_new_and_init (class_java_io_IOException);
458 s8 java_io_RandomAccessFile_length (struct java_io_RandomAccessFile* this)
461 s4 r = fstat(this->fd->fd, &buffer);
462 if (r>=0) return builtin_i2l(buffer.st_size);
463 exceptionptr = native_new_and_init (class_java_io_IOException);
464 return builtin_i2l(0);
467 void java_io_RandomAccessFile_close (struct java_io_RandomAccessFile* this)
469 if (this->fd->fd >= 0) {
470 s4 r = close (this->fd->fd);
473 exceptionptr = native_new_and_init (class_java_io_IOException);
481 * These are local overrides for various environment variables in Emacs.
482 * Please do not remove this and leave it at the end of the file, where
483 * Emacs will automagically detect them.
484 * ---------------------------------------------------------------------
487 * indent-tabs-mode: t