* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / io.c
blob30e407f5d8be4ed8e7777361db5099c428e756ca
1 /**********************************************************************
3 io.c -
5 $Author$
6 created at: Fri Oct 15 18:08:59 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/io.h"
16 #include "ruby/signal.h"
17 #include "vm_core.h"
18 #include <ctype.h>
19 #include <errno.h>
21 #define free(x) xfree(x)
23 #if defined(DOSISH) || defined(__CYGWIN__)
24 #include <io.h>
25 #endif
27 #include <sys/types.h>
28 #if defined HAVE_NET_SOCKET_H
29 # include <net/socket.h>
30 #elif defined HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
34 #if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
35 # define NO_SAFE_RENAME
36 #endif
38 #if defined(MSDOS) || defined(__CYGWIN__) || defined(_WIN32)
39 # define NO_LONG_FNAME
40 #endif
42 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(sun) || defined(_nec_ews)
43 # define USE_SETVBUF
44 #endif
46 #ifdef __QNXNTO__
47 #include "unix.h"
48 #endif
50 #include <sys/types.h>
51 #if defined(HAVE_SYS_IOCTL_H) && !defined(DJGPP) && !defined(_WIN32) && !defined(__human68k__)
52 #include <sys/ioctl.h>
53 #endif
54 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
55 #include <fcntl.h>
56 #elif defined(HAVE_SYS_FCNTL_H)
57 #include <sys/fcntl.h>
58 #endif
60 #if !HAVE_OFF_T && !defined(off_t)
61 # define off_t long
62 #endif
64 #include <sys/stat.h>
66 /* EMX has sys/param.h, but.. */
67 #if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
68 # include <sys/param.h>
69 #endif
71 #if !defined NOFILE
72 # define NOFILE 64
73 #endif
75 #ifdef HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif
79 #ifdef HAVE_SYSCALL_H
80 #include <syscall.h>
81 #elif defined HAVE_SYS_SYSCALL_H
82 #include <sys/syscall.h>
83 #endif
85 extern void Init_File(void);
87 #ifdef __BEOS__
88 # ifndef NOFILE
89 # define NOFILE (OPEN_MAX)
90 # endif
91 #endif
93 #include "ruby/util.h"
95 #ifndef O_ACCMODE
96 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
97 #endif
99 #if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
100 # error off_t is bigger than long, but you have no long long...
101 #endif
103 #ifndef PIPE_BUF
104 # ifdef _POSIX_PIPE_BUF
105 # define PIPE_BUF _POSIX_PIPE_BUF
106 # else
107 # define PIPE_BUF 512 /* is this ok? */
108 # endif
109 #endif
111 VALUE rb_cIO;
112 VALUE rb_eEOFError;
113 VALUE rb_eIOError;
115 VALUE rb_stdin, rb_stdout, rb_stderr;
116 VALUE rb_deferr; /* rescue VIM plugin */
117 static VALUE orig_stdout, orig_stderr;
119 VALUE rb_output_fs;
120 VALUE rb_rs;
121 VALUE rb_output_rs;
122 VALUE rb_default_rs;
124 static VALUE argf;
126 static ID id_write, id_read, id_getc, id_flush, id_readpartial;
127 static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
129 struct timeval rb_time_interval(VALUE);
131 struct argf {
132 VALUE filename, current_file;
133 int gets_lineno;
134 int init_p, next_p;
135 VALUE lineno;
136 VALUE argv;
137 char *inplace;
138 int binmode;
139 rb_encoding *enc, *enc2;
142 static int max_file_descriptor = NOFILE;
143 #define UPDATE_MAXFD(fd) \
144 do { \
145 if (max_file_descriptor < (fd)) max_file_descriptor = (fd); \
146 } while (0)
148 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
149 #define ARGF argf_of(argf)
151 #ifdef _STDIO_USES_IOSTREAM /* GNU libc */
152 # ifdef _IO_fpos_t
153 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
154 # else
155 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
156 # endif
157 #elif defined(FILE_COUNT)
158 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
159 #elif defined(FILE_READEND)
160 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
161 #elif defined(__BEOS__)
162 # define STDIO_READ_DATA_PENDING(fp) (fp->_state._eof == 0)
163 #elif defined(__VMS)
164 # define STDIO_READ_DATA_PENDING(fp) (((unsigned int)(*(fp))->_cnt) > 0)
165 #else
166 # define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
167 #endif
169 #if defined(__VMS)
170 #define fopen(file_spec, mode) fopen(file_spec, mode, "rfm=stmlf")
171 #define open(file_spec, flags, mode) open(file_spec, flags, mode, "rfm=stmlf")
172 #endif
174 #define GetWriteIO(io) rb_io_get_write_io(io)
176 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf_len)
177 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf_len)
178 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf+(fptr)->rbuf_off)
179 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
181 #define READ_CHECK(fptr) do {\
182 if (!READ_DATA_PENDING(fptr)) {\
183 rb_thread_wait_fd((fptr)->fd);\
184 rb_io_check_closed(fptr);\
186 } while(0)
188 #ifndef S_ISSOCK
189 # ifdef _S_ISSOCK
190 # define S_ISSOCK(m) _S_ISSOCK(m)
191 # else
192 # ifdef _S_IFSOCK
193 # define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
194 # else
195 # ifdef S_IFSOCK
196 # define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
197 # endif
198 # endif
199 # endif
200 #endif
202 #if !defined HAVE_SHUTDOWN && !defined shutdown
203 #define shutdown(a,b) 0
204 #endif
206 #if defined(_WIN32)
207 #define is_socket(fd, path) rb_w32_is_socket(fd)
208 #elif !defined(S_ISSOCK)
209 #define is_socket(fd, path) 0
210 #else
211 static int
212 is_socket(int fd, const char *path)
214 struct stat sbuf;
215 if (fstat(fd, &sbuf) < 0)
216 rb_sys_fail(path);
217 return S_ISSOCK(sbuf.st_mode);
219 #endif
221 void
222 rb_eof_error(void)
224 rb_raise(rb_eEOFError, "end of file reached");
227 VALUE
228 rb_io_taint_check(VALUE io)
230 if (!OBJ_UNTRUSTED(io) && rb_safe_level() >= 4)
231 rb_raise(rb_eSecurityError, "Insecure: operation on trusted IO");
232 rb_check_frozen(io);
233 return io;
236 void
237 rb_io_check_initialized(rb_io_t *fptr)
239 if (!fptr) {
240 rb_raise(rb_eIOError, "uninitialized stream");
244 void
245 rb_io_check_closed(rb_io_t *fptr)
247 rb_io_check_initialized(fptr);
248 if (fptr->fd < 0) {
249 rb_raise(rb_eIOError, "closed stream");
253 static int io_fflush(rb_io_t *);
255 VALUE
256 rb_io_get_io(VALUE io)
258 return rb_convert_type(io, T_FILE, "IO", "to_io");
261 static VALUE
262 rb_io_check_io(VALUE io)
264 return rb_check_convert_type(io, T_FILE, "IO", "to_io");
267 VALUE
268 rb_io_get_write_io(VALUE io)
270 VALUE write_io;
271 rb_io_check_initialized(RFILE(io)->fptr);
272 write_io = RFILE(io)->fptr->tied_io_for_writing;
273 if (write_io) {
274 return write_io;
276 return io;
280 * call-seq:
281 * IO.try_convert(obj) -> io or nil
283 * Try to convert <i>obj</i> into an IO, using to_io method.
284 * Returns converted IO or nil if <i>obj</i> cannot be converted
285 * for any reason.
287 * IO.try_convert(STDOUT) # => STDOUT
288 * IO.try_convert("STDOUT") # => nil
290 static VALUE
291 rb_io_s_try_convert(VALUE dummy, VALUE io)
293 return rb_io_check_io(io);
296 static void
297 io_unread(rb_io_t *fptr)
299 off_t r;
300 rb_io_check_closed(fptr);
301 if (fptr->rbuf_len == 0 || fptr->mode & FMODE_DUPLEX)
302 return;
303 /* xxx: target position may be negative if buffer is filled by ungetc */
304 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
305 if (!(fptr->mode & FMODE_BINMODE)) {
306 int len = fptr->rbuf_len;
307 while (fptr->rbuf_len-- > 0) {
308 if (fptr->rbuf[fptr->rbuf_len] == '\n')
309 ++len;
311 r = lseek(fptr->fd, -len, SEEK_CUR);
313 else
314 #endif
315 r = lseek(fptr->fd, -fptr->rbuf_len, SEEK_CUR);
316 if (r < 0) {
317 if (errno == ESPIPE)
318 fptr->mode |= FMODE_DUPLEX;
319 return;
321 fptr->rbuf_off = 0;
322 fptr->rbuf_len = 0;
323 return;
326 static rb_encoding *io_input_encoding(rb_io_t *fptr);
328 static void
329 io_ungetbyte(VALUE str, rb_io_t *fptr)
331 int len = RSTRING_LEN(str);
333 if (fptr->rbuf == NULL) {
334 fptr->rbuf_off = 0;
335 fptr->rbuf_len = 0;
336 if (len > 8192)
337 fptr->rbuf_capa = len;
338 else
339 fptr->rbuf_capa = 8192;
340 fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
342 if (fptr->rbuf_capa < len + fptr->rbuf_len) {
343 rb_raise(rb_eIOError, "ungetbyte failed");
345 if (fptr->rbuf_off < len) {
346 MEMMOVE(fptr->rbuf+fptr->rbuf_capa-fptr->rbuf_len,
347 fptr->rbuf+fptr->rbuf_off,
348 char, fptr->rbuf_len);
349 fptr->rbuf_off = fptr->rbuf_capa-fptr->rbuf_len;
351 fptr->rbuf_off-=len;
352 fptr->rbuf_len+=len;
353 MEMMOVE(fptr->rbuf+fptr->rbuf_off, RSTRING_PTR(str), char, len);
356 static rb_io_t *
357 flush_before_seek(rb_io_t *fptr)
359 io_fflush(fptr);
360 io_unread(fptr);
361 errno = 0;
362 return fptr;
365 #define io_seek(fptr, ofs, whence) lseek(flush_before_seek(fptr)->fd, ofs, whence)
366 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
368 #ifndef SEEK_CUR
369 # define SEEK_SET 0
370 # define SEEK_CUR 1
371 # define SEEK_END 2
372 #endif
374 #define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
376 void
377 rb_io_check_readable(rb_io_t *fptr)
379 rb_io_check_closed(fptr);
380 if (!(fptr->mode & FMODE_READABLE)) {
381 rb_raise(rb_eIOError, "not opened for reading");
383 if (fptr->wbuf_len) {
384 io_fflush(fptr);
386 if (fptr->tied_io_for_writing) {
387 rb_io_t *wfptr;
388 GetOpenFile(fptr->tied_io_for_writing, wfptr);
389 io_fflush(wfptr);
393 static rb_encoding*
394 io_read_encoding(rb_io_t *fptr)
396 if (fptr->enc) {
397 return fptr->enc;
399 return rb_default_external_encoding();
402 static rb_encoding*
403 io_input_encoding(rb_io_t *fptr)
405 if (fptr->enc2) {
406 return fptr->enc2;
408 return io_read_encoding(fptr);
411 void
412 rb_io_check_writable(rb_io_t *fptr)
414 rb_io_check_closed(fptr);
415 if (!(fptr->mode & FMODE_WRITABLE)) {
416 rb_raise(rb_eIOError, "not opened for writing");
418 if (fptr->rbuf_len) {
419 io_unread(fptr);
424 rb_read_pending(FILE *fp)
426 return STDIO_READ_DATA_PENDING(fp);
430 rb_io_read_pending(rb_io_t *fptr)
432 return READ_DATA_PENDING(fptr);
435 void
436 rb_read_check(FILE *fp)
438 if (!STDIO_READ_DATA_PENDING(fp)) {
439 rb_thread_wait_fd(fileno(fp));
443 void
444 rb_io_read_check(rb_io_t *fptr)
446 if (!READ_DATA_PENDING(fptr)) {
447 rb_thread_wait_fd(fptr->fd);
449 return;
452 static int
453 ruby_dup(int orig)
455 int fd;
457 fd = dup(orig);
458 if (fd < 0) {
459 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
460 rb_gc();
461 fd = dup(orig);
463 if (fd < 0) {
464 rb_sys_fail(0);
467 return fd;
470 static VALUE
471 io_alloc(VALUE klass)
473 NEWOBJ(io, struct RFile);
474 OBJSETUP(io, klass, T_FILE);
476 io->fptr = 0;
478 return (VALUE)io;
481 #ifndef S_ISREG
482 # define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
483 #endif
485 static int
486 wsplit_p(rb_io_t *fptr)
488 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
489 int r;
490 #endif
492 if (!(fptr->mode & FMODE_WSPLIT_INITIALIZED)) {
493 struct stat buf;
494 if (fstat(fptr->fd, &buf) == 0 &&
495 !S_ISREG(buf.st_mode)
496 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
497 && (r = fcntl(fptr->fd, F_GETFL)) != -1 &&
498 !(r & O_NONBLOCK)
499 #endif
501 fptr->mode |= FMODE_WSPLIT;
503 fptr->mode |= FMODE_WSPLIT_INITIALIZED;
505 return fptr->mode & FMODE_WSPLIT;
508 struct io_internal_struct {
509 int fd;
510 void *buf;
511 size_t capa;
514 static VALUE
515 internal_read_func(void *ptr)
517 struct io_internal_struct *iis = (struct io_internal_struct*)ptr;
518 return read(iis->fd, iis->buf, iis->capa);
521 static VALUE
522 internal_write_func(void *ptr)
524 struct io_internal_struct *iis = (struct io_internal_struct*)ptr;
525 return write(iis->fd, iis->buf, iis->capa);
528 static int
529 rb_read_internal(int fd, void *buf, size_t count)
531 struct io_internal_struct iis;
532 iis.fd = fd;
533 iis.buf = buf;
534 iis.capa = count;
536 return rb_thread_blocking_region(internal_read_func, &iis, RB_UBF_DFL, 0);
539 static int
540 rb_write_internal(int fd, void *buf, size_t count)
542 struct io_internal_struct iis;
543 iis.fd = fd;
544 iis.buf = buf;
545 iis.capa = count;
547 return rb_thread_blocking_region(internal_write_func, &iis, RB_UBF_DFL, 0);
550 static int
551 io_fflush(rb_io_t *fptr)
553 int r, l;
554 int wbuf_off, wbuf_len;
556 rb_io_check_closed(fptr);
557 if (fptr->wbuf_len == 0)
558 return 0;
559 if (!rb_thread_fd_writable(fptr->fd)) {
560 rb_io_check_closed(fptr);
562 retry:
563 if (fptr->wbuf_len == 0)
564 return 0;
565 wbuf_off = fptr->wbuf_off;
566 wbuf_len = fptr->wbuf_len;
567 l = wbuf_len;
568 if (PIPE_BUF < l &&
569 !rb_thread_critical &&
570 !rb_thread_alone() &&
571 wsplit_p(fptr)) {
572 l = PIPE_BUF;
574 r = rb_write_internal(fptr->fd, fptr->wbuf+wbuf_off, l);
575 /* xxx: Other threads may modify wbuf.
576 * A lock is required, definitely. */
577 rb_io_check_closed(fptr);
578 if (fptr->wbuf_len <= r) {
579 fptr->wbuf_off = 0;
580 fptr->wbuf_len = 0;
581 return 0;
583 if (0 <= r) {
584 fptr->wbuf_off += r;
585 fptr->wbuf_len -= r;
586 errno = EAGAIN;
588 if (rb_io_wait_writable(fptr->fd)) {
589 rb_io_check_closed(fptr);
590 goto retry;
592 return -1;
595 #ifdef HAVE_RB_FD_INIT
596 static VALUE
597 wait_readable(VALUE p)
599 rb_fdset_t *rfds = (rb_fdset_t *)p;
601 return rb_thread_select(rb_fd_max(rfds), rb_fd_ptr(rfds), NULL, NULL, NULL);
603 #endif
606 rb_io_wait_readable(int f)
608 rb_fdset_t rfds;
610 if (f < 0) {
611 rb_raise(rb_eIOError, "closed stream");
613 switch (errno) {
614 case EINTR:
615 #if defined(ERESTART)
616 case ERESTART:
617 #endif
618 rb_thread_wait_fd(f);
619 return Qtrue;
621 case EAGAIN:
622 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
623 case EWOULDBLOCK:
624 #endif
625 rb_fd_init(&rfds);
626 rb_fd_set(f, &rfds);
627 #ifdef HAVE_RB_FD_INIT
628 rb_ensure(wait_readable, (VALUE)&rfds,
629 (VALUE (*)(VALUE))rb_fd_term, (VALUE)&rfds);
630 #else
631 rb_thread_select(f + 1, &rfds, NULL, NULL, NULL);
632 #endif
633 return Qtrue;
635 default:
636 return Qfalse;
640 #ifdef HAVE_RB_FD_INIT
641 static VALUE
642 wait_writable(VALUE p)
644 rb_fdset_t *wfds = (rb_fdset_t *)p;
646 return rb_thread_select(rb_fd_max(wfds), NULL, rb_fd_ptr(wfds), NULL, NULL);
648 #endif
651 rb_io_wait_writable(int f)
653 rb_fdset_t wfds;
655 if (f < 0) {
656 rb_raise(rb_eIOError, "closed stream");
658 switch (errno) {
659 case EINTR:
660 #if defined(ERESTART)
661 case ERESTART:
662 #endif
663 rb_thread_fd_writable(f);
664 return Qtrue;
666 case EAGAIN:
667 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
668 case EWOULDBLOCK:
669 #endif
670 rb_fd_init(&wfds);
671 rb_fd_set(f, &wfds);
672 #ifdef HAVE_RB_FD_INIT
673 rb_ensure(wait_writable, (VALUE)&wfds,
674 (VALUE (*)(VALUE))rb_fd_term, (VALUE)&wfds);
675 #else
676 rb_thread_select(f + 1, NULL, &wfds, NULL, NULL);
677 #endif
678 return Qtrue;
680 default:
681 return Qfalse;
685 static void
686 make_writeconv(rb_io_t *fptr)
688 if (!fptr->writeconv_initialized) {
689 const char *senc, *denc;
690 rb_encoding *enc;
692 enc = fptr->enc2 ? fptr->enc2 : fptr->enc;
693 senc = rb_econv_stateless_encoding(enc->name);
694 if (senc) {
695 denc = enc->name;
696 fptr->writeconv_stateless = rb_str_new2(senc);
698 else {
699 denc = NULL;
700 fptr->writeconv_stateless = Qnil;
702 if (senc) {
703 fptr->writeconv = rb_econv_open(senc, denc, 0);
704 if (!fptr->writeconv)
705 rb_raise(rb_eIOError, "code converter open failed (%s to %s)", senc, denc);
707 else {
708 fptr->writeconv = NULL;
710 fptr->writeconv_initialized = 1;
714 /* writing functions */
715 static long
716 io_fwrite(VALUE str, rb_io_t *fptr)
718 long len, n, r, l, offset = 0;
721 * If an external encoding was specified and it differs from
722 * the strings encoding then we must transcode before writing.
724 if (fptr->enc) {
725 make_writeconv(fptr);
726 if (fptr->writeconv) {
727 str = rb_str_transcode(str, fptr->writeconv_stateless);
728 str = rb_econv_string(fptr->writeconv, str, 0, RSTRING_LEN(str), Qnil, ECONV_PARTIAL_INPUT);
730 else {
731 if (fptr->enc2)
732 str = rb_str_transcode(str, rb_enc_from_encoding(fptr->enc2));
733 else
734 str = rb_str_transcode(str, rb_enc_from_encoding(fptr->enc));
738 len = RSTRING_LEN(str);
739 if ((n = len) <= 0) return n;
740 if (fptr->wbuf == NULL && !(fptr->mode & FMODE_SYNC)) {
741 fptr->wbuf_off = 0;
742 fptr->wbuf_len = 0;
743 fptr->wbuf_capa = 8192;
744 fptr->wbuf = ALLOC_N(char, fptr->wbuf_capa);
746 if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) ||
747 (fptr->wbuf && fptr->wbuf_capa <= fptr->wbuf_len + len)) {
748 /* xxx: use writev to avoid double write if available */
749 if (fptr->wbuf_len && fptr->wbuf_len+len <= fptr->wbuf_capa) {
750 if (fptr->wbuf_capa < fptr->wbuf_off+fptr->wbuf_len+len) {
751 MEMMOVE(fptr->wbuf, fptr->wbuf+fptr->wbuf_off, char, fptr->wbuf_len);
752 fptr->wbuf_off = 0;
754 MEMMOVE(fptr->wbuf+fptr->wbuf_off+fptr->wbuf_len, RSTRING_PTR(str)+offset, char, len);
755 fptr->wbuf_len += len;
756 n = 0;
758 if (io_fflush(fptr) < 0)
759 return -1L;
760 if (n == 0)
761 return len;
762 /* avoid context switch between "a" and "\n" in STDERR.puts "a".
763 [ruby-dev:25080] */
764 if (fptr->stdio_file != stderr && !rb_thread_fd_writable(fptr->fd)) {
765 rb_io_check_closed(fptr);
767 retry:
768 l = n;
769 if (PIPE_BUF < l &&
770 !rb_thread_critical &&
771 !rb_thread_alone() &&
772 wsplit_p(fptr)) {
773 l = PIPE_BUF;
775 r = rb_write_internal(fptr->fd, RSTRING_PTR(str)+offset, l);
776 /* xxx: other threads may modify given string. */
777 if (r == n) return len;
778 if (0 <= r) {
779 offset += r;
780 n -= r;
781 errno = EAGAIN;
783 if (rb_io_wait_writable(fptr->fd)) {
784 rb_io_check_closed(fptr);
785 if (offset < RSTRING_LEN(str))
786 goto retry;
788 return -1L;
791 if (fptr->wbuf_off) {
792 if (fptr->wbuf_len)
793 MEMMOVE(fptr->wbuf, fptr->wbuf+fptr->wbuf_off, char, fptr->wbuf_len);
794 fptr->wbuf_off = 0;
796 MEMMOVE(fptr->wbuf+fptr->wbuf_off+fptr->wbuf_len, RSTRING_PTR(str)+offset, char, len);
797 fptr->wbuf_len += len;
798 return len;
801 long
802 rb_io_fwrite(const char *ptr, long len, FILE *f)
804 rb_io_t of;
806 of.fd = fileno(f);
807 of.stdio_file = f;
808 of.mode = FMODE_WRITABLE;
809 of.path = NULL;
810 return io_fwrite(rb_str_new(ptr, len), &of);
814 * call-seq:
815 * ios.write(string) => integer
817 * Writes the given string to <em>ios</em>. The stream must be opened
818 * for writing. If the argument is not a string, it will be converted
819 * to a string using <code>to_s</code>. Returns the number of bytes
820 * written.
822 * count = $stdout.write( "This is a test\n" )
823 * puts "That was #{count} bytes of data"
825 * <em>produces:</em>
827 * This is a test
828 * That was 15 bytes of data
831 static VALUE
832 io_write(VALUE io, VALUE str)
834 rb_io_t *fptr;
835 long n;
836 VALUE tmp;
838 rb_secure(4);
839 io = GetWriteIO(io);
840 str = rb_obj_as_string(str);
841 tmp = rb_io_check_io(io);
842 if (NIL_P(tmp)) {
843 /* port is not IO, call write method for it. */
844 return rb_funcall(io, id_write, 1, str);
846 io = tmp;
847 if (RSTRING_LEN(str) == 0) return INT2FIX(0);
849 GetOpenFile(io, fptr);
850 rb_io_check_writable(fptr);
852 n = io_fwrite(str, fptr);
853 if (n == -1L) rb_sys_fail(fptr->path);
855 return LONG2FIX(n);
858 VALUE
859 rb_io_write(VALUE io, VALUE str)
861 return rb_funcall(io, id_write, 1, str);
865 * call-seq:
866 * ios << obj => ios
868 * String Output---Writes <i>obj</i> to <em>ios</em>.
869 * <i>obj</i> will be converted to a string using
870 * <code>to_s</code>.
872 * $stdout << "Hello " << "world!\n"
874 * <em>produces:</em>
876 * Hello world!
880 VALUE
881 rb_io_addstr(VALUE io, VALUE str)
883 rb_io_write(io, str);
884 return io;
888 * call-seq:
889 * ios.flush => ios
891 * Flushes any buffered data within <em>ios</em> to the underlying
892 * operating system (note that this is Ruby internal buffering only;
893 * the OS may buffer the data as well).
895 * $stdout.print "no newline"
896 * $stdout.flush
898 * <em>produces:</em>
900 * no newline
903 VALUE
904 rb_io_flush(VALUE io)
906 rb_io_t *fptr;
908 if (TYPE(io) != T_FILE) {
909 return rb_funcall(io, id_flush, 0);
912 io = GetWriteIO(io);
913 GetOpenFile(io, fptr);
915 if (fptr->mode & FMODE_WRITABLE) {
916 io_fflush(fptr);
918 if (fptr->mode & FMODE_READABLE) {
919 io_unread(fptr);
922 return io;
926 * call-seq:
927 * ios.pos => integer
928 * ios.tell => integer
930 * Returns the current offset (in bytes) of <em>ios</em>.
932 * f = File.new("testfile")
933 * f.pos #=> 0
934 * f.gets #=> "This is line one\n"
935 * f.pos #=> 17
938 static VALUE
939 rb_io_tell(VALUE io)
941 rb_io_t *fptr;
942 off_t pos;
944 GetOpenFile(io, fptr);
945 pos = io_tell(fptr);
946 if (pos < 0 && errno) rb_sys_fail(fptr->path);
947 return OFFT2NUM(pos);
950 static VALUE
951 rb_io_seek(VALUE io, VALUE offset, int whence)
953 rb_io_t *fptr;
954 off_t pos;
956 pos = NUM2OFFT(offset);
957 GetOpenFile(io, fptr);
958 pos = io_seek(fptr, pos, whence);
959 if (pos < 0 && errno) rb_sys_fail(fptr->path);
961 return INT2FIX(0);
965 * call-seq:
966 * ios.seek(amount, whence=SEEK_SET) -> 0
968 * Seeks to a given offset <i>anInteger</i> in the stream according to
969 * the value of <i>whence</i>:
971 * IO::SEEK_CUR | Seeks to _amount_ plus current position
972 * --------------+----------------------------------------------------
973 * IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
974 * | want a negative value for _amount_)
975 * --------------+----------------------------------------------------
976 * IO::SEEK_SET | Seeks to the absolute location given by _amount_
978 * Example:
980 * f = File.new("testfile")
981 * f.seek(-13, IO::SEEK_END) #=> 0
982 * f.readline #=> "And so on...\n"
985 static VALUE
986 rb_io_seek_m(int argc, VALUE *argv, VALUE io)
988 VALUE offset, ptrname;
989 int whence = SEEK_SET;
991 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
992 whence = NUM2INT(ptrname);
995 return rb_io_seek(io, offset, whence);
999 * call-seq:
1000 * ios.pos = integer => integer
1002 * Seeks to the given position (in bytes) in <em>ios</em>.
1004 * f = File.new("testfile")
1005 * f.pos = 17
1006 * f.gets #=> "This is line two\n"
1009 static VALUE
1010 rb_io_set_pos(VALUE io, VALUE offset)
1012 rb_io_t *fptr;
1013 off_t pos;
1015 pos = NUM2OFFT(offset);
1016 GetOpenFile(io, fptr);
1017 pos = io_seek(fptr, pos, SEEK_SET);
1018 if (pos < 0) rb_sys_fail(fptr->path);
1020 return OFFT2NUM(pos);
1024 * call-seq:
1025 * ios.rewind => 0
1027 * Positions <em>ios</em> to the beginning of input, resetting
1028 * <code>lineno</code> to zero.
1030 * f = File.new("testfile")
1031 * f.readline #=> "This is line one\n"
1032 * f.rewind #=> 0
1033 * f.lineno #=> 0
1034 * f.readline #=> "This is line one\n"
1037 static VALUE
1038 rb_io_rewind(VALUE io)
1040 rb_io_t *fptr;
1042 GetOpenFile(io, fptr);
1043 if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path);
1044 if (io == ARGF.current_file) {
1045 ARGF.gets_lineno -= fptr->lineno;
1047 fptr->lineno = 0;
1049 return INT2FIX(0);
1052 static int
1053 io_fillbuf(rb_io_t *fptr)
1055 int r;
1057 if (fptr->rbuf == NULL) {
1058 fptr->rbuf_off = 0;
1059 fptr->rbuf_len = 0;
1060 fptr->rbuf_capa = 8192;
1061 fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
1063 if (fptr->rbuf_len == 0) {
1064 retry:
1066 r = rb_read_internal(fptr->fd, fptr->rbuf, fptr->rbuf_capa);
1068 if (r < 0) {
1069 if (rb_io_wait_readable(fptr->fd))
1070 goto retry;
1071 rb_sys_fail(fptr->path);
1073 fptr->rbuf_off = 0;
1074 fptr->rbuf_len = r;
1075 if (r == 0)
1076 return -1; /* EOF */
1078 return 0;
1082 * call-seq:
1083 * ios.eof => true or false
1084 * ios.eof? => true or false
1086 * Returns true if <em>ios</em> is at end of file that means
1087 * there are no more data to read.
1088 * The stream must be opened for reading or an <code>IOError</code> will be
1089 * raised.
1091 * f = File.new("testfile")
1092 * dummy = f.readlines
1093 * f.eof #=> true
1095 * If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
1096 * blocks until the other end sends some data or closes it.
1098 * r, w = IO.pipe
1099 * Thread.new { sleep 1; w.close }
1100 * r.eof? #=> true after 1 second blocking
1102 * r, w = IO.pipe
1103 * Thread.new { sleep 1; w.puts "a" }
1104 * r.eof? #=> false after 1 second blocking
1106 * r, w = IO.pipe
1107 * r.eof? # blocks forever
1109 * Note that <code>IO#eof?</code> reads data to a input buffer.
1110 * So <code>IO#sysread</code> doesn't work with <code>IO#eof?</code>.
1113 VALUE
1114 rb_io_eof(VALUE io)
1116 rb_io_t *fptr;
1118 GetOpenFile(io, fptr);
1119 rb_io_check_readable(fptr);
1121 if (READ_DATA_PENDING(fptr)) return Qfalse;
1122 READ_CHECK(fptr);
1123 if (io_fillbuf(fptr) < 0) {
1124 return Qtrue;
1126 return Qfalse;
1130 * call-seq:
1131 * ios.sync => true or false
1133 * Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
1134 * true, all output is immediately flushed to the underlying operating
1135 * system and is not buffered by Ruby internally. See also
1136 * <code>IO#fsync</code>.
1138 * f = File.new("testfile")
1139 * f.sync #=> false
1142 static VALUE
1143 rb_io_sync(VALUE io)
1145 rb_io_t *fptr;
1147 io = GetWriteIO(io);
1148 GetOpenFile(io, fptr);
1149 return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
1153 * call-seq:
1154 * ios.sync = boolean => boolean
1156 * Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
1157 * When sync mode is true, all output is immediately flushed to the
1158 * underlying operating system and is not buffered internally. Returns
1159 * the new state. See also <code>IO#fsync</code>.
1161 * f = File.new("testfile")
1162 * f.sync = true
1164 * <em>(produces no output)</em>
1167 static VALUE
1168 rb_io_set_sync(VALUE io, VALUE mode)
1170 rb_io_t *fptr;
1172 io = GetWriteIO(io);
1173 GetOpenFile(io, fptr);
1174 if (RTEST(mode)) {
1175 fptr->mode |= FMODE_SYNC;
1177 else {
1178 fptr->mode &= ~FMODE_SYNC;
1180 return mode;
1184 * call-seq:
1185 * ios.fsync => 0 or nil
1187 * Immediately writes all buffered data in <em>ios</em> to disk.
1188 * Returns <code>nil</code> if the underlying operating system does not
1189 * support <em>fsync(2)</em>. Note that <code>fsync</code> differs from
1190 * using <code>IO#sync=</code>. The latter ensures that data is flushed
1191 * from Ruby's buffers, but doesn't not guarantee that the underlying
1192 * operating system actually writes it to disk.
1195 static VALUE
1196 rb_io_fsync(VALUE io)
1198 #ifdef HAVE_FSYNC
1199 rb_io_t *fptr;
1201 io = GetWriteIO(io);
1202 GetOpenFile(io, fptr);
1204 io_fflush(fptr);
1205 if (fsync(fptr->fd) < 0)
1206 rb_sys_fail(fptr->path);
1207 return INT2FIX(0);
1208 #else
1209 rb_notimplement();
1210 return Qnil; /* not reached */
1211 #endif
1215 * call-seq:
1216 * ios.fileno => fixnum
1217 * ios.to_i => fixnum
1219 * Returns an integer representing the numeric file descriptor for
1220 * <em>ios</em>.
1222 * $stdin.fileno #=> 0
1223 * $stdout.fileno #=> 1
1226 static VALUE
1227 rb_io_fileno(VALUE io)
1229 rb_io_t *fptr;
1230 int fd;
1232 GetOpenFile(io, fptr);
1233 fd = fptr->fd;
1234 return INT2FIX(fd);
1239 * call-seq:
1240 * ios.pid => fixnum
1242 * Returns the process ID of a child process associated with
1243 * <em>ios</em>. This will be set by <code>IO::popen</code>.
1245 * pipe = IO.popen("-")
1246 * if pipe
1247 * $stderr.puts "In parent, child pid is #{pipe.pid}"
1248 * else
1249 * $stderr.puts "In child, pid is #{$$}"
1250 * end
1252 * <em>produces:</em>
1254 * In child, pid is 26209
1255 * In parent, child pid is 26209
1258 static VALUE
1259 rb_io_pid(VALUE io)
1261 rb_io_t *fptr;
1263 GetOpenFile(io, fptr);
1264 if (!fptr->pid)
1265 return Qnil;
1266 return INT2FIX(fptr->pid);
1271 * call-seq:
1272 * ios.inspect => string
1274 * Return a string describing this IO object.
1277 static VALUE
1278 rb_io_inspect(VALUE obj)
1280 rb_io_t *fptr;
1281 const char *cname;
1282 const char *st = "";
1284 fptr = RFILE(rb_io_taint_check(obj))->fptr;
1285 if (!fptr || !fptr->path) return rb_any_to_s(obj);
1286 cname = rb_obj_classname(obj);
1287 if (fptr->fd < 0) {
1288 st = " (closed)";
1290 return rb_sprintf("#<%s:%s%s>", cname, fptr->path, st);
1294 * call-seq:
1295 * ios.to_io -> ios
1297 * Returns <em>ios</em>.
1300 static VALUE
1301 rb_io_to_io(VALUE io)
1303 return io;
1306 /* reading functions */
1307 static long
1308 read_buffered_data(char *ptr, long len, rb_io_t *fptr)
1310 long n;
1312 n = READ_DATA_PENDING_COUNT(fptr);
1313 if (n <= 0) return 0;
1314 if (n > len) n = len;
1315 MEMMOVE(ptr, fptr->rbuf+fptr->rbuf_off, char, n);
1316 fptr->rbuf_off += n;
1317 fptr->rbuf_len -= n;
1318 return n;
1321 static long
1322 io_fread(VALUE str, long offset, rb_io_t *fptr)
1324 long len = RSTRING_LEN(str) - offset;
1325 long n = len;
1326 int c;
1328 if (READ_DATA_PENDING(fptr) == 0) {
1329 while (n > 0) {
1330 c = rb_read_internal(fptr->fd, RSTRING_PTR(str)+offset, n);
1331 if (c == 0) break;
1332 if (c < 0) {
1333 rb_sys_fail(fptr->path);
1335 offset += c;
1336 if ((n -= c) <= 0) break;
1337 rb_thread_wait_fd(fptr->fd);
1339 return len - n;
1342 while (n > 0) {
1343 c = read_buffered_data(RSTRING_PTR(str)+offset, n, fptr);
1344 if (c > 0) {
1345 offset += c;
1346 if ((n -= c) <= 0) break;
1348 rb_thread_wait_fd(fptr->fd);
1349 rb_io_check_closed(fptr);
1350 if (io_fillbuf(fptr) < 0) {
1351 break;
1354 return len - n;
1357 long
1358 rb_io_fread(char *ptr, long len, FILE *f)
1360 rb_io_t of;
1361 VALUE str;
1362 long n;
1364 of.fd = fileno(f);
1365 of.stdio_file = f;
1366 of.mode = FMODE_READABLE;
1367 str = rb_str_new(ptr, len);
1368 n = io_fread(str, 0, &of);
1369 MEMCPY(ptr, RSTRING_PTR(str), char, n);
1370 return n;
1373 #define SMALLBUF 100
1375 static long
1376 remain_size(rb_io_t *fptr)
1378 struct stat st;
1379 off_t siz = READ_DATA_PENDING_COUNT(fptr);
1380 off_t pos;
1382 if (fstat(fptr->fd, &st) == 0 && S_ISREG(st.st_mode)
1383 #ifdef __BEOS__
1384 && (st.st_dev > 3)
1385 #endif
1388 io_fflush(fptr);
1389 pos = lseek(fptr->fd, 0, SEEK_CUR);
1390 if (st.st_size >= pos && pos >= 0) {
1391 siz += st.st_size - pos;
1392 if (siz > LONG_MAX) {
1393 rb_raise(rb_eIOError, "file too big for single read");
1397 else {
1398 siz += BUFSIZ;
1400 return (long)siz;
1403 static VALUE
1404 io_enc_str(VALUE str, rb_io_t *fptr)
1406 OBJ_TAINT(str);
1407 rb_enc_associate(str, io_read_encoding(fptr));
1408 return str;
1411 static void
1412 make_readconv(rb_io_t *fptr)
1414 if (!fptr->readconv) {
1415 fptr->readconv = rb_econv_open(fptr->enc2->name, fptr->enc->name, 0);
1416 if (!fptr->readconv)
1417 rb_raise(rb_eIOError, "code converter open failed (%s to %s)", fptr->enc2->name, fptr->enc->name);
1418 fptr->crbuf_off = 0;
1419 fptr->crbuf_len = 0;
1420 fptr->crbuf_capa = 1024;
1421 fptr->crbuf = ALLOC_N(char, fptr->crbuf_capa);
1425 static int
1426 more_char(rb_io_t *fptr)
1428 const unsigned char *ss, *sp, *se;
1429 unsigned char *ds, *dp, *de;
1430 rb_econv_result_t res;
1431 int putbackable;
1432 int crbuf_len0;
1434 if (fptr->crbuf_len == fptr->crbuf_capa)
1435 return 0; /* crbuf full */
1436 if (fptr->crbuf_len == 0)
1437 fptr->crbuf_off = 0;
1438 else if (fptr->crbuf_off + fptr->crbuf_len == fptr->crbuf_capa) {
1439 memmove(fptr->crbuf, fptr->crbuf+fptr->crbuf_off, fptr->crbuf_len);
1440 fptr->crbuf_off = 0;
1443 crbuf_len0 = fptr->crbuf_len;
1445 while (1) {
1446 ss = sp = (const unsigned char *)fptr->rbuf + fptr->rbuf_off;
1447 se = sp + fptr->rbuf_len;
1448 ds = dp = (unsigned char *)fptr->crbuf + fptr->crbuf_off + fptr->crbuf_len;
1449 de = (unsigned char *)fptr->crbuf + fptr->crbuf_capa;
1450 res = rb_econv_convert(fptr->readconv, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_OUTPUT_FOLLOWED_BY_INPUT);
1451 fptr->rbuf_off += sp - ss;
1452 fptr->rbuf_len -= sp - ss;
1453 fptr->crbuf_len += dp - ds;
1455 putbackable = rb_econv_putbackable(fptr->readconv);
1456 if (putbackable) {
1457 rb_econv_putback(fptr->readconv, (unsigned char *)fptr->rbuf + fptr->rbuf_off - putbackable, putbackable);
1458 fptr->rbuf_off -= putbackable;
1459 fptr->rbuf_len += putbackable;
1462 rb_econv_check_error(fptr->readconv);
1464 if (crbuf_len0 != fptr->crbuf_len)
1465 return 0;
1467 if (res == econv_finished)
1468 return -1;
1470 if (res == econv_source_buffer_empty) {
1471 if (fptr->rbuf_len == 0) {
1472 rb_thread_wait_fd(fptr->fd);
1473 rb_io_check_closed(fptr);
1474 if (io_fillbuf(fptr) == -1) {
1475 ds = dp = (unsigned char *)fptr->crbuf + fptr->crbuf_off + fptr->crbuf_len;
1476 de = (unsigned char *)fptr->crbuf + fptr->crbuf_capa;
1477 res = rb_econv_convert(fptr->readconv, NULL, NULL, &dp, de, 0);
1478 fptr->crbuf_len += dp - ds;
1479 rb_econv_check_error(fptr->readconv);
1486 static VALUE
1487 io_shift_crbuf(rb_io_t *fptr, int len, VALUE *strp)
1489 VALUE str;
1490 if (NIL_P(*strp)) {
1491 *strp = str = rb_str_new(fptr->crbuf+fptr->crbuf_off, len);
1493 else {
1494 size_t slen;
1495 str = *strp;
1496 slen = RSTRING_LEN(str);
1497 rb_str_resize(str, RSTRING_LEN(str) + len);
1498 memcpy(RSTRING_PTR(str)+slen, fptr->crbuf+fptr->crbuf_off, len);
1500 fptr->crbuf_off += len;
1501 fptr->crbuf_len -= len;
1502 OBJ_TAINT(str);
1503 rb_enc_associate(str, fptr->enc);
1504 /* xxx: set coderange */
1505 if (fptr->crbuf_len == 0)
1506 fptr->crbuf_off = 0;
1507 if (fptr->crbuf_off < fptr->crbuf_capa/2) {
1508 memmove(fptr->crbuf, fptr->crbuf+fptr->crbuf_off, fptr->crbuf_len);
1509 fptr->crbuf_off = 0;
1511 return str;
1514 static VALUE
1515 read_all(rb_io_t *fptr, long siz, VALUE str)
1517 long bytes;
1518 long n;
1519 long pos;
1520 rb_encoding *enc;
1521 int cr;
1523 if (fptr->enc2) {
1524 VALUE str = rb_str_new(NULL, 0);
1525 make_readconv(fptr);
1526 while (1) {
1527 if (fptr->crbuf_len) {
1528 io_shift_crbuf(fptr, fptr->crbuf_len, &str);
1530 if (more_char(fptr) == -1) {
1531 return io_enc_str(str, fptr);
1536 bytes = 0;
1537 pos = 0;
1539 enc = io_read_encoding(fptr);
1540 cr = fptr->enc2 ? ENC_CODERANGE_BROKEN : 0;
1542 if (siz == 0) siz = BUFSIZ;
1543 if (NIL_P(str)) {
1544 str = rb_str_new(0, siz);
1546 else {
1547 rb_str_resize(str, siz);
1549 for (;;) {
1550 READ_CHECK(fptr);
1551 n = io_fread(str, bytes, fptr);
1552 if (n == 0 && bytes == 0) {
1553 break;
1555 bytes += n;
1556 if (cr != ENC_CODERANGE_BROKEN)
1557 pos = rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
1558 if (bytes < siz) break;
1559 siz += BUFSIZ;
1560 rb_str_resize(str, siz);
1562 if (bytes != siz) rb_str_resize(str, bytes);
1563 str = io_enc_str(str, fptr);
1564 if (!fptr->enc2) {
1565 ENC_CODERANGE_SET(str, cr);
1567 return str;
1570 void
1571 rb_io_set_nonblock(rb_io_t *fptr)
1573 int flags;
1574 #ifdef F_GETFL
1575 flags = fcntl(fptr->fd, F_GETFL);
1576 if (flags == -1) {
1577 rb_sys_fail(fptr->path);
1579 #else
1580 flags = 0;
1581 #endif
1582 if ((flags & O_NONBLOCK) == 0) {
1583 flags |= O_NONBLOCK;
1584 if (fcntl(fptr->fd, F_SETFL, flags) == -1) {
1585 rb_sys_fail(fptr->path);
1590 static VALUE
1591 io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
1593 rb_io_t *fptr;
1594 VALUE length, str;
1595 long n, len;
1597 rb_scan_args(argc, argv, "11", &length, &str);
1599 if ((len = NUM2LONG(length)) < 0) {
1600 rb_raise(rb_eArgError, "negative length %ld given", len);
1603 if (NIL_P(str)) {
1604 str = rb_str_new(0, len);
1606 else {
1607 StringValue(str);
1608 rb_str_modify(str);
1609 rb_str_resize(str, len);
1611 OBJ_TAINT(str);
1613 GetOpenFile(io, fptr);
1614 rb_io_check_readable(fptr);
1616 if (len == 0)
1617 return str;
1619 if (!nonblock)
1620 READ_CHECK(fptr);
1621 if (RSTRING_LEN(str) != len) {
1622 modified:
1623 rb_raise(rb_eRuntimeError, "buffer string modified");
1625 n = read_buffered_data(RSTRING_PTR(str), len, fptr);
1626 if (n <= 0) {
1627 again:
1628 if (RSTRING_LEN(str) != len) goto modified;
1629 if (nonblock) {
1630 rb_io_set_nonblock(fptr);
1632 n = rb_read_internal(fptr->fd, RSTRING_PTR(str), len);
1633 if (n < 0) {
1634 if (!nonblock && rb_io_wait_readable(fptr->fd))
1635 goto again;
1636 rb_sys_fail(fptr->path);
1639 rb_str_resize(str, n);
1641 if (n == 0)
1642 return Qnil;
1643 else
1644 return str;
1648 * call-seq:
1649 * ios.readpartial(maxlen) => string
1650 * ios.readpartial(maxlen, outbuf) => outbuf
1652 * Reads at most <i>maxlen</i> bytes from the I/O stream.
1653 * It blocks only if <em>ios</em> has no data immediately available.
1654 * It doesn't block if some data available.
1655 * If the optional <i>outbuf</i> argument is present,
1656 * it must reference a String, which will receive the data.
1657 * It raises <code>EOFError</code> on end of file.
1659 * readpartial is designed for streams such as pipe, socket, tty, etc.
1660 * It blocks only when no data immediately available.
1661 * This means that it blocks only when following all conditions hold.
1662 * * the buffer in the IO object is empty.
1663 * * the content of the stream is empty.
1664 * * the stream is not reached to EOF.
1666 * When readpartial blocks, it waits data or EOF on the stream.
1667 * If some data is reached, readpartial returns with the data.
1668 * If EOF is reached, readpartial raises EOFError.
1670 * When readpartial doesn't blocks, it returns or raises immediately.
1671 * If the buffer is not empty, it returns the data in the buffer.
1672 * Otherwise if the stream has some content,
1673 * it returns the data in the stream.
1674 * Otherwise if the stream is reached to EOF, it raises EOFError.
1676 * r, w = IO.pipe # buffer pipe content
1677 * w << "abc" # "" "abc".
1678 * r.readpartial(4096) #=> "abc" "" ""
1679 * r.readpartial(4096) # blocks because buffer and pipe is empty.
1681 * r, w = IO.pipe # buffer pipe content
1682 * w << "abc" # "" "abc"
1683 * w.close # "" "abc" EOF
1684 * r.readpartial(4096) #=> "abc" "" EOF
1685 * r.readpartial(4096) # raises EOFError
1687 * r, w = IO.pipe # buffer pipe content
1688 * w << "abc\ndef\n" # "" "abc\ndef\n"
1689 * r.gets #=> "abc\n" "def\n" ""
1690 * w << "ghi\n" # "def\n" "ghi\n"
1691 * r.readpartial(4096) #=> "def\n" "" "ghi\n"
1692 * r.readpartial(4096) #=> "ghi\n" "" ""
1694 * Note that readpartial behaves similar to sysread.
1695 * The differences are:
1696 * * If the buffer is not empty, read from the buffer instead of "sysread for buffered IO (IOError)".
1697 * * It doesn't cause Errno::EAGAIN and Errno::EINTR. When readpartial meets EAGAIN and EINTR by read system call, readpartial retry the system call.
1699 * The later means that readpartial is nonblocking-flag insensitive.
1700 * It blocks on the situation IO#sysread causes Errno::EAGAIN as if the fd is blocking mode.
1704 static VALUE
1705 io_readpartial(int argc, VALUE *argv, VALUE io)
1707 VALUE ret;
1709 ret = io_getpartial(argc, argv, io, 0);
1710 if (NIL_P(ret))
1711 rb_eof_error();
1712 else
1713 return ret;
1717 * call-seq:
1718 * ios.read_nonblock(maxlen) => string
1719 * ios.read_nonblock(maxlen, outbuf) => outbuf
1721 * Reads at most <i>maxlen</i> bytes from <em>ios</em> using
1722 * read(2) system call after O_NONBLOCK is set for
1723 * the underlying file descriptor.
1725 * If the optional <i>outbuf</i> argument is present,
1726 * it must reference a String, which will receive the data.
1728 * read_nonblock just calls read(2).
1729 * It causes all errors read(2) causes: EAGAIN, EINTR, etc.
1730 * The caller should care such errors.
1732 * read_nonblock causes EOFError on EOF.
1734 * If the read buffer is not empty,
1735 * read_nonblock reads from the buffer like readpartial.
1736 * In this case, read(2) is not called.
1740 static VALUE
1741 io_read_nonblock(int argc, VALUE *argv, VALUE io)
1743 VALUE ret;
1745 ret = io_getpartial(argc, argv, io, 1);
1746 if (NIL_P(ret))
1747 rb_eof_error();
1748 else
1749 return ret;
1753 * call-seq:
1754 * ios.write_nonblock(string) => integer
1756 * Writes the given string to <em>ios</em> using
1757 * write(2) system call after O_NONBLOCK is set for
1758 * the underlying file descriptor.
1760 * write_nonblock just calls write(2).
1761 * It causes all errors write(2) causes: EAGAIN, EINTR, etc.
1762 * The result may also be smaller than string.length (partial write).
1763 * The caller should care such errors and partial write.
1765 * If the write buffer is not empty, it is flushed at first.
1769 static VALUE
1770 rb_io_write_nonblock(VALUE io, VALUE str)
1772 rb_io_t *fptr;
1773 long n;
1775 rb_secure(4);
1776 if (TYPE(str) != T_STRING)
1777 str = rb_obj_as_string(str);
1779 io = GetWriteIO(io);
1780 GetOpenFile(io, fptr);
1781 rb_io_check_writable(fptr);
1783 io_fflush(fptr);
1785 rb_io_set_nonblock(fptr);
1786 n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
1788 if (n == -1) rb_sys_fail(fptr->path);
1790 return LONG2FIX(n);
1794 * call-seq:
1795 * ios.read([length [, buffer]]) => string, buffer, or nil
1797 * Reads at most <i>length</i> bytes from the I/O stream, or to the
1798 * end of file if <i>length</i> is omitted or is <code>nil</code>.
1799 * <i>length</i> must be a non-negative integer or nil.
1800 * If the optional <i>buffer</i> argument is present, it must reference
1801 * a String, which will receive the data.
1803 * At end of file, it returns <code>nil</code> or <code>""</code>
1804 * depend on <i>length</i>.
1805 * <code><i>ios</i>.read()</code> and
1806 * <code><i>ios</i>.read(nil)</code> returns <code>""</code>.
1807 * <code><i>ios</i>.read(<i>positive-integer</i>)</code> returns nil.
1809 * <code><i>ios</i>.read(0)</code> returns <code>""</code>.
1811 * f = File.new("testfile")
1812 * f.read(16) #=> "This is line one"
1815 static VALUE
1816 io_read(int argc, VALUE *argv, VALUE io)
1818 rb_io_t *fptr;
1819 long n, len;
1820 VALUE length, str;
1822 rb_scan_args(argc, argv, "02", &length, &str);
1824 if (NIL_P(length)) {
1825 if (!NIL_P(str)) StringValue(str);
1826 GetOpenFile(io, fptr);
1827 rb_io_check_readable(fptr);
1828 return read_all(fptr, remain_size(fptr), str);
1830 len = NUM2LONG(length);
1831 if (len < 0) {
1832 rb_raise(rb_eArgError, "negative length %ld given", len);
1835 if (NIL_P(str)) {
1836 str = rb_str_new(0, len);
1838 else {
1839 StringValue(str);
1840 rb_str_modify(str);
1841 rb_str_resize(str,len);
1844 GetOpenFile(io, fptr);
1845 rb_io_check_readable(fptr);
1846 if (len == 0) return str;
1848 READ_CHECK(fptr);
1849 if (RSTRING_LEN(str) != len) {
1850 rb_raise(rb_eRuntimeError, "buffer string modified");
1852 n = io_fread(str, 0, fptr);
1853 if (n == 0) {
1854 if (fptr->fd < 0) return Qnil;
1855 rb_str_resize(str, 0);
1856 return Qnil;
1858 rb_str_resize(str, n);
1860 return str;
1863 static void
1864 rscheck(const char *rsptr, long rslen, VALUE rs)
1866 if (!rs) return;
1867 if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
1868 rb_raise(rb_eRuntimeError, "rs modified");
1871 static int
1872 appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
1874 VALUE str = *strp;
1875 long limit = *lp;
1877 if (fptr->enc2) {
1878 make_readconv(fptr);
1879 while (1) {
1880 const char *p, *e;
1881 int searchlen;
1882 if (fptr->crbuf_len) {
1883 p = fptr->crbuf+fptr->crbuf_off;
1884 searchlen = fptr->crbuf_len;
1885 if (0 < limit && limit < searchlen)
1886 searchlen = limit;
1887 e = memchr(p, delim, searchlen);
1888 if (e) {
1889 if (NIL_P(str))
1890 *strp = str = rb_str_new(p, e-p+1);
1891 else
1892 rb_str_buf_cat(str, p, e-p+1);
1893 fptr->crbuf_off += e-p+1;
1894 fptr->crbuf_len -= e-p+1;
1895 limit -= e-p+1;
1896 *lp = limit;
1897 return delim;
1900 if (NIL_P(str))
1901 *strp = str = rb_str_new(p, searchlen);
1902 else
1903 rb_str_buf_cat(str, p, searchlen);
1904 fptr->crbuf_off += searchlen;
1905 fptr->crbuf_len -= searchlen;
1906 limit -= searchlen;
1908 if (limit == 0) {
1909 *lp = limit;
1910 return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
1914 if (more_char(fptr) == -1) {
1915 *lp = limit;
1916 return EOF;
1921 while (1) {
1922 long pending = READ_DATA_PENDING_COUNT(fptr);
1923 if (pending > 0) {
1924 const char *p = READ_DATA_PENDING_PTR(fptr);
1925 const char *e;
1926 long last;
1928 if (limit > 0 && pending > limit) pending = limit;
1929 e = memchr(p, delim, pending);
1930 if (e) pending = e - p + 1;
1931 if (!NIL_P(str)) {
1932 last = RSTRING_LEN(str);
1933 rb_str_resize(str, last + pending);
1935 else {
1936 last = 0;
1937 *strp = str = rb_str_buf_new(pending);
1938 rb_str_set_len(str, pending);
1940 read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
1941 limit -= pending;
1942 *lp = limit;
1943 if (e) return delim;
1944 if (limit == 0)
1945 return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
1947 rb_thread_wait_fd(fptr->fd);
1948 rb_io_check_closed(fptr);
1949 if (io_fillbuf(fptr) < 0) {
1950 *lp = limit;
1951 return EOF;
1956 static inline int
1957 swallow(rb_io_t *fptr, int term)
1959 do {
1960 long cnt;
1961 while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
1962 char buf[1024];
1963 const char *p = READ_DATA_PENDING_PTR(fptr);
1964 int i;
1965 if (cnt > sizeof buf) cnt = sizeof buf;
1966 if (*p != term) return Qtrue;
1967 i = cnt;
1968 while (--i && *++p == term);
1969 if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
1970 rb_sys_fail(fptr->path);
1972 rb_thread_wait_fd(fptr->fd);
1973 rb_io_check_closed(fptr);
1974 } while (io_fillbuf(fptr) == 0);
1975 return Qfalse;
1978 static VALUE
1979 rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc)
1981 VALUE str = Qnil;
1982 int len = 0;
1983 long pos = 0;
1984 int cr = fptr->enc2 ? ENC_CODERANGE_BROKEN : 0;
1986 for (;;) {
1987 long pending = READ_DATA_PENDING_COUNT(fptr);
1989 if (pending > 0) {
1990 const char *p = READ_DATA_PENDING_PTR(fptr);
1991 const char *e;
1993 e = memchr(p, '\n', pending);
1994 if (e) {
1995 pending = e - p + 1;
1997 if (NIL_P(str)) {
1998 str = rb_str_new(p, pending);
1999 fptr->rbuf_off += pending;
2000 fptr->rbuf_len -= pending;
2002 else {
2003 rb_str_resize(str, len + pending);
2004 read_buffered_data(RSTRING_PTR(str)+len, pending, fptr);
2006 len += pending;
2007 if (cr != ENC_CODERANGE_BROKEN)
2008 pos = rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
2009 if (e) break;
2011 rb_thread_wait_fd(fptr->fd);
2012 rb_io_check_closed(fptr);
2013 if (io_fillbuf(fptr) < 0) {
2014 if (NIL_P(str)) return Qnil;
2015 break;
2019 str = io_enc_str(str, fptr);
2020 if (!fptr->enc2) ENC_CODERANGE_SET(str, cr);
2021 fptr->lineno++;
2022 ARGF.lineno = INT2FIX(fptr->lineno);
2023 return str;
2026 static void
2027 prepare_getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit, VALUE io)
2029 VALUE rs = rb_rs, lim = Qnil;
2030 rb_io_t *fptr;
2032 if (argc == 1) {
2033 VALUE tmp = Qnil;
2035 if (NIL_P(argv[0]) || !NIL_P(tmp = rb_check_string_type(argv[0]))) {
2036 rs = tmp;
2038 else {
2039 lim = argv[0];
2042 else if (2 <= argc) {
2043 rb_scan_args(argc, argv, "2", &rs, &lim);
2044 if (!NIL_P(rs))
2045 StringValue(rs);
2047 if (!NIL_P(rs)) {
2048 rb_encoding *enc_rs, *enc_io;
2050 GetOpenFile(io, fptr);
2051 enc_rs = rb_enc_get(rs);
2052 enc_io = io_read_encoding(fptr);
2053 if (enc_io != enc_rs &&
2054 (rb_enc_str_coderange(rs) != ENC_CODERANGE_7BIT ||
2055 !rb_enc_asciicompat(enc_io))) {
2056 if (rs == rb_default_rs) {
2057 rs = rb_enc_str_new(0, 0, enc_io);
2058 rb_str_buf_cat_ascii(rs, "\n");
2060 else {
2061 rb_raise(rb_eArgError, "encoding mismatch: %s IO with %s RS",
2062 rb_enc_name(enc_io),
2063 rb_enc_name(enc_rs));
2067 *rsp = rs;
2068 *limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
2071 static VALUE
2072 rb_io_getline_1(VALUE rs, long limit, VALUE io)
2074 VALUE str = Qnil;
2075 rb_io_t *fptr;
2076 int nolimit = 0;
2077 rb_encoding *enc;
2079 GetOpenFile(io, fptr);
2080 rb_io_check_readable(fptr);
2081 if (NIL_P(rs)) {
2082 str = read_all(fptr, 0, Qnil);
2083 if (RSTRING_LEN(str) == 0) return Qnil;
2085 else if (limit == 0) {
2086 return rb_enc_str_new(0, 0, io_read_encoding(fptr));
2088 else if (rs == rb_default_rs && limit < 0 && !fptr->enc2 &&
2089 rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
2090 return rb_io_getline_fast(fptr, enc);
2092 else {
2093 int c, newline;
2094 const char *rsptr;
2095 long rslen;
2096 int rspara = 0;
2097 int extra_limit = 16;
2099 rslen = RSTRING_LEN(rs);
2100 if (rslen == 0) {
2101 rsptr = "\n\n";
2102 rslen = 2;
2103 rspara = 1;
2104 swallow(fptr, '\n');
2105 rs = 0;
2107 else {
2108 rsptr = RSTRING_PTR(rs);
2110 newline = (unsigned char)rsptr[rslen - 1];
2112 if (fptr->enc2)
2113 enc = fptr->enc;
2114 else
2115 enc = io_input_encoding(fptr);
2116 while ((c = appendline(fptr, newline, &str, &limit)) != EOF) {
2117 const char *s, *p, *pp;
2119 if (c == newline) {
2120 if (RSTRING_LEN(str) < rslen) continue;
2121 s = RSTRING_PTR(str);
2122 p = s + RSTRING_LEN(str) - rslen;
2123 pp = rb_enc_left_char_head(s, p, enc);
2124 if (pp != p) continue;
2125 if (!rspara) rscheck(rsptr, rslen, rs);
2126 if (memcmp(p, rsptr, rslen) == 0) break;
2128 if (limit == 0) {
2129 s = RSTRING_PTR(str);
2130 p = s + RSTRING_LEN(str);
2131 pp = rb_enc_left_char_head(s, p-1, enc);
2132 if (extra_limit &&
2133 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(pp, p, enc))) {
2134 /* relax the limit while incomplete character.
2135 * extra_limit limits the relax length */
2136 limit = 1;
2137 extra_limit--;
2139 else {
2140 nolimit = 1;
2141 break;
2146 if (rspara) {
2147 if (c != EOF) {
2148 swallow(fptr, '\n');
2151 if (!NIL_P(str))
2152 str = io_enc_str(str, fptr);
2155 if (!NIL_P(str)) {
2156 if (!nolimit) {
2157 fptr->lineno++;
2158 ARGF.lineno = INT2FIX(fptr->lineno);
2162 return str;
2165 static VALUE
2166 rb_io_getline(int argc, VALUE *argv, VALUE io)
2168 VALUE rs;
2169 long limit;
2171 prepare_getline_args(argc, argv, &rs, &limit, io);
2172 return rb_io_getline_1(rs, limit, io);
2175 VALUE
2176 rb_io_gets(VALUE io)
2178 rb_io_t *fptr;
2180 GetOpenFile(io, fptr);
2181 rb_io_check_readable(fptr);
2182 return rb_io_getline_fast(fptr, io_read_encoding(fptr));
2186 * call-seq:
2187 * ios.gets(sep=$/) => string or nil
2188 * ios.gets(limit) => string or nil
2189 * ios.gets(sep, limit) => string or nil
2191 * Reads the next ``line'' from the I/O stream; lines are separated by
2192 * <i>sep</i>. A separator of <code>nil</code> reads the entire
2193 * contents, and a zero-length separator reads the input a paragraph at
2194 * a time (two successive newlines in the input separate paragraphs).
2195 * The stream must be opened for reading or an <code>IOError</code>
2196 * will be raised. The line read in will be returned and also assigned
2197 * to <code>$_</code>. Returns <code>nil</code> if called at end of
2198 * file. If the first argument is an integer, or optional second
2199 * argument is given, the returning string would not be longer than the
2200 * given value.
2202 * File.new("testfile").gets #=> "This is line one\n"
2203 * $_ #=> "This is line one\n"
2206 static VALUE
2207 rb_io_gets_m(int argc, VALUE *argv, VALUE io)
2209 VALUE str;
2211 str = rb_io_getline(argc, argv, io);
2212 rb_lastline_set(str);
2214 return str;
2218 * call-seq:
2219 * ios.lineno => integer
2221 * Returns the current line number in <em>ios</em>. The stream must be
2222 * opened for reading. <code>lineno</code> counts the number of times
2223 * <code>gets</code> is called, rather than the number of newlines
2224 * encountered. The two values will differ if <code>gets</code> is
2225 * called with a separator other than newline. See also the
2226 * <code>$.</code> variable.
2228 * f = File.new("testfile")
2229 * f.lineno #=> 0
2230 * f.gets #=> "This is line one\n"
2231 * f.lineno #=> 1
2232 * f.gets #=> "This is line two\n"
2233 * f.lineno #=> 2
2236 static VALUE
2237 rb_io_lineno(VALUE io)
2239 rb_io_t *fptr;
2241 GetOpenFile(io, fptr);
2242 rb_io_check_readable(fptr);
2243 return INT2NUM(fptr->lineno);
2247 * call-seq:
2248 * ios.lineno = integer => integer
2250 * Manually sets the current line number to the given value.
2251 * <code>$.</code> is updated only on the next read.
2253 * f = File.new("testfile")
2254 * f.gets #=> "This is line one\n"
2255 * $. #=> 1
2256 * f.lineno = 1000
2257 * f.lineno #=> 1000
2258 * $. #=> 1 # lineno of last read
2259 * f.gets #=> "This is line two\n"
2260 * $. #=> 1001 # lineno of last read
2263 static VALUE
2264 rb_io_set_lineno(VALUE io, VALUE lineno)
2266 rb_io_t *fptr;
2268 GetOpenFile(io, fptr);
2269 rb_io_check_readable(fptr);
2270 fptr->lineno = NUM2INT(lineno);
2271 return lineno;
2275 * call-seq:
2276 * ios.readline(sep=$/) => string
2277 * ios.readline(limit) => string
2278 * ios.readline(sep, limit) => string
2280 * Reads a line as with <code>IO#gets</code>, but raises an
2281 * <code>EOFError</code> on end of file.
2284 static VALUE
2285 rb_io_readline(int argc, VALUE *argv, VALUE io)
2287 VALUE line = rb_io_gets_m(argc, argv, io);
2289 if (NIL_P(line)) {
2290 rb_eof_error();
2292 return line;
2296 * call-seq:
2297 * ios.readlines(sep=$/) => array
2298 * ios.readlines(limit) => array
2299 * ios.readlines(sep, limit) => array
2301 * Reads all of the lines in <em>ios</em>, and returns them in
2302 * <i>anArray</i>. Lines are separated by the optional <i>sep</i>. If
2303 * <i>sep</i> is <code>nil</code>, the rest of the stream is returned
2304 * as a single record. If the first argument is an integer, or
2305 * optional second argument is given, the returning string would not be
2306 * longer than the given value. The stream must be opened for reading
2307 * or an <code>IOError</code> will be raised.
2309 * f = File.new("testfile")
2310 * f.readlines[0] #=> "This is line one\n"
2313 static VALUE
2314 rb_io_readlines(int argc, VALUE *argv, VALUE io)
2316 VALUE line, ary, rs;
2317 long limit;
2319 prepare_getline_args(argc, argv, &rs, &limit, io);
2320 ary = rb_ary_new();
2321 while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) {
2322 rb_ary_push(ary, line);
2324 return ary;
2328 * call-seq:
2329 * ios.each(sep=$/) {|line| block } => ios
2330 * ios.each(limit) {|line| block } => ios
2331 * ios.each(sep,limit) {|line| block } => ios
2332 * ios.each_line(sep=$/) {|line| block } => ios
2333 * ios.each_line(limit) {|line| block } => ios
2334 * ios.each_line(sep,limit) {|line| block } => ios
2336 * Executes the block for every line in <em>ios</em>, where lines are
2337 * separated by <i>sep</i>. <em>ios</em> must be opened for
2338 * reading or an <code>IOError</code> will be raised.
2340 * f = File.new("testfile")
2341 * f.each {|line| puts "#{f.lineno}: #{line}" }
2343 * <em>produces:</em>
2345 * 1: This is line one
2346 * 2: This is line two
2347 * 3: This is line three
2348 * 4: And so on...
2351 static VALUE
2352 rb_io_each_line(int argc, VALUE *argv, VALUE io)
2354 VALUE str, rs;
2355 long limit;
2357 RETURN_ENUMERATOR(io, argc, argv);
2358 prepare_getline_args(argc, argv, &rs, &limit, io);
2359 while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) {
2360 rb_yield(str);
2362 return io;
2366 * call-seq:
2367 * ios.each_byte {|byte| block } => ios
2369 * Calls the given block once for each byte (0..255) in <em>ios</em>,
2370 * passing the byte as an argument. The stream must be opened for
2371 * reading or an <code>IOError</code> will be raised.
2373 * f = File.new("testfile")
2374 * checksum = 0
2375 * f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
2376 * checksum #=> 12
2379 static VALUE
2380 rb_io_each_byte(VALUE io)
2382 rb_io_t *fptr;
2383 char *p, *e;
2385 RETURN_ENUMERATOR(io, 0, 0);
2386 GetOpenFile(io, fptr);
2388 for (;;) {
2389 p = fptr->rbuf+fptr->rbuf_off;
2390 e = p + fptr->rbuf_len;
2391 while (p < e) {
2392 fptr->rbuf_off++;
2393 fptr->rbuf_len--;
2394 rb_yield(INT2FIX(*p & 0xff));
2395 p++;
2396 errno = 0;
2398 rb_io_check_readable(fptr);
2399 READ_CHECK(fptr);
2400 if (io_fillbuf(fptr) < 0) {
2401 break;
2404 return io;
2407 static VALUE
2408 io_getc(rb_io_t *fptr, rb_encoding *enc)
2410 int r, n, cr = 0;
2411 VALUE str;
2413 if (fptr->enc2) {
2414 VALUE str = Qnil;
2416 if (!fptr->readconv) {
2417 make_readconv(fptr);
2420 while (1) {
2421 if (fptr->crbuf_len) {
2422 r = rb_enc_precise_mbclen(fptr->crbuf+fptr->crbuf_off,
2423 fptr->crbuf+fptr->crbuf_off+fptr->crbuf_len,
2424 fptr->enc);
2425 if (!MBCLEN_NEEDMORE_P(r))
2426 break;
2427 if (fptr->crbuf_len == fptr->crbuf_capa) {
2428 rb_raise(rb_eIOError, "too long character");
2432 if (more_char(fptr) == -1) {
2433 if (fptr->crbuf_len == 0)
2434 return Qnil;
2435 /* return an incomplete character just before EOF */
2436 return io_shift_crbuf(fptr, fptr->crbuf_len, &str);
2439 if (MBCLEN_INVALID_P(r)) {
2440 r = rb_enc_mbclen(fptr->crbuf+fptr->crbuf_off,
2441 fptr->crbuf+fptr->crbuf_off+fptr->crbuf_len,
2442 fptr->enc);
2443 return io_shift_crbuf(fptr, r, &str);
2445 return io_shift_crbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str);
2448 if (io_fillbuf(fptr) < 0) {
2449 return Qnil;
2451 if (rb_enc_asciicompat(enc) && ISASCII(fptr->rbuf[fptr->rbuf_off])) {
2452 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, 1);
2453 fptr->rbuf_off += 1;
2454 fptr->rbuf_len -= 1;
2455 cr = ENC_CODERANGE_7BIT;
2457 else {
2458 r = rb_enc_precise_mbclen(fptr->rbuf+fptr->rbuf_off, fptr->rbuf+fptr->rbuf_off+fptr->rbuf_len, enc);
2459 if (MBCLEN_CHARFOUND_P(r) &&
2460 (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf_len) {
2461 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, n);
2462 fptr->rbuf_off += n;
2463 fptr->rbuf_len -= n;
2464 cr = ENC_CODERANGE_VALID;
2466 else if (MBCLEN_NEEDMORE_P(r)) {
2467 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, fptr->rbuf_len);
2468 fptr->rbuf_len = 0;
2469 getc_needmore:
2470 if (io_fillbuf(fptr) != -1) {
2471 rb_str_cat(str, fptr->rbuf+fptr->rbuf_off, 1);
2472 fptr->rbuf_off++;
2473 fptr->rbuf_len--;
2474 r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
2475 if (MBCLEN_NEEDMORE_P(r)) {
2476 goto getc_needmore;
2478 else if (MBCLEN_CHARFOUND_P(r)) {
2479 cr = ENC_CODERANGE_VALID;
2483 else {
2484 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, 1);
2485 fptr->rbuf_off++;
2486 fptr->rbuf_len--;
2489 if (!cr) cr = ENC_CODERANGE_BROKEN;
2490 str = io_enc_str(str, fptr);
2491 if (!fptr->enc2) {
2492 ENC_CODERANGE_SET(str, cr);
2494 return str;
2498 * call-seq:
2499 * ios.each_char {|c| block } => ios
2501 * Calls the given block once for each character in <em>ios</em>,
2502 * passing the character as an argument. The stream must be opened for
2503 * reading or an <code>IOError</code> will be raised.
2505 * f = File.new("testfile")
2506 * f.each_char {|c| print c, ' ' } #=> #<File:testfile>
2509 static VALUE
2510 rb_io_each_char(VALUE io)
2512 rb_io_t *fptr;
2513 rb_encoding *enc;
2514 VALUE c;
2516 RETURN_ENUMERATOR(io, 0, 0);
2517 GetOpenFile(io, fptr);
2518 rb_io_check_readable(fptr);
2520 enc = io_input_encoding(fptr);
2521 READ_CHECK(fptr);
2522 while (!NIL_P(c = io_getc(fptr, enc))) {
2523 rb_yield(c);
2525 return io;
2531 * call-seq:
2532 * ios.lines(sep=$/) => anEnumerator
2533 * ios.lines(limit) => anEnumerator
2534 * ios.lines(sep, limit) => anEnumerator
2536 * Returns an enumerator that gives each line in <em>ios</em>.
2537 * The stream must be opened for reading or an <code>IOError</code>
2538 * will be raised.
2540 * f = File.new("testfile")
2541 * f.lines.to_a #=> ["foo\n", "bar\n"]
2542 * f.rewind
2543 * f.lines.sort #=> ["bar\n", "foo\n"]
2546 static VALUE
2547 rb_io_lines(int argc, VALUE *argv, VALUE io)
2549 return rb_enumeratorize(io, ID2SYM(rb_intern("each_line")), argc, argv);
2553 * call-seq:
2554 * ios.bytes => anEnumerator
2556 * Returns an enumerator that gives each byte (0..255) in <em>ios</em>.
2557 * The stream must be opened for reading or an <code>IOError</code>
2558 * will be raised.
2560 * f = File.new("testfile")
2561 * f.bytes.to_a #=> [104, 101, 108, 108, 111]
2562 * f.rewind
2563 * f.bytes.sort #=> [101, 104, 108, 108, 111]
2566 static VALUE
2567 rb_io_bytes(VALUE io)
2569 return rb_enumeratorize(io, ID2SYM(rb_intern("each_byte")), 0, 0);
2573 * call-seq:
2574 * ios.chars => anEnumerator
2576 * Returns an enumerator that gives each character in <em>ios</em>.
2577 * The stream must be opened for reading or an <code>IOError</code>
2578 * will be raised.
2580 * f = File.new("testfile")
2581 * f.chars.to_a #=> ["h", "e", "l", "l", "o"]
2582 * f.rewind
2583 * f.chars.sort #=> ["e", "h", "l", "l", "o"]
2586 static VALUE
2587 rb_io_chars(VALUE io)
2589 return rb_enumeratorize(io, ID2SYM(rb_intern("each_char")), 0, 0);
2593 * call-seq:
2594 * ios.getc => fixnum or nil
2596 * Reads a one-character string from <em>ios</em>. Returns
2597 * <code>nil</code> if called at end of file.
2599 * f = File.new("testfile")
2600 * f.getc #=> "8"
2601 * f.getc #=> "1"
2604 static VALUE
2605 rb_io_getc(VALUE io)
2607 rb_io_t *fptr;
2608 rb_encoding *enc;
2610 GetOpenFile(io, fptr);
2611 rb_io_check_readable(fptr);
2613 enc = io_input_encoding(fptr);
2614 READ_CHECK(fptr);
2615 return io_getc(fptr, enc);
2618 rb_getc(FILE *f)
2620 int c;
2622 rb_read_check(f);
2623 TRAP_BEG;
2624 c = getc(f);
2625 TRAP_END;
2627 return c;
2631 * call-seq:
2632 * ios.readchar => string
2634 * Reads a one-character string from <em>ios</em>. Raises an
2635 * <code>EOFError</code> on end of file.
2637 * f = File.new("testfile")
2638 * f.readchar #=> "8"
2639 * f.readchar #=> "1"
2642 static VALUE
2643 rb_io_readchar(VALUE io)
2645 VALUE c = rb_io_getc(io);
2647 if (NIL_P(c)) {
2648 rb_eof_error();
2650 return c;
2654 * call-seq:
2655 * ios.getbyte => fixnum or nil
2657 * Gets the next 8-bit byte (0..255) from <em>ios</em>. Returns
2658 * <code>nil</code> if called at end of file.
2660 * f = File.new("testfile")
2661 * f.getbyte #=> 84
2662 * f.getbyte #=> 104
2665 VALUE
2666 rb_io_getbyte(VALUE io)
2668 rb_io_t *fptr;
2669 int c;
2671 GetOpenFile(io, fptr);
2672 rb_io_check_readable(fptr);
2673 READ_CHECK(fptr);
2674 if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && TYPE(rb_stdout) == T_FILE) {
2675 rb_io_t *ofp;
2676 GetOpenFile(rb_stdout, ofp);
2677 if (ofp->mode & FMODE_TTY) {
2678 rb_io_flush(rb_stdout);
2681 if (io_fillbuf(fptr) < 0) {
2682 return Qnil;
2684 fptr->rbuf_off++;
2685 fptr->rbuf_len--;
2686 c = (unsigned char)fptr->rbuf[fptr->rbuf_off-1];
2687 return INT2FIX(c & 0xff);
2691 * call-seq:
2692 * ios.readbyte => fixnum
2694 * Reads a character as with <code>IO#getc</code>, but raises an
2695 * <code>EOFError</code> on end of file.
2698 static VALUE
2699 rb_io_readbyte(VALUE io)
2701 VALUE c = rb_io_getbyte(io);
2703 if (NIL_P(c)) {
2704 rb_eof_error();
2706 return c;
2710 * call-seq:
2711 * ios.ungetbyte(string) => nil
2712 * ios.ungetbyte(integer) => nil
2714 * Pushes back bytes (passed as a parameter) onto <em>ios</em>,
2715 * such that a subsequent buffered read will return it. Only one byte
2716 * may be pushed back before a subsequent read operation (that is,
2717 * you will be able to read only the last of several bytes that have been pushed
2718 * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
2720 * f = File.new("testfile") #=> #<File:testfile>
2721 * b = f.getbyte #=> 0x38
2722 * f.ungetbyte(b) #=> nil
2723 * f.getbyte #=> 0x38
2726 VALUE
2727 rb_io_ungetbyte(VALUE io, VALUE b)
2729 rb_io_t *fptr;
2731 GetOpenFile(io, fptr);
2732 rb_io_check_readable(fptr);
2733 if (NIL_P(b)) return Qnil;
2734 if (FIXNUM_P(b)) {
2735 char cc = FIX2INT(b);
2736 b = rb_str_new(&cc, 1);
2738 else {
2739 SafeStringValue(b);
2741 io_ungetbyte(b, fptr);
2742 return Qnil;
2746 * call-seq:
2747 * ios.ungetc(string) => nil
2749 * Pushes back one character (passed as a parameter) onto <em>ios</em>,
2750 * such that a subsequent buffered read will return it. Only one character
2751 * may be pushed back before a subsequent read operation (that is,
2752 * you will be able to read only the last of several characters that have been pushed
2753 * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
2755 * f = File.new("testfile") #=> #<File:testfile>
2756 * c = f.getc #=> "8"
2757 * f.ungetc(c) #=> nil
2758 * f.getc #=> "8"
2761 VALUE
2762 rb_io_ungetc(VALUE io, VALUE c)
2764 rb_io_t *fptr;
2765 long len;
2767 GetOpenFile(io, fptr);
2768 rb_io_check_readable(fptr);
2769 if (NIL_P(c)) return Qnil;
2770 if (FIXNUM_P(c)) {
2771 int cc = FIX2INT(c);
2772 rb_encoding *enc = io_read_encoding(fptr);
2773 char buf[16];
2775 c = rb_str_new(buf, rb_enc_mbcput(cc, buf, enc));
2777 else {
2778 SafeStringValue(c);
2780 if (fptr->enc2) {
2781 make_readconv(fptr);
2782 len = RSTRING_LEN(c);
2783 if (fptr->crbuf_capa - fptr->crbuf_len < len)
2784 rb_raise(rb_eIOError, "ungetc failed");
2785 if (fptr->crbuf_off < len) {
2786 MEMMOVE(fptr->crbuf+fptr->crbuf_capa-fptr->crbuf_len,
2787 fptr->crbuf+fptr->crbuf_off,
2788 char, fptr->crbuf_len);
2789 fptr->crbuf_off = fptr->crbuf_capa-fptr->crbuf_len;
2791 fptr->crbuf_off -= len;
2792 fptr->crbuf_len += len;
2793 MEMMOVE(fptr->crbuf+fptr->crbuf_off, RSTRING_PTR(c), char, len);
2795 else {
2796 io_ungetbyte(c, fptr);
2798 return Qnil;
2802 * call-seq:
2803 * ios.isatty => true or false
2804 * ios.tty? => true or false
2806 * Returns <code>true</code> if <em>ios</em> is associated with a
2807 * terminal device (tty), <code>false</code> otherwise.
2809 * File.new("testfile").isatty #=> false
2810 * File.new("/dev/tty").isatty #=> true
2813 static VALUE
2814 rb_io_isatty(VALUE io)
2816 rb_io_t *fptr;
2818 GetOpenFile(io, fptr);
2819 if (isatty(fptr->fd) == 0)
2820 return Qfalse;
2821 return Qtrue;
2825 * call-seq:
2826 * ios.close_on_exec? => true or false
2828 * Returns <code>true</code> if <em>ios</em> will be closed on exec.
2830 * f = open("/dev/null")
2831 * f.close_on_exec? #=> false
2832 * f.close_on_exec = true
2833 * f.close_on_exec? #=> true
2834 * f.close_on_exec = false
2835 * f.close_on_exec? #=> false
2838 static VALUE
2839 rb_io_close_on_exec_p(VALUE io)
2841 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
2842 rb_io_t *fptr;
2843 VALUE write_io;
2844 int fd, ret;
2846 write_io = GetWriteIO(io);
2847 if (io != write_io) {
2848 GetOpenFile(write_io, fptr);
2849 if (fptr && 0 <= (fd = fptr->fd)) {
2850 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2851 if (!(ret & FD_CLOEXEC)) return Qfalse;
2855 GetOpenFile(io, fptr);
2856 if (fptr && 0 <= (fd = fptr->fd)) {
2857 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2858 if (!(ret & FD_CLOEXEC)) return Qfalse;
2860 return Qtrue;
2861 #else
2862 rb_notimplement();
2863 return Qnil; /* not reached */
2864 #endif
2868 * call-seq:
2869 * ios.close_on_exec = bool => true or false
2871 * Sets a close-on-exec flag.
2873 * f = open("/dev/null")
2874 * f.close_on_exec = true
2875 * system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
2876 * f.closed? #=> false
2879 static VALUE
2880 rb_io_set_close_on_exec(VALUE io, VALUE arg)
2882 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
2883 int flag = RTEST(arg) ? FD_CLOEXEC : 0;
2884 rb_io_t *fptr;
2885 VALUE write_io;
2886 int fd, ret;
2888 write_io = GetWriteIO(io);
2889 if (io != write_io) {
2890 GetOpenFile(write_io, fptr);
2891 if (fptr && 0 <= (fd = fptr->fd)) {
2892 if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2893 if ((ret & FD_CLOEXEC) != flag) {
2894 ret = (ret & ~FD_CLOEXEC) | flag;
2895 ret = fcntl(fd, F_SETFD, ret);
2896 if (ret == -1) rb_sys_fail(fptr->path);
2902 GetOpenFile(io, fptr);
2903 if (fptr && 0 <= (fd = fptr->fd)) {
2904 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2905 if ((ret & FD_CLOEXEC) != flag) {
2906 ret = (ret & ~FD_CLOEXEC) | flag;
2907 ret = fcntl(fd, F_SETFD, ret);
2908 if (ret == -1) rb_sys_fail(fptr->path);
2911 #else
2912 rb_notimplement();
2913 #endif
2914 return Qnil;
2917 #define FMODE_PREP (1<<16)
2918 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
2919 #define PREP_STDIO_NAME(f) ((f)->path)
2921 static void
2922 finish_writeconv(rb_io_t *fptr, int noraise)
2924 unsigned char *ds, *dp, *de;
2925 rb_econv_result_t res;
2927 if (!fptr->wbuf) {
2928 unsigned char buf[1024];
2929 int r;
2931 res = econv_destination_buffer_full;
2932 while (res == econv_destination_buffer_full) {
2933 ds = dp = buf;
2934 de = buf + sizeof(buf);
2935 res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
2936 while (dp-ds) {
2937 retry:
2938 r = rb_write_internal(fptr->fd, ds, dp-ds);
2939 if (r == dp-ds)
2940 break;
2941 if (0 <= r) {
2942 ds += r;
2944 if (rb_io_wait_writable(fptr->fd)) {
2945 if (!noraise)
2946 rb_io_check_closed(fptr);
2947 else if (fptr->fd < 0)
2948 return;
2949 goto retry;
2951 return;
2953 if (!noraise) {
2954 rb_econv_check_error(fptr->writeconv);
2956 if (res == econv_invalid_byte_sequence ||
2957 res == econv_undefined_conversion) {
2958 break;
2962 return;
2965 res = econv_destination_buffer_full;
2966 while (res == econv_destination_buffer_full) {
2967 if (fptr->wbuf_len == fptr->wbuf_capa) {
2968 io_fflush(fptr);
2971 ds = dp = (unsigned char *)fptr->wbuf + fptr->wbuf_off + fptr->wbuf_len;
2972 de = (unsigned char *)fptr->wbuf + fptr->wbuf_capa;
2973 res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
2974 fptr->wbuf_len += dp - ds;
2975 if (!noraise) {
2976 rb_econv_check_error(fptr->writeconv);
2978 if (res == econv_invalid_byte_sequence ||
2979 res == econv_undefined_conversion) {
2980 break;
2986 static void
2987 fptr_finalize(rb_io_t *fptr, int noraise)
2989 int ebadf = 0;
2990 if (fptr->writeconv) {
2991 finish_writeconv(fptr, noraise);
2993 if (fptr->wbuf_len) {
2994 io_fflush(fptr);
2996 if (IS_PREP_STDIO(fptr) ||
2997 fptr->fd <= 2) {
2998 return;
3000 if (fptr->stdio_file) {
3001 if (fclose(fptr->stdio_file) < 0 && !noraise) {
3002 /* fptr->stdio_file is deallocated anyway */
3003 fptr->stdio_file = 0;
3004 fptr->fd = -1;
3005 rb_sys_fail(fptr->path);
3008 else if (0 <= fptr->fd) {
3009 if (close(fptr->fd) < 0 && !noraise) {
3010 if (errno != EBADF) {
3011 /* fptr->fd is still not closed */
3012 rb_sys_fail(fptr->path);
3014 else {
3015 /* fptr->fd is already closed. */
3016 ebadf = 1;
3020 fptr->fd = -1;
3021 fptr->stdio_file = 0;
3022 fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
3023 if (ebadf) {
3024 rb_sys_fail(fptr->path);
3028 static void
3029 rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
3031 if (fptr->finalize) {
3032 (*fptr->finalize)(fptr, noraise);
3034 else {
3035 fptr_finalize(fptr, noraise);
3039 static void
3040 clear_readconv(rb_io_t *fptr)
3042 if (fptr->readconv) {
3043 rb_econv_close(fptr->readconv);
3044 fptr->readconv = NULL;
3046 if (fptr->crbuf) {
3047 free(fptr->crbuf);
3048 fptr->crbuf = NULL;
3052 static void
3053 clear_writeconv(rb_io_t *fptr)
3055 if (fptr->writeconv) {
3056 rb_econv_close(fptr->writeconv);
3057 fptr->writeconv = NULL;
3059 fptr->writeconv_initialized = 0;
3062 static void
3063 clear_codeconv(rb_io_t *fptr)
3065 clear_readconv(fptr);
3066 clear_writeconv(fptr);
3070 rb_io_fptr_finalize(rb_io_t *fptr)
3072 if (!fptr) return 0;
3073 if (fptr->refcnt <= 0 || --fptr->refcnt) return 0;
3074 if (fptr->path) {
3075 free(fptr->path);
3076 fptr->path = 0;
3078 if (0 <= fptr->fd)
3079 rb_io_fptr_cleanup(fptr, Qtrue);
3080 if (fptr->rbuf) {
3081 free(fptr->rbuf);
3082 fptr->rbuf = 0;
3084 if (fptr->wbuf) {
3085 free(fptr->wbuf);
3086 fptr->wbuf = 0;
3088 clear_codeconv(fptr);
3089 free(fptr);
3090 return 1;
3093 VALUE
3094 rb_io_close(VALUE io)
3096 rb_io_t *fptr;
3097 int fd;
3098 VALUE write_io;
3099 rb_io_t *write_fptr;
3101 write_io = GetWriteIO(io);
3102 if (io != write_io) {
3103 write_fptr = RFILE(write_io)->fptr;
3104 if (write_fptr && 0 <= write_fptr->fd) {
3105 rb_io_fptr_cleanup(write_fptr, Qtrue);
3109 fptr = RFILE(io)->fptr;
3110 if (!fptr) return Qnil;
3111 if (fptr->fd < 0) return Qnil;
3113 fd = fptr->fd;
3114 rb_io_fptr_cleanup(fptr, Qfalse);
3115 rb_thread_fd_close(fd);
3117 if (fptr->pid) {
3118 rb_syswait(fptr->pid);
3119 fptr->pid = 0;
3122 return Qnil;
3126 * call-seq:
3127 * ios.close => nil
3129 * Closes <em>ios</em> and flushes any pending writes to the operating
3130 * system. The stream is unavailable for any further data operations;
3131 * an <code>IOError</code> is raised if such an attempt is made. I/O
3132 * streams are automatically closed when they are claimed by the
3133 * garbage collector.
3135 * If <em>ios</em> is opened by <code>IO.popen</code>,
3136 * <code>close</code> sets <code>$?</code>.
3139 static VALUE
3140 rb_io_close_m(VALUE io)
3142 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
3143 rb_raise(rb_eSecurityError, "Insecure: can't close");
3145 rb_io_check_closed(RFILE(io)->fptr);
3146 rb_io_close(io);
3147 return Qnil;
3150 static VALUE
3151 io_call_close(VALUE io)
3153 return rb_funcall(io, rb_intern("close"), 0, 0);
3156 static VALUE
3157 io_close(VALUE io)
3159 return rb_rescue(io_call_close, io, 0, 0);
3163 * call-seq:
3164 * ios.closed? => true or false
3166 * Returns <code>true</code> if <em>ios</em> is completely closed (for
3167 * duplex streams, both reader and writer), <code>false</code>
3168 * otherwise.
3170 * f = File.new("testfile")
3171 * f.close #=> nil
3172 * f.closed? #=> true
3173 * f = IO.popen("/bin/sh","r+")
3174 * f.close_write #=> nil
3175 * f.closed? #=> false
3176 * f.close_read #=> nil
3177 * f.closed? #=> true
3181 static VALUE
3182 rb_io_closed(VALUE io)
3184 rb_io_t *fptr;
3185 VALUE write_io;
3186 rb_io_t *write_fptr;
3188 write_io = GetWriteIO(io);
3189 if (io != write_io) {
3190 write_fptr = RFILE(write_io)->fptr;
3191 if (write_fptr && 0 <= write_fptr->fd) {
3192 return Qfalse;
3196 fptr = RFILE(io)->fptr;
3197 rb_io_check_initialized(fptr);
3198 return 0 <= fptr->fd ? Qfalse : Qtrue;
3202 * call-seq:
3203 * ios.close_read => nil
3205 * Closes the read end of a duplex I/O stream (i.e., one that contains
3206 * both a read and a write stream, such as a pipe). Will raise an
3207 * <code>IOError</code> if the stream is not duplexed.
3209 * f = IO.popen("/bin/sh","r+")
3210 * f.close_read
3211 * f.readlines
3213 * <em>produces:</em>
3215 * prog.rb:3:in `readlines': not opened for reading (IOError)
3216 * from prog.rb:3
3219 static VALUE
3220 rb_io_close_read(VALUE io)
3222 rb_io_t *fptr;
3223 VALUE write_io;
3225 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
3226 rb_raise(rb_eSecurityError, "Insecure: can't close");
3228 GetOpenFile(io, fptr);
3229 if (is_socket(fptr->fd, fptr->path)) {
3230 #ifndef SHUT_RD
3231 # define SHUT_RD 0
3232 #endif
3233 if (shutdown(fptr->fd, SHUT_RD) < 0)
3234 rb_sys_fail(fptr->path);
3235 fptr->mode &= ~FMODE_READABLE;
3236 if (!(fptr->mode & FMODE_WRITABLE))
3237 return rb_io_close(io);
3238 return Qnil;
3241 write_io = GetWriteIO(io);
3242 if (io != write_io) {
3243 rb_io_t *wfptr;
3244 fptr_finalize(fptr, Qfalse);
3245 GetOpenFile(write_io, wfptr);
3246 if (fptr->refcnt < LONG_MAX) {
3247 wfptr->refcnt++;
3248 RFILE(io)->fptr = wfptr;
3249 rb_io_fptr_finalize(fptr);
3251 return Qnil;
3254 if (fptr->mode & FMODE_WRITABLE) {
3255 rb_raise(rb_eIOError, "closing non-duplex IO for reading");
3257 return rb_io_close(io);
3261 * call-seq:
3262 * ios.close_write => nil
3264 * Closes the write end of a duplex I/O stream (i.e., one that contains
3265 * both a read and a write stream, such as a pipe). Will raise an
3266 * <code>IOError</code> if the stream is not duplexed.
3268 * f = IO.popen("/bin/sh","r+")
3269 * f.close_write
3270 * f.print "nowhere"
3272 * <em>produces:</em>
3274 * prog.rb:3:in `write': not opened for writing (IOError)
3275 * from prog.rb:3:in `print'
3276 * from prog.rb:3
3279 static VALUE
3280 rb_io_close_write(VALUE io)
3282 rb_io_t *fptr;
3283 VALUE write_io;
3285 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
3286 rb_raise(rb_eSecurityError, "Insecure: can't close");
3288 write_io = GetWriteIO(io);
3289 GetOpenFile(write_io, fptr);
3290 if (is_socket(fptr->fd, fptr->path)) {
3291 #ifndef SHUT_WR
3292 # define SHUT_WR 1
3293 #endif
3294 if (shutdown(fptr->fd, SHUT_WR) < 0)
3295 rb_sys_fail(fptr->path);
3296 fptr->mode &= ~FMODE_WRITABLE;
3297 if (!(fptr->mode & FMODE_READABLE))
3298 return rb_io_close(write_io);
3299 return Qnil;
3302 if (fptr->mode & FMODE_READABLE) {
3303 rb_raise(rb_eIOError, "closing non-duplex IO for writing");
3306 rb_io_close(write_io);
3307 if (io != write_io) {
3308 GetOpenFile(io, fptr);
3309 fptr->tied_io_for_writing = 0;
3310 fptr->mode &= ~FMODE_DUPLEX;
3312 return Qnil;
3316 * call-seq:
3317 * ios.sysseek(offset, whence=SEEK_SET) => integer
3319 * Seeks to a given <i>offset</i> in the stream according to the value
3320 * of <i>whence</i> (see <code>IO#seek</code> for values of
3321 * <i>whence</i>). Returns the new offset into the file.
3323 * f = File.new("testfile")
3324 * f.sysseek(-13, IO::SEEK_END) #=> 53
3325 * f.sysread(10) #=> "And so on."
3328 static VALUE
3329 rb_io_sysseek(int argc, VALUE *argv, VALUE io)
3331 VALUE offset, ptrname;
3332 int whence = SEEK_SET;
3333 rb_io_t *fptr;
3334 off_t pos;
3336 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
3337 whence = NUM2INT(ptrname);
3339 pos = NUM2OFFT(offset);
3340 GetOpenFile(io, fptr);
3341 if ((fptr->mode & FMODE_READABLE) && READ_DATA_BUFFERED(fptr)) {
3342 rb_raise(rb_eIOError, "sysseek for buffered IO");
3344 if ((fptr->mode & FMODE_WRITABLE) && fptr->wbuf_len) {
3345 rb_warn("sysseek for buffered IO");
3347 pos = lseek(fptr->fd, pos, whence);
3348 if (pos == -1) rb_sys_fail(fptr->path);
3350 return OFFT2NUM(pos);
3354 * call-seq:
3355 * ios.syswrite(string) => integer
3357 * Writes the given string to <em>ios</em> using a low-level write.
3358 * Returns the number of bytes written. Do not mix with other methods
3359 * that write to <em>ios</em> or you may get unpredictable results.
3360 * Raises <code>SystemCallError</code> on error.
3362 * f = File.new("out", "w")
3363 * f.syswrite("ABCDEF") #=> 6
3366 static VALUE
3367 rb_io_syswrite(VALUE io, VALUE str)
3369 rb_io_t *fptr;
3370 long n;
3372 rb_secure(4);
3373 if (TYPE(str) != T_STRING)
3374 str = rb_obj_as_string(str);
3376 io = GetWriteIO(io);
3377 GetOpenFile(io, fptr);
3378 rb_io_check_writable(fptr);
3380 if (fptr->wbuf_len) {
3381 rb_warn("syswrite for buffered IO");
3383 if (!rb_thread_fd_writable(fptr->fd)) {
3384 rb_io_check_closed(fptr);
3386 TRAP_BEG;
3387 n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
3388 TRAP_END;
3390 if (n == -1) rb_sys_fail(fptr->path);
3392 return LONG2FIX(n);
3396 * call-seq:
3397 * ios.sysread(integer[, outbuf]) => string
3399 * Reads <i>integer</i> bytes from <em>ios</em> using a low-level
3400 * read and returns them as a string. Do not mix with other methods
3401 * that read from <em>ios</em> or you may get unpredictable results.
3402 * If the optional <i>outbuf</i> argument is present, it must reference
3403 * a String, which will receive the data.
3404 * Raises <code>SystemCallError</code> on error and
3405 * <code>EOFError</code> at end of file.
3407 * f = File.new("testfile")
3408 * f.sysread(16) #=> "This is line one"
3411 static VALUE
3412 rb_io_sysread(int argc, VALUE *argv, VALUE io)
3414 VALUE len, str;
3415 rb_io_t *fptr;
3416 long n, ilen;
3418 rb_scan_args(argc, argv, "11", &len, &str);
3419 ilen = NUM2LONG(len);
3421 if (NIL_P(str)) {
3422 str = rb_str_new(0, ilen);
3424 else {
3425 StringValue(str);
3426 rb_str_modify(str);
3427 rb_str_resize(str, ilen);
3429 if (ilen == 0) return str;
3431 GetOpenFile(io, fptr);
3432 rb_io_check_readable(fptr);
3434 if (READ_DATA_BUFFERED(fptr)) {
3435 rb_raise(rb_eIOError, "sysread for buffered IO");
3438 n = fptr->fd;
3439 rb_thread_wait_fd(fptr->fd);
3440 rb_io_check_closed(fptr);
3441 if (RSTRING_LEN(str) != ilen) {
3442 rb_raise(rb_eRuntimeError, "buffer string modified");
3445 n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen);
3447 if (n == -1) {
3448 rb_sys_fail(fptr->path);
3450 rb_str_set_len(str, n);
3451 if (n == 0 && ilen > 0) {
3452 rb_eof_error();
3454 rb_str_resize(str, n);
3455 OBJ_TAINT(str);
3457 return str;
3460 VALUE
3461 rb_io_binmode(VALUE io)
3463 rb_io_t *fptr;
3465 GetOpenFile(io, fptr);
3466 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3467 if (!(fptr->mode & FMODE_BINMODE) && READ_DATA_BUFFERED(fptr)) {
3468 rb_raise(rb_eIOError, "buffer already filled with text-mode content");
3470 if (0 <= fptr->fd && setmode(fptr->fd, O_BINARY) == -1)
3471 rb_sys_fail(fptr->path);
3472 #endif
3473 fptr->mode |= FMODE_BINMODE;
3474 return io;
3478 * call-seq:
3479 * ios.binmode => ios
3481 * Puts <em>ios</em> into binary mode. This is useful only in
3482 * MS-DOS/Windows environments. Once a stream is in binary mode, it
3483 * cannot be reset to nonbinary mode.
3486 static VALUE
3487 rb_io_binmode_m(VALUE io)
3489 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3490 VALUE write_io;
3491 #endif
3493 rb_io_binmode(io);
3495 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3496 write_io = GetWriteIO(io);
3497 if (write_io != io)
3498 rb_io_binmode(write_io);
3499 #endif
3500 return io;
3504 * call-seq:
3505 * ios.binmode? => true or false
3507 * Returns <code>true</code> if <em>ios</em> is binmode.
3509 static VALUE
3510 rb_io_binmode_p(VALUE io)
3512 rb_io_t *fptr;
3513 GetOpenFile(io, fptr);
3514 return fptr->mode & FMODE_BINMODE ? Qtrue : Qfalse;
3517 static const char*
3518 rb_io_flags_mode(int flags)
3520 #ifdef O_BINARY
3521 # define MODE_BINMODE(a,b) ((flags & FMODE_BINMODE) ? (b) : (a))
3522 #else
3523 # define MODE_BINMODE(a,b) (a)
3524 #endif
3525 if (flags & FMODE_APPEND) {
3526 if ((flags & FMODE_READWRITE) == FMODE_READWRITE) {
3527 return MODE_BINMODE("a+", "ab+");
3529 return MODE_BINMODE("a", "ab");
3531 switch (flags & FMODE_READWRITE) {
3532 case FMODE_READABLE:
3533 return MODE_BINMODE("r", "rb");
3534 case FMODE_WRITABLE:
3535 return MODE_BINMODE("w", "wb");
3536 case FMODE_READWRITE:
3537 if (flags & FMODE_CREATE) {
3538 return MODE_BINMODE("w+", "wb+");
3540 return MODE_BINMODE("r+", "rb+");
3542 rb_raise(rb_eArgError, "invalid access modenum %o", flags);
3543 return NULL; /* not reached */
3547 rb_io_mode_flags(const char *mode)
3549 int flags = 0;
3550 const char *m = mode;
3552 switch (*m++) {
3553 case 'r':
3554 flags |= FMODE_READABLE;
3555 break;
3556 case 'w':
3557 flags |= FMODE_WRITABLE | FMODE_CREATE;
3558 break;
3559 case 'a':
3560 flags |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
3561 break;
3562 default:
3563 error:
3564 rb_raise(rb_eArgError, "invalid access mode %s", mode);
3567 while (*m) {
3568 switch (*m++) {
3569 case 'b':
3570 flags |= FMODE_BINMODE;
3571 break;
3572 case '+':
3573 flags |= FMODE_READWRITE;
3574 break;
3575 default:
3576 goto error;
3577 case ':':
3578 return flags;
3582 return flags;
3586 rb_io_modenum_flags(int mode)
3588 int flags = 0;
3590 switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
3591 case O_RDONLY:
3592 flags = FMODE_READABLE;
3593 break;
3594 case O_WRONLY:
3595 flags = FMODE_WRITABLE;
3596 break;
3597 case O_RDWR:
3598 flags = FMODE_READWRITE;
3599 break;
3602 if (mode & O_APPEND) {
3603 flags |= FMODE_APPEND;
3605 if (mode & O_CREAT) {
3606 flags |= FMODE_CREATE;
3608 #ifdef O_BINARY
3609 if (mode & O_BINARY) {
3610 flags |= FMODE_BINMODE;
3612 #endif
3614 return flags;
3618 rb_io_mode_modenum(const char *mode)
3620 int flags = 0;
3621 const char *m = mode;
3623 switch (*m++) {
3624 case 'r':
3625 flags |= O_RDONLY;
3626 break;
3627 case 'w':
3628 flags |= O_WRONLY | O_CREAT | O_TRUNC;
3629 break;
3630 case 'a':
3631 flags |= O_WRONLY | O_CREAT | O_APPEND;
3632 break;
3633 default:
3634 error:
3635 rb_raise(rb_eArgError, "invalid access mode %s", mode);
3638 while (*m) {
3639 switch (*m++) {
3640 case 'b':
3641 #ifdef O_BINARY
3642 flags |= O_BINARY;
3643 #endif
3644 break;
3645 case '+':
3646 flags = (flags & ~O_ACCMODE) | O_RDWR;
3647 break;
3648 default:
3649 goto error;
3650 case ':':
3651 return flags;
3655 return flags;
3658 #define MODENUM_MAX 4
3660 static const char*
3661 rb_io_modenum_mode(int flags)
3663 #ifdef O_BINARY
3664 # define MODE_BINARY(a,b) ((flags & O_BINARY) ? (b) : (a))
3665 #else
3666 # define MODE_BINARY(a,b) (a)
3667 #endif
3668 if (flags & O_APPEND) {
3669 if ((flags & O_RDWR) == O_RDWR) {
3670 return MODE_BINARY("a+", "ab+");
3672 return MODE_BINARY("a", "ab");
3674 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
3675 case O_RDONLY:
3676 return MODE_BINARY("r", "rb");
3677 case O_WRONLY:
3678 return MODE_BINARY("w", "wb");
3679 case O_RDWR:
3680 return MODE_BINARY("r+", "rb+");
3682 rb_raise(rb_eArgError, "invalid access modenum %o", flags);
3683 return NULL; /* not reached */
3686 static void
3687 mode_enc(rb_io_t *fptr, const char *estr)
3689 const char *p0, *p1;
3690 char *enc2name;
3691 int idx, idx2;
3693 /* parse estr as "enc" or "enc2:enc" */
3695 fptr->enc = 0;
3696 fptr->enc2 = 0;
3697 clear_codeconv(fptr);
3699 p0 = strrchr(estr, ':');
3700 if (!p0) p1 = estr;
3701 else p1 = p0 + 1;
3702 idx = rb_enc_find_index(p1);
3703 if (idx >= 0) {
3704 fptr->enc = rb_enc_from_index(idx);
3706 else {
3707 rb_warn("Unsupported encoding %s ignored", p1);
3710 if (fptr->enc && p0) {
3711 int n = p0 - estr;
3712 if (n > ENCODING_MAXNAMELEN) {
3713 idx2 = -1;
3715 else {
3716 enc2name = ALLOCA_N(char, n+1);
3717 memcpy(enc2name, estr, n);
3718 enc2name[n] = '\0';
3719 estr = enc2name;
3720 idx2 = rb_enc_find_index(enc2name);
3722 if (idx2 < 0) {
3723 rb_warn("Unsupported encoding %.*s ignored", n, estr);
3725 else if (idx2 == idx) {
3726 rb_warn("Ignoring internal encoding %.*s: it is identical to external encoding %s",
3727 n, estr, p1);
3729 else {
3730 fptr->enc2 = rb_enc_from_index(idx2);
3735 void
3736 rb_io_mode_enc(rb_io_t *fptr, const char *mode)
3738 const char *p = strchr(mode, ':');
3739 if (p) {
3740 mode_enc(fptr, p+1);
3744 struct sysopen_struct {
3745 char *fname;
3746 int flag;
3747 unsigned int mode;
3750 static VALUE
3751 sysopen_func(void *ptr)
3753 struct sysopen_struct *data = ptr;
3754 return (VALUE)open(data->fname, data->flag, data->mode);
3757 static int
3758 rb_sysopen_internal(char *fname, int flags, unsigned int mode)
3760 struct sysopen_struct data;
3761 data.fname = fname;
3762 data.flag = flags;
3763 data.mode = mode;
3764 return (int)rb_thread_blocking_region(sysopen_func, &data, RB_UBF_DFL, 0);
3767 static int
3768 rb_sysopen(char *fname, int flags, unsigned int mode)
3770 int fd;
3772 fd = rb_sysopen_internal(fname, flags, mode);
3773 if (fd < 0) {
3774 if (errno == EMFILE || errno == ENFILE) {
3775 rb_gc();
3776 fd = rb_sysopen_internal(fname, flags, mode);
3778 if (fd < 0) {
3779 rb_sys_fail(fname);
3782 UPDATE_MAXFD(fd);
3783 return fd;
3786 FILE *
3787 rb_fopen(const char *fname, const char *mode)
3789 FILE *file;
3791 file = fopen(fname, mode);
3792 if (!file) {
3793 if (errno == EMFILE || errno == ENFILE) {
3794 rb_gc();
3795 file = fopen(fname, mode);
3797 if (!file) {
3798 rb_sys_fail(fname);
3801 #ifdef USE_SETVBUF
3802 if (setvbuf(file, NULL, _IOFBF, 0) != 0)
3803 rb_warn("setvbuf() can't be honoured for %s", fname);
3804 #endif
3805 #ifdef __human68k__
3806 setmode(fileno(file), O_TEXT);
3807 #endif
3808 return file;
3811 FILE *
3812 rb_fdopen(int fd, const char *mode)
3814 FILE *file;
3816 #if defined(sun)
3817 errno = 0;
3818 #endif
3819 file = fdopen(fd, mode);
3820 if (!file) {
3821 if (
3822 #if defined(sun)
3823 errno == 0 ||
3824 #endif
3825 errno == EMFILE || errno == ENFILE) {
3826 rb_gc();
3827 #if defined(sun)
3828 errno = 0;
3829 #endif
3830 file = fdopen(fd, mode);
3832 if (!file) {
3833 #ifdef _WIN32
3834 if (errno == 0) errno = EINVAL;
3835 #elif defined(sun)
3836 if (errno == 0) errno = EMFILE;
3837 #endif
3838 rb_sys_fail(0);
3842 /* xxx: should be _IONBF? A buffer in FILE may have trouble. */
3843 #ifdef USE_SETVBUF
3844 if (setvbuf(file, NULL, _IOFBF, 0) != 0)
3845 rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
3846 #endif
3847 return file;
3850 static void
3851 io_check_tty(rb_io_t *fptr)
3853 if (isatty(fptr->fd))
3854 fptr->mode |= FMODE_TTY|FMODE_DUPLEX;
3857 static VALUE
3858 rb_file_open_internal(VALUE io, const char *fname, const char *mode)
3860 rb_io_t *fptr;
3862 MakeOpenFile(io, fptr);
3863 fptr->mode = rb_io_mode_flags(mode);
3864 rb_io_mode_enc(fptr, mode);
3865 fptr->path = strdup(fname);
3866 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(rb_io_flags_mode(fptr->mode)), 0666);
3867 io_check_tty(fptr);
3869 return io;
3872 VALUE
3873 rb_file_open(const char *fname, const char *mode)
3875 return rb_file_open_internal(io_alloc(rb_cFile), fname, mode);
3878 static VALUE
3879 rb_file_sysopen_internal(VALUE io, const char *fname, int flags, int mode)
3881 rb_io_t *fptr;
3883 MakeOpenFile(io, fptr);
3885 fptr->path = strdup(fname);
3886 fptr->mode = rb_io_modenum_flags(flags);
3887 fptr->fd = rb_sysopen(fptr->path, flags, mode);
3888 io_check_tty(fptr);
3890 return io;
3893 VALUE
3894 rb_file_sysopen(const char *fname, int flags, int mode)
3896 return rb_file_sysopen_internal(io_alloc(rb_cFile), fname, flags, mode);
3899 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
3900 static struct pipe_list {
3901 rb_io_t *fptr;
3902 struct pipe_list *next;
3903 } *pipe_list;
3905 static void
3906 pipe_add_fptr(rb_io_t *fptr)
3908 struct pipe_list *list;
3910 list = ALLOC(struct pipe_list);
3911 list->fptr = fptr;
3912 list->next = pipe_list;
3913 pipe_list = list;
3916 static void
3917 pipe_del_fptr(rb_io_t *fptr)
3919 struct pipe_list *list = pipe_list;
3920 struct pipe_list *tmp;
3922 if (list->fptr == fptr) {
3923 pipe_list = list->next;
3924 free(list);
3925 return;
3928 while (list->next) {
3929 if (list->next->fptr == fptr) {
3930 tmp = list->next;
3931 list->next = list->next->next;
3932 free(tmp);
3933 return;
3935 list = list->next;
3939 static void
3940 pipe_atexit(void)
3942 struct pipe_list *list = pipe_list;
3943 struct pipe_list *tmp;
3945 while (list) {
3946 tmp = list->next;
3947 rb_io_fptr_finalize(list->fptr);
3948 list = tmp;
3952 static void
3953 pipe_finalize(rb_io_t *fptr, int noraise)
3955 #if !defined(HAVE_FORK) && !defined(_WIN32)
3956 int status;
3957 if (fptr->stdio_file) {
3958 status = pclose(fptr->stdio_file);
3960 fptr->fd = -1;
3961 fptr->stdio_file = 0;
3962 #if defined DJGPP
3963 status <<= 8;
3964 #endif
3965 rb_last_status_set(status, fptr->pid);
3966 #else
3967 fptr_finalize(fptr, noraise);
3968 #endif
3969 pipe_del_fptr(fptr);
3971 #endif
3973 void
3974 rb_io_synchronized(rb_io_t *fptr)
3976 rb_io_check_initialized(fptr);
3977 fptr->mode |= FMODE_SYNC;
3980 void
3981 rb_io_unbuffered(rb_io_t *fptr)
3983 rb_io_synchronized(fptr);
3987 rb_pipe(int *pipes)
3989 int ret;
3990 ret = pipe(pipes);
3991 if (ret == -1) {
3992 if (errno == EMFILE || errno == ENFILE) {
3993 rb_gc();
3994 ret = pipe(pipes);
3997 if (ret == 0) {
3998 UPDATE_MAXFD(pipes[0]);
3999 UPDATE_MAXFD(pipes[1]);
4001 return ret;
4004 #ifdef HAVE_FORK
4005 struct popen_arg {
4006 struct rb_exec_arg *execp;
4007 int modef;
4008 int pair[2];
4009 int write_pair[2];
4012 static void
4013 popen_redirect(struct popen_arg *p)
4015 if ((p->modef & FMODE_READABLE) && (p->modef & FMODE_WRITABLE)) {
4016 close(p->write_pair[1]);
4017 if (p->write_pair[0] != 0) {
4018 dup2(p->write_pair[0], 0);
4019 close(p->write_pair[0]);
4021 close(p->pair[0]);
4022 if (p->pair[1] != 1) {
4023 dup2(p->pair[1], 1);
4024 close(p->pair[1]);
4027 else if (p->modef & FMODE_READABLE) {
4028 close(p->pair[0]);
4029 if (p->pair[1] != 1) {
4030 dup2(p->pair[1], 1);
4031 close(p->pair[1]);
4034 else {
4035 close(p->pair[1]);
4036 if (p->pair[0] != 0) {
4037 dup2(p->pair[0], 0);
4038 close(p->pair[0]);
4043 void
4044 rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
4046 int fd, ret;
4047 int max = max_file_descriptor;
4048 if (max < maxhint)
4049 max = maxhint;
4050 for (fd = lowfd; fd <= max; fd++) {
4051 if (!NIL_P(noclose_fds) &&
4052 RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd))))
4053 continue;
4054 #ifdef FD_CLOEXEC
4055 ret = fcntl(fd, F_GETFD);
4056 if (ret != -1 && !(ret & FD_CLOEXEC)) {
4057 fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
4059 #else
4060 close(fd);
4061 #endif
4065 static int
4066 popen_exec(void *pp)
4068 struct popen_arg *p = (struct popen_arg*)pp;
4070 rb_thread_atfork_before_exec();
4071 return rb_exec(p->execp);
4073 #endif
4075 static VALUE
4076 pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *mode)
4078 int modef = rb_io_mode_flags(mode);
4079 int pid = 0;
4080 rb_io_t *fptr;
4081 VALUE port;
4082 rb_io_t *write_fptr;
4083 VALUE write_port;
4084 #if defined(HAVE_FORK)
4085 int status;
4086 struct popen_arg arg;
4087 #elif defined(_WIN32)
4088 int openmode = rb_io_mode_modenum(mode);
4089 const char *exename = NULL;
4090 volatile VALUE cmdbuf;
4091 struct rb_exec_arg sarg;
4092 #endif
4093 FILE *fp = 0;
4094 int fd = -1;
4095 int write_fd = -1;
4096 const char *cmd = 0;
4097 int argc;
4098 VALUE *argv;
4100 if (prog)
4101 cmd = StringValueCStr(prog);
4103 if (!eargp) {
4104 /* fork : IO.popen("-") */
4105 argc = 0;
4106 argv = 0;
4108 else if (eargp->argc) {
4109 /* no shell : IO.popen([prog, arg0], arg1, ...) */
4110 argc = eargp->argc;
4111 argv = eargp->argv;
4113 else {
4114 /* with shell : IO.popen(prog) */
4115 argc = 0;
4116 argv = 0;
4119 #if defined(HAVE_FORK)
4120 arg.execp = eargp;
4121 arg.modef = modef;
4122 arg.pair[0] = arg.pair[1] = -1;
4123 arg.write_pair[0] = arg.write_pair[1] = -1;
4124 switch (modef & (FMODE_READABLE|FMODE_WRITABLE)) {
4125 case FMODE_READABLE|FMODE_WRITABLE:
4126 if (rb_pipe(arg.write_pair) < 0)
4127 rb_sys_fail(cmd);
4128 if (rb_pipe(arg.pair) < 0) {
4129 int e = errno;
4130 close(arg.write_pair[0]);
4131 close(arg.write_pair[1]);
4132 errno = e;
4133 rb_sys_fail(cmd);
4135 if (eargp) {
4136 rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.write_pair[0]));
4137 rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
4139 break;
4140 case FMODE_READABLE:
4141 if (rb_pipe(arg.pair) < 0)
4142 rb_sys_fail(cmd);
4143 if (eargp)
4144 rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
4145 break;
4146 case FMODE_WRITABLE:
4147 if (rb_pipe(arg.pair) < 0)
4148 rb_sys_fail(cmd);
4149 if (eargp)
4150 rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.pair[0]));
4151 break;
4152 default:
4153 rb_sys_fail(cmd);
4155 if (eargp) {
4156 rb_exec_arg_fixup(arg.execp);
4157 pid = rb_fork(&status, popen_exec, &arg, arg.execp->redirect_fds);
4159 else {
4160 fflush(stdin); /* is it really needed? */
4161 rb_io_flush(rb_stdout);
4162 rb_io_flush(rb_stderr);
4163 pid = rb_fork(&status, 0, 0, Qnil);
4164 if (pid == 0) { /* child */
4165 popen_redirect(&arg);
4166 rb_io_synchronized(RFILE(orig_stdout)->fptr);
4167 rb_io_synchronized(RFILE(orig_stderr)->fptr);
4168 return Qnil;
4172 /* parent */
4173 if (pid == -1) {
4174 int e = errno;
4175 close(arg.pair[0]);
4176 close(arg.pair[1]);
4177 if ((modef & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
4178 close(arg.write_pair[0]);
4179 close(arg.write_pair[1]);
4181 errno = e;
4182 rb_sys_fail(cmd);
4184 if ((modef & FMODE_READABLE) && (modef & FMODE_WRITABLE)) {
4185 close(arg.pair[1]);
4186 fd = arg.pair[0];
4187 close(arg.write_pair[0]);
4188 write_fd = arg.write_pair[1];
4190 else if (modef & FMODE_READABLE) {
4191 close(arg.pair[1]);
4192 fd = arg.pair[0];
4194 else {
4195 close(arg.pair[0]);
4196 fd = arg.pair[1];
4198 #elif defined(_WIN32)
4199 if (argc) {
4200 volatile VALUE argbuf;
4201 char **args;
4202 int i;
4204 if (argc >= FIXNUM_MAX / sizeof(char *)) {
4205 rb_raise(rb_eArgError, "too many arguments");
4207 argbuf = rb_str_tmp_new((argc+1) * sizeof(char *));
4208 args = (void *)RSTRING_PTR(argbuf);
4209 for (i = 0; i < argc; ++i) {
4210 args[i] = StringValueCStr(argv[i]);
4212 args[i] = NULL;
4213 exename = cmd;
4214 cmdbuf = rb_str_tmp_new(rb_w32_argv_size(args));
4215 cmd = rb_w32_join_argv(RSTRING_PTR(cmdbuf), args);
4216 rb_str_resize(argbuf, 0);
4218 if (eargp) {
4219 rb_exec_arg_fixup(eargp);
4220 rb_run_exec_options(eargp, &sarg);
4222 while ((pid = rb_w32_pipe_exec(cmd, exename, openmode, &fd, &write_fd)) == -1) {
4223 /* exec failed */
4224 switch (errno) {
4225 case EAGAIN:
4226 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4227 case EWOULDBLOCK:
4228 #endif
4229 rb_thread_sleep(1);
4230 break;
4231 default:
4232 if (eargp)
4233 rb_run_exec_options(&sarg, NULL);
4234 rb_sys_fail(cmd);
4235 break;
4238 if (eargp)
4239 rb_run_exec_options(&sarg, NULL);
4240 #else
4241 if (argc) {
4242 prog = rb_ary_join(rb_ary_new4(argc, argv), rb_str_new2(" "));
4243 cmd = StringValueCStr(prog);
4245 if (eargp) {
4246 rb_exec_arg_fixup(eargp);
4247 rb_run_exec_options(eargp, &sarg);
4249 fp = popen(cmd, mode);
4250 if (eargp)
4251 rb_run_exec_options(&sarg, NULL);
4252 if (!fp) rb_sys_fail(RSTRING_PTR(prog));
4253 fd = fileno(fp);
4254 #endif
4256 port = io_alloc(rb_cIO);
4257 MakeOpenFile(port, fptr);
4258 fptr->fd = fd;
4259 fptr->stdio_file = fp;
4260 fptr->mode = modef | FMODE_SYNC|FMODE_DUPLEX;
4261 rb_io_mode_enc(fptr, mode);
4262 fptr->pid = pid;
4264 if (0 <= write_fd) {
4265 write_port = io_alloc(rb_cIO);
4266 MakeOpenFile(write_port, write_fptr);
4267 write_fptr->fd = write_fd;
4268 write_fptr->mode = (modef & ~FMODE_READABLE)| FMODE_SYNC|FMODE_DUPLEX;
4269 fptr->mode &= ~FMODE_WRITABLE;
4270 fptr->tied_io_for_writing = write_port;
4271 rb_ivar_set(port, rb_intern("@tied_io_for_writing"), write_port);
4274 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
4275 fptr->finalize = pipe_finalize;
4276 pipe_add_fptr(fptr);
4277 #endif
4278 return port;
4281 static VALUE
4282 pipe_open_v(int argc, VALUE *argv, const char *mode)
4284 VALUE prog;
4285 struct rb_exec_arg earg;
4286 prog = rb_exec_arg_init(argc, argv, Qfalse, &earg);
4287 return pipe_open(&earg, prog, mode);
4290 static VALUE
4291 pipe_open_s(VALUE prog, const char *mode)
4293 const char *cmd = RSTRING_PTR(prog);
4294 int argc = 1;
4295 VALUE *argv = &prog;
4296 struct rb_exec_arg earg;
4298 if (RSTRING_LEN(prog) == 1 && cmd[0] == '-') {
4299 #if !defined(HAVE_FORK)
4300 rb_raise(rb_eNotImpError,
4301 "fork() function is unimplemented on this machine");
4302 #endif
4303 return pipe_open(0, 0, mode);
4306 rb_exec_arg_init(argc, argv, Qtrue, &earg);
4307 return pipe_open(&earg, prog, mode);
4311 * call-seq:
4312 * IO.popen(cmd, mode="r") => io
4313 * IO.popen(cmd, mode="r") {|io| block } => obj
4315 * Runs the specified command as a subprocess; the subprocess's
4316 * standard input and output will be connected to the returned
4317 * <code>IO</code> object. If _cmd_ is a +String+
4318 * ``<code>-</code>'', then a new instance of Ruby is started as the
4319 * subprocess. If <i>cmd</i> is an +Array+ of +String+, then it will
4320 * be used as the subprocess's +argv+ bypassing a shell.
4321 * The array can contains a hash at first for environments and
4322 * a hash at last for options similar to <code>spawn</code>. The default
4323 * mode for the new file object is ``r'', but <i>mode</i> may be set
4324 * to any of the modes listed in the description for class IO.
4326 * Raises exceptions which <code>IO::pipe</code> and
4327 * <code>Kernel::system</code> raise.
4329 * If a block is given, Ruby will run the command as a child connected
4330 * to Ruby with a pipe. Ruby's end of the pipe will be passed as a
4331 * parameter to the block.
4332 * At the end of block, Ruby close the pipe and sets <code>$?</code>.
4333 * In this case <code>IO::popen</code> returns
4334 * the value of the block.
4336 * If a block is given with a _cmd_ of ``<code>-</code>'',
4337 * the block will be run in two separate processes: once in the parent,
4338 * and once in a child. The parent process will be passed the pipe
4339 * object as a parameter to the block, the child version of the block
4340 * will be passed <code>nil</code>, and the child's standard in and
4341 * standard out will be connected to the parent through the pipe. Not
4342 * available on all platforms.
4344 * f = IO.popen("uname")
4345 * p f.readlines
4346 * puts "Parent is #{Process.pid}"
4347 * IO.popen("date") { |f| puts f.gets }
4348 * IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f}"}
4349 * p $?
4350 * IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
4351 * f.puts "bar"; f.close_write; puts f.gets
4354 * <em>produces:</em>
4356 * ["Linux\n"]
4357 * Parent is 26166
4358 * Wed Apr 9 08:53:52 CDT 2003
4359 * 26169 is here, f is
4360 * 26166 is here, f is #<IO:0x401b3d44>
4361 * #<Process::Status: pid=26166,exited(0)>
4362 * <foo>bar;zot;
4365 static VALUE
4366 rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
4368 const char *mode;
4369 VALUE pname, pmode, port, tmp;
4371 if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
4372 mode = "r";
4374 else if (FIXNUM_P(pmode)) {
4375 mode = rb_io_modenum_mode(FIX2INT(pmode));
4377 else {
4378 mode = StringValueCStr(pmode);
4380 tmp = rb_check_array_type(pname);
4381 if (!NIL_P(tmp)) {
4382 tmp = rb_ary_dup(tmp);
4383 RBASIC(tmp)->klass = 0;
4384 port = pipe_open_v(RARRAY_LEN(tmp), RARRAY_PTR(tmp), mode);
4385 rb_ary_clear(tmp);
4387 else {
4388 SafeStringValue(pname);
4389 port = pipe_open_s(pname, mode);
4391 if (NIL_P(port)) {
4392 /* child */
4393 if (rb_block_given_p()) {
4394 rb_yield(Qnil);
4395 rb_io_flush(rb_stdout);
4396 rb_io_flush(rb_stderr);
4397 _exit(0);
4399 return Qnil;
4401 RBASIC(port)->klass = klass;
4402 if (rb_block_given_p()) {
4403 return rb_ensure(rb_yield, port, io_close, port);
4405 return port;
4408 static void
4409 io_set_encoding(VALUE io, VALUE opt)
4411 rb_io_t *fptr;
4412 VALUE encoding=Qnil, extenc=Qnil, intenc=Qnil;
4413 if (!NIL_P(opt)) {
4414 VALUE v;
4415 v = rb_hash_aref(opt, sym_encoding);
4416 if (!NIL_P(v)) encoding = v;
4417 v = rb_hash_aref(opt, sym_extenc);
4418 if (!NIL_P(v)) extenc = v;
4419 v = rb_hash_aref(opt, sym_intenc);
4420 if (!NIL_P(v)) intenc = v;
4422 if (!NIL_P(extenc)) {
4423 rb_encoding *extencoding = rb_to_encoding(extenc);
4424 GetOpenFile(io, fptr);
4425 fptr->enc = 0;
4426 fptr->enc2 = 0;
4427 clear_codeconv(fptr);
4428 if (!NIL_P(encoding)) {
4429 rb_warn("Ignoring encoding parameter '%s': external_encoding is used",
4430 RSTRING_PTR(encoding));
4432 if (!NIL_P(intenc)) {
4433 rb_encoding *intencoding = rb_to_encoding(intenc);
4434 if (extencoding == intencoding) {
4435 rb_warn("Ignoring internal encoding '%s': it is identical to external encoding '%s'",
4436 RSTRING_PTR(rb_inspect(intenc)),
4437 RSTRING_PTR(rb_inspect(extenc)));
4439 else {
4440 fptr->enc2 = intencoding;
4443 fptr->enc = extencoding;
4445 else {
4446 if (!NIL_P(intenc)) {
4447 rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given");
4449 if (!NIL_P(encoding)) {
4450 GetOpenFile(io, fptr);
4451 mode_enc(fptr, StringValueCStr(encoding));
4456 static VALUE
4457 rb_open_file(int argc, VALUE *argv, VALUE io)
4459 VALUE opt=Qnil, fname, vmode, perm;
4460 const char *mode;
4461 int flags;
4462 unsigned int fmode;
4464 if (0 < argc) {
4465 opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
4466 if (!NIL_P(opt)) {
4467 VALUE v;
4468 v = rb_hash_aref(opt, sym_mode);
4469 if (!NIL_P(v)) vmode = v;
4470 v = rb_hash_aref(opt, sym_perm);
4471 if (!NIL_P(v)) perm = v;
4472 argc -= 1;
4476 rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
4477 #if defined _WIN32 || defined __APPLE__
4479 static rb_encoding *fs_encoding;
4480 rb_encoding *fname_encoding = rb_enc_get(fname);
4481 if (!fs_encoding)
4482 fs_encoding = rb_filesystem_encoding();
4483 if (rb_usascii_encoding() != fname_encoding
4484 && rb_ascii8bit_encoding() != fname_encoding
4485 #if defined __APPLE__
4486 && rb_utf8_encoding() != fname_encoding
4487 #endif
4488 && fs_encoding != fname_encoding) {
4489 static VALUE fs_enc;
4490 if (!fs_enc)
4491 fs_enc = rb_enc_from_encoding(fs_encoding);
4492 fname = rb_str_transcode(fname, fs_enc);
4495 #endif
4496 FilePathValue(fname);
4498 if (FIXNUM_P(vmode) || !NIL_P(perm)) {
4499 if (FIXNUM_P(vmode)) {
4500 flags = FIX2INT(vmode);
4502 else {
4503 SafeStringValue(vmode);
4504 flags = rb_io_mode_modenum(StringValueCStr(vmode));
4506 fmode = NIL_P(perm) ? 0666 : NUM2UINT(perm);
4508 rb_file_sysopen_internal(io, RSTRING_PTR(fname), flags, fmode);
4510 if (!FIXNUM_P(vmode)) {
4511 rb_io_t *fptr;
4512 GetOpenFile(io, fptr);
4513 rb_io_mode_enc(fptr, StringValueCStr(vmode));
4516 else {
4517 mode = NIL_P(vmode) ? "r" : StringValueCStr(vmode);
4518 rb_file_open_internal(io, RSTRING_PTR(fname), mode);
4521 io_set_encoding(io, opt);
4522 return io;
4526 * call-seq:
4527 * IO.open(fd, mode_string="r" ) => io
4528 * IO.open(fd, mode_string="r" ) {|io| block } => obj
4530 * With no associated block, <code>open</code> is a synonym for
4531 * <code>IO::new</code>. If the optional code block is given, it will
4532 * be passed <i>io</i> as an argument, and the IO object will
4533 * automatically be closed when the block terminates. In this instance,
4534 * <code>IO::open</code> returns the value of the block.
4538 static VALUE
4539 rb_io_s_open(int argc, VALUE *argv, VALUE klass)
4541 VALUE io = rb_class_new_instance(argc, argv, klass);
4543 if (rb_block_given_p()) {
4544 return rb_ensure(rb_yield, io, io_close, io);
4547 return io;
4551 * call-seq:
4552 * IO.sysopen(path, [mode, [perm]]) => fixnum
4554 * Opens the given path, returning the underlying file descriptor as a
4555 * <code>Fixnum</code>.
4557 * IO.sysopen("testfile") #=> 3
4561 static VALUE
4562 rb_io_s_sysopen(int argc, VALUE *argv)
4564 VALUE fname, vmode, perm;
4565 int flags, fd;
4566 unsigned int fmode;
4567 char *path;
4569 rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
4570 FilePathValue(fname);
4572 if (NIL_P(vmode)) flags = O_RDONLY;
4573 else if (FIXNUM_P(vmode)) flags = FIX2INT(vmode);
4574 else {
4575 SafeStringValue(vmode);
4576 flags = rb_io_mode_modenum(StringValueCStr(vmode));
4578 if (NIL_P(perm)) fmode = 0666;
4579 else fmode = NUM2UINT(perm);
4581 RB_GC_GUARD(fname) = rb_str_new4(fname);
4582 path = RSTRING_PTR(fname);
4583 fd = rb_sysopen(path, flags, fmode);
4584 return INT2NUM(fd);
4588 * call-seq:
4589 * open(path [, mode_enc [, perm]] ) => io or nil
4590 * open(path [, mode_enc [, perm]] ) {|io| block } => obj
4592 * Creates an <code>IO</code> object connected to the given stream,
4593 * file, or subprocess.
4595 * If <i>path</i> does not start with a pipe character
4596 * (``<code>|</code>''), treat it as the name of a file to open using
4597 * the specified mode (defaulting to ``<code>r</code>'').
4599 * The mode_enc is
4600 * either a string or an integer. If it is an integer, it must be
4601 * bitwise-or of open(2) flags, such as File::RDWR or File::EXCL.
4602 * If it is a string, it is either "mode", "mode:ext_enc", or
4603 * "mode:ext_enc:int_enc".
4604 * The mode is one of the following:
4606 * r: read (default)
4607 * w: write
4608 * a: append
4610 * The mode can be followed by "b" (means binary-mode), or "+"
4611 * (means both reading and writing allowed) or both.
4612 * If ext_enc (external encoding) is specified,
4613 * read string will be tagged by the encoding in reading,
4614 * and output string will be converted
4615 * to the specified encoding in writing.
4616 * If two encoding names,
4617 * ext_enc and int_enc (external encoding and internal encoding),
4618 * are specified, the read string is converted from ext_enc
4619 * to int_enc then tagged with the int_enc in read mode,
4620 * and in write mode, the output string will be
4621 * converted from int_enc to ext_enc before writing.
4623 * If a file is being created, its initial permissions may be
4624 * set using the integer third parameter.
4626 * If a block is specified, it will be invoked with the
4627 * <code>File</code> object as a parameter, and the file will be
4628 * automatically closed when the block terminates. The call
4629 * returns the value of the block.
4631 * If <i>path</i> starts with a pipe character, a subprocess is
4632 * created, connected to the caller by a pair of pipes. The returned
4633 * <code>IO</code> object may be used to write to the standard input
4634 * and read from the standard output of this subprocess. If the command
4635 * following the ``<code>|</code>'' is a single minus sign, Ruby forks,
4636 * and this subprocess is connected to the parent. In the subprocess,
4637 * the <code>open</code> call returns <code>nil</code>. If the command
4638 * is not ``<code>-</code>'', the subprocess runs the command. If a
4639 * block is associated with an <code>open("|-")</code> call, that block
4640 * will be run twice---once in the parent and once in the child. The
4641 * block parameter will be an <code>IO</code> object in the parent and
4642 * <code>nil</code> in the child. The parent's <code>IO</code> object
4643 * will be connected to the child's <code>$stdin</code> and
4644 * <code>$stdout</code>. The subprocess will be terminated at the end
4645 * of the block.
4647 * open("testfile") do |f|
4648 * print f.gets
4649 * end
4651 * <em>produces:</em>
4653 * This is line one
4655 * Open a subprocess and read its output:
4657 * cmd = open("|date")
4658 * print cmd.gets
4659 * cmd.close
4661 * <em>produces:</em>
4663 * Wed Apr 9 08:56:31 CDT 2003
4665 * Open a subprocess running the same Ruby program:
4667 * f = open("|-", "w+")
4668 * if f == nil
4669 * puts "in Child"
4670 * exit
4671 * else
4672 * puts "Got: #{f.gets}"
4673 * end
4675 * <em>produces:</em>
4677 * Got: in Child
4679 * Open a subprocess using a block to receive the I/O object:
4681 * open("|-") do |f|
4682 * if f == nil
4683 * puts "in Child"
4684 * else
4685 * puts "Got: #{f.gets}"
4686 * end
4687 * end
4689 * <em>produces:</em>
4691 * Got: in Child
4694 static VALUE
4695 rb_f_open(int argc, VALUE *argv)
4697 ID to_open = 0;
4698 int redirect = Qfalse;
4700 if (argc >= 1) {
4701 CONST_ID(to_open, "to_open");
4702 if (rb_respond_to(argv[0], to_open)) {
4703 redirect = Qtrue;
4705 else {
4706 VALUE tmp = argv[0];
4707 FilePathValue(tmp);
4708 if (NIL_P(tmp)) {
4709 redirect = Qtrue;
4711 else {
4712 char *str = StringValuePtr(tmp);
4713 if (str && str[0] == '|') {
4714 argv[0] = rb_str_new(str+1, RSTRING_LEN(tmp)-1);
4715 OBJ_INFECT(argv[0], tmp);
4716 return rb_io_s_popen(argc, argv, rb_cIO);
4721 if (redirect) {
4722 VALUE io = rb_funcall2(argv[0], to_open, argc-1, argv+1);
4724 if (rb_block_given_p()) {
4725 return rb_ensure(rb_yield, io, io_close, io);
4727 return io;
4729 return rb_io_s_open(argc, argv, rb_cFile);
4732 static VALUE
4733 rb_io_open(const char *fname, const char *mode)
4735 if (fname[0] == '|') {
4736 VALUE cmd = rb_str_new2(fname+1);
4737 return pipe_open_s(cmd, mode);
4739 else {
4740 return rb_file_open(fname, mode);
4744 static VALUE
4745 rb_io_open_with_args(int argc, VALUE *argv)
4747 const char *mode;
4748 VALUE pname, pmode;
4750 if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
4751 mode = "r";
4753 else if (FIXNUM_P(pmode)) {
4754 mode = rb_io_modenum_mode(FIX2INT(pmode));
4756 else {
4757 mode = StringValueCStr(pmode);
4759 return rb_io_open(StringValueCStr(pname), mode);
4762 static VALUE
4763 io_reopen(VALUE io, VALUE nfile)
4765 rb_io_t *fptr, *orig;
4766 int fd, fd2;
4767 off_t pos = 0;
4769 nfile = rb_io_get_io(nfile);
4770 if (rb_safe_level() >= 4 &&
4771 (!OBJ_UNTRUSTED(io) || !OBJ_UNTRUSTED(nfile))) {
4772 rb_raise(rb_eSecurityError, "Insecure: can't reopen");
4774 GetOpenFile(io, fptr);
4775 GetOpenFile(nfile, orig);
4777 if (fptr == orig) return io;
4778 if (IS_PREP_STDIO(fptr)) {
4779 if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
4780 (fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
4781 (fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
4782 rb_raise(rb_eArgError,
4783 "%s can't change access mode from \"%s\" to \"%s\"",
4784 PREP_STDIO_NAME(fptr), rb_io_flags_mode(fptr->mode),
4785 rb_io_flags_mode(orig->mode));
4788 if (orig->mode & FMODE_READABLE) {
4789 pos = io_tell(orig);
4791 if (orig->mode & FMODE_WRITABLE) {
4792 io_fflush(orig);
4794 if (fptr->mode & FMODE_WRITABLE) {
4795 io_fflush(fptr);
4798 /* copy rb_io_t structure */
4799 fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
4800 fptr->pid = orig->pid;
4801 fptr->lineno = orig->lineno;
4802 if (fptr->path) free(fptr->path);
4803 if (orig->path) fptr->path = strdup(orig->path);
4804 else fptr->path = 0;
4805 fptr->finalize = orig->finalize;
4807 fd = fptr->fd;
4808 fd2 = orig->fd;
4809 if (fd != fd2) {
4810 if (IS_PREP_STDIO(fptr)) {
4811 /* need to keep stdio objects */
4812 if (dup2(fd2, fd) < 0)
4813 rb_sys_fail(orig->path);
4815 else {
4816 if (fptr->stdio_file)
4817 fclose(fptr->stdio_file);
4818 else
4819 close(fptr->fd);
4820 fptr->stdio_file = 0;
4821 fptr->fd = -1;
4822 if (dup2(fd2, fd) < 0)
4823 rb_sys_fail(orig->path);
4824 fptr->fd = fd;
4826 rb_thread_fd_close(fd);
4827 if ((orig->mode & FMODE_READABLE) && pos >= 0) {
4828 if (io_seek(fptr, pos, SEEK_SET) < 0) {
4829 rb_sys_fail(fptr->path);
4831 if (io_seek(orig, pos, SEEK_SET) < 0) {
4832 rb_sys_fail(orig->path);
4837 if (fptr->mode & FMODE_BINMODE) {
4838 rb_io_binmode(io);
4841 RBASIC(io)->klass = rb_obj_class(nfile);
4842 return io;
4846 * call-seq:
4847 * ios.reopen(other_IO) => ios
4848 * ios.reopen(path, mode_str) => ios
4850 * Reassociates <em>ios</em> with the I/O stream given in
4851 * <i>other_IO</i> or to a new stream opened on <i>path</i>. This may
4852 * dynamically change the actual class of this stream.
4854 * f1 = File.new("testfile")
4855 * f2 = File.new("testfile")
4856 * f2.readlines[0] #=> "This is line one\n"
4857 * f2.reopen(f1) #=> #<File:testfile>
4858 * f2.readlines[0] #=> "This is line one\n"
4861 static VALUE
4862 rb_io_reopen(int argc, VALUE *argv, VALUE file)
4864 VALUE fname, nmode;
4865 const char *mode;
4866 rb_io_t *fptr;
4868 rb_secure(4);
4869 if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
4870 VALUE tmp = rb_io_check_io(fname);
4871 if (!NIL_P(tmp)) {
4872 return io_reopen(file, tmp);
4876 FilePathValue(fname);
4877 rb_io_taint_check(file);
4878 fptr = RFILE(file)->fptr;
4879 if (!fptr) {
4880 fptr = RFILE(file)->fptr = ALLOC(rb_io_t);
4881 MEMZERO(fptr, rb_io_t, 1);
4884 if (!NIL_P(nmode)) {
4885 int flags = rb_io_mode_flags(StringValueCStr(nmode));
4886 if (IS_PREP_STDIO(fptr) &&
4887 ((fptr->mode & FMODE_READWRITE) & (flags & FMODE_READWRITE)) !=
4888 (fptr->mode & FMODE_READWRITE)) {
4889 rb_raise(rb_eArgError,
4890 "%s can't change access mode from \"%s\" to \"%s\"",
4891 PREP_STDIO_NAME(fptr), rb_io_flags_mode(fptr->mode),
4892 rb_io_flags_mode(flags));
4894 fptr->mode = flags;
4895 rb_io_mode_enc(fptr, StringValueCStr(nmode));
4898 if (fptr->path) {
4899 free(fptr->path);
4900 fptr->path = 0;
4903 fptr->path = strdup(StringValueCStr(fname));
4904 mode = rb_io_flags_mode(fptr->mode);
4905 if (fptr->fd < 0) {
4906 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(mode), 0666);
4907 fptr->stdio_file = 0;
4908 return file;
4911 if (fptr->mode & FMODE_WRITABLE) {
4912 io_fflush(fptr);
4914 fptr->rbuf_off = fptr->rbuf_len = 0;
4916 if (fptr->stdio_file) {
4917 if (freopen(fptr->path, mode, fptr->stdio_file) == 0) {
4918 rb_sys_fail(fptr->path);
4920 fptr->fd = fileno(fptr->stdio_file);
4921 #ifdef USE_SETVBUF
4922 if (setvbuf(fptr->stdio_file, NULL, _IOFBF, 0) != 0)
4923 rb_warn("setvbuf() can't be honoured for %s", fptr->path);
4924 #endif
4926 else {
4927 if (close(fptr->fd) < 0)
4928 rb_sys_fail(fptr->path);
4929 fptr->fd = -1;
4930 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(mode), 0666);
4933 return file;
4936 /* :nodoc: */
4937 static VALUE
4938 rb_io_init_copy(VALUE dest, VALUE io)
4940 rb_io_t *fptr, *orig;
4941 int fd;
4942 VALUE write_io;
4944 io = rb_io_get_io(io);
4945 if (dest == io) return dest;
4946 GetOpenFile(io, orig);
4947 MakeOpenFile(dest, fptr);
4949 rb_io_flush(io);
4951 /* copy rb_io_t structure */
4952 fptr->mode = orig->mode & ~FMODE_PREP;
4953 fptr->pid = orig->pid;
4954 fptr->lineno = orig->lineno;
4955 if (orig->path) fptr->path = strdup(orig->path);
4956 fptr->finalize = orig->finalize;
4958 fd = ruby_dup(orig->fd);
4959 fptr->fd = fd;
4960 io_seek(fptr, io_tell(orig), SEEK_SET);
4961 if (fptr->mode & FMODE_BINMODE) {
4962 rb_io_binmode(dest);
4965 write_io = GetWriteIO(io);
4966 if (io != write_io) {
4967 write_io = rb_obj_dup(write_io);
4968 fptr->tied_io_for_writing = write_io;
4969 rb_ivar_set(dest, rb_intern("@tied_io_for_writing"), write_io);
4972 return dest;
4976 * call-seq:
4977 * ios.printf(format_string [, obj, ...] ) => nil
4979 * Formats and writes to <em>ios</em>, converting parameters under
4980 * control of the format string. See <code>Kernel#sprintf</code>
4981 * for details.
4984 VALUE
4985 rb_io_printf(int argc, VALUE *argv, VALUE out)
4987 rb_io_write(out, rb_f_sprintf(argc, argv));
4988 return Qnil;
4992 * call-seq:
4993 * printf(io, string [, obj ... ] ) => nil
4994 * printf(string [, obj ... ] ) => nil
4996 * Equivalent to:
4997 * io.write(sprintf(string, obj, ...)
4998 * or
4999 * $stdout.write(sprintf(string, obj, ...)
5002 static VALUE
5003 rb_f_printf(int argc, VALUE *argv)
5005 VALUE out;
5007 if (argc == 0) return Qnil;
5008 if (TYPE(argv[0]) == T_STRING) {
5009 out = rb_stdout;
5011 else {
5012 out = argv[0];
5013 argv++;
5014 argc--;
5016 rb_io_write(out, rb_f_sprintf(argc, argv));
5018 return Qnil;
5022 * call-seq:
5023 * ios.print() => nil
5024 * ios.print(obj, ...) => nil
5026 * Writes the given object(s) to <em>ios</em>. The stream must be
5027 * opened for writing. If the output record separator (<code>$\\</code>)
5028 * is not <code>nil</code>, it will be appended to the output. If no
5029 * arguments are given, prints <code>$_</code>. Objects that aren't
5030 * strings will be converted by calling their <code>to_s</code> method.
5031 * With no argument, prints the contents of the variable <code>$_</code>.
5032 * Returns <code>nil</code>.
5034 * $stdout.print("This is ", 100, " percent.\n")
5036 * <em>produces:</em>
5038 * This is 100 percent.
5041 VALUE
5042 rb_io_print(int argc, VALUE *argv, VALUE out)
5044 int i;
5045 VALUE line;
5047 /* if no argument given, print `$_' */
5048 if (argc == 0) {
5049 argc = 1;
5050 line = rb_lastline_get();
5051 argv = &line;
5053 for (i=0; i<argc; i++) {
5054 rb_io_write(out, argv[i]);
5055 if (!NIL_P(rb_output_fs)) {
5056 rb_io_write(out, rb_output_fs);
5059 if (argc > 0 && !NIL_P(rb_output_rs)) {
5060 rb_io_write(out, rb_output_rs);
5063 return Qnil;
5067 * call-seq:
5068 * print(obj, ...) => nil
5070 * Prints each object in turn to <code>$stdout</code>. If the output
5071 * field separator (<code>$,</code>) is not +nil+, its
5072 * contents will appear between each field. If the output record
5073 * separator (<code>$\\</code>) is not +nil+, it will be
5074 * appended to the output. If no arguments are given, prints
5075 * <code>$_</code>. Objects that aren't strings will be converted by
5076 * calling their <code>to_s</code> method.
5078 * print "cat", [1,2,3], 99, "\n"
5079 * $, = ", "
5080 * $\ = "\n"
5081 * print "cat", [1,2,3], 99
5083 * <em>produces:</em>
5085 * cat12399
5086 * cat, 1, 2, 3, 99
5089 static VALUE
5090 rb_f_print(int argc, VALUE *argv)
5092 rb_io_print(argc, argv, rb_stdout);
5093 return Qnil;
5097 * call-seq:
5098 * ios.putc(obj) => obj
5100 * If <i>obj</i> is <code>Numeric</code>, write the character whose
5101 * code is <i>obj</i>, otherwise write the first character of the
5102 * string representation of <i>obj</i> to <em>ios</em>.
5104 * $stdout.putc "A"
5105 * $stdout.putc 65
5107 * <em>produces:</em>
5109 * AA
5112 static VALUE
5113 rb_io_putc(VALUE io, VALUE ch)
5115 char c = NUM2CHR(ch);
5117 rb_io_write(io, rb_str_new(&c, 1));
5118 return ch;
5122 * call-seq:
5123 * putc(int) => int
5125 * Equivalent to:
5127 * $stdout.putc(int)
5130 static VALUE
5131 rb_f_putc(VALUE recv, VALUE ch)
5133 if (recv == rb_stdout) {
5134 return rb_io_putc(recv, ch);
5136 return rb_funcall2(rb_stdout, rb_intern("putc"), 1, &ch);
5139 static VALUE
5140 io_puts_ary(VALUE ary, VALUE out, int recur)
5142 VALUE tmp;
5143 long i;
5145 if (recur) {
5146 tmp = rb_str_new2("[...]");
5147 rb_io_puts(1, &tmp, out);
5148 return Qnil;
5150 for (i=0; i<RARRAY_LEN(ary); i++) {
5151 tmp = RARRAY_PTR(ary)[i];
5152 rb_io_puts(1, &tmp, out);
5154 return Qnil;
5158 * call-seq:
5159 * ios.puts(obj, ...) => nil
5161 * Writes the given objects to <em>ios</em> as with
5162 * <code>IO#print</code>. Writes a record separator (typically a
5163 * newline) after any that do not already end with a newline sequence.
5164 * If called with an array argument, writes each element on a new line.
5165 * If called without arguments, outputs a single record separator.
5167 * $stdout.puts("this", "is", "a", "test")
5169 * <em>produces:</em>
5171 * this
5172 * is
5174 * test
5177 VALUE
5178 rb_io_puts(int argc, VALUE *argv, VALUE out)
5180 int i;
5181 VALUE line;
5183 /* if no argument given, print newline. */
5184 if (argc == 0) {
5185 rb_io_write(out, rb_default_rs);
5186 return Qnil;
5188 for (i=0; i<argc; i++) {
5189 line = rb_check_array_type(argv[i]);
5190 if (!NIL_P(line)) {
5191 rb_exec_recursive(io_puts_ary, line, out);
5192 continue;
5194 line = rb_obj_as_string(argv[i]);
5195 rb_io_write(out, line);
5196 if (RSTRING_LEN(line) == 0 ||
5197 RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') {
5198 rb_io_write(out, rb_default_rs);
5202 return Qnil;
5206 * call-seq:
5207 * puts(obj, ...) => nil
5209 * Equivalent to
5211 * $stdout.puts(obj, ...)
5214 static VALUE
5215 rb_f_puts(int argc, VALUE *argv, VALUE recv)
5217 if (recv == rb_stdout) {
5218 return rb_io_puts(argc, argv, recv);
5220 return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv);
5223 void
5224 rb_p(VALUE obj) /* for debug print within C code */
5226 VALUE str = rb_obj_as_string(rb_inspect(obj));
5227 rb_str_buf_append(str, rb_default_rs);
5228 rb_io_write(rb_stdout, str);
5232 * call-seq:
5233 * p(obj) => obj
5234 * p(obj1, obj2, ...) => [obj, ...]
5235 * p() => nil
5237 * For each object, directly writes
5238 * _obj_.+inspect+ followed by the current output
5239 * record separator to the program's standard output.
5241 * S = Struct.new(:name, :state)
5242 * s = S['dave', 'TX']
5243 * p s
5245 * <em>produces:</em>
5247 * #<S name="dave", state="TX">
5250 static VALUE
5251 rb_f_p(int argc, VALUE *argv, VALUE self)
5253 int i;
5254 VALUE ret = Qnil;
5256 for (i=0; i<argc; i++) {
5257 rb_p(argv[i]);
5259 if (argc == 1) {
5260 ret = argv[0];
5262 else if (argc > 1) {
5263 ret = rb_ary_new4(argc, argv);
5265 if (TYPE(rb_stdout) == T_FILE) {
5266 rb_io_flush(rb_stdout);
5268 return ret;
5272 * call-seq:
5273 * obj.display(port=$>) => nil
5275 * Prints <i>obj</i> on the given port (default <code>$></code>).
5276 * Equivalent to:
5278 * def display(port=$>)
5279 * port.write self
5280 * end
5282 * For example:
5284 * 1.display
5285 * "cat".display
5286 * [ 4, 5, 6 ].display
5287 * puts
5289 * <em>produces:</em>
5291 * 1cat456
5294 static VALUE
5295 rb_obj_display(int argc, VALUE *argv, VALUE self)
5297 VALUE out;
5299 if (argc == 0) {
5300 out = rb_stdout;
5302 else {
5303 rb_scan_args(argc, argv, "01", &out);
5305 rb_io_write(out, self);
5307 return Qnil;
5310 void
5311 rb_write_error2(const char *mesg, long len)
5313 if (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0) {
5314 fwrite(mesg, sizeof(char), len, stderr);
5316 else {
5317 rb_io_write(rb_stderr, rb_str_new(mesg, len));
5321 void
5322 rb_write_error(const char *mesg)
5324 rb_write_error2(mesg, strlen(mesg));
5327 static void
5328 must_respond_to(ID mid, VALUE val, ID id)
5330 if (!rb_respond_to(val, mid)) {
5331 rb_raise(rb_eTypeError, "%s must have %s method, %s given",
5332 rb_id2name(id), rb_id2name(mid),
5333 rb_obj_classname(val));
5337 static void
5338 stdout_setter(VALUE val, ID id, VALUE *variable)
5340 must_respond_to(id_write, val, id);
5341 *variable = val;
5344 static VALUE
5345 prep_io(int fd, int mode, VALUE klass, const char *path)
5347 rb_io_t *fp;
5348 VALUE io = io_alloc(klass);
5350 MakeOpenFile(io, fp);
5351 fp->fd = fd;
5352 #ifdef __CYGWIN__
5353 if (!isatty(fd)) {
5354 mode |= O_BINARY;
5355 setmode(fd, O_BINARY);
5357 #endif
5358 fp->mode = mode;
5359 io_check_tty(fp);
5360 if (path) fp->path = strdup(path);
5362 return io;
5365 VALUE
5366 rb_io_fdopen(int fd, int mode, const char *path)
5368 VALUE klass = rb_cIO;
5370 if (path && strcmp(path, "-")) klass = rb_cFile;
5371 return prep_io(fd, rb_io_modenum_flags(mode), klass, path);
5374 static VALUE
5375 prep_stdio(FILE *f, int mode, VALUE klass, const char *path)
5377 rb_io_t *fptr;
5378 VALUE io = prep_io(fileno(f), mode|FMODE_PREP, klass, path);
5380 GetOpenFile(io, fptr);
5381 fptr->stdio_file = f;
5383 return io;
5386 FILE *
5387 rb_io_stdio_file(rb_io_t *fptr)
5389 if (!fptr->stdio_file) {
5390 fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_flags_mode(fptr->mode));
5392 return fptr->stdio_file;
5396 * call-seq:
5397 * IO.new(fd, mode) => io
5399 * Returns a new <code>IO</code> object (a stream) for the given
5400 * <code>IO</code> object or integer file descriptor and mode
5401 * string. See also <code>IO#fileno</code> and
5402 * <code>IO::for_fd</code>.
5404 * puts IO.new($stdout).fileno # => 1
5406 * a = IO.new(2,"w") # '2' is standard error
5407 * $stderr.puts "Hello"
5408 * a.puts "World"
5410 * <em>produces:</em>
5412 * Hello
5413 * World
5416 static VALUE
5417 rb_io_initialize(int argc, VALUE *argv, VALUE io)
5419 VALUE fnum, mode, orig;
5420 rb_io_t *fp, *ofp = NULL;
5421 int fd, fmode, flags = O_RDONLY;
5423 rb_secure(4);
5424 rb_scan_args(argc, argv, "11", &fnum, &mode);
5425 if (argc == 2) {
5426 if (FIXNUM_P(mode)) {
5427 flags = FIX2LONG(mode);
5429 else {
5430 SafeStringValue(mode);
5431 flags = rb_io_mode_modenum(StringValueCStr(mode));
5434 orig = rb_io_check_io(fnum);
5435 if (NIL_P(orig)) {
5436 fd = NUM2INT(fnum);
5437 UPDATE_MAXFD(fd);
5438 if (argc != 2) {
5439 #if defined(HAVE_FCNTL) && defined(F_GETFL)
5440 flags = fcntl(fd, F_GETFL);
5441 if (flags == -1) rb_sys_fail(0);
5442 #endif
5444 MakeOpenFile(io, fp);
5445 fp->fd = fd;
5446 fp->mode = rb_io_modenum_flags(flags);
5447 io_check_tty(fp);
5449 else if (RFILE(io)->fptr) {
5450 rb_raise(rb_eRuntimeError, "reinitializing IO");
5452 else {
5453 GetOpenFile(orig, ofp);
5454 if (ofp->refcnt == LONG_MAX) {
5455 VALUE s = rb_inspect(orig);
5456 rb_raise(rb_eIOError, "too many shared IO for %s", StringValueCStr(s));
5458 if (argc == 2) {
5459 fmode = rb_io_modenum_flags(flags);
5460 if ((ofp->mode ^ fmode) & (FMODE_READWRITE|FMODE_BINMODE)) {
5461 if (FIXNUM_P(mode)) {
5462 rb_raise(rb_eArgError, "incompatible mode 0%o", flags);
5464 else {
5465 rb_raise(rb_eArgError, "incompatible mode \"%s\"", RSTRING_PTR(mode));
5469 ofp->refcnt++;
5470 RFILE(io)->fptr = ofp;
5473 return io;
5478 * call-seq:
5479 * File.new(filename, mode="r") => file
5480 * File.new(filename [, mode [, perm]]) => file
5483 * Opens the file named by _filename_ according to
5484 * _mode_ (default is ``r'') and returns a new
5485 * <code>File</code> object. See the description of class +IO+ for
5486 * a description of _mode_. The file mode may optionally be
5487 * specified as a +Fixnum+ by _or_-ing together the
5488 * flags (O_RDONLY etc, again described under +IO+). Optional
5489 * permission bits may be given in _perm_. These mode and permission
5490 * bits are platform dependent; on Unix systems, see
5491 * <code>open(2)</code> for details.
5493 * f = File.new("testfile", "r")
5494 * f = File.new("newfile", "w+")
5495 * f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
5498 static VALUE
5499 rb_file_initialize(int argc, VALUE *argv, VALUE io)
5501 if (RFILE(io)->fptr) {
5502 rb_raise(rb_eRuntimeError, "reinitializing File");
5504 if (0 < argc && argc < 3) {
5505 VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
5507 if (!NIL_P(fd)) {
5508 argv[0] = fd;
5509 return rb_io_initialize(argc, argv, io);
5512 rb_open_file(argc, argv, io);
5514 return io;
5518 * call-seq:
5519 * IO.new(fd, mode_string) => io
5521 * Returns a new <code>IO</code> object (a stream) for the given
5522 * integer file descriptor and mode string. See also
5523 * <code>IO#fileno</code> and <code>IO::for_fd</code>.
5525 * a = IO.new(2,"w") # '2' is standard error
5526 * $stderr.puts "Hello"
5527 * a.puts "World"
5529 * <em>produces:</em>
5531 * Hello
5532 * World
5535 static VALUE
5536 rb_io_s_new(int argc, VALUE *argv, VALUE klass)
5538 if (rb_block_given_p()) {
5539 const char *cname = rb_class2name(klass);
5541 rb_warn("%s::new() does not take block; use %s::open() instead",
5542 cname, cname);
5544 return rb_class_new_instance(argc, argv, klass);
5549 * call-seq:
5550 * IO.for_fd(fd, mode) => io
5552 * Synonym for <code>IO::new</code>.
5556 static VALUE
5557 rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
5559 VALUE io = rb_obj_alloc(klass);
5560 rb_io_initialize(argc, argv, io);
5561 return io;
5564 static void
5565 argf_mark(void *ptr)
5567 struct argf *p = ptr;
5568 rb_gc_mark(p->filename);
5569 rb_gc_mark(p->current_file);
5570 rb_gc_mark(p->lineno);
5571 rb_gc_mark(p->argv);
5574 static void
5575 argf_free(void *ptr)
5577 struct argf *p = ptr;
5578 free(p->inplace);
5581 static inline void
5582 argf_init(struct argf *p, VALUE v)
5584 p->filename = Qnil;
5585 p->current_file = Qnil;
5586 p->lineno = Qnil;
5587 p->argv = v;
5590 static VALUE
5591 argf_alloc(VALUE klass)
5593 struct argf *p;
5594 VALUE argf = Data_Make_Struct(klass, struct argf, argf_mark, argf_free, p);
5596 argf_init(p, Qnil);
5597 return argf;
5600 #undef rb_argv
5601 #define filename ARGF.filename
5602 #define current_file ARGF.current_file
5603 #define gets_lineno ARGF.gets_lineno
5604 #define init_p ARGF.init_p
5605 #define next_p ARGF.next_p
5606 #define lineno ARGF.lineno
5607 #define ruby_inplace_mode ARGF.inplace
5608 #define argf_binmode ARGF.binmode
5609 #define argf_enc ARGF.enc
5610 #define argf_enc2 ARGF.enc2
5611 #define rb_argv ARGF.argv
5613 static VALUE
5614 argf_initialize(VALUE argf, VALUE argv)
5616 memset(&ARGF, 0, sizeof(ARGF));
5617 argf_init(&ARGF, argv);
5619 return argf;
5622 static VALUE
5623 argf_initialize_copy(VALUE argf, VALUE orig)
5625 ARGF = argf_of(orig);
5626 rb_argv = rb_obj_dup(rb_argv);
5627 if (ARGF.inplace) {
5628 const char *inplace = ARGF.inplace;
5629 ARGF.inplace = 0;
5630 ARGF.inplace = ruby_strdup(inplace);
5632 return argf;
5635 static VALUE
5636 argf_set_lineno(VALUE argf, VALUE val)
5638 gets_lineno = NUM2INT(val);
5639 lineno = INT2FIX(gets_lineno);
5640 return Qnil;
5643 static VALUE
5644 argf_lineno(VALUE argf)
5646 return lineno;
5649 static VALUE
5650 argf_forward(int argc, VALUE *argv, VALUE argf)
5652 return rb_funcall3(current_file, rb_frame_this_func(), argc, argv);
5655 #define next_argv() argf_next_argv(argf)
5656 #define ARGF_GENERIC_INPUT_P() \
5657 (current_file == rb_stdin && TYPE(current_file) != T_FILE)
5658 #define ARGF_FORWARD(argc, argv) do {\
5659 if (ARGF_GENERIC_INPUT_P())\
5660 return argf_forward(argc, argv, argf);\
5661 } while (0)
5662 #define NEXT_ARGF_FORWARD(argc, argv) do {\
5663 if (!next_argv()) return Qnil;\
5664 ARGF_FORWARD(argc, argv);\
5665 } while (0)
5667 static void
5668 argf_close(VALUE file)
5670 rb_funcall3(file, rb_intern("close"), 0, 0);
5673 static int
5674 argf_next_argv(VALUE argf)
5676 char *fn;
5677 rb_io_t *fptr;
5678 int stdout_binmode = 0;
5680 if (TYPE(rb_stdout) == T_FILE) {
5681 GetOpenFile(rb_stdout, fptr);
5682 if (fptr->mode & FMODE_BINMODE)
5683 stdout_binmode = 1;
5686 if (init_p == 0) {
5687 if (!NIL_P(rb_argv) && RARRAY_LEN(rb_argv) > 0) {
5688 next_p = 1;
5690 else {
5691 next_p = -1;
5693 init_p = 1;
5694 gets_lineno = 0;
5697 if (next_p == 1) {
5698 next_p = 0;
5699 retry:
5700 if (RARRAY_LEN(rb_argv) > 0) {
5701 filename = rb_ary_shift(rb_argv);
5702 fn = StringValueCStr(filename);
5703 if (strlen(fn) == 1 && fn[0] == '-') {
5704 current_file = rb_stdin;
5705 if (ruby_inplace_mode) {
5706 rb_warn("Can't do inplace edit for stdio; skipping");
5707 goto retry;
5710 else {
5711 int fr = rb_sysopen(fn, O_RDONLY, 0);
5713 if (ruby_inplace_mode) {
5714 struct stat st;
5715 #ifndef NO_SAFE_RENAME
5716 struct stat st2;
5717 #endif
5718 VALUE str;
5719 int fw;
5721 if (TYPE(rb_stdout) == T_FILE && rb_stdout != orig_stdout) {
5722 rb_io_close(rb_stdout);
5724 fstat(fr, &st);
5725 if (*ruby_inplace_mode) {
5726 str = rb_str_new2(fn);
5727 #ifdef NO_LONG_FNAME
5728 ruby_add_suffix(str, ruby_inplace_mode);
5729 #else
5730 rb_str_cat2(str, ruby_inplace_mode);
5731 #endif
5732 #ifdef NO_SAFE_RENAME
5733 (void)close(fr);
5734 (void)unlink(RSTRING_PTR(str));
5735 (void)rename(fn, RSTRING_PTR(str));
5736 fr = rb_sysopen(RSTRING_PTR(str), O_RDONLY, 0);
5737 #else
5738 if (rename(fn, RSTRING_PTR(str)) < 0) {
5739 rb_warn("Can't rename %s to %s: %s, skipping file",
5740 fn, RSTRING_PTR(str), strerror(errno));
5741 close(fr);
5742 goto retry;
5744 #endif
5746 else {
5747 #ifdef NO_SAFE_RENAME
5748 rb_fatal("Can't do inplace edit without backup");
5749 #else
5750 if (unlink(fn) < 0) {
5751 rb_warn("Can't remove %s: %s, skipping file",
5752 fn, strerror(errno));
5753 close(fr);
5754 goto retry;
5756 #endif
5758 fw = rb_sysopen(fn, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5759 #ifndef NO_SAFE_RENAME
5760 fstat(fw, &st2);
5761 #ifdef HAVE_FCHMOD
5762 fchmod(fw, st.st_mode);
5763 #else
5764 chmod(fn, st.st_mode);
5765 #endif
5766 if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
5767 fchown(fw, st.st_uid, st.st_gid);
5769 #endif
5770 rb_stdout = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
5771 if (stdout_binmode) rb_io_binmode(rb_stdout);
5773 current_file = prep_io(fr, FMODE_READABLE, rb_cFile, fn);
5775 if (argf_binmode) rb_io_binmode(current_file);
5776 if (argf_enc) {
5777 rb_io_t *fptr;
5779 GetOpenFile(current_file, fptr);
5780 fptr->enc = argf_enc;
5781 fptr->enc2 = argf_enc2;
5782 clear_codeconv(fptr);
5785 else {
5786 next_p = 1;
5787 return Qfalse;
5790 else if (next_p == -1) {
5791 current_file = rb_stdin;
5792 filename = rb_str_new2("-");
5793 if (ruby_inplace_mode) {
5794 rb_warn("Can't do inplace edit for stdio");
5795 rb_stdout = orig_stdout;
5798 return Qtrue;
5801 static VALUE
5802 argf_getline(int argc, VALUE *argv, VALUE argf)
5804 VALUE line;
5806 retry:
5807 if (!next_argv()) return Qnil;
5808 if (ARGF_GENERIC_INPUT_P()) {
5809 line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
5811 else {
5812 if (argc == 0 && rb_rs == rb_default_rs) {
5813 line = rb_io_gets(current_file);
5815 else {
5816 line = rb_io_getline(argc, argv, current_file);
5818 if (NIL_P(line) && next_p != -1) {
5819 argf_close(current_file);
5820 next_p = 1;
5821 goto retry;
5824 if (!NIL_P(line)) {
5825 gets_lineno++;
5826 lineno = INT2FIX(gets_lineno);
5828 return line;
5831 static VALUE
5832 argf_lineno_getter(ID id, VALUE *var)
5834 VALUE argf = *var;
5835 return lineno;
5838 static void
5839 argf_lineno_setter(VALUE val, ID id, VALUE *var)
5841 VALUE argf = *var;
5842 int n = NUM2INT(val);
5843 gets_lineno = n;
5844 lineno = INT2FIX(n);
5847 static VALUE argf_gets(int, VALUE *, VALUE);
5850 * call-seq:
5851 * gets(sep=$/) => string or nil
5852 * gets(limit) => string or nil
5853 * gets(sep,limit) => string or nil
5855 * Returns (and assigns to <code>$_</code>) the next line from the list
5856 * of files in +ARGV+ (or <code>$*</code>), or from standard input if
5857 * no files are present on the command line. Returns +nil+ at end of
5858 * file. The optional argument specifies the record separator. The
5859 * separator is included with the contents of each record. A separator
5860 * of +nil+ reads the entire contents, and a zero-length separator
5861 * reads the input one paragraph at a time, where paragraphs are
5862 * divided by two consecutive newlines. If the first argument is an
5863 * integer, or optional second argument is given, the returning string
5864 * would not be longer than the given value. If multiple filenames are
5865 * present in +ARGV+, +gets(nil)+ will read the contents one file at a
5866 * time.
5868 * ARGV << "testfile"
5869 * print while gets
5871 * <em>produces:</em>
5873 * This is line one
5874 * This is line two
5875 * This is line three
5876 * And so on...
5878 * The style of programming using <code>$_</code> as an implicit
5879 * parameter is gradually losing favor in the Ruby community.
5882 static VALUE
5883 rb_f_gets(int argc, VALUE *argv, VALUE recv)
5885 if (recv == argf) {
5886 return argf_gets(argc, argv, argf);
5888 return rb_funcall2(argf, rb_intern("gets"), argc, argv);
5891 static VALUE
5892 argf_gets(int argc, VALUE *argv, VALUE argf)
5894 VALUE line;
5896 line = argf_getline(argc, argv, argf);
5897 rb_lastline_set(line);
5898 return line;
5901 VALUE
5902 rb_gets(void)
5904 VALUE line;
5906 if (rb_rs != rb_default_rs) {
5907 return rb_f_gets(0, 0, argf);
5910 retry:
5911 if (!next_argv()) return Qnil;
5912 line = rb_io_gets(current_file);
5913 if (NIL_P(line) && next_p != -1) {
5914 rb_io_close(current_file);
5915 next_p = 1;
5916 goto retry;
5918 rb_lastline_set(line);
5919 if (!NIL_P(line)) {
5920 gets_lineno++;
5921 lineno = INT2FIX(gets_lineno);
5924 return line;
5927 static VALUE argf_readline(int, VALUE *, VALUE);
5930 * call-seq:
5931 * readline(sep=$/) => string
5932 * readline(limit) => string
5933 * readline(sep, limit) => string
5935 * Equivalent to <code>Kernel::gets</code>, except
5936 * +readline+ raises +EOFError+ at end of file.
5939 static VALUE
5940 rb_f_readline(int argc, VALUE *argv, VALUE recv)
5942 if (recv == argf) {
5943 return argf_readline(argc, argv, argf);
5945 return rb_funcall2(argf, rb_intern("readline"), argc, argv);
5948 static VALUE
5949 argf_readline(int argc, VALUE *argv, VALUE argf)
5951 VALUE line;
5953 if (!next_argv()) rb_eof_error();
5954 ARGF_FORWARD(argc, argv);
5955 line = argf_gets(argc, argv, argf);
5956 if (NIL_P(line)) {
5957 rb_eof_error();
5960 return line;
5963 static VALUE argf_readlines(int, VALUE *, VALUE);
5966 * call-seq:
5967 * readlines(sep=$/) => array
5968 * readlines(limit) => array
5969 * readlines(sep,limit) => array
5971 * Returns an array containing the lines returned by calling
5972 * <code>Kernel.gets(<i>sep</i>)</code> until the end of file.
5975 static VALUE
5976 rb_f_readlines(int argc, VALUE *argv, VALUE recv)
5978 if (recv == argf) {
5979 return argf_readlines(argc, argv, argf);
5981 return rb_funcall2(argf, rb_intern("readlines"), argc, argv);
5984 static VALUE
5985 argf_readlines(int argc, VALUE *argv, VALUE argf)
5987 VALUE line, ary;
5989 ary = rb_ary_new();
5990 while (!NIL_P(line = argf_getline(argc, argv, argf))) {
5991 rb_ary_push(ary, line);
5994 return ary;
5998 * call-seq:
5999 * `cmd` => string
6001 * Returns the standard output of running _cmd_ in a subshell.
6002 * The built-in syntax <code>%x{...}</code> uses
6003 * this method. Sets <code>$?</code> to the process status.
6005 * `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
6006 * `ls testdir`.split[1] #=> "main.rb"
6007 * `echo oops && exit 99` #=> "oops\n"
6008 * $?.exitstatus #=> 99
6011 static VALUE
6012 rb_f_backquote(VALUE obj, VALUE str)
6014 volatile VALUE port;
6015 VALUE result;
6016 rb_io_t *fptr;
6018 SafeStringValue(str);
6019 port = pipe_open_s(str, "r");
6020 if (NIL_P(port)) return rb_str_new(0,0);
6022 GetOpenFile(port, fptr);
6023 result = read_all(fptr, remain_size(fptr), Qnil);
6024 rb_io_close(port);
6026 return result;
6029 #ifdef HAVE_SYS_SELECT_H
6030 #include <sys/select.h>
6031 #endif
6033 static VALUE
6034 select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fdset_t *fds)
6036 VALUE res, list;
6037 fd_set *rp, *wp, *ep;
6038 rb_io_t *fptr;
6039 long i;
6040 int max = 0, n;
6041 int interrupt_flag = 0;
6042 int pending = 0;
6043 struct timeval timerec;
6045 if (!NIL_P(read)) {
6046 Check_Type(read, T_ARRAY);
6047 for (i=0; i<RARRAY_LEN(read); i++) {
6048 GetOpenFile(rb_io_get_io(RARRAY_PTR(read)[i]), fptr);
6049 rb_fd_set(fptr->fd, &fds[0]);
6050 if (READ_DATA_PENDING(fptr)) { /* check for buffered data */
6051 pending++;
6052 rb_fd_set(fptr->fd, &fds[3]);
6054 if (max < fptr->fd) max = fptr->fd;
6056 if (pending) { /* no blocking if there's buffered data */
6057 timerec.tv_sec = timerec.tv_usec = 0;
6058 tp = &timerec;
6060 rp = rb_fd_ptr(&fds[0]);
6062 else
6063 rp = 0;
6065 if (!NIL_P(write)) {
6066 Check_Type(write, T_ARRAY);
6067 for (i=0; i<RARRAY_LEN(write); i++) {
6068 VALUE write_io = GetWriteIO(rb_io_get_io(RARRAY_PTR(write)[i]));
6069 GetOpenFile(write_io, fptr);
6070 rb_fd_set(fptr->fd, &fds[1]);
6071 if (max < fptr->fd) max = fptr->fd;
6073 wp = rb_fd_ptr(&fds[1]);
6075 else
6076 wp = 0;
6078 if (!NIL_P(except)) {
6079 Check_Type(except, T_ARRAY);
6080 for (i=0; i<RARRAY_LEN(except); i++) {
6081 VALUE io = rb_io_get_io(RARRAY_PTR(except)[i]);
6082 VALUE write_io = GetWriteIO(io);
6083 GetOpenFile(io, fptr);
6084 rb_fd_set(fptr->fd, &fds[2]);
6085 if (max < fptr->fd) max = fptr->fd;
6086 if (io != write_io) {
6087 GetOpenFile(write_io, fptr);
6088 rb_fd_set(fptr->fd, &fds[2]);
6089 if (max < fptr->fd) max = fptr->fd;
6092 ep = rb_fd_ptr(&fds[2]);
6094 else {
6095 ep = 0;
6098 max++;
6100 n = rb_thread_select(max, rp, wp, ep, tp);
6101 if (n < 0) {
6102 rb_sys_fail(0);
6104 if (!pending && n == 0) return Qnil; /* returns nil on timeout */
6106 res = rb_ary_new2(3);
6107 rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
6108 rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
6109 rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
6111 if (interrupt_flag == 0) {
6112 if (rp) {
6113 list = RARRAY_PTR(res)[0];
6114 for (i=0; i< RARRAY_LEN(read); i++) {
6115 VALUE obj = rb_ary_entry(read, i);
6116 VALUE io = rb_io_get_io(obj);
6117 GetOpenFile(io, fptr);
6118 if (rb_fd_isset(fptr->fd, &fds[0]) ||
6119 rb_fd_isset(fptr->fd, &fds[3])) {
6120 rb_ary_push(list, obj);
6125 if (wp) {
6126 list = RARRAY_PTR(res)[1];
6127 for (i=0; i< RARRAY_LEN(write); i++) {
6128 VALUE obj = rb_ary_entry(write, i);
6129 VALUE io = rb_io_get_io(obj);
6130 VALUE write_io = GetWriteIO(io);
6131 GetOpenFile(write_io, fptr);
6132 if (rb_fd_isset(fptr->fd, &fds[1])) {
6133 rb_ary_push(list, obj);
6138 if (ep) {
6139 list = RARRAY_PTR(res)[2];
6140 for (i=0; i< RARRAY_LEN(except); i++) {
6141 VALUE obj = rb_ary_entry(except, i);
6142 VALUE io = rb_io_get_io(obj);
6143 VALUE write_io = GetWriteIO(io);
6144 GetOpenFile(io, fptr);
6145 if (rb_fd_isset(fptr->fd, &fds[2])) {
6146 rb_ary_push(list, obj);
6148 else if (io != write_io) {
6149 GetOpenFile(write_io, fptr);
6150 if (rb_fd_isset(fptr->fd, &fds[2])) {
6151 rb_ary_push(list, obj);
6158 return res; /* returns an empty array on interrupt */
6161 struct select_args {
6162 VALUE read, write, except;
6163 struct timeval *timeout;
6164 rb_fdset_t fdsets[4];
6167 #ifdef HAVE_RB_FD_INIT
6168 static VALUE
6169 select_call(VALUE arg)
6171 struct select_args *p = (struct select_args *)arg;
6173 return select_internal(p->read, p->write, p->except, p->timeout, p->fdsets);
6176 static VALUE
6177 select_end(VALUE arg)
6179 struct select_args *p = (struct select_args *)arg;
6180 int i;
6182 for (i = 0; i < sizeof(p->fdsets) / sizeof(p->fdsets[0]); ++i)
6183 rb_fd_term(&p->fdsets[i]);
6184 return Qnil;
6186 #endif
6189 * call-seq:
6190 * IO.select(read_array
6191 * [, write_array
6192 * [, error_array
6193 * [, timeout]]] ) => array or nil
6195 * See <code>Kernel#select</code>.
6198 static VALUE
6199 rb_f_select(int argc, VALUE *argv, VALUE obj)
6201 VALUE timeout;
6202 struct select_args args;
6203 struct timeval timerec;
6204 int i;
6206 rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
6207 if (NIL_P(timeout)) {
6208 args.timeout = 0;
6210 else {
6211 timerec = rb_time_interval(timeout);
6212 args.timeout = &timerec;
6215 for (i = 0; i < sizeof(args.fdsets) / sizeof(args.fdsets[0]); ++i)
6216 rb_fd_init(&args.fdsets[i]);
6218 #ifdef HAVE_RB_FD_INIT
6219 return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
6220 #else
6221 return select_internal(args.read, args.write, args.except,
6222 args.timeout, args.fdsets);
6223 #endif
6227 #if !defined(MSDOS) && !defined(__human68k__)
6228 static int
6229 io_cntl(int fd, int cmd, long narg, int io_p)
6231 int retval;
6233 #ifdef HAVE_FCNTL
6234 TRAP_BEG;
6235 # if defined(__CYGWIN__)
6236 retval = io_p?ioctl(fd, cmd, (void*)narg):fcntl(fd, cmd, narg);
6237 # else
6238 retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
6239 # endif
6240 TRAP_END;
6241 #else
6242 if (!io_p) {
6243 rb_notimplement();
6245 TRAP_BEG;
6246 retval = ioctl(fd, cmd, narg);
6247 TRAP_END;
6248 #endif
6249 return retval;
6251 #endif
6253 static VALUE
6254 rb_io_ctl(VALUE io, VALUE req, VALUE arg, int io_p)
6256 #if !defined(MSDOS) && !defined(__human68k__)
6257 int cmd = NUM2ULONG(req);
6258 rb_io_t *fptr;
6259 long len = 0;
6260 long narg = 0;
6261 int retval;
6263 rb_secure(2);
6265 if (NIL_P(arg) || arg == Qfalse) {
6266 narg = 0;
6268 else if (FIXNUM_P(arg)) {
6269 narg = FIX2LONG(arg);
6271 else if (arg == Qtrue) {
6272 narg = 1;
6274 else {
6275 VALUE tmp = rb_check_string_type(arg);
6277 if (NIL_P(tmp)) {
6278 narg = NUM2LONG(arg);
6280 else {
6281 arg = tmp;
6282 #ifdef IOCPARM_MASK
6283 #ifndef IOCPARM_LEN
6284 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
6285 #endif
6286 #endif
6287 #ifdef IOCPARM_LEN
6288 len = IOCPARM_LEN(cmd); /* on BSDish systems we're safe */
6289 #else
6290 len = 256; /* otherwise guess at what's safe */
6291 #endif
6292 rb_str_modify(arg);
6294 if (len <= RSTRING_LEN(arg)) {
6295 len = RSTRING_LEN(arg);
6297 if (RSTRING_LEN(arg) < len) {
6298 rb_str_resize(arg, len+1);
6300 RSTRING_PTR(arg)[len] = 17; /* a little sanity check here */
6301 narg = (long)RSTRING_PTR(arg);
6304 GetOpenFile(io, fptr);
6305 retval = io_cntl(fptr->fd, cmd, narg, io_p);
6306 if (retval < 0) rb_sys_fail(fptr->path);
6307 if (TYPE(arg) == T_STRING && RSTRING_PTR(arg)[len] != 17) {
6308 rb_raise(rb_eArgError, "return value overflowed string");
6311 if (!io_p && cmd == F_SETFL) {
6312 if (narg & O_NONBLOCK) {
6313 fptr->mode |= FMODE_WSPLIT_INITIALIZED;
6314 fptr->mode &= ~FMODE_WSPLIT;
6316 else {
6317 fptr->mode &= ~(FMODE_WSPLIT_INITIALIZED|FMODE_WSPLIT);
6321 return INT2NUM(retval);
6322 #else
6323 rb_notimplement();
6324 return Qnil; /* not reached */
6325 #endif
6330 * call-seq:
6331 * ios.ioctl(integer_cmd, arg) => integer
6333 * Provides a mechanism for issuing low-level commands to control or
6334 * query I/O devices. Arguments and results are platform dependent. If
6335 * <i>arg</i> is a number, its value is passed directly. If it is a
6336 * string, it is interpreted as a binary sequence of bytes. On Unix
6337 * platforms, see <code>ioctl(2)</code> for details. Not implemented on
6338 * all platforms.
6341 static VALUE
6342 rb_io_ioctl(int argc, VALUE *argv, VALUE io)
6344 VALUE req, arg;
6346 rb_scan_args(argc, argv, "11", &req, &arg);
6347 return rb_io_ctl(io, req, arg, 1);
6351 * call-seq:
6352 * ios.fcntl(integer_cmd, arg) => integer
6354 * Provides a mechanism for issuing low-level commands to control or
6355 * query file-oriented I/O streams. Arguments and results are platform
6356 * dependent. If <i>arg</i> is a number, its value is passed
6357 * directly. If it is a string, it is interpreted as a binary sequence
6358 * of bytes (<code>Array#pack</code> might be a useful way to build this
6359 * string). On Unix platforms, see <code>fcntl(2)</code> for details.
6360 * Not implemented on all platforms.
6363 static VALUE
6364 rb_io_fcntl(int argc, VALUE *argv, VALUE io)
6366 #ifdef HAVE_FCNTL
6367 VALUE req, arg;
6369 rb_scan_args(argc, argv, "11", &req, &arg);
6370 return rb_io_ctl(io, req, arg, 0);
6371 #else
6372 rb_notimplement();
6373 return Qnil; /* not reached */
6374 #endif
6378 * call-seq:
6379 * syscall(fixnum [, args...]) => integer
6381 * Calls the operating system function identified by _fixnum_,
6382 * passing in the arguments, which must be either +String+
6383 * objects, or +Integer+ objects that ultimately fit within
6384 * a native +long+. Up to nine parameters may be passed (14
6385 * on the Atari-ST). The function identified by _fixnum_ is system
6386 * dependent. On some Unix systems, the numbers may be obtained from a
6387 * header file called <code>syscall.h</code>.
6389 * syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
6391 * <em>produces:</em>
6393 * hello
6396 static VALUE
6397 rb_f_syscall(int argc, VALUE *argv)
6399 #if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
6400 #ifdef atarist
6401 unsigned long arg[14]; /* yes, we really need that many ! */
6402 #else
6403 unsigned long arg[8];
6404 #endif
6405 int retval = -1;
6406 int i = 1;
6407 int items = argc - 1;
6409 /* This probably won't work on machines where sizeof(long) != sizeof(int)
6410 * or where sizeof(long) != sizeof(char*). But such machines will
6411 * not likely have syscall implemented either, so who cares?
6414 rb_secure(2);
6415 if (argc == 0)
6416 rb_raise(rb_eArgError, "too few arguments for syscall");
6417 if (argc > sizeof(arg) / sizeof(arg[0]))
6418 rb_raise(rb_eArgError, "too many arguments for syscall");
6419 arg[0] = NUM2LONG(argv[0]); argv++;
6420 while (items--) {
6421 VALUE v = rb_check_string_type(*argv);
6423 if (!NIL_P(v)) {
6424 StringValue(v);
6425 rb_str_modify(v);
6426 arg[i] = (unsigned long)StringValueCStr(v);
6428 else {
6429 arg[i] = (unsigned long)NUM2LONG(*argv);
6431 argv++;
6432 i++;
6434 TRAP_BEG;
6435 switch (argc) {
6436 case 1:
6437 retval = syscall(arg[0]);
6438 break;
6439 case 2:
6440 retval = syscall(arg[0],arg[1]);
6441 break;
6442 case 3:
6443 retval = syscall(arg[0],arg[1],arg[2]);
6444 break;
6445 case 4:
6446 retval = syscall(arg[0],arg[1],arg[2],arg[3]);
6447 break;
6448 case 5:
6449 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4]);
6450 break;
6451 case 6:
6452 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
6453 break;
6454 case 7:
6455 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
6456 break;
6457 case 8:
6458 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6459 arg[7]);
6460 break;
6461 #ifdef atarist
6462 case 9:
6463 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6464 arg[7], arg[8]);
6465 break;
6466 case 10:
6467 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6468 arg[7], arg[8], arg[9]);
6469 break;
6470 case 11:
6471 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6472 arg[7], arg[8], arg[9], arg[10]);
6473 break;
6474 case 12:
6475 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6476 arg[7], arg[8], arg[9], arg[10], arg[11]);
6477 break;
6478 case 13:
6479 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6480 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
6481 break;
6482 case 14:
6483 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6484 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12], arg[13]);
6485 break;
6486 #endif /* atarist */
6488 TRAP_END;
6489 if (retval < 0) rb_sys_fail(0);
6490 return INT2NUM(retval);
6491 #else
6492 rb_notimplement();
6493 return Qnil; /* not reached */
6494 #endif
6497 static VALUE
6498 io_new_instance(VALUE args)
6500 return rb_class_new_instance(2, (VALUE*)args+1, *(VALUE*)args);
6503 static void
6504 io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2)
6506 if (NIL_P(v2)) argc = 1;
6507 if (argc == 2) {
6508 fptr->enc2 = rb_to_encoding(v1);
6509 fptr->enc = rb_to_encoding(v2);
6510 clear_codeconv(fptr);
6512 else if (argc == 1) {
6513 if (NIL_P(v1)) {
6514 fptr->enc = 0;
6515 fptr->enc2 = 0;
6516 clear_codeconv(fptr);
6518 else {
6519 VALUE tmp = rb_check_string_type(v1);
6520 if (!NIL_P(tmp)) {
6521 mode_enc(fptr, StringValueCStr(tmp));
6523 else {
6524 fptr->enc = rb_to_encoding(v1);
6525 fptr->enc2 = 0;
6526 clear_codeconv(fptr);
6533 * call-seq:
6534 * IO.pipe -> [read_io, write_io]
6535 * IO.pipe(ext_enc) -> [read_io, write_io]
6536 * IO.pipe("ext_enc:int_enc") -> [read_io, write_io]
6537 * IO.pipe(ext_enc, int_enc) -> [read_io, write_io]
6539 * Creates a pair of pipe endpoints (connected to each other) and
6540 * returns them as a two-element array of <code>IO</code> objects:
6541 * <code>[</code> <i>read_io</i>, <i>write_io</i> <code>]</code>. Not
6542 * available on all platforms.
6544 * If an encoding (encoding name or encoding object) is specified as an optional argument,
6545 * read string from pipe is tagged with the encoding specified.
6546 * If the argument is a colon separated two encoding names "A:B",
6547 * the read string is converted from encoding A (external encoding)
6548 * to encoding B (internal encoding), then tagged with B.
6549 * If two optional arguments are specified, those must be
6550 * encoding objects or encoding names,
6551 * and the first one is the external encoding,
6552 * and the second one is the internal encoding.
6554 * In the example below, the two processes close the ends of the pipe
6555 * that they are not using. This is not just a cosmetic nicety. The
6556 * read end of a pipe will not generate an end of file condition if
6557 * there are any writers with the pipe still open. In the case of the
6558 * parent process, the <code>rd.read</code> will never return if it
6559 * does not first issue a <code>wr.close</code>.
6561 * rd, wr = IO.pipe
6563 * if fork
6564 * wr.close
6565 * puts "Parent got: <#{rd.read}>"
6566 * rd.close
6567 * Process.wait
6568 * else
6569 * rd.close
6570 * puts "Sending message to parent"
6571 * wr.write "Hi Dad"
6572 * wr.close
6573 * end
6575 * <em>produces:</em>
6577 * Sending message to parent
6578 * Parent got: <Hi Dad>
6581 static VALUE
6582 rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
6584 #ifdef __human68k__
6585 rb_notimplement();
6586 return Qnil; /* not reached */
6587 #else
6588 int pipes[2], state;
6589 VALUE r, w, args[3], v1, v2;
6590 rb_io_t *fptr;
6592 rb_scan_args(argc, argv, "02", &v1, &v2);
6593 if (rb_pipe(pipes) == -1)
6594 rb_sys_fail(0);
6596 args[0] = klass;
6597 args[1] = INT2NUM(pipes[0]);
6598 args[2] = INT2FIX(O_RDONLY);
6599 r = rb_protect(io_new_instance, (VALUE)args, &state);
6600 if (state) {
6601 close(pipes[0]);
6602 close(pipes[1]);
6603 rb_jump_tag(state);
6605 GetOpenFile(r, fptr);
6606 io_encoding_set(fptr, argc, v1, v2);
6607 args[1] = INT2NUM(pipes[1]);
6608 args[2] = INT2FIX(O_WRONLY);
6609 w = rb_protect(io_new_instance, (VALUE)args, &state);
6610 if (state) {
6611 close(pipes[1]);
6612 if (!NIL_P(r)) rb_io_close(r);
6613 rb_jump_tag(state);
6615 rb_io_synchronized(RFILE(w)->fptr);
6617 return rb_assoc_new(r, w);
6618 #endif
6621 struct foreach_arg {
6622 int argc;
6623 VALUE *argv;
6624 VALUE io;
6627 static void
6628 open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
6630 VALUE opt, v;
6632 FilePathValue(argv[0]);
6633 arg->io = 0;
6634 arg->argc = argc > 1 ? 1 : 0;
6635 arg->argv = argv + 1;
6636 if (argc == 1) {
6637 no_key:
6638 arg->io = rb_io_open(RSTRING_PTR(argv[0]), "r");
6639 return;
6641 opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
6642 if (NIL_P(opt)) goto no_key;
6643 if (argc > 2) arg->argc = 1;
6644 else arg->argc = 0;
6646 v = rb_hash_aref(opt, sym_open_args);
6647 if (!NIL_P(v)) {
6648 VALUE args;
6650 v = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
6651 args = rb_ary_new2(RARRAY_LEN(v)+1);
6652 rb_ary_push(args, argv[0]);
6653 rb_ary_concat(args, v);
6654 MEMCPY(RARRAY_PTR(args)+1, RARRAY_PTR(v), VALUE, RARRAY_LEN(v));
6656 arg->io = rb_io_open_with_args(RARRAY_LEN(args), RARRAY_PTR(args));
6657 return;
6659 v = rb_hash_aref(opt, sym_mode);
6660 if (!NIL_P(v)) {
6661 arg->io = rb_io_open(RSTRING_PTR(argv[0]), StringValueCStr(v));
6663 else {
6664 arg->io = rb_io_open(RSTRING_PTR(argv[0]), "r");
6667 io_set_encoding(arg->io, opt);
6670 static VALUE
6671 io_s_foreach(struct foreach_arg *arg)
6673 VALUE str;
6675 while (!NIL_P(str = rb_io_gets_m(arg->argc, arg->argv, arg->io))) {
6676 rb_yield(str);
6678 return Qnil;
6682 * call-seq:
6683 * IO.foreach(name, sep=$/) {|line| block } => nil
6684 * IO.foreach(name, limit) {|line| block } => nil
6685 * IO.foreach(name, sep, limit) {|line| block } => nil
6687 * Executes the block for every line in the named I/O port, where lines
6688 * are separated by <em>sep</em>.
6690 * IO.foreach("testfile") {|x| print "GOT ", x }
6692 * <em>produces:</em>
6694 * GOT This is line one
6695 * GOT This is line two
6696 * GOT This is line three
6697 * GOT And so on...
6699 * If the last argument is a hash, it's the keyword argument to open.
6700 * See <code>IO.read</code> for detail.
6704 static VALUE
6705 rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
6707 struct foreach_arg arg;
6709 rb_scan_args(argc, argv, "13", NULL, NULL, NULL, NULL);
6710 RETURN_ENUMERATOR(self, argc, argv);
6711 open_key_args(argc, argv, &arg);
6712 if (NIL_P(arg.io)) return Qnil;
6713 return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);
6716 static VALUE
6717 io_s_readlines(struct foreach_arg *arg)
6719 return rb_io_readlines(arg->argc, arg->argv, arg->io);
6723 * call-seq:
6724 * IO.readlines(name, sep=$/) => array
6725 * IO.readlines(name, limit) => array
6726 * IO.readlines(name, sep, limit) => array
6728 * Reads the entire file specified by <i>name</i> as individual
6729 * lines, and returns those lines in an array. Lines are separated by
6730 * <i>sep</i>.
6732 * a = IO.readlines("testfile")
6733 * a[0] #=> "This is line one\n"
6735 * If the last argument is a hash, it's the keyword argument to open.
6736 * See <code>IO.read</code> for detail.
6740 static VALUE
6741 rb_io_s_readlines(int argc, VALUE *argv, VALUE io)
6743 struct foreach_arg arg;
6745 rb_scan_args(argc, argv, "13", NULL, NULL, NULL, NULL);
6746 open_key_args(argc, argv, &arg);
6747 if (NIL_P(arg.io)) return Qnil;
6748 return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
6751 static VALUE
6752 io_s_read(struct foreach_arg *arg)
6754 return io_read(arg->argc, arg->argv, arg->io);
6758 * call-seq:
6759 * IO.read(name, [length [, offset]] ) => string
6760 * IO.read(name, [length [, offset]], opt) => string
6762 * Opens the file, optionally seeks to the given offset, then returns
6763 * <i>length</i> bytes (defaulting to the rest of the file).
6764 * <code>read</code> ensures the file is closed before returning.
6766 * If the last argument is a hash, it specifies option for internal
6767 * open(). The key would be the following. open_args: is exclusive
6768 * to others.
6770 * encoding: string or encoding
6772 * specifies encoding of the read string. encoding will be ignored
6773 * if length is specified.
6775 * mode: string
6777 * specifies mode argument for open(). it should start with "r"
6778 * otherwise it would cause error.
6780 * open_args: array of strings
6782 * specifies arguments for open() as an array.
6784 * IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
6785 * IO.read("testfile", 20) #=> "This is line one\nThi"
6786 * IO.read("testfile", 20, 10) #=> "ne one\nThis is line "
6789 static VALUE
6790 rb_io_s_read(int argc, VALUE *argv, VALUE io)
6792 VALUE offset;
6793 struct foreach_arg arg;
6795 rb_scan_args(argc, argv, "13", NULL, NULL, &offset, NULL);
6796 open_key_args(argc, argv, &arg);
6797 if (NIL_P(arg.io)) return Qnil;
6798 if (!NIL_P(offset)) {
6799 rb_io_binmode(arg.io);
6800 rb_io_seek(arg.io, offset, SEEK_SET);
6801 if (arg.argc == 2) arg.argc = 1;
6803 return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
6806 struct copy_stream_struct {
6807 VALUE src;
6808 VALUE dst;
6809 off_t copy_length; /* (off_t)-1 if not specified */
6810 off_t src_offset; /* (off_t)-1 if not specified */
6812 int src_fd;
6813 int dst_fd;
6814 int close_src;
6815 int close_dst;
6816 off_t total;
6817 const char *syserr;
6818 int error_no;
6819 const char *notimp;
6820 rb_fdset_t fds;
6821 rb_thread_t *th;
6824 static int
6825 copy_stream_wait_read(struct copy_stream_struct *stp)
6827 int ret;
6828 rb_fd_zero(&stp->fds);
6829 rb_fd_set(stp->src_fd, &stp->fds);
6830 ret = rb_fd_select(rb_fd_max(&stp->fds), &stp->fds, NULL, NULL, NULL);
6831 if (ret == -1) {
6832 stp->syserr = "select";
6833 stp->error_no = errno;
6834 return -1;
6836 return 0;
6839 static int
6840 copy_stream_wait_write(struct copy_stream_struct *stp)
6842 int ret;
6843 rb_fd_zero(&stp->fds);
6844 rb_fd_set(stp->dst_fd, &stp->fds);
6845 ret = rb_fd_select(rb_fd_max(&stp->fds), NULL, &stp->fds, NULL, NULL);
6846 if (ret == -1) {
6847 stp->syserr = "select";
6848 stp->error_no = errno;
6849 return -1;
6851 return 0;
6854 #ifdef HAVE_SENDFILE
6856 #ifdef __linux__
6857 #define USE_SENDFILE
6859 #ifdef HAVE_SYS_SENDFILE_H
6860 #include <sys/sendfile.h>
6861 #endif
6863 static ssize_t
6864 simple_sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
6866 return sendfile(out_fd, in_fd, offset, count);
6869 #endif
6871 #endif
6873 #ifdef USE_SENDFILE
6874 static int
6875 copy_stream_sendfile(struct copy_stream_struct *stp)
6877 struct stat src_stat, dst_stat;
6878 ssize_t ss;
6879 int ret;
6881 off_t copy_length;
6882 off_t src_offset;
6883 int use_pread;
6885 ret = fstat(stp->src_fd, &src_stat);
6886 if (ret == -1) {
6887 stp->syserr = "fstat";
6888 stp->error_no = errno;
6889 return -1;
6891 if (!S_ISREG(src_stat.st_mode))
6892 return 0;
6894 ret = fstat(stp->dst_fd, &dst_stat);
6895 if (ret == -1) {
6896 stp->syserr = "fstat";
6897 stp->error_no = errno;
6898 return -1;
6900 if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
6901 return 0;
6903 src_offset = stp->src_offset;
6904 use_pread = src_offset != (off_t)-1;
6906 copy_length = stp->copy_length;
6907 if (copy_length == (off_t)-1) {
6908 if (use_pread)
6909 copy_length = src_stat.st_size - src_offset;
6910 else {
6911 off_t cur = lseek(stp->src_fd, 0, SEEK_CUR);
6912 if (cur == (off_t)-1) {
6913 stp->syserr = "lseek";
6914 stp->error_no = errno;
6915 return -1;
6917 copy_length = src_stat.st_size - cur;
6921 retry_sendfile:
6922 if (use_pread) {
6923 ss = simple_sendfile(stp->dst_fd, stp->src_fd, &src_offset, copy_length);
6925 else {
6926 ss = simple_sendfile(stp->dst_fd, stp->src_fd, NULL, copy_length);
6928 if (0 < ss) {
6929 stp->total += ss;
6930 copy_length -= ss;
6931 if (0 < copy_length) {
6932 ss = -1;
6933 errno = EAGAIN;
6936 if (ss == -1) {
6937 switch (errno) {
6938 case EINVAL:
6939 #ifdef ENOSYS
6940 case ENOSYS:
6941 #endif
6942 return 0;
6943 case EAGAIN:
6944 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6945 case EWOULDBLOCK:
6946 #endif
6947 if (copy_stream_wait_write(stp) == -1)
6948 return -1;
6949 if (RUBY_VM_INTERRUPTED(stp->th))
6950 return -1;
6951 goto retry_sendfile;
6953 stp->syserr = "sendfile";
6954 stp->error_no = errno;
6955 return -1;
6957 return 1;
6959 #endif
6961 static ssize_t
6962 copy_stream_read(struct copy_stream_struct *stp, char *buf, int len, off_t offset)
6964 ssize_t ss;
6965 retry_read:
6966 if (offset == (off_t)-1)
6967 ss = read(stp->src_fd, buf, len);
6968 else {
6969 #ifdef HAVE_PREAD
6970 ss = pread(stp->src_fd, buf, len, offset);
6971 #else
6972 stp->notimp = "pread";
6973 return -1;
6974 #endif
6976 if (ss == 0) {
6977 return 0;
6979 if (ss == -1) {
6980 switch (errno) {
6981 case EAGAIN:
6982 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6983 case EWOULDBLOCK:
6984 #endif
6985 if (copy_stream_wait_read(stp) == -1)
6986 return -1;
6987 goto retry_read;
6988 #ifdef ENOSYS
6989 case ENOSYS:
6990 #endif
6991 stp->notimp = "pread";
6992 return -1;
6994 stp->syserr = offset == (off_t)-1 ? "read" : "pread";
6995 stp->error_no = errno;
6996 return -1;
6998 return ss;
7001 static int
7002 copy_stream_write(struct copy_stream_struct *stp, char *buf, int len)
7004 ssize_t ss;
7005 int off = 0;
7006 while (len) {
7007 ss = write(stp->dst_fd, buf+off, len);
7008 if (ss == -1) {
7009 if (errno == EAGAIN || errno == EWOULDBLOCK) {
7010 if (copy_stream_wait_write(stp) == -1)
7011 return -1;
7012 continue;
7014 stp->syserr = "write";
7015 stp->error_no = errno;
7016 return -1;
7018 off += ss;
7019 len -= ss;
7020 stp->total += ss;
7022 return 0;
7025 static void
7026 copy_stream_read_write(struct copy_stream_struct *stp)
7028 char buf[1024*16];
7029 int len;
7030 ssize_t ss;
7031 int ret;
7032 off_t copy_length;
7033 int use_eof;
7034 off_t src_offset;
7035 int use_pread;
7037 copy_length = stp->copy_length;
7038 use_eof = copy_length == (off_t)-1;
7039 src_offset = stp->src_offset;
7040 use_pread = src_offset != (off_t)-1;
7042 if (use_pread && stp->close_src) {
7043 off_t r;
7044 r = lseek(stp->src_fd, src_offset, SEEK_SET);
7045 if (r == (off_t)-1) {
7046 stp->syserr = "lseek";
7047 stp->error_no = errno;
7048 return;
7050 src_offset = (off_t)-1;
7051 use_pread = 0;
7054 while (use_eof || 0 < copy_length) {
7055 if (!use_eof && copy_length < sizeof(buf)) {
7056 len = copy_length;
7058 else {
7059 len = sizeof(buf);
7061 if (use_pread) {
7062 ss = copy_stream_read(stp, buf, len, src_offset);
7063 if (0 < ss)
7064 src_offset += ss;
7066 else {
7067 ss = copy_stream_read(stp, buf, len, (off_t)-1);
7069 if (ss <= 0) /* EOF or error */
7070 return;
7072 ret = copy_stream_write(stp, buf, ss);
7073 if (ret < 0)
7074 return;
7076 if (!use_eof)
7077 copy_length -= ss;
7079 if (RUBY_VM_INTERRUPTED(stp->th))
7080 return;
7084 static VALUE
7085 copy_stream_func(void *arg)
7087 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
7088 #ifdef USE_SENDFILE
7089 int ret;
7090 #endif
7092 #ifdef USE_SENDFILE
7093 ret = copy_stream_sendfile(stp);
7094 if (ret != 0)
7095 goto finish; /* error or success */
7096 #endif
7098 copy_stream_read_write(stp);
7100 #ifdef USE_SENDFILE
7101 finish:
7102 #endif
7103 return Qnil;
7106 static VALUE
7107 copy_stream_fallback_body(VALUE arg)
7109 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
7110 const int buflen = 16*1024;
7111 VALUE n;
7112 VALUE buf = rb_str_buf_new(buflen);
7113 long rest = stp->copy_length;
7114 off_t off = stp->src_offset;
7116 while (1) {
7117 long numwrote;
7118 long l;
7119 if (stp->copy_length == (off_t)-1) {
7120 l = buflen;
7122 else {
7123 if (rest == 0)
7124 break;
7125 l = buflen < rest ? buflen : rest;
7127 if (stp->src_fd == -1) {
7128 rb_funcall(stp->src, id_readpartial, 2, INT2FIX(l), buf);
7130 else {
7131 ssize_t ss;
7132 rb_thread_wait_fd(stp->src_fd);
7133 rb_str_resize(buf, buflen);
7134 ss = copy_stream_read(stp, RSTRING_PTR(buf), l, off);
7135 if (ss == -1)
7136 return Qnil;
7137 if (ss == 0)
7138 rb_eof_error();
7139 rb_str_resize(buf, ss);
7140 if (off != (off_t)-1)
7141 off += ss;
7143 n = rb_io_write(stp->dst, buf);
7144 numwrote = NUM2LONG(n);
7145 stp->total += numwrote;
7146 rest -= numwrote;
7149 return Qnil;
7152 static VALUE
7153 copy_stream_fallback(struct copy_stream_struct *stp)
7155 if (stp->src_fd == -1 && stp->src_offset != (off_t)-1) {
7156 rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
7158 rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
7159 (VALUE (*) (ANYARGS))0, (VALUE)0,
7160 rb_eEOFError, (VALUE)0);
7161 return Qnil;
7164 static VALUE
7165 copy_stream_body(VALUE arg)
7167 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
7168 VALUE src_io, dst_io;
7169 rb_io_t *src_fptr = 0, *dst_fptr = 0;
7170 int src_fd, dst_fd;
7172 stp->th = GET_THREAD();
7174 stp->total = 0;
7176 if (stp->src == argf ||
7177 !(TYPE(stp->src) == T_FILE ||
7178 rb_respond_to(stp->src, rb_intern("to_io")) ||
7179 TYPE(stp->src) == T_STRING ||
7180 rb_respond_to(stp->src, rb_intern("to_path")))) {
7181 src_fd = -1;
7183 else {
7184 src_io = rb_check_convert_type(stp->src, T_FILE, "IO", "to_io");
7185 if (NIL_P(src_io)) {
7186 VALUE args[2];
7187 int flags = O_RDONLY;
7188 #ifdef O_NOCTTY
7189 flags |= O_NOCTTY;
7190 #endif
7191 FilePathValue(stp->src);
7192 args[0] = stp->src;
7193 args[1] = INT2NUM(flags);
7194 src_io = rb_class_new_instance(2, args, rb_cFile);
7195 stp->src = src_io;
7196 stp->close_src = 1;
7198 GetOpenFile(src_io, src_fptr);
7199 rb_io_check_readable(src_fptr);
7200 src_fd = src_fptr->fd;
7202 stp->src_fd = src_fd;
7204 if (stp->dst == argf ||
7205 !(TYPE(stp->dst) == T_FILE ||
7206 rb_respond_to(stp->dst, rb_intern("to_io")) ||
7207 TYPE(stp->dst) == T_STRING ||
7208 rb_respond_to(stp->dst, rb_intern("to_path")))) {
7209 dst_fd = -1;
7211 else {
7212 dst_io = rb_check_convert_type(stp->dst, T_FILE, "IO", "to_io");
7213 if (NIL_P(dst_io)) {
7214 VALUE args[3];
7215 int flags = O_WRONLY|O_CREAT|O_TRUNC;
7216 #ifdef O_NOCTTY
7217 flags |= O_NOCTTY;
7218 #endif
7219 FilePathValue(stp->dst);
7220 args[0] = stp->dst;
7221 args[1] = INT2NUM(flags);
7222 args[2] = INT2FIX(0600);
7223 dst_io = rb_class_new_instance(3, args, rb_cFile);
7224 stp->dst = dst_io;
7225 stp->close_dst = 1;
7227 else {
7228 dst_io = GetWriteIO(dst_io);
7229 stp->dst = dst_io;
7231 GetOpenFile(dst_io, dst_fptr);
7232 rb_io_check_writable(dst_fptr);
7233 dst_fd = dst_fptr->fd;
7235 stp->dst_fd = dst_fd;
7237 if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf_len) {
7238 long len = src_fptr->rbuf_len;
7239 VALUE str;
7240 if (stp->copy_length != (off_t)-1 && stp->copy_length < len) {
7241 len = stp->copy_length;
7243 str = rb_str_buf_new(len);
7244 rb_str_resize(str,len);
7245 read_buffered_data(RSTRING_PTR(str), len, src_fptr);
7246 if (dst_fptr) /* IO or filename */
7247 io_fwrite(str, dst_fptr);
7248 else /* others such as StringIO */
7249 rb_io_write(stp->dst, str);
7250 stp->total += len;
7251 if (stp->copy_length != (off_t)-1)
7252 stp->copy_length -= len;
7255 if (dst_fptr && io_fflush(dst_fptr) < 0) {
7256 rb_raise(rb_eIOError, "flush failed");
7259 if (stp->copy_length == 0)
7260 return Qnil;
7262 if (src_fd == -1 || dst_fd == -1) {
7263 return copy_stream_fallback(stp);
7266 rb_fd_init(&stp->fds);
7267 rb_fd_set(src_fd, &stp->fds);
7268 rb_fd_set(dst_fd, &stp->fds);
7270 return rb_thread_blocking_region(copy_stream_func, (void*)stp, RB_UBF_DFL, 0);
7273 static VALUE
7274 copy_stream_finalize(VALUE arg)
7276 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
7277 if (stp->close_src) {
7278 rb_io_close_m(stp->src);
7280 if (stp->close_dst) {
7281 rb_io_close_m(stp->dst);
7283 rb_fd_term(&stp->fds);
7284 if (stp->syserr) {
7285 errno = stp->error_no;
7286 rb_sys_fail(stp->syserr);
7288 if (stp->notimp) {
7289 rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
7291 return Qnil;
7295 * call-seq:
7296 * IO.copy_stream(src, dst)
7297 * IO.copy_stream(src, dst, copy_length)
7298 * IO.copy_stream(src, dst, copy_length, src_offset)
7300 * IO.copy_stream copies <i>src</i> to <i>dst</i>.
7301 * <i>src</i> and <i>dst</i> is either a filename or an IO.
7303 * This method returns the number of bytes copied.
7305 * If optional arguments are not given,
7306 * the start position of the copy is
7307 * the beginning of the filename or
7308 * the current file offset of the IO.
7309 * The end position of the copy is the end of file.
7311 * If <i>copy_length</i> is given,
7312 * No more than <i>copy_length</i> bytes are copied.
7314 * If <i>src_offset</i> is given,
7315 * it specifies the start position of the copy.
7317 * When <i>src_offset</i> is specified and
7318 * <i>src</i> is an IO,
7319 * IO.copy_stream doesn't move the current file offset.
7322 static VALUE
7323 rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
7325 VALUE src, dst, length, src_offset;
7326 struct copy_stream_struct st;
7328 MEMZERO(&st, struct copy_stream_struct, 1);
7330 rb_scan_args(argc, argv, "22", &src, &dst, &length, &src_offset);
7332 st.src = src;
7333 st.dst = dst;
7335 if (NIL_P(length))
7336 st.copy_length = (off_t)-1;
7337 else
7338 st.copy_length = NUM2OFFT(length);
7340 if (NIL_P(src_offset))
7341 st.src_offset = (off_t)-1;
7342 else
7343 st.src_offset = NUM2OFFT(src_offset);
7345 rb_ensure(copy_stream_body, (VALUE)&st, copy_stream_finalize, (VALUE)&st);
7347 return OFFT2NUM(st.total);
7351 * call-seq:
7352 * io.external_encoding => encoding
7354 * Returns the Encoding object that represents the encoding of the file.
7355 * If io is write mode and no encoding is specified, returns <code>nil</code>.
7358 static VALUE
7359 rb_io_external_encoding(VALUE io)
7361 rb_io_t *fptr;
7363 GetOpenFile(io, fptr);
7364 if (fptr->enc2) {
7365 return rb_enc_from_encoding(fptr->enc2);
7367 if (fptr->mode & FMODE_WRITABLE) {
7368 if (fptr->enc)
7369 return rb_enc_from_encoding(fptr->enc);
7370 return Qnil;
7372 return rb_enc_from_encoding(io_read_encoding(fptr));
7376 * call-seq:
7377 * io.internal_encoding => encoding
7379 * Returns the Encoding of the internal string if conversion is
7380 * specified. Otherwise returns nil.
7383 static VALUE
7384 rb_io_internal_encoding(VALUE io)
7386 rb_io_t *fptr;
7388 GetOpenFile(io, fptr);
7389 if (!fptr->enc2) return Qnil;
7390 return rb_enc_from_encoding(io_read_encoding(fptr));
7394 * call-seq:
7395 * io.set_encoding(ext_enc) => io
7396 * io.set_encoding("ext_enc:int_enc") => io
7397 * io.set_encoding(ext_enc, int_enc) => io
7399 * If single argument is specified, read string from io is tagged
7400 * with the encoding specified. If encoding is a colon separated two
7401 * encoding names "A:B", the read string is converted from encoding A
7402 * (external encoding) to encoding B (internal encoding), then tagged
7403 * with B. If two arguments are specified, those must be encoding
7404 * objects or encoding names, and the first one is the external encoding, and the
7405 * second one is the internal encoding.
7408 static VALUE
7409 rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
7411 rb_io_t *fptr;
7412 VALUE v1, v2;
7414 rb_scan_args(argc, argv, "11", &v1, &v2);
7415 GetOpenFile(io, fptr);
7416 io_encoding_set(fptr, argc, v1, v2);
7417 return io;
7420 static VALUE
7421 argf_external_encoding(VALUE argf)
7423 if (!RTEST(current_file)) {
7424 return rb_enc_from_encoding(rb_default_external_encoding());
7426 return rb_io_external_encoding(rb_io_check_io(current_file));
7429 static VALUE
7430 argf_internal_encoding(VALUE argf)
7432 if (!RTEST(current_file)) {
7433 return rb_enc_from_encoding(rb_default_external_encoding());
7435 return rb_io_internal_encoding(rb_io_check_io(current_file));
7438 static VALUE
7439 argf_set_encoding(int argc, VALUE *argv, VALUE argf)
7441 rb_io_t *fptr;
7443 if (!next_argv()) {
7444 rb_raise(rb_eArgError, "no stream to set encoding");
7446 rb_io_set_encoding(argc, argv, current_file);
7447 GetOpenFile(current_file, fptr);
7448 argf_enc = fptr->enc;
7449 argf_enc2 = fptr->enc2;
7450 return argf;
7453 static VALUE
7454 argf_tell(VALUE argf)
7456 if (!next_argv()) {
7457 rb_raise(rb_eArgError, "no stream to tell");
7459 ARGF_FORWARD(0, 0);
7460 return rb_io_tell(current_file);
7463 static VALUE
7464 argf_seek_m(int argc, VALUE *argv, VALUE argf)
7466 if (!next_argv()) {
7467 rb_raise(rb_eArgError, "no stream to seek");
7469 ARGF_FORWARD(argc, argv);
7470 return rb_io_seek_m(argc, argv, current_file);
7473 static VALUE
7474 argf_set_pos(VALUE argf, VALUE offset)
7476 if (!next_argv()) {
7477 rb_raise(rb_eArgError, "no stream to set position");
7479 ARGF_FORWARD(1, &offset);
7480 return rb_io_set_pos(current_file, offset);
7483 static VALUE
7484 argf_rewind(VALUE argf)
7486 if (!next_argv()) {
7487 rb_raise(rb_eArgError, "no stream to rewind");
7489 ARGF_FORWARD(0, 0);
7490 return rb_io_rewind(current_file);
7493 static VALUE
7494 argf_fileno(VALUE argf)
7496 if (!next_argv()) {
7497 rb_raise(rb_eArgError, "no stream");
7499 ARGF_FORWARD(0, 0);
7500 return rb_io_fileno(current_file);
7503 static VALUE
7504 argf_to_io(VALUE argf)
7506 next_argv();
7507 ARGF_FORWARD(0, 0);
7508 return current_file;
7511 static VALUE
7512 argf_eof(VALUE argf)
7514 if (current_file) {
7515 if (init_p == 0) return Qtrue;
7516 ARGF_FORWARD(0, 0);
7517 if (rb_io_eof(current_file)) {
7518 return Qtrue;
7521 return Qfalse;
7524 static VALUE
7525 argf_read(int argc, VALUE *argv, VALUE argf)
7527 VALUE tmp, str, length;
7528 long len = 0;
7530 rb_scan_args(argc, argv, "02", &length, &str);
7531 if (!NIL_P(length)) {
7532 len = NUM2LONG(argv[0]);
7534 if (!NIL_P(str)) {
7535 StringValue(str);
7536 rb_str_resize(str,0);
7537 argv[1] = Qnil;
7540 retry:
7541 if (!next_argv()) {
7542 return str;
7544 if (ARGF_GENERIC_INPUT_P()) {
7545 tmp = argf_forward(argc, argv, argf);
7547 else {
7548 tmp = io_read(argc, argv, current_file);
7550 if (NIL_P(str)) str = tmp;
7551 else if (!NIL_P(tmp)) rb_str_append(str, tmp);
7552 if (NIL_P(tmp) || NIL_P(length)) {
7553 if (next_p != -1) {
7554 argf_close(current_file);
7555 next_p = 1;
7556 goto retry;
7559 else if (argc >= 1) {
7560 if (RSTRING_LEN(str) < len) {
7561 len -= RSTRING_LEN(str);
7562 argv[0] = INT2NUM(len);
7563 goto retry;
7566 return str;
7569 struct argf_call_arg {
7570 int argc;
7571 VALUE *argv;
7572 VALUE argf;
7575 static VALUE
7576 argf_forward_call(VALUE arg)
7578 struct argf_call_arg *p = (struct argf_call_arg *)arg;
7579 argf_forward(p->argc, p->argv, p->argf);
7580 return Qnil;
7583 static VALUE
7584 argf_readpartial(int argc, VALUE *argv, VALUE argf)
7586 VALUE tmp, str, length;
7588 rb_scan_args(argc, argv, "11", &length, &str);
7589 if (!NIL_P(str)) {
7590 StringValue(str);
7591 argv[1] = str;
7594 if (!next_argv()) {
7595 rb_str_resize(str, 0);
7596 rb_eof_error();
7598 if (ARGF_GENERIC_INPUT_P()) {
7599 struct argf_call_arg arg;
7600 arg.argc = argc;
7601 arg.argv = argv;
7602 arg.argf = argf;
7603 tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
7604 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
7606 else {
7607 tmp = io_getpartial(argc, argv, current_file, 0);
7609 if (NIL_P(tmp)) {
7610 if (next_p == -1) {
7611 rb_eof_error();
7613 argf_close(current_file);
7614 next_p = 1;
7615 if (RARRAY_LEN(rb_argv) == 0)
7616 rb_eof_error();
7617 if (NIL_P(str))
7618 str = rb_str_new(NULL, 0);
7619 return str;
7621 return tmp;
7624 static VALUE
7625 argf_getc(VALUE argf)
7627 VALUE ch;
7629 retry:
7630 if (!next_argv()) return Qnil;
7631 if (ARGF_GENERIC_INPUT_P()) {
7632 ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
7634 else {
7635 ch = rb_io_getc(current_file);
7637 if (NIL_P(ch) && next_p != -1) {
7638 argf_close(current_file);
7639 next_p = 1;
7640 goto retry;
7643 return ch;
7646 static VALUE
7647 argf_getbyte(VALUE argf)
7649 VALUE ch;
7651 retry:
7652 if (!next_argv()) return Qnil;
7653 if (TYPE(current_file) != T_FILE) {
7654 ch = rb_funcall3(current_file, rb_intern("getbyte"), 0, 0);
7656 else {
7657 ch = rb_io_getbyte(current_file);
7659 if (NIL_P(ch) && next_p != -1) {
7660 argf_close(current_file);
7661 next_p = 1;
7662 goto retry;
7665 return ch;
7668 static VALUE
7669 argf_readchar(VALUE argf)
7671 VALUE ch;
7673 retry:
7674 if (!next_argv()) rb_eof_error();
7675 if (TYPE(current_file) != T_FILE) {
7676 ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
7678 else {
7679 ch = rb_io_getc(current_file);
7681 if (NIL_P(ch) && next_p != -1) {
7682 argf_close(current_file);
7683 next_p = 1;
7684 goto retry;
7687 return ch;
7690 static VALUE
7691 argf_readbyte(VALUE argf)
7693 VALUE c;
7695 NEXT_ARGF_FORWARD(0, 0);
7696 c = argf_getbyte(argf);
7697 if (NIL_P(c)) {
7698 rb_eof_error();
7700 return c;
7703 static VALUE
7704 argf_each_line(int argc, VALUE *argv, VALUE argf)
7706 RETURN_ENUMERATOR(argf, argc, argv);
7707 for (;;) {
7708 if (!next_argv()) return Qnil;
7709 rb_block_call(current_file, rb_intern("each_line"), argc, argv, rb_yield, 0);
7710 next_p = 1;
7712 return argf;
7715 static VALUE
7716 argf_each_byte(VALUE argf)
7718 RETURN_ENUMERATOR(argf, 0, 0);
7719 for (;;) {
7720 if (!next_argv()) return Qnil;
7721 rb_block_call(current_file, rb_intern("each_byte"), 0, 0, rb_yield, 0);
7722 next_p = 1;
7726 static VALUE
7727 argf_each_char(VALUE argf)
7729 RETURN_ENUMERATOR(argf, 0, 0);
7730 for (;;) {
7731 if (!next_argv()) return Qnil;
7732 rb_block_call(current_file, rb_intern("each_char"), 0, 0, rb_yield, 0);
7733 next_p = 1;
7737 static VALUE
7738 argf_filename(VALUE argf)
7740 next_argv();
7741 return filename;
7744 static VALUE
7745 argf_filename_getter(ID id, VALUE *var)
7747 return argf_filename(*var);
7750 static VALUE
7751 argf_file(VALUE argf)
7753 next_argv();
7754 return current_file;
7757 static VALUE
7758 argf_binmode_m(VALUE argf)
7760 argf_binmode = 1;
7761 next_argv();
7762 ARGF_FORWARD(0, 0);
7763 rb_io_binmode(current_file);
7764 return argf;
7767 static VALUE
7768 argf_binmode_p(VALUE argf)
7770 return argf_binmode ? Qtrue : Qfalse;
7773 static VALUE
7774 argf_skip(VALUE argf)
7776 if (next_p != -1) {
7777 argf_close(current_file);
7778 next_p = 1;
7780 return argf;
7783 static VALUE
7784 argf_close_m(VALUE argf)
7786 next_argv();
7787 argf_close(current_file);
7788 if (next_p != -1) {
7789 next_p = 1;
7791 gets_lineno = 0;
7792 return argf;
7795 static VALUE
7796 argf_closed(VALUE argf)
7798 next_argv();
7799 ARGF_FORWARD(0, 0);
7800 return rb_io_closed(current_file);
7803 static VALUE
7804 argf_to_s(VALUE argf)
7806 return rb_str_new2("ARGF");
7809 static VALUE
7810 argf_inplace_mode_get(VALUE argf)
7812 if (!ruby_inplace_mode) return Qnil;
7813 return rb_str_new2(ruby_inplace_mode);
7816 static VALUE
7817 opt_i_get(ID id, VALUE *var)
7819 return argf_inplace_mode_get(*var);
7822 static VALUE
7823 argf_inplace_mode_set(VALUE argf, VALUE val)
7825 if (!RTEST(val)) {
7826 if (ruby_inplace_mode) free(ruby_inplace_mode);
7827 ruby_inplace_mode = 0;
7829 else {
7830 StringValue(val);
7831 if (ruby_inplace_mode) free(ruby_inplace_mode);
7832 ruby_inplace_mode = 0;
7833 ruby_inplace_mode = strdup(RSTRING_PTR(val));
7835 return argf;
7838 static void
7839 opt_i_set(VALUE val, ID id, VALUE *var)
7841 argf_inplace_mode_set(*var, val);
7844 const char *
7845 ruby_get_inplace_mode(void)
7847 return ruby_inplace_mode;
7850 void
7851 ruby_set_inplace_mode(const char *suffix)
7853 if (ruby_inplace_mode) free(ruby_inplace_mode);
7854 ruby_inplace_mode = 0;
7855 if (suffix) ruby_inplace_mode = strdup(suffix);
7858 static VALUE
7859 argf_argv(VALUE argf)
7861 return rb_argv;
7864 static VALUE
7865 argf_argv_getter(ID id, VALUE *var)
7867 return argf_argv(*var);
7870 VALUE
7871 rb_get_argv(void)
7873 return rb_argv;
7877 * Class <code>IO</code> is the basis for all input and output in Ruby.
7878 * An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
7879 * so may use more than one native operating system stream.
7881 * Many of the examples in this section use class <code>File</code>,
7882 * the only standard subclass of <code>IO</code>. The two classes are
7883 * closely associated.
7885 * As used in this section, <em>portname</em> may take any of the
7886 * following forms.
7888 * * A plain string represents a filename suitable for the underlying
7889 * operating system.
7891 * * A string starting with ``<code>|</code>'' indicates a subprocess.
7892 * The remainder of the string following the ``<code>|</code>'' is
7893 * invoked as a process with appropriate input/output channels
7894 * connected to it.
7896 * * A string equal to ``<code>|-</code>'' will create another Ruby
7897 * instance as a subprocess.
7899 * Ruby will convert pathnames between different operating system
7900 * conventions if possible. For instance, on a Windows system the
7901 * filename ``<code>/gumby/ruby/test.rb</code>'' will be opened as
7902 * ``<code>\gumby\ruby\test.rb</code>''. When specifying a
7903 * Windows-style filename in a Ruby string, remember to escape the
7904 * backslashes:
7906 * "c:\\gumby\\ruby\\test.rb"
7908 * Our examples here will use the Unix-style forward slashes;
7909 * <code>File::SEPARATOR</code> can be used to get the
7910 * platform-specific separator character.
7912 * I/O ports may be opened in any one of several different modes, which
7913 * are shown in this section as <em>mode</em>. The mode may
7914 * either be a Fixnum or a String. If numeric, it should be
7915 * one of the operating system specific constants (O_RDONLY,
7916 * O_WRONLY, O_RDWR, O_APPEND and so on). See man open(2) for
7917 * more information.
7919 * If the mode is given as a String, it must be one of the
7920 * values listed in the following table.
7922 * Mode | Meaning
7923 * -----+--------------------------------------------------------
7924 * "r" | Read-only, starts at beginning of file (default mode).
7925 * -----+--------------------------------------------------------
7926 * "r+" | Read-write, starts at beginning of file.
7927 * -----+--------------------------------------------------------
7928 * "w" | Write-only, truncates existing file
7929 * | to zero length or creates a new file for writing.
7930 * -----+--------------------------------------------------------
7931 * "w+" | Read-write, truncates existing file to zero length
7932 * | or creates a new file for reading and writing.
7933 * -----+--------------------------------------------------------
7934 * "a" | Write-only, starts at end of file if file exists,
7935 * | otherwise creates a new file for writing.
7936 * -----+--------------------------------------------------------
7937 * "a+" | Read-write, starts at end of file if file exists,
7938 * | otherwise creates a new file for reading and
7939 * | writing.
7940 * -----+--------------------------------------------------------
7941 * "b" | (DOS/Windows only) Binary file mode (may appear with
7942 * | any of the key letters listed above).
7945 * The global constant ARGF (also accessible as $<) provides an
7946 * IO-like stream which allows access to all files mentioned on the
7947 * command line (or STDIN if no files are mentioned). ARGF provides
7948 * the methods <code>#path</code> and <code>#filename</code> to access
7949 * the name of the file currently being read.
7952 void
7953 Init_IO(void)
7955 #undef rb_intern
7956 #define rb_intern(str) rb_intern_const(str)
7958 VALUE rb_cARGF;
7959 #ifdef __CYGWIN__
7960 #include <sys/cygwin.h>
7961 static struct __cygwin_perfile pf[] =
7963 {"", O_RDONLY | O_BINARY},
7964 {"", O_WRONLY | O_BINARY},
7965 {"", O_RDWR | O_BINARY},
7966 {"", O_APPEND | O_BINARY},
7967 {NULL, 0}
7969 cygwin_internal(CW_PERFILE, pf);
7970 #endif
7972 rb_eIOError = rb_define_class("IOError", rb_eStandardError);
7973 rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
7975 id_write = rb_intern("write");
7976 id_read = rb_intern("read");
7977 id_getc = rb_intern("getc");
7978 id_flush = rb_intern("flush");
7979 id_readpartial = rb_intern("readpartial");
7981 rb_define_global_function("syscall", rb_f_syscall, -1);
7983 rb_define_global_function("open", rb_f_open, -1);
7984 rb_define_global_function("printf", rb_f_printf, -1);
7985 rb_define_global_function("print", rb_f_print, -1);
7986 rb_define_global_function("putc", rb_f_putc, 1);
7987 rb_define_global_function("puts", rb_f_puts, -1);
7988 rb_define_global_function("gets", rb_f_gets, -1);
7989 rb_define_global_function("readline", rb_f_readline, -1);
7990 rb_define_global_function("select", rb_f_select, -1);
7992 rb_define_global_function("readlines", rb_f_readlines, -1);
7994 rb_define_global_function("`", rb_f_backquote, 1);
7996 rb_define_global_function("p", rb_f_p, -1);
7997 rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
7999 rb_cIO = rb_define_class("IO", rb_cObject);
8000 rb_include_module(rb_cIO, rb_mEnumerable);
8002 rb_define_alloc_func(rb_cIO, io_alloc);
8003 rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
8004 rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1);
8005 rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
8006 rb_define_singleton_method(rb_cIO, "for_fd", rb_io_s_for_fd, -1);
8007 rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
8008 rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
8009 rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
8010 rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
8011 rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
8012 rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
8013 rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
8014 rb_define_singleton_method(rb_cIO, "copy_stream", rb_io_s_copy_stream, -1);
8016 rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
8018 rb_output_fs = Qnil;
8019 rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
8021 rb_global_variable(&rb_default_rs);
8022 rb_rs = rb_default_rs = rb_str_new2("\n");
8023 rb_output_rs = Qnil;
8024 OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
8025 rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
8026 rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
8027 rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
8029 rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
8031 rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
8032 rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
8034 rb_define_method(rb_cIO, "print", rb_io_print, -1);
8035 rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
8036 rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
8037 rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
8039 rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
8040 rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
8041 rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
8042 rb_define_method(rb_cIO, "each_char", rb_io_each_char, 0);
8043 rb_define_method(rb_cIO, "lines", rb_io_lines, -1);
8044 rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0);
8045 rb_define_method(rb_cIO, "chars", rb_io_chars, 0);
8047 rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
8048 rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
8050 rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
8051 rb_define_alias(rb_cIO, "to_i", "fileno");
8052 rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
8054 rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
8055 rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
8056 rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
8058 rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
8059 rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
8061 rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
8063 rb_define_method(rb_cIO, "read_nonblock", io_read_nonblock, -1);
8064 rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, 1);
8065 rb_define_method(rb_cIO, "readpartial", io_readpartial, -1);
8066 rb_define_method(rb_cIO, "read", io_read, -1);
8067 rb_define_method(rb_cIO, "write", io_write, 1);
8068 rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
8069 rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
8070 rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
8071 rb_define_method(rb_cIO, "getbyte", rb_io_getbyte, 0);
8072 rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
8073 rb_define_method(rb_cIO, "readbyte", rb_io_readbyte, 0);
8074 rb_define_method(rb_cIO, "ungetbyte",rb_io_ungetbyte, 1);
8075 rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
8076 rb_define_method(rb_cIO, "<<", rb_io_addstr, 1);
8077 rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
8078 rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
8079 rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
8080 rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
8081 rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
8082 rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
8083 rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
8084 rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
8085 rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
8086 rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
8087 rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
8089 rb_define_method(rb_cIO, "close_on_exec?", rb_io_close_on_exec_p, 0);
8090 rb_define_method(rb_cIO, "close_on_exec=", rb_io_set_close_on_exec, 1);
8092 rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
8093 rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
8094 rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
8095 rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
8097 rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
8098 rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
8099 rb_define_method(rb_cIO, "binmode", rb_io_binmode_m, 0);
8100 rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
8101 rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
8103 rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
8104 rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
8105 rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
8106 rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
8108 rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
8109 rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
8110 rb_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
8112 rb_define_variable("$stdin", &rb_stdin);
8113 rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
8114 rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
8115 rb_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO, "<STDOUT>");
8116 rb_define_hooked_variable("$stderr", &rb_stderr, 0, stdout_setter);
8117 rb_stderr = prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
8118 rb_define_hooked_variable("$>", &rb_stdout, 0, stdout_setter);
8119 orig_stdout = rb_stdout;
8120 rb_deferr = orig_stderr = rb_stderr;
8122 /* constants to hold original stdin/stdout/stderr */
8123 rb_define_global_const("STDIN", rb_stdin);
8124 rb_define_global_const("STDOUT", rb_stdout);
8125 rb_define_global_const("STDERR", rb_stderr);
8127 rb_cARGF = rb_class_new(rb_cObject);
8128 rb_set_class_path(rb_cARGF, rb_cObject, "ARGF.class");
8129 rb_define_alloc_func(rb_cARGF, argf_alloc);
8131 rb_include_module(rb_cARGF, rb_mEnumerable);
8133 rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
8134 rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
8135 rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
8136 rb_define_method(rb_cARGF, "argv", argf_argv, 0);
8138 rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
8139 rb_define_method(rb_cARGF, "to_i", argf_fileno, 0);
8140 rb_define_method(rb_cARGF, "to_io", argf_to_io, 0);
8141 rb_define_method(rb_cARGF, "each", argf_each_line, -1);
8142 rb_define_method(rb_cARGF, "each_line", argf_each_line, -1);
8143 rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0);
8144 rb_define_method(rb_cARGF, "each_char", argf_each_char, 0);
8145 rb_define_method(rb_cARGF, "lines", argf_each_line, -1);
8146 rb_define_method(rb_cARGF, "bytes", argf_each_byte, 0);
8147 rb_define_method(rb_cARGF, "chars", argf_each_char, 0);
8149 rb_define_method(rb_cARGF, "read", argf_read, -1);
8150 rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1);
8151 rb_define_method(rb_cARGF, "readlines", argf_readlines, -1);
8152 rb_define_method(rb_cARGF, "to_a", argf_readlines, -1);
8153 rb_define_method(rb_cARGF, "gets", argf_gets, -1);
8154 rb_define_method(rb_cARGF, "readline", argf_readline, -1);
8155 rb_define_method(rb_cARGF, "getc", argf_getc, 0);
8156 rb_define_method(rb_cARGF, "getbyte", argf_getbyte, 0);
8157 rb_define_method(rb_cARGF, "readchar", argf_readchar, 0);
8158 rb_define_method(rb_cARGF, "readbyte", argf_readbyte, 0);
8159 rb_define_method(rb_cARGF, "tell", argf_tell, 0);
8160 rb_define_method(rb_cARGF, "seek", argf_seek_m, -1);
8161 rb_define_method(rb_cARGF, "rewind", argf_rewind, 0);
8162 rb_define_method(rb_cARGF, "pos", argf_tell, 0);
8163 rb_define_method(rb_cARGF, "pos=", argf_set_pos, 1);
8164 rb_define_method(rb_cARGF, "eof", argf_eof, 0);
8165 rb_define_method(rb_cARGF, "eof?", argf_eof, 0);
8166 rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
8167 rb_define_method(rb_cARGF, "binmode?", argf_binmode_p, 0);
8169 rb_define_method(rb_cARGF, "filename", argf_filename, 0);
8170 rb_define_method(rb_cARGF, "path", argf_filename, 0);
8171 rb_define_method(rb_cARGF, "file", argf_file, 0);
8172 rb_define_method(rb_cARGF, "skip", argf_skip, 0);
8173 rb_define_method(rb_cARGF, "close", argf_close_m, 0);
8174 rb_define_method(rb_cARGF, "closed?", argf_closed, 0);
8176 rb_define_method(rb_cARGF, "lineno", argf_lineno, 0);
8177 rb_define_method(rb_cARGF, "lineno=", argf_set_lineno, 1);
8179 rb_define_method(rb_cARGF, "inplace_mode", argf_inplace_mode_get, 0);
8180 rb_define_method(rb_cARGF, "inplace_mode=", argf_inplace_mode_set, 1);
8182 rb_define_method(rb_cARGF, "external_encoding", argf_external_encoding, 0);
8183 rb_define_method(rb_cARGF, "internal_encoding", argf_internal_encoding, 0);
8184 rb_define_method(rb_cARGF, "set_encoding", argf_set_encoding, -1);
8186 argf = rb_class_new_instance(0, 0, rb_cARGF);
8188 rb_define_readonly_variable("$<", &argf);
8189 rb_define_global_const("ARGF", argf);
8191 rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
8192 rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, 0);
8193 filename = rb_str_new2("-");
8195 rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
8196 rb_define_hooked_variable("$*", &argf, argf_argv_getter, 0);
8198 #if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
8199 atexit(pipe_atexit);
8200 #endif
8202 Init_File();
8204 rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
8206 rb_file_const("RDONLY", INT2FIX(O_RDONLY));
8207 rb_file_const("WRONLY", INT2FIX(O_WRONLY));
8208 rb_file_const("RDWR", INT2FIX(O_RDWR));
8209 rb_file_const("APPEND", INT2FIX(O_APPEND));
8210 rb_file_const("CREAT", INT2FIX(O_CREAT));
8211 rb_file_const("EXCL", INT2FIX(O_EXCL));
8212 #if defined(O_NDELAY) || defined(O_NONBLOCK)
8213 # ifdef O_NONBLOCK
8214 rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK));
8215 # else
8216 rb_file_const("NONBLOCK", INT2FIX(O_NDELAY));
8217 # endif
8218 #endif
8219 rb_file_const("TRUNC", INT2FIX(O_TRUNC));
8220 #ifdef O_NOCTTY
8221 rb_file_const("NOCTTY", INT2FIX(O_NOCTTY));
8222 #endif
8223 #ifdef O_BINARY
8224 rb_file_const("BINARY", INT2FIX(O_BINARY));
8225 #else
8226 rb_file_const("BINARY", INT2FIX(0));
8227 #endif
8228 #ifdef O_SYNC
8229 rb_file_const("SYNC", INT2FIX(O_SYNC));
8230 #endif
8232 sym_mode = ID2SYM(rb_intern("mode"));
8233 sym_perm = ID2SYM(rb_intern("perm"));
8234 sym_extenc = ID2SYM(rb_intern("external_encoding"));
8235 sym_intenc = ID2SYM(rb_intern("internal_encoding"));
8236 sym_encoding = ID2SYM(rb_intern("encoding"));
8237 sym_open_args = ID2SYM(rb_intern("open_args"));