1 /**********************************************************************
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"
16 #include "ruby/signal.h"
21 #define free(x) xfree(x)
23 #if defined(DOSISH) || defined(__CYGWIN__)
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>
34 #if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
35 # define NO_SAFE_RENAME
38 #if defined(MSDOS) || defined(__CYGWIN__) || defined(_WIN32)
39 # define NO_LONG_FNAME
42 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(sun) || defined(_nec_ews)
50 #include <sys/types.h>
51 #if defined(HAVE_SYS_IOCTL_H) && !defined(DJGPP) && !defined(_WIN32) && !defined(__human68k__)
52 #include <sys/ioctl.h>
54 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
56 #elif defined(HAVE_SYS_FCNTL_H)
57 #include <sys/fcntl.h>
60 #if !HAVE_OFF_T && !defined(off_t)
66 /* EMX has sys/param.h, but.. */
67 #if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
68 # include <sys/param.h>
81 #elif defined HAVE_SYS_SYSCALL_H
82 #include <sys/syscall.h>
85 extern void Init_File(void);
89 # define NOFILE (OPEN_MAX)
93 #include "ruby/util.h"
96 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
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...
104 # ifdef _POSIX_PIPE_BUF
105 # define PIPE_BUF _POSIX_PIPE_BUF
107 # define PIPE_BUF 512 /* is this ok? */
115 VALUE rb_stdin
, rb_stdout
, rb_stderr
;
116 VALUE rb_deferr
; /* rescue VIM plugin */
117 static VALUE orig_stdout
, orig_stderr
;
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
);
132 VALUE filename
, current_file
;
139 rb_encoding
*enc
, *enc2
;
142 static int max_file_descriptor
= NOFILE
;
143 #define UPDATE_MAXFD(fd) \
145 if (max_file_descriptor < (fd)) max_file_descriptor = (fd); \
148 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
149 #define ARGF argf_of(argf)
151 #ifdef _STDIO_USES_IOSTREAM /* GNU libc */
153 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
155 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
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)
164 # define STDIO_READ_DATA_PENDING(fp) (((unsigned int)(*(fp))->_cnt) > 0)
166 # define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
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")
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);\
190 # define S_ISSOCK(m) _S_ISSOCK(m)
193 # define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
196 # define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
202 #if !defined HAVE_SHUTDOWN && !defined shutdown
203 #define shutdown(a,b) 0
207 #define is_socket(fd, path) rb_w32_is_socket(fd)
208 #elif !defined(S_ISSOCK)
209 #define is_socket(fd, path) 0
212 is_socket(int fd
, const char *path
)
215 if (fstat(fd
, &sbuf
) < 0)
217 return S_ISSOCK(sbuf
.st_mode
);
224 rb_raise(rb_eEOFError
, "end of file reached");
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");
237 rb_io_check_initialized(rb_io_t
*fptr
)
240 rb_raise(rb_eIOError
, "uninitialized stream");
245 rb_io_check_closed(rb_io_t
*fptr
)
247 rb_io_check_initialized(fptr
);
249 rb_raise(rb_eIOError
, "closed stream");
253 static int io_fflush(rb_io_t
*);
256 rb_io_get_io(VALUE io
)
258 return rb_convert_type(io
, T_FILE
, "IO", "to_io");
262 rb_io_check_io(VALUE io
)
264 return rb_check_convert_type(io
, T_FILE
, "IO", "to_io");
268 rb_io_get_write_io(VALUE io
)
271 rb_io_check_initialized(RFILE(io
)->fptr
);
272 write_io
= RFILE(io
)->fptr
->tied_io_for_writing
;
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
287 * IO.try_convert(STDOUT) # => STDOUT
288 * IO.try_convert("STDOUT") # => nil
291 rb_io_s_try_convert(VALUE dummy
, VALUE io
)
293 return rb_io_check_io(io
);
297 io_unread(rb_io_t
*fptr
)
300 rb_io_check_closed(fptr
);
301 if (fptr
->rbuf_len
== 0 || fptr
->mode
& FMODE_DUPLEX
)
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')
311 r
= lseek(fptr
->fd
, -len
, SEEK_CUR
);
315 r
= lseek(fptr
->fd
, -fptr
->rbuf_len
, SEEK_CUR
);
318 fptr
->mode
|= FMODE_DUPLEX
;
326 static rb_encoding
*io_input_encoding(rb_io_t
*fptr
);
329 io_ungetbyte(VALUE str
, rb_io_t
*fptr
)
331 int len
= RSTRING_LEN(str
);
333 if (fptr
->rbuf
== NULL
) {
337 fptr
->rbuf_capa
= len
;
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
;
353 MEMMOVE(fptr
->rbuf
+fptr
->rbuf_off
, RSTRING_PTR(str
), char, len
);
357 flush_before_seek(rb_io_t
*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)
374 #define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
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
) {
386 if (fptr
->tied_io_for_writing
) {
388 GetOpenFile(fptr
->tied_io_for_writing
, wfptr
);
394 io_read_encoding(rb_io_t
*fptr
)
399 return rb_default_external_encoding();
403 io_input_encoding(rb_io_t
*fptr
)
408 return io_read_encoding(fptr
);
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
) {
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
);
436 rb_read_check(FILE *fp
)
438 if (!STDIO_READ_DATA_PENDING(fp
)) {
439 rb_thread_wait_fd(fileno(fp
));
444 rb_io_read_check(rb_io_t
*fptr
)
446 if (!READ_DATA_PENDING(fptr
)) {
447 rb_thread_wait_fd(fptr
->fd
);
459 if (errno
== EMFILE
|| errno
== ENFILE
|| errno
== ENOMEM
) {
471 io_alloc(VALUE klass
)
473 NEWOBJ(io
, struct RFile
);
474 OBJSETUP(io
, klass
, T_FILE
);
482 # define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
486 wsplit_p(rb_io_t
*fptr
)
488 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
492 if (!(fptr
->mode
& FMODE_WSPLIT_INITIALIZED
)) {
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 &&
501 fptr
->mode
|= FMODE_WSPLIT
;
503 fptr
->mode
|= FMODE_WSPLIT_INITIALIZED
;
505 return fptr
->mode
& FMODE_WSPLIT
;
508 struct io_internal_struct
{
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
);
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
);
529 rb_read_internal(int fd
, void *buf
, size_t count
)
531 struct io_internal_struct iis
;
536 return rb_thread_blocking_region(internal_read_func
, &iis
, RB_UBF_DFL
, 0);
540 rb_write_internal(int fd
, void *buf
, size_t count
)
542 struct io_internal_struct iis
;
547 return rb_thread_blocking_region(internal_write_func
, &iis
, RB_UBF_DFL
, 0);
551 io_fflush(rb_io_t
*fptr
)
554 int wbuf_off
, wbuf_len
;
556 rb_io_check_closed(fptr
);
557 if (fptr
->wbuf_len
== 0)
559 if (!rb_thread_fd_writable(fptr
->fd
)) {
560 rb_io_check_closed(fptr
);
563 if (fptr
->wbuf_len
== 0)
565 wbuf_off
= fptr
->wbuf_off
;
566 wbuf_len
= fptr
->wbuf_len
;
569 !rb_thread_critical
&&
570 !rb_thread_alone() &&
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
) {
588 if (rb_io_wait_writable(fptr
->fd
)) {
589 rb_io_check_closed(fptr
);
595 #ifdef HAVE_RB_FD_INIT
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
);
606 rb_io_wait_readable(int f
)
611 rb_raise(rb_eIOError
, "closed stream");
615 #if defined(ERESTART)
618 rb_thread_wait_fd(f
);
622 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
627 #ifdef HAVE_RB_FD_INIT
628 rb_ensure(wait_readable
, (VALUE
)&rfds
,
629 (VALUE (*)(VALUE
))rb_fd_term
, (VALUE
)&rfds
);
631 rb_thread_select(f
+ 1, &rfds
, NULL
, NULL
, NULL
);
640 #ifdef HAVE_RB_FD_INIT
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
);
651 rb_io_wait_writable(int f
)
656 rb_raise(rb_eIOError
, "closed stream");
660 #if defined(ERESTART)
663 rb_thread_fd_writable(f
);
667 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
672 #ifdef HAVE_RB_FD_INIT
673 rb_ensure(wait_writable
, (VALUE
)&wfds
,
674 (VALUE (*)(VALUE
))rb_fd_term
, (VALUE
)&wfds
);
676 rb_thread_select(f
+ 1, NULL
, &wfds
, NULL
, NULL
);
686 make_writeconv(rb_io_t
*fptr
)
688 if (!fptr
->writeconv_initialized
) {
689 const char *senc
, *denc
;
692 enc
= fptr
->enc2
? fptr
->enc2
: fptr
->enc
;
693 senc
= rb_econv_stateless_encoding(enc
->name
);
696 fptr
->writeconv_stateless
= rb_str_new2(senc
);
700 fptr
->writeconv_stateless
= Qnil
;
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
);
708 fptr
->writeconv
= NULL
;
710 fptr
->writeconv_initialized
= 1;
714 /* writing functions */
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.
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
);
732 str
= rb_str_transcode(str
, rb_enc_from_encoding(fptr
->enc2
));
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
)) {
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
);
754 MEMMOVE(fptr
->wbuf
+fptr
->wbuf_off
+fptr
->wbuf_len
, RSTRING_PTR(str
)+offset
, char, len
);
755 fptr
->wbuf_len
+= len
;
758 if (io_fflush(fptr
) < 0)
762 /* avoid context switch between "a" and "\n" in STDERR.puts "a".
764 if (fptr
->stdio_file
!= stderr
&& !rb_thread_fd_writable(fptr
->fd
)) {
765 rb_io_check_closed(fptr
);
770 !rb_thread_critical
&&
771 !rb_thread_alone() &&
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
;
783 if (rb_io_wait_writable(fptr
->fd
)) {
784 rb_io_check_closed(fptr
);
785 if (offset
< RSTRING_LEN(str
))
791 if (fptr
->wbuf_off
) {
793 MEMMOVE(fptr
->wbuf
, fptr
->wbuf
+fptr
->wbuf_off
, char, fptr
->wbuf_len
);
796 MEMMOVE(fptr
->wbuf
+fptr
->wbuf_off
+fptr
->wbuf_len
, RSTRING_PTR(str
)+offset
, char, len
);
797 fptr
->wbuf_len
+= len
;
802 rb_io_fwrite(const char *ptr
, long len
, FILE *f
)
808 of
.mode
= FMODE_WRITABLE
;
810 return io_fwrite(rb_str_new(ptr
, len
), &of
);
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
822 * count = $stdout.write( "This is a test\n" )
823 * puts "That was #{count} bytes of data"
828 * That was 15 bytes of data
832 io_write(VALUE io
, VALUE str
)
840 str
= rb_obj_as_string(str
);
841 tmp
= rb_io_check_io(io
);
843 /* port is not IO, call write method for it. */
844 return rb_funcall(io
, id_write
, 1, str
);
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
);
859 rb_io_write(VALUE io
, VALUE str
)
861 return rb_funcall(io
, id_write
, 1, str
);
868 * String Output---Writes <i>obj</i> to <em>ios</em>.
869 * <i>obj</i> will be converted to a string using
872 * $stdout << "Hello " << "world!\n"
881 rb_io_addstr(VALUE io
, VALUE str
)
883 rb_io_write(io
, str
);
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"
904 rb_io_flush(VALUE io
)
908 if (TYPE(io
) != T_FILE
) {
909 return rb_funcall(io
, id_flush
, 0);
913 GetOpenFile(io
, fptr
);
915 if (fptr
->mode
& FMODE_WRITABLE
) {
918 if (fptr
->mode
& FMODE_READABLE
) {
928 * ios.tell => integer
930 * Returns the current offset (in bytes) of <em>ios</em>.
932 * f = File.new("testfile")
934 * f.gets #=> "This is line one\n"
944 GetOpenFile(io
, fptr
);
946 if (pos
< 0 && errno
) rb_sys_fail(fptr
->path
);
947 return OFFT2NUM(pos
);
951 rb_io_seek(VALUE io
, VALUE offset
, int whence
)
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
);
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_
980 * f = File.new("testfile")
981 * f.seek(-13, IO::SEEK_END) #=> 0
982 * f.readline #=> "And so on...\n"
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
);
1000 * ios.pos = integer => integer
1002 * Seeks to the given position (in bytes) in <em>ios</em>.
1004 * f = File.new("testfile")
1006 * f.gets #=> "This is line two\n"
1010 rb_io_set_pos(VALUE io
, VALUE offset
)
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
);
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"
1034 * f.readline #=> "This is line one\n"
1038 rb_io_rewind(VALUE io
)
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
;
1053 io_fillbuf(rb_io_t
*fptr
)
1057 if (fptr
->rbuf
== NULL
) {
1060 fptr
->rbuf_capa
= 8192;
1061 fptr
->rbuf
= ALLOC_N(char, fptr
->rbuf_capa
);
1063 if (fptr
->rbuf_len
== 0) {
1066 r
= rb_read_internal(fptr
->fd
, fptr
->rbuf
, fptr
->rbuf_capa
);
1069 if (rb_io_wait_readable(fptr
->fd
))
1071 rb_sys_fail(fptr
->path
);
1076 return -1; /* EOF */
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
1091 * f = File.new("testfile")
1092 * dummy = f.readlines
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.
1099 * Thread.new { sleep 1; w.close }
1100 * r.eof? #=> true after 1 second blocking
1103 * Thread.new { sleep 1; w.puts "a" }
1104 * r.eof? #=> false after 1 second blocking
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>.
1118 GetOpenFile(io
, fptr
);
1119 rb_io_check_readable(fptr
);
1121 if (READ_DATA_PENDING(fptr
)) return Qfalse
;
1123 if (io_fillbuf(fptr
) < 0) {
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")
1143 rb_io_sync(VALUE io
)
1147 io
= GetWriteIO(io
);
1148 GetOpenFile(io
, fptr
);
1149 return (fptr
->mode
& FMODE_SYNC
) ? Qtrue
: Qfalse
;
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")
1164 * <em>(produces no output)</em>
1168 rb_io_set_sync(VALUE io
, VALUE mode
)
1172 io
= GetWriteIO(io
);
1173 GetOpenFile(io
, fptr
);
1175 fptr
->mode
|= FMODE_SYNC
;
1178 fptr
->mode
&= ~FMODE_SYNC
;
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.
1196 rb_io_fsync(VALUE io
)
1201 io
= GetWriteIO(io
);
1202 GetOpenFile(io
, fptr
);
1205 if (fsync(fptr
->fd
) < 0)
1206 rb_sys_fail(fptr
->path
);
1210 return Qnil
; /* not reached */
1216 * ios.fileno => fixnum
1217 * ios.to_i => fixnum
1219 * Returns an integer representing the numeric file descriptor for
1222 * $stdin.fileno #=> 0
1223 * $stdout.fileno #=> 1
1227 rb_io_fileno(VALUE io
)
1232 GetOpenFile(io
, fptr
);
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("-")
1247 * $stderr.puts "In parent, child pid is #{pipe.pid}"
1249 * $stderr.puts "In child, pid is #{$$}"
1252 * <em>produces:</em>
1254 * In child, pid is 26209
1255 * In parent, child pid is 26209
1263 GetOpenFile(io
, fptr
);
1266 return INT2FIX(fptr
->pid
);
1272 * ios.inspect => string
1274 * Return a string describing this IO object.
1278 rb_io_inspect(VALUE obj
)
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
);
1290 return rb_sprintf("#<%s:%s%s>", cname
, fptr
->path
, st
);
1297 * Returns <em>ios</em>.
1301 rb_io_to_io(VALUE io
)
1306 /* reading functions */
1308 read_buffered_data(char *ptr
, long len
, rb_io_t
*fptr
)
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
;
1322 io_fread(VALUE str
, long offset
, rb_io_t
*fptr
)
1324 long len
= RSTRING_LEN(str
) - offset
;
1328 if (READ_DATA_PENDING(fptr
) == 0) {
1330 c
= rb_read_internal(fptr
->fd
, RSTRING_PTR(str
)+offset
, n
);
1333 rb_sys_fail(fptr
->path
);
1336 if ((n
-= c
) <= 0) break;
1337 rb_thread_wait_fd(fptr
->fd
);
1343 c
= read_buffered_data(RSTRING_PTR(str
)+offset
, n
, fptr
);
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) {
1358 rb_io_fread(char *ptr
, long len
, 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
);
1373 #define SMALLBUF 100
1376 remain_size(rb_io_t
*fptr
)
1379 off_t siz
= READ_DATA_PENDING_COUNT(fptr
);
1382 if (fstat(fptr
->fd
, &st
) == 0 && S_ISREG(st
.st_mode
)
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");
1404 io_enc_str(VALUE str
, rb_io_t
*fptr
)
1407 rb_enc_associate(str
, io_read_encoding(fptr
));
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
);
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
;
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
;
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
);
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
)
1467 if (res
== econv_finished
)
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
);
1487 io_shift_crbuf(rb_io_t
*fptr
, int len
, VALUE
*strp
)
1491 *strp
= str
= rb_str_new(fptr
->crbuf
+fptr
->crbuf_off
, len
);
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
;
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;
1515 read_all(rb_io_t
*fptr
, long siz
, VALUE str
)
1524 VALUE str
= rb_str_new(NULL
, 0);
1525 make_readconv(fptr
);
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
);
1539 enc
= io_read_encoding(fptr
);
1540 cr
= fptr
->enc2
? ENC_CODERANGE_BROKEN
: 0;
1542 if (siz
== 0) siz
= BUFSIZ
;
1544 str
= rb_str_new(0, siz
);
1547 rb_str_resize(str
, siz
);
1551 n
= io_fread(str
, bytes
, fptr
);
1552 if (n
== 0 && bytes
== 0) {
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;
1560 rb_str_resize(str
, siz
);
1562 if (bytes
!= siz
) rb_str_resize(str
, bytes
);
1563 str
= io_enc_str(str
, fptr
);
1565 ENC_CODERANGE_SET(str
, cr
);
1571 rb_io_set_nonblock(rb_io_t
*fptr
)
1575 flags
= fcntl(fptr
->fd
, F_GETFL
);
1577 rb_sys_fail(fptr
->path
);
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
);
1591 io_getpartial(int argc
, VALUE
*argv
, VALUE io
, int nonblock
)
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
);
1604 str
= rb_str_new(0, len
);
1609 rb_str_resize(str
, len
);
1613 GetOpenFile(io
, fptr
);
1614 rb_io_check_readable(fptr
);
1621 if (RSTRING_LEN(str
) != len
) {
1623 rb_raise(rb_eRuntimeError
, "buffer string modified");
1625 n
= read_buffered_data(RSTRING_PTR(str
), len
, fptr
);
1628 if (RSTRING_LEN(str
) != len
) goto modified
;
1630 rb_io_set_nonblock(fptr
);
1632 n
= rb_read_internal(fptr
->fd
, RSTRING_PTR(str
), len
);
1634 if (!nonblock
&& rb_io_wait_readable(fptr
->fd
))
1636 rb_sys_fail(fptr
->path
);
1639 rb_str_resize(str
, n
);
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.
1705 io_readpartial(int argc
, VALUE
*argv
, VALUE io
)
1709 ret
= io_getpartial(argc
, argv
, io
, 0);
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.
1741 io_read_nonblock(int argc
, VALUE
*argv
, VALUE io
)
1745 ret
= io_getpartial(argc
, argv
, io
, 1);
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.
1770 rb_io_write_nonblock(VALUE io
, VALUE str
)
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
);
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
);
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"
1816 io_read(int argc
, VALUE
*argv
, VALUE io
)
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
);
1832 rb_raise(rb_eArgError
, "negative length %ld given", len
);
1836 str
= rb_str_new(0, len
);
1841 rb_str_resize(str
,len
);
1844 GetOpenFile(io
, fptr
);
1845 rb_io_check_readable(fptr
);
1846 if (len
== 0) return str
;
1849 if (RSTRING_LEN(str
) != len
) {
1850 rb_raise(rb_eRuntimeError
, "buffer string modified");
1852 n
= io_fread(str
, 0, fptr
);
1854 if (fptr
->fd
< 0) return Qnil
;
1855 rb_str_resize(str
, 0);
1858 rb_str_resize(str
, n
);
1864 rscheck(const char *rsptr
, long rslen
, VALUE rs
)
1867 if (RSTRING_PTR(rs
) != rsptr
&& RSTRING_LEN(rs
) != rslen
)
1868 rb_raise(rb_eRuntimeError
, "rs modified");
1872 appendline(rb_io_t
*fptr
, int delim
, VALUE
*strp
, long *lp
)
1878 make_readconv(fptr
);
1882 if (fptr
->crbuf_len
) {
1883 p
= fptr
->crbuf
+fptr
->crbuf_off
;
1884 searchlen
= fptr
->crbuf_len
;
1885 if (0 < limit
&& limit
< searchlen
)
1887 e
= memchr(p
, delim
, searchlen
);
1890 *strp
= str
= rb_str_new(p
, e
-p
+1);
1892 rb_str_buf_cat(str
, p
, e
-p
+1);
1893 fptr
->crbuf_off
+= e
-p
+1;
1894 fptr
->crbuf_len
-= e
-p
+1;
1901 *strp
= str
= rb_str_new(p
, searchlen
);
1903 rb_str_buf_cat(str
, p
, searchlen
);
1904 fptr
->crbuf_off
+= searchlen
;
1905 fptr
->crbuf_len
-= searchlen
;
1910 return (unsigned char)RSTRING_PTR(str
)[RSTRING_LEN(str
)-1];
1914 if (more_char(fptr
) == -1) {
1922 long pending
= READ_DATA_PENDING_COUNT(fptr
);
1924 const char *p
= READ_DATA_PENDING_PTR(fptr
);
1928 if (limit
> 0 && pending
> limit
) pending
= limit
;
1929 e
= memchr(p
, delim
, pending
);
1930 if (e
) pending
= e
- p
+ 1;
1932 last
= RSTRING_LEN(str
);
1933 rb_str_resize(str
, last
+ pending
);
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 */
1943 if (e
) return delim
;
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) {
1957 swallow(rb_io_t
*fptr
, int term
)
1961 while ((cnt
= READ_DATA_PENDING_COUNT(fptr
)) > 0) {
1963 const char *p
= READ_DATA_PENDING_PTR(fptr
);
1965 if (cnt
> sizeof buf
) cnt
= sizeof buf
;
1966 if (*p
!= term
) return Qtrue
;
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);
1979 rb_io_getline_fast(rb_io_t
*fptr
, rb_encoding
*enc
)
1984 int cr
= fptr
->enc2
? ENC_CODERANGE_BROKEN
: 0;
1987 long pending
= READ_DATA_PENDING_COUNT(fptr
);
1990 const char *p
= READ_DATA_PENDING_PTR(fptr
);
1993 e
= memchr(p
, '\n', pending
);
1995 pending
= e
- p
+ 1;
1998 str
= rb_str_new(p
, pending
);
1999 fptr
->rbuf_off
+= pending
;
2000 fptr
->rbuf_len
-= pending
;
2003 rb_str_resize(str
, len
+ pending
);
2004 read_buffered_data(RSTRING_PTR(str
)+len
, pending
, fptr
);
2007 if (cr
!= ENC_CODERANGE_BROKEN
)
2008 pos
= rb_str_coderange_scan_restartable(RSTRING_PTR(str
) + pos
, RSTRING_PTR(str
) + len
, enc
, &cr
);
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
;
2019 str
= io_enc_str(str
, fptr
);
2020 if (!fptr
->enc2
) ENC_CODERANGE_SET(str
, cr
);
2022 ARGF
.lineno
= INT2FIX(fptr
->lineno
);
2027 prepare_getline_args(int argc
, VALUE
*argv
, VALUE
*rsp
, long *limit
, VALUE io
)
2029 VALUE rs
= rb_rs
, lim
= Qnil
;
2035 if (NIL_P(argv
[0]) || !NIL_P(tmp
= rb_check_string_type(argv
[0]))) {
2042 else if (2 <= argc
) {
2043 rb_scan_args(argc
, argv
, "2", &rs
, &lim
);
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");
2061 rb_raise(rb_eArgError
, "encoding mismatch: %s IO with %s RS",
2062 rb_enc_name(enc_io
),
2063 rb_enc_name(enc_rs
));
2068 *limit
= NIL_P(lim
) ? -1L : NUM2LONG(lim
);
2072 rb_io_getline_1(VALUE rs
, long limit
, VALUE io
)
2079 GetOpenFile(io
, fptr
);
2080 rb_io_check_readable(fptr
);
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
);
2097 int extra_limit
= 16;
2099 rslen
= RSTRING_LEN(rs
);
2104 swallow(fptr
, '\n');
2108 rsptr
= RSTRING_PTR(rs
);
2110 newline
= (unsigned char)rsptr
[rslen
- 1];
2115 enc
= io_input_encoding(fptr
);
2116 while ((c
= appendline(fptr
, newline
, &str
, &limit
)) != EOF
) {
2117 const char *s
, *p
, *pp
;
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;
2129 s
= RSTRING_PTR(str
);
2130 p
= s
+ RSTRING_LEN(str
);
2131 pp
= rb_enc_left_char_head(s
, p
-1, enc
);
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 */
2148 swallow(fptr
, '\n');
2152 str
= io_enc_str(str
, fptr
);
2158 ARGF
.lineno
= INT2FIX(fptr
->lineno
);
2166 rb_io_getline(int argc
, VALUE
*argv
, VALUE io
)
2171 prepare_getline_args(argc
, argv
, &rs
, &limit
, io
);
2172 return rb_io_getline_1(rs
, limit
, io
);
2176 rb_io_gets(VALUE io
)
2180 GetOpenFile(io
, fptr
);
2181 rb_io_check_readable(fptr
);
2182 return rb_io_getline_fast(fptr
, io_read_encoding(fptr
));
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
2202 * File.new("testfile").gets #=> "This is line one\n"
2203 * $_ #=> "This is line one\n"
2207 rb_io_gets_m(int argc
, VALUE
*argv
, VALUE io
)
2211 str
= rb_io_getline(argc
, argv
, io
);
2212 rb_lastline_set(str
);
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")
2230 * f.gets #=> "This is line one\n"
2232 * f.gets #=> "This is line two\n"
2237 rb_io_lineno(VALUE io
)
2241 GetOpenFile(io
, fptr
);
2242 rb_io_check_readable(fptr
);
2243 return INT2NUM(fptr
->lineno
);
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"
2258 * $. #=> 1 # lineno of last read
2259 * f.gets #=> "This is line two\n"
2260 * $. #=> 1001 # lineno of last read
2264 rb_io_set_lineno(VALUE io
, VALUE lineno
)
2268 GetOpenFile(io
, fptr
);
2269 rb_io_check_readable(fptr
);
2270 fptr
->lineno
= NUM2INT(lineno
);
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.
2285 rb_io_readline(int argc
, VALUE
*argv
, VALUE io
)
2287 VALUE line
= rb_io_gets_m(argc
, argv
, io
);
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"
2314 rb_io_readlines(int argc
, VALUE
*argv
, VALUE io
)
2316 VALUE line
, ary
, rs
;
2319 prepare_getline_args(argc
, argv
, &rs
, &limit
, io
);
2321 while (!NIL_P(line
= rb_io_getline_1(rs
, limit
, io
))) {
2322 rb_ary_push(ary
, line
);
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
2352 rb_io_each_line(int argc
, VALUE
*argv
, VALUE io
)
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
))) {
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")
2375 * f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
2380 rb_io_each_byte(VALUE io
)
2385 RETURN_ENUMERATOR(io
, 0, 0);
2386 GetOpenFile(io
, fptr
);
2389 p
= fptr
->rbuf
+fptr
->rbuf_off
;
2390 e
= p
+ fptr
->rbuf_len
;
2394 rb_yield(INT2FIX(*p
& 0xff));
2398 rb_io_check_readable(fptr
);
2400 if (io_fillbuf(fptr
) < 0) {
2408 io_getc(rb_io_t
*fptr
, rb_encoding
*enc
)
2416 if (!fptr
->readconv
) {
2417 make_readconv(fptr
);
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
,
2425 if (!MBCLEN_NEEDMORE_P(r
))
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)
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
,
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) {
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
;
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
);
2470 if (io_fillbuf(fptr
) != -1) {
2471 rb_str_cat(str
, fptr
->rbuf
+fptr
->rbuf_off
, 1);
2474 r
= rb_enc_precise_mbclen(RSTRING_PTR(str
), RSTRING_PTR(str
)+RSTRING_LEN(str
), enc
);
2475 if (MBCLEN_NEEDMORE_P(r
)) {
2478 else if (MBCLEN_CHARFOUND_P(r
)) {
2479 cr
= ENC_CODERANGE_VALID
;
2484 str
= rb_str_new(fptr
->rbuf
+fptr
->rbuf_off
, 1);
2489 if (!cr
) cr
= ENC_CODERANGE_BROKEN
;
2490 str
= io_enc_str(str
, fptr
);
2492 ENC_CODERANGE_SET(str
, cr
);
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>
2510 rb_io_each_char(VALUE io
)
2516 RETURN_ENUMERATOR(io
, 0, 0);
2517 GetOpenFile(io
, fptr
);
2518 rb_io_check_readable(fptr
);
2520 enc
= io_input_encoding(fptr
);
2522 while (!NIL_P(c
= io_getc(fptr
, enc
))) {
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>
2540 * f = File.new("testfile")
2541 * f.lines.to_a #=> ["foo\n", "bar\n"]
2543 * f.lines.sort #=> ["bar\n", "foo\n"]
2547 rb_io_lines(int argc
, VALUE
*argv
, VALUE io
)
2549 return rb_enumeratorize(io
, ID2SYM(rb_intern("each_line")), argc
, argv
);
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>
2560 * f = File.new("testfile")
2561 * f.bytes.to_a #=> [104, 101, 108, 108, 111]
2563 * f.bytes.sort #=> [101, 104, 108, 108, 111]
2567 rb_io_bytes(VALUE io
)
2569 return rb_enumeratorize(io
, ID2SYM(rb_intern("each_byte")), 0, 0);
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>
2580 * f = File.new("testfile")
2581 * f.chars.to_a #=> ["h", "e", "l", "l", "o"]
2583 * f.chars.sort #=> ["e", "h", "l", "l", "o"]
2587 rb_io_chars(VALUE io
)
2589 return rb_enumeratorize(io
, ID2SYM(rb_intern("each_char")), 0, 0);
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")
2605 rb_io_getc(VALUE io
)
2610 GetOpenFile(io
, fptr
);
2611 rb_io_check_readable(fptr
);
2613 enc
= io_input_encoding(fptr
);
2615 return io_getc(fptr
, enc
);
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"
2643 rb_io_readchar(VALUE io
)
2645 VALUE c
= rb_io_getc(io
);
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")
2666 rb_io_getbyte(VALUE io
)
2671 GetOpenFile(io
, fptr
);
2672 rb_io_check_readable(fptr
);
2674 if (fptr
->fd
== 0 && (fptr
->mode
& FMODE_TTY
) && TYPE(rb_stdout
) == T_FILE
) {
2676 GetOpenFile(rb_stdout
, ofp
);
2677 if (ofp
->mode
& FMODE_TTY
) {
2678 rb_io_flush(rb_stdout
);
2681 if (io_fillbuf(fptr
) < 0) {
2686 c
= (unsigned char)fptr
->rbuf
[fptr
->rbuf_off
-1];
2687 return INT2FIX(c
& 0xff);
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.
2699 rb_io_readbyte(VALUE io
)
2701 VALUE c
= rb_io_getbyte(io
);
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
2727 rb_io_ungetbyte(VALUE io
, VALUE b
)
2731 GetOpenFile(io
, fptr
);
2732 rb_io_check_readable(fptr
);
2733 if (NIL_P(b
)) return Qnil
;
2735 char cc
= FIX2INT(b
);
2736 b
= rb_str_new(&cc
, 1);
2741 io_ungetbyte(b
, fptr
);
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
2762 rb_io_ungetc(VALUE io
, VALUE c
)
2767 GetOpenFile(io
, fptr
);
2768 rb_io_check_readable(fptr
);
2769 if (NIL_P(c
)) return Qnil
;
2771 int cc
= FIX2INT(c
);
2772 rb_encoding
*enc
= io_read_encoding(fptr
);
2775 c
= rb_str_new(buf
, rb_enc_mbcput(cc
, buf
, enc
));
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
);
2796 io_ungetbyte(c
, fptr
);
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
2814 rb_io_isatty(VALUE io
)
2818 GetOpenFile(io
, fptr
);
2819 if (isatty(fptr
->fd
) == 0)
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
2839 rb_io_close_on_exec_p(VALUE io
)
2841 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
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
;
2863 return Qnil
; /* not reached */
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
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;
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
);
2917 #define FMODE_PREP (1<<16)
2918 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
2919 #define PREP_STDIO_NAME(f) ((f)->path)
2922 finish_writeconv(rb_io_t
*fptr
, int noraise
)
2924 unsigned char *ds
, *dp
, *de
;
2925 rb_econv_result_t res
;
2928 unsigned char buf
[1024];
2931 res
= econv_destination_buffer_full
;
2932 while (res
== econv_destination_buffer_full
) {
2934 de
= buf
+ sizeof(buf
);
2935 res
= rb_econv_convert(fptr
->writeconv
, NULL
, NULL
, &dp
, de
, 0);
2938 r
= rb_write_internal(fptr
->fd
, ds
, dp
-ds
);
2944 if (rb_io_wait_writable(fptr
->fd
)) {
2946 rb_io_check_closed(fptr
);
2947 else if (fptr
->fd
< 0)
2954 rb_econv_check_error(fptr
->writeconv
);
2956 if (res
== econv_invalid_byte_sequence
||
2957 res
== econv_undefined_conversion
) {
2965 res
= econv_destination_buffer_full
;
2966 while (res
== econv_destination_buffer_full
) {
2967 if (fptr
->wbuf_len
== fptr
->wbuf_capa
) {
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
;
2976 rb_econv_check_error(fptr
->writeconv
);
2978 if (res
== econv_invalid_byte_sequence
||
2979 res
== econv_undefined_conversion
) {
2987 fptr_finalize(rb_io_t
*fptr
, int noraise
)
2990 if (fptr
->writeconv
) {
2991 finish_writeconv(fptr
, noraise
);
2993 if (fptr
->wbuf_len
) {
2996 if (IS_PREP_STDIO(fptr
) ||
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;
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
);
3015 /* fptr->fd is already closed. */
3021 fptr
->stdio_file
= 0;
3022 fptr
->mode
&= ~(FMODE_READABLE
|FMODE_WRITABLE
);
3024 rb_sys_fail(fptr
->path
);
3029 rb_io_fptr_cleanup(rb_io_t
*fptr
, int noraise
)
3031 if (fptr
->finalize
) {
3032 (*fptr
->finalize
)(fptr
, noraise
);
3035 fptr_finalize(fptr
, noraise
);
3040 clear_readconv(rb_io_t
*fptr
)
3042 if (fptr
->readconv
) {
3043 rb_econv_close(fptr
->readconv
);
3044 fptr
->readconv
= NULL
;
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;
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;
3079 rb_io_fptr_cleanup(fptr
, Qtrue
);
3088 clear_codeconv(fptr
);
3094 rb_io_close(VALUE 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
;
3114 rb_io_fptr_cleanup(fptr
, Qfalse
);
3115 rb_thread_fd_close(fd
);
3118 rb_syswait(fptr
->pid
);
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>.
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
);
3151 io_call_close(VALUE io
)
3153 return rb_funcall(io
, rb_intern("close"), 0, 0);
3159 return rb_rescue(io_call_close
, io
, 0, 0);
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>
3170 * f = File.new("testfile")
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
3182 rb_io_closed(VALUE 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
) {
3196 fptr
= RFILE(io
)->fptr
;
3197 rb_io_check_initialized(fptr
);
3198 return 0 <= fptr
->fd
? Qfalse
: Qtrue
;
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+")
3213 * <em>produces:</em>
3215 * prog.rb:3:in `readlines': not opened for reading (IOError)
3220 rb_io_close_read(VALUE 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
)) {
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
);
3241 write_io
= GetWriteIO(io
);
3242 if (io
!= write_io
) {
3244 fptr_finalize(fptr
, Qfalse
);
3245 GetOpenFile(write_io
, wfptr
);
3246 if (fptr
->refcnt
< LONG_MAX
) {
3248 RFILE(io
)->fptr
= wfptr
;
3249 rb_io_fptr_finalize(fptr
);
3254 if (fptr
->mode
& FMODE_WRITABLE
) {
3255 rb_raise(rb_eIOError
, "closing non-duplex IO for reading");
3257 return rb_io_close(io
);
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+")
3272 * <em>produces:</em>
3274 * prog.rb:3:in `write': not opened for writing (IOError)
3275 * from prog.rb:3:in `print'
3280 rb_io_close_write(VALUE 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
)) {
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
);
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
;
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."
3329 rb_io_sysseek(int argc
, VALUE
*argv
, VALUE io
)
3331 VALUE offset
, ptrname
;
3332 int whence
= SEEK_SET
;
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
);
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
3367 rb_io_syswrite(VALUE io
, VALUE str
)
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
);
3387 n
= write(fptr
->fd
, RSTRING_PTR(str
), RSTRING_LEN(str
));
3390 if (n
== -1) rb_sys_fail(fptr
->path
);
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"
3412 rb_io_sysread(int argc
, VALUE
*argv
, VALUE io
)
3418 rb_scan_args(argc
, argv
, "11", &len
, &str
);
3419 ilen
= NUM2LONG(len
);
3422 str
= rb_str_new(0, ilen
);
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");
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
);
3448 rb_sys_fail(fptr
->path
);
3450 rb_str_set_len(str
, n
);
3451 if (n
== 0 && ilen
> 0) {
3454 rb_str_resize(str
, n
);
3461 rb_io_binmode(VALUE io
)
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
);
3473 fptr
->mode
|= FMODE_BINMODE
;
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.
3487 rb_io_binmode_m(VALUE io
)
3489 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3495 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3496 write_io
= GetWriteIO(io
);
3498 rb_io_binmode(write_io
);
3505 * ios.binmode? => true or false
3507 * Returns <code>true</code> if <em>ios</em> is binmode.
3510 rb_io_binmode_p(VALUE io
)
3513 GetOpenFile(io
, fptr
);
3514 return fptr
->mode
& FMODE_BINMODE
? Qtrue
: Qfalse
;
3518 rb_io_flags_mode(int flags
)
3521 # define MODE_BINMODE(a,b) ((flags & FMODE_BINMODE) ? (b) : (a))
3523 # define MODE_BINMODE(a,b) (a)
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
)
3550 const char *m
= mode
;
3554 flags
|= FMODE_READABLE
;
3557 flags
|= FMODE_WRITABLE
| FMODE_CREATE
;
3560 flags
|= FMODE_WRITABLE
| FMODE_APPEND
| FMODE_CREATE
;
3564 rb_raise(rb_eArgError
, "invalid access mode %s", mode
);
3570 flags
|= FMODE_BINMODE
;
3573 flags
|= FMODE_READWRITE
;
3586 rb_io_modenum_flags(int mode
)
3590 switch (mode
& (O_RDONLY
|O_WRONLY
|O_RDWR
)) {
3592 flags
= FMODE_READABLE
;
3595 flags
= FMODE_WRITABLE
;
3598 flags
= FMODE_READWRITE
;
3602 if (mode
& O_APPEND
) {
3603 flags
|= FMODE_APPEND
;
3605 if (mode
& O_CREAT
) {
3606 flags
|= FMODE_CREATE
;
3609 if (mode
& O_BINARY
) {
3610 flags
|= FMODE_BINMODE
;
3618 rb_io_mode_modenum(const char *mode
)
3621 const char *m
= mode
;
3628 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
3631 flags
|= O_WRONLY
| O_CREAT
| O_APPEND
;
3635 rb_raise(rb_eArgError
, "invalid access mode %s", mode
);
3646 flags
= (flags
& ~O_ACCMODE
) | O_RDWR
;
3658 #define MODENUM_MAX 4
3661 rb_io_modenum_mode(int flags
)
3664 # define MODE_BINARY(a,b) ((flags & O_BINARY) ? (b) : (a))
3666 # define MODE_BINARY(a,b) (a)
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
)) {
3676 return MODE_BINARY("r", "rb");
3678 return MODE_BINARY("w", "wb");
3680 return MODE_BINARY("r+", "rb+");
3682 rb_raise(rb_eArgError
, "invalid access modenum %o", flags
);
3683 return NULL
; /* not reached */
3687 mode_enc(rb_io_t
*fptr
, const char *estr
)
3689 const char *p0
, *p1
;
3693 /* parse estr as "enc" or "enc2:enc" */
3697 clear_codeconv(fptr
);
3699 p0
= strrchr(estr
, ':');
3702 idx
= rb_enc_find_index(p1
);
3704 fptr
->enc
= rb_enc_from_index(idx
);
3707 rb_warn("Unsupported encoding %s ignored", p1
);
3710 if (fptr
->enc
&& p0
) {
3712 if (n
> ENCODING_MAXNAMELEN
) {
3716 enc2name
= ALLOCA_N(char, n
+1);
3717 memcpy(enc2name
, estr
, n
);
3720 idx2
= rb_enc_find_index(enc2name
);
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",
3730 fptr
->enc2
= rb_enc_from_index(idx2
);
3736 rb_io_mode_enc(rb_io_t
*fptr
, const char *mode
)
3738 const char *p
= strchr(mode
, ':');
3740 mode_enc(fptr
, p
+1);
3744 struct sysopen_struct
{
3751 sysopen_func(void *ptr
)
3753 struct sysopen_struct
*data
= ptr
;
3754 return (VALUE
)open(data
->fname
, data
->flag
, data
->mode
);
3758 rb_sysopen_internal(char *fname
, int flags
, unsigned int mode
)
3760 struct sysopen_struct data
;
3764 return (int)rb_thread_blocking_region(sysopen_func
, &data
, RB_UBF_DFL
, 0);
3768 rb_sysopen(char *fname
, int flags
, unsigned int mode
)
3772 fd
= rb_sysopen_internal(fname
, flags
, mode
);
3774 if (errno
== EMFILE
|| errno
== ENFILE
) {
3776 fd
= rb_sysopen_internal(fname
, flags
, mode
);
3787 rb_fopen(const char *fname
, const char *mode
)
3791 file
= fopen(fname
, mode
);
3793 if (errno
== EMFILE
|| errno
== ENFILE
) {
3795 file
= fopen(fname
, mode
);
3802 if (setvbuf(file
, NULL
, _IOFBF
, 0) != 0)
3803 rb_warn("setvbuf() can't be honoured for %s", fname
);
3806 setmode(fileno(file
), O_TEXT
);
3812 rb_fdopen(int fd
, const char *mode
)
3819 file
= fdopen(fd
, mode
);
3825 errno
== EMFILE
|| errno
== ENFILE
) {
3830 file
= fdopen(fd
, mode
);
3834 if (errno
== 0) errno
= EINVAL
;
3836 if (errno
== 0) errno
= EMFILE
;
3842 /* xxx: should be _IONBF? A buffer in FILE may have trouble. */
3844 if (setvbuf(file
, NULL
, _IOFBF
, 0) != 0)
3845 rb_warn("setvbuf() can't be honoured (fd=%d)", fd
);
3851 io_check_tty(rb_io_t
*fptr
)
3853 if (isatty(fptr
->fd
))
3854 fptr
->mode
|= FMODE_TTY
|FMODE_DUPLEX
;
3858 rb_file_open_internal(VALUE io
, const char *fname
, const char *mode
)
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);
3873 rb_file_open(const char *fname
, const char *mode
)
3875 return rb_file_open_internal(io_alloc(rb_cFile
), fname
, mode
);
3879 rb_file_sysopen_internal(VALUE io
, const char *fname
, int flags
, int mode
)
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
);
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
{
3902 struct pipe_list
*next
;
3906 pipe_add_fptr(rb_io_t
*fptr
)
3908 struct pipe_list
*list
;
3910 list
= ALLOC(struct pipe_list
);
3912 list
->next
= pipe_list
;
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
;
3928 while (list
->next
) {
3929 if (list
->next
->fptr
== fptr
) {
3931 list
->next
= list
->next
->next
;
3942 struct pipe_list
*list
= pipe_list
;
3943 struct pipe_list
*tmp
;
3947 rb_io_fptr_finalize(list
->fptr
);
3953 pipe_finalize(rb_io_t
*fptr
, int noraise
)
3955 #if !defined(HAVE_FORK) && !defined(_WIN32)
3957 if (fptr
->stdio_file
) {
3958 status
= pclose(fptr
->stdio_file
);
3961 fptr
->stdio_file
= 0;
3965 rb_last_status_set(status
, fptr
->pid
);
3967 fptr_finalize(fptr
, noraise
);
3969 pipe_del_fptr(fptr
);
3974 rb_io_synchronized(rb_io_t
*fptr
)
3976 rb_io_check_initialized(fptr
);
3977 fptr
->mode
|= FMODE_SYNC
;
3981 rb_io_unbuffered(rb_io_t
*fptr
)
3983 rb_io_synchronized(fptr
);
3992 if (errno
== EMFILE
|| errno
== ENFILE
) {
3998 UPDATE_MAXFD(pipes
[0]);
3999 UPDATE_MAXFD(pipes
[1]);
4006 struct rb_exec_arg
*execp
;
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]);
4022 if (p
->pair
[1] != 1) {
4023 dup2(p
->pair
[1], 1);
4027 else if (p
->modef
& FMODE_READABLE
) {
4029 if (p
->pair
[1] != 1) {
4030 dup2(p
->pair
[1], 1);
4036 if (p
->pair
[0] != 0) {
4037 dup2(p
->pair
[0], 0);
4044 rb_close_before_exec(int lowfd
, int maxhint
, VALUE noclose_fds
)
4047 int max
= max_file_descriptor
;
4050 for (fd
= lowfd
; fd
<= max
; fd
++) {
4051 if (!NIL_P(noclose_fds
) &&
4052 RTEST(rb_hash_lookup(noclose_fds
, INT2FIX(fd
))))
4055 ret
= fcntl(fd
, F_GETFD
);
4056 if (ret
!= -1 && !(ret
& FD_CLOEXEC
)) {
4057 fcntl(fd
, F_SETFD
, ret
|FD_CLOEXEC
);
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
);
4076 pipe_open(struct rb_exec_arg
*eargp
, VALUE prog
, const char *mode
)
4078 int modef
= rb_io_mode_flags(mode
);
4082 rb_io_t
*write_fptr
;
4084 #if defined(HAVE_FORK)
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
;
4096 const char *cmd
= 0;
4101 cmd
= StringValueCStr(prog
);
4104 /* fork : IO.popen("-") */
4108 else if (eargp
->argc
) {
4109 /* no shell : IO.popen([prog, arg0], arg1, ...) */
4114 /* with shell : IO.popen(prog) */
4119 #if defined(HAVE_FORK)
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)
4128 if (rb_pipe(arg
.pair
) < 0) {
4130 close(arg
.write_pair
[0]);
4131 close(arg
.write_pair
[1]);
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]));
4140 case FMODE_READABLE
:
4141 if (rb_pipe(arg
.pair
) < 0)
4144 rb_exec_arg_addopt(eargp
, INT2FIX(1), INT2FIX(arg
.pair
[1]));
4146 case FMODE_WRITABLE
:
4147 if (rb_pipe(arg
.pair
) < 0)
4150 rb_exec_arg_addopt(eargp
, INT2FIX(0), INT2FIX(arg
.pair
[0]));
4156 rb_exec_arg_fixup(arg
.execp
);
4157 pid
= rb_fork(&status
, popen_exec
, &arg
, arg
.execp
->redirect_fds
);
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
);
4177 if ((modef
& (FMODE_READABLE
|FMODE_WRITABLE
)) == (FMODE_READABLE
|FMODE_WRITABLE
)) {
4178 close(arg
.write_pair
[0]);
4179 close(arg
.write_pair
[1]);
4184 if ((modef
& FMODE_READABLE
) && (modef
& FMODE_WRITABLE
)) {
4187 close(arg
.write_pair
[0]);
4188 write_fd
= arg
.write_pair
[1];
4190 else if (modef
& FMODE_READABLE
) {
4198 #elif defined(_WIN32)
4200 volatile VALUE argbuf
;
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
]);
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);
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) {
4226 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4233 rb_run_exec_options(&sarg
, NULL
);
4239 rb_run_exec_options(&sarg
, NULL
);
4242 prog
= rb_ary_join(rb_ary_new4(argc
, argv
), rb_str_new2(" "));
4243 cmd
= StringValueCStr(prog
);
4246 rb_exec_arg_fixup(eargp
);
4247 rb_run_exec_options(eargp
, &sarg
);
4249 fp
= popen(cmd
, mode
);
4251 rb_run_exec_options(&sarg
, NULL
);
4252 if (!fp
) rb_sys_fail(RSTRING_PTR(prog
));
4256 port
= io_alloc(rb_cIO
);
4257 MakeOpenFile(port
, fptr
);
4259 fptr
->stdio_file
= fp
;
4260 fptr
->mode
= modef
| FMODE_SYNC
|FMODE_DUPLEX
;
4261 rb_io_mode_enc(fptr
, mode
);
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
);
4282 pipe_open_v(int argc
, VALUE
*argv
, const char *mode
)
4285 struct rb_exec_arg earg
;
4286 prog
= rb_exec_arg_init(argc
, argv
, Qfalse
, &earg
);
4287 return pipe_open(&earg
, prog
, mode
);
4291 pipe_open_s(VALUE prog
, const char *mode
)
4293 const char *cmd
= RSTRING_PTR(prog
);
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");
4303 return pipe_open(0, 0, mode
);
4306 rb_exec_arg_init(argc
, argv
, Qtrue
, &earg
);
4307 return pipe_open(&earg
, prog
, mode
);
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")
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}"}
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>
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)>
4366 rb_io_s_popen(int argc
, VALUE
*argv
, VALUE klass
)
4369 VALUE pname
, pmode
, port
, tmp
;
4371 if (rb_scan_args(argc
, argv
, "11", &pname
, &pmode
) == 1) {
4374 else if (FIXNUM_P(pmode
)) {
4375 mode
= rb_io_modenum_mode(FIX2INT(pmode
));
4378 mode
= StringValueCStr(pmode
);
4380 tmp
= rb_check_array_type(pname
);
4382 tmp
= rb_ary_dup(tmp
);
4383 RBASIC(tmp
)->klass
= 0;
4384 port
= pipe_open_v(RARRAY_LEN(tmp
), RARRAY_PTR(tmp
), mode
);
4388 SafeStringValue(pname
);
4389 port
= pipe_open_s(pname
, mode
);
4393 if (rb_block_given_p()) {
4395 rb_io_flush(rb_stdout
);
4396 rb_io_flush(rb_stderr
);
4401 RBASIC(port
)->klass
= klass
;
4402 if (rb_block_given_p()) {
4403 return rb_ensure(rb_yield
, port
, io_close
, port
);
4409 io_set_encoding(VALUE io
, VALUE opt
)
4412 VALUE encoding
=Qnil
, extenc
=Qnil
, intenc
=Qnil
;
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
);
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
)));
4440 fptr
->enc2
= intencoding
;
4443 fptr
->enc
= extencoding
;
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
));
4457 rb_open_file(int argc
, VALUE
*argv
, VALUE io
)
4459 VALUE opt
=Qnil
, fname
, vmode
, perm
;
4465 opt
= rb_check_convert_type(argv
[argc
-1], T_HASH
, "Hash", "to_hash");
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
;
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
);
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
4488 && fs_encoding
!= fname_encoding
) {
4489 static VALUE fs_enc
;
4491 fs_enc
= rb_enc_from_encoding(fs_encoding
);
4492 fname
= rb_str_transcode(fname
, fs_enc
);
4496 FilePathValue(fname
);
4498 if (FIXNUM_P(vmode
) || !NIL_P(perm
)) {
4499 if (FIXNUM_P(vmode
)) {
4500 flags
= FIX2INT(vmode
);
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
)) {
4512 GetOpenFile(io
, fptr
);
4513 rb_io_mode_enc(fptr
, StringValueCStr(vmode
));
4517 mode
= NIL_P(vmode
) ? "r" : StringValueCStr(vmode
);
4518 rb_file_open_internal(io
, RSTRING_PTR(fname
), mode
);
4521 io_set_encoding(io
, opt
);
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.
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
);
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
4562 rb_io_s_sysopen(int argc
, VALUE
*argv
)
4564 VALUE fname
, vmode
, perm
;
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
);
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
);
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>'').
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:
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
4647 * open("testfile") do |f|
4651 * <em>produces:</em>
4655 * Open a subprocess and read its output:
4657 * cmd = open("|date")
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+")
4672 * puts "Got: #{f.gets}"
4675 * <em>produces:</em>
4679 * Open a subprocess using a block to receive the I/O object:
4685 * puts "Got: #{f.gets}"
4689 * <em>produces:</em>
4695 rb_f_open(int argc
, VALUE
*argv
)
4698 int redirect
= Qfalse
;
4701 CONST_ID(to_open
, "to_open");
4702 if (rb_respond_to(argv
[0], to_open
)) {
4706 VALUE tmp
= argv
[0];
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
);
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
);
4729 return rb_io_s_open(argc
, argv
, rb_cFile
);
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
);
4740 return rb_file_open(fname
, mode
);
4745 rb_io_open_with_args(int argc
, VALUE
*argv
)
4750 if (rb_scan_args(argc
, argv
, "11", &pname
, &pmode
) == 1) {
4753 else if (FIXNUM_P(pmode
)) {
4754 mode
= rb_io_modenum_mode(FIX2INT(pmode
));
4757 mode
= StringValueCStr(pmode
);
4759 return rb_io_open(StringValueCStr(pname
), mode
);
4763 io_reopen(VALUE io
, VALUE nfile
)
4765 rb_io_t
*fptr
, *orig
;
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
) {
4794 if (fptr
->mode
& FMODE_WRITABLE
) {
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
;
4810 if (IS_PREP_STDIO(fptr
)) {
4811 /* need to keep stdio objects */
4812 if (dup2(fd2
, fd
) < 0)
4813 rb_sys_fail(orig
->path
);
4816 if (fptr
->stdio_file
)
4817 fclose(fptr
->stdio_file
);
4820 fptr
->stdio_file
= 0;
4822 if (dup2(fd2
, fd
) < 0)
4823 rb_sys_fail(orig
->path
);
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
) {
4841 RBASIC(io
)->klass
= rb_obj_class(nfile
);
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"
4862 rb_io_reopen(int argc
, VALUE
*argv
, VALUE file
)
4869 if (rb_scan_args(argc
, argv
, "11", &fname
, &nmode
) == 1) {
4870 VALUE tmp
= rb_io_check_io(fname
);
4872 return io_reopen(file
, tmp
);
4876 FilePathValue(fname
);
4877 rb_io_taint_check(file
);
4878 fptr
= RFILE(file
)->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
));
4895 rb_io_mode_enc(fptr
, StringValueCStr(nmode
));
4903 fptr
->path
= strdup(StringValueCStr(fname
));
4904 mode
= rb_io_flags_mode(fptr
->mode
);
4906 fptr
->fd
= rb_sysopen(fptr
->path
, rb_io_mode_modenum(mode
), 0666);
4907 fptr
->stdio_file
= 0;
4911 if (fptr
->mode
& FMODE_WRITABLE
) {
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
);
4922 if (setvbuf(fptr
->stdio_file
, NULL
, _IOFBF
, 0) != 0)
4923 rb_warn("setvbuf() can't be honoured for %s", fptr
->path
);
4927 if (close(fptr
->fd
) < 0)
4928 rb_sys_fail(fptr
->path
);
4930 fptr
->fd
= rb_sysopen(fptr
->path
, rb_io_mode_modenum(mode
), 0666);
4938 rb_io_init_copy(VALUE dest
, VALUE io
)
4940 rb_io_t
*fptr
, *orig
;
4944 io
= rb_io_get_io(io
);
4945 if (dest
== io
) return dest
;
4946 GetOpenFile(io
, orig
);
4947 MakeOpenFile(dest
, fptr
);
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
);
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
);
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>
4985 rb_io_printf(int argc
, VALUE
*argv
, VALUE out
)
4987 rb_io_write(out
, rb_f_sprintf(argc
, argv
));
4993 * printf(io, string [, obj ... ] ) => nil
4994 * printf(string [, obj ... ] ) => nil
4997 * io.write(sprintf(string, obj, ...)
4999 * $stdout.write(sprintf(string, obj, ...)
5003 rb_f_printf(int argc
, VALUE
*argv
)
5007 if (argc
== 0) return Qnil
;
5008 if (TYPE(argv
[0]) == T_STRING
) {
5016 rb_io_write(out
, rb_f_sprintf(argc
, argv
));
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.
5042 rb_io_print(int argc
, VALUE
*argv
, VALUE out
)
5047 /* if no argument given, print `$_' */
5050 line
= rb_lastline_get();
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
);
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"
5081 * print "cat", [1,2,3], 99
5083 * <em>produces:</em>
5090 rb_f_print(int argc
, VALUE
*argv
)
5092 rb_io_print(argc
, argv
, rb_stdout
);
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>.
5107 * <em>produces:</em>
5113 rb_io_putc(VALUE io
, VALUE ch
)
5115 char c
= NUM2CHR(ch
);
5117 rb_io_write(io
, rb_str_new(&c
, 1));
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
);
5140 io_puts_ary(VALUE ary
, VALUE out
, int recur
)
5146 tmp
= rb_str_new2("[...]");
5147 rb_io_puts(1, &tmp
, out
);
5150 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
5151 tmp
= RARRAY_PTR(ary
)[i
];
5152 rb_io_puts(1, &tmp
, out
);
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>
5178 rb_io_puts(int argc
, VALUE
*argv
, VALUE out
)
5183 /* if no argument given, print newline. */
5185 rb_io_write(out
, rb_default_rs
);
5188 for (i
=0; i
<argc
; i
++) {
5189 line
= rb_check_array_type(argv
[i
]);
5191 rb_exec_recursive(io_puts_ary
, line
, out
);
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
);
5207 * puts(obj, ...) => nil
5211 * $stdout.puts(obj, ...)
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
);
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
);
5234 * p(obj1, obj2, ...) => [obj, ...]
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']
5245 * <em>produces:</em>
5247 * #<S name="dave", state="TX">
5251 rb_f_p(int argc
, VALUE
*argv
, VALUE self
)
5256 for (i
=0; i
<argc
; i
++) {
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
);
5273 * obj.display(port=$>) => nil
5275 * Prints <i>obj</i> on the given port (default <code>$></code>).
5278 * def display(port=$>)
5286 * [ 4, 5, 6 ].display
5289 * <em>produces:</em>
5295 rb_obj_display(int argc
, VALUE
*argv
, VALUE self
)
5303 rb_scan_args(argc
, argv
, "01", &out
);
5305 rb_io_write(out
, self
);
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
);
5317 rb_io_write(rb_stderr
, rb_str_new(mesg
, len
));
5322 rb_write_error(const char *mesg
)
5324 rb_write_error2(mesg
, strlen(mesg
));
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
));
5338 stdout_setter(VALUE val
, ID id
, VALUE
*variable
)
5340 must_respond_to(id_write
, val
, id
);
5345 prep_io(int fd
, int mode
, VALUE klass
, const char *path
)
5348 VALUE io
= io_alloc(klass
);
5350 MakeOpenFile(io
, fp
);
5355 setmode(fd
, O_BINARY
);
5360 if (path
) fp
->path
= strdup(path
);
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
);
5375 prep_stdio(FILE *f
, int mode
, VALUE klass
, const char *path
)
5378 VALUE io
= prep_io(fileno(f
), mode
|FMODE_PREP
, klass
, path
);
5380 GetOpenFile(io
, fptr
);
5381 fptr
->stdio_file
= f
;
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
;
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"
5410 * <em>produces:</em>
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
;
5424 rb_scan_args(argc
, argv
, "11", &fnum
, &mode
);
5426 if (FIXNUM_P(mode
)) {
5427 flags
= FIX2LONG(mode
);
5430 SafeStringValue(mode
);
5431 flags
= rb_io_mode_modenum(StringValueCStr(mode
));
5434 orig
= rb_io_check_io(fnum
);
5439 #if defined(HAVE_FCNTL) && defined(F_GETFL)
5440 flags
= fcntl(fd
, F_GETFL
);
5441 if (flags
== -1) rb_sys_fail(0);
5444 MakeOpenFile(io
, fp
);
5446 fp
->mode
= rb_io_modenum_flags(flags
);
5449 else if (RFILE(io
)->fptr
) {
5450 rb_raise(rb_eRuntimeError
, "reinitializing IO");
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
));
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
);
5465 rb_raise(rb_eArgError
, "incompatible mode \"%s\"", RSTRING_PTR(mode
));
5470 RFILE(io
)->fptr
= ofp
;
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)
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");
5509 return rb_io_initialize(argc
, argv
, io
);
5512 rb_open_file(argc
, argv
, io
);
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"
5529 * <em>produces:</em>
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",
5544 return rb_class_new_instance(argc
, argv
, klass
);
5550 * IO.for_fd(fd, mode) => io
5552 * Synonym for <code>IO::new</code>.
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
);
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
);
5575 argf_free(void *ptr
)
5577 struct argf
*p
= ptr
;
5582 argf_init(struct argf
*p
, VALUE v
)
5585 p
->current_file
= Qnil
;
5591 argf_alloc(VALUE klass
)
5594 VALUE argf
= Data_Make_Struct(klass
, struct argf
, argf_mark
, argf_free
, p
);
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
5614 argf_initialize(VALUE argf
, VALUE argv
)
5616 memset(&ARGF
, 0, sizeof(ARGF
));
5617 argf_init(&ARGF
, argv
);
5623 argf_initialize_copy(VALUE argf
, VALUE orig
)
5625 ARGF
= argf_of(orig
);
5626 rb_argv
= rb_obj_dup(rb_argv
);
5628 const char *inplace
= ARGF
.inplace
;
5630 ARGF
.inplace
= ruby_strdup(inplace
);
5636 argf_set_lineno(VALUE argf
, VALUE val
)
5638 gets_lineno
= NUM2INT(val
);
5639 lineno
= INT2FIX(gets_lineno
);
5644 argf_lineno(VALUE argf
)
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);\
5662 #define NEXT_ARGF_FORWARD(argc, argv) do {\
5663 if (!next_argv()) return Qnil;\
5664 ARGF_FORWARD(argc, argv);\
5668 argf_close(VALUE file
)
5670 rb_funcall3(file
, rb_intern("close"), 0, 0);
5674 argf_next_argv(VALUE argf
)
5678 int stdout_binmode
= 0;
5680 if (TYPE(rb_stdout
) == T_FILE
) {
5681 GetOpenFile(rb_stdout
, fptr
);
5682 if (fptr
->mode
& FMODE_BINMODE
)
5687 if (!NIL_P(rb_argv
) && RARRAY_LEN(rb_argv
) > 0) {
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");
5711 int fr
= rb_sysopen(fn
, O_RDONLY
, 0);
5713 if (ruby_inplace_mode
) {
5715 #ifndef NO_SAFE_RENAME
5721 if (TYPE(rb_stdout
) == T_FILE
&& rb_stdout
!= orig_stdout
) {
5722 rb_io_close(rb_stdout
);
5725 if (*ruby_inplace_mode
) {
5726 str
= rb_str_new2(fn
);
5727 #ifdef NO_LONG_FNAME
5728 ruby_add_suffix(str
, ruby_inplace_mode
);
5730 rb_str_cat2(str
, ruby_inplace_mode
);
5732 #ifdef NO_SAFE_RENAME
5734 (void)unlink(RSTRING_PTR(str
));
5735 (void)rename(fn
, RSTRING_PTR(str
));
5736 fr
= rb_sysopen(RSTRING_PTR(str
), O_RDONLY
, 0);
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
));
5747 #ifdef NO_SAFE_RENAME
5748 rb_fatal("Can't do inplace edit without backup");
5750 if (unlink(fn
) < 0) {
5751 rb_warn("Can't remove %s: %s, skipping file",
5752 fn
, strerror(errno
));
5758 fw
= rb_sysopen(fn
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
5759 #ifndef NO_SAFE_RENAME
5762 fchmod(fw
, st
.st_mode
);
5764 chmod(fn
, st
.st_mode
);
5766 if (st
.st_uid
!=st2
.st_uid
|| st
.st_gid
!=st2
.st_gid
) {
5767 fchown(fw
, st
.st_uid
, st
.st_gid
);
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
);
5779 GetOpenFile(current_file
, fptr
);
5780 fptr
->enc
= argf_enc
;
5781 fptr
->enc2
= argf_enc2
;
5782 clear_codeconv(fptr
);
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
;
5802 argf_getline(int argc
, VALUE
*argv
, VALUE argf
)
5807 if (!next_argv()) return Qnil
;
5808 if (ARGF_GENERIC_INPUT_P()) {
5809 line
= rb_funcall3(current_file
, rb_intern("gets"), argc
, argv
);
5812 if (argc
== 0 && rb_rs
== rb_default_rs
) {
5813 line
= rb_io_gets(current_file
);
5816 line
= rb_io_getline(argc
, argv
, current_file
);
5818 if (NIL_P(line
) && next_p
!= -1) {
5819 argf_close(current_file
);
5826 lineno
= INT2FIX(gets_lineno
);
5832 argf_lineno_getter(ID id
, VALUE
*var
)
5839 argf_lineno_setter(VALUE val
, ID id
, VALUE
*var
)
5842 int n
= NUM2INT(val
);
5844 lineno
= INT2FIX(n
);
5847 static VALUE
argf_gets(int, VALUE
*, VALUE
);
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
5868 * ARGV << "testfile"
5871 * <em>produces:</em>
5875 * This is line three
5878 * The style of programming using <code>$_</code> as an implicit
5879 * parameter is gradually losing favor in the Ruby community.
5883 rb_f_gets(int argc
, VALUE
*argv
, VALUE recv
)
5886 return argf_gets(argc
, argv
, argf
);
5888 return rb_funcall2(argf
, rb_intern("gets"), argc
, argv
);
5892 argf_gets(int argc
, VALUE
*argv
, VALUE argf
)
5896 line
= argf_getline(argc
, argv
, argf
);
5897 rb_lastline_set(line
);
5906 if (rb_rs
!= rb_default_rs
) {
5907 return rb_f_gets(0, 0, argf
);
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
);
5918 rb_lastline_set(line
);
5921 lineno
= INT2FIX(gets_lineno
);
5927 static VALUE
argf_readline(int, VALUE
*, VALUE
);
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.
5940 rb_f_readline(int argc
, VALUE
*argv
, VALUE recv
)
5943 return argf_readline(argc
, argv
, argf
);
5945 return rb_funcall2(argf
, rb_intern("readline"), argc
, argv
);
5949 argf_readline(int argc
, VALUE
*argv
, VALUE argf
)
5953 if (!next_argv()) rb_eof_error();
5954 ARGF_FORWARD(argc
, argv
);
5955 line
= argf_gets(argc
, argv
, argf
);
5963 static VALUE
argf_readlines(int, VALUE
*, VALUE
);
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.
5976 rb_f_readlines(int argc
, VALUE
*argv
, VALUE recv
)
5979 return argf_readlines(argc
, argv
, argf
);
5981 return rb_funcall2(argf
, rb_intern("readlines"), argc
, argv
);
5985 argf_readlines(int argc
, VALUE
*argv
, VALUE argf
)
5990 while (!NIL_P(line
= argf_getline(argc
, argv
, argf
))) {
5991 rb_ary_push(ary
, line
);
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
6012 rb_f_backquote(VALUE obj
, VALUE str
)
6014 volatile VALUE port
;
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
);
6029 #ifdef HAVE_SYS_SELECT_H
6030 #include <sys/select.h>
6034 select_internal(VALUE read
, VALUE write
, VALUE except
, struct timeval
*tp
, rb_fdset_t
*fds
)
6037 fd_set
*rp
, *wp
, *ep
;
6041 int interrupt_flag
= 0;
6043 struct timeval timerec
;
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 */
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;
6060 rp
= rb_fd_ptr(&fds
[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]);
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]);
6100 n
= rb_thread_select(max
, rp
, wp
, ep
, tp
);
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) {
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
);
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
);
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
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
);
6177 select_end(VALUE arg
)
6179 struct select_args
*p
= (struct select_args
*)arg
;
6182 for (i
= 0; i
< sizeof(p
->fdsets
) / sizeof(p
->fdsets
[0]); ++i
)
6183 rb_fd_term(&p
->fdsets
[i
]);
6190 * IO.select(read_array
6193 * [, timeout]]] ) => array or nil
6195 * See <code>Kernel#select</code>.
6199 rb_f_select(int argc
, VALUE
*argv
, VALUE obj
)
6202 struct select_args args
;
6203 struct timeval timerec
;
6206 rb_scan_args(argc
, argv
, "13", &args
.read
, &args
.write
, &args
.except
, &timeout
);
6207 if (NIL_P(timeout
)) {
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
);
6221 return select_internal(args
.read
, args
.write
, args
.except
,
6222 args
.timeout
, args
.fdsets
);
6227 #if !defined(MSDOS) && !defined(__human68k__)
6229 io_cntl(int fd
, int cmd
, long narg
, int io_p
)
6235 # if defined(__CYGWIN__)
6236 retval
= io_p
?ioctl(fd
, cmd
, (void*)narg
):fcntl(fd
, cmd
, narg
);
6238 retval
= io_p
?ioctl(fd
, cmd
, narg
):fcntl(fd
, cmd
, narg
);
6246 retval
= ioctl(fd
, cmd
, narg
);
6254 rb_io_ctl(VALUE io
, VALUE req
, VALUE arg
, int io_p
)
6256 #if !defined(MSDOS) && !defined(__human68k__)
6257 int cmd
= NUM2ULONG(req
);
6265 if (NIL_P(arg
) || arg
== Qfalse
) {
6268 else if (FIXNUM_P(arg
)) {
6269 narg
= FIX2LONG(arg
);
6271 else if (arg
== Qtrue
) {
6275 VALUE tmp
= rb_check_string_type(arg
);
6278 narg
= NUM2LONG(arg
);
6284 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
6288 len
= IOCPARM_LEN(cmd
); /* on BSDish systems we're safe */
6290 len
= 256; /* otherwise guess at what's safe */
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
;
6317 fptr
->mode
&= ~(FMODE_WSPLIT_INITIALIZED
|FMODE_WSPLIT
);
6321 return INT2NUM(retval
);
6324 return Qnil
; /* not reached */
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
6342 rb_io_ioctl(int argc
, VALUE
*argv
, VALUE io
)
6346 rb_scan_args(argc
, argv
, "11", &req
, &arg
);
6347 return rb_io_ctl(io
, req
, arg
, 1);
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.
6364 rb_io_fcntl(int argc
, VALUE
*argv
, VALUE io
)
6369 rb_scan_args(argc
, argv
, "11", &req
, &arg
);
6370 return rb_io_ctl(io
, req
, arg
, 0);
6373 return Qnil
; /* not reached */
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>
6397 rb_f_syscall(int argc
, VALUE
*argv
)
6399 #if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
6401 unsigned long arg
[14]; /* yes, we really need that many ! */
6403 unsigned long arg
[8];
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?
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
++;
6421 VALUE v
= rb_check_string_type(*argv
);
6426 arg
[i
] = (unsigned long)StringValueCStr(v
);
6429 arg
[i
] = (unsigned long)NUM2LONG(*argv
);
6437 retval
= syscall(arg
[0]);
6440 retval
= syscall(arg
[0],arg
[1]);
6443 retval
= syscall(arg
[0],arg
[1],arg
[2]);
6446 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3]);
6449 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4]);
6452 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4],arg
[5]);
6455 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4],arg
[5],arg
[6]);
6458 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4],arg
[5],arg
[6],
6463 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4],arg
[5],arg
[6],
6467 retval
= syscall(arg
[0],arg
[1],arg
[2],arg
[3],arg
[4],arg
[5],arg
[6],
6468 arg
[7], arg
[8], arg
[9]);
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]);
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]);
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]);
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]);
6486 #endif /* atarist */
6489 if (retval
< 0) rb_sys_fail(0);
6490 return INT2NUM(retval
);
6493 return Qnil
; /* not reached */
6498 io_new_instance(VALUE args
)
6500 return rb_class_new_instance(2, (VALUE
*)args
+1, *(VALUE
*)args
);
6504 io_encoding_set(rb_io_t
*fptr
, int argc
, VALUE v1
, VALUE v2
)
6506 if (NIL_P(v2
)) argc
= 1;
6508 fptr
->enc2
= rb_to_encoding(v1
);
6509 fptr
->enc
= rb_to_encoding(v2
);
6510 clear_codeconv(fptr
);
6512 else if (argc
== 1) {
6516 clear_codeconv(fptr
);
6519 VALUE tmp
= rb_check_string_type(v1
);
6521 mode_enc(fptr
, StringValueCStr(tmp
));
6524 fptr
->enc
= rb_to_encoding(v1
);
6526 clear_codeconv(fptr
);
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>.
6565 * puts "Parent got: <#{rd.read}>"
6570 * puts "Sending message to parent"
6575 * <em>produces:</em>
6577 * Sending message to parent
6578 * Parent got: <Hi Dad>
6582 rb_io_s_pipe(int argc
, VALUE
*argv
, VALUE klass
)
6586 return Qnil
; /* not reached */
6588 int pipes
[2], state
;
6589 VALUE r
, w
, args
[3], v1
, v2
;
6592 rb_scan_args(argc
, argv
, "02", &v1
, &v2
);
6593 if (rb_pipe(pipes
) == -1)
6597 args
[1] = INT2NUM(pipes
[0]);
6598 args
[2] = INT2FIX(O_RDONLY
);
6599 r
= rb_protect(io_new_instance
, (VALUE
)args
, &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
);
6612 if (!NIL_P(r
)) rb_io_close(r
);
6615 rb_io_synchronized(RFILE(w
)->fptr
);
6617 return rb_assoc_new(r
, w
);
6621 struct foreach_arg
{
6628 open_key_args(int argc
, VALUE
*argv
, struct foreach_arg
*arg
)
6632 FilePathValue(argv
[0]);
6634 arg
->argc
= argc
> 1 ? 1 : 0;
6635 arg
->argv
= argv
+ 1;
6638 arg
->io
= rb_io_open(RSTRING_PTR(argv
[0]), "r");
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;
6646 v
= rb_hash_aref(opt
, sym_open_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
));
6659 v
= rb_hash_aref(opt
, sym_mode
);
6661 arg
->io
= rb_io_open(RSTRING_PTR(argv
[0]), StringValueCStr(v
));
6664 arg
->io
= rb_io_open(RSTRING_PTR(argv
[0]), "r");
6667 io_set_encoding(arg
->io
, opt
);
6671 io_s_foreach(struct foreach_arg
*arg
)
6675 while (!NIL_P(str
= rb_io_gets_m(arg
->argc
, arg
->argv
, arg
->io
))) {
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
6699 * If the last argument is a hash, it's the keyword argument to open.
6700 * See <code>IO.read</code> for detail.
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
);
6717 io_s_readlines(struct foreach_arg
*arg
)
6719 return rb_io_readlines(arg
->argc
, arg
->argv
, arg
->io
);
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
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.
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
);
6752 io_s_read(struct foreach_arg
*arg
)
6754 return io_read(arg
->argc
, arg
->argv
, arg
->io
);
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
6770 * encoding: string or encoding
6772 * specifies encoding of the read string. encoding will be ignored
6773 * if length is specified.
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 "
6790 rb_io_s_read(int argc
, VALUE
*argv
, VALUE io
)
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
{
6809 off_t copy_length
; /* (off_t)-1 if not specified */
6810 off_t src_offset
; /* (off_t)-1 if not specified */
6825 copy_stream_wait_read(struct copy_stream_struct
*stp
)
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
);
6832 stp
->syserr
= "select";
6833 stp
->error_no
= errno
;
6840 copy_stream_wait_write(struct copy_stream_struct
*stp
)
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
);
6847 stp
->syserr
= "select";
6848 stp
->error_no
= errno
;
6854 #ifdef HAVE_SENDFILE
6857 #define USE_SENDFILE
6859 #ifdef HAVE_SYS_SENDFILE_H
6860 #include <sys/sendfile.h>
6864 simple_sendfile(int out_fd
, int in_fd
, off_t
*offset
, size_t count
)
6866 return sendfile(out_fd
, in_fd
, offset
, count
);
6875 copy_stream_sendfile(struct copy_stream_struct
*stp
)
6877 struct stat src_stat
, dst_stat
;
6885 ret
= fstat(stp
->src_fd
, &src_stat
);
6887 stp
->syserr
= "fstat";
6888 stp
->error_no
= errno
;
6891 if (!S_ISREG(src_stat
.st_mode
))
6894 ret
= fstat(stp
->dst_fd
, &dst_stat
);
6896 stp
->syserr
= "fstat";
6897 stp
->error_no
= errno
;
6900 if ((dst_stat
.st_mode
& S_IFMT
) != S_IFSOCK
)
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) {
6909 copy_length
= src_stat
.st_size
- src_offset
;
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
;
6917 copy_length
= src_stat
.st_size
- cur
;
6923 ss
= simple_sendfile(stp
->dst_fd
, stp
->src_fd
, &src_offset
, copy_length
);
6926 ss
= simple_sendfile(stp
->dst_fd
, stp
->src_fd
, NULL
, copy_length
);
6931 if (0 < copy_length
) {
6944 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6947 if (copy_stream_wait_write(stp
) == -1)
6949 if (RUBY_VM_INTERRUPTED(stp
->th
))
6951 goto retry_sendfile
;
6953 stp
->syserr
= "sendfile";
6954 stp
->error_no
= errno
;
6962 copy_stream_read(struct copy_stream_struct
*stp
, char *buf
, int len
, off_t offset
)
6966 if (offset
== (off_t
)-1)
6967 ss
= read(stp
->src_fd
, buf
, len
);
6970 ss
= pread(stp
->src_fd
, buf
, len
, offset
);
6972 stp
->notimp
= "pread";
6982 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6985 if (copy_stream_wait_read(stp
) == -1)
6991 stp
->notimp
= "pread";
6994 stp
->syserr
= offset
== (off_t
)-1 ? "read" : "pread";
6995 stp
->error_no
= errno
;
7002 copy_stream_write(struct copy_stream_struct
*stp
, char *buf
, int len
)
7007 ss
= write(stp
->dst_fd
, buf
+off
, len
);
7009 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
7010 if (copy_stream_wait_write(stp
) == -1)
7014 stp
->syserr
= "write";
7015 stp
->error_no
= errno
;
7026 copy_stream_read_write(struct copy_stream_struct
*stp
)
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
) {
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
;
7050 src_offset
= (off_t
)-1;
7054 while (use_eof
|| 0 < copy_length
) {
7055 if (!use_eof
&& copy_length
< sizeof(buf
)) {
7062 ss
= copy_stream_read(stp
, buf
, len
, src_offset
);
7067 ss
= copy_stream_read(stp
, buf
, len
, (off_t
)-1);
7069 if (ss
<= 0) /* EOF or error */
7072 ret
= copy_stream_write(stp
, buf
, ss
);
7079 if (RUBY_VM_INTERRUPTED(stp
->th
))
7085 copy_stream_func(void *arg
)
7087 struct copy_stream_struct
*stp
= (struct copy_stream_struct
*)arg
;
7093 ret
= copy_stream_sendfile(stp
);
7095 goto finish
; /* error or success */
7098 copy_stream_read_write(stp
);
7107 copy_stream_fallback_body(VALUE arg
)
7109 struct copy_stream_struct
*stp
= (struct copy_stream_struct
*)arg
;
7110 const int buflen
= 16*1024;
7112 VALUE buf
= rb_str_buf_new(buflen
);
7113 long rest
= stp
->copy_length
;
7114 off_t off
= stp
->src_offset
;
7119 if (stp
->copy_length
== (off_t
)-1) {
7125 l
= buflen
< rest
? buflen
: rest
;
7127 if (stp
->src_fd
== -1) {
7128 rb_funcall(stp
->src
, id_readpartial
, 2, INT2FIX(l
), buf
);
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
);
7139 rb_str_resize(buf
, ss
);
7140 if (off
!= (off_t
)-1)
7143 n
= rb_io_write(stp
->dst
, buf
);
7144 numwrote
= NUM2LONG(n
);
7145 stp
->total
+= numwrote
;
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);
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;
7172 stp
->th
= GET_THREAD();
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")))) {
7184 src_io
= rb_check_convert_type(stp
->src
, T_FILE
, "IO", "to_io");
7185 if (NIL_P(src_io
)) {
7187 int flags
= O_RDONLY
;
7191 FilePathValue(stp
->src
);
7193 args
[1] = INT2NUM(flags
);
7194 src_io
= rb_class_new_instance(2, args
, rb_cFile
);
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")))) {
7212 dst_io
= rb_check_convert_type(stp
->dst
, T_FILE
, "IO", "to_io");
7213 if (NIL_P(dst_io
)) {
7215 int flags
= O_WRONLY
|O_CREAT
|O_TRUNC
;
7219 FilePathValue(stp
->dst
);
7221 args
[1] = INT2NUM(flags
);
7222 args
[2] = INT2FIX(0600);
7223 dst_io
= rb_class_new_instance(3, args
, rb_cFile
);
7228 dst_io
= GetWriteIO(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
;
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
);
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)
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);
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
);
7285 errno
= stp
->error_no
;
7286 rb_sys_fail(stp
->syserr
);
7289 rb_raise(rb_eNotImpError
, "%s() not implemented", stp
->notimp
);
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.
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
);
7336 st
.copy_length
= (off_t
)-1;
7338 st
.copy_length
= NUM2OFFT(length
);
7340 if (NIL_P(src_offset
))
7341 st
.src_offset
= (off_t
)-1;
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
);
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>.
7359 rb_io_external_encoding(VALUE io
)
7363 GetOpenFile(io
, fptr
);
7365 return rb_enc_from_encoding(fptr
->enc2
);
7367 if (fptr
->mode
& FMODE_WRITABLE
) {
7369 return rb_enc_from_encoding(fptr
->enc
);
7372 return rb_enc_from_encoding(io_read_encoding(fptr
));
7377 * io.internal_encoding => encoding
7379 * Returns the Encoding of the internal string if conversion is
7380 * specified. Otherwise returns nil.
7384 rb_io_internal_encoding(VALUE io
)
7388 GetOpenFile(io
, fptr
);
7389 if (!fptr
->enc2
) return Qnil
;
7390 return rb_enc_from_encoding(io_read_encoding(fptr
));
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.
7409 rb_io_set_encoding(int argc
, VALUE
*argv
, VALUE io
)
7414 rb_scan_args(argc
, argv
, "11", &v1
, &v2
);
7415 GetOpenFile(io
, fptr
);
7416 io_encoding_set(fptr
, argc
, v1
, v2
);
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
));
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
));
7439 argf_set_encoding(int argc
, VALUE
*argv
, VALUE argf
)
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
;
7454 argf_tell(VALUE argf
)
7457 rb_raise(rb_eArgError
, "no stream to tell");
7460 return rb_io_tell(current_file
);
7464 argf_seek_m(int argc
, VALUE
*argv
, VALUE argf
)
7467 rb_raise(rb_eArgError
, "no stream to seek");
7469 ARGF_FORWARD(argc
, argv
);
7470 return rb_io_seek_m(argc
, argv
, current_file
);
7474 argf_set_pos(VALUE argf
, VALUE offset
)
7477 rb_raise(rb_eArgError
, "no stream to set position");
7479 ARGF_FORWARD(1, &offset
);
7480 return rb_io_set_pos(current_file
, offset
);
7484 argf_rewind(VALUE argf
)
7487 rb_raise(rb_eArgError
, "no stream to rewind");
7490 return rb_io_rewind(current_file
);
7494 argf_fileno(VALUE argf
)
7497 rb_raise(rb_eArgError
, "no stream");
7500 return rb_io_fileno(current_file
);
7504 argf_to_io(VALUE argf
)
7508 return current_file
;
7512 argf_eof(VALUE argf
)
7515 if (init_p
== 0) return Qtrue
;
7517 if (rb_io_eof(current_file
)) {
7525 argf_read(int argc
, VALUE
*argv
, VALUE argf
)
7527 VALUE tmp
, str
, length
;
7530 rb_scan_args(argc
, argv
, "02", &length
, &str
);
7531 if (!NIL_P(length
)) {
7532 len
= NUM2LONG(argv
[0]);
7536 rb_str_resize(str
,0);
7544 if (ARGF_GENERIC_INPUT_P()) {
7545 tmp
= argf_forward(argc
, argv
, argf
);
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
)) {
7554 argf_close(current_file
);
7559 else if (argc
>= 1) {
7560 if (RSTRING_LEN(str
) < len
) {
7561 len
-= RSTRING_LEN(str
);
7562 argv
[0] = INT2NUM(len
);
7569 struct argf_call_arg
{
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
);
7584 argf_readpartial(int argc
, VALUE
*argv
, VALUE argf
)
7586 VALUE tmp
, str
, length
;
7588 rb_scan_args(argc
, argv
, "11", &length
, &str
);
7595 rb_str_resize(str
, 0);
7598 if (ARGF_GENERIC_INPUT_P()) {
7599 struct argf_call_arg arg
;
7603 tmp
= rb_rescue2(argf_forward_call
, (VALUE
)&arg
,
7604 RUBY_METHOD_FUNC(0), Qnil
, rb_eEOFError
, (VALUE
)0);
7607 tmp
= io_getpartial(argc
, argv
, current_file
, 0);
7613 argf_close(current_file
);
7615 if (RARRAY_LEN(rb_argv
) == 0)
7618 str
= rb_str_new(NULL
, 0);
7625 argf_getc(VALUE argf
)
7630 if (!next_argv()) return Qnil
;
7631 if (ARGF_GENERIC_INPUT_P()) {
7632 ch
= rb_funcall3(current_file
, rb_intern("getc"), 0, 0);
7635 ch
= rb_io_getc(current_file
);
7637 if (NIL_P(ch
) && next_p
!= -1) {
7638 argf_close(current_file
);
7647 argf_getbyte(VALUE argf
)
7652 if (!next_argv()) return Qnil
;
7653 if (TYPE(current_file
) != T_FILE
) {
7654 ch
= rb_funcall3(current_file
, rb_intern("getbyte"), 0, 0);
7657 ch
= rb_io_getbyte(current_file
);
7659 if (NIL_P(ch
) && next_p
!= -1) {
7660 argf_close(current_file
);
7669 argf_readchar(VALUE argf
)
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);
7679 ch
= rb_io_getc(current_file
);
7681 if (NIL_P(ch
) && next_p
!= -1) {
7682 argf_close(current_file
);
7691 argf_readbyte(VALUE argf
)
7695 NEXT_ARGF_FORWARD(0, 0);
7696 c
= argf_getbyte(argf
);
7704 argf_each_line(int argc
, VALUE
*argv
, VALUE argf
)
7706 RETURN_ENUMERATOR(argf
, argc
, argv
);
7708 if (!next_argv()) return Qnil
;
7709 rb_block_call(current_file
, rb_intern("each_line"), argc
, argv
, rb_yield
, 0);
7716 argf_each_byte(VALUE argf
)
7718 RETURN_ENUMERATOR(argf
, 0, 0);
7720 if (!next_argv()) return Qnil
;
7721 rb_block_call(current_file
, rb_intern("each_byte"), 0, 0, rb_yield
, 0);
7727 argf_each_char(VALUE argf
)
7729 RETURN_ENUMERATOR(argf
, 0, 0);
7731 if (!next_argv()) return Qnil
;
7732 rb_block_call(current_file
, rb_intern("each_char"), 0, 0, rb_yield
, 0);
7738 argf_filename(VALUE argf
)
7745 argf_filename_getter(ID id
, VALUE
*var
)
7747 return argf_filename(*var
);
7751 argf_file(VALUE argf
)
7754 return current_file
;
7758 argf_binmode_m(VALUE argf
)
7763 rb_io_binmode(current_file
);
7768 argf_binmode_p(VALUE argf
)
7770 return argf_binmode
? Qtrue
: Qfalse
;
7774 argf_skip(VALUE argf
)
7777 argf_close(current_file
);
7784 argf_close_m(VALUE argf
)
7787 argf_close(current_file
);
7796 argf_closed(VALUE argf
)
7800 return rb_io_closed(current_file
);
7804 argf_to_s(VALUE argf
)
7806 return rb_str_new2("ARGF");
7810 argf_inplace_mode_get(VALUE argf
)
7812 if (!ruby_inplace_mode
) return Qnil
;
7813 return rb_str_new2(ruby_inplace_mode
);
7817 opt_i_get(ID id
, VALUE
*var
)
7819 return argf_inplace_mode_get(*var
);
7823 argf_inplace_mode_set(VALUE argf
, VALUE val
)
7826 if (ruby_inplace_mode
) free(ruby_inplace_mode
);
7827 ruby_inplace_mode
= 0;
7831 if (ruby_inplace_mode
) free(ruby_inplace_mode
);
7832 ruby_inplace_mode
= 0;
7833 ruby_inplace_mode
= strdup(RSTRING_PTR(val
));
7839 opt_i_set(VALUE val
, ID id
, VALUE
*var
)
7841 argf_inplace_mode_set(*var
, val
);
7845 ruby_get_inplace_mode(void)
7847 return ruby_inplace_mode
;
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
);
7859 argf_argv(VALUE argf
)
7865 argf_argv_getter(ID id
, VALUE
*var
)
7867 return argf_argv(*var
);
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
7888 * * A plain string represents a filename suitable for the underlying
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
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
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
7919 * If the mode is given as a String, it must be one of the
7920 * values listed in the following table.
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
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.
7956 #define rb_intern(str) rb_intern_const(str)
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
},
7969 cygwin_internal(CW_PERFILE
, pf
);
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
);
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)
8214 rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK
));
8216 rb_file_const("NONBLOCK", INT2FIX(O_NDELAY
));
8219 rb_file_const("TRUNC", INT2FIX(O_TRUNC
));
8221 rb_file_const("NOCTTY", INT2FIX(O_NOCTTY
));
8224 rb_file_const("BINARY", INT2FIX(O_BINARY
));
8226 rb_file_const("BINARY", INT2FIX(0));
8229 rb_file_const("SYNC", INT2FIX(O_SYNC
));
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"));