Ruby 1.9.3p194(2012-04-20revision35410)

io.c

Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   io.c -
00004 
00005   $Author: naruse $
00006   created at: Fri Oct 15 18:08:59 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
00010   Copyright (C) 2000  Information-technology Promotion Agency, Japan
00011 
00012 **********************************************************************/
00013 
00014 #include "ruby/ruby.h"
00015 #include "ruby/io.h"
00016 #include "dln.h"
00017 #include "internal.h"
00018 #include <ctype.h>
00019 #include <errno.h>
00020 
00021 #define free(x) xfree(x)
00022 
00023 #if defined(DOSISH) || defined(__CYGWIN__)
00024 #include <io.h>
00025 #endif
00026 
00027 #include <sys/types.h>
00028 #if defined HAVE_NET_SOCKET_H
00029 # include <net/socket.h>
00030 #elif defined HAVE_SYS_SOCKET_H
00031 # include <sys/socket.h>
00032 #endif
00033 
00034 #if defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__EMX__) || defined(__BEOS__) || defined(__HAIKU__)
00035 # define NO_SAFE_RENAME
00036 #endif
00037 
00038 #if defined(__CYGWIN__) || defined(_WIN32)
00039 # define NO_LONG_FNAME
00040 #endif
00041 
00042 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(sun) || defined(_nec_ews)
00043 # define USE_SETVBUF
00044 #endif
00045 
00046 #ifdef __QNXNTO__
00047 #include "unix.h"
00048 #endif
00049 
00050 #include <sys/types.h>
00051 #if defined(HAVE_SYS_IOCTL_H) && !defined(_WIN32)
00052 #include <sys/ioctl.h>
00053 #endif
00054 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
00055 #include <fcntl.h>
00056 #elif defined(HAVE_SYS_FCNTL_H)
00057 #include <sys/fcntl.h>
00058 #endif
00059 
00060 #if !HAVE_OFF_T && !defined(off_t)
00061 # define off_t  long
00062 #endif
00063 
00064 #include <sys/stat.h>
00065 
00066 /* EMX has sys/param.h, but.. */
00067 #if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
00068 # include <sys/param.h>
00069 #endif
00070 
00071 #if !defined NOFILE
00072 # define NOFILE 64
00073 #endif
00074 
00075 #ifdef HAVE_UNISTD_H
00076 #include <unistd.h>
00077 #endif
00078 
00079 #ifdef HAVE_SYSCALL_H
00080 #include <syscall.h>
00081 #elif defined HAVE_SYS_SYSCALL_H
00082 #include <sys/syscall.h>
00083 #endif
00084 
00085 #if defined(__BEOS__) || defined(__HAIKU__)
00086 # ifndef NOFILE
00087 #  define NOFILE (OPEN_MAX)
00088 # endif
00089 #endif
00090 
00091 #include "ruby/util.h"
00092 
00093 #ifndef O_ACCMODE
00094 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
00095 #endif
00096 
00097 #if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
00098 # error off_t is bigger than long, but you have no long long...
00099 #endif
00100 
00101 #ifndef PIPE_BUF
00102 # ifdef _POSIX_PIPE_BUF
00103 #  define PIPE_BUF _POSIX_PIPE_BUF
00104 # else
00105 #  define PIPE_BUF 512 /* is this ok? */
00106 # endif
00107 #endif
00108 
00109 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
00110 
00111 #define IO_RBUF_CAPA_MIN  8192
00112 #define IO_CBUF_CAPA_MIN  (128*1024)
00113 #define IO_RBUF_CAPA_FOR(fptr) (NEED_READCONV(fptr) ? IO_CBUF_CAPA_MIN : IO_RBUF_CAPA_MIN)
00114 #define IO_WBUF_CAPA_MIN  8192
00115 
00116 /* define system APIs */
00117 #ifdef _WIN32
00118 #undef open
00119 #define open    rb_w32_uopen
00120 #endif
00121 
00122 VALUE rb_cIO;
00123 VALUE rb_eEOFError;
00124 VALUE rb_eIOError;
00125 VALUE rb_mWaitReadable;
00126 VALUE rb_mWaitWritable;
00127 
00128 VALUE rb_stdin, rb_stdout, rb_stderr;
00129 VALUE rb_deferr;                /* rescue VIM plugin */
00130 static VALUE orig_stdout, orig_stderr;
00131 
00132 VALUE rb_output_fs;
00133 VALUE rb_rs;
00134 VALUE rb_output_rs;
00135 VALUE rb_default_rs;
00136 
00137 static VALUE argf;
00138 
00139 static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
00140 static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
00141 static VALUE sym_textmode, sym_binmode, sym_autoclose;
00142 
00143 struct argf {
00144     VALUE filename, current_file;
00145     long last_lineno;           /* $. */
00146     long lineno;
00147     VALUE argv;
00148     char *inplace;
00149     struct rb_io_enc_t encs;
00150     int8_t init_p, next_p, binmode;
00151 };
00152 
00153 static int max_file_descriptor = NOFILE;
00154 void
00155 rb_update_max_fd(int fd)
00156 {
00157     struct stat buf;
00158     if (fstat(fd, &buf) != 0 && errno == EBADF) {
00159         rb_bug("rb_update_max_fd: invalid fd (%d) given.", fd);
00160     }
00161     if (max_file_descriptor < fd) max_file_descriptor = fd;
00162 }
00163 
00164 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
00165 #define ARGF argf_of(argf)
00166 
00167 #ifdef _STDIO_USES_IOSTREAM  /* GNU libc */
00168 #  ifdef _IO_fpos_t
00169 #    define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
00170 #  else
00171 #    define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
00172 #  endif
00173 #elif defined(FILE_COUNT)
00174 #  define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
00175 #elif defined(FILE_READEND)
00176 #  define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
00177 #elif defined(__BEOS__) || defined(__HAIKU__)
00178 #  define STDIO_READ_DATA_PENDING(fp) ((fp)->_state._eof == 0)
00179 #else
00180 #  define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
00181 #endif
00182 
00183 #define GetWriteIO(io) rb_io_get_write_io(io)
00184 
00185 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf.len)
00186 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf.len)
00187 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf.ptr+(fptr)->rbuf.off)
00188 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
00189 
00190 #define READ_CHAR_PENDING(fptr) ((fptr)->cbuf.len)
00191 #define READ_CHAR_PENDING_COUNT(fptr) ((fptr)->cbuf.len)
00192 #define READ_CHAR_PENDING_PTR(fptr) ((fptr)->cbuf.ptr+(fptr)->cbuf.off)
00193 
00194 #if defined(_WIN32)
00195 #define WAIT_FD_IN_WIN32(fptr) \
00196     (rb_w32_has_cancel_io() ? 0 : rb_thread_wait_fd((fptr)->fd))
00197 #else
00198 #define WAIT_FD_IN_WIN32(fptr)
00199 #endif
00200 
00201 #define READ_CHECK(fptr) do {\
00202     if (!READ_DATA_PENDING(fptr)) {\
00203         WAIT_FD_IN_WIN32(fptr);\
00204         rb_io_check_closed(fptr);\
00205      }\
00206 } while(0)
00207 
00208 #ifndef S_ISSOCK
00209 #  ifdef _S_ISSOCK
00210 #    define S_ISSOCK(m) _S_ISSOCK(m)
00211 #  else
00212 #    ifdef _S_IFSOCK
00213 #      define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
00214 #    else
00215 #      ifdef S_IFSOCK
00216 #        define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
00217 #      endif
00218 #    endif
00219 #  endif
00220 #endif
00221 
00222 #define rb_sys_fail_path(path) rb_sys_fail_str(path)
00223 
00224 static int io_fflush(rb_io_t *);
00225 static rb_io_t *flush_before_seek(rb_io_t *fptr);
00226 
00227 #define NEED_NEWLINE_DECORATOR_ON_READ(fptr) ((fptr)->mode & FMODE_TEXTMODE)
00228 #define NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) ((fptr)->mode & FMODE_TEXTMODE)
00229 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
00230 /* Windows */
00231 # define DEFAULT_TEXTMODE FMODE_TEXTMODE
00232 # define TEXTMODE_NEWLINE_DECORATOR_ON_WRITE ECONV_CRLF_NEWLINE_DECORATOR
00233 /*
00234  * CRLF newline is set as default newline decorator.
00235  * If only CRLF newline conversion is needed, we use binary IO process
00236  * with OS's text mode for IO performance improvement.
00237  * If encoding conversion is needed or a user sets text mode, we use encoding
00238  * conversion IO process and universal newline decorator by default.
00239  */
00240 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || (fptr)->encs.ecflags & ~ECONV_CRLF_NEWLINE_DECORATOR)
00241 #define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || ((fptr)->encs.ecflags & ((ECONV_DECORATOR_MASK & ~ECONV_CRLF_NEWLINE_DECORATOR)|ECONV_STATEFUL_DECORATOR_MASK)))
00242 #define SET_BINARY_MODE(fptr) setmode((fptr)->fd, O_BINARY)
00243 
00244 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) do {\
00245     if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {\
00246         if (((fptr)->mode & FMODE_READABLE) &&\
00247             !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
00248             setmode((fptr)->fd, O_BINARY);\
00249         }\
00250         else {\
00251             setmode((fptr)->fd, O_TEXT);\
00252         }\
00253     }\
00254 } while(0)
00255 
00256 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) do {\
00257     if ((enc2) && ((ecflags) & ECONV_DEFAULT_NEWLINE_DECORATOR)) {\
00258         (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
00259     }\
00260 } while(0)
00261 
00262 /*
00263  * IO unread with taking care of removed '\r' in text mode.
00264  */
00265 static void
00266 io_unread(rb_io_t *fptr)
00267 {
00268     off_t r, pos;
00269     ssize_t read_size;
00270     long i;
00271     long newlines = 0;
00272     long extra_max;
00273     char *p;
00274     char *buf;
00275 
00276     rb_io_check_closed(fptr);
00277     if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
00278         return;
00279     }
00280 
00281     errno = 0;
00282     if (!rb_w32_fd_is_text(fptr->fd)) {
00283         r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
00284         if (r < 0 && errno) {
00285             if (errno == ESPIPE)
00286                 fptr->mode |= FMODE_DUPLEX;
00287             return;
00288         }
00289 
00290         fptr->rbuf.off = 0;
00291         fptr->rbuf.len = 0;
00292         return;
00293     }
00294 
00295     pos = lseek(fptr->fd, 0, SEEK_CUR);
00296     if (pos < 0 && errno) {
00297         if (errno == ESPIPE)
00298             fptr->mode |= FMODE_DUPLEX;
00299         return;
00300     }
00301 
00302     /* add extra offset for removed '\r' in rbuf */
00303     extra_max = (long)(pos - fptr->rbuf.len);
00304     p = fptr->rbuf.ptr + fptr->rbuf.off;
00305     for (i = 0; i < fptr->rbuf.len; i++) {
00306         if (*p == '\n') newlines++;
00307         if (extra_max == newlines) break;
00308         p++;
00309     }
00310 
00311     buf = ALLOC_N(char, fptr->rbuf.len + newlines);
00312     while (newlines >= 0) {
00313         r = lseek(fptr->fd, pos - fptr->rbuf.len - newlines, SEEK_SET);
00314         if (newlines == 0) break;
00315         if (r < 0) {
00316             newlines--;
00317             continue;
00318         }
00319         read_size = _read(fptr->fd, buf, fptr->rbuf.len + newlines);
00320         if (read_size < 0) {
00321             free(buf);
00322             rb_sys_fail_path(fptr->pathv);
00323         }
00324         if (read_size == fptr->rbuf.len) {
00325             lseek(fptr->fd, r, SEEK_SET);
00326             break;
00327         }
00328         else {
00329             newlines--;
00330         }
00331     }
00332     free(buf);
00333     fptr->rbuf.off = 0;
00334     fptr->rbuf.len = 0;
00335     return;
00336 }
00337 
00338 /*
00339  * We use io_seek to back cursor position when changing mode from text to binary,
00340  * but stdin and pipe cannot seek back. Stdin and pipe read should use encoding
00341  * conversion for working properly with mode change.
00342  *
00343  * Return previous translation mode.
00344  */
00345 static inline int
00346 set_binary_mode_with_seek_cur(rb_io_t *fptr)
00347 {
00348     if (!rb_w32_fd_is_text(fptr->fd)) return O_BINARY;
00349 
00350     if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
00351         return setmode(fptr->fd, O_BINARY);
00352     }
00353     flush_before_seek(fptr);
00354     return setmode(fptr->fd, O_BINARY);
00355 }
00356 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) set_binary_mode_with_seek_cur(fptr)
00357 
00358 #else
00359 /* Unix */
00360 # define DEFAULT_TEXTMODE 0
00361 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || NEED_NEWLINE_DECORATOR_ON_READ(fptr))
00362 #define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) || ((fptr)->encs.ecflags & (ECONV_DECORATOR_MASK|ECONV_STATEFUL_DECORATOR_MASK)))
00363 #define SET_BINARY_MODE(fptr) 0
00364 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) 0
00365 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) 0
00366 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) 0
00367 #endif
00368 
00369 #if !defined HAVE_SHUTDOWN && !defined shutdown
00370 #define shutdown(a,b)   0
00371 #endif
00372 
00373 #if defined(_WIN32)
00374 #define is_socket(fd, path)     rb_w32_is_socket(fd)
00375 #elif !defined(S_ISSOCK)
00376 #define is_socket(fd, path)     0
00377 #else
00378 static int
00379 is_socket(int fd, VALUE path)
00380 {
00381     struct stat sbuf;
00382     if (fstat(fd, &sbuf) < 0)
00383         rb_sys_fail_path(path);
00384     return S_ISSOCK(sbuf.st_mode);
00385 }
00386 #endif
00387 
00388 void
00389 rb_eof_error(void)
00390 {
00391     rb_raise(rb_eEOFError, "end of file reached");
00392 }
00393 
00394 VALUE
00395 rb_io_taint_check(VALUE io)
00396 {
00397     if (!OBJ_UNTRUSTED(io) && rb_safe_level() >= 4)
00398         rb_raise(rb_eSecurityError, "Insecure: operation on trusted IO");
00399     rb_check_frozen(io);
00400     return io;
00401 }
00402 
00403 void
00404 rb_io_check_initialized(rb_io_t *fptr)
00405 {
00406     if (!fptr) {
00407         rb_raise(rb_eIOError, "uninitialized stream");
00408     }
00409 }
00410 
00411 void
00412 rb_io_check_closed(rb_io_t *fptr)
00413 {
00414     rb_io_check_initialized(fptr);
00415     if (fptr->fd < 0) {
00416         rb_raise(rb_eIOError, "closed stream");
00417     }
00418 }
00419 
00420 
00421 VALUE
00422 rb_io_get_io(VALUE io)
00423 {
00424     return rb_convert_type(io, T_FILE, "IO", "to_io");
00425 }
00426 
00427 static VALUE
00428 rb_io_check_io(VALUE io)
00429 {
00430     return rb_check_convert_type(io, T_FILE, "IO", "to_io");
00431 }
00432 
00433 VALUE
00434 rb_io_get_write_io(VALUE io)
00435 {
00436     VALUE write_io;
00437     rb_io_check_initialized(RFILE(io)->fptr);
00438     write_io = RFILE(io)->fptr->tied_io_for_writing;
00439     if (write_io) {
00440         return write_io;
00441     }
00442     return io;
00443 }
00444 
00445 VALUE
00446 rb_io_set_write_io(VALUE io, VALUE w)
00447 {
00448     VALUE write_io;
00449     rb_io_check_initialized(RFILE(io)->fptr);
00450     if (!RTEST(w)) {
00451         w = 0;
00452     }
00453     else {
00454         GetWriteIO(w);
00455     }
00456     write_io = RFILE(io)->fptr->tied_io_for_writing;
00457     RFILE(io)->fptr->tied_io_for_writing = w;
00458     return write_io ? write_io : Qnil;
00459 }
00460 
00461 /*
00462  *  call-seq:
00463  *     IO.try_convert(obj)  ->  io or nil
00464  *
00465  *  Try to convert <i>obj</i> into an IO, using to_io method.
00466  *  Returns converted IO or nil if <i>obj</i> cannot be converted
00467  *  for any reason.
00468  *
00469  *     IO.try_convert(STDOUT)     #=> STDOUT
00470  *     IO.try_convert("STDOUT")   #=> nil
00471  *
00472  *     require 'zlib'
00473  *     f = open("/tmp/zz.gz")       #=> #<File:/tmp/zz.gz>
00474  *     z = Zlib::GzipReader.open(f) #=> #<Zlib::GzipReader:0x81d8744>
00475  *     IO.try_convert(z)            #=> #<File:/tmp/zz.gz>
00476  *
00477  */
00478 static VALUE
00479 rb_io_s_try_convert(VALUE dummy, VALUE io)
00480 {
00481     return rb_io_check_io(io);
00482 }
00483 
00484 #if !(defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32))
00485 static void
00486 io_unread(rb_io_t *fptr)
00487 {
00488     off_t r;
00489     rb_io_check_closed(fptr);
00490     if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX)
00491         return;
00492     /* xxx: target position may be negative if buffer is filled by ungetc */
00493     errno = 0;
00494     r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
00495     if (r < 0 && errno) {
00496         if (errno == ESPIPE)
00497             fptr->mode |= FMODE_DUPLEX;
00498         return;
00499     }
00500     fptr->rbuf.off = 0;
00501     fptr->rbuf.len = 0;
00502     return;
00503 }
00504 #endif
00505 
00506 static rb_encoding *io_input_encoding(rb_io_t *fptr);
00507 
00508 static void
00509 io_ungetbyte(VALUE str, rb_io_t *fptr)
00510 {
00511     long len = RSTRING_LEN(str);
00512 
00513     if (fptr->rbuf.ptr == NULL) {
00514         const int min_capa = IO_RBUF_CAPA_FOR(fptr);
00515         fptr->rbuf.off = 0;
00516         fptr->rbuf.len = 0;
00517 #if SIZEOF_LONG > SIZEOF_INT
00518         if (len > INT_MAX)
00519             rb_raise(rb_eIOError, "ungetbyte failed");
00520 #endif
00521         if (len > min_capa)
00522             fptr->rbuf.capa = (int)len;
00523         else
00524             fptr->rbuf.capa = min_capa;
00525         fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
00526     }
00527     if (fptr->rbuf.capa < len + fptr->rbuf.len) {
00528         rb_raise(rb_eIOError, "ungetbyte failed");
00529     }
00530     if (fptr->rbuf.off < len) {
00531         MEMMOVE(fptr->rbuf.ptr+fptr->rbuf.capa-fptr->rbuf.len,
00532                 fptr->rbuf.ptr+fptr->rbuf.off,
00533                 char, fptr->rbuf.len);
00534         fptr->rbuf.off = fptr->rbuf.capa-fptr->rbuf.len;
00535     }
00536     fptr->rbuf.off-=(int)len;
00537     fptr->rbuf.len+=(int)len;
00538     MEMMOVE(fptr->rbuf.ptr+fptr->rbuf.off, RSTRING_PTR(str), char, len);
00539 }
00540 
00541 static rb_io_t *
00542 flush_before_seek(rb_io_t *fptr)
00543 {
00544     if (io_fflush(fptr) < 0)
00545         rb_sys_fail(0);
00546     io_unread(fptr);
00547     errno = 0;
00548     return fptr;
00549 }
00550 
00551 #define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, (ofs), (whence)))
00552 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
00553 
00554 #ifndef SEEK_CUR
00555 # define SEEK_SET 0
00556 # define SEEK_CUR 1
00557 # define SEEK_END 2
00558 #endif
00559 
00560 #define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
00561 
00562 void
00563 rb_io_check_char_readable(rb_io_t *fptr)
00564 {
00565     rb_io_check_closed(fptr);
00566     if (!(fptr->mode & FMODE_READABLE)) {
00567         rb_raise(rb_eIOError, "not opened for reading");
00568     }
00569     if (fptr->wbuf.len) {
00570         if (io_fflush(fptr) < 0)
00571             rb_sys_fail(0);
00572     }
00573     if (fptr->tied_io_for_writing) {
00574         rb_io_t *wfptr;
00575         GetOpenFile(fptr->tied_io_for_writing, wfptr);
00576         if (io_fflush(wfptr) < 0)
00577             rb_sys_fail(0);
00578     }
00579 }
00580 
00581 void
00582 rb_io_check_byte_readable(rb_io_t *fptr)
00583 {
00584     rb_io_check_char_readable(fptr);
00585     if (READ_CHAR_PENDING(fptr)) {
00586         rb_raise(rb_eIOError, "byte oriented read for character buffered IO");
00587     }
00588 }
00589 
00590 void
00591 rb_io_check_readable(rb_io_t *fptr)
00592 {
00593     rb_io_check_byte_readable(fptr);
00594 }
00595 
00596 static rb_encoding*
00597 io_read_encoding(rb_io_t *fptr)
00598 {
00599     if (fptr->encs.enc) {
00600         return fptr->encs.enc;
00601     }
00602     return rb_default_external_encoding();
00603 }
00604 
00605 static rb_encoding*
00606 io_input_encoding(rb_io_t *fptr)
00607 {
00608     if (fptr->encs.enc2) {
00609         return fptr->encs.enc2;
00610     }
00611     return io_read_encoding(fptr);
00612 }
00613 
00614 void
00615 rb_io_check_writable(rb_io_t *fptr)
00616 {
00617     rb_io_check_closed(fptr);
00618     if (!(fptr->mode & FMODE_WRITABLE)) {
00619         rb_raise(rb_eIOError, "not opened for writing");
00620     }
00621     if (fptr->rbuf.len) {
00622         io_unread(fptr);
00623     }
00624 }
00625 
00626 int
00627 rb_io_read_pending(rb_io_t *fptr)
00628 {
00629     /* This function is used for bytes and chars.  Confusing. */
00630     if (READ_CHAR_PENDING(fptr))
00631         return 1; /* should raise? */
00632     return READ_DATA_PENDING(fptr);
00633 }
00634 
00635 void
00636 rb_read_check(FILE *fp)
00637 {
00638     if (!STDIO_READ_DATA_PENDING(fp)) {
00639         rb_thread_wait_fd(fileno(fp));
00640     }
00641 }
00642 
00643 void
00644 rb_io_read_check(rb_io_t *fptr)
00645 {
00646     if (!READ_DATA_PENDING(fptr)) {
00647         rb_thread_wait_fd(fptr->fd);
00648     }
00649     return;
00650 }
00651 
00652 static int
00653 ruby_dup(int orig)
00654 {
00655     int fd;
00656 
00657     fd = dup(orig);
00658     if (fd < 0) {
00659         if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
00660             rb_gc();
00661             fd = dup(orig);
00662         }
00663         if (fd < 0) {
00664             rb_sys_fail(0);
00665         }
00666     }
00667     rb_update_max_fd(fd);
00668     return fd;
00669 }
00670 
00671 static VALUE
00672 io_alloc(VALUE klass)
00673 {
00674     NEWOBJ(io, struct RFile);
00675     OBJSETUP(io, klass, T_FILE);
00676 
00677     io->fptr = 0;
00678 
00679     return (VALUE)io;
00680 }
00681 
00682 #ifndef S_ISREG
00683 #   define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
00684 #endif
00685 
00686 static int
00687 wsplit_p(rb_io_t *fptr)
00688 {
00689 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
00690     int r;
00691 #endif
00692 
00693     if (!(fptr->mode & FMODE_WSPLIT_INITIALIZED)) {
00694         struct stat buf;
00695         if (fstat(fptr->fd, &buf) == 0 &&
00696             !S_ISREG(buf.st_mode)
00697 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
00698             && (r = fcntl(fptr->fd, F_GETFL)) != -1 &&
00699             !(r & O_NONBLOCK)
00700 #endif
00701             ) {
00702             fptr->mode |= FMODE_WSPLIT;
00703         }
00704         fptr->mode |= FMODE_WSPLIT_INITIALIZED;
00705     }
00706     return fptr->mode & FMODE_WSPLIT;
00707 }
00708 
00709 struct io_internal_read_struct {
00710     int fd;
00711     void *buf;
00712     size_t capa;
00713 };
00714 
00715 struct io_internal_write_struct {
00716     int fd;
00717     const void *buf;
00718     size_t capa;
00719 };
00720 
00721 static VALUE
00722 internal_read_func(void *ptr)
00723 {
00724     struct io_internal_read_struct *iis = ptr;
00725     return read(iis->fd, iis->buf, iis->capa);
00726 }
00727 
00728 static VALUE
00729 internal_write_func(void *ptr)
00730 {
00731     struct io_internal_write_struct *iis = ptr;
00732     return write(iis->fd, iis->buf, iis->capa);
00733 }
00734 
00735 static ssize_t
00736 rb_read_internal(int fd, void *buf, size_t count)
00737 {
00738     struct io_internal_read_struct iis;
00739     iis.fd = fd;
00740     iis.buf = buf;
00741     iis.capa = count;
00742 
00743     return (ssize_t)rb_thread_io_blocking_region(internal_read_func, &iis, fd);
00744 }
00745 
00746 static ssize_t
00747 rb_write_internal(int fd, const void *buf, size_t count)
00748 {
00749     struct io_internal_write_struct iis;
00750     iis.fd = fd;
00751     iis.buf = buf;
00752     iis.capa = count;
00753 
00754     return (ssize_t)rb_thread_io_blocking_region(internal_write_func, &iis, fd);
00755 }
00756 
00757 static long
00758 io_writable_length(rb_io_t *fptr, long l)
00759 {
00760     if (PIPE_BUF < l &&
00761         !rb_thread_alone() &&
00762         wsplit_p(fptr)) {
00763         l = PIPE_BUF;
00764     }
00765     return l;
00766 }
00767 
00768 static VALUE
00769 io_flush_buffer_sync(void *arg)
00770 {
00771     rb_io_t *fptr = arg;
00772     long l = io_writable_length(fptr, fptr->wbuf.len);
00773     ssize_t r = write(fptr->fd, fptr->wbuf.ptr+fptr->wbuf.off, (size_t)l);
00774 
00775     if (fptr->wbuf.len <= r) {
00776         fptr->wbuf.off = 0;
00777         fptr->wbuf.len = 0;
00778         return 0;
00779     }
00780     if (0 <= r) {
00781         fptr->wbuf.off += (int)r;
00782         fptr->wbuf.len -= (int)r;
00783         errno = EAGAIN;
00784     }
00785     return (VALUE)-1;
00786 }
00787 
00788 static VALUE
00789 io_flush_buffer_async(VALUE arg)
00790 {
00791     rb_io_t *fptr = (rb_io_t *)arg;
00792     return rb_thread_io_blocking_region(io_flush_buffer_sync, fptr, fptr->fd);
00793 }
00794 
00795 static inline int
00796 io_flush_buffer(rb_io_t *fptr)
00797 {
00798     if (fptr->write_lock) {
00799         return (int)rb_mutex_synchronize(fptr->write_lock, io_flush_buffer_async, (VALUE)fptr);
00800     }
00801     else {
00802         return (int)io_flush_buffer_async((VALUE)fptr);
00803     }
00804 }
00805 
00806 static int
00807 io_fflush(rb_io_t *fptr)
00808 {
00809     rb_io_check_closed(fptr);
00810     if (fptr->wbuf.len == 0)
00811         return 0;
00812     if (!rb_thread_fd_writable(fptr->fd)) {
00813         rb_io_check_closed(fptr);
00814     }
00815     while (fptr->wbuf.len > 0 && io_flush_buffer(fptr) != 0) {
00816         if (!rb_io_wait_writable(fptr->fd))
00817             return -1;
00818         rb_io_check_closed(fptr);
00819     }
00820     return 0;
00821 }
00822 
00823 int
00824 rb_io_wait_readable(int f)
00825 {
00826     if (f < 0) {
00827         rb_raise(rb_eIOError, "closed stream");
00828     }
00829     switch (errno) {
00830       case EINTR:
00831 #if defined(ERESTART)
00832       case ERESTART:
00833 #endif
00834         rb_thread_wait_fd(f);
00835         return TRUE;
00836 
00837       case EAGAIN:
00838 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
00839       case EWOULDBLOCK:
00840 #endif
00841         rb_wait_for_single_fd(f, RB_WAITFD_IN, NULL);
00842         return TRUE;
00843 
00844       default:
00845         return FALSE;
00846     }
00847 }
00848 
00849 int
00850 rb_io_wait_writable(int f)
00851 {
00852     if (f < 0) {
00853         rb_raise(rb_eIOError, "closed stream");
00854     }
00855     switch (errno) {
00856       case EINTR:
00857 #if defined(ERESTART)
00858       case ERESTART:
00859 #endif
00860         rb_thread_fd_writable(f);
00861         return TRUE;
00862 
00863       case EAGAIN:
00864 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
00865       case EWOULDBLOCK:
00866 #endif
00867         rb_wait_for_single_fd(f, RB_WAITFD_OUT, NULL);
00868         return TRUE;
00869 
00870       default:
00871         return FALSE;
00872     }
00873 }
00874 
00875 static void
00876 make_writeconv(rb_io_t *fptr)
00877 {
00878     if (!fptr->writeconv_initialized) {
00879         const char *senc, *denc;
00880         rb_encoding *enc;
00881         int ecflags;
00882         VALUE ecopts;
00883 
00884         fptr->writeconv_initialized = 1;
00885 
00886         ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_READ_MASK;
00887         ecopts = fptr->encs.ecopts;
00888 
00889         if (!fptr->encs.enc || (fptr->encs.enc == rb_ascii8bit_encoding() && !fptr->encs.enc2)) {
00890             /* no encoding conversion */
00891             fptr->writeconv_pre_ecflags = 0;
00892             fptr->writeconv_pre_ecopts = Qnil;
00893             fptr->writeconv = rb_econv_open_opts("", "", ecflags, ecopts);
00894             if (!fptr->writeconv)
00895                 rb_exc_raise(rb_econv_open_exc("", "", ecflags));
00896             fptr->writeconv_asciicompat = Qnil;
00897         }
00898         else {
00899             enc = fptr->encs.enc2 ? fptr->encs.enc2 : fptr->encs.enc;
00900             senc = rb_econv_asciicompat_encoding(rb_enc_name(enc));
00901             if (!senc && !(fptr->encs.ecflags & ECONV_STATEFUL_DECORATOR_MASK)) {
00902                 /* single conversion */
00903                 fptr->writeconv_pre_ecflags = ecflags;
00904                 fptr->writeconv_pre_ecopts = ecopts;
00905                 fptr->writeconv = NULL;
00906                 fptr->writeconv_asciicompat = Qnil;
00907             }
00908             else {
00909                 /* double conversion */
00910                 fptr->writeconv_pre_ecflags = ecflags & ~ECONV_STATEFUL_DECORATOR_MASK;
00911                 fptr->writeconv_pre_ecopts = ecopts;
00912                 if (senc) {
00913                     denc = rb_enc_name(enc);
00914                     fptr->writeconv_asciicompat = rb_str_new2(senc);
00915                 }
00916                 else {
00917                     senc = denc = "";
00918                     fptr->writeconv_asciicompat = rb_str_new2(rb_enc_name(enc));
00919                 }
00920                 ecflags = fptr->encs.ecflags & (ECONV_ERROR_HANDLER_MASK|ECONV_STATEFUL_DECORATOR_MASK);
00921                 ecopts = fptr->encs.ecopts;
00922                 fptr->writeconv = rb_econv_open_opts(senc, denc, ecflags, ecopts);
00923                 if (!fptr->writeconv)
00924                     rb_exc_raise(rb_econv_open_exc(senc, denc, ecflags));
00925             }
00926         }
00927     }
00928 }
00929 
00930 /* writing functions */
00931 struct binwrite_arg {
00932     rb_io_t *fptr;
00933     VALUE str;
00934     const char *ptr;
00935     long length;
00936 };
00937 
00938 struct write_arg {
00939     VALUE io;
00940     VALUE str;
00941     int nosync;
00942 };
00943 
00944 static VALUE
00945 io_binwrite_string(VALUE arg)
00946 {
00947     struct binwrite_arg *p = (struct binwrite_arg *)arg;
00948     long l = io_writable_length(p->fptr, p->length);
00949     return rb_write_internal(p->fptr->fd, p->ptr, l);
00950 }
00951 
00952 static long
00953 io_binwrite(VALUE str, const char *ptr, long len, rb_io_t *fptr, int nosync)
00954 {
00955     long n, r, offset = 0;
00956 
00957     if ((n = len) <= 0) return n;
00958     if (fptr->wbuf.ptr == NULL && !(!nosync && (fptr->mode & FMODE_SYNC))) {
00959         fptr->wbuf.off = 0;
00960         fptr->wbuf.len = 0;
00961         fptr->wbuf.capa = IO_WBUF_CAPA_MIN;
00962         fptr->wbuf.ptr = ALLOC_N(char, fptr->wbuf.capa);
00963         fptr->write_lock = rb_mutex_new();
00964     }
00965     if ((!nosync && (fptr->mode & (FMODE_SYNC|FMODE_TTY))) ||
00966         (fptr->wbuf.ptr && fptr->wbuf.capa <= fptr->wbuf.len + len)) {
00967         struct binwrite_arg arg;
00968 
00969         /* xxx: use writev to avoid double write if available */
00970         if (fptr->wbuf.len && fptr->wbuf.len+len <= fptr->wbuf.capa) {
00971             if (fptr->wbuf.capa < fptr->wbuf.off+fptr->wbuf.len+len) {
00972                 MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
00973                 fptr->wbuf.off = 0;
00974             }
00975             MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr+offset, char, len);
00976             fptr->wbuf.len += (int)len;
00977             n = 0;
00978         }
00979         if (io_fflush(fptr) < 0)
00980             return -1L;
00981         if (n == 0)
00982             return len;
00983         /* avoid context switch between "a" and "\n" in STDERR.puts "a".
00984            [ruby-dev:25080] */
00985         if (fptr->stdio_file != stderr && !rb_thread_fd_writable(fptr->fd)) {
00986             rb_io_check_closed(fptr);
00987         }
00988         arg.fptr = fptr;
00989         arg.str = str;
00990       retry:
00991         arg.ptr = ptr + offset;
00992         arg.length = n;
00993         if (fptr->write_lock) {
00994             r = rb_mutex_synchronize(fptr->write_lock, io_binwrite_string, (VALUE)&arg);
00995         }
00996         else {
00997             long l = io_writable_length(fptr, n);
00998             r = rb_write_internal(fptr->fd, ptr+offset, l);
00999         }
01000         /* xxx: other threads may modify given string. */
01001         if (r == n) return len;
01002         if (0 <= r) {
01003             offset += r;
01004             n -= r;
01005             errno = EAGAIN;
01006         }
01007         if (rb_io_wait_writable(fptr->fd)) {
01008             rb_io_check_closed(fptr);
01009             if (offset < len)
01010                 goto retry;
01011         }
01012         return -1L;
01013     }
01014 
01015     if (fptr->wbuf.off) {
01016         if (fptr->wbuf.len)
01017             MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
01018         fptr->wbuf.off = 0;
01019     }
01020     MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr+offset, char, len);
01021     fptr->wbuf.len += (int)len;
01022     return len;
01023 }
01024 
01025 # define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \
01026                              (fmode & FMODE_TEXTMODE) ? (c) : (a))
01027 static VALUE
01028 do_writeconv(VALUE str, rb_io_t *fptr)
01029 {
01030     if (NEED_WRITECONV(fptr)) {
01031         VALUE common_encoding = Qnil;
01032         SET_BINARY_MODE(fptr);
01033 
01034         make_writeconv(fptr);
01035 
01036         if (fptr->writeconv) {
01037 #define fmode (fptr->mode)
01038             if (!NIL_P(fptr->writeconv_asciicompat))
01039                 common_encoding = fptr->writeconv_asciicompat;
01040             else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1) && !rb_enc_asciicompat(rb_enc_get(str))) {
01041                 rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
01042                          rb_enc_name(rb_enc_get(str)));
01043             }
01044 #undef fmode
01045         }
01046         else {
01047             if (fptr->encs.enc2)
01048                 common_encoding = rb_enc_from_encoding(fptr->encs.enc2);
01049             else if (fptr->encs.enc != rb_ascii8bit_encoding())
01050                 common_encoding = rb_enc_from_encoding(fptr->encs.enc);
01051         }
01052 
01053         if (!NIL_P(common_encoding)) {
01054             str = rb_str_encode(str, common_encoding,
01055                 fptr->writeconv_pre_ecflags, fptr->writeconv_pre_ecopts);
01056         }
01057 
01058         if (fptr->writeconv) {
01059             str = rb_econv_str_convert(fptr->writeconv, str, ECONV_PARTIAL_INPUT);
01060         }
01061     }
01062 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
01063 #define fmode (fptr->mode)
01064     else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1)) {
01065         if ((fptr->mode & FMODE_READABLE) &&
01066             !(fptr->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
01067             setmode(fptr->fd, O_BINARY);
01068         }
01069         else {
01070             setmode(fptr->fd, O_TEXT);
01071         }
01072         if (!rb_enc_asciicompat(rb_enc_get(str))) {
01073             rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
01074             rb_enc_name(rb_enc_get(str)));
01075         }
01076     }
01077 #undef fmode
01078 #endif
01079     return str;
01080 }
01081 
01082 static long
01083 io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
01084 {
01085 #ifdef _WIN32
01086     if (fptr->mode & FMODE_TTY) {
01087         long len = rb_w32_write_console(str, fptr->fd);
01088         if (len > 0) return len;
01089     }
01090 #endif
01091     str = do_writeconv(str, fptr);
01092     return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
01093                        fptr, nosync);
01094 }
01095 
01096 ssize_t
01097 rb_io_bufwrite(VALUE io, const void *buf, size_t size)
01098 {
01099     rb_io_t *fptr;
01100 
01101     GetOpenFile(io, fptr);
01102     rb_io_check_writable(fptr);
01103     return (ssize_t)io_binwrite(0, buf, (long)size, fptr, 0);
01104 }
01105 
01106 static VALUE
01107 io_write(VALUE io, VALUE str, int nosync)
01108 {
01109     rb_io_t *fptr;
01110     long n;
01111     VALUE tmp;
01112 
01113     rb_secure(4);
01114     io = GetWriteIO(io);
01115     str = rb_obj_as_string(str);
01116     tmp = rb_io_check_io(io);
01117     if (NIL_P(tmp)) {
01118         /* port is not IO, call write method for it. */
01119         return rb_funcall(io, id_write, 1, str);
01120     }
01121     io = tmp;
01122     if (RSTRING_LEN(str) == 0) return INT2FIX(0);
01123 
01124     GetOpenFile(io, fptr);
01125     rb_io_check_writable(fptr);
01126 
01127     n = io_fwrite(str, fptr, nosync);
01128     if (n == -1L) rb_sys_fail_path(fptr->pathv);
01129 
01130     return LONG2FIX(n);
01131 }
01132 
01133 /*
01134  *  call-seq:
01135  *     ios.write(string)    -> integer
01136  *
01137  *  Writes the given string to <em>ios</em>. The stream must be opened
01138  *  for writing. If the argument is not a string, it will be converted
01139  *  to a string using <code>to_s</code>. Returns the number of bytes
01140  *  written.
01141  *
01142  *     count = $stdout.write("This is a test\n")
01143  *     puts "That was #{count} bytes of data"
01144  *
01145  *  <em>produces:</em>
01146  *
01147  *     This is a test
01148  *     That was 15 bytes of data
01149  */
01150 
01151 static VALUE
01152 io_write_m(VALUE io, VALUE str)
01153 {
01154     return io_write(io, str, 0);
01155 }
01156 
01157 VALUE
01158 rb_io_write(VALUE io, VALUE str)
01159 {
01160     return rb_funcall(io, id_write, 1, str);
01161 }
01162 
01163 /*
01164  *  call-seq:
01165  *     ios << obj     -> ios
01166  *
01167  *  String Output---Writes <i>obj</i> to <em>ios</em>.
01168  *  <i>obj</i> will be converted to a string using
01169  *  <code>to_s</code>.
01170  *
01171  *     $stdout << "Hello " << "world!\n"
01172  *
01173  *  <em>produces:</em>
01174  *
01175  *     Hello world!
01176  */
01177 
01178 
01179 VALUE
01180 rb_io_addstr(VALUE io, VALUE str)
01181 {
01182     rb_io_write(io, str);
01183     return io;
01184 }
01185 
01186 /*
01187  *  call-seq:
01188  *     ios.flush    -> ios
01189  *
01190  *  Flushes any buffered data within <em>ios</em> to the underlying
01191  *  operating system (note that this is Ruby internal buffering only;
01192  *  the OS may buffer the data as well).
01193  *
01194  *     $stdout.print "no newline"
01195  *     $stdout.flush
01196  *
01197  *  <em>produces:</em>
01198  *
01199  *     no newline
01200  */
01201 
01202 VALUE
01203 rb_io_flush(VALUE io)
01204 {
01205     rb_io_t *fptr;
01206 
01207     if (TYPE(io) != T_FILE) {
01208         return rb_funcall(io, id_flush, 0);
01209     }
01210 
01211     io = GetWriteIO(io);
01212     GetOpenFile(io, fptr);
01213 
01214     if (fptr->mode & FMODE_WRITABLE) {
01215         if (io_fflush(fptr) < 0)
01216             rb_sys_fail(0);
01217 #ifdef _WIN32
01218         if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) == FILE_TYPE_DISK) {
01219             fsync(fptr->fd);
01220         }
01221 #endif
01222     }
01223     if (fptr->mode & FMODE_READABLE) {
01224         io_unread(fptr);
01225     }
01226 
01227     return io;
01228 }
01229 
01230 /*
01231  *  call-seq:
01232  *     ios.pos     -> integer
01233  *     ios.tell    -> integer
01234  *
01235  *  Returns the current offset (in bytes) of <em>ios</em>.
01236  *
01237  *     f = File.new("testfile")
01238  *     f.pos    #=> 0
01239  *     f.gets   #=> "This is line one\n"
01240  *     f.pos    #=> 17
01241  */
01242 
01243 static VALUE
01244 rb_io_tell(VALUE io)
01245 {
01246     rb_io_t *fptr;
01247     off_t pos;
01248 
01249     GetOpenFile(io, fptr);
01250     pos = io_tell(fptr);
01251     if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
01252     pos -= fptr->rbuf.len;
01253     return OFFT2NUM(pos);
01254 }
01255 
01256 static VALUE
01257 rb_io_seek(VALUE io, VALUE offset, int whence)
01258 {
01259     rb_io_t *fptr;
01260     off_t pos;
01261 
01262     pos = NUM2OFFT(offset);
01263     GetOpenFile(io, fptr);
01264     pos = io_seek(fptr, pos, whence);
01265     if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
01266 
01267     return INT2FIX(0);
01268 }
01269 
01270 /*
01271  *  call-seq:
01272  *     ios.seek(amount, whence=IO::SEEK_SET)  ->  0
01273  *
01274  *  Seeks to a given offset <i>anInteger</i> in the stream according to
01275  *  the value of <i>whence</i>:
01276  *
01277  *    IO::SEEK_CUR  | Seeks to _amount_ plus current position
01278  *    --------------+----------------------------------------------------
01279  *    IO::SEEK_END  | Seeks to _amount_ plus end of stream (you probably
01280  *                  | want a negative value for _amount_)
01281  *    --------------+----------------------------------------------------
01282  *    IO::SEEK_SET  | Seeks to the absolute location given by _amount_
01283  *
01284  *  Example:
01285  *
01286  *     f = File.new("testfile")
01287  *     f.seek(-13, IO::SEEK_END)   #=> 0
01288  *     f.readline                  #=> "And so on...\n"
01289  */
01290 
01291 static VALUE
01292 rb_io_seek_m(int argc, VALUE *argv, VALUE io)
01293 {
01294     VALUE offset, ptrname;
01295     int whence = SEEK_SET;
01296 
01297     if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
01298         whence = NUM2INT(ptrname);
01299     }
01300 
01301     return rb_io_seek(io, offset, whence);
01302 }
01303 
01304 /*
01305  *  call-seq:
01306  *     ios.pos = integer    -> integer
01307  *
01308  *  Seeks to the given position (in bytes) in <em>ios</em>.
01309  *
01310  *     f = File.new("testfile")
01311  *     f.pos = 17
01312  *     f.gets   #=> "This is line two\n"
01313  */
01314 
01315 static VALUE
01316 rb_io_set_pos(VALUE io, VALUE offset)
01317 {
01318     rb_io_t *fptr;
01319     off_t pos;
01320 
01321     pos = NUM2OFFT(offset);
01322     GetOpenFile(io, fptr);
01323     pos = io_seek(fptr, pos, SEEK_SET);
01324     if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
01325 
01326     return OFFT2NUM(pos);
01327 }
01328 
01329 static void clear_readconv(rb_io_t *fptr);
01330 
01331 /*
01332  *  call-seq:
01333  *     ios.rewind    -> 0
01334  *
01335  *  Positions <em>ios</em> to the beginning of input, resetting
01336  *  <code>lineno</code> to zero.
01337  *
01338  *     f = File.new("testfile")
01339  *     f.readline   #=> "This is line one\n"
01340  *     f.rewind     #=> 0
01341  *     f.lineno     #=> 0
01342  *     f.readline   #=> "This is line one\n"
01343  *
01344  *  Note that it cannot be used with streams such as pipes, ttys, and sockets.
01345  */
01346 
01347 static VALUE
01348 rb_io_rewind(VALUE io)
01349 {
01350     rb_io_t *fptr;
01351 
01352     GetOpenFile(io, fptr);
01353     if (io_seek(fptr, 0L, 0) < 0 && errno) rb_sys_fail_path(fptr->pathv);
01354 #ifdef _WIN32
01355     if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) == FILE_TYPE_DISK) {
01356         fsync(fptr->fd);
01357     }
01358 #endif
01359     if (io == ARGF.current_file) {
01360         ARGF.lineno -= fptr->lineno;
01361     }
01362     fptr->lineno = 0;
01363     if (fptr->readconv) {
01364         clear_readconv(fptr);
01365     }
01366 
01367     return INT2FIX(0);
01368 }
01369 
01370 static int
01371 io_fillbuf(rb_io_t *fptr)
01372 {
01373     ssize_t r;
01374 
01375     if (fptr->rbuf.ptr == NULL) {
01376         fptr->rbuf.off = 0;
01377         fptr->rbuf.len = 0;
01378         fptr->rbuf.capa = IO_RBUF_CAPA_FOR(fptr);
01379         fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
01380 #ifdef _WIN32
01381         fptr->rbuf.capa--;
01382 #endif
01383     }
01384     if (fptr->rbuf.len == 0) {
01385       retry:
01386         {
01387             r = rb_read_internal(fptr->fd, fptr->rbuf.ptr, fptr->rbuf.capa);
01388         }
01389         if (r < 0) {
01390             if (rb_io_wait_readable(fptr->fd))
01391                 goto retry;
01392             rb_sys_fail_path(fptr->pathv);
01393         }
01394         fptr->rbuf.off = 0;
01395         fptr->rbuf.len = (int)r; /* r should be <= rbuf_capa */
01396         if (r == 0)
01397             return -1; /* EOF */
01398     }
01399     return 0;
01400 }
01401 
01402 /*
01403  *  call-seq:
01404  *     ios.eof     -> true or false
01405  *     ios.eof?    -> true or false
01406  *
01407  *  Returns true if <em>ios</em> is at end of file that means
01408  *  there are no more data to read.
01409  *  The stream must be opened for reading or an <code>IOError</code> will be
01410  *  raised.
01411  *
01412  *     f = File.new("testfile")
01413  *     dummy = f.readlines
01414  *     f.eof   #=> true
01415  *
01416  *  If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
01417  *  blocks until the other end sends some data or closes it.
01418  *
01419  *     r, w = IO.pipe
01420  *     Thread.new { sleep 1; w.close }
01421  *     r.eof?  #=> true after 1 second blocking
01422  *
01423  *     r, w = IO.pipe
01424  *     Thread.new { sleep 1; w.puts "a" }
01425  *     r.eof?  #=> false after 1 second blocking
01426  *
01427  *     r, w = IO.pipe
01428  *     r.eof?  # blocks forever
01429  *
01430  *  Note that <code>IO#eof?</code> reads data to the input byte buffer.
01431  *  So <code>IO#sysread</code> may not behave as you intend with
01432  *  <code>IO#eof?</code>, unless you call <code>IO#rewind</code>
01433  *  first (which is not available for some streams).
01434  */
01435 
01436 VALUE
01437 rb_io_eof(VALUE io)
01438 {
01439     rb_io_t *fptr;
01440 
01441     GetOpenFile(io, fptr);
01442     rb_io_check_char_readable(fptr);
01443 
01444     if (READ_CHAR_PENDING(fptr)) return Qfalse;
01445     if (READ_DATA_PENDING(fptr)) return Qfalse;
01446     READ_CHECK(fptr);
01447 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
01448     if (!NEED_READCONV(fptr) && NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
01449         return eof(fptr->fd) ? Qtrue : Qfalse;
01450     }
01451 #endif
01452     if (io_fillbuf(fptr) < 0) {
01453         return Qtrue;
01454     }
01455     return Qfalse;
01456 }
01457 
01458 /*
01459  *  call-seq:
01460  *     ios.sync    -> true or false
01461  *
01462  *  Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
01463  *  true, all output is immediately flushed to the underlying operating
01464  *  system and is not buffered by Ruby internally. See also
01465  *  <code>IO#fsync</code>.
01466  *
01467  *     f = File.new("testfile")
01468  *     f.sync   #=> false
01469  */
01470 
01471 static VALUE
01472 rb_io_sync(VALUE io)
01473 {
01474     rb_io_t *fptr;
01475 
01476     io = GetWriteIO(io);
01477     GetOpenFile(io, fptr);
01478     return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
01479 }
01480 
01481 /*
01482  *  call-seq:
01483  *     ios.sync = boolean   -> boolean
01484  *
01485  *  Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
01486  *  When sync mode is true, all output is immediately flushed to the
01487  *  underlying operating system and is not buffered internally. Returns
01488  *  the new state. See also <code>IO#fsync</code>.
01489  *
01490  *     f = File.new("testfile")
01491  *     f.sync = true
01492  *
01493  *  <em>(produces no output)</em>
01494  */
01495 
01496 static VALUE
01497 rb_io_set_sync(VALUE io, VALUE sync)
01498 {
01499     rb_io_t *fptr;
01500 
01501     io = GetWriteIO(io);
01502     GetOpenFile(io, fptr);
01503     if (RTEST(sync)) {
01504         fptr->mode |= FMODE_SYNC;
01505     }
01506     else {
01507         fptr->mode &= ~FMODE_SYNC;
01508     }
01509     return sync;
01510 }
01511 
01512 #ifdef HAVE_FSYNC
01513 static VALUE nogvl_fsync(void *ptr)
01514 {
01515     rb_io_t *fptr = ptr;
01516 
01517     return (VALUE)fsync(fptr->fd);
01518 }
01519 
01520 /*
01521  *  call-seq:
01522  *     ios.fsync   -> 0 or nil
01523  *
01524  *  Immediately writes all buffered data in <em>ios</em> to disk.
01525  *  Note that <code>fsync</code> differs from
01526  *  using <code>IO#sync=</code>. The latter ensures that data is flushed
01527  *  from Ruby's buffers, but doesn't not guarantee that the underlying
01528  *  operating system actually writes it to disk.
01529  *
01530  *  <code>NotImplementedError</code> is raised
01531  *  if the underlying operating system does not support <em>fsync(2)</em>.
01532  */
01533 
01534 static VALUE
01535 rb_io_fsync(VALUE io)
01536 {
01537     rb_io_t *fptr;
01538 
01539     io = GetWriteIO(io);
01540     GetOpenFile(io, fptr);
01541 
01542     if (io_fflush(fptr) < 0)
01543         rb_sys_fail(0);
01544 #ifndef _WIN32  /* already called in io_fflush() */
01545     if ((int)rb_thread_io_blocking_region(nogvl_fsync, fptr, fptr->fd) < 0)
01546         rb_sys_fail_path(fptr->pathv);
01547 #endif
01548     return INT2FIX(0);
01549 }
01550 #else
01551 #define rb_io_fsync rb_f_notimplement
01552 #endif
01553 
01554 #ifdef HAVE_FDATASYNC
01555 static VALUE nogvl_fdatasync(void *ptr)
01556 {
01557     rb_io_t *fptr = ptr;
01558 
01559     return (VALUE)fdatasync(fptr->fd);
01560 }
01561 
01562 /*
01563  *  call-seq:
01564  *     ios.fdatasync   -> 0 or nil
01565  *
01566  *  Immediately writes all buffered data in <em>ios</em> to disk.
01567  *
01568  *  If the underlying operating system does not support <em>fdatasync(2)</em>,
01569  *  <code>IO#fsync</code> is called instead (which might raise a
01570  *  <code>NotImplementedError</code>).
01571  */
01572 
01573 static VALUE
01574 rb_io_fdatasync(VALUE io)
01575 {
01576     rb_io_t *fptr;
01577 
01578     io = GetWriteIO(io);
01579     GetOpenFile(io, fptr);
01580 
01581     if (io_fflush(fptr) < 0)
01582         rb_sys_fail(0);
01583 
01584     if ((int)rb_thread_io_blocking_region(nogvl_fdatasync, fptr, fptr->fd) == 0)
01585         return INT2FIX(0);
01586 
01587     /* fall back */
01588     return rb_io_fsync(io);
01589 }
01590 #else
01591 #define rb_io_fdatasync rb_io_fsync
01592 #endif
01593 
01594 /*
01595  *  call-seq:
01596  *     ios.fileno    -> fixnum
01597  *     ios.to_i      -> fixnum
01598  *
01599  *  Returns an integer representing the numeric file descriptor for
01600  *  <em>ios</em>.
01601  *
01602  *     $stdin.fileno    #=> 0
01603  *     $stdout.fileno   #=> 1
01604  */
01605 
01606 static VALUE
01607 rb_io_fileno(VALUE io)
01608 {
01609     rb_io_t *fptr;
01610     int fd;
01611 
01612     GetOpenFile(io, fptr);
01613     fd = fptr->fd;
01614     return INT2FIX(fd);
01615 }
01616 
01617 
01618 /*
01619  *  call-seq:
01620  *     ios.pid    -> fixnum
01621  *
01622  *  Returns the process ID of a child process associated with
01623  *  <em>ios</em>. This will be set by <code>IO.popen</code>.
01624  *
01625  *     pipe = IO.popen("-")
01626  *     if pipe
01627  *       $stderr.puts "In parent, child pid is #{pipe.pid}"
01628  *     else
01629  *       $stderr.puts "In child, pid is #{$$}"
01630  *     end
01631  *
01632  *  <em>produces:</em>
01633  *
01634  *     In child, pid is 26209
01635  *     In parent, child pid is 26209
01636  */
01637 
01638 static VALUE
01639 rb_io_pid(VALUE io)
01640 {
01641     rb_io_t *fptr;
01642 
01643     GetOpenFile(io, fptr);
01644     if (!fptr->pid)
01645         return Qnil;
01646     return PIDT2NUM(fptr->pid);
01647 }
01648 
01649 
01650 /*
01651  * call-seq:
01652  *   ios.inspect   -> string
01653  *
01654  * Return a string describing this IO object.
01655  */
01656 
01657 static VALUE
01658 rb_io_inspect(VALUE obj)
01659 {
01660     rb_io_t *fptr;
01661     VALUE result;
01662     static const char closed[] = " (closed)";
01663 
01664     fptr = RFILE(rb_io_taint_check(obj))->fptr;
01665     if (!fptr) return rb_any_to_s(obj);
01666     result = rb_str_new_cstr("#<");
01667     rb_str_append(result, rb_class_name(CLASS_OF(obj)));
01668     rb_str_cat2(result, ":");
01669     if (NIL_P(fptr->pathv)) {
01670         if (fptr->fd < 0) {
01671             rb_str_cat(result, closed+1, strlen(closed)-1);
01672         }
01673         else {
01674             rb_str_catf(result, "fd %d", fptr->fd);
01675         }
01676     }
01677     else {
01678         rb_str_append(result, fptr->pathv);
01679         if (fptr->fd < 0) {
01680             rb_str_cat(result, closed, strlen(closed));
01681         }
01682     }
01683     return rb_str_cat2(result, ">");
01684 }
01685 
01686 /*
01687  *  call-seq:
01688  *     ios.to_io  ->  ios
01689  *
01690  *  Returns <em>ios</em>.
01691  */
01692 
01693 static VALUE
01694 rb_io_to_io(VALUE io)
01695 {
01696     return io;
01697 }
01698 
01699 /* reading functions */
01700 static long
01701 read_buffered_data(char *ptr, long len, rb_io_t *fptr)
01702 {
01703     int n;
01704 
01705     n = READ_DATA_PENDING_COUNT(fptr);
01706     if (n <= 0) return 0;
01707     if (n > len) n = (int)len;
01708     MEMMOVE(ptr, fptr->rbuf.ptr+fptr->rbuf.off, char, n);
01709     fptr->rbuf.off += n;
01710     fptr->rbuf.len -= n;
01711     return n;
01712 }
01713 
01714 static long
01715 io_bufread(char *ptr, long len, rb_io_t *fptr)
01716 {
01717     long offset = 0;
01718     long n = len;
01719     long c;
01720 
01721     if (READ_DATA_PENDING(fptr) == 0) {
01722         while (n > 0) {
01723           again:
01724             c = rb_read_internal(fptr->fd, ptr+offset, n);
01725             if (c == 0) break;
01726             if (c < 0) {
01727                 if (rb_io_wait_readable(fptr->fd))
01728                     goto again;
01729                 return -1;
01730             }
01731             offset += c;
01732             if ((n -= c) <= 0) break;
01733             rb_thread_wait_fd(fptr->fd);
01734         }
01735         return len - n;
01736     }
01737 
01738     while (n > 0) {
01739         c = read_buffered_data(ptr+offset, n, fptr);
01740         if (c > 0) {
01741             offset += c;
01742             if ((n -= c) <= 0) break;
01743         }
01744         rb_thread_wait_fd(fptr->fd);
01745         rb_io_check_closed(fptr);
01746         if (io_fillbuf(fptr) < 0) {
01747             break;
01748         }
01749     }
01750     return len - n;
01751 }
01752 
01753 static long
01754 io_fread(VALUE str, long offset, rb_io_t *fptr)
01755 {
01756     long len;
01757 
01758     rb_str_locktmp(str);
01759     len = io_bufread(RSTRING_PTR(str) + offset, RSTRING_LEN(str) - offset,
01760                      fptr);
01761     rb_str_unlocktmp(str);
01762     if (len < 0) rb_sys_fail_path(fptr->pathv);
01763     return len;
01764 }
01765 
01766 ssize_t
01767 rb_io_bufread(VALUE io, void *buf, size_t size)
01768 {
01769     rb_io_t *fptr;
01770 
01771     GetOpenFile(io, fptr);
01772     rb_io_check_readable(fptr);
01773     return (ssize_t)io_bufread(buf, (long)size, fptr);
01774 }
01775 
01776 #define SMALLBUF 100
01777 
01778 static long
01779 remain_size(rb_io_t *fptr)
01780 {
01781     struct stat st;
01782     off_t siz = READ_DATA_PENDING_COUNT(fptr);
01783     off_t pos;
01784 
01785     if (fstat(fptr->fd, &st) == 0  && S_ISREG(st.st_mode)
01786 #if defined(__BEOS__) || defined(__HAIKU__)
01787         && (st.st_dev > 3)
01788 #endif
01789         )
01790     {
01791         if (io_fflush(fptr) < 0)
01792             rb_sys_fail(0);
01793         pos = lseek(fptr->fd, 0, SEEK_CUR);
01794         if (st.st_size >= pos && pos >= 0) {
01795             siz += st.st_size - pos;
01796             if (siz > LONG_MAX) {
01797                 rb_raise(rb_eIOError, "file too big for single read");
01798             }
01799         }
01800     }
01801     else {
01802         siz += BUFSIZ;
01803     }
01804     return (long)siz;
01805 }
01806 
01807 static VALUE
01808 io_enc_str(VALUE str, rb_io_t *fptr)
01809 {
01810     OBJ_TAINT(str);
01811     rb_enc_associate(str, io_read_encoding(fptr));
01812     return str;
01813 }
01814 
01815 static void
01816 make_readconv(rb_io_t *fptr, int size)
01817 {
01818     if (!fptr->readconv) {
01819         int ecflags;
01820         VALUE ecopts;
01821         const char *sname, *dname;
01822         ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_WRITE_MASK;
01823         ecopts = fptr->encs.ecopts;
01824         if (fptr->encs.enc2) {
01825             sname = rb_enc_name(fptr->encs.enc2);
01826             dname = rb_enc_name(fptr->encs.enc);
01827         }
01828         else {
01829             sname = dname = "";
01830         }
01831         fptr->readconv = rb_econv_open_opts(sname, dname, ecflags, ecopts);
01832         if (!fptr->readconv)
01833             rb_exc_raise(rb_econv_open_exc(sname, dname, ecflags));
01834         fptr->cbuf.off = 0;
01835         fptr->cbuf.len = 0;
01836         if (size < IO_CBUF_CAPA_MIN) size = IO_CBUF_CAPA_MIN;
01837         fptr->cbuf.capa = size;
01838         fptr->cbuf.ptr = ALLOC_N(char, fptr->cbuf.capa);
01839     }
01840 }
01841 
01842 #define MORE_CHAR_SUSPENDED Qtrue
01843 #define MORE_CHAR_FINISHED Qnil
01844 static VALUE
01845 fill_cbuf(rb_io_t *fptr, int ec_flags)
01846 {
01847     const unsigned char *ss, *sp, *se;
01848     unsigned char *ds, *dp, *de;
01849     rb_econv_result_t res;
01850     int putbackable;
01851     int cbuf_len0;
01852     VALUE exc;
01853 
01854     ec_flags |= ECONV_PARTIAL_INPUT;
01855 
01856     if (fptr->cbuf.len == fptr->cbuf.capa)
01857         return MORE_CHAR_SUSPENDED; /* cbuf full */
01858     if (fptr->cbuf.len == 0)
01859         fptr->cbuf.off = 0;
01860     else if (fptr->cbuf.off + fptr->cbuf.len == fptr->cbuf.capa) {
01861         memmove(fptr->cbuf.ptr, fptr->cbuf.ptr+fptr->cbuf.off, fptr->cbuf.len);
01862         fptr->cbuf.off = 0;
01863     }
01864 
01865     cbuf_len0 = fptr->cbuf.len;
01866 
01867     while (1) {
01868         ss = sp = (const unsigned char *)fptr->rbuf.ptr + fptr->rbuf.off;
01869         se = sp + fptr->rbuf.len;
01870         ds = dp = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.off + fptr->cbuf.len;
01871         de = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.capa;
01872         res = rb_econv_convert(fptr->readconv, &sp, se, &dp, de, ec_flags);
01873         fptr->rbuf.off += (int)(sp - ss);
01874         fptr->rbuf.len -= (int)(sp - ss);
01875         fptr->cbuf.len += (int)(dp - ds);
01876 
01877         putbackable = rb_econv_putbackable(fptr->readconv);
01878         if (putbackable) {
01879             rb_econv_putback(fptr->readconv, (unsigned char *)fptr->rbuf.ptr + fptr->rbuf.off - putbackable, putbackable);
01880             fptr->rbuf.off -= putbackable;
01881             fptr->rbuf.len += putbackable;
01882         }
01883 
01884         exc = rb_econv_make_exception(fptr->readconv);
01885         if (!NIL_P(exc))
01886             return exc;
01887 
01888         if (cbuf_len0 != fptr->cbuf.len)
01889             return MORE_CHAR_SUSPENDED;
01890 
01891         if (res == econv_finished) {
01892             return MORE_CHAR_FINISHED;
01893         }
01894 
01895         if (res == econv_source_buffer_empty) {
01896             if (fptr->rbuf.len == 0) {
01897                 READ_CHECK(fptr);
01898                 if (io_fillbuf(fptr) == -1) {
01899                     if (!fptr->readconv) {
01900                         return MORE_CHAR_FINISHED;
01901                     }
01902                     ds = dp = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.off + fptr->cbuf.len;
01903                     de = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.capa;
01904                     res = rb_econv_convert(fptr->readconv, NULL, NULL, &dp, de, 0);
01905                     fptr->cbuf.len += (int)(dp - ds);
01906                     rb_econv_check_error(fptr->readconv);
01907                     break;
01908                 }
01909             }
01910         }
01911     }
01912     if (cbuf_len0 != fptr->cbuf.len)
01913         return MORE_CHAR_SUSPENDED;
01914 
01915     return MORE_CHAR_FINISHED;
01916 }
01917 
01918 static VALUE
01919 more_char(rb_io_t *fptr)
01920 {
01921     VALUE v;
01922     v = fill_cbuf(fptr, ECONV_AFTER_OUTPUT);
01923     if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED)
01924         rb_exc_raise(v);
01925     return v;
01926 }
01927 
01928 static VALUE
01929 io_shift_cbuf(rb_io_t *fptr, int len, VALUE *strp)
01930 {
01931     VALUE str = Qnil;
01932     if (strp) {
01933         str = *strp;
01934         if (NIL_P(str)) {
01935             *strp = str = rb_str_new(fptr->cbuf.ptr+fptr->cbuf.off, len);
01936         }
01937         else {
01938             rb_str_cat(str, fptr->cbuf.ptr+fptr->cbuf.off, len);
01939         }
01940         OBJ_TAINT(str);
01941         rb_enc_associate(str, fptr->encs.enc);
01942     }
01943     fptr->cbuf.off += len;
01944     fptr->cbuf.len -= len;
01945     /* xxx: set coderange */
01946     if (fptr->cbuf.len == 0)
01947         fptr->cbuf.off = 0;
01948     else if (fptr->cbuf.capa/2 < fptr->cbuf.off) {
01949         memmove(fptr->cbuf.ptr, fptr->cbuf.ptr+fptr->cbuf.off, fptr->cbuf.len);
01950         fptr->cbuf.off = 0;
01951     }
01952     return str;
01953 }
01954 
01955 static void
01956 io_setstrbuf(VALUE *str,long len)
01957 {
01958 #ifdef _WIN32
01959     if (NIL_P(*str)) {
01960         *str = rb_str_new(0, len+1);
01961         rb_str_set_len(*str,len);
01962     }
01963     else {
01964         StringValue(*str);
01965         rb_str_modify(*str);
01966         rb_str_resize(*str, len+1);
01967         rb_str_set_len(*str,len);
01968     }
01969 #else
01970     if (NIL_P(*str)) {
01971         *str = rb_str_new(0, len);
01972     }
01973     else {
01974         StringValue(*str);
01975         rb_str_modify(*str);
01976         rb_str_resize(*str, len);
01977     }
01978 #endif
01979 }
01980 
01981 static VALUE
01982 read_all(rb_io_t *fptr, long siz, VALUE str)
01983 {
01984     long bytes;
01985     long n;
01986     long pos;
01987     rb_encoding *enc;
01988     int cr;
01989 
01990     if (NEED_READCONV(fptr)) {
01991         SET_BINARY_MODE(fptr);
01992         io_setstrbuf(&str,0);
01993         make_readconv(fptr, 0);
01994         while (1) {
01995             VALUE v;
01996             if (fptr->cbuf.len) {
01997                 io_shift_cbuf(fptr, fptr->cbuf.len, &str);
01998             }
01999             v = fill_cbuf(fptr, 0);
02000             if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED) {
02001                 if (fptr->cbuf.len) {
02002                     io_shift_cbuf(fptr, fptr->cbuf.len, &str);
02003                 }
02004                 rb_exc_raise(v);
02005             }
02006             if (v == MORE_CHAR_FINISHED) {
02007                 clear_readconv(fptr);
02008                 return io_enc_str(str, fptr);
02009             }
02010         }
02011     }
02012 
02013     NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
02014     bytes = 0;
02015     pos = 0;
02016 
02017     enc = io_read_encoding(fptr);
02018     cr = 0;
02019 
02020     if (siz == 0) siz = BUFSIZ;
02021     io_setstrbuf(&str,siz);
02022     for (;;) {
02023         READ_CHECK(fptr);
02024         n = io_fread(str, bytes, fptr);
02025         if (n == 0 && bytes == 0) {
02026             break;
02027         }
02028         bytes += n;
02029         if (cr != ENC_CODERANGE_BROKEN)
02030             pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
02031         if (bytes < siz) break;
02032         siz += BUFSIZ;
02033         rb_str_resize(str, siz);
02034     }
02035     if (bytes != siz) rb_str_resize(str, bytes);
02036     str = io_enc_str(str, fptr);
02037     ENC_CODERANGE_SET(str, cr);
02038     return str;
02039 }
02040 
02041 void
02042 rb_io_set_nonblock(rb_io_t *fptr)
02043 {
02044     int oflags;
02045 #ifdef F_GETFL
02046     oflags = fcntl(fptr->fd, F_GETFL);
02047     if (oflags == -1) {
02048         rb_sys_fail_path(fptr->pathv);
02049     }
02050 #else
02051     oflags = 0;
02052 #endif
02053     if ((oflags & O_NONBLOCK) == 0) {
02054         oflags |= O_NONBLOCK;
02055         if (fcntl(fptr->fd, F_SETFL, oflags) == -1) {
02056             rb_sys_fail_path(fptr->pathv);
02057         }
02058     }
02059 }
02060 
02061 static VALUE
02062 io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
02063 {
02064     rb_io_t *fptr;
02065     VALUE length, str;
02066     long n, len;
02067 
02068     rb_scan_args(argc, argv, "11", &length, &str);
02069 
02070     if ((len = NUM2LONG(length)) < 0) {
02071         rb_raise(rb_eArgError, "negative length %ld given", len);
02072     }
02073 
02074     io_setstrbuf(&str,len);
02075     OBJ_TAINT(str);
02076 
02077     GetOpenFile(io, fptr);
02078     rb_io_check_byte_readable(fptr);
02079 
02080     if (len == 0)
02081         return str;
02082 
02083     if (!nonblock)
02084         READ_CHECK(fptr);
02085     n = read_buffered_data(RSTRING_PTR(str), len, fptr);
02086     if (n <= 0) {
02087       again:
02088         if (nonblock) {
02089             rb_io_set_nonblock(fptr);
02090         }
02091         rb_str_locktmp(str);
02092         n = rb_read_internal(fptr->fd, RSTRING_PTR(str), len);
02093         rb_str_unlocktmp(str);
02094         if (n < 0) {
02095             if (!nonblock && rb_io_wait_readable(fptr->fd))
02096                 goto again;
02097             if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
02098                 rb_mod_sys_fail(rb_mWaitReadable, "read would block");
02099             rb_sys_fail_path(fptr->pathv);
02100         }
02101     }
02102     rb_str_resize(str, n);
02103 
02104     if (n == 0)
02105         return Qnil;
02106     else
02107         return str;
02108 }
02109 
02110 /*
02111  *  call-seq:
02112  *     ios.readpartial(maxlen)              -> string
02113  *     ios.readpartial(maxlen, outbuf)      -> outbuf
02114  *
02115  *  Reads at most <i>maxlen</i> bytes from the I/O stream.
02116  *  It blocks only if <em>ios</em> has no data immediately available.
02117  *  It doesn't block if some data available.
02118  *  If the optional <i>outbuf</i> argument is present,
02119  *  it must reference a String, which will receive the data.
02120  *  It raises <code>EOFError</code> on end of file.
02121  *
02122  *  readpartial is designed for streams such as pipe, socket, tty, etc.
02123  *  It blocks only when no data immediately available.
02124  *  This means that it blocks only when following all conditions hold.
02125  *  * the byte buffer in the IO object is empty.
02126  *  * the content of the stream is empty.
02127  *  * the stream is not reached to EOF.
02128  *
02129  *  When readpartial blocks, it waits data or EOF on the stream.
02130  *  If some data is reached, readpartial returns with the data.
02131  *  If EOF is reached, readpartial raises EOFError.
02132  *
02133  *  When readpartial doesn't blocks, it returns or raises immediately.
02134  *  If the byte buffer is not empty, it returns the data in the buffer.
02135  *  Otherwise if the stream has some content,
02136  *  it returns the data in the stream.
02137  *  Otherwise if the stream is reached to EOF, it raises EOFError.
02138  *
02139  *     r, w = IO.pipe           #               buffer          pipe content
02140  *     w << "abc"               #               ""              "abc".
02141  *     r.readpartial(4096)      #=> "abc"       ""              ""
02142  *     r.readpartial(4096)      # blocks because buffer and pipe is empty.
02143  *
02144  *     r, w = IO.pipe           #               buffer          pipe content
02145  *     w << "abc"               #               ""              "abc"
02146  *     w.close                  #               ""              "abc" EOF
02147  *     r.readpartial(4096)      #=> "abc"       ""              EOF
02148  *     r.readpartial(4096)      # raises EOFError
02149  *
02150  *     r, w = IO.pipe           #               buffer          pipe content
02151  *     w << "abc\ndef\n"        #               ""              "abc\ndef\n"
02152  *     r.gets                   #=> "abc\n"     "def\n"         ""
02153  *     w << "ghi\n"             #               "def\n"         "ghi\n"
02154  *     r.readpartial(4096)      #=> "def\n"     ""              "ghi\n"
02155  *     r.readpartial(4096)      #=> "ghi\n"     ""              ""
02156  *
02157  *  Note that readpartial behaves similar to sysread.
02158  *  The differences are:
02159  *  * If the byte buffer is not empty, read from the byte buffer instead of "sysread for buffered IO (IOError)".
02160  *  * It doesn't cause Errno::EWOULDBLOCK and Errno::EINTR.  When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retry the system call.
02161  *
02162  *  The later means that readpartial is nonblocking-flag insensitive.
02163  *  It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking mode.
02164  *
02165  */
02166 
02167 static VALUE
02168 io_readpartial(int argc, VALUE *argv, VALUE io)
02169 {
02170     VALUE ret;
02171 
02172     ret = io_getpartial(argc, argv, io, 0);
02173     if (NIL_P(ret))
02174         rb_eof_error();
02175     else
02176         return ret;
02177 }
02178 
02179 /*
02180  *  call-seq:
02181  *     ios.read_nonblock(maxlen)              -> string
02182  *     ios.read_nonblock(maxlen, outbuf)      -> outbuf
02183  *
02184  *  Reads at most <i>maxlen</i> bytes from <em>ios</em> using
02185  *  the read(2) system call after O_NONBLOCK is set for
02186  *  the underlying file descriptor.
02187  *
02188  *  If the optional <i>outbuf</i> argument is present,
02189  *  it must reference a String, which will receive the data.
02190  *
02191  *  read_nonblock just calls the read(2) system call.
02192  *  It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc.
02193  *  The caller should care such errors.
02194  *
02195  *  If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
02196  *  it is extended by IO::WaitReadable.
02197  *  So IO::WaitReadable can be used to rescue the exceptions for retrying read_nonblock.
02198  *
02199  *  read_nonblock causes EOFError on EOF.
02200  *
02201  *  If the read byte buffer is not empty,
02202  *  read_nonblock reads from the buffer like readpartial.
02203  *  In this case, the read(2) system call is not called.
02204  *
02205  *  When read_nonblock raises an exception kind of IO::WaitReadable,
02206  *  read_nonblock should not be called
02207  *  until io is readable for avoiding busy loop.
02208  *  This can be done as follows.
02209  *
02210  *    # emulates blocking read (readpartial).
02211  *    begin
02212  *      result = io.read_nonblock(maxlen)
02213  *    rescue IO::WaitReadable
02214  *      IO.select([io])
02215  *      retry
02216  *    end
02217  *
02218  *  Although IO#read_nonblock doesn't raise IO::WaitWritable.
02219  *  OpenSSL::Buffering#read_nonblock can raise IO::WaitWritable.
02220  *  If IO and SSL should be used polymorphically,
02221  *  IO::WaitWritable should be rescued too.
02222  *  See the document of OpenSSL::Buffering#read_nonblock for sample code.
02223  *
02224  *  Note that this method is identical to readpartial
02225  *  except the non-blocking flag is set.
02226  */
02227 
02228 static VALUE
02229 io_read_nonblock(int argc, VALUE *argv, VALUE io)
02230 {
02231     VALUE ret;
02232 
02233     ret = io_getpartial(argc, argv, io, 1);
02234     if (NIL_P(ret))
02235         rb_eof_error();
02236     else
02237         return ret;
02238 }
02239 
02240 /*
02241  *  call-seq:
02242  *     ios.write_nonblock(string)   -> integer
02243  *
02244  *  Writes the given string to <em>ios</em> using
02245  *  the write(2) system call after O_NONBLOCK is set for
02246  *  the underlying file descriptor.
02247  *
02248  *  It returns the number of bytes written.
02249  *
02250  *  write_nonblock just calls the write(2) system call.
02251  *  It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc.
02252  *  The result may also be smaller than string.length (partial write).
02253  *  The caller should care such errors and partial write.
02254  *
02255  *  If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
02256  *  it is extended by IO::WaitWritable.
02257  *  So IO::WaitWritable can be used to rescue the exceptions for retrying write_nonblock.
02258  *
02259  *    # Creates a pipe.
02260  *    r, w = IO.pipe
02261  *
02262  *    # write_nonblock writes only 65536 bytes and return 65536.
02263  *    # (The pipe size is 65536 bytes on this environment.)
02264  *    s = "a" * 100000
02265  *    p w.write_nonblock(s)     #=> 65536
02266  *
02267  *    # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN).
02268  *    p w.write_nonblock("b")   # Resource temporarily unavailable (Errno::EAGAIN)
02269  *
02270  *  If the write buffer is not empty, it is flushed at first.
02271  *
02272  *  When write_nonblock raises an exception kind of IO::WaitWritable,
02273  *  write_nonblock should not be called
02274  *  until io is writable for avoiding busy loop.
02275  *  This can be done as follows.
02276  *
02277  *    begin
02278  *      result = io.write_nonblock(string)
02279  *    rescue IO::WaitWritable, Errno::EINTR
02280  *      IO.select(nil, [io])
02281  *      retry
02282  *    end
02283  *
02284  *  Note that this doesn't guarantee to write all data in string.
02285  *  The length written is reported as result and it should be checked later.
02286  *
02287  *  On some platforms such as Windows, write_nonblock is not supported
02288  *  according to the kind of the IO object.
02289  *  In such cases, write_nonblock raises <code>Errno::EBADF</code>.
02290  *
02291  */
02292 
02293 static VALUE
02294 rb_io_write_nonblock(VALUE io, VALUE str)
02295 {
02296     rb_io_t *fptr;
02297     long n;
02298 
02299     rb_secure(4);
02300     if (TYPE(str) != T_STRING)
02301         str = rb_obj_as_string(str);
02302 
02303     io = GetWriteIO(io);
02304     GetOpenFile(io, fptr);
02305     rb_io_check_writable(fptr);
02306 
02307     if (io_fflush(fptr) < 0)
02308         rb_sys_fail(0);
02309 
02310     rb_io_set_nonblock(fptr);
02311     n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
02312 
02313     if (n == -1) {
02314         if (errno == EWOULDBLOCK || errno == EAGAIN)
02315             rb_mod_sys_fail(rb_mWaitWritable, "write would block");
02316         rb_sys_fail_path(fptr->pathv);
02317     }
02318 
02319     return LONG2FIX(n);
02320 }
02321 
02322 /*
02323  *  call-seq:
02324  *     ios.read([length [, buffer]])    -> string, buffer, or nil
02325  *
02326  *  Reads <i>length</i> bytes from the I/O stream.
02327  *
02328  *  <i>length</i> must be a non-negative integer or <code>nil</code>.
02329  *
02330  *  If <i>length</i> is a positive integer,
02331  *  it try to read <i>length</i> bytes without any conversion (binary mode).
02332  *  It returns <code>nil</code> or a string whose length is 1 to <i>length</i> bytes.
02333  *  <code>nil</code> means it met EOF at beginning.
02334  *  The 1 to <i>length</i>-1 bytes string means it met EOF after reading the result.
02335  *  The <i>length</i> bytes string means it doesn't meet EOF.
02336  *  The resulted string is always ASCII-8BIT encoding.
02337  *
02338  *  If <i>length</i> is omitted or is <code>nil</code>,
02339  *  it reads until EOF and the encoding conversion is applied.
02340  *  It returns a string even if EOF is met at beginning.
02341  *
02342  *  If <i>length</i> is zero, it returns <code>""</code>.
02343  *
02344  *  If the optional <i>buffer</i> argument is present, it must reference
02345  *  a String, which will receive the data.
02346  *
02347  *  At end of file, it returns <code>nil</code> or <code>""</code>
02348  *  depend on <i>length</i>.
02349  *  <code><i>ios</i>.read()</code> and
02350  *  <code><i>ios</i>.read(nil)</code> returns <code>""</code>.
02351  *  <code><i>ios</i>.read(<i>positive-integer</i>)</code> returns <code>nil</code>.
02352  *
02353  *     f = File.new("testfile")
02354  *     f.read(16)   #=> "This is line one"
02355  *
02356  *     # reads whole file
02357  *     open("file") {|f|
02358  *       data = f.read # This returns a string even if the file is empty.
02359  *       ...
02360  *     }
02361  *
02362  *     # iterate over fixed length records.
02363  *     open("fixed-record-file") {|f|
02364  *       while record = f.read(256)
02365  *         ...
02366  *       end
02367  *     }
02368  *
02369  *     # iterate over variable length records.
02370  *     # record is prefixed by 32-bit length.
02371  *     open("variable-record-file") {|f|
02372  *       while len = f.read(4)
02373  *         len = len.unpack("N")[0] # 32-bit length
02374  *         record = f.read(len) # This returns a string even if len is 0.
02375  *       end
02376  *     }
02377  *
02378  *  Note that this method behaves like fread() function in C.
02379  *  If you need the behavior like read(2) system call,
02380  *  consider readpartial, read_nonblock and sysread.
02381  */
02382 
02383 static VALUE
02384 io_read(int argc, VALUE *argv, VALUE io)
02385 {
02386     rb_io_t *fptr;
02387     long n, len;
02388     VALUE length, str;
02389 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
02390     int previous_mode;
02391 #endif
02392 
02393     rb_scan_args(argc, argv, "02", &length, &str);
02394 
02395     if (NIL_P(length)) {
02396         GetOpenFile(io, fptr);
02397         rb_io_check_char_readable(fptr);
02398         return read_all(fptr, remain_size(fptr), str);
02399     }
02400     len = NUM2LONG(length);
02401     if (len < 0) {
02402         rb_raise(rb_eArgError, "negative length %ld given", len);
02403     }
02404 
02405     io_setstrbuf(&str,len);
02406 
02407     GetOpenFile(io, fptr);
02408     rb_io_check_byte_readable(fptr);
02409     if (len == 0) return str;
02410 
02411     READ_CHECK(fptr);
02412 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
02413     previous_mode = set_binary_mode_with_seek_cur(fptr);
02414 #endif
02415     n = io_fread(str, 0, fptr);
02416 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
02417     if (previous_mode == O_TEXT) {
02418         setmode(fptr->fd, O_TEXT);
02419     }
02420 #endif
02421     if (n == 0) {
02422         if (fptr->fd < 0) return Qnil;
02423         rb_str_resize(str, 0);
02424         return Qnil;
02425     }
02426     rb_str_resize(str, n);
02427     OBJ_TAINT(str);
02428 
02429     return str;
02430 }
02431 
02432 static void
02433 rscheck(const char *rsptr, long rslen, VALUE rs)
02434 {
02435     if (!rs) return;
02436     if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
02437         rb_raise(rb_eRuntimeError, "rs modified");
02438 }
02439 
02440 static int
02441 appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
02442 {
02443     VALUE str = *strp;
02444     long limit = *lp;
02445 
02446     if (NEED_READCONV(fptr)) {
02447         SET_BINARY_MODE(fptr);
02448         make_readconv(fptr, 0);
02449         do {
02450             const char *p, *e;
02451             int searchlen;
02452             if (fptr->cbuf.len) {
02453                 p = fptr->cbuf.ptr+fptr->cbuf.off;
02454                 searchlen = fptr->cbuf.len;
02455                 if (0 < limit && limit < searchlen)
02456                     searchlen = (int)limit;
02457                 e = memchr(p, delim, searchlen);
02458                 if (e) {
02459                     int len = (int)(e-p+1);
02460                     if (NIL_P(str))
02461                         *strp = str = rb_str_new(p, len);
02462                     else
02463                         rb_str_buf_cat(str, p, len);
02464                     fptr->cbuf.off += len;
02465                     fptr->cbuf.len -= len;
02466                     limit -= len;
02467                     *lp = limit;
02468                     return delim;
02469                 }
02470 
02471                 if (NIL_P(str))
02472                     *strp = str = rb_str_new(p, searchlen);
02473                 else
02474                     rb_str_buf_cat(str, p, searchlen);
02475                 fptr->cbuf.off += searchlen;
02476                 fptr->cbuf.len -= searchlen;
02477                 limit -= searchlen;
02478 
02479                 if (limit == 0) {
02480                     *lp = limit;
02481                     return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
02482                 }
02483             }
02484         } while (more_char(fptr) != MORE_CHAR_FINISHED);
02485         clear_readconv(fptr);
02486         *lp = limit;
02487         return EOF;
02488     }
02489 
02490     NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
02491     do {
02492         long pending = READ_DATA_PENDING_COUNT(fptr);
02493         if (pending > 0) {
02494             const char *p = READ_DATA_PENDING_PTR(fptr);
02495             const char *e;
02496             long last;
02497 
02498             if (limit > 0 && pending > limit) pending = limit;
02499             e = memchr(p, delim, pending);
02500             if (e) pending = e - p + 1;
02501             if (!NIL_P(str)) {
02502                 last = RSTRING_LEN(str);
02503                 rb_str_resize(str, last + pending);
02504             }
02505             else {
02506                 last = 0;
02507                 *strp = str = rb_str_buf_new(pending);
02508                 rb_str_set_len(str, pending);
02509             }
02510             read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
02511             limit -= pending;
02512             *lp = limit;
02513             if (e) return delim;
02514             if (limit == 0)
02515                 return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
02516         }
02517         READ_CHECK(fptr);
02518     } while (io_fillbuf(fptr) >= 0);
02519     *lp = limit;
02520     return EOF;
02521 }
02522 
02523 static inline int
02524 swallow(rb_io_t *fptr, int term)
02525 {
02526     if (NEED_READCONV(fptr)) {
02527         rb_encoding *enc = io_read_encoding(fptr);
02528         int needconv = rb_enc_mbminlen(enc) != 1;
02529         SET_BINARY_MODE(fptr);
02530         make_readconv(fptr, 0);
02531         do {
02532             size_t cnt;
02533             while ((cnt = READ_CHAR_PENDING_COUNT(fptr)) > 0) {
02534                 const char *p = READ_CHAR_PENDING_PTR(fptr);
02535                 int i;
02536                 if (!needconv) {
02537                     if (*p != term) return TRUE;
02538                     i = (int)cnt;
02539                     while (--i && *++p == term);
02540                 }
02541                 else {
02542                     const char *e = p + cnt;
02543                     if (rb_enc_ascget(p, e, &i, enc) != term) return TRUE;
02544                     while ((p += i) < e && rb_enc_ascget(p, e, &i, enc) == term);
02545                     i = (int)(e - p);
02546                 }
02547                 io_shift_cbuf(fptr, (int)cnt - i, NULL);
02548             }
02549         } while (more_char(fptr) != MORE_CHAR_FINISHED);
02550         return FALSE;
02551     }
02552 
02553     NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
02554     do {
02555         size_t cnt;
02556         while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
02557             char buf[1024];
02558             const char *p = READ_DATA_PENDING_PTR(fptr);
02559             int i;
02560             if (cnt > sizeof buf) cnt = sizeof buf;
02561             if (*p != term) return TRUE;
02562             i = (int)cnt;
02563             while (--i && *++p == term);
02564             if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
02565                 rb_sys_fail_path(fptr->pathv);
02566         }
02567         READ_CHECK(fptr);
02568     } while (io_fillbuf(fptr) == 0);
02569     return FALSE;
02570 }
02571 
02572 static VALUE
02573 rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, VALUE io)
02574 {
02575     VALUE str = Qnil;
02576     int len = 0;
02577     long pos = 0;
02578     int cr = 0;
02579 
02580     for (;;) {
02581         int pending = READ_DATA_PENDING_COUNT(fptr);
02582 
02583         if (pending > 0) {
02584             const char *p = READ_DATA_PENDING_PTR(fptr);
02585             const char *e;
02586 
02587             e = memchr(p, '\n', pending);
02588             if (e) {
02589                 pending = (int)(e - p + 1);
02590             }
02591             if (NIL_P(str)) {
02592                 str = rb_str_new(p, pending);
02593                 fptr->rbuf.off += pending;
02594                 fptr->rbuf.len -= pending;
02595             }
02596             else {
02597                 rb_str_resize(str, len + pending);
02598                 read_buffered_data(RSTRING_PTR(str)+len, pending, fptr);
02599             }
02600             len += pending;
02601             if (cr != ENC_CODERANGE_BROKEN)
02602                 pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
02603             if (e) break;
02604         }
02605         READ_CHECK(fptr);
02606         if (io_fillbuf(fptr) < 0) {
02607             if (NIL_P(str)) return Qnil;
02608             break;
02609         }
02610     }
02611 
02612     str = io_enc_str(str, fptr);
02613     ENC_CODERANGE_SET(str, cr);
02614     fptr->lineno++;
02615     if (io == ARGF.current_file) {
02616         ARGF.lineno++;
02617         ARGF.last_lineno = ARGF.lineno;
02618     }
02619     else {
02620         ARGF.last_lineno = fptr->lineno;
02621     }
02622 
02623     return str;
02624 }
02625 
02626 static void
02627 prepare_getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit, VALUE io)
02628 {
02629     VALUE rs = rb_rs, lim = Qnil;
02630     rb_io_t *fptr;
02631 
02632     if (argc == 1) {
02633         VALUE tmp = Qnil;
02634 
02635         if (NIL_P(argv[0]) || !NIL_P(tmp = rb_check_string_type(argv[0]))) {
02636             rs = tmp;
02637         }
02638         else {
02639             lim = argv[0];
02640         }
02641     }
02642     else if (2 <= argc) {
02643         rb_scan_args(argc, argv, "2", &rs, &lim);
02644         if (!NIL_P(rs))
02645             StringValue(rs);
02646     }
02647     if (!NIL_P(rs)) {
02648         rb_encoding *enc_rs, *enc_io;
02649 
02650         GetOpenFile(io, fptr);
02651         enc_rs = rb_enc_get(rs);
02652         enc_io = io_read_encoding(fptr);
02653         if (enc_io != enc_rs &&
02654             (rb_enc_str_coderange(rs) != ENC_CODERANGE_7BIT ||
02655              (RSTRING_LEN(rs) > 0 && !rb_enc_asciicompat(enc_io)))) {
02656             if (rs == rb_default_rs) {
02657                 rs = rb_enc_str_new(0, 0, enc_io);
02658                 rb_str_buf_cat_ascii(rs, "\n");
02659             }
02660             else {
02661                 rb_raise(rb_eArgError, "encoding mismatch: %s IO with %s RS",
02662                          rb_enc_name(enc_io),
02663                          rb_enc_name(enc_rs));
02664             }
02665         }
02666     }
02667     *rsp = rs;
02668     *limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
02669 }
02670 
02671 static VALUE
02672 rb_io_getline_1(VALUE rs, long limit, VALUE io)
02673 {
02674     VALUE str = Qnil;
02675     rb_io_t *fptr;
02676     int nolimit = 0;
02677     rb_encoding *enc;
02678 
02679     GetOpenFile(io, fptr);
02680     rb_io_check_char_readable(fptr);
02681     if (NIL_P(rs) && limit < 0) {
02682         str = read_all(fptr, 0, Qnil);
02683         if (RSTRING_LEN(str) == 0) return Qnil;
02684     }
02685     else if (limit == 0) {
02686         return rb_enc_str_new(0, 0, io_read_encoding(fptr));
02687     }
02688     else if (rs == rb_default_rs && limit < 0 && !NEED_READCONV(fptr) &&
02689              rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
02690         NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
02691         return rb_io_getline_fast(fptr, enc, io);
02692     }
02693     else {
02694         int c, newline = -1;
02695         const char *rsptr = 0;
02696         long rslen = 0;
02697         int rspara = 0;
02698         int extra_limit = 16;
02699 
02700         SET_BINARY_MODE(fptr);
02701         enc = io_read_encoding(fptr);
02702 
02703         if (!NIL_P(rs)) {
02704             rslen = RSTRING_LEN(rs);
02705             if (rslen == 0) {
02706                 rsptr = "\n\n";
02707                 rslen = 2;
02708                 rspara = 1;
02709                 swallow(fptr, '\n');
02710                 rs = 0;
02711                 if (!rb_enc_asciicompat(enc)) {
02712                     rs = rb_usascii_str_new(rsptr, rslen);
02713                     rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
02714                     OBJ_FREEZE(rs);
02715                     rsptr = RSTRING_PTR(rs);
02716                     rslen = RSTRING_LEN(rs);
02717                 }
02718             }
02719             else {
02720                 rsptr = RSTRING_PTR(rs);
02721             }
02722             newline = (unsigned char)rsptr[rslen - 1];
02723         }
02724 
02725         /* MS - Optimisation */
02726         while ((c = appendline(fptr, newline, &str, &limit)) != EOF) {
02727             const char *s, *p, *pp, *e;
02728 
02729             if (c == newline) {
02730                 if (RSTRING_LEN(str) < rslen) continue;
02731                 s = RSTRING_PTR(str);
02732                 e = s + RSTRING_LEN(str);
02733                 p = e - rslen;
02734                 pp = rb_enc_left_char_head(s, p, e, enc);
02735                 if (pp != p) continue;
02736                 if (!rspara) rscheck(rsptr, rslen, rs);
02737                 if (memcmp(p, rsptr, rslen) == 0) break;
02738             }
02739             if (limit == 0) {
02740                 s = RSTRING_PTR(str);
02741                 p = s + RSTRING_LEN(str);
02742                 pp = rb_enc_left_char_head(s, p-1, p, enc);
02743                 if (extra_limit &&
02744                     MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(pp, p, enc))) {
02745                     /* relax the limit while incomplete character.
02746                      * extra_limit limits the relax length */
02747                     limit = 1;
02748                     extra_limit--;
02749                 }
02750                 else {
02751                     nolimit = 1;
02752                     break;
02753                 }
02754             }
02755         }
02756 
02757         if (rspara) {
02758             if (c != EOF) {
02759                 swallow(fptr, '\n');
02760             }
02761         }
02762         if (!NIL_P(str))
02763             str = io_enc_str(str, fptr);
02764     }
02765 
02766     if (!NIL_P(str)) {
02767         if (!nolimit) {
02768             fptr->lineno++;
02769             if (io == ARGF.current_file) {
02770                 ARGF.lineno++;
02771                 ARGF.last_lineno = ARGF.lineno;
02772             }
02773             else {
02774                 ARGF.last_lineno = fptr->lineno;
02775             }
02776         }
02777     }
02778 
02779     return str;
02780 }
02781 
02782 static VALUE
02783 rb_io_getline(int argc, VALUE *argv, VALUE io)
02784 {
02785     VALUE rs;
02786     long limit;
02787 
02788     prepare_getline_args(argc, argv, &rs, &limit, io);
02789     return rb_io_getline_1(rs, limit, io);
02790 }
02791 
02792 VALUE
02793 rb_io_gets(VALUE io)
02794 {
02795     return rb_io_getline_1(rb_default_rs, -1, io);
02796 }
02797 
02798 /*
02799  *  call-seq:
02800  *     ios.gets(sep=$/)     -> string or nil
02801  *     ios.gets(limit)      -> string or nil
02802  *     ios.gets(sep, limit) -> string or nil
02803  *
02804  *  Reads the next ``line'' from the I/O stream; lines are separated by
02805  *  <i>sep</i>. A separator of <code>nil</code> reads the entire
02806  *  contents, and a zero-length separator reads the input a paragraph at
02807  *  a time (two successive newlines in the input separate paragraphs).
02808  *  The stream must be opened for reading or an <code>IOError</code>
02809  *  will be raised. The line read in will be returned and also assigned
02810  *  to <code>$_</code>. Returns <code>nil</code> if called at end of
02811  *  file.  If the first argument is an integer, or optional second
02812  *  argument is given, the returning string would not be longer than the
02813  *  given value in bytes.
02814  *
02815  *     File.new("testfile").gets   #=> "This is line one\n"
02816  *     $_                          #=> "This is line one\n"
02817  */
02818 
02819 static VALUE
02820 rb_io_gets_m(int argc, VALUE *argv, VALUE io)
02821 {
02822     VALUE str;
02823 
02824     str = rb_io_getline(argc, argv, io);
02825     rb_lastline_set(str);
02826 
02827     return str;
02828 }
02829 
02830 /*
02831  *  call-seq:
02832  *     ios.lineno    -> integer
02833  *
02834  *  Returns the current line number in <em>ios</em>.  The stream must be
02835  *  opened for reading. <code>lineno</code> counts the number of times
02836  *  #gets is called rather than the number of newlines encountered.  The two
02837  *  values will differ if #gets is called with a separator other than newline.
02838  *
02839  *  Methods that use <code>$/</code> like #each, #lines and #readline will
02840  *  also increment <code>lineno</code>.
02841  *
02842  *  See also the <code>$.</code> variable.
02843  *
02844  *     f = File.new("testfile")
02845  *     f.lineno   #=> 0
02846  *     f.gets     #=> "This is line one\n"
02847  *     f.lineno   #=> 1
02848  *     f.gets     #=> "This is line two\n"
02849  *     f.lineno   #=> 2
02850  */
02851 
02852 static VALUE
02853 rb_io_lineno(VALUE io)
02854 {
02855     rb_io_t *fptr;
02856 
02857     GetOpenFile(io, fptr);
02858     rb_io_check_char_readable(fptr);
02859     return INT2NUM(fptr->lineno);
02860 }
02861 
02862 /*
02863  *  call-seq:
02864  *     ios.lineno = integer    -> integer
02865  *
02866  *  Manually sets the current line number to the given value.
02867  *  <code>$.</code> is updated only on the next read.
02868  *
02869  *     f = File.new("testfile")
02870  *     f.gets                     #=> "This is line one\n"
02871  *     $.                         #=> 1
02872  *     f.lineno = 1000
02873  *     f.lineno                   #=> 1000
02874  *     $.                         #=> 1         # lineno of last read
02875  *     f.gets                     #=> "This is line two\n"
02876  *     $.                         #=> 1001      # lineno of last read
02877  */
02878 
02879 static VALUE
02880 rb_io_set_lineno(VALUE io, VALUE lineno)
02881 {
02882     rb_io_t *fptr;
02883 
02884     GetOpenFile(io, fptr);
02885     rb_io_check_char_readable(fptr);
02886     fptr->lineno = NUM2INT(lineno);
02887     return lineno;
02888 }
02889 
02890 /*
02891  *  call-seq:
02892  *     ios.readline(sep=$/)     -> string
02893  *     ios.readline(limit)      -> string
02894  *     ios.readline(sep, limit) -> string
02895  *
02896  *  Reads a line as with <code>IO#gets</code>, but raises an
02897  *  <code>EOFError</code> on end of file.
02898  */
02899 
02900 static VALUE
02901 rb_io_readline(int argc, VALUE *argv, VALUE io)
02902 {
02903     VALUE line = rb_io_gets_m(argc, argv, io);
02904 
02905     if (NIL_P(line)) {
02906         rb_eof_error();
02907     }
02908     return line;
02909 }
02910 
02911 /*
02912  *  call-seq:
02913  *     ios.readlines(sep=$/)     -> array
02914  *     ios.readlines(limit)      -> array
02915  *     ios.readlines(sep, limit) -> array
02916  *
02917  *  Reads all of the lines in <em>ios</em>, and returns them in
02918  *  <i>anArray</i>. Lines are separated by the optional <i>sep</i>. If
02919  *  <i>sep</i> is <code>nil</code>, the rest of the stream is returned
02920  *  as a single record.  If the first argument is an integer, or
02921  *  optional second argument is given, the returning string would not be
02922  *  longer than the given value in bytes. The stream must be opened for
02923  *  reading or an <code>IOError</code> will be raised.
02924  *
02925  *     f = File.new("testfile")
02926  *     f.readlines[0]   #=> "This is line one\n"
02927  */
02928 
02929 static VALUE
02930 rb_io_readlines(int argc, VALUE *argv, VALUE io)
02931 {
02932     VALUE line, ary, rs;
02933     long limit;
02934 
02935     prepare_getline_args(argc, argv, &rs, &limit, io);
02936     if (limit == 0)
02937         rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
02938     ary = rb_ary_new();
02939     while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) {
02940         rb_ary_push(ary, line);
02941     }
02942     return ary;
02943 }
02944 
02945 /*
02946  *  call-seq:
02947  *     ios.each(sep=$/) {|line| block }         -> ios
02948  *     ios.each(limit) {|line| block }          -> ios
02949  *     ios.each(sep,limit) {|line| block }      -> ios
02950  *     ios.each(...)                            -> an_enumerator
02951  *
02952  *     ios.each_line(sep=$/) {|line| block }    -> ios
02953  *     ios.each_line(limit) {|line| block }     -> ios
02954  *     ios.each_line(sep,limit) {|line| block } -> ios
02955  *     ios.each_line(...)                       -> an_enumerator
02956  *
02957  *     ios.lines(sep=$/) {|line| block }        -> ios
02958  *     ios.lines(limit) {|line| block }         -> ios
02959  *     ios.lines(sep,limit) {|line| block }     -> ios
02960  *     ios.lines(...)                           -> an_enumerator
02961  *
02962  *  Executes the block for every line in <em>ios</em>, where lines are
02963  *  separated by <i>sep</i>. <em>ios</em> must be opened for
02964  *  reading or an <code>IOError</code> will be raised.
02965  *
02966  *  If no block is given, an enumerator is returned instead.
02967  *
02968  *     f = File.new("testfile")
02969  *     f.each {|line| puts "#{f.lineno}: #{line}" }
02970  *
02971  *  <em>produces:</em>
02972  *
02973  *     1: This is line one
02974  *     2: This is line two
02975  *     3: This is line three
02976  *     4: And so on...
02977  */
02978 
02979 static VALUE
02980 rb_io_each_line(int argc, VALUE *argv, VALUE io)
02981 {
02982     VALUE str, rs;
02983     long limit;
02984 
02985     RETURN_ENUMERATOR(io, argc, argv);
02986     prepare_getline_args(argc, argv, &rs, &limit, io);
02987     if (limit == 0)
02988         rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
02989     while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) {
02990         rb_yield(str);
02991     }
02992     return io;
02993 }
02994 
02995 /*
02996  *  call-seq:
02997  *     ios.bytes {|byte| block }      -> ios
02998  *     ios.bytes                      -> an_enumerator
02999  *
03000  *     ios.each_byte {|byte| block }  -> ios
03001  *     ios.each_byte                  -> an_enumerator
03002  *
03003  *  Calls the given block once for each byte (0..255) in <em>ios</em>,
03004  *  passing the byte as an argument. The stream must be opened for
03005  *  reading or an <code>IOError</code> will be raised.
03006  *
03007  *  If no block is given, an enumerator is returned instead.
03008  *
03009  *     f = File.new("testfile")
03010  *     checksum = 0
03011  *     f.each_byte {|x| checksum ^= x }   #=> #<File:testfile>
03012  *     checksum                           #=> 12
03013  */
03014 
03015 static VALUE
03016 rb_io_each_byte(VALUE io)
03017 {
03018     rb_io_t *fptr;
03019     char *p, *e;
03020 
03021     RETURN_ENUMERATOR(io, 0, 0);
03022     GetOpenFile(io, fptr);
03023 
03024     for (;;) {
03025         while (fptr->rbuf.len > 0) {
03026             p = fptr->rbuf.ptr + fptr->rbuf.off++;
03027             e = p + fptr->rbuf.len--;
03028             rb_yield(INT2FIX(*p & 0xff));
03029             errno = 0;
03030         }
03031         rb_io_check_byte_readable(fptr);
03032         READ_CHECK(fptr);
03033         if (io_fillbuf(fptr) < 0) {
03034             break;
03035         }
03036     }
03037     return io;
03038 }
03039 
03040 static VALUE
03041 io_getc(rb_io_t *fptr, rb_encoding *enc)
03042 {
03043     int r, n, cr = 0;
03044     VALUE str;
03045 
03046     if (NEED_READCONV(fptr)) {
03047         VALUE str = Qnil;
03048         rb_encoding *read_enc = io_read_encoding(fptr);
03049 
03050         SET_BINARY_MODE(fptr);
03051         make_readconv(fptr, 0);
03052 
03053         while (1) {
03054             if (fptr->cbuf.len) {
03055                 r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
03056                         fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
03057                         read_enc);
03058                 if (!MBCLEN_NEEDMORE_P(r))
03059                     break;
03060                 if (fptr->cbuf.len == fptr->cbuf.capa) {
03061                     rb_raise(rb_eIOError, "too long character");
03062                 }
03063             }
03064 
03065             if (more_char(fptr) == MORE_CHAR_FINISHED) {
03066                 if (fptr->cbuf.len == 0) {
03067                     clear_readconv(fptr);
03068                     return Qnil;
03069                 }
03070                 /* return an unit of an incomplete character just before EOF */
03071                 str = rb_enc_str_new(fptr->cbuf.ptr+fptr->cbuf.off, 1, read_enc);
03072                 fptr->cbuf.off += 1;
03073                 fptr->cbuf.len -= 1;
03074                 if (fptr->cbuf.len == 0) clear_readconv(fptr);
03075                 ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
03076                 return str;
03077             }
03078         }
03079         if (MBCLEN_INVALID_P(r)) {
03080             r = rb_enc_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
03081                               fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
03082                               read_enc);
03083             io_shift_cbuf(fptr, r, &str);
03084             cr = ENC_CODERANGE_BROKEN;
03085         }
03086         else {
03087             io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str);
03088             cr = ISASCII(r) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
03089         }
03090         str = io_enc_str(str, fptr);
03091         ENC_CODERANGE_SET(str, cr);
03092         return str;
03093     }
03094 
03095     NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
03096     if (io_fillbuf(fptr) < 0) {
03097         return Qnil;
03098     }
03099     if (rb_enc_asciicompat(enc) && ISASCII(fptr->rbuf.ptr[fptr->rbuf.off])) {
03100         str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
03101         fptr->rbuf.off += 1;
03102         fptr->rbuf.len -= 1;
03103         cr = ENC_CODERANGE_7BIT;
03104     }
03105     else {
03106         r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
03107         if (MBCLEN_CHARFOUND_P(r) &&
03108             (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
03109             str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, n);
03110             fptr->rbuf.off += n;
03111             fptr->rbuf.len -= n;
03112             cr = ENC_CODERANGE_VALID;
03113         }
03114         else if (MBCLEN_NEEDMORE_P(r)) {
03115             str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.len);
03116             fptr->rbuf.len = 0;
03117           getc_needmore:
03118             if (io_fillbuf(fptr) != -1) {
03119                 rb_str_cat(str, fptr->rbuf.ptr+fptr->rbuf.off, 1);
03120                 fptr->rbuf.off++;
03121                 fptr->rbuf.len--;
03122                 r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
03123                 if (MBCLEN_NEEDMORE_P(r)) {
03124                     goto getc_needmore;
03125                 }
03126                 else if (MBCLEN_CHARFOUND_P(r)) {
03127                     cr = ENC_CODERANGE_VALID;
03128                 }
03129             }
03130         }
03131         else {
03132             str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
03133             fptr->rbuf.off++;
03134             fptr->rbuf.len--;
03135         }
03136     }
03137     if (!cr) cr = ENC_CODERANGE_BROKEN;
03138     str = io_enc_str(str, fptr);
03139     ENC_CODERANGE_SET(str, cr);
03140     return str;
03141 }
03142 
03143 /*
03144  *  call-seq:
03145  *     ios.chars {|c| block }      -> ios
03146  *     ios.chars                   -> an_enumerator
03147  *
03148  *     ios.each_char {|c| block }  -> ios
03149  *     ios.each_char               -> an_enumerator
03150  *
03151  *  Calls the given block once for each character in <em>ios</em>,
03152  *  passing the character as an argument. The stream must be opened for
03153  *  reading or an <code>IOError</code> will be raised.
03154  *
03155  *  If no block is given, an enumerator is returned instead.
03156  *
03157  *     f = File.new("testfile")
03158  *     f.each_char {|c| print c, ' ' }   #=> #<File:testfile>
03159  */
03160 
03161 static VALUE
03162 rb_io_each_char(VALUE io)
03163 {
03164     rb_io_t *fptr;
03165     rb_encoding *enc;
03166     VALUE c;
03167 
03168     RETURN_ENUMERATOR(io, 0, 0);
03169     GetOpenFile(io, fptr);
03170     rb_io_check_char_readable(fptr);
03171 
03172     enc = io_input_encoding(fptr);
03173     READ_CHECK(fptr);
03174     while (!NIL_P(c = io_getc(fptr, enc))) {
03175         rb_yield(c);
03176     }
03177     return io;
03178 }
03179 
03180 
03181 /*
03182  *  call-seq:
03183  *     ios.each_codepoint {|c| block }  -> ios
03184  *     ios.codepoints     {|c| block }  -> ios
03185  *     ios.each_codepoint               -> an_enumerator
03186  *     ios.codepoints                   -> an_enumerator
03187  *
03188  *  Passes the <code>Integer</code> ordinal of each character in <i>ios</i>,
03189  *  passing the codepoint as an argument. The stream must be opened for
03190  *  reading or an <code>IOError</code> will be raised.
03191  *
03192  *  If no block is given, an enumerator is returned instead.
03193  *
03194  */
03195 
03196 static VALUE
03197 rb_io_each_codepoint(VALUE io)
03198 {
03199     rb_io_t *fptr;
03200     rb_encoding *enc;
03201     unsigned int c;
03202     int r, n;
03203 
03204     RETURN_ENUMERATOR(io, 0, 0);
03205     GetOpenFile(io, fptr);
03206     rb_io_check_char_readable(fptr);
03207 
03208     READ_CHECK(fptr);
03209     if (NEED_READCONV(fptr)) {
03210         SET_BINARY_MODE(fptr);
03211         for (;;) {
03212             make_readconv(fptr, 0);
03213             for (;;) {
03214                 if (fptr->cbuf.len) {
03215                     if (fptr->encs.enc)
03216                         r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
03217                                                   fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
03218                                                   fptr->encs.enc);
03219                     else
03220                         r = ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(1);
03221                     if (!MBCLEN_NEEDMORE_P(r))
03222                         break;
03223                     if (fptr->cbuf.len == fptr->cbuf.capa) {
03224                         rb_raise(rb_eIOError, "too long character");
03225                     }
03226                 }
03227                 if (more_char(fptr) == MORE_CHAR_FINISHED) {
03228                     clear_readconv(fptr);
03229                     /* ignore an incomplete character before EOF */
03230                     return io;
03231                 }
03232             }
03233             if (MBCLEN_INVALID_P(r)) {
03234                 rb_raise(rb_eArgError, "invalid byte sequence in %s",
03235                          rb_enc_name(fptr->encs.enc));
03236             }
03237             n = MBCLEN_CHARFOUND_LEN(r);
03238             if (fptr->encs.enc) {
03239                 c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
03240                                      fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
03241                                      fptr->encs.enc);
03242             }
03243             else {
03244                 c = (unsigned char)fptr->cbuf.ptr[fptr->cbuf.off];
03245             }
03246             fptr->cbuf.off += n;
03247             fptr->cbuf.len -= n;
03248             rb_yield(UINT2NUM(c));
03249         }
03250     }
03251     NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
03252     enc = io_input_encoding(fptr);
03253     for (;;) {
03254         if (io_fillbuf(fptr) < 0) {
03255             return io;
03256         }
03257         r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off,
03258                                   fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
03259         if (MBCLEN_CHARFOUND_P(r) &&
03260             (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
03261             c = rb_enc_codepoint(fptr->rbuf.ptr+fptr->rbuf.off,
03262                                  fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
03263             fptr->rbuf.off += n;
03264             fptr->rbuf.len -= n;
03265             rb_yield(UINT2NUM(c));
03266         }
03267         else if (MBCLEN_INVALID_P(r)) {
03268             rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
03269         }
03270         else {
03271             continue;
03272         }
03273     }
03274     return io;
03275 }
03276 
03277 
03278 
03279 /*
03280  *  call-seq:
03281  *     ios.getc   -> string or nil
03282  *
03283  *  Reads a one-character string from <em>ios</em>. Returns
03284  *  <code>nil</code> if called at end of file.
03285  *
03286  *     f = File.new("testfile")
03287  *     f.getc   #=> "h"
03288  *     f.getc   #=> "e"
03289  */
03290 
03291 static VALUE
03292 rb_io_getc(VALUE io)
03293 {
03294     rb_io_t *fptr;
03295     rb_encoding *enc;
03296 
03297     GetOpenFile(io, fptr);
03298     rb_io_check_char_readable(fptr);
03299 
03300     enc = io_input_encoding(fptr);
03301     READ_CHECK(fptr);
03302     return io_getc(fptr, enc);
03303 }
03304 
03305 /*
03306  *  call-seq:
03307  *     ios.readchar   -> string
03308  *
03309  *  Reads a one-character string from <em>ios</em>. Raises an
03310  *  <code>EOFError</code> on end of file.
03311  *
03312  *     f = File.new("testfile")
03313  *     f.readchar   #=> "h"
03314  *     f.readchar   #=> "e"
03315  */
03316 
03317 static VALUE
03318 rb_io_readchar(VALUE io)
03319 {
03320     VALUE c = rb_io_getc(io);
03321 
03322     if (NIL_P(c)) {
03323         rb_eof_error();
03324     }
03325     return c;
03326 }
03327 
03328 /*
03329  *  call-seq:
03330  *     ios.getbyte   -> fixnum or nil
03331  *
03332  *  Gets the next 8-bit byte (0..255) from <em>ios</em>. Returns
03333  *  <code>nil</code> if called at end of file.
03334  *
03335  *     f = File.new("testfile")
03336  *     f.getbyte   #=> 84
03337  *     f.getbyte   #=> 104
03338  */
03339 
03340 VALUE
03341 rb_io_getbyte(VALUE io)
03342 {
03343     rb_io_t *fptr;
03344     int c;
03345 
03346     GetOpenFile(io, fptr);
03347     rb_io_check_byte_readable(fptr);
03348     READ_CHECK(fptr);
03349     if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && TYPE(rb_stdout) == T_FILE) {
03350         rb_io_t *ofp;
03351         GetOpenFile(rb_stdout, ofp);
03352         if (ofp->mode & FMODE_TTY) {
03353             rb_io_flush(rb_stdout);
03354         }
03355     }
03356     if (io_fillbuf(fptr) < 0) {
03357         return Qnil;
03358     }
03359     fptr->rbuf.off++;
03360     fptr->rbuf.len--;
03361     c = (unsigned char)fptr->rbuf.ptr[fptr->rbuf.off-1];
03362     return INT2FIX(c & 0xff);
03363 }
03364 
03365 /*
03366  *  call-seq:
03367  *     ios.readbyte   -> fixnum
03368  *
03369  *  Reads a byte as with <code>IO#getbyte</code>, but raises an
03370  *  <code>EOFError</code> on end of file.
03371  */
03372 
03373 static VALUE
03374 rb_io_readbyte(VALUE io)
03375 {
03376     VALUE c = rb_io_getbyte(io);
03377 
03378     if (NIL_P(c)) {
03379         rb_eof_error();
03380     }
03381     return c;
03382 }
03383 
03384 /*
03385  *  call-seq:
03386  *     ios.ungetbyte(string)   -> nil
03387  *     ios.ungetbyte(integer)   -> nil
03388  *
03389  *  Pushes back bytes (passed as a parameter) onto <em>ios</em>,
03390  *  such that a subsequent buffered read will return it. Only one byte
03391  *  may be pushed back before a subsequent read operation (that is,
03392  *  you will be able to read only the last of several bytes that have been pushed
03393  *  back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
03394  *
03395  *     f = File.new("testfile")   #=> #<File:testfile>
03396  *     b = f.getbyte              #=> 0x38
03397  *     f.ungetbyte(b)             #=> nil
03398  *     f.getbyte                  #=> 0x38
03399  */
03400 
03401 VALUE
03402 rb_io_ungetbyte(VALUE io, VALUE b)
03403 {
03404     rb_io_t *fptr;
03405 
03406     GetOpenFile(io, fptr);
03407     rb_io_check_byte_readable(fptr);
03408     if (NIL_P(b)) return Qnil;
03409     if (FIXNUM_P(b)) {
03410         char cc = FIX2INT(b);
03411         b = rb_str_new(&cc, 1);
03412     }
03413     else {
03414         SafeStringValue(b);
03415     }
03416     io_ungetbyte(b, fptr);
03417     return Qnil;
03418 }
03419 
03420 /*
03421  *  call-seq:
03422  *     ios.ungetc(string)   -> nil
03423  *
03424  *  Pushes back one character (passed as a parameter) onto <em>ios</em>,
03425  *  such that a subsequent buffered character read will return it. Only one character
03426  *  may be pushed back before a subsequent read operation (that is,
03427  *  you will be able to read only the last of several characters that have been pushed
03428  *  back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
03429  *
03430  *     f = File.new("testfile")   #=> #<File:testfile>
03431  *     c = f.getc                 #=> "8"
03432  *     f.ungetc(c)                #=> nil
03433  *     f.getc                     #=> "8"
03434  */
03435 
03436 VALUE
03437 rb_io_ungetc(VALUE io, VALUE c)
03438 {
03439     rb_io_t *fptr;
03440     long len;
03441 
03442     GetOpenFile(io, fptr);
03443     rb_io_check_char_readable(fptr);
03444     if (NIL_P(c)) return Qnil;
03445     if (FIXNUM_P(c)) {
03446         c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
03447     }
03448     else if (TYPE(c) == T_BIGNUM) {
03449         c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
03450     }
03451     else {
03452         SafeStringValue(c);
03453     }
03454     if (NEED_READCONV(fptr)) {
03455         SET_BINARY_MODE(fptr);
03456         len = RSTRING_LEN(c);
03457 #if SIZEOF_LONG > SIZEOF_INT
03458         if (len > INT_MAX)
03459             rb_raise(rb_eIOError, "ungetc failed");
03460 #endif
03461         make_readconv(fptr, (int)len);
03462         if (fptr->cbuf.capa - fptr->cbuf.len < len)
03463             rb_raise(rb_eIOError, "ungetc failed");
03464         if (fptr->cbuf.off < len) {
03465             MEMMOVE(fptr->cbuf.ptr+fptr->cbuf.capa-fptr->cbuf.len,
03466                     fptr->cbuf.ptr+fptr->cbuf.off,
03467                     char, fptr->cbuf.len);
03468             fptr->cbuf.off = fptr->cbuf.capa-fptr->cbuf.len;
03469         }
03470         fptr->cbuf.off -= (int)len;
03471         fptr->cbuf.len += (int)len;
03472         MEMMOVE(fptr->cbuf.ptr+fptr->cbuf.off, RSTRING_PTR(c), char, len);
03473     }
03474     else {
03475         NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
03476         io_ungetbyte(c, fptr);
03477     }
03478     return Qnil;
03479 }
03480 
03481 /*
03482  *  call-seq:
03483  *     ios.isatty   -> true or false
03484  *     ios.tty?     -> true or false
03485  *
03486  *  Returns <code>true</code> if <em>ios</em> is associated with a
03487  *  terminal device (tty), <code>false</code> otherwise.
03488  *
03489  *     File.new("testfile").isatty   #=> false
03490  *     File.new("/dev/tty").isatty   #=> true
03491  */
03492 
03493 static VALUE
03494 rb_io_isatty(VALUE io)
03495 {
03496     rb_io_t *fptr;
03497 
03498     GetOpenFile(io, fptr);
03499     if (isatty(fptr->fd) == 0)
03500         return Qfalse;
03501     return Qtrue;
03502 }
03503 
03504 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
03505 /*
03506  *  call-seq:
03507  *     ios.close_on_exec?   -> true or false
03508  *
03509  *  Returns <code>true</code> if <em>ios</em> will be closed on exec.
03510  *
03511  *     f = open("/dev/null")
03512  *     f.close_on_exec?                 #=> false
03513  *     f.close_on_exec = true
03514  *     f.close_on_exec?                 #=> true
03515  *     f.close_on_exec = false
03516  *     f.close_on_exec?                 #=> false
03517  */
03518 
03519 static VALUE
03520 rb_io_close_on_exec_p(VALUE io)
03521 {
03522     rb_io_t *fptr;
03523     VALUE write_io;
03524     int fd, ret;
03525 
03526     write_io = GetWriteIO(io);
03527     if (io != write_io) {
03528         GetOpenFile(write_io, fptr);
03529         if (fptr && 0 <= (fd = fptr->fd)) {
03530             if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
03531             if (!(ret & FD_CLOEXEC)) return Qfalse;
03532         }
03533     }
03534 
03535     GetOpenFile(io, fptr);
03536     if (fptr && 0 <= (fd = fptr->fd)) {
03537         if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
03538         if (!(ret & FD_CLOEXEC)) return Qfalse;
03539     }
03540     return Qtrue;
03541 }
03542 #else
03543 #define rb_io_close_on_exec_p rb_f_notimplement
03544 #endif
03545 
03546 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
03547 /*
03548  *  call-seq:
03549  *     ios.close_on_exec = bool    -> true or false
03550  *
03551  *  Sets a close-on-exec flag.
03552  *
03553  *     f = open("/dev/null")
03554  *     f.close_on_exec = true
03555  *     system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
03556  *     f.closed?                #=> false
03557  */
03558 
03559 static VALUE
03560 rb_io_set_close_on_exec(VALUE io, VALUE arg)
03561 {
03562     int flag = RTEST(arg) ? FD_CLOEXEC : 0;
03563     rb_io_t *fptr;
03564     VALUE write_io;
03565     int fd, ret;
03566 
03567     write_io = GetWriteIO(io);
03568     if (io != write_io) {
03569         GetOpenFile(write_io, fptr);
03570         if (fptr && 0 <= (fd = fptr->fd)) {
03571             if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
03572             if ((ret & FD_CLOEXEC) != flag) {
03573                 ret = (ret & ~FD_CLOEXEC) | flag;
03574                 ret = fcntl(fd, F_SETFD, ret);
03575                 if (ret == -1) rb_sys_fail_path(fptr->pathv);
03576             }
03577         }
03578 
03579     }
03580 
03581     GetOpenFile(io, fptr);
03582     if (fptr && 0 <= (fd = fptr->fd)) {
03583         if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
03584         if ((ret & FD_CLOEXEC) != flag) {
03585             ret = (ret & ~FD_CLOEXEC) | flag;
03586             ret = fcntl(fd, F_SETFD, ret);
03587             if (ret == -1) rb_sys_fail_path(fptr->pathv);
03588         }
03589     }
03590     return Qnil;
03591 }
03592 #else
03593 #define rb_io_set_close_on_exec rb_f_notimplement
03594 #endif
03595 
03596 #define FMODE_PREP (1<<16)
03597 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
03598 #define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv))
03599 
03600 static VALUE
03601 finish_writeconv(rb_io_t *fptr, int noalloc)
03602 {
03603     unsigned char *ds, *dp, *de;
03604     rb_econv_result_t res;
03605 
03606     if (!fptr->wbuf.ptr) {
03607         unsigned char buf[1024];
03608         long r;
03609 
03610         res = econv_destination_buffer_full;
03611         while (res == econv_destination_buffer_full) {
03612             ds = dp = buf;
03613             de = buf + sizeof(buf);
03614             res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
03615             while (dp-ds) {
03616               retry:
03617                 r = rb_write_internal(fptr->fd, ds, dp-ds);
03618                 if (r == dp-ds)
03619                     break;
03620                 if (0 <= r) {
03621                     ds += r;
03622                 }
03623                 if (rb_io_wait_writable(fptr->fd)) {
03624                     if (fptr->fd < 0)
03625                         return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr("closed stream"));
03626                     goto retry;
03627                 }
03628                 return noalloc ? Qtrue : INT2NUM(errno);
03629             }
03630             if (res == econv_invalid_byte_sequence ||
03631                 res == econv_incomplete_input ||
03632                 res == econv_undefined_conversion) {
03633                 return noalloc ? Qtrue : rb_econv_make_exception(fptr->writeconv);
03634             }
03635         }
03636 
03637         return Qnil;
03638     }
03639 
03640     res = econv_destination_buffer_full;
03641     while (res == econv_destination_buffer_full) {
03642         if (fptr->wbuf.len == fptr->wbuf.capa) {
03643             if (io_fflush(fptr) < 0)
03644                 return noalloc ? Qtrue : INT2NUM(errno);
03645         }
03646 
03647         ds = dp = (unsigned char *)fptr->wbuf.ptr + fptr->wbuf.off + fptr->wbuf.len;
03648         de = (unsigned char *)fptr->wbuf.ptr + fptr->wbuf.capa;
03649         res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
03650         fptr->wbuf.len += (int)(dp - ds);
03651         if (res == econv_invalid_byte_sequence ||
03652             res == econv_incomplete_input ||
03653             res == econv_undefined_conversion) {
03654             return noalloc ? Qtrue : rb_econv_make_exception(fptr->writeconv);
03655         }
03656     }
03657     return Qnil;
03658 }
03659 
03660 struct finish_writeconv_arg {
03661     rb_io_t *fptr;
03662     int noalloc;
03663 };
03664 
03665 static VALUE
03666 finish_writeconv_sync(VALUE arg)
03667 {
03668     struct finish_writeconv_arg *p = (struct finish_writeconv_arg *)arg;
03669     return finish_writeconv(p->fptr, p->noalloc);
03670 }
03671 
03672 static void
03673 fptr_finalize(rb_io_t *fptr, int noraise)
03674 {
03675     VALUE err = Qnil;
03676     if (fptr->writeconv) {
03677         if (fptr->write_lock && !noraise) {
03678             struct finish_writeconv_arg arg;
03679             arg.fptr = fptr;
03680             arg.noalloc = noraise;
03681             err = rb_mutex_synchronize(fptr->write_lock, finish_writeconv_sync, (VALUE)&arg);
03682         }
03683         else {
03684             err = finish_writeconv(fptr, noraise);
03685         }
03686     }
03687     if (fptr->wbuf.len) {
03688         if (noraise) {
03689             if ((int)io_flush_buffer_sync(fptr) < 0 && NIL_P(err))
03690                 err = Qtrue;
03691         }
03692         else {
03693             if (io_fflush(fptr) < 0 && NIL_P(err))
03694                 err = INT2NUM(errno);
03695         }
03696     }
03697     if (IS_PREP_STDIO(fptr) || fptr->fd <= 2) {
03698         goto skip_fd_close;
03699     }
03700     if (fptr->stdio_file) {
03701         /* fptr->stdio_file is deallocated anyway
03702          * even if fclose failed.  */
03703         if (fclose(fptr->stdio_file) < 0 && NIL_P(err))
03704             err = noraise ? Qtrue : INT2NUM(errno);
03705     }
03706     else if (0 <= fptr->fd) {
03707         /* fptr->fd may be closed even if close fails.
03708          * POSIX doesn't specify it.
03709          * We assumes it is closed.  */
03710         if (close(fptr->fd) < 0 && NIL_P(err))
03711             err = noraise ? Qtrue : INT2NUM(errno);
03712     }
03713   skip_fd_close:
03714     fptr->fd = -1;
03715     fptr->stdio_file = 0;
03716     fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
03717 
03718     if (!NIL_P(err) && !noraise) {
03719         switch(TYPE(err)) {
03720           case T_FIXNUM:
03721           case T_BIGNUM:
03722             errno = NUM2INT(err);
03723             rb_sys_fail_path(fptr->pathv);
03724 
03725           default:
03726             rb_exc_raise(err);
03727         }
03728     }
03729 }
03730 
03731 static void
03732 rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
03733 {
03734     if (fptr->finalize) {
03735         (*fptr->finalize)(fptr, noraise);
03736     }
03737     else {
03738         fptr_finalize(fptr, noraise);
03739     }
03740 }
03741 
03742 static void
03743 clear_readconv(rb_io_t *fptr)
03744 {
03745     if (fptr->readconv) {
03746         rb_econv_close(fptr->readconv);
03747         fptr->readconv = NULL;
03748     }
03749     if (fptr->cbuf.ptr) {
03750         free(fptr->cbuf.ptr);
03751         fptr->cbuf.ptr = NULL;
03752     }
03753 }
03754 
03755 static void
03756 clear_writeconv(rb_io_t *fptr)
03757 {
03758     if (fptr->writeconv) {
03759         rb_econv_close(fptr->writeconv);
03760         fptr->writeconv = NULL;
03761     }
03762     fptr->writeconv_initialized = 0;
03763 }
03764 
03765 static void
03766 clear_codeconv(rb_io_t *fptr)
03767 {
03768     clear_readconv(fptr);
03769     clear_writeconv(fptr);
03770 }
03771 
03772 int
03773 rb_io_fptr_finalize(rb_io_t *fptr)
03774 {
03775     if (!fptr) return 0;
03776     fptr->pathv = Qnil;
03777     if (0 <= fptr->fd)
03778         rb_io_fptr_cleanup(fptr, TRUE);
03779     fptr->write_lock = 0;
03780     if (fptr->rbuf.ptr) {
03781         free(fptr->rbuf.ptr);
03782         fptr->rbuf.ptr = 0;
03783     }
03784     if (fptr->wbuf.ptr) {
03785         free(fptr->wbuf.ptr);
03786         fptr->wbuf.ptr = 0;
03787     }
03788     clear_codeconv(fptr);
03789     free(fptr);
03790     return 1;
03791 }
03792 
03793 size_t rb_econv_memsize(rb_econv_t *);
03794 
03795 RUBY_FUNC_EXPORTED size_t
03796 rb_io_memsize(const rb_io_t *fptr)
03797 {
03798     size_t size = sizeof(rb_io_t);
03799     size += fptr->rbuf.capa;
03800     size += fptr->wbuf.capa;
03801     size += fptr->cbuf.capa;
03802     if (fptr->readconv) size += rb_econv_memsize(fptr->readconv);
03803     if (fptr->writeconv) size += rb_econv_memsize(fptr->writeconv);
03804     return size;
03805 }
03806 
03807 VALUE
03808 rb_io_close(VALUE io)
03809 {
03810     rb_io_t *fptr;
03811     int fd;
03812     VALUE write_io;
03813     rb_io_t *write_fptr;
03814 
03815     write_io = GetWriteIO(io);
03816     if (io != write_io) {
03817         write_fptr = RFILE(write_io)->fptr;
03818         if (write_fptr && 0 <= write_fptr->fd) {
03819             rb_io_fptr_cleanup(write_fptr, TRUE);
03820         }
03821     }
03822 
03823     fptr = RFILE(io)->fptr;
03824     if (!fptr) return Qnil;
03825     if (fptr->fd < 0) return Qnil;
03826 
03827     fd = fptr->fd;
03828 #if defined __APPLE__ && defined(__MACH__) && \
03829     (!defined(MAC_OS_X_VERSION_MIN_ALLOWED) || MAC_OS_X_VERSION_MIN_ALLOWED <= 1050)
03830     /* close(2) on a fd which is being read by another thread causes
03831      * deadlock on Mac OS X 10.5 */
03832     rb_thread_fd_close(fd);
03833 #endif
03834     rb_io_fptr_cleanup(fptr, FALSE);
03835     rb_thread_fd_close(fd);
03836 
03837     if (fptr->pid) {
03838         rb_syswait(fptr->pid);
03839         fptr->pid = 0;
03840     }
03841 
03842     return Qnil;
03843 }
03844 
03845 /*
03846  *  call-seq:
03847  *     ios.close   -> nil
03848  *
03849  *  Closes <em>ios</em> and flushes any pending writes to the operating
03850  *  system. The stream is unavailable for any further data operations;
03851  *  an <code>IOError</code> is raised if such an attempt is made. I/O
03852  *  streams are automatically closed when they are claimed by the
03853  *  garbage collector.
03854  *
03855  *  If <em>ios</em> is opened by <code>IO.popen</code>,
03856  *  <code>close</code> sets <code>$?</code>.
03857  */
03858 
03859 static VALUE
03860 rb_io_close_m(VALUE io)
03861 {
03862     if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
03863         rb_raise(rb_eSecurityError, "Insecure: can't close");
03864     }
03865     rb_io_check_closed(RFILE(io)->fptr);
03866     rb_io_close(io);
03867     return Qnil;
03868 }
03869 
03870 static VALUE
03871 io_call_close(VALUE io)
03872 {
03873     return rb_funcall(io, rb_intern("close"), 0, 0);
03874 }
03875 
03876 static VALUE
03877 io_close(VALUE io)
03878 {
03879     return rb_rescue(io_call_close, io, 0, 0);
03880 }
03881 
03882 /*
03883  *  call-seq:
03884  *     ios.closed?    -> true or false
03885  *
03886  *  Returns <code>true</code> if <em>ios</em> is completely closed (for
03887  *  duplex streams, both reader and writer), <code>false</code>
03888  *  otherwise.
03889  *
03890  *     f = File.new("testfile")
03891  *     f.close         #=> nil
03892  *     f.closed?       #=> true
03893  *     f = IO.popen("/bin/sh","r+")
03894  *     f.close_write   #=> nil
03895  *     f.closed?       #=> false
03896  *     f.close_read    #=> nil
03897  *     f.closed?       #=> true
03898  */
03899 
03900 
03901 static VALUE
03902 rb_io_closed(VALUE io)
03903 {
03904     rb_io_t *fptr;
03905     VALUE write_io;
03906     rb_io_t *write_fptr;
03907 
03908     write_io = GetWriteIO(io);
03909     if (io != write_io) {
03910         write_fptr = RFILE(write_io)->fptr;
03911         if (write_fptr && 0 <= write_fptr->fd) {
03912             return Qfalse;
03913         }
03914     }
03915 
03916     fptr = RFILE(io)->fptr;
03917     rb_io_check_initialized(fptr);
03918     return 0 <= fptr->fd ? Qfalse : Qtrue;
03919 }
03920 
03921 /*
03922  *  call-seq:
03923  *     ios.close_read    -> nil
03924  *
03925  *  Closes the read end of a duplex I/O stream (i.e., one that contains
03926  *  both a read and a write stream, such as a pipe). Will raise an
03927  *  <code>IOError</code> if the stream is not duplexed.
03928  *
03929  *     f = IO.popen("/bin/sh","r+")
03930  *     f.close_read
03931  *     f.readlines
03932  *
03933  *  <em>produces:</em>
03934  *
03935  *     prog.rb:3:in `readlines': not opened for reading (IOError)
03936  *      from prog.rb:3
03937  */
03938 
03939 static VALUE
03940 rb_io_close_read(VALUE io)
03941 {
03942     rb_io_t *fptr;
03943     VALUE write_io;
03944 
03945     if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
03946         rb_raise(rb_eSecurityError, "Insecure: can't close");
03947     }
03948     GetOpenFile(io, fptr);
03949     if (is_socket(fptr->fd, fptr->pathv)) {
03950 #ifndef SHUT_RD
03951 # define SHUT_RD 0
03952 #endif
03953         if (shutdown(fptr->fd, SHUT_RD) < 0)
03954             rb_sys_fail_path(fptr->pathv);
03955         fptr->mode &= ~FMODE_READABLE;
03956         if (!(fptr->mode & FMODE_WRITABLE))
03957             return rb_io_close(io);
03958         return Qnil;
03959     }
03960 
03961     write_io = GetWriteIO(io);
03962     if (io != write_io) {
03963         rb_io_t *wfptr;
03964         rb_io_fptr_cleanup(fptr, FALSE);
03965         GetOpenFile(write_io, wfptr);
03966         RFILE(io)->fptr = wfptr;
03967         RFILE(write_io)->fptr = NULL;
03968         rb_io_fptr_finalize(fptr);
03969         return Qnil;
03970     }
03971 
03972     if (fptr->mode & FMODE_WRITABLE) {
03973         rb_raise(rb_eIOError, "closing non-duplex IO for reading");
03974     }
03975     return rb_io_close(io);
03976 }
03977 
03978 /*
03979  *  call-seq:
03980  *     ios.close_write   -> nil
03981  *
03982  *  Closes the write end of a duplex I/O stream (i.e., one that contains
03983  *  both a read and a write stream, such as a pipe). Will raise an
03984  *  <code>IOError</code> if the stream is not duplexed.
03985  *
03986  *     f = IO.popen("/bin/sh","r+")
03987  *     f.close_write
03988  *     f.print "nowhere"
03989  *
03990  *  <em>produces:</em>
03991  *
03992  *     prog.rb:3:in `write': not opened for writing (IOError)
03993  *      from prog.rb:3:in `print'
03994  *      from prog.rb:3
03995  */
03996 
03997 static VALUE
03998 rb_io_close_write(VALUE io)
03999 {
04000     rb_io_t *fptr;
04001     VALUE write_io;
04002 
04003     if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
04004         rb_raise(rb_eSecurityError, "Insecure: can't close");
04005     }
04006     write_io = GetWriteIO(io);
04007     GetOpenFile(write_io, fptr);
04008     if (is_socket(fptr->fd, fptr->pathv)) {
04009 #ifndef SHUT_WR
04010 # define SHUT_WR 1
04011 #endif
04012         if (shutdown(fptr->fd, SHUT_WR) < 0)
04013             rb_sys_fail_path(fptr->pathv);
04014         fptr->mode &= ~FMODE_WRITABLE;
04015         if (!(fptr->mode & FMODE_READABLE))
04016             return rb_io_close(write_io);
04017         return Qnil;
04018     }
04019 
04020     if (fptr->mode & FMODE_READABLE) {
04021         rb_raise(rb_eIOError, "closing non-duplex IO for writing");
04022     }
04023 
04024     rb_io_close(write_io);
04025     if (io != write_io) {
04026         GetOpenFile(io, fptr);
04027         fptr->tied_io_for_writing = 0;
04028         fptr->mode &= ~FMODE_DUPLEX;
04029     }
04030     return Qnil;
04031 }
04032 
04033 /*
04034  *  call-seq:
04035  *     ios.sysseek(offset, whence=IO::SEEK_SET)   -> integer
04036  *
04037  *  Seeks to a given <i>offset</i> in the stream according to the value
04038  *  of <i>whence</i> (see <code>IO#seek</code> for values of
04039  *  <i>whence</i>). Returns the new offset into the file.
04040  *
04041  *     f = File.new("testfile")
04042  *     f.sysseek(-13, IO::SEEK_END)   #=> 53
04043  *     f.sysread(10)                  #=> "And so on."
04044  */
04045 
04046 static VALUE
04047 rb_io_sysseek(int argc, VALUE *argv, VALUE io)
04048 {
04049     VALUE offset, ptrname;
04050     int whence = SEEK_SET;
04051     rb_io_t *fptr;
04052     off_t pos;
04053 
04054     if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
04055         whence = NUM2INT(ptrname);
04056     }
04057     pos = NUM2OFFT(offset);
04058     GetOpenFile(io, fptr);
04059     if ((fptr->mode & FMODE_READABLE) &&
04060         (READ_DATA_BUFFERED(fptr) || READ_CHAR_PENDING(fptr))) {
04061         rb_raise(rb_eIOError, "sysseek for buffered IO");
04062     }
04063     if ((fptr->mode & FMODE_WRITABLE) && fptr->wbuf.len) {
04064         rb_warn("sysseek for buffered IO");
04065     }
04066     errno = 0;
04067     pos = lseek(fptr->fd, pos, whence);
04068     if (pos == -1 && errno) rb_sys_fail_path(fptr->pathv);
04069 
04070     return OFFT2NUM(pos);
04071 }
04072 
04073 /*
04074  *  call-seq:
04075  *     ios.syswrite(string)   -> integer
04076  *
04077  *  Writes the given string to <em>ios</em> using a low-level write.
04078  *  Returns the number of bytes written. Do not mix with other methods
04079  *  that write to <em>ios</em> or you may get unpredictable results.
04080  *  Raises <code>SystemCallError</code> on error.
04081  *
04082  *     f = File.new("out", "w")
04083  *     f.syswrite("ABCDEF")   #=> 6
04084  */
04085 
04086 static VALUE
04087 rb_io_syswrite(VALUE io, VALUE str)
04088 {
04089     rb_io_t *fptr;
04090     long n;
04091 
04092     rb_secure(4);
04093     if (TYPE(str) != T_STRING)
04094         str = rb_obj_as_string(str);
04095 
04096     io = GetWriteIO(io);
04097     GetOpenFile(io, fptr);
04098     rb_io_check_writable(fptr);
04099 
04100     if (fptr->wbuf.len) {
04101         rb_warn("syswrite for buffered IO");
04102     }
04103     if (!rb_thread_fd_writable(fptr->fd)) {
04104         rb_io_check_closed(fptr);
04105     }
04106 
04107     n = rb_write_internal(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
04108 
04109     if (n == -1) rb_sys_fail_path(fptr->pathv);
04110 
04111     return LONG2FIX(n);
04112 }
04113 
04114 /*
04115  *  call-seq:
04116  *     ios.sysread(maxlen[, outbuf])    -> string
04117  *
04118  *  Reads <i>maxlen</i> bytes from <em>ios</em> using a low-level
04119  *  read and returns them as a string.  Do not mix with other methods
04120  *  that read from <em>ios</em> or you may get unpredictable results.
04121  *  If the optional <i>outbuf</i> argument is present, it must reference
04122  *  a String, which will receive the data.
04123  *  Raises <code>SystemCallError</code> on error and
04124  *  <code>EOFError</code> at end of file.
04125  *
04126  *     f = File.new("testfile")
04127  *     f.sysread(16)   #=> "This is line one"
04128  */
04129 
04130 static VALUE
04131 rb_io_sysread(int argc, VALUE *argv, VALUE io)
04132 {
04133     VALUE len, str;
04134     rb_io_t *fptr;
04135     long n, ilen;
04136 
04137     rb_scan_args(argc, argv, "11", &len, &str);
04138     ilen = NUM2LONG(len);
04139 
04140     io_setstrbuf(&str,ilen);
04141     if (ilen == 0) return str;
04142 
04143     GetOpenFile(io, fptr);
04144     rb_io_check_byte_readable(fptr);
04145 
04146     if (READ_DATA_BUFFERED(fptr)) {
04147         rb_raise(rb_eIOError, "sysread for buffered IO");
04148     }
04149 
04150     n = fptr->fd;
04151     rb_thread_wait_fd(fptr->fd);
04152     rb_io_check_closed(fptr);
04153 
04154     rb_str_locktmp(str);
04155     n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen);
04156     rb_str_unlocktmp(str);
04157 
04158     if (n == -1) {
04159         rb_sys_fail_path(fptr->pathv);
04160     }
04161     rb_str_set_len(str, n);
04162     if (n == 0 && ilen > 0) {
04163         rb_eof_error();
04164     }
04165     rb_str_resize(str, n);
04166     OBJ_TAINT(str);
04167 
04168     return str;
04169 }
04170 
04171 VALUE
04172 rb_io_binmode(VALUE io)
04173 {
04174     rb_io_t *fptr;
04175 
04176     GetOpenFile(io, fptr);
04177     if (fptr->readconv)
04178         rb_econv_binmode(fptr->readconv);
04179     if (fptr->writeconv)
04180         rb_econv_binmode(fptr->writeconv);
04181     fptr->mode |= FMODE_BINMODE;
04182     fptr->mode &= ~FMODE_TEXTMODE;
04183     fptr->writeconv_pre_ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
04184 #ifdef O_BINARY
04185     if (!fptr->readconv) {
04186         SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
04187     }
04188     else {
04189         setmode(fptr->fd, O_BINARY);
04190     }
04191 #endif
04192     return io;
04193 }
04194 
04195 VALUE
04196 rb_io_ascii8bit_binmode(VALUE io)
04197 {
04198     rb_io_t *fptr;
04199 
04200     GetOpenFile(io, fptr);
04201     if (fptr->readconv) {
04202         rb_econv_close(fptr->readconv);
04203         fptr->readconv = NULL;
04204     }
04205     if (fptr->writeconv) {
04206         rb_econv_close(fptr->writeconv);
04207         fptr->writeconv = NULL;
04208     }
04209     fptr->mode |= FMODE_BINMODE;
04210     fptr->mode &= ~FMODE_TEXTMODE;
04211     SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
04212 
04213     fptr->encs.enc = rb_ascii8bit_encoding();
04214     fptr->encs.enc2 = NULL;
04215     fptr->encs.ecflags = 0;
04216     fptr->encs.ecopts = Qnil;
04217     clear_codeconv(fptr);
04218 
04219     return io;
04220 }
04221 
04222 /*
04223  *  call-seq:
04224  *     ios.binmode    -> ios
04225  *
04226  *  Puts <em>ios</em> into binary mode.
04227  *  Once a stream is in binary mode, it cannot be reset to nonbinary mode.
04228  *
04229  *  - newline conversion disabled
04230  *  - encoding conversion disabled
04231  *  - content is treated as ASCII-8BIT
04232  *
04233  */
04234 
04235 static VALUE
04236 rb_io_binmode_m(VALUE io)
04237 {
04238     VALUE write_io;
04239 
04240     rb_io_ascii8bit_binmode(io);
04241 
04242     write_io = GetWriteIO(io);
04243     if (write_io != io)
04244         rb_io_ascii8bit_binmode(write_io);
04245     return io;
04246 }
04247 
04248 /*
04249  *  call-seq:
04250  *     ios.binmode?    -> true or false
04251  *
04252  *  Returns <code>true</code> if <em>ios</em> is binmode.
04253  */
04254 static VALUE
04255 rb_io_binmode_p(VALUE io)
04256 {
04257     rb_io_t *fptr;
04258     GetOpenFile(io, fptr);
04259     return fptr->mode & FMODE_BINMODE ? Qtrue : Qfalse;
04260 }
04261 
04262 static const char*
04263 rb_io_fmode_modestr(int fmode)
04264 {
04265     if (fmode & FMODE_APPEND) {
04266         if ((fmode & FMODE_READWRITE) == FMODE_READWRITE) {
04267             return MODE_BTMODE("a+", "ab+", "at+");
04268         }
04269         return MODE_BTMODE("a", "ab", "at");
04270     }
04271     switch (fmode & FMODE_READWRITE) {
04272       case FMODE_READABLE:
04273         return MODE_BTMODE("r", "rb", "rt");
04274       case FMODE_WRITABLE:
04275         return MODE_BTMODE("w", "wb", "wt");
04276       case FMODE_READWRITE:
04277         if (fmode & FMODE_CREATE) {
04278             return MODE_BTMODE("w+", "wb+", "wt+");
04279         }
04280         return MODE_BTMODE("r+", "rb+", "rt+");
04281     }
04282     rb_raise(rb_eArgError, "invalid access fmode 0x%x", fmode);
04283     return NULL;                /* not reached */
04284 }
04285 
04286 static int
04287 io_encname_bom_p(const char *name, long len)
04288 {
04289     static const char bom_prefix[] = "bom|utf-";
04290     enum {bom_prefix_len = (int)sizeof(bom_prefix) - 1};
04291     if (!len) {
04292         const char *p = strchr(name, ':');
04293         len = p ? (long)(p - name) : (long)strlen(name);
04294     }
04295     return len > bom_prefix_len && STRNCASECMP(name, bom_prefix, bom_prefix_len) == 0;
04296 }
04297 
04298 int
04299 rb_io_modestr_fmode(const char *modestr)
04300 {
04301     int fmode = 0;
04302     const char *m = modestr, *p = NULL;
04303 
04304     switch (*m++) {
04305       case 'r':
04306         fmode |= FMODE_READABLE;
04307         break;
04308       case 'w':
04309         fmode |= FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE;
04310         break;
04311       case 'a':
04312         fmode |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
04313         break;
04314       default:
04315       error:
04316         rb_raise(rb_eArgError, "invalid access mode %s", modestr);
04317     }
04318 
04319     while (*m) {
04320         switch (*m++) {
04321           case 'b':
04322             fmode |= FMODE_BINMODE;
04323             break;
04324           case 't':
04325             fmode |= FMODE_TEXTMODE;
04326             break;
04327           case '+':
04328             fmode |= FMODE_READWRITE;
04329             break;
04330           default:
04331             goto error;
04332           case ':':
04333             p = m;
04334             goto finished;
04335         }
04336     }
04337 
04338   finished:
04339     if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE))
04340         goto error;
04341     if (p && io_encname_bom_p(p, 0))
04342         fmode |= FMODE_SETENC_BY_BOM;
04343 
04344     return fmode;
04345 }
04346 
04347 int
04348 rb_io_oflags_fmode(int oflags)
04349 {
04350     int fmode = 0;
04351 
04352     switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
04353       case O_RDONLY:
04354         fmode = FMODE_READABLE;
04355         break;
04356       case O_WRONLY:
04357         fmode = FMODE_WRITABLE;
04358         break;
04359       case O_RDWR:
04360         fmode = FMODE_READWRITE;
04361         break;
04362     }
04363 
04364     if (oflags & O_APPEND) {
04365         fmode |= FMODE_APPEND;
04366     }
04367     if (oflags & O_TRUNC) {
04368         fmode |= FMODE_TRUNC;
04369     }
04370     if (oflags & O_CREAT) {
04371         fmode |= FMODE_CREATE;
04372     }
04373 #ifdef O_BINARY
04374     if (oflags & O_BINARY) {
04375         fmode |= FMODE_BINMODE;
04376     }
04377 #endif
04378 
04379     return fmode;
04380 }
04381 
04382 static int
04383 rb_io_fmode_oflags(int fmode)
04384 {
04385     int oflags = 0;
04386 
04387     switch (fmode & FMODE_READWRITE) {
04388       case FMODE_READABLE:
04389         oflags |= O_RDONLY;
04390         break;
04391       case FMODE_WRITABLE:
04392         oflags |= O_WRONLY;
04393         break;
04394       case FMODE_READWRITE:
04395         oflags |= O_RDWR;
04396         break;
04397     }
04398 
04399     if (fmode & FMODE_APPEND) {
04400         oflags |= O_APPEND;
04401     }
04402     if (fmode & FMODE_TRUNC) {
04403         oflags |= O_TRUNC;
04404     }
04405     if (fmode & FMODE_CREATE) {
04406         oflags |= O_CREAT;
04407     }
04408 #ifdef O_BINARY
04409     if (fmode & FMODE_BINMODE) {
04410         oflags |= O_BINARY;
04411     }
04412 #endif
04413 
04414     return oflags;
04415 }
04416 
04417 int
04418 rb_io_modestr_oflags(const char *modestr)
04419 {
04420     return rb_io_fmode_oflags(rb_io_modestr_fmode(modestr));
04421 }
04422 
04423 static const char*
04424 rb_io_oflags_modestr(int oflags)
04425 {
04426 #ifdef O_BINARY
04427 # define MODE_BINARY(a,b) ((oflags & O_BINARY) ? (b) : (a))
04428 #else
04429 # define MODE_BINARY(a,b) (a)
04430 #endif
04431     int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
04432     if (oflags & O_APPEND) {
04433         if (accmode == O_WRONLY) {
04434             return MODE_BINARY("a", "ab");
04435         }
04436         if (accmode == O_RDWR) {
04437             return MODE_BINARY("a+", "ab+");
04438         }
04439     }
04440     switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
04441       case O_RDONLY:
04442         return MODE_BINARY("r", "rb");
04443       case O_WRONLY:
04444         return MODE_BINARY("w", "wb");
04445       case O_RDWR:
04446         return MODE_BINARY("r+", "rb+");
04447     }
04448     rb_raise(rb_eArgError, "invalid access oflags 0x%x", oflags);
04449     return NULL;                /* not reached */
04450 }
04451 
04452 /*
04453  * Convert external/internal encodings to enc/enc2
04454  * NULL => use default encoding
04455  * Qnil => no encoding specified (internal only)
04456  */
04457 static void
04458 rb_io_ext_int_to_encs(rb_encoding *ext, rb_encoding *intern, rb_encoding **enc, rb_encoding **enc2)
04459 {
04460     int default_ext = 0;
04461 
04462     if (ext == NULL) {
04463         ext = rb_default_external_encoding();
04464         default_ext = 1;
04465     }
04466     if (intern == NULL && ext != rb_ascii8bit_encoding())
04467         /* If external is ASCII-8BIT, no default transcoding */
04468         intern = rb_default_internal_encoding();
04469     if (intern == NULL || intern == (rb_encoding *)Qnil || intern == ext) {
04470         /* No internal encoding => use external + no transcoding */
04471         *enc = (default_ext && intern != ext) ? NULL : ext;
04472         *enc2 = NULL;
04473     }
04474     else {
04475         *enc = intern;
04476         *enc2 = ext;
04477     }
04478 }
04479 
04480 static void
04481 parse_mode_enc(const char *estr, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
04482 {
04483     const char *p;
04484     char encname[ENCODING_MAXNAMELEN+1];
04485     int idx, idx2;
04486     rb_encoding *ext_enc, *int_enc;
04487 
04488     /* parse estr as "enc" or "enc2:enc" or "enc:-" */
04489 
04490     p = strrchr(estr, ':');
04491     if (p) {
04492         long len = (p++) - estr;
04493         if (len == 0 || len > ENCODING_MAXNAMELEN)
04494             idx = -1;
04495         else {
04496             if (io_encname_bom_p(estr, len)) {
04497                 if (fmode_p) *fmode_p |= FMODE_SETENC_BY_BOM;
04498                 estr += 4;
04499                 len -= 4;
04500             }
04501             memcpy(encname, estr, len);
04502             encname[len] = '\0';
04503             estr = encname;
04504             idx = rb_enc_find_index(encname);
04505         }
04506     }
04507     else {
04508         long len = strlen(estr);
04509         if (io_encname_bom_p(estr, len)) {
04510             if (fmode_p) *fmode_p |= FMODE_SETENC_BY_BOM;
04511             estr += 4;
04512             len -= 4;
04513             memcpy(encname, estr, len);
04514             encname[len] = '\0';
04515             estr = encname;
04516         }
04517         idx = rb_enc_find_index(estr);
04518     }
04519 
04520     if (idx >= 0)
04521         ext_enc = rb_enc_from_index(idx);
04522     else {
04523         if (idx != -2)
04524             rb_warn("Unsupported encoding %s ignored", estr);
04525         ext_enc = NULL;
04526     }
04527 
04528     int_enc = NULL;
04529     if (p) {
04530         if (*p == '-' && *(p+1) == '\0') {
04531             /* Special case - "-" => no transcoding */
04532             int_enc = (rb_encoding *)Qnil;
04533         }
04534         else {
04535             idx2 = rb_enc_find_index(p);
04536             if (idx2 < 0)
04537                 rb_warn("Unsupported encoding %s ignored", p);
04538             else if (idx2 == idx) {
04539                 rb_warn("Ignoring internal encoding %s: it is identical to external encoding %s", p, estr);
04540                 int_enc = (rb_encoding *)Qnil;
04541             }
04542             else
04543                 int_enc = rb_enc_from_index(idx2);
04544         }
04545     }
04546 
04547     rb_io_ext_int_to_encs(ext_enc, int_enc, enc_p, enc2_p);
04548 }
04549 
04550 static void
04551 mode_enc(rb_io_t *fptr, const char *estr)
04552 {
04553     clear_codeconv(fptr);
04554 
04555     parse_mode_enc(estr, &fptr->encs.enc, &fptr->encs.enc2, NULL);
04556 }
04557 
04558 static void
04559 rb_io_mode_enc(rb_io_t *fptr, const char *modestr)
04560 {
04561     const char *p = strchr(modestr, ':');
04562     if (p) {
04563         mode_enc(fptr, p+1);
04564     }
04565 }
04566 
04567 int
04568 rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
04569 {
04570     VALUE encoding=Qnil, extenc=Qundef, intenc=Qundef, tmp;
04571     int extracted = 0;
04572     rb_encoding *extencoding = NULL;
04573     rb_encoding *intencoding = NULL;
04574 
04575     if (!NIL_P(opt)) {
04576         VALUE v;
04577         v = rb_hash_lookup2(opt, sym_encoding, Qnil);
04578         if (v != Qnil) encoding = v;
04579         v = rb_hash_lookup2(opt, sym_extenc, Qundef);
04580         if (v != Qnil) extenc = v;
04581         v = rb_hash_lookup2(opt, sym_intenc, Qundef);
04582         if (v != Qundef) intenc = v;
04583     }
04584     if ((extenc != Qundef || intenc != Qundef) && !NIL_P(encoding)) {
04585         if (!NIL_P(ruby_verbose)) {
04586             int idx = rb_to_encoding_index(encoding);
04587             rb_warn("Ignoring encoding parameter '%s': %s_encoding is used",
04588                     idx < 0 ? StringValueCStr(encoding) : rb_enc_name(rb_enc_from_index(idx)),
04589                     extenc == Qundef ? "internal" : "external");
04590         }
04591         encoding = Qnil;
04592     }
04593     if (extenc != Qundef && !NIL_P(extenc)) {
04594         extencoding = rb_to_encoding(extenc);
04595     }
04596     if (intenc != Qundef) {
04597         if (NIL_P(intenc)) {
04598             /* internal_encoding: nil => no transcoding */
04599             intencoding = (rb_encoding *)Qnil;
04600         }
04601         else if (!NIL_P(tmp = rb_check_string_type(intenc))) {
04602             char *p = StringValueCStr(tmp);
04603 
04604             if (*p == '-' && *(p+1) == '\0') {
04605                 /* Special case - "-" => no transcoding */
04606                 intencoding = (rb_encoding *)Qnil;
04607             }
04608             else {
04609                 intencoding = rb_to_encoding(intenc);
04610             }
04611         }
04612         else {
04613             intencoding = rb_to_encoding(intenc);
04614         }
04615         if (extencoding == intencoding) {
04616             intencoding = (rb_encoding *)Qnil;
04617         }
04618     }
04619     if (!NIL_P(encoding)) {
04620         extracted = 1;
04621         if (!NIL_P(tmp = rb_check_string_type(encoding))) {
04622             parse_mode_enc(StringValueCStr(tmp), enc_p, enc2_p, fmode_p);
04623         }
04624         else {
04625             rb_io_ext_int_to_encs(rb_to_encoding(encoding), NULL, enc_p, enc2_p);
04626         }
04627     }
04628     else if (extenc != Qundef || intenc != Qundef) {
04629         extracted = 1;
04630         rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p);
04631     }
04632     return extracted;
04633 }
04634 
04635 typedef struct rb_io_enc_t convconfig_t;
04636 
04637 static void
04638 validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *enc2)
04639 {
04640     int fmode = *fmode_p;
04641 
04642     if ((fmode & FMODE_READABLE) &&
04643         !enc2 &&
04644         !(fmode & FMODE_BINMODE) &&
04645         !rb_enc_asciicompat(enc ? enc : rb_default_external_encoding()))
04646         rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode");
04647 
04648     if (!(fmode & FMODE_BINMODE) &&
04649         (DEFAULT_TEXTMODE || (ecflags & ECONV_NEWLINE_DECORATOR_MASK))) {
04650         fmode |= DEFAULT_TEXTMODE;
04651         *fmode_p = fmode;
04652     }
04653 #if !DEFAULT_TEXTMODE
04654     else if (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
04655         fmode &= ~FMODE_TEXTMODE;
04656         *fmode_p = fmode;
04657     }
04658 #endif
04659 }
04660 
04661 static void
04662 extract_binmode(VALUE opthash, int *fmode)
04663 {
04664     if (!NIL_P(opthash)) {
04665         VALUE v;
04666         v = rb_hash_aref(opthash, sym_textmode);
04667         if (!NIL_P(v) && RTEST(v))
04668             *fmode |= FMODE_TEXTMODE;
04669         v = rb_hash_aref(opthash, sym_binmode);
04670         if (!NIL_P(v) && RTEST(v))
04671             *fmode |= FMODE_BINMODE;
04672 
04673         if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE))
04674             rb_raise(rb_eArgError, "both textmode and binmode specified");
04675     }
04676 }
04677 
04678 static void
04679 rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
04680         int *oflags_p, int *fmode_p, convconfig_t *convconfig_p)
04681 {
04682     VALUE vmode;
04683     int oflags, fmode;
04684     rb_encoding *enc, *enc2;
04685     int ecflags;
04686     VALUE ecopts;
04687     int has_enc = 0, has_vmode = 0;
04688     VALUE intmode;
04689 
04690     vmode = *vmode_p;
04691 
04692     /* Set to defaults */
04693     rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2);
04694 
04695   vmode_handle:
04696     if (NIL_P(vmode)) {
04697         fmode = FMODE_READABLE | DEFAULT_TEXTMODE;
04698         oflags = O_RDONLY;
04699     }
04700     else if (!NIL_P(intmode = rb_check_to_integer(vmode, "to_int"))) {
04701         vmode = intmode;
04702         oflags = NUM2INT(intmode);
04703         fmode = rb_io_oflags_fmode(oflags);
04704     }
04705     else {
04706         const char *p;
04707 
04708         SafeStringValue(vmode);
04709         p = StringValueCStr(vmode);
04710         fmode = rb_io_modestr_fmode(p);
04711         oflags = rb_io_fmode_oflags(fmode);
04712         p = strchr(p, ':');
04713         if (p) {
04714             has_enc = 1;
04715             parse_mode_enc(p+1, &enc, &enc2, &fmode);
04716         }
04717         else {
04718             rb_encoding *e;
04719 
04720             e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
04721             rb_io_ext_int_to_encs(e, NULL, &enc, &enc2);
04722         }
04723     }
04724 
04725     if (NIL_P(opthash)) {
04726         ecflags = (fmode & FMODE_READABLE) ?
04727             MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
04728                         0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
04729 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
04730         ecflags |= (fmode & FMODE_WRITABLE) ?
04731             MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
04732                         0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
04733 #endif
04734         SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
04735         ecopts = Qnil;
04736     }
04737     else {
04738         VALUE v;
04739         extract_binmode(opthash, &fmode);
04740 #ifdef O_BINARY
04741         if (fmode & FMODE_BINMODE)
04742             oflags |= O_BINARY;
04743 #endif
04744         if (!has_vmode) {
04745             v = rb_hash_aref(opthash, sym_mode);
04746             if (!NIL_P(v)) {
04747                 if (!NIL_P(vmode)) {
04748                     rb_raise(rb_eArgError, "mode specified twice");
04749                 }
04750                 has_vmode = 1;
04751                 vmode = v;
04752                 goto vmode_handle;
04753             }
04754         }
04755         v = rb_hash_aref(opthash, sym_perm);
04756         if (!NIL_P(v)) {
04757             if (vperm_p) {
04758                 if (!NIL_P(*vperm_p)) {
04759                     rb_raise(rb_eArgError, "perm specified twice");
04760                 }
04761                 *vperm_p = v;
04762             }
04763             else {
04764                 /* perm no use, just ignore */
04765             }
04766         }
04767         ecflags = (fmode & FMODE_READABLE) ?
04768             MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
04769                         0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
04770 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
04771         ecflags |= (fmode & FMODE_WRITABLE) ?
04772             MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
04773                         0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
04774 #endif
04775 
04776         if (rb_io_extract_encoding_option(opthash, &enc, &enc2, &fmode)) {
04777             if (has_enc) {
04778                 rb_raise(rb_eArgError, "encoding specified twice");
04779             }
04780         }
04781         SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
04782         ecflags = rb_econv_prepare_options(opthash, &ecopts, ecflags);
04783     }
04784 
04785     validate_enc_binmode(&fmode, ecflags, enc, enc2);
04786 
04787     *vmode_p = vmode;
04788 
04789     *oflags_p = oflags;
04790     *fmode_p = fmode;
04791     convconfig_p->enc = enc;
04792     convconfig_p->enc2 = enc2;
04793     convconfig_p->ecflags = ecflags;
04794     convconfig_p->ecopts = ecopts;
04795 }
04796 
04797 struct sysopen_struct {
04798     VALUE fname;
04799     int oflags;
04800     mode_t perm;
04801 };
04802 
04803 static VALUE
04804 sysopen_func(void *ptr)
04805 {
04806     const struct sysopen_struct *data = ptr;
04807     const char *fname = RSTRING_PTR(data->fname);
04808     return (VALUE)open(fname, data->oflags, data->perm);
04809 }
04810 
04811 static inline int
04812 rb_sysopen_internal(struct sysopen_struct *data)
04813 {
04814     int fd;
04815     fd = (int)rb_thread_blocking_region(sysopen_func, data, RUBY_UBF_IO, 0);
04816     if (0 <= fd)
04817         rb_update_max_fd(fd);
04818     return fd;
04819 }
04820 
04821 static int
04822 rb_sysopen(VALUE fname, int oflags, mode_t perm)
04823 {
04824     int fd;
04825     struct sysopen_struct data;
04826 
04827     data.fname = rb_str_encode_ospath(fname);
04828     data.oflags = oflags;
04829     data.perm = perm;
04830 
04831     fd = rb_sysopen_internal(&data);
04832     if (fd < 0) {
04833         if (errno == EMFILE || errno == ENFILE) {
04834             rb_gc();
04835             fd = rb_sysopen_internal(&data);
04836         }
04837         if (fd < 0) {
04838             rb_sys_fail_path(fname);
04839         }
04840     }
04841     rb_update_max_fd(fd);
04842     return fd;
04843 }
04844 
04845 FILE *
04846 rb_fdopen(int fd, const char *modestr)
04847 {
04848     FILE *file;
04849 
04850 #if defined(sun)
04851     errno = 0;
04852 #endif
04853     file = fdopen(fd, modestr);
04854     if (!file) {
04855         if (
04856 #if defined(sun)
04857             errno == 0 ||
04858 #endif
04859             errno == EMFILE || errno == ENFILE) {
04860             rb_gc();
04861 #if defined(sun)
04862             errno = 0;
04863 #endif
04864             file = fdopen(fd, modestr);
04865         }
04866         if (!file) {
04867 #ifdef _WIN32
04868             if (errno == 0) errno = EINVAL;
04869 #elif defined(sun)
04870             if (errno == 0) errno = EMFILE;
04871 #endif
04872             rb_sys_fail(0);
04873         }
04874     }
04875 
04876     /* xxx: should be _IONBF?  A buffer in FILE may have trouble. */
04877 #ifdef USE_SETVBUF
04878     if (setvbuf(file, NULL, _IOFBF, 0) != 0)
04879         rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
04880 #endif
04881     return file;
04882 }
04883 
04884 static void
04885 io_check_tty(rb_io_t *fptr)
04886 {
04887     if (isatty(fptr->fd))
04888         fptr->mode |= FMODE_TTY|FMODE_DUPLEX;
04889 }
04890 
04891 static VALUE rb_io_internal_encoding(VALUE);
04892 static void io_encoding_set(rb_io_t *, VALUE, VALUE, VALUE);
04893 
04894 static int
04895 io_strip_bom(VALUE io)
04896 {
04897     int b1, b2, b3, b4;
04898     switch (b1 = FIX2INT(rb_io_getbyte(io))) {
04899       case 0xEF:
04900         b2 = FIX2INT(rb_io_getbyte(io));
04901         if (b2 == 0xBB) {
04902             b3 = FIX2INT(rb_io_getbyte(io));
04903             if (b3 == 0xBF) {
04904                 return rb_utf8_encindex();
04905             }
04906             rb_io_ungetbyte(io, INT2FIX(b3));
04907         }
04908         rb_io_ungetbyte(io, INT2FIX(b2));
04909         break;
04910 
04911       case 0xFE:
04912         b2 = FIX2INT(rb_io_getbyte(io));
04913         if (b2 == 0xFF) {
04914             return rb_enc_find_index("UTF-16BE");
04915         }
04916         rb_io_ungetbyte(io, INT2FIX(b2));
04917         break;
04918 
04919       case 0xFF:
04920         b2 = FIX2INT(rb_io_getbyte(io));
04921         if (b2 == 0xFE) {
04922             b3 = FIX2INT(rb_io_getbyte(io));
04923             if (b3 == 0) {
04924                 b4 = FIX2INT(rb_io_getbyte(io));
04925                 if (b4 == 0) {
04926                     return rb_enc_find_index("UTF-32LE");
04927                 }
04928                 rb_io_ungetbyte(io, INT2FIX(b4));
04929             }
04930             else {
04931                 rb_io_ungetbyte(io, INT2FIX(b3));
04932                 return rb_enc_find_index("UTF-16LE");
04933             }
04934             rb_io_ungetbyte(io, INT2FIX(b3));
04935         }
04936         rb_io_ungetbyte(io, INT2FIX(b2));
04937         break;
04938 
04939       case 0:
04940         b2 = FIX2INT(rb_io_getbyte(io));
04941         if (b2 == 0) {
04942             b3 = FIX2INT(rb_io_getbyte(io));
04943             if (b3 == 0xFE) {
04944                 b4 = FIX2INT(rb_io_getbyte(io));
04945                 if (b4 == 0xFF) {
04946                     return rb_enc_find_index("UTF-32BE");
04947                 }
04948                 rb_io_ungetbyte(io, INT2FIX(b4));
04949             }
04950             rb_io_ungetbyte(io, INT2FIX(b3));
04951         }
04952         rb_io_ungetbyte(io, INT2FIX(b2));
04953         break;
04954     }
04955     rb_io_ungetbyte(io, INT2FIX(b1));
04956     return 0;
04957 }
04958 
04959 static void
04960 io_set_encoding_by_bom(VALUE io)
04961 {
04962     int idx = io_strip_bom(io);
04963 
04964     if (idx) {
04965         rb_io_t *fptr;
04966         GetOpenFile(io, fptr);
04967         io_encoding_set(fptr, rb_enc_from_encoding(rb_enc_from_index(idx)),
04968                 rb_io_internal_encoding(io), Qnil);
04969     }
04970 }
04971 
04972 static VALUE
04973 rb_file_open_generic(VALUE io, VALUE filename, int oflags, int fmode, convconfig_t *convconfig, mode_t perm)
04974 {
04975     rb_io_t *fptr;
04976     convconfig_t cc;
04977     if (!convconfig) {
04978         /* Set to default encodings */
04979         rb_io_ext_int_to_encs(NULL, NULL, &cc.enc, &cc.enc2);
04980         cc.ecflags = 0;
04981         cc.ecopts = Qnil;
04982         convconfig = &cc;
04983     }
04984     validate_enc_binmode(&fmode, convconfig->ecflags,
04985                          convconfig->enc, convconfig->enc2);
04986 
04987     MakeOpenFile(io, fptr);
04988     fptr->mode = fmode;
04989     fptr->encs = *convconfig;
04990     fptr->pathv = rb_str_new_frozen(filename);
04991     fptr->fd = rb_sysopen(fptr->pathv, oflags, perm);
04992     io_check_tty(fptr);
04993     if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
04994 
04995     return io;
04996 }
04997 
04998 static VALUE
04999 rb_file_open_internal(VALUE io, VALUE filename, const char *modestr)
05000 {
05001     int fmode = rb_io_modestr_fmode(modestr);
05002     const char *p = strchr(modestr, ':');
05003     convconfig_t convconfig;
05004 
05005     if (p) {
05006         parse_mode_enc(p+1, &convconfig.enc, &convconfig.enc2, &fmode);
05007     }
05008     else {
05009         rb_encoding *e;
05010         /* Set to default encodings */
05011 
05012         e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
05013         rb_io_ext_int_to_encs(e, NULL, &convconfig.enc, &convconfig.enc2);
05014         convconfig.ecflags = 0;
05015         convconfig.ecopts = Qnil;
05016     }
05017 
05018     return rb_file_open_generic(io, filename,
05019             rb_io_fmode_oflags(fmode),
05020             fmode,
05021             &convconfig,
05022             0666);
05023 }
05024 
05025 VALUE
05026 rb_file_open_str(VALUE fname, const char *modestr)
05027 {
05028     FilePathValue(fname);
05029     return rb_file_open_internal(io_alloc(rb_cFile), fname, modestr);
05030 }
05031 
05032 VALUE
05033 rb_file_open(const char *fname, const char *modestr)
05034 {
05035     return rb_file_open_internal(io_alloc(rb_cFile), rb_str_new_cstr(fname), modestr);
05036 }
05037 
05038 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
05039 static struct pipe_list {
05040     rb_io_t *fptr;
05041     struct pipe_list *next;
05042 } *pipe_list;
05043 
05044 static void
05045 pipe_add_fptr(rb_io_t *fptr)
05046 {
05047     struct pipe_list *list;
05048 
05049     list = ALLOC(struct pipe_list);
05050     list->fptr = fptr;
05051     list->next = pipe_list;
05052     pipe_list = list;
05053 }
05054 
05055 static void
05056 pipe_del_fptr(rb_io_t *fptr)
05057 {
05058     struct pipe_list *list = pipe_list;
05059     struct pipe_list *tmp;
05060 
05061     if (list->fptr == fptr) {
05062         pipe_list = list->next;
05063         free(list);
05064         return;
05065     }
05066 
05067     while (list->next) {
05068         if (list->next->fptr == fptr) {
05069             tmp = list->next;
05070             list->next = list->next->next;
05071             free(tmp);
05072             return;
05073         }
05074         list = list->next;
05075     }
05076 }
05077 
05078 static void
05079 pipe_atexit(void)
05080 {
05081     struct pipe_list *list = pipe_list;
05082     struct pipe_list *tmp;
05083 
05084     while (list) {
05085         tmp = list->next;
05086         rb_io_fptr_finalize(list->fptr);
05087         list = tmp;
05088     }
05089 }
05090 
05091 static void
05092 pipe_finalize(rb_io_t *fptr, int noraise)
05093 {
05094 #if !defined(HAVE_FORK) && !defined(_WIN32)
05095     int status = 0;
05096     if (fptr->stdio_file) {
05097         status = pclose(fptr->stdio_file);
05098     }
05099     fptr->fd = -1;
05100     fptr->stdio_file = 0;
05101     rb_last_status_set(status, fptr->pid);
05102 #else
05103     fptr_finalize(fptr, noraise);
05104 #endif
05105     pipe_del_fptr(fptr);
05106 }
05107 #endif
05108 
05109 void
05110 rb_io_synchronized(rb_io_t *fptr)
05111 {
05112     rb_io_check_initialized(fptr);
05113     fptr->mode |= FMODE_SYNC;
05114 }
05115 
05116 void
05117 rb_io_unbuffered(rb_io_t *fptr)
05118 {
05119     rb_io_synchronized(fptr);
05120 }
05121 
05122 int
05123 rb_pipe(int *pipes)
05124 {
05125     int ret;
05126     ret = pipe(pipes);
05127     if (ret == -1) {
05128         if (errno == EMFILE || errno == ENFILE) {
05129             rb_gc();
05130             ret = pipe(pipes);
05131         }
05132     }
05133     if (ret == 0) {
05134         rb_update_max_fd(pipes[0]);
05135         rb_update_max_fd(pipes[1]);
05136     }
05137     return ret;
05138 }
05139 
05140 #ifdef HAVE_FORK
05141 struct popen_arg {
05142     struct rb_exec_arg *execp;
05143     int modef;
05144     int pair[2];
05145     int write_pair[2];
05146 };
05147 
05148 static void
05149 popen_redirect(struct popen_arg *p)
05150 {
05151     if ((p->modef & FMODE_READABLE) && (p->modef & FMODE_WRITABLE)) {
05152         close(p->write_pair[1]);
05153         if (p->write_pair[0] != 0) {
05154             dup2(p->write_pair[0], 0);
05155             close(p->write_pair[0]);
05156         }
05157         close(p->pair[0]);
05158         if (p->pair[1] != 1) {
05159             dup2(p->pair[1], 1);
05160             close(p->pair[1]);
05161         }
05162     }
05163     else if (p->modef & FMODE_READABLE) {
05164         close(p->pair[0]);
05165         if (p->pair[1] != 1) {
05166             dup2(p->pair[1], 1);
05167             close(p->pair[1]);
05168         }
05169     }
05170     else {
05171         close(p->pair[1]);
05172         if (p->pair[0] != 0) {
05173             dup2(p->pair[0], 0);
05174             close(p->pair[0]);
05175         }
05176     }
05177 }
05178 
05179 void
05180 rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
05181 {
05182     int fd, ret;
05183     int max = max_file_descriptor;
05184     if (max < maxhint)
05185         max = maxhint;
05186     for (fd = lowfd; fd <= max; fd++) {
05187         if (!NIL_P(noclose_fds) &&
05188             RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd))))
05189             continue;
05190 #ifdef FD_CLOEXEC
05191         ret = fcntl(fd, F_GETFD);
05192         if (ret != -1 && !(ret & FD_CLOEXEC)) {
05193             fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
05194         }
05195 #else
05196         ret = close(fd);
05197 #endif
05198 #define CONTIGUOUS_CLOSED_FDS 20
05199         if (ret != -1) {
05200             if (max < fd + CONTIGUOUS_CLOSED_FDS)
05201                 max = fd + CONTIGUOUS_CLOSED_FDS;
05202         }
05203     }
05204 }
05205 
05206 static int
05207 popen_exec(void *pp, char *errmsg, size_t errmsg_len)
05208 {
05209     struct popen_arg *p = (struct popen_arg*)pp;
05210 
05211     rb_thread_atfork_before_exec();
05212     return rb_exec_err(p->execp, errmsg, errmsg_len);
05213 }
05214 #endif
05215 
05216 static VALUE
05217 pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, convconfig_t *convconfig)
05218 {
05219     rb_pid_t pid = 0;
05220     rb_io_t *fptr;
05221     VALUE port;
05222     rb_io_t *write_fptr;
05223     VALUE write_port;
05224 #if defined(HAVE_FORK)
05225     int status;
05226     struct popen_arg arg;
05227     char errmsg[80] = { '\0' };
05228 #elif defined(_WIN32)
05229     volatile VALUE argbuf;
05230     char **args = NULL;
05231     int pair[2], write_pair[2];
05232 #endif
05233 #if !defined(HAVE_FORK)
05234     struct rb_exec_arg sarg;
05235 #endif
05236     FILE *fp = 0;
05237     int fd = -1;
05238     int write_fd = -1;
05239     const char *cmd = 0;
05240     int argc;
05241     VALUE *argv;
05242 
05243     if (prog)
05244         cmd = StringValueCStr(prog);
05245 
05246     if (!eargp) {
05247         /* fork : IO.popen("-") */
05248         argc = 0;
05249         argv = 0;
05250     }
05251     else if (eargp->argc) {
05252         /* no shell : IO.popen([prog, arg0], arg1, ...) */
05253         argc = eargp->argc;
05254         argv = eargp->argv;
05255     }
05256     else {
05257         /* with shell : IO.popen(prog) */
05258         argc = 0;
05259         argv = 0;
05260     }
05261 
05262 #if defined(HAVE_FORK)
05263     arg.execp = eargp;
05264     arg.modef = fmode;
05265     arg.pair[0] = arg.pair[1] = -1;
05266     arg.write_pair[0] = arg.write_pair[1] = -1;
05267     switch (fmode & (FMODE_READABLE|FMODE_WRITABLE)) {
05268       case FMODE_READABLE|FMODE_WRITABLE:
05269         if (rb_pipe(arg.write_pair) < 0)
05270             rb_sys_fail(cmd);
05271         if (rb_pipe(arg.pair) < 0) {
05272             int e = errno;
05273             close(arg.write_pair[0]);
05274             close(arg.write_pair[1]);
05275             errno = e;
05276             rb_sys_fail(cmd);
05277         }
05278         if (eargp) {
05279             rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.write_pair[0]));
05280             rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
05281         }
05282         break;
05283       case FMODE_READABLE:
05284         if (rb_pipe(arg.pair) < 0)
05285             rb_sys_fail(cmd);
05286         if (eargp)
05287             rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
05288         break;
05289       case FMODE_WRITABLE:
05290         if (rb_pipe(arg.pair) < 0)
05291             rb_sys_fail(cmd);
05292         if (eargp)
05293             rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.pair[0]));
05294         break;
05295       default:
05296         rb_sys_fail(cmd);
05297     }
05298     if (eargp) {
05299         rb_exec_arg_fixup(arg.execp);
05300         pid = rb_fork_err(&status, popen_exec, &arg, arg.execp->redirect_fds, errmsg, sizeof(errmsg));
05301     }
05302     else {
05303         fflush(stdin);          /* is it really needed? */
05304         pid = rb_fork(&status, 0, 0, Qnil);
05305         if (pid == 0) {         /* child */
05306             rb_thread_atfork();
05307             popen_redirect(&arg);
05308             rb_io_synchronized(RFILE(orig_stdout)->fptr);
05309             rb_io_synchronized(RFILE(orig_stderr)->fptr);
05310             return Qnil;
05311         }
05312     }
05313 
05314     /* parent */
05315     if (pid == -1) {
05316         int e = errno;
05317         close(arg.pair[0]);
05318         close(arg.pair[1]);
05319         if ((fmode & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
05320             close(arg.write_pair[0]);
05321             close(arg.write_pair[1]);
05322         }
05323         errno = e;
05324         if (errmsg[0])
05325             rb_sys_fail(errmsg);
05326         rb_sys_fail(cmd);
05327     }
05328     if ((fmode & FMODE_READABLE) && (fmode & FMODE_WRITABLE)) {
05329         close(arg.pair[1]);
05330         fd = arg.pair[0];
05331         close(arg.write_pair[0]);
05332         write_fd = arg.write_pair[1];
05333     }
05334     else if (fmode & FMODE_READABLE) {
05335         close(arg.pair[1]);
05336         fd = arg.pair[0];
05337     }
05338     else {
05339         close(arg.pair[0]);
05340         fd = arg.pair[1];
05341     }
05342 #elif defined(_WIN32)
05343     if (argc) {
05344         int i;
05345 
05346         if (argc >= (int)(FIXNUM_MAX / sizeof(char *))) {
05347             rb_raise(rb_eArgError, "too many arguments");
05348         }
05349         argbuf = rb_str_tmp_new((argc+1) * sizeof(char *));
05350         args = (void *)RSTRING_PTR(argbuf);
05351         for (i = 0; i < argc; ++i) {
05352             args[i] = StringValueCStr(argv[i]);
05353         }
05354         args[i] = NULL;
05355     }
05356     switch (fmode & (FMODE_READABLE|FMODE_WRITABLE)) {
05357       case FMODE_READABLE|FMODE_WRITABLE:
05358         if (rb_pipe(write_pair) < 0)
05359             rb_sys_fail(cmd);
05360         if (rb_pipe(pair) < 0) {
05361             int e = errno;
05362             close(write_pair[0]);
05363             close(write_pair[1]);
05364             errno = e;
05365             rb_sys_fail(cmd);
05366         }
05367         if (eargp) {
05368             rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(write_pair[0]));
05369             rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(pair[1]));
05370         }
05371         break;
05372       case FMODE_READABLE:
05373         if (rb_pipe(pair) < 0)
05374             rb_sys_fail(cmd);
05375         if (eargp)
05376             rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(pair[1]));
05377         break;
05378       case FMODE_WRITABLE:
05379         if (rb_pipe(pair) < 0)
05380             rb_sys_fail(cmd);
05381         if (eargp)
05382             rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(pair[0]));
05383         break;
05384       default:
05385         rb_sys_fail(cmd);
05386     }
05387     if (eargp) {
05388         rb_exec_arg_fixup(eargp);
05389         rb_run_exec_options(eargp, &sarg);
05390     }
05391     while ((pid = (args ?
05392                    rb_w32_aspawn(P_NOWAIT, cmd, args) :
05393                    rb_w32_spawn(P_NOWAIT, cmd, 0))) == -1) {
05394         /* exec failed */
05395         switch (errno) {
05396           case EAGAIN:
05397 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
05398           case EWOULDBLOCK:
05399 #endif
05400             rb_thread_sleep(1);
05401             break;
05402           default:
05403             {
05404                 int e = errno;
05405                 if (eargp)
05406                     rb_run_exec_options(&sarg, NULL);
05407                 close(pair[0]);
05408                 close(pair[1]);
05409                 if ((fmode & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
05410                     close(write_pair[0]);
05411                     close(write_pair[1]);
05412                 }
05413                 errno = e;
05414                 rb_sys_fail(cmd);
05415             }
05416             break;
05417         }
05418     }
05419 
05420     RB_GC_GUARD(argbuf);
05421 
05422     if (eargp)
05423         rb_run_exec_options(&sarg, NULL);
05424     if ((fmode & FMODE_READABLE) && (fmode & FMODE_WRITABLE)) {
05425         close(pair[1]);
05426         fd = pair[0];
05427         close(write_pair[0]);
05428         write_fd = write_pair[1];
05429     }
05430     else if (fmode & FMODE_READABLE) {
05431         close(pair[1]);
05432         fd = pair[0];
05433     }
05434     else {
05435         close(pair[0]);
05436         fd = pair[1];
05437     }
05438 #else
05439     if (argc) {
05440         prog = rb_ary_join(rb_ary_new4(argc, argv), rb_str_new2(" "));
05441         cmd = StringValueCStr(prog);
05442     }
05443     if (eargp) {
05444         rb_exec_arg_fixup(eargp);
05445         rb_run_exec_options(eargp, &sarg);
05446     }
05447     fp = popen(cmd, modestr);
05448     if (eargp)
05449         rb_run_exec_options(&sarg, NULL);
05450     if (!fp) rb_sys_fail_path(prog);
05451     fd = fileno(fp);
05452 #endif
05453 
05454     port = io_alloc(rb_cIO);
05455     MakeOpenFile(port, fptr);
05456     fptr->fd = fd;
05457     fptr->stdio_file = fp;
05458     fptr->mode = fmode | FMODE_SYNC|FMODE_DUPLEX;
05459     if (convconfig) {
05460         fptr->encs = *convconfig;
05461 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
05462         if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
05463             fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
05464         }
05465 #endif
05466     }
05467     else {
05468         if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
05469             fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
05470         }
05471 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
05472         if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr)) {
05473             fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
05474         }
05475 #endif
05476     }
05477     fptr->pid = pid;
05478 
05479     if (0 <= write_fd) {
05480         write_port = io_alloc(rb_cIO);
05481         MakeOpenFile(write_port, write_fptr);
05482         write_fptr->fd = write_fd;
05483         write_fptr->mode = (fmode & ~FMODE_READABLE)| FMODE_SYNC|FMODE_DUPLEX;
05484         fptr->mode &= ~FMODE_WRITABLE;
05485         fptr->tied_io_for_writing = write_port;
05486         rb_ivar_set(port, rb_intern("@tied_io_for_writing"), write_port);
05487     }
05488 
05489 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
05490     fptr->finalize = pipe_finalize;
05491     pipe_add_fptr(fptr);
05492 #endif
05493     return port;
05494 }
05495 
05496 static VALUE
05497 pipe_open_v(int argc, VALUE *argv, const char *modestr, int fmode, convconfig_t *convconfig)
05498 {
05499     VALUE prog;
05500     struct rb_exec_arg earg;
05501     prog = rb_exec_arg_init(argc, argv, FALSE, &earg);
05502     return pipe_open(&earg, prog, modestr, fmode, convconfig);
05503 }
05504 
05505 static VALUE
05506 pipe_open_s(VALUE prog, const char *modestr, int fmode, convconfig_t *convconfig)
05507 {
05508     const char *cmd = RSTRING_PTR(prog);
05509     int argc = 1;
05510     VALUE *argv = &prog;
05511     struct rb_exec_arg earg;
05512 
05513     if (RSTRING_LEN(prog) == 1 && cmd[0] == '-') {
05514 #if !defined(HAVE_FORK)
05515         rb_raise(rb_eNotImpError,
05516                  "fork() function is unimplemented on this machine");
05517 #endif
05518         return pipe_open(0, 0, modestr, fmode, convconfig);
05519     }
05520 
05521     rb_exec_arg_init(argc, argv, TRUE, &earg);
05522     return pipe_open(&earg, prog, modestr, fmode, convconfig);
05523 }
05524 
05525 /*
05526  *  call-seq:
05527  *     IO.popen(cmd, mode="r" [, opt])               -> io
05528  *     IO.popen(cmd, mode="r" [, opt]) {|io| block } -> obj
05529  *
05530  *  Runs the specified command as a subprocess; the subprocess's
05531  *  standard input and output will be connected to the returned
05532  *  <code>IO</code> object.
05533  *
05534  *  The PID of the started process can be obtained by IO#pid method.
05535  *
05536  *  _cmd_ is a string or an array as follows.
05537  *
05538  *    cmd:
05539  *      "-"                                      : fork
05540  *      commandline                              : command line string which is passed to a shell
05541  *      [env, cmdname, arg1, ..., opts]          : command name and zero or more arguments (no shell)
05542  *      [env, [cmdname, argv0], arg1, ..., opts] : command name, argv[0] and zero or more arguments (no shell)
05543  *    (env and opts are optional.)
05544  *
05545  *  If _cmd_ is a +String+ ``<code>-</code>'',
05546  *  then a new instance of Ruby is started as the subprocess.
05547  *
05548  *  If <i>cmd</i> is an +Array+ of +String+,
05549  *  then it will be used as the subprocess's +argv+ bypassing a shell.
05550  *  The array can contains a hash at first for environments and
05551  *  a hash at last for options similar to <code>spawn</code>.
05552  *
05553  *  The default mode for the new file object is ``r'',
05554  *  but <i>mode</i> may be set to any of the modes listed in the description for class IO.
05555  *  The last argument <i>opt</i> qualifies <i>mode</i>.
05556  *
05557  *    # set IO encoding
05558  *    IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
05559  *      euc_jp_string = nkf_io.read
05560  *    }
05561  *
05562  *    # merge standard output and standard error using
05563  *    # spawn option.  See the document of Kernel.spawn.
05564  *    IO.popen(["ls", "/", :err=>[:child, :out]]) {|ls_io|
05565  *      ls_result_with_error = ls_io.read
05566  *    }
05567  *
05568  *  Raises exceptions which <code>IO.pipe</code> and
05569  *  <code>Kernel.spawn</code> raise.
05570  *
05571  *  If a block is given, Ruby will run the command as a child connected
05572  *  to Ruby with a pipe. Ruby's end of the pipe will be passed as a
05573  *  parameter to the block.
05574  *  At the end of block, Ruby close the pipe and sets <code>$?</code>.
05575  *  In this case <code>IO.popen</code> returns
05576  *  the value of the block.
05577  *
05578  *  If a block is given with a _cmd_ of ``<code>-</code>'',
05579  *  the block will be run in two separate processes: once in the parent,
05580  *  and once in a child. The parent process will be passed the pipe
05581  *  object as a parameter to the block, the child version of the block
05582  *  will be passed <code>nil</code>, and the child's standard in and
05583  *  standard out will be connected to the parent through the pipe. Not
05584  *  available on all platforms.
05585  *
05586  *     f = IO.popen("uname")
05587  *     p f.readlines
05588  *     f.close
05589  *     puts "Parent is #{Process.pid}"
05590  *     IO.popen("date") { |f| puts f.gets }
05591  *     IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
05592  *     p $?
05593  *     IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
05594  *       f.puts "bar"; f.close_write; puts f.gets
05595  *     }
05596  *
05597  *  <em>produces:</em>
05598  *
05599  *     ["Linux\n"]
05600  *     Parent is 21346
05601  *     Thu Jan 15 22:41:19 JST 2009
05602  *     21346 is here, f is #<IO:fd 3>
05603  *     21352 is here, f is nil
05604  *     #<Process::Status: pid 21352 exit 0>
05605  *     <foo>bar;zot;
05606  */
05607 
05608 static VALUE
05609 rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
05610 {
05611     const char *modestr;
05612     VALUE pname, pmode, port, tmp, opt;
05613     int oflags, fmode;
05614     convconfig_t convconfig;
05615 
05616     argc = rb_scan_args(argc, argv, "11:", &pname, &pmode, &opt);
05617 
05618     rb_io_extract_modeenc(&pmode, 0, opt, &oflags, &fmode, &convconfig);
05619     modestr = rb_io_oflags_modestr(oflags);
05620 
05621     tmp = rb_check_array_type(pname);
05622     if (!NIL_P(tmp)) {
05623         long len = RARRAY_LEN(tmp);
05624 #if SIZEOF_LONG > SIZEOF_INT
05625         if (len > INT_MAX) {
05626             rb_raise(rb_eArgError, "too many arguments");
05627         }
05628 #endif
05629         tmp = rb_ary_dup(tmp);
05630         RBASIC(tmp)->klass = 0;
05631         port = pipe_open_v((int)len, RARRAY_PTR(tmp), modestr, fmode, &convconfig);
05632         rb_ary_clear(tmp);
05633     }
05634     else {
05635         SafeStringValue(pname);
05636         port = pipe_open_s(pname, modestr, fmode, &convconfig);
05637     }
05638     if (NIL_P(port)) {
05639         /* child */
05640         if (rb_block_given_p()) {
05641             rb_yield(Qnil);
05642             rb_io_flush(rb_stdout);
05643             rb_io_flush(rb_stderr);
05644             _exit(0);
05645         }
05646         return Qnil;
05647     }
05648     RBASIC(port)->klass = klass;
05649     if (rb_block_given_p()) {
05650         return rb_ensure(rb_yield, port, io_close, port);
05651     }
05652     return port;
05653 }
05654 
05655 static void
05656 rb_scan_open_args(int argc, VALUE *argv,
05657         VALUE *fname_p, int *oflags_p, int *fmode_p,
05658         convconfig_t *convconfig_p, mode_t *perm_p)
05659 {
05660     VALUE opt, fname, vmode, vperm;
05661     int oflags, fmode;
05662     mode_t perm;
05663 
05664     argc = rb_scan_args(argc, argv, "12:", &fname, &vmode, &vperm, &opt);
05665     FilePathValue(fname);
05666 
05667     rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, convconfig_p);
05668 
05669     perm = NIL_P(vperm) ? 0666 :  NUM2MODET(vperm);
05670 
05671     *fname_p = fname;
05672     *oflags_p = oflags;
05673     *fmode_p = fmode;
05674     *perm_p = perm;
05675 }
05676 
05677 static VALUE
05678 rb_open_file(int argc, VALUE *argv, VALUE io)
05679 {
05680     VALUE fname;
05681     int oflags, fmode;
05682     convconfig_t convconfig;
05683     mode_t perm;
05684 
05685     rb_scan_open_args(argc, argv, &fname, &oflags, &fmode, &convconfig, &perm);
05686     rb_file_open_generic(io, fname, oflags, fmode, &convconfig, perm);
05687 
05688     return io;
05689 }
05690 
05691 
05692 /*
05693  *  Document-method: File::open
05694  *
05695  *  call-seq:
05696  *     File.open(filename, mode="r" [, opt])                 -> file
05697  *     File.open(filename [, mode [, perm]] [, opt])         -> file
05698  *     File.open(filename, mode="r" [, opt]) {|file| block } -> obj
05699  *     File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj
05700  *
05701  *  With no associated block, <code>File.open</code> is a synonym for
05702  *  File.new. If the optional code block is given, it will
05703  *  be passed the opened +file+ as an argument, and the File object will
05704  *  automatically be closed when the block terminates.  In this instance,
05705  *  <code>File.open</code> returns the value of the block.
05706  *
05707  *  See IO.new for a list of values for the +opt+ parameter.
05708  */
05709 
05710 /*
05711  *  Document-method: IO::open
05712  *
05713  *  call-seq:
05714  *     IO.open(fd, mode_string="r" [, opt])               -> io
05715  *     IO.open(fd, mode_string="r" [, opt]) {|io| block } -> obj
05716  *
05717  *  With no associated block, <code>IO.open</code> is a synonym for IO.new. If
05718  *  the optional code block is given, it will be passed +io+ as an
05719  *  argument, and the IO object will automatically be closed when the block
05720  *  terminates. In this instance, IO.open returns the value of the block.
05721  *
05722  *  See IO.new for a description of values for the +opt+ parameter.
05723  *
05724  */
05725 
05726 static VALUE
05727 rb_io_s_open(int argc, VALUE *argv, VALUE klass)
05728 {
05729     VALUE io = rb_class_new_instance(argc, argv, klass);
05730 
05731     if (rb_block_given_p()) {
05732         return rb_ensure(rb_yield, io, io_close, io);
05733     }
05734 
05735     return io;
05736 }
05737 
05738 /*
05739  *  call-seq:
05740  *     IO.sysopen(path, [mode, [perm]])  -> fixnum
05741  *
05742  *  Opens the given path, returning the underlying file descriptor as a
05743  *  <code>Fixnum</code>.
05744  *
05745  *     IO.sysopen("testfile")   #=> 3
05746  *
05747  */
05748 
05749 static VALUE
05750 rb_io_s_sysopen(int argc, VALUE *argv)
05751 {
05752     VALUE fname, vmode, vperm;
05753     VALUE intmode;
05754     int oflags, fd;
05755     mode_t perm;
05756 
05757     rb_scan_args(argc, argv, "12", &fname, &vmode, &vperm);
05758     FilePathValue(fname);
05759 
05760     if (NIL_P(vmode))
05761         oflags = O_RDONLY;
05762     else if (!NIL_P(intmode = rb_check_to_integer(vmode, "to_int")))
05763         oflags = NUM2INT(intmode);
05764     else {
05765         SafeStringValue(vmode);
05766         oflags = rb_io_modestr_oflags(StringValueCStr(vmode));
05767     }
05768     if (NIL_P(vperm)) perm = 0666;
05769     else              perm = NUM2MODET(vperm);
05770 
05771     RB_GC_GUARD(fname) = rb_str_new4(fname);
05772     fd = rb_sysopen(fname, oflags, perm);
05773     return INT2NUM(fd);
05774 }
05775 
05776 static VALUE
05777 check_pipe_command(VALUE filename_or_command)
05778 {
05779     char *s = RSTRING_PTR(filename_or_command);
05780     long l = RSTRING_LEN(filename_or_command);
05781     char *e = s + l;
05782     int chlen;
05783 
05784     if (rb_enc_ascget(s, e, &chlen, rb_enc_get(filename_or_command)) == '|') {
05785         VALUE cmd = rb_str_new(s+chlen, l-chlen);
05786         OBJ_INFECT(cmd, filename_or_command);
05787         return cmd;
05788     }
05789     return Qnil;
05790 }
05791 
05792 /*
05793  *  call-seq:
05794  *     open(path [, mode_enc [, perm]] [, opt])                -> io or nil
05795  *     open(path [, mode_enc [, perm]] [, opt]) {|io| block }  -> obj
05796  *
05797  *  Creates an <code>IO</code> object connected to the given stream,
05798  *  file, or subprocess.
05799  *
05800  *  If <i>path</i> does not start with a pipe character
05801  *  (``<code>|</code>''), treat it as the name of a file to open using
05802  *  the specified mode (defaulting to ``<code>r</code>'').
05803  *
05804  *  The mode_enc is
05805  *  either a string or an integer.  If it is an integer, it must be
05806  *  bitwise-or of open(2) flags, such as File::RDWR or File::EXCL.
05807  *  If it is a string, it is either "mode", "mode:ext_enc", or
05808  *  "mode:ext_enc:int_enc".
05809  *  The mode is one of the following:
05810  *
05811  *   r: read (default)
05812  *   w: write
05813  *   a: append
05814  *
05815  *  The mode can be followed by "b" (means binary-mode), or "+"
05816  *  (means both reading and writing allowed) or both.
05817  *  If ext_enc (external encoding) is specified,
05818  *  read string will be tagged by the encoding in reading,
05819  *  and output string will be converted
05820  *  to the specified encoding in writing.
05821  *  If ext_enc starts with 'BOM|', check whether the input has a BOM. If
05822  *  there is a BOM, strip it and set external encoding as
05823  *  what the BOM tells. If there is no BOM, use ext_enc without 'BOM|'.
05824  *  If two encoding names,
05825  *  ext_enc and int_enc (external encoding and internal encoding),
05826  *  are specified, the read string is converted from ext_enc
05827  *  to int_enc then tagged with the int_enc in read mode,
05828  *  and in write mode, the output string will be
05829  *  converted from int_enc to ext_enc before writing.
05830  *
05831  *  If a file is being created, its initial permissions may be
05832  *  set using the integer third parameter.
05833  *
05834  *  If a block is specified, it will be invoked with the
05835  *  <code>File</code> object as a parameter, and the file will be
05836  *  automatically closed when the block terminates. The call
05837  *  returns the value of the block.
05838  *
05839  *  If <i>path</i> starts with a pipe character, a subprocess is
05840  *  created, connected to the caller by a pair of pipes. The returned
05841  *  <code>IO</code> object may be used to write to the standard input
05842  *  and read from the standard output of this subprocess. If the command
05843  *  following the ``<code>|</code>'' is a single minus sign, Ruby forks,
05844  *  and this subprocess is connected to the parent. In the subprocess,
05845  *  the <code>open</code> call returns <code>nil</code>. If the command
05846  *  is not ``<code>-</code>'', the subprocess runs the command. If a
05847  *  block is associated with an <code>open("|-")</code> call, that block
05848  *  will be run twice---once in the parent and once in the child. The
05849  *  block parameter will be an <code>IO</code> object in the parent and
05850  *  <code>nil</code> in the child. The parent's <code>IO</code> object
05851  *  will be connected to the child's <code>$stdin</code> and
05852  *  <code>$stdout</code>. The subprocess will be terminated at the end
05853  *  of the block.
05854  *
05855  *     open("testfile") do |f|
05856  *       print f.gets
05857  *     end
05858  *
05859  *  <em>produces:</em>
05860  *
05861  *     This is line one
05862  *
05863  *  Open a subprocess and read its output:
05864  *
05865  *     cmd = open("|date")
05866  *     print cmd.gets
05867  *     cmd.close
05868  *
05869  *  <em>produces:</em>
05870  *
05871  *     Wed Apr  9 08:56:31 CDT 2003
05872  *
05873  *  Open a subprocess running the same Ruby program:
05874  *
05875  *     f = open("|-", "w+")
05876  *     if f == nil
05877  *       puts "in Child"
05878  *       exit
05879  *     else
05880  *       puts "Got: #{f.gets}"
05881  *     end
05882  *
05883  *  <em>produces:</em>
05884  *
05885  *     Got: in Child
05886  *
05887  *  Open a subprocess using a block to receive the I/O object:
05888  *
05889  *     open("|-") do |f|
05890  *       if f == nil
05891  *         puts "in Child"
05892  *       else
05893  *         puts "Got: #{f.gets}"
05894  *       end
05895  *     end
05896  *
05897  *  <em>produces:</em>
05898  *
05899  *     Got: in Child
05900  */
05901 
05902 static VALUE
05903 rb_f_open(int argc, VALUE *argv)
05904 {
05905     ID to_open = 0;
05906     int redirect = FALSE;
05907 
05908     if (argc >= 1) {
05909         CONST_ID(to_open, "to_open");
05910         if (rb_respond_to(argv[0], to_open)) {
05911             redirect = TRUE;
05912         }
05913         else {
05914             VALUE tmp = argv[0];
05915             FilePathValue(tmp);
05916             if (NIL_P(tmp)) {
05917                 redirect = TRUE;
05918             }
05919             else {
05920                 VALUE cmd = check_pipe_command(tmp);
05921                 if (!NIL_P(cmd)) {
05922                     argv[0] = cmd;
05923                     return rb_io_s_popen(argc, argv, rb_cIO);
05924                 }
05925             }
05926         }
05927     }
05928     if (redirect) {
05929         VALUE io = rb_funcall2(argv[0], to_open, argc-1, argv+1);
05930 
05931         if (rb_block_given_p()) {
05932             return rb_ensure(rb_yield, io, io_close, io);
05933         }
05934         return io;
05935     }
05936     return rb_io_s_open(argc, argv, rb_cFile);
05937 }
05938 
05939 static VALUE
05940 rb_io_open(VALUE filename, VALUE vmode, VALUE vperm, VALUE opt)
05941 {
05942     VALUE cmd;
05943     int oflags, fmode;
05944     convconfig_t convconfig;
05945     mode_t perm;
05946 
05947     rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, &convconfig);
05948     perm = NIL_P(vperm) ? 0666 :  NUM2MODET(vperm);
05949 
05950     if (!NIL_P(cmd = check_pipe_command(filename))) {
05951         return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, &convconfig);
05952     }
05953     else {
05954         return rb_file_open_generic(io_alloc(rb_cFile), filename,
05955                 oflags, fmode, &convconfig, perm);
05956     }
05957 }
05958 
05959 static VALUE
05960 rb_io_open_with_args(int argc, VALUE *argv)
05961 {
05962     VALUE io;
05963 
05964     io = io_alloc(rb_cFile);
05965     rb_open_file(argc, argv, io);
05966     return io;
05967 }
05968 
05969 static VALUE
05970 io_reopen(VALUE io, VALUE nfile)
05971 {
05972     rb_io_t *fptr, *orig;
05973     int fd, fd2;
05974     off_t pos = 0;
05975 
05976     nfile = rb_io_get_io(nfile);
05977     if (rb_safe_level() >= 4 &&
05978         (!OBJ_UNTRUSTED(io) || !OBJ_UNTRUSTED(nfile))) {
05979         rb_raise(rb_eSecurityError, "Insecure: can't reopen");
05980     }
05981     GetOpenFile(io, fptr);
05982     GetOpenFile(nfile, orig);
05983 
05984     if (fptr == orig) return io;
05985     if (IS_PREP_STDIO(fptr)) {
05986         if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
05987             (fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
05988             (fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
05989             rb_raise(rb_eArgError,
05990                      "%s can't change access mode from \"%s\" to \"%s\"",
05991                      PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
05992                      rb_io_fmode_modestr(orig->mode));
05993         }
05994     }
05995     if (fptr->mode & FMODE_WRITABLE) {
05996         if (io_fflush(fptr) < 0)
05997             rb_sys_fail(0);
05998     }
05999     else {
06000         io_tell(fptr);
06001     }
06002     if (orig->mode & FMODE_READABLE) {
06003         pos = io_tell(orig);
06004     }
06005     if (orig->mode & FMODE_WRITABLE) {
06006         if (io_fflush(orig) < 0)
06007             rb_sys_fail(0);
06008     }
06009 
06010     /* copy rb_io_t structure */
06011     fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
06012     fptr->pid = orig->pid;
06013     fptr->lineno = orig->lineno;
06014     if (RTEST(orig->pathv)) fptr->pathv = orig->pathv;
06015     else if (!IS_PREP_STDIO(fptr)) fptr->pathv = Qnil;
06016     fptr->finalize = orig->finalize;
06017 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
06018     if (fptr->finalize == pipe_finalize)
06019         pipe_add_fptr(fptr);
06020 #endif
06021 
06022     fd = fptr->fd;
06023     fd2 = orig->fd;
06024     if (fd != fd2) {
06025         if (IS_PREP_STDIO(fptr) || fd <= 2 || !fptr->stdio_file) {
06026             /* need to keep FILE objects of stdin, stdout and stderr */
06027             if (dup2(fd2, fd) < 0)
06028                 rb_sys_fail_path(orig->pathv);
06029             rb_update_max_fd(fd);
06030         }
06031         else {
06032             fclose(fptr->stdio_file);
06033             fptr->stdio_file = 0;
06034             fptr->fd = -1;
06035             if (dup2(fd2, fd) < 0)
06036                 rb_sys_fail_path(orig->pathv);
06037             rb_update_max_fd(fd);
06038             fptr->fd = fd;
06039         }
06040         rb_thread_fd_close(fd);
06041         if ((orig->mode & FMODE_READABLE) && pos >= 0) {
06042             if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) {
06043                 rb_sys_fail_path(fptr->pathv);
06044             }
06045             if (io_seek(orig, pos, SEEK_SET) < 0 && errno) {
06046                 rb_sys_fail_path(orig->pathv);
06047             }
06048         }
06049     }
06050 
06051     if (fptr->mode & FMODE_BINMODE) {
06052         rb_io_binmode(io);
06053     }
06054 
06055     RBASIC(io)->klass = rb_obj_class(nfile);
06056     return io;
06057 }
06058 
06059 /*
06060  *  call-seq:
06061  *     ios.reopen(other_IO)         -> ios
06062  *     ios.reopen(path, mode_str)   -> ios
06063  *
06064  *  Reassociates <em>ios</em> with the I/O stream given in
06065  *  <i>other_IO</i> or to a new stream opened on <i>path</i>. This may
06066  *  dynamically change the actual class of this stream.
06067  *
06068  *     f1 = File.new("testfile")
06069  *     f2 = File.new("testfile")
06070  *     f2.readlines[0]   #=> "This is line one\n"
06071  *     f2.reopen(f1)     #=> #<File:testfile>
06072  *     f2.readlines[0]   #=> "This is line one\n"
06073  */
06074 
06075 static VALUE
06076 rb_io_reopen(int argc, VALUE *argv, VALUE file)
06077 {
06078     VALUE fname, nmode;
06079     int oflags;
06080     rb_io_t *fptr;
06081 
06082     rb_secure(4);
06083     if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
06084         VALUE tmp = rb_io_check_io(fname);
06085         if (!NIL_P(tmp)) {
06086             return io_reopen(file, tmp);
06087         }
06088     }
06089 
06090     FilePathValue(fname);
06091     rb_io_taint_check(file);
06092     fptr = RFILE(file)->fptr;
06093     if (!fptr) {
06094         fptr = RFILE(file)->fptr = ALLOC(rb_io_t);
06095         MEMZERO(fptr, rb_io_t, 1);
06096     }
06097 
06098     if (!NIL_P(nmode)) {
06099         int fmode = rb_io_modestr_fmode(StringValueCStr(nmode));
06100         if (IS_PREP_STDIO(fptr) &&
06101             ((fptr->mode & FMODE_READWRITE) & (fmode & FMODE_READWRITE)) !=
06102             (fptr->mode & FMODE_READWRITE)) {
06103             rb_raise(rb_eArgError,
06104                      "%s can't change access mode from \"%s\" to \"%s\"",
06105                      PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
06106                      rb_io_fmode_modestr(fmode));
06107         }
06108         fptr->mode = fmode;
06109         rb_io_mode_enc(fptr, StringValueCStr(nmode));
06110         fptr->encs.ecflags = 0;
06111         fptr->encs.ecopts = Qnil;
06112     }
06113 
06114     fptr->pathv = rb_str_new_frozen(fname);
06115     oflags = rb_io_fmode_oflags(fptr->mode);
06116     if (fptr->fd < 0) {
06117         fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
06118         fptr->stdio_file = 0;
06119         return file;
06120     }
06121 
06122     if (fptr->mode & FMODE_WRITABLE) {
06123         if (io_fflush(fptr) < 0)
06124             rb_sys_fail(0);
06125     }
06126     fptr->rbuf.off = fptr->rbuf.len = 0;
06127 
06128     if (fptr->stdio_file) {
06129         if (freopen(RSTRING_PTR(fptr->pathv), rb_io_oflags_modestr(oflags), fptr->stdio_file) == 0) {
06130             rb_sys_fail_path(fptr->pathv);
06131         }
06132         fptr->fd = fileno(fptr->stdio_file);
06133 #ifdef USE_SETVBUF
06134         if (setvbuf(fptr->stdio_file, NULL, _IOFBF, 0) != 0)
06135             rb_warn("setvbuf() can't be honoured for %s", RSTRING_PTR(fptr->pathv));
06136 #endif
06137     }
06138     else {
06139         if (close(fptr->fd) < 0)
06140             rb_sys_fail_path(fptr->pathv);
06141         fptr->fd = -1;
06142         fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
06143     }
06144 
06145     return file;
06146 }
06147 
06148 /* :nodoc: */
06149 static VALUE
06150 rb_io_init_copy(VALUE dest, VALUE io)
06151 {
06152     rb_io_t *fptr, *orig;
06153     int fd;
06154     VALUE write_io;
06155     off_t pos;
06156 
06157     io = rb_io_get_io(io);
06158     if (dest == io) return dest;
06159     GetOpenFile(io, orig);
06160     MakeOpenFile(dest, fptr);
06161 
06162     rb_io_flush(io);
06163 
06164     /* copy rb_io_t structure */
06165     fptr->mode = orig->mode & ~FMODE_PREP;
06166     fptr->encs = orig->encs;
06167     fptr->pid = orig->pid;
06168     fptr->lineno = orig->lineno;
06169     if (!NIL_P(orig->pathv)) fptr->pathv = orig->pathv;
06170     fptr->finalize = orig->finalize;
06171 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
06172     if (fptr->finalize == pipe_finalize)
06173         pipe_add_fptr(fptr);
06174 #endif
06175 
06176     fd = ruby_dup(orig->fd);
06177     fptr->fd = fd;
06178     pos = io_tell(orig);
06179     if (0 <= pos)
06180         io_seek(fptr, pos, SEEK_SET);
06181     if (fptr->mode & FMODE_BINMODE) {
06182         rb_io_binmode(dest);
06183     }
06184 
06185     write_io = GetWriteIO(io);
06186     if (io != write_io) {
06187         write_io = rb_obj_dup(write_io);
06188         fptr->tied_io_for_writing = write_io;
06189         rb_ivar_set(dest, rb_intern("@tied_io_for_writing"), write_io);
06190     }
06191 
06192     return dest;
06193 }
06194 
06195 /*
06196  *  call-seq:
06197  *     ios.printf(format_string [, obj, ...])   -> nil
06198  *
06199  *  Formats and writes to <em>ios</em>, converting parameters under
06200  *  control of the format string. See <code>Kernel#sprintf</code>
06201  *  for details.
06202  */
06203 
06204 VALUE
06205 rb_io_printf(int argc, VALUE *argv, VALUE out)
06206 {
06207     rb_io_write(out, rb_f_sprintf(argc, argv));
06208     return Qnil;
06209 }
06210 
06211 /*
06212  *  call-seq:
06213  *     printf(io, string [, obj ... ])    -> nil
06214  *     printf(string [, obj ... ])        -> nil
06215  *
06216  *  Equivalent to:
06217  *     io.write(sprintf(string, obj, ...)
06218  *  or
06219  *     $stdout.write(sprintf(string, obj, ...)
06220  */
06221 
06222 static VALUE
06223 rb_f_printf(int argc, VALUE *argv)
06224 {
06225     VALUE out;
06226 
06227     if (argc == 0) return Qnil;
06228     if (TYPE(argv[0]) == T_STRING) {
06229         out = rb_stdout;
06230     }
06231     else {
06232         out = argv[0];
06233         argv++;
06234         argc--;
06235     }
06236     rb_io_write(out, rb_f_sprintf(argc, argv));
06237 
06238     return Qnil;
06239 }
06240 
06241 /*
06242  *  call-seq:
06243  *     ios.print()             -> nil
06244  *     ios.print(obj, ...)     -> nil
06245  *
06246  *  Writes the given object(s) to <em>ios</em>. The stream must be
06247  *  opened for writing. If the output field separator (<code>$,</code>)
06248  *  is not <code>nil</code>, it will be inserted between each object.
06249  *  If the output record separator (<code>$\</code>)
06250  *  is not <code>nil</code>, it will be appended to the output. If no
06251  *  arguments are given, prints <code>$_</code>. Objects that aren't
06252  *  strings will be converted by calling their <code>to_s</code> method.
06253  *  With no argument, prints the contents of the variable <code>$_</code>.
06254  *  Returns <code>nil</code>.
06255  *
06256  *     $stdout.print("This is ", 100, " percent.\n")
06257  *
06258  *  <em>produces:</em>
06259  *
06260  *     This is 100 percent.
06261  */
06262 
06263 VALUE
06264 rb_io_print(int argc, VALUE *argv, VALUE out)
06265 {
06266     int i;
06267     VALUE line;
06268 
06269     /* if no argument given, print `$_' */
06270     if (argc == 0) {
06271         argc = 1;
06272         line = rb_lastline_get();
06273         argv = &line;
06274     }
06275     for (i=0; i<argc; i++) {
06276         if (!NIL_P(rb_output_fs) && i>0) {
06277             rb_io_write(out, rb_output_fs);
06278         }
06279         rb_io_write(out, argv[i]);
06280     }
06281     if (argc > 0 && !NIL_P(rb_output_rs)) {
06282         rb_io_write(out, rb_output_rs);
06283     }
06284 
06285     return Qnil;
06286 }
06287 
06288 /*
06289  *  call-seq:
06290  *     print(obj, ...)    -> nil
06291  *
06292  *  Prints each object in turn to <code>$stdout</code>. If the output
06293  *  field separator (<code>$,</code>) is not +nil+, its
06294  *  contents will appear between each field. If the output record
06295  *  separator (<code>$\</code>) is not +nil+, it will be
06296  *  appended to the output. If no arguments are given, prints
06297  *  <code>$_</code>. Objects that aren't strings will be converted by
06298  *  calling their <code>to_s</code> method.
06299  *
06300  *     print "cat", [1,2,3], 99, "\n"
06301  *     $, = ", "
06302  *     $\ = "\n"
06303  *     print "cat", [1,2,3], 99
06304  *
06305  *  <em>produces:</em>
06306  *
06307  *     cat12399
06308  *     cat, 1, 2, 3, 99
06309  */
06310 
06311 static VALUE
06312 rb_f_print(int argc, VALUE *argv)
06313 {
06314     rb_io_print(argc, argv, rb_stdout);
06315     return Qnil;
06316 }
06317 
06318 /*
06319  *  call-seq:
06320  *     ios.putc(obj)    -> obj
06321  *
06322  *  If <i>obj</i> is <code>Numeric</code>, write the character whose code is
06323  *  the least-significant byte of <i>obj</i>, otherwise write the first byte
06324  *  of the string representation of <i>obj</i> to <em>ios</em>. Note: This
06325  *  method is not safe for use with multi-byte characters as it will truncate
06326  *  them.
06327  *
06328  *     $stdout.putc "A"
06329  *     $stdout.putc 65
06330  *
06331  *  <em>produces:</em>
06332  *
06333  *     AA
06334  */
06335 
06336 static VALUE
06337 rb_io_putc(VALUE io, VALUE ch)
06338 {
06339     VALUE str;
06340     if (TYPE(ch) == T_STRING) {
06341         str = rb_str_substr(ch, 0, 1);
06342     }
06343     else {
06344         char c = NUM2CHR(ch);
06345         str = rb_str_new(&c, 1);
06346     }
06347     rb_io_write(io, str);
06348     return ch;
06349 }
06350 
06351 /*
06352  *  call-seq:
06353  *     putc(int)   -> int
06354  *
06355  *  Equivalent to:
06356  *
06357  *    $stdout.putc(int)
06358  *
06359  * Refer to the documentation for IO#putc for important information regarding
06360  * multi-byte characters.
06361  */
06362 
06363 static VALUE
06364 rb_f_putc(VALUE recv, VALUE ch)
06365 {
06366     if (recv == rb_stdout) {
06367         return rb_io_putc(recv, ch);
06368     }
06369     return rb_funcall2(rb_stdout, rb_intern("putc"), 1, &ch);
06370 }
06371 
06372 
06373 static int
06374 str_end_with_asciichar(VALUE str, int c)
06375 {
06376     long len = RSTRING_LEN(str);
06377     const char *ptr = RSTRING_PTR(str);
06378     rb_encoding *enc = rb_enc_from_index(ENCODING_GET(str));
06379     int n;
06380 
06381     if (len == 0) return 0;
06382     if ((n = rb_enc_mbminlen(enc)) == 1) {
06383         return ptr[len - 1] == c;
06384     }
06385     return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) == c;
06386 }
06387 
06388 static VALUE
06389 io_puts_ary(VALUE ary, VALUE out, int recur)
06390 {
06391     VALUE tmp;
06392     long i;
06393 
06394     if (recur) {
06395         tmp = rb_str_new2("[...]");
06396         rb_io_puts(1, &tmp, out);
06397         return Qnil;
06398     }
06399     for (i=0; i<RARRAY_LEN(ary); i++) {
06400         tmp = RARRAY_PTR(ary)[i];
06401         rb_io_puts(1, &tmp, out);
06402     }
06403     return Qnil;
06404 }
06405 
06406 /*
06407  *  call-seq:
06408  *     ios.puts(obj, ...)    -> nil
06409  *
06410  *  Writes the given objects to <em>ios</em> as with
06411  *  <code>IO#print</code>. Writes a record separator (typically a
06412  *  newline) after any that do not already end with a newline sequence.
06413  *  If called with an array argument, writes each element on a new line.
06414  *  If called without arguments, outputs a single record separator.
06415  *
06416  *     $stdout.puts("this", "is", "a", "test")
06417  *
06418  *  <em>produces:</em>
06419  *
06420  *     this
06421  *     is
06422  *     a
06423  *     test
06424  */
06425 
06426 VALUE
06427 rb_io_puts(int argc, VALUE *argv, VALUE out)
06428 {
06429     int i;
06430     VALUE line;
06431 
06432     /* if no argument given, print newline. */
06433     if (argc == 0) {
06434         rb_io_write(out, rb_default_rs);
06435         return Qnil;
06436     }
06437     for (i=0; i<argc; i++) {
06438         if (TYPE(argv[i]) == T_STRING) {
06439             line = argv[i];
06440             goto string;
06441         }
06442         line = rb_check_array_type(argv[i]);
06443         if (!NIL_P(line)) {
06444             rb_exec_recursive(io_puts_ary, line, out);
06445             continue;
06446         }
06447         line = rb_obj_as_string(argv[i]);
06448       string:
06449         rb_io_write(out, line);
06450         if (RSTRING_LEN(line) == 0 ||
06451             !str_end_with_asciichar(line, '\n')) {
06452             rb_io_write(out, rb_default_rs);
06453         }
06454     }
06455 
06456     return Qnil;
06457 }
06458 
06459 /*
06460  *  call-seq:
06461  *     puts(obj, ...)    -> nil
06462  *
06463  *  Equivalent to
06464  *
06465  *      $stdout.puts(obj, ...)
06466  */
06467 
06468 static VALUE
06469 rb_f_puts(int argc, VALUE *argv, VALUE recv)
06470 {
06471     if (recv == rb_stdout) {
06472         return rb_io_puts(argc, argv, recv);
06473     }
06474     return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv);
06475 }
06476 
06477 void
06478 rb_p(VALUE obj) /* for debug print within C code */
06479 {
06480     VALUE str = rb_obj_as_string(rb_inspect(obj));
06481     if (TYPE(rb_stdout) == T_FILE &&
06482         rb_method_basic_definition_p(CLASS_OF(rb_stdout), id_write)) {
06483         io_write(rb_stdout, str, 1);
06484         io_write(rb_stdout, rb_default_rs, 0);
06485     }
06486     else {
06487         rb_io_write(rb_stdout, str);
06488         rb_io_write(rb_stdout, rb_default_rs);
06489     }
06490 }
06491 
06492 /*
06493  *  call-seq:
06494  *     p(obj)              -> obj
06495  *     p(obj1, obj2, ...)  -> [obj, ...]
06496  *     p()                 -> nil
06497  *
06498  *  For each object, directly writes _obj_.+inspect+ followed by a
06499  *  newline to the program's standard output.
06500  *
06501  *     S = Struct.new(:name, :state)
06502  *     s = S['dave', 'TX']
06503  *     p s
06504  *
06505  *  <em>produces:</em>
06506  *
06507  *     #<S name="dave", state="TX">
06508  */
06509 
06510 static VALUE
06511 rb_f_p(int argc, VALUE *argv, VALUE self)
06512 {
06513     int i;
06514     VALUE ret = Qnil;
06515 
06516     for (i=0; i<argc; i++) {
06517         rb_p(argv[i]);
06518     }
06519     if (argc == 1) {
06520         ret = argv[0];
06521     }
06522     else if (argc > 1) {
06523         ret = rb_ary_new4(argc, argv);
06524     }
06525     if (TYPE(rb_stdout) == T_FILE) {
06526         rb_io_flush(rb_stdout);
06527     }
06528     return ret;
06529 }
06530 
06531 /*
06532  *  call-seq:
06533  *     obj.display(port=$>)    -> nil
06534  *
06535  *  Prints <i>obj</i> on the given port (default <code>$></code>).
06536  *  Equivalent to:
06537  *
06538  *     def display(port=$>)
06539  *       port.write self
06540  *     end
06541  *
06542  *  For example:
06543  *
06544  *     1.display
06545  *     "cat".display
06546  *     [ 4, 5, 6 ].display
06547  *     puts
06548  *
06549  *  <em>produces:</em>
06550  *
06551  *     1cat456
06552  */
06553 
06554 static VALUE
06555 rb_obj_display(int argc, VALUE *argv, VALUE self)
06556 {
06557     VALUE out;
06558 
06559     if (argc == 0) {
06560         out = rb_stdout;
06561     }
06562     else {
06563         rb_scan_args(argc, argv, "01", &out);
06564     }
06565     rb_io_write(out, self);
06566 
06567     return Qnil;
06568 }
06569 
06570 void
06571 rb_write_error2(const char *mesg, long len)
06572 {
06573     if (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0) {
06574         (void)fwrite(mesg, sizeof(char), len, stderr);
06575     }
06576     else {
06577         rb_io_write(rb_stderr, rb_str_new(mesg, len));
06578     }
06579 }
06580 
06581 void
06582 rb_write_error(const char *mesg)
06583 {
06584     rb_write_error2(mesg, strlen(mesg));
06585 }
06586 
06587 static void
06588 must_respond_to(ID mid, VALUE val, ID id)
06589 {
06590     if (!rb_respond_to(val, mid)) {
06591         rb_raise(rb_eTypeError, "%s must have %s method, %s given",
06592                  rb_id2name(id), rb_id2name(mid),
06593                  rb_obj_classname(val));
06594     }
06595 }
06596 
06597 static void
06598 stdout_setter(VALUE val, ID id, VALUE *variable)
06599 {
06600     must_respond_to(id_write, val, id);
06601     *variable = val;
06602 }
06603 
06604 static VALUE
06605 prep_io(int fd, int fmode, VALUE klass, const char *path)
06606 {
06607     rb_io_t *fp;
06608     VALUE io = io_alloc(klass);
06609 
06610     MakeOpenFile(io, fp);
06611     fp->fd = fd;
06612 #ifdef __CYGWIN__
06613     if (!isatty(fd)) {
06614         fmode |= FMODE_BINMODE;
06615         setmode(fd, O_BINARY);
06616     }
06617 #endif
06618     fp->mode = fmode;
06619     io_check_tty(fp);
06620     if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path));
06621     rb_update_max_fd(fd);
06622 
06623     return io;
06624 }
06625 
06626 VALUE
06627 rb_io_fdopen(int fd, int oflags, const char *path)
06628 {
06629     VALUE klass = rb_cIO;
06630 
06631     if (path && strcmp(path, "-")) klass = rb_cFile;
06632     return prep_io(fd, rb_io_oflags_fmode(oflags), klass, path);
06633 }
06634 
06635 static VALUE
06636 prep_stdio(FILE *f, int fmode, VALUE klass, const char *path)
06637 {
06638     rb_io_t *fptr;
06639     VALUE io = prep_io(fileno(f), fmode|FMODE_PREP|DEFAULT_TEXTMODE, klass, path);
06640 
06641     GetOpenFile(io, fptr);
06642     fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
06643 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
06644     fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
06645     if (fmode & FMODE_READABLE) {
06646         fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
06647     }
06648 #endif
06649     fptr->stdio_file = f;
06650 
06651     return io;
06652 }
06653 
06654 FILE *
06655 rb_io_stdio_file(rb_io_t *fptr)
06656 {
06657     if (!fptr->stdio_file) {
06658         int oflags = rb_io_fmode_oflags(fptr->mode);
06659         fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_oflags_modestr(oflags));
06660     }
06661     return fptr->stdio_file;
06662 }
06663 
06664 /*
06665  *  call-seq:
06666  *     IO.new(fd [, mode] [, opt])   -> io
06667  *
06668  *  Returns a new IO object (a stream) for the given IO object or integer file
06669  *  descriptor and mode string.  See also IO.sysopen and IO.for_fd.
06670  *
06671  *  === Parameters
06672  *
06673  *  fd:: numeric file descriptor or IO object
06674  *  mode:: file mode. a string or an integer
06675  *  opt:: hash for specifying +mode+ by name.
06676  *
06677  *  ==== Mode
06678  *
06679  *  When mode is an integer it must be combination of the modes defined in
06680  *  File::Constants.
06681  *
06682  *  When mode is a string it must be in one of the following forms:
06683  *  - "fmode",
06684  *  - "fmode:extern",
06685  *  - "fmode:extern:intern".
06686  *  <code>extern</code> is the external encoding name for the IO.
06687  *  <code>intern</code> is the internal encoding.
06688  *  <code>fmode</code> must be a file open mode string. See the description of
06689  *  class IO for mode string directives.
06690  *
06691  *  When the mode of original IO is read only, the mode cannot be changed to
06692  *  be writable.  Similarly, the mode cannot be changed from write only to
06693  *  readable.
06694  *
06695  *  When such a change is attempted the error is raised in different locations
06696  *  according to the platform.
06697  *
06698  *  ==== Options
06699  *  +opt+ can have the following keys
06700  *  :mode ::
06701  *    Same as +mode+ parameter
06702  *  :external_encoding ::
06703  *    External encoding for the IO.  "-" is a synonym for the default external
06704  *    encoding.
06705  *  :internal_encoding ::
06706  *    Internal encoding for the IO.  "-" is a synonym for the default internal
06707  *    encoding.
06708  *
06709  *    If the value is nil no conversion occurs.
06710  *  :encoding ::
06711  *    Specifies external and internal encodings as "extern:intern".
06712  *  :textmode ::
06713  *    If the value is truth value, same as "t" in argument +mode+.
06714  *  :binmode ::
06715  *    If the value is truth value, same as "b" in argument +mode+.
06716  *  :autoclose ::
06717  *    If the value is +false+, the +fd+ will be kept open after this IO
06718  *    instance gets finalized.
06719  *
06720  *  Also +opt+ can have same keys in String#encode for controlling conversion
06721  *  between the external encoding and the internal encoding.
06722  *
06723  *  === Example 1
06724  *
06725  *    fd = IO.sysopen("/dev/tty", "w")
06726  *    a = IO.new(fd,"w")
06727  *    $stderr.puts "Hello"
06728  *    a.puts "World"
06729  *
06730  *  <em>produces:</em>
06731  *
06732  *    Hello
06733  *    World
06734  *
06735  *  === Example 2
06736  *
06737  *    require 'fcntl'
06738  *
06739  *    fd = STDERR.fcntl(Fcntl::F_DUPFD)
06740  *    io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true)
06741  *    io.puts "Hello, World!"
06742  *
06743  *    fd = STDERR.fcntl(Fcntl::F_DUPFD)
06744  *    io = IO.new(fd, mode: 'w', cr_newline: true,
06745  *                external_encoding: Encoding::UTF_16LE)
06746  *    io.puts "Hello, World!"
06747  *
06748  *  Both of above print "Hello, World!" in UTF-16LE to standard error output
06749  *  with converting EOL generated by <code>puts</code> to CR.
06750  */
06751 
06752 static VALUE
06753 rb_io_initialize(int argc, VALUE *argv, VALUE io)
06754 {
06755     VALUE fnum, vmode;
06756     rb_io_t *fp;
06757     int fd, fmode, oflags = O_RDONLY;
06758     convconfig_t convconfig;
06759     VALUE opt;
06760 #if defined(HAVE_FCNTL) && defined(F_GETFL)
06761     int ofmode;
06762 #else
06763     struct stat st;
06764 #endif
06765 
06766     rb_secure(4);
06767 
06768     argc = rb_scan_args(argc, argv, "11:", &fnum, &vmode, &opt);
06769     rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
06770 
06771     fd = NUM2INT(fnum);
06772     if (rb_reserved_fd_p(fd)) {
06773         rb_raise(rb_eArgError, "The given fd is not accessible because RubyVM reserves it");
06774     }
06775 #if defined(HAVE_FCNTL) && defined(F_GETFL)
06776     oflags = fcntl(fd, F_GETFL);
06777     if (oflags == -1) rb_sys_fail(0);
06778 #else
06779     if (fstat(fd, &st) == -1) rb_sys_fail(0);
06780 #endif
06781     rb_update_max_fd(fd);
06782 #if defined(HAVE_FCNTL) && defined(F_GETFL)
06783     ofmode = rb_io_oflags_fmode(oflags);
06784     if (NIL_P(vmode)) {
06785         fmode = ofmode;
06786     }
06787     else if ((~ofmode & fmode) & FMODE_READWRITE) {
06788         VALUE error = INT2FIX(EINVAL);
06789         rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError));
06790     }
06791 #endif
06792     if (!NIL_P(opt) && rb_hash_aref(opt, sym_autoclose) == Qfalse) {
06793         fmode |= FMODE_PREP;
06794     }
06795     MakeOpenFile(io, fp);
06796     fp->fd = fd;
06797     fp->mode = fmode;
06798     fp->encs = convconfig;
06799     clear_codeconv(fp);
06800     io_check_tty(fp);
06801     if (fileno(stdin) == fd)
06802         fp->stdio_file = stdin;
06803     else if (fileno(stdout) == fd)
06804         fp->stdio_file = stdout;
06805     else if (fileno(stderr) == fd)
06806         fp->stdio_file = stderr;
06807 
06808     if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
06809     return io;
06810 }
06811 
06812 /*
06813  *  call-seq:
06814  *     File.new(filename, mode="r" [, opt])            -> file
06815  *     File.new(filename [, mode [, perm]] [, opt])    -> file
06816  *
06817  *  Opens the file named by +filename+ according to +mode+ (default is "r")
06818  *  and returns a new <code>File</code> object.
06819  *
06820  *  === Parameters
06821  *
06822  *  See the description of class IO for a description of +mode+.  The file
06823  *  mode may optionally be specified as a Fixnum by +or+-ing together the
06824  *  flags (O_RDONLY etc, again described under +IO+).
06825  *
06826  *  Optional permission bits may be given in +perm+.  These mode and
06827  *  permission bits are platform dependent; on Unix systems, see
06828  *  <code>open(2)</code> for details.
06829  *
06830  *  Optional +opt+ parameter is same as in IO.open.
06831  *
06832  *  === Examples
06833  *
06834  *    f = File.new("testfile", "r")
06835  *    f = File.new("newfile",  "w+")
06836  *    f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
06837  */
06838 
06839 static VALUE
06840 rb_file_initialize(int argc, VALUE *argv, VALUE io)
06841 {
06842     if (RFILE(io)->fptr) {
06843         rb_raise(rb_eRuntimeError, "reinitializing File");
06844     }
06845     if (0 < argc && argc < 3) {
06846         VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
06847 
06848         if (!NIL_P(fd)) {
06849             argv[0] = fd;
06850             return rb_io_initialize(argc, argv, io);
06851         }
06852     }
06853     rb_open_file(argc, argv, io);
06854 
06855     return io;
06856 }
06857 
06858 /* :nodoc: */
06859 static VALUE
06860 rb_io_s_new(int argc, VALUE *argv, VALUE klass)
06861 {
06862     if (rb_block_given_p()) {
06863         const char *cname = rb_class2name(klass);
06864 
06865         rb_warn("%s::new() does not take block; use %s::open() instead",
06866                 cname, cname);
06867     }
06868     return rb_class_new_instance(argc, argv, klass);
06869 }
06870 
06871 
06872 /*
06873  *  call-seq:
06874  *     IO.for_fd(fd, mode [, opt])    -> io
06875  *
06876  *  Synonym for <code>IO.new</code>.
06877  *
06878  */
06879 
06880 static VALUE
06881 rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
06882 {
06883     VALUE io = rb_obj_alloc(klass);
06884     rb_io_initialize(argc, argv, io);
06885     return io;
06886 }
06887 
06888 /*
06889  *  call-seq:
06890  *     ios.autoclose?   -> true or false
06891  *
06892  *  Returns +true+ if the underlying file descriptor of _ios_ will be
06893  *  closed automatically at its finalization, otherwise +false+.
06894  */
06895 
06896 static VALUE
06897 rb_io_autoclose_p(VALUE io)
06898 {
06899     rb_io_t *fptr;
06900     rb_secure(4);
06901     GetOpenFile(io, fptr);
06902     return (fptr->mode & FMODE_PREP) ? Qfalse : Qtrue;
06903 }
06904 
06905 /*
06906  *  call-seq:
06907  *     io.autoclose = bool    -> true or false
06908  *
06909  *  Sets auto-close flag.
06910  *
06911  *     f = open("/dev/null")
06912  *     IO.for_fd(f.fileno)
06913  *     # ...
06914  *     f.gets # may cause IOError
06915  *
06916  *     f = open("/dev/null")
06917  *     IO.for_fd(f.fileno).autoclose = true
06918  *     # ...
06919  *     f.gets # won't cause IOError
06920  */
06921 
06922 static VALUE
06923 rb_io_set_autoclose(VALUE io, VALUE autoclose)
06924 {
06925     rb_io_t *fptr;
06926     rb_secure(4);
06927     GetOpenFile(io, fptr);
06928     if (!RTEST(autoclose))
06929         fptr->mode |= FMODE_PREP;
06930     else
06931         fptr->mode &= ~FMODE_PREP;
06932     return io;
06933 }
06934 
06935 static void
06936 argf_mark(void *ptr)
06937 {
06938     struct argf *p = ptr;
06939     rb_gc_mark(p->filename);
06940     rb_gc_mark(p->current_file);
06941     rb_gc_mark(p->argv);
06942     rb_gc_mark(p->encs.ecopts);
06943 }
06944 
06945 static void
06946 argf_free(void *ptr)
06947 {
06948     struct argf *p = ptr;
06949     xfree(p->inplace);
06950     xfree(p);
06951 }
06952 
06953 static inline void
06954 argf_init(struct argf *p, VALUE v)
06955 {
06956     p->filename = Qnil;
06957     p->current_file = Qnil;
06958     p->lineno = 0;
06959     p->argv = v;
06960 }
06961 
06962 static VALUE
06963 argf_alloc(VALUE klass)
06964 {
06965     struct argf *p;
06966     VALUE argf = Data_Make_Struct(klass, struct argf, argf_mark, argf_free, p);
06967 
06968     argf_init(p, Qnil);
06969     return argf;
06970 }
06971 
06972 #undef rb_argv
06973 
06974 /* :nodoc: */
06975 static VALUE
06976 argf_initialize(VALUE argf, VALUE argv)
06977 {
06978     memset(&ARGF, 0, sizeof(ARGF));
06979     argf_init(&ARGF, argv);
06980 
06981     return argf;
06982 }
06983 
06984 /* :nodoc: */
06985 static VALUE
06986 argf_initialize_copy(VALUE argf, VALUE orig)
06987 {
06988     ARGF = argf_of(orig);
06989     ARGF.argv = rb_obj_dup(ARGF.argv);
06990     if (ARGF.inplace) {
06991         const char *inplace = ARGF.inplace;
06992         ARGF.inplace = 0;
06993         ARGF.inplace = ruby_strdup(inplace);
06994     }
06995     return argf;
06996 }
06997 
06998 /*
06999  *  call-seq:
07000  *     ARGF.lineno = number  -> nil
07001  *
07002  *  Sets the line number of +ARGF+ as a whole to the given +Integer+.
07003  *
07004  *  +ARGF+ sets the line number automatically as you read data, so normally
07005  *  you will not need to set it explicitly. To access the current line number
07006  *  use +ARGF.lineno+.
07007  *
07008  *  For example:
07009  *
07010  *      ARGF.lineno      #=> 0
07011  *      ARGF.readline    #=> "This is line 1\n"
07012  *      ARGF.lineno      #=> 1
07013  *      ARGF.lineno = 0  #=> nil
07014  *      ARGF.lineno      #=> 0
07015  */
07016 static VALUE
07017 argf_set_lineno(VALUE argf, VALUE val)
07018 {
07019     ARGF.lineno = NUM2INT(val);
07020     ARGF.last_lineno = ARGF.lineno;
07021     return Qnil;
07022 }
07023 
07024 /*
07025  *  call-seq:
07026  *     ARGF.lineno -> integer
07027  *
07028  *  Returns the current line number of ARGF as a whole. This value
07029  *  can be set manually with +ARGF.lineno=+.
07030  *
07031  *  For example:
07032  *
07033  *      ARGF.lineno   #=> 0
07034  *      ARGF.readline #=> "This is line 1\n"
07035  *      ARGF.lineno   #=> 1
07036  */
07037 static VALUE
07038 argf_lineno(VALUE argf)
07039 {
07040     return INT2FIX(ARGF.lineno);
07041 }
07042 
07043 static VALUE
07044 argf_forward(int argc, VALUE *argv, VALUE argf)
07045 {
07046     return rb_funcall3(ARGF.current_file, rb_frame_this_func(), argc, argv);
07047 }
07048 
07049 #define next_argv() argf_next_argv(argf)
07050 #define ARGF_GENERIC_INPUT_P() \
07051     (ARGF.current_file == rb_stdin && TYPE(ARGF.current_file) != T_FILE)
07052 #define ARGF_FORWARD(argc, argv) do {\
07053     if (ARGF_GENERIC_INPUT_P())\
07054         return argf_forward((argc), (argv), argf);\
07055 } while (0)
07056 #define NEXT_ARGF_FORWARD(argc, argv) do {\
07057     if (!next_argv()) return Qnil;\
07058     ARGF_FORWARD((argc), (argv));\
07059 } while (0)
07060 
07061 static void
07062 argf_close(VALUE file)
07063 {
07064     if (file == rb_stdin) return;
07065     if (RB_TYPE_P(file, T_FILE)) {
07066         rb_io_set_write_io(file, Qnil);
07067     }
07068     rb_funcall3(file, rb_intern("close"), 0, 0);
07069 }
07070 
07071 static int
07072 argf_next_argv(VALUE argf)
07073 {
07074     char *fn;
07075     rb_io_t *fptr;
07076     int stdout_binmode = 0;
07077     int fmode;
07078 
07079     if (TYPE(rb_stdout) == T_FILE) {
07080         GetOpenFile(rb_stdout, fptr);
07081         if (fptr->mode & FMODE_BINMODE)
07082             stdout_binmode = 1;
07083     }
07084 
07085     if (ARGF.init_p == 0) {
07086         if (!NIL_P(ARGF.argv) && RARRAY_LEN(ARGF.argv) > 0) {
07087             ARGF.next_p = 1;
07088         }
07089         else {
07090             ARGF.next_p = -1;
07091         }
07092         ARGF.init_p = 1;
07093     }
07094     else {
07095         if (NIL_P(ARGF.argv)) {
07096             ARGF.next_p = -1;
07097         }
07098         else if (ARGF.next_p == -1 && RARRAY_LEN(ARGF.argv) > 0) {
07099             ARGF.next_p = 1;
07100         }
07101     }
07102 
07103     if (ARGF.next_p == 1) {
07104       retry:
07105         if (RARRAY_LEN(ARGF.argv) > 0) {
07106             ARGF.filename = rb_ary_shift(ARGF.argv);
07107             fn = StringValueCStr(ARGF.filename);
07108             if (strlen(fn) == 1 && fn[0] == '-') {
07109                 ARGF.current_file = rb_stdin;
07110                 if (ARGF.inplace) {
07111                     rb_warn("Can't do inplace edit for stdio; skipping");
07112                     goto retry;
07113                 }
07114             }
07115             else {
07116                 VALUE write_io = Qnil;
07117                 int fr = rb_sysopen(ARGF.filename, O_RDONLY, 0);
07118 
07119                 if (ARGF.inplace) {
07120                     struct stat st;
07121 #ifndef NO_SAFE_RENAME
07122                     struct stat st2;
07123 #endif
07124                     VALUE str;
07125                     int fw;
07126 
07127                     if (TYPE(rb_stdout) == T_FILE && rb_stdout != orig_stdout) {
07128                         rb_io_close(rb_stdout);
07129                     }
07130                     fstat(fr, &st);
07131                     if (*ARGF.inplace) {
07132                         str = rb_str_new2(fn);
07133 #ifdef NO_LONG_FNAME
07134                         ruby_add_suffix(str, ARGF.inplace);
07135 #else
07136                         rb_str_cat2(str, ARGF.inplace);
07137 #endif
07138 #ifdef NO_SAFE_RENAME
07139                         (void)close(fr);
07140                         (void)unlink(RSTRING_PTR(str));
07141                         (void)rename(fn, RSTRING_PTR(str));
07142                         fr = rb_sysopen(str, O_RDONLY, 0);
07143 #else
07144                         if (rename(fn, RSTRING_PTR(str)) < 0) {
07145                             rb_warn("Can't rename %s to %s: %s, skipping file",
07146                                     fn, RSTRING_PTR(str), strerror(errno));
07147                             close(fr);
07148                             goto retry;
07149                         }
07150 #endif
07151                     }
07152                     else {
07153 #ifdef NO_SAFE_RENAME
07154                         rb_fatal("Can't do inplace edit without backup");
07155 #else
07156                         if (unlink(fn) < 0) {
07157                             rb_warn("Can't remove %s: %s, skipping file",
07158                                     fn, strerror(errno));
07159                             close(fr);
07160                             goto retry;
07161                         }
07162 #endif
07163                     }
07164                     fw = rb_sysopen(ARGF.filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
07165 #ifndef NO_SAFE_RENAME
07166                     fstat(fw, &st2);
07167 #ifdef HAVE_FCHMOD
07168                     fchmod(fw, st.st_mode);
07169 #else
07170                     chmod(fn, st.st_mode);
07171 #endif
07172                     if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
07173                         int err;
07174 #ifdef HAVE_FCHOWN
07175                         err = fchown(fw, st.st_uid, st.st_gid);
07176 #else
07177                         err = chown(fn, st.st_uid, st.st_gid);
07178 #endif
07179                         if (err && getuid() == 0 && st2.st_uid == 0) {
07180                             const char *wkfn = RSTRING_PTR(ARGF.filename);
07181                             rb_warn("Can't set owner/group of %s to same as %s: %s, skipping file",
07182                                     wkfn, fn, strerror(errno));
07183                             (void)close(fr);
07184                             (void)close(fw);
07185                             (void)unlink(wkfn);
07186                             goto retry;
07187                         }
07188                     }
07189 #endif
07190                     write_io = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
07191                     rb_stdout = write_io;
07192                     if (stdout_binmode) rb_io_binmode(rb_stdout);
07193                 }
07194                 fmode = FMODE_READABLE;
07195                 if (!ARGF.binmode) {
07196                     fmode |= DEFAULT_TEXTMODE;
07197                 }
07198                 ARGF.current_file = prep_io(fr, fmode, rb_cFile, fn);
07199                 if (!NIL_P(write_io)) {
07200                     rb_io_set_write_io(ARGF.current_file, write_io);
07201                 }
07202             }
07203             if (ARGF.binmode) rb_io_ascii8bit_binmode(ARGF.current_file);
07204             GetOpenFile(ARGF.current_file, fptr);
07205             if (ARGF.encs.enc) {
07206                 fptr->encs = ARGF.encs;
07207                 clear_codeconv(fptr);
07208             }
07209             else {
07210                 fptr->encs.ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
07211                 if (!ARGF.binmode) {
07212                     fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
07213 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
07214                     fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
07215 #endif
07216                 }
07217             }
07218             ARGF.next_p = 0;
07219         }
07220         else {
07221             ARGF.next_p = 1;
07222             return FALSE;
07223         }
07224     }
07225     else if (ARGF.next_p == -1) {
07226         ARGF.current_file = rb_stdin;
07227         ARGF.filename = rb_str_new2("-");
07228         if (ARGF.inplace) {
07229             rb_warn("Can't do inplace edit for stdio");
07230             rb_stdout = orig_stdout;
07231         }
07232     }
07233     return TRUE;
07234 }
07235 
07236 static VALUE
07237 argf_getline(int argc, VALUE *argv, VALUE argf)
07238 {
07239     VALUE line;
07240     long lineno = ARGF.lineno;
07241 
07242   retry:
07243     if (!next_argv()) return Qnil;
07244     if (ARGF_GENERIC_INPUT_P()) {
07245         line = rb_funcall3(ARGF.current_file, rb_intern("gets"), argc, argv);
07246     }
07247     else {
07248         if (argc == 0 && rb_rs == rb_default_rs) {
07249             line = rb_io_gets(ARGF.current_file);
07250         }
07251         else {
07252             line = rb_io_getline(argc, argv, ARGF.current_file);
07253         }
07254         if (NIL_P(line) && ARGF.next_p != -1) {
07255             argf_close(ARGF.current_file);
07256             ARGF.next_p = 1;
07257             goto retry;
07258         }
07259     }
07260     if (!NIL_P(line)) {
07261         ARGF.lineno = ++lineno;
07262         ARGF.last_lineno = ARGF.lineno;
07263     }
07264     return line;
07265 }
07266 
07267 static VALUE
07268 argf_lineno_getter(ID id, VALUE *var)
07269 {
07270     VALUE argf = *var;
07271     return INT2FIX(ARGF.last_lineno);
07272 }
07273 
07274 static void
07275 argf_lineno_setter(VALUE val, ID id, VALUE *var)
07276 {
07277     VALUE argf = *var;
07278     int n = NUM2INT(val);
07279     ARGF.last_lineno = ARGF.lineno = n;
07280 }
07281 
07282 static VALUE argf_gets(int, VALUE *, VALUE);
07283 
07284 /*
07285  *  call-seq:
07286  *     gets(sep=$/)    -> string or nil
07287  *     gets(limit)     -> string or nil
07288  *     gets(sep,limit) -> string or nil
07289  *
07290  *  Returns (and assigns to <code>$_</code>) the next line from the list
07291  *  of files in +ARGV+ (or <code>$*</code>), or from standard input if
07292  *  no files are present on the command line. Returns +nil+ at end of
07293  *  file. The optional argument specifies the record separator. The
07294  *  separator is included with the contents of each record. A separator
07295  *  of +nil+ reads the entire contents, and a zero-length separator
07296  *  reads the input one paragraph at a time, where paragraphs are
07297  *  divided by two consecutive newlines.  If the first argument is an
07298  *  integer, or optional second argument is given, the returning string
07299  *  would not be longer than the given value in bytes.  If multiple
07300  *  filenames are present in +ARGV+, +gets(nil)+ will read the contents
07301  *  one file at a time.
07302  *
07303  *     ARGV << "testfile"
07304  *     print while gets
07305  *
07306  *  <em>produces:</em>
07307  *
07308  *     This is line one
07309  *     This is line two
07310  *     This is line three
07311  *     And so on...
07312  *
07313  *  The style of programming using <code>$_</code> as an implicit
07314  *  parameter is gradually losing favor in the Ruby community.
07315  */
07316 
07317 static VALUE
07318 rb_f_gets(int argc, VALUE *argv, VALUE recv)
07319 {
07320     if (recv == argf) {
07321         return argf_gets(argc, argv, argf);
07322     }
07323     return rb_funcall2(argf, rb_intern("gets"), argc, argv);
07324 }
07325 
07326 /*
07327  *  call-seq:
07328  *     ARGF.gets(sep=$/)     -> string
07329  *     ARGF.gets(limit)      -> string
07330  *     ARGF.gets(sep, limit) -> string
07331  *
07332  *  Returns the next line from the current file in +ARGF+.
07333  *
07334  *  By default lines are assumed to be separated by +$/+; to use a different
07335  *  character as a separator, supply it as a +String+ for the _sep_ argument.
07336  *
07337  *  The optional  _limit_ argument specifies how many characters of each line
07338  *  to return. By default all characters are returned.
07339  *
07340  */
07341 static VALUE
07342 argf_gets(int argc, VALUE *argv, VALUE argf)
07343 {
07344     VALUE line;
07345 
07346     line = argf_getline(argc, argv, argf);
07347     rb_lastline_set(line);
07348 
07349     return line;
07350 }
07351 
07352 VALUE
07353 rb_gets(void)
07354 {
07355     VALUE line;
07356 
07357     if (rb_rs != rb_default_rs) {
07358         return rb_f_gets(0, 0, argf);
07359     }
07360 
07361   retry:
07362     if (!next_argv()) return Qnil;
07363     line = rb_io_gets(ARGF.current_file);
07364     if (NIL_P(line) && ARGF.next_p != -1) {
07365         rb_io_close(ARGF.current_file);
07366         ARGF.next_p = 1;
07367         goto retry;
07368     }
07369     rb_lastline_set(line);
07370     if (!NIL_P(line)) {
07371         ARGF.lineno++;
07372         ARGF.last_lineno = ARGF.lineno;
07373     }
07374 
07375     return line;
07376 }
07377 
07378 static VALUE argf_readline(int, VALUE *, VALUE);
07379 
07380 /*
07381  *  call-seq:
07382  *     readline(sep=$/)     -> string
07383  *     readline(limit)      -> string
07384  *     readline(sep, limit) -> string
07385  *
07386  *  Equivalent to <code>Kernel::gets</code>, except
07387  *  +readline+ raises +EOFError+ at end of file.
07388  */
07389 
07390 static VALUE
07391 rb_f_readline(int argc, VALUE *argv, VALUE recv)
07392 {
07393     if (recv == argf) {
07394         return argf_readline(argc, argv, argf);
07395     }
07396     return rb_funcall2(argf, rb_intern("readline"), argc, argv);
07397 }
07398 
07399 
07400 /*
07401  *  call-seq:
07402  *     ARGF.readline(sep=$/)     -> string
07403  *     ARGF.readline(limit)      -> string
07404  *     ARGF.readline(sep, limit) -> string
07405  *
07406  *  Returns the next line from the current file in +ARGF+.
07407  *
07408  *  By default lines are assumed to be separated by +$/+; to use a different
07409  *  character as a separator, supply it as a +String+ for the _sep_ argument.
07410  *
07411  *  The optional  _limit_ argument specifies how many characters of each line
07412  *  to return. By default all characters are returned.
07413  *
07414  *  An +EOFError+ is raised at the end of the file.
07415  */
07416 static VALUE
07417 argf_readline(int argc, VALUE *argv, VALUE argf)
07418 {
07419     VALUE line;
07420 
07421     if (!next_argv()) rb_eof_error();
07422     ARGF_FORWARD(argc, argv);
07423     line = argf_gets(argc, argv, argf);
07424     if (NIL_P(line)) {
07425         rb_eof_error();
07426     }
07427 
07428     return line;
07429 }
07430 
07431 static VALUE argf_readlines(int, VALUE *, VALUE);
07432 
07433 /*
07434  *  call-seq:
07435  *     readlines(sep=$/)    -> array
07436  *     readlines(limit)     -> array
07437  *     readlines(sep,limit) -> array
07438  *
07439  *  Returns an array containing the lines returned by calling
07440  *  <code>Kernel.gets(<i>sep</i>)</code> until the end of file.
07441  */
07442 
07443 static VALUE
07444 rb_f_readlines(int argc, VALUE *argv, VALUE recv)
07445 {
07446     if (recv == argf) {
07447         return argf_readlines(argc, argv, argf);
07448     }
07449     return rb_funcall2(argf, rb_intern("readlines"), argc, argv);
07450 }
07451 
07452 /*
07453  *  call-seq:
07454  *     ARGF.readlines(sep=$/)     -> array
07455  *     ARGF.readlines(limit)      -> array
07456  *     ARGF.readlines(sep, limit) -> array
07457  *
07458  *     ARGF.to_a(sep=$/)     -> array
07459  *     ARGF.to_a(limit)      -> array
07460  *     ARGF.to_a(sep, limit) -> array
07461  *
07462  *  Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
07463  *  lines, one line per element. Lines are assumed to be separated by _sep_.
07464  *
07465  *     lines = ARGF.readlines
07466  *     lines[0]                #=> "This is line one\n"
07467  */
07468 static VALUE
07469 argf_readlines(int argc, VALUE *argv, VALUE argf)
07470 {
07471     long lineno = ARGF.lineno;
07472     VALUE lines, ary;
07473 
07474     ary = rb_ary_new();
07475     while (next_argv()) {
07476         if (ARGF_GENERIC_INPUT_P()) {
07477             lines = rb_funcall3(ARGF.current_file, rb_intern("readlines"), argc, argv);
07478         }
07479         else {
07480             lines = rb_io_readlines(argc, argv, ARGF.current_file);
07481             argf_close(ARGF.current_file);
07482         }
07483         ARGF.next_p = 1;
07484         rb_ary_concat(ary, lines);
07485         ARGF.lineno = lineno + RARRAY_LEN(ary);
07486         ARGF.last_lineno = ARGF.lineno;
07487     }
07488     ARGF.init_p = 0;
07489     return ary;
07490 }
07491 
07492 /*
07493  *  call-seq:
07494  *     `cmd`    -> string
07495  *
07496  *  Returns the standard output of running _cmd_ in a subshell.
07497  *  The built-in syntax <code>%x{...}</code> uses
07498  *  this method. Sets <code>$?</code> to the process status.
07499  *
07500  *     `date`                   #=> "Wed Apr  9 08:56:30 CDT 2003\n"
07501  *     `ls testdir`.split[1]    #=> "main.rb"
07502  *     `echo oops && exit 99`   #=> "oops\n"
07503  *     $?.exitstatus            #=> 99
07504  */
07505 
07506 static VALUE
07507 rb_f_backquote(VALUE obj, VALUE str)
07508 {
07509     volatile VALUE port;
07510     VALUE result;
07511     rb_io_t *fptr;
07512 
07513     SafeStringValue(str);
07514     port = pipe_open_s(str, "r", FMODE_READABLE|DEFAULT_TEXTMODE, NULL);
07515     if (NIL_P(port)) return rb_str_new(0,0);
07516 
07517     GetOpenFile(port, fptr);
07518     result = read_all(fptr, remain_size(fptr), Qnil);
07519     rb_io_close(port);
07520 
07521     return result;
07522 }
07523 
07524 #ifdef HAVE_SYS_SELECT_H
07525 #include <sys/select.h>
07526 #endif
07527 
07528 static VALUE
07529 select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fdset_t *fds)
07530 {
07531     VALUE res, list;
07532     rb_fdset_t *rp, *wp, *ep;
07533     rb_io_t *fptr;
07534     long i;
07535     int max = 0, n;
07536     int pending = 0;
07537     struct timeval timerec;
07538 
07539     if (!NIL_P(read)) {
07540         Check_Type(read, T_ARRAY);
07541         for (i=0; i<RARRAY_LEN(read); i++) {
07542             GetOpenFile(rb_io_get_io(RARRAY_PTR(read)[i]), fptr);
07543             rb_fd_set(fptr->fd, &fds[0]);
07544             if (READ_DATA_PENDING(fptr) || READ_CHAR_PENDING(fptr)) { /* check for buffered data */
07545                 pending++;
07546                 rb_fd_set(fptr->fd, &fds[3]);
07547             }
07548             if (max < fptr->fd) max = fptr->fd;
07549         }
07550         if (pending) {          /* no blocking if there's buffered data */
07551             timerec.tv_sec = timerec.tv_usec = 0;
07552             tp = &timerec;
07553         }
07554         rp = &fds[0];
07555     }
07556     else
07557         rp = 0;
07558 
07559     if (!NIL_P(write)) {
07560         Check_Type(write, T_ARRAY);
07561         for (i=0; i<RARRAY_LEN(write); i++) {
07562             VALUE write_io = GetWriteIO(rb_io_get_io(RARRAY_PTR(write)[i]));
07563             GetOpenFile(write_io, fptr);
07564             rb_fd_set(fptr->fd, &fds[1]);
07565             if (max < fptr->fd) max = fptr->fd;
07566         }
07567         wp = &fds[1];
07568     }
07569     else
07570         wp = 0;
07571 
07572     if (!NIL_P(except)) {
07573         Check_Type(except, T_ARRAY);
07574         for (i=0; i<RARRAY_LEN(except); i++) {
07575             VALUE io = rb_io_get_io(RARRAY_PTR(except)[i]);
07576             VALUE write_io = GetWriteIO(io);
07577             GetOpenFile(io, fptr);
07578             rb_fd_set(fptr->fd, &fds[2]);
07579             if (max < fptr->fd) max = fptr->fd;
07580             if (io != write_io) {
07581                 GetOpenFile(write_io, fptr);
07582                 rb_fd_set(fptr->fd, &fds[2]);
07583                 if (max < fptr->fd) max = fptr->fd;
07584             }
07585         }
07586         ep = &fds[2];
07587     }
07588     else {
07589         ep = 0;
07590     }
07591 
07592     max++;
07593 
07594     n = rb_thread_fd_select(max, rp, wp, ep, tp);
07595     if (n < 0) {
07596         rb_sys_fail(0);
07597     }
07598     if (!pending && n == 0) return Qnil; /* returns nil on timeout */
07599 
07600     res = rb_ary_new2(3);
07601     rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
07602     rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
07603     rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
07604 
07605     if (rp) {
07606         list = RARRAY_PTR(res)[0];
07607         for (i=0; i< RARRAY_LEN(read); i++) {
07608             VALUE obj = rb_ary_entry(read, i);
07609             VALUE io = rb_io_get_io(obj);
07610             GetOpenFile(io, fptr);
07611             if (rb_fd_isset(fptr->fd, &fds[0]) ||
07612                 rb_fd_isset(fptr->fd, &fds[3])) {
07613                 rb_ary_push(list, obj);
07614             }
07615         }
07616     }
07617 
07618     if (wp) {
07619         list = RARRAY_PTR(res)[1];
07620         for (i=0; i< RARRAY_LEN(write); i++) {
07621             VALUE obj = rb_ary_entry(write, i);
07622             VALUE io = rb_io_get_io(obj);
07623             VALUE write_io = GetWriteIO(io);
07624             GetOpenFile(write_io, fptr);
07625             if (rb_fd_isset(fptr->fd, &fds[1])) {
07626                 rb_ary_push(list, obj);
07627             }
07628         }
07629     }
07630 
07631     if (ep) {
07632         list = RARRAY_PTR(res)[2];
07633         for (i=0; i< RARRAY_LEN(except); i++) {
07634             VALUE obj = rb_ary_entry(except, i);
07635             VALUE io = rb_io_get_io(obj);
07636             VALUE write_io = GetWriteIO(io);
07637             GetOpenFile(io, fptr);
07638             if (rb_fd_isset(fptr->fd, &fds[2])) {
07639                 rb_ary_push(list, obj);
07640             }
07641             else if (io != write_io) {
07642                 GetOpenFile(write_io, fptr);
07643                 if (rb_fd_isset(fptr->fd, &fds[2])) {
07644                     rb_ary_push(list, obj);
07645                 }
07646             }
07647         }
07648     }
07649 
07650     return res;                 /* returns an empty array on interrupt */
07651 }
07652 
07653 struct select_args {
07654     VALUE read, write, except;
07655     struct timeval *timeout;
07656     rb_fdset_t fdsets[4];
07657 };
07658 
07659 static VALUE
07660 select_call(VALUE arg)
07661 {
07662     struct select_args *p = (struct select_args *)arg;
07663 
07664     return select_internal(p->read, p->write, p->except, p->timeout, p->fdsets);
07665 }
07666 
07667 static VALUE
07668 select_end(VALUE arg)
07669 {
07670     struct select_args *p = (struct select_args *)arg;
07671     int i;
07672 
07673     for (i = 0; i < numberof(p->fdsets); ++i)
07674         rb_fd_term(&p->fdsets[i]);
07675     return Qnil;
07676 }
07677 
07678 static VALUE sym_normal,   sym_sequential, sym_random,
07679              sym_willneed, sym_dontneed, sym_noreuse;
07680 
07681 #ifdef HAVE_POSIX_FADVISE
07682 struct io_advise_struct {
07683     int fd;
07684     off_t offset;
07685     off_t len;
07686     int advice;
07687 };
07688 
07689 static VALUE
07690 io_advise_internal(void *arg)
07691 {
07692     struct io_advise_struct *ptr = arg;
07693     return posix_fadvise(ptr->fd, ptr->offset, ptr->len, ptr->advice);
07694 }
07695 
07696 static VALUE
07697 io_advise_sym_to_const(VALUE sym)
07698 {
07699 #ifdef POSIX_FADV_NORMAL
07700     if (sym == sym_normal)
07701         return INT2NUM(POSIX_FADV_NORMAL);
07702 #endif
07703 
07704 #ifdef POSIX_FADV_RANDOM
07705     if (sym == sym_random)
07706         return INT2NUM(POSIX_FADV_RANDOM);
07707 #endif
07708 
07709 #ifdef POSIX_FADV_SEQUENTIAL
07710     if (sym == sym_sequential)
07711         return INT2NUM(POSIX_FADV_SEQUENTIAL);
07712 #endif
07713 
07714 #ifdef POSIX_FADV_WILLNEED
07715     if (sym == sym_willneed)
07716         return INT2NUM(POSIX_FADV_WILLNEED);
07717 #endif
07718 
07719 #ifdef POSIX_FADV_DONTNEED
07720     if (sym == sym_dontneed)
07721         return INT2NUM(POSIX_FADV_DONTNEED);
07722 #endif
07723 
07724 #ifdef POSIX_FADV_NOREUSE
07725     if (sym == sym_noreuse)
07726         return INT2NUM(POSIX_FADV_NOREUSE);
07727 #endif
07728 
07729     return Qnil;
07730 }
07731 
07732 static VALUE
07733 do_io_advise(rb_io_t *fptr, VALUE advice, off_t offset, off_t len)
07734 {
07735     int rv;
07736     struct io_advise_struct ias;
07737     VALUE num_adv;
07738 
07739     num_adv = io_advise_sym_to_const(advice);
07740 
07741     /*
07742      * The platform doesn't support this hint. We don't raise exception, instead
07743      * silently ignore it. Because IO::advise is only hint.
07744      */
07745     if (num_adv == Qnil)
07746         return Qnil;
07747 
07748     ias.fd     = fptr->fd;
07749     ias.advice = NUM2INT(num_adv);
07750     ias.offset = offset;
07751     ias.len    = len;
07752 
07753     rv = (int)rb_thread_io_blocking_region(io_advise_internal, &ias, fptr->fd);
07754     if (rv)
07755         /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
07756            it returns the error code. */
07757         rb_syserr_fail(rv, RSTRING_PTR(fptr->pathv));
07758 
07759     return Qnil;
07760 }
07761 
07762 #endif /* HAVE_POSIX_FADVISE */
07763 
07764 static void
07765 advice_arg_check(VALUE advice)
07766 {
07767     if (!SYMBOL_P(advice))
07768         rb_raise(rb_eTypeError, "advice must be a Symbol");
07769 
07770     if (advice != sym_normal &&
07771         advice != sym_sequential &&
07772         advice != sym_random &&
07773         advice != sym_willneed &&
07774         advice != sym_dontneed &&
07775         advice != sym_noreuse) {
07776         VALUE symname = rb_inspect(advice);
07777         rb_raise(rb_eNotImpError, "Unsupported advice: %s",
07778                  StringValuePtr(symname));
07779     }
07780 }
07781 
07782 /*
07783  *  call-seq:
07784  *     ios.advise(advice, offset=0, len=0) -> nil
07785  *
07786  *  Announce an intention to access data from the current file in a
07787  *  specific pattern. On platforms that do not support the
07788  *  <em>posix_fadvise(2)</em> system call, this method is a no-op.
07789  *
07790  * _advice_ is one of the following symbols:
07791  *
07792  *  * :normal - No advice to give; the default assumption for an open file.
07793  *  * :sequential - The data will be accessed sequentially:
07794  *     with lower offsets read before higher ones.
07795  *  * :random - The data will be accessed in random order.
07796  *  * :willneed - The data will be accessed in the near future.
07797  *  * :dontneed - The data will not be accessed in the near future.
07798  *  * :noreuse - The data will only be accessed once.
07799  *
07800  * The semantics of a piece of advice are platform-dependent. See
07801  * <em>man 2 posix_fadvise</em> for details.
07802  *
07803  *  "data" means the region of the current file that begins at
07804  *  _offset_ and extends for _len_ bytes. If _len_ is 0, the region
07805  *  ends at the last byte of the file. By default, both _offset_ and
07806  *  _len_ are 0, meaning that the advice applies to the entire file.
07807  *
07808  *  If an error occurs, one of the following exceptions will be raised:
07809  *
07810  *  * <code>IOError</code> - The <code>IO</code> stream is closed.
07811  *  * <code>Errno::EBADF</code> - The file descriptor of the current file is
07812       invalid.
07813  *  * <code>Errno::EINVAL</code> - An invalid value for _advice_ was given.
07814  *  * <code>Errno::ESPIPE</code> - The file descriptor of the current
07815  *  * file refers to a FIFO or pipe. (Linux raises <code>Errno::EINVAL</code>
07816  *  * in this case).
07817  *  * <code>TypeError</code> - Either _advice_ was not a Symbol, or one of the
07818       other arguments was not an <code>Integer</code>.
07819  *  * <code>RangeError</code> - One of the arguments given was too big/small.
07820  *
07821  * This list is not exhaustive; other Errno:: exceptions are also possible.
07822  */
07823 static VALUE
07824 rb_io_advise(int argc, VALUE *argv, VALUE io)
07825 {
07826     VALUE advice, offset, len;
07827     off_t off, l;
07828     rb_io_t *fptr;
07829 
07830     rb_scan_args(argc, argv, "12", &advice, &offset, &len);
07831     advice_arg_check(advice);
07832 
07833     io = GetWriteIO(io);
07834     GetOpenFile(io, fptr);
07835 
07836     off = NIL_P(offset) ? 0 : NUM2OFFT(offset);
07837     l   = NIL_P(len)    ? 0 : NUM2OFFT(len);
07838 
07839 #ifdef HAVE_POSIX_FADVISE
07840     return do_io_advise(fptr, advice, off, l);
07841 #else
07842     /* Ignore all hint */
07843     return Qnil;
07844 #endif
07845 }
07846 
07847 /*
07848  *  call-seq:
07849  *     IO.select(read_array
07850  *               [, write_array
07851  *               [, error_array
07852  *               [, timeout]]]) -> array  or  nil
07853  *
07854  *  Calls select(2) system call.
07855  *  It monitors given arrays of <code>IO</code> objects, waits one or more
07856  *  of <code>IO</code> objects ready for reading, are ready for writing,
07857  *  and have pending exceptions respectably, and returns an array that
07858  *  contains arrays of those IO objects.  It will return <code>nil</code>
07859  *  if optional <i>timeout</i> value is given and no <code>IO</code> object
07860  *  is ready in <i>timeout</i> seconds.
07861  *
07862  *  === Parameters
07863  *  read_array:: an array of <code>IO</code> objects that wait until ready for read
07864  *  write_array:: an array of <code>IO</code> objects that wait until ready for write
07865  *  error_array:: an array of <code>IO</code> objects that wait for exceptions
07866  *  timeout:: a numeric value in second
07867  *
07868  *  === Example
07869  *
07870  *      rp, wp = IO.pipe
07871  *      mesg = "ping "
07872  *      100.times {
07873  *        rs, ws, = IO.select([rp], [wp])
07874  *        if r = rs[0]
07875  *          ret = r.read(5)
07876  *          print ret
07877  *          case ret
07878  *          when /ping/
07879  *            mesg = "pong\n"
07880  *          when /pong/
07881  *            mesg = "ping "
07882  *          end
07883  *        end
07884  *        if w = ws[0]
07885  *          w.write(mesg)
07886  *        end
07887  *      }
07888  *
07889  *  <em>produces:</em>
07890  *
07891  *      ping pong
07892  *      ping pong
07893  *      ping pong
07894  *      (snipped)
07895  *      ping
07896  */
07897 
07898 static VALUE
07899 rb_f_select(int argc, VALUE *argv, VALUE obj)
07900 {
07901     VALUE timeout;
07902     struct select_args args;
07903     struct timeval timerec;
07904     int i;
07905 
07906     rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
07907     if (NIL_P(timeout)) {
07908         args.timeout = 0;
07909     }
07910     else {
07911         timerec = rb_time_interval(timeout);
07912         args.timeout = &timerec;
07913     }
07914 
07915     for (i = 0; i < numberof(args.fdsets); ++i)
07916         rb_fd_init(&args.fdsets[i]);
07917 
07918     return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
07919 }
07920 
07921 struct io_cntl_arg {
07922     int         fd;
07923     int         cmd;
07924     long        narg;
07925     int         io_p;
07926 };
07927 
07928 static VALUE nogvl_io_cntl(void *ptr)
07929 {
07930     struct io_cntl_arg *arg = ptr;
07931 
07932     if (arg->io_p)
07933         return (VALUE)ioctl(arg->fd, arg->cmd, arg->narg);
07934     else
07935         return (VALUE)fcntl(arg->fd, arg->cmd, arg->narg);
07936 }
07937 
07938 static int
07939 io_cntl(int fd, int cmd, long narg, int io_p)
07940 {
07941     int retval;
07942     struct io_cntl_arg arg;
07943 
07944 #ifndef HAVE_FCNTL
07945     if (!io_p) {
07946         rb_notimplement();
07947     }
07948 #endif
07949 
07950     arg.fd = fd;
07951     arg.cmd = cmd;
07952     arg.narg = narg;
07953     arg.io_p = io_p;
07954 
07955     retval = (int)rb_thread_io_blocking_region(nogvl_io_cntl, &arg, fd);
07956 #if defined(F_DUPFD)
07957     if (!io_p && retval != -1 && cmd == F_DUPFD) {
07958         rb_update_max_fd(retval);
07959     }
07960 #endif
07961 
07962     return retval;
07963 }
07964 
07965 static VALUE
07966 rb_io_ctl(VALUE io, VALUE req, VALUE arg, int io_p)
07967 {
07968     int cmd = NUM2INT(req);
07969     rb_io_t *fptr;
07970     long len = 0;
07971     long narg = 0;
07972     int retval;
07973 
07974     rb_secure(2);
07975 
07976     if (NIL_P(arg) || arg == Qfalse) {
07977         narg = 0;
07978     }
07979     else if (FIXNUM_P(arg)) {
07980         narg = FIX2LONG(arg);
07981     }
07982     else if (arg == Qtrue) {
07983         narg = 1;
07984     }
07985     else {
07986         VALUE tmp = rb_check_string_type(arg);
07987 
07988         if (NIL_P(tmp)) {
07989             narg = NUM2LONG(arg);
07990         }
07991         else {
07992             arg = tmp;
07993 #ifdef IOCPARM_MASK
07994 #ifndef IOCPARM_LEN
07995 #define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
07996 #endif
07997 #endif
07998 #ifdef IOCPARM_LEN
07999             len = IOCPARM_LEN(cmd);     /* on BSDish systems we're safe */
08000 #else
08001             len = 256;          /* otherwise guess at what's safe */
08002 #endif
08003             rb_str_modify(arg);
08004 
08005             if (len <= RSTRING_LEN(arg)) {
08006                 len = RSTRING_LEN(arg);
08007             }
08008             if (RSTRING_LEN(arg) < len) {
08009                 rb_str_resize(arg, len+1);
08010             }
08011             RSTRING_PTR(arg)[len] = 17; /* a little sanity check here */
08012             narg = (long)(SIGNED_VALUE)RSTRING_PTR(arg);
08013         }
08014     }
08015     GetOpenFile(io, fptr);
08016     retval = io_cntl(fptr->fd, cmd, narg, io_p);
08017     if (retval < 0) rb_sys_fail_path(fptr->pathv);
08018     if (TYPE(arg) == T_STRING && RSTRING_PTR(arg)[len] != 17) {
08019         rb_raise(rb_eArgError, "return value overflowed string");
08020     }
08021 
08022     if (!io_p && cmd == F_SETFL) {
08023         if (narg & O_NONBLOCK) {
08024             fptr->mode |= FMODE_WSPLIT_INITIALIZED;
08025             fptr->mode &= ~FMODE_WSPLIT;
08026         }
08027         else {
08028             fptr->mode &= ~(FMODE_WSPLIT_INITIALIZED|FMODE_WSPLIT);
08029         }
08030     }
08031 
08032     return INT2NUM(retval);
08033 }
08034 
08035 
08036 /*
08037  *  call-seq:
08038  *     ios.ioctl(integer_cmd, arg)    -> integer
08039  *
08040  *  Provides a mechanism for issuing low-level commands to control or
08041  *  query I/O devices. Arguments and results are platform dependent. If
08042  *  <i>arg</i> is a number, its value is passed directly. If it is a
08043  *  string, it is interpreted as a binary sequence of bytes. On Unix
08044  *  platforms, see <code>ioctl(2)</code> for details. Not implemented on
08045  *  all platforms.
08046  */
08047 
08048 static VALUE
08049 rb_io_ioctl(int argc, VALUE *argv, VALUE io)
08050 {
08051     VALUE req, arg;
08052 
08053     rb_scan_args(argc, argv, "11", &req, &arg);
08054     return rb_io_ctl(io, req, arg, 1);
08055 }
08056 
08057 #ifdef HAVE_FCNTL
08058 /*
08059  *  call-seq:
08060  *     ios.fcntl(integer_cmd, arg)    -> integer
08061  *
08062  *  Provides a mechanism for issuing low-level commands to control or
08063  *  query file-oriented I/O streams. Arguments and results are platform
08064  *  dependent. If <i>arg</i> is a number, its value is passed
08065  *  directly. If it is a string, it is interpreted as a binary sequence
08066  *  of bytes (<code>Array#pack</code> might be a useful way to build this
08067  *  string). On Unix platforms, see <code>fcntl(2)</code> for details.
08068  *  Not implemented on all platforms.
08069  */
08070 
08071 static VALUE
08072 rb_io_fcntl(int argc, VALUE *argv, VALUE io)
08073 {
08074     VALUE req, arg;
08075 
08076     rb_scan_args(argc, argv, "11", &req, &arg);
08077     return rb_io_ctl(io, req, arg, 0);
08078 }
08079 #else
08080 #define rb_io_fcntl rb_f_notimplement
08081 #endif
08082 
08083 #if defined(HAVE_SYSCALL) || defined(HAVE___SYSCALL)
08084 /*
08085  *  call-seq:
08086  *     syscall(num [, args...])   -> integer
08087  *
08088  *  Calls the operating system function identified by _num_ and
08089  *  returns the result of the function or raises SystemCallError if
08090  *  it failed.
08091  *
08092  *  Arguments for the function can follow _num_. They must be either
08093  *  +String+ objects or +Integer+ objects. A +String+ object is passed
08094  *  as a pointer to the byte sequence. An +Integer+ object is passed
08095  *  as an integer whose bit size is same as a pointer.
08096  *  Up to nine parameters may be passed (14 on the Atari-ST).
08097  *
08098  *  The function identified by _num_ is system
08099  *  dependent. On some Unix systems, the numbers may be obtained from a
08100  *  header file called <code>syscall.h</code>.
08101  *
08102  *     syscall 4, 1, "hello\n", 6   # '4' is write(2) on our box
08103  *
08104  *  <em>produces:</em>
08105  *
08106  *     hello
08107  *
08108  *
08109  *  Calling +syscall+ on a platform which does not have any way to
08110  *  an arbitrary system function just fails with NotImplementedError.
08111  *
08112  * Note::
08113  *   +syscall+ is essentially unsafe and unportable. Feel free to shoot your foot.
08114  *   DL (Fiddle) library is preferred for safer and a bit more portable programming.
08115  */
08116 
08117 static VALUE
08118 rb_f_syscall(int argc, VALUE *argv)
08119 {
08120 #ifdef atarist
08121     VALUE arg[13]; /* yes, we really need that many ! */
08122 #else
08123     VALUE arg[8];
08124 #endif
08125 #if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */
08126 # define SYSCALL __syscall
08127 # define NUM2SYSCALLID(x) NUM2LONG(x)
08128 # define RETVAL2NUM(x) LONG2NUM(x)
08129 # if SIZEOF_LONG == 8
08130     long num, retval = -1;
08131 # elif SIZEOF_LONG_LONG == 8
08132     long long num, retval = -1;
08133 # else
08134 #  error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
08135 # endif
08136 #elif defined linux
08137 # define SYSCALL syscall
08138 # define NUM2SYSCALLID(x) NUM2LONG(x)
08139 # define RETVAL2NUM(x) LONG2NUM(x)
08140     /*
08141      * Linux man page says, syscall(2) function prototype is below.
08142      *
08143      *     int syscall(int number, ...);
08144      *
08145      * But, it's incorrect. Actual one takes and returned long. (see unistd.h)
08146      */
08147     long num, retval = -1;
08148 #else
08149 # define SYSCALL syscall
08150 # define NUM2SYSCALLID(x) NUM2INT(x)
08151 # define RETVAL2NUM(x) INT2NUM(x)
08152     int num, retval = -1;
08153 #endif
08154     int i;
08155 
08156     if (RTEST(ruby_verbose)) {
08157         rb_warning("We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
08158     }
08159 
08160     rb_secure(2);
08161     if (argc == 0)
08162         rb_raise(rb_eArgError, "too few arguments for syscall");
08163     if (argc > numberof(arg))
08164         rb_raise(rb_eArgError, "too many arguments for syscall");
08165     num = NUM2SYSCALLID(argv[0]); ++argv;
08166     for (i = argc - 1; i--; ) {
08167         VALUE v = rb_check_string_type(argv[i]);
08168 
08169         if (!NIL_P(v)) {
08170             SafeStringValue(v);
08171             rb_str_modify(v);
08172             arg[i] = (VALUE)StringValueCStr(v);
08173         }
08174         else {
08175             arg[i] = (VALUE)NUM2LONG(argv[i]);
08176         }
08177     }
08178 
08179     switch (argc) {
08180       case 1:
08181         retval = SYSCALL(num);
08182         break;
08183       case 2:
08184         retval = SYSCALL(num, arg[0]);
08185         break;
08186       case 3:
08187         retval = SYSCALL(num, arg[0],arg[1]);
08188         break;
08189       case 4:
08190         retval = SYSCALL(num, arg[0],arg[1],arg[2]);
08191         break;
08192       case 5:
08193         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
08194         break;
08195       case 6:
08196         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
08197         break;
08198       case 7:
08199         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
08200         break;
08201       case 8:
08202         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
08203         break;
08204 #ifdef atarist
08205       case 9:
08206         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08207           arg[7]);
08208         break;
08209       case 10:
08210         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08211           arg[7], arg[8]);
08212         break;
08213       case 11:
08214         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08215           arg[7], arg[8], arg[9]);
08216         break;
08217       case 12:
08218         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08219           arg[7], arg[8], arg[9], arg[10]);
08220         break;
08221       case 13:
08222         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08223           arg[7], arg[8], arg[9], arg[10], arg[11]);
08224         break;
08225       case 14:
08226         retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
08227           arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
08228         break;
08229 #endif
08230     }
08231 
08232     if (retval == -1)
08233         rb_sys_fail(0);
08234     return RETVAL2NUM(retval);
08235 #undef SYSCALL
08236 #undef NUM2SYSCALLID
08237 #undef RETVAL2NUM
08238 }
08239 #else
08240 #define rb_f_syscall rb_f_notimplement
08241 #endif
08242 
08243 static VALUE
08244 io_new_instance(VALUE args)
08245 {
08246     return rb_class_new_instance(2, (VALUE*)args+1, *(VALUE*)args);
08247 }
08248 
08249 static void
08250 io_encoding_set(rb_io_t *fptr, VALUE v1, VALUE v2, VALUE opt)
08251 {
08252     rb_encoding *enc, *enc2;
08253     int ecflags = fptr->encs.ecflags;
08254     VALUE ecopts, tmp;
08255 
08256     if (!NIL_P(v2)) {
08257         enc2 = rb_to_encoding(v1);
08258         tmp = rb_check_string_type(v2);
08259         if (!NIL_P(tmp)) {
08260             if (RSTRING_LEN(tmp) == 1 && RSTRING_PTR(tmp)[0] == '-') {
08261                 /* Special case - "-" => no transcoding */
08262                 enc = enc2;
08263                 enc2 = NULL;
08264             }
08265             else
08266                 enc = rb_to_encoding(v2);
08267             if (enc == enc2) {
08268                 /* Special case - "-" => no transcoding */
08269                 enc2 = NULL;
08270             }
08271         }
08272         else
08273             enc = rb_to_encoding(v2);
08274         SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
08275         ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
08276     }
08277     else {
08278         if (NIL_P(v1)) {
08279             /* Set to default encodings */
08280             rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2);
08281             SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
08282             ecopts = Qnil;
08283         }
08284         else {
08285             tmp = rb_check_string_type(v1);
08286             if (!NIL_P(tmp) && rb_enc_asciicompat(rb_enc_get(tmp))) {
08287                 parse_mode_enc(RSTRING_PTR(tmp), &enc, &enc2, NULL);
08288                 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
08289                 ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
08290             }
08291             else {
08292                 rb_io_ext_int_to_encs(rb_to_encoding(v1), NULL, &enc, &enc2);
08293                 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
08294                 ecopts = Qnil;
08295             }
08296         }
08297     }
08298     validate_enc_binmode(&fptr->mode, ecflags, enc, enc2);
08299     fptr->encs.enc = enc;
08300     fptr->encs.enc2 = enc2;
08301     fptr->encs.ecflags = ecflags;
08302     fptr->encs.ecopts = ecopts;
08303     clear_codeconv(fptr);
08304 
08305 }
08306 
08307 static VALUE
08308 pipe_pair_close(VALUE rw)
08309 {
08310     VALUE *rwp = (VALUE *)rw;
08311     return rb_ensure(io_close, rwp[0], io_close, rwp[1]);
08312 }
08313 
08314 /*
08315  *  call-seq:
08316  *     IO.pipe                             ->  [read_io, write_io]
08317  *     IO.pipe(ext_enc)                    ->  [read_io, write_io]
08318  *     IO.pipe("ext_enc:int_enc" [, opt])  ->  [read_io, write_io]
08319  *     IO.pipe(ext_enc, int_enc [, opt])   ->  [read_io, write_io]
08320  *
08321  *     IO.pipe(...) {|read_io, write_io| ... }
08322  *
08323  *  Creates a pair of pipe endpoints (connected to each other) and
08324  *  returns them as a two-element array of <code>IO</code> objects:
08325  *  <code>[</code> <i>read_io</i>, <i>write_io</i> <code>]</code>.
08326  *
08327  *  If a block is given, the block is called and
08328  *  returns the value of the block.
08329  *  <i>read_io</i> and <i>write_io</i> are sent to the block as arguments.
08330  *  If read_io and write_io are not closed when the block exits, they are closed.
08331  *  i.e. closing read_io and/or write_io doesn't cause an error.
08332  *
08333  *  Not available on all platforms.
08334  *
08335  *  If an encoding (encoding name or encoding object) is specified as an optional argument,
08336  *  read string from pipe is tagged with the encoding specified.
08337  *  If the argument is a colon separated two encoding names "A:B",
08338  *  the read string is converted from encoding A (external encoding)
08339  *  to encoding B (internal encoding), then tagged with B.
08340  *  If two optional arguments are specified, those must be
08341  *  encoding objects or encoding names,
08342  *  and the first one is the external encoding,
08343  *  and the second one is the internal encoding.
08344  *  If the external encoding and the internal encoding is specified,
08345  *  optional hash argument specify the conversion option.
08346  *
08347  *  In the example below, the two processes close the ends of the pipe
08348  *  that they are not using. This is not just a cosmetic nicety. The
08349  *  read end of a pipe will not generate an end of file condition if
08350  *  there are any writers with the pipe still open. In the case of the
08351  *  parent process, the <code>rd.read</code> will never return if it
08352  *  does not first issue a <code>wr.close</code>.
08353  *
08354  *     rd, wr = IO.pipe
08355  *
08356  *     if fork
08357  *       wr.close
08358  *       puts "Parent got: <#{rd.read}>"
08359  *       rd.close
08360  *       Process.wait
08361  *     else
08362  *       rd.close
08363  *       puts "Sending message to parent"
08364  *       wr.write "Hi Dad"
08365  *       wr.close
08366  *     end
08367  *
08368  *  <em>produces:</em>
08369  *
08370  *     Sending message to parent
08371  *     Parent got: <Hi Dad>
08372  */
08373 
08374 static VALUE
08375 rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
08376 {
08377     int pipes[2], state;
08378     VALUE r, w, args[3], v1, v2;
08379     VALUE opt;
08380     rb_io_t *fptr, *fptr2;
08381     int fmode = 0;
08382     VALUE ret;
08383 
08384     argc = rb_scan_args(argc, argv, "02:", &v1, &v2, &opt);
08385     if (rb_pipe(pipes) == -1)
08386         rb_sys_fail(0);
08387 
08388     args[0] = klass;
08389     args[1] = INT2NUM(pipes[0]);
08390     args[2] = INT2FIX(O_RDONLY);
08391     r = rb_protect(io_new_instance, (VALUE)args, &state);
08392     if (state) {
08393         close(pipes[0]);
08394         close(pipes[1]);
08395         rb_jump_tag(state);
08396     }
08397     GetOpenFile(r, fptr);
08398     io_encoding_set(fptr, v1, v2, opt);
08399     args[1] = INT2NUM(pipes[1]);
08400     args[2] = INT2FIX(O_WRONLY);
08401     w = rb_protect(io_new_instance, (VALUE)args, &state);
08402     if (state) {
08403         close(pipes[1]);
08404         if (!NIL_P(r)) rb_io_close(r);
08405         rb_jump_tag(state);
08406     }
08407     GetOpenFile(w, fptr2);
08408     rb_io_synchronized(fptr2);
08409 
08410     extract_binmode(opt, &fmode);
08411 #if DEFAULT_TEXTMODE
08412     if ((fptr->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
08413         fptr->mode &= ~FMODE_TEXTMODE;
08414         setmode(fptr->fd, O_BINARY);
08415     }
08416 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
08417     if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
08418         fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
08419     }
08420 #endif
08421 #endif
08422     fptr->mode |= fmode;
08423 #if DEFAULT_TEXTMODE
08424     if ((fptr2->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
08425         fptr2->mode &= ~FMODE_TEXTMODE;
08426         setmode(fptr2->fd, O_BINARY);
08427     }
08428 #endif
08429     fptr2->mode |= fmode;
08430 
08431     ret = rb_assoc_new(r, w);
08432     if (rb_block_given_p()) {
08433         VALUE rw[2];
08434         rw[0] = r;
08435         rw[1] = w;
08436         return rb_ensure(rb_yield, ret, pipe_pair_close, (VALUE)rw);
08437     }
08438     return ret;
08439 }
08440 
08441 struct foreach_arg {
08442     int argc;
08443     VALUE *argv;
08444     VALUE io;
08445 };
08446 
08447 static void
08448 open_key_args(int argc, VALUE *argv, VALUE opt, struct foreach_arg *arg)
08449 {
08450     VALUE path, v;
08451 
08452     path = *argv++;
08453     argc--;
08454     FilePathValue(path);
08455     arg->io = 0;
08456     arg->argc = argc;
08457     arg->argv = argv;
08458     if (NIL_P(opt)) {
08459         arg->io = rb_io_open(path, INT2NUM(O_RDONLY), INT2FIX(0666), Qnil);
08460         return;
08461     }
08462     v = rb_hash_aref(opt, sym_open_args);
08463     if (!NIL_P(v)) {
08464         VALUE args;
08465         long n;
08466 
08467         v = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
08468         n = RARRAY_LEN(v) + 1;
08469 #if SIZEOF_LONG > SIZEOF_INT
08470         if (n > INT_MAX) {
08471             rb_raise(rb_eArgError, "too many arguments");
08472         }
08473 #endif
08474         args = rb_ary_tmp_new(n);
08475         rb_ary_push(args, path);
08476         rb_ary_concat(args, v);
08477         arg->io = rb_io_open_with_args((int)n, RARRAY_PTR(args));
08478         rb_ary_clear(args);     /* prevent from GC */
08479         return;
08480     }
08481     arg->io = rb_io_open(path, Qnil, Qnil, opt);
08482 }
08483 
08484 static VALUE
08485 io_s_foreach(struct foreach_arg *arg)
08486 {
08487     VALUE str;
08488 
08489     while (!NIL_P(str = rb_io_gets_m(arg->argc, arg->argv, arg->io))) {
08490         rb_yield(str);
08491     }
08492     return Qnil;
08493 }
08494 
08495 /*
08496  *  call-seq:
08497  *     IO.foreach(name, sep=$/ [, open_args]) {|line| block }     -> nil
08498  *     IO.foreach(name, limit [, open_args]) {|line| block }      -> nil
08499  *     IO.foreach(name, sep, limit [, open_args]) {|line| block } -> nil
08500  *     IO.foreach(...)                                            -> an_enumerator
08501  *
08502  *  Executes the block for every line in the named I/O port, where lines
08503  *  are separated by <em>sep</em>.
08504  *
08505  *  If no block is given, an enumerator is returned instead.
08506  *
08507  *     IO.foreach("testfile") {|x| print "GOT ", x }
08508  *
08509  *  <em>produces:</em>
08510  *
08511  *     GOT This is line one
08512  *     GOT This is line two
08513  *     GOT This is line three
08514  *     GOT And so on...
08515  *
08516  *  If the last argument is a hash, it's the keyword argument to open.
08517  *  See <code>IO.read</code> for detail.
08518  *
08519  */
08520 
08521 static VALUE
08522 rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
08523 {
08524     VALUE opt;
08525     int orig_argc = argc;
08526     struct foreach_arg arg;
08527 
08528     argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
08529     RETURN_ENUMERATOR(self, orig_argc, argv);
08530     open_key_args(argc, argv, opt, &arg);
08531     if (NIL_P(arg.io)) return Qnil;
08532     return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);
08533 }
08534 
08535 static VALUE
08536 io_s_readlines(struct foreach_arg *arg)
08537 {
08538     return rb_io_readlines(arg->argc, arg->argv, arg->io);
08539 }
08540 
08541 /*
08542  *  call-seq:
08543  *     IO.readlines(name, sep=$/ [, open_args])     -> array
08544  *     IO.readlines(name, limit [, open_args])      -> array
08545  *     IO.readlines(name, sep, limit [, open_args]) -> array
08546  *
08547  *  Reads the entire file specified by <i>name</i> as individual
08548  *  lines, and returns those lines in an array. Lines are separated by
08549  *  <i>sep</i>.
08550  *
08551  *     a = IO.readlines("testfile")
08552  *     a[0]   #=> "This is line one\n"
08553  *
08554  *  If the last argument is a hash, it's the keyword argument to open.
08555  *  See <code>IO.read</code> for detail.
08556  *
08557  */
08558 
08559 static VALUE
08560 rb_io_s_readlines(int argc, VALUE *argv, VALUE io)
08561 {
08562     VALUE opt;
08563     struct foreach_arg arg;
08564 
08565     argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
08566     open_key_args(argc, argv, opt, &arg);
08567     if (NIL_P(arg.io)) return Qnil;
08568     return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
08569 }
08570 
08571 static VALUE
08572 io_s_read(struct foreach_arg *arg)
08573 {
08574     return io_read(arg->argc, arg->argv, arg->io);
08575 }
08576 
08577 struct seek_arg {
08578     VALUE io;
08579     VALUE offset;
08580     int mode;
08581 };
08582 
08583 static VALUE
08584 seek_before_access(VALUE argp)
08585 {
08586     struct seek_arg *arg = (struct seek_arg *)argp;
08587     rb_io_binmode(arg->io);
08588     return rb_io_seek(arg->io, arg->offset, arg->mode);
08589 }
08590 
08591 /*
08592  *  call-seq:
08593  *     IO.read(name, [length [, offset]] )   -> string
08594  *     IO.read(name, [length [, offset]], open_args)   -> string
08595  *
08596  *  Opens the file, optionally seeks to the given <i>offset</i>, then returns
08597  *  <i>length</i> bytes (defaulting to the rest of the file).
08598  *  <code>read</code> ensures the file is closed before returning.
08599  *
08600  *  If the last argument is a hash, it specifies option for internal
08601  *  open().  The key would be the following.  open_args: is exclusive
08602  *  to others.
08603  *
08604  *   encoding: string or encoding
08605  *
08606  *    specifies encoding of the read string.  encoding will be ignored
08607  *    if length is specified.
08608  *
08609  *   mode: string
08610  *
08611  *    specifies mode argument for open().  it should start with "r"
08612  *    otherwise it would cause error.
08613  *
08614  *   open_args: array of strings
08615  *
08616  *    specifies arguments for open() as an array.
08617  *
08618  *     IO.read("testfile")           #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
08619  *     IO.read("testfile", 20)       #=> "This is line one\nThi"
08620  *     IO.read("testfile", 20, 10)   #=> "ne one\nThis is line "
08621  */
08622 
08623 static VALUE
08624 rb_io_s_read(int argc, VALUE *argv, VALUE io)
08625 {
08626     VALUE opt, offset;
08627     struct foreach_arg arg;
08628 
08629     argc = rb_scan_args(argc, argv, "13:", NULL, NULL, &offset, NULL, &opt);
08630     open_key_args(argc, argv, opt, &arg);
08631     if (NIL_P(arg.io)) return Qnil;
08632     if (!NIL_P(offset)) {
08633         struct seek_arg sarg;
08634         int state = 0;
08635         sarg.io = arg.io;
08636         sarg.offset = offset;
08637         sarg.mode = SEEK_SET;
08638         rb_protect(seek_before_access, (VALUE)&sarg, &state);
08639         if (state) {
08640             rb_io_close(arg.io);
08641             rb_jump_tag(state);
08642         }
08643         if (arg.argc == 2) arg.argc = 1;
08644     }
08645     return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
08646 }
08647 
08648 /*
08649  *  call-seq:
08650  *     IO.binread(name, [length [, offset]] )   -> string
08651  *
08652  *  Opens the file, optionally seeks to the given <i>offset</i>, then returns
08653  *  <i>length</i> bytes (defaulting to the rest of the file).
08654  *  <code>binread</code> ensures the file is closed before returning.
08655  *  The open mode would be "rb:ASCII-8BIT".
08656  *
08657  *     IO.binread("testfile")           #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
08658  *     IO.binread("testfile", 20)       #=> "This is line one\nThi"
08659  *     IO.binread("testfile", 20, 10)   #=> "ne one\nThis is line "
08660  */
08661 
08662 static VALUE
08663 rb_io_s_binread(int argc, VALUE *argv, VALUE io)
08664 {
08665     VALUE offset;
08666     struct foreach_arg arg;
08667 
08668     rb_scan_args(argc, argv, "12", NULL, NULL, &offset);
08669     FilePathValue(argv[0]);
08670     arg.io = rb_io_open(argv[0], rb_str_new_cstr("rb:ASCII-8BIT"), Qnil, Qnil);
08671     if (NIL_P(arg.io)) return Qnil;
08672     arg.argv = argv+1;
08673     arg.argc = (argc > 1) ? 1 : 0;
08674     if (!NIL_P(offset)) {
08675         rb_io_seek(arg.io, offset, SEEK_SET);
08676     }
08677     return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
08678 }
08679 
08680 static VALUE
08681 io_s_write0(struct write_arg *arg)
08682 {
08683     return io_write(arg->io,arg->str,arg->nosync);
08684 }
08685 
08686 static VALUE
08687 io_s_write(int argc, VALUE *argv, int binary)
08688 {
08689     VALUE string, offset, opt;
08690     struct foreach_arg arg;
08691     struct write_arg warg;
08692 
08693     rb_scan_args(argc, argv, "21:", NULL, &string, &offset, &opt);
08694 
08695     if (NIL_P(opt)) opt = rb_hash_new();
08696     else opt = rb_hash_dup(opt);
08697 
08698 
08699     if (NIL_P(rb_hash_aref(opt,sym_mode))) {
08700        int mode = O_WRONLY|O_CREAT;
08701 #ifdef O_BINARY
08702        if (binary) mode |= O_BINARY;
08703 #endif
08704        if (NIL_P(offset)) mode |= O_TRUNC;
08705        rb_hash_aset(opt,sym_mode,INT2NUM(mode));
08706     }
08707     open_key_args(argc,argv,opt,&arg);
08708 
08709 #ifndef O_BINARY
08710     if (binary) rb_io_binmode_m(arg.io);
08711 #endif
08712 
08713     if (NIL_P(arg.io)) return Qnil;
08714     if (!NIL_P(offset)) {
08715        struct seek_arg sarg;
08716        int state = 0;
08717        sarg.io = arg.io;
08718        sarg.offset = offset;
08719        sarg.mode = SEEK_SET;
08720        rb_protect(seek_before_access, (VALUE)&sarg, &state);
08721        if (state) {
08722            rb_io_close(arg.io);
08723            rb_jump_tag(state);
08724        }
08725     }
08726 
08727     warg.io = arg.io;
08728     warg.str = string;
08729     warg.nosync = 0;
08730 
08731     return rb_ensure(io_s_write0, (VALUE)&warg, rb_io_close, arg.io);
08732 }
08733 
08734 /*
08735  *  call-seq:
08736  *     IO.write(name, string, [offset] )   => fixnum
08737  *     IO.write(name, string, [offset], open_args )   => fixnum
08738  *
08739  *  Opens the file, optionally seeks to the given <i>offset</i>, writes
08740  *  <i>string</i>, then returns the length written.
08741  *  <code>write</code> ensures the file is closed before returning.
08742  *  If <i>offset</i> is not given, the file is truncated.  Otherwise,
08743  *  it is not truncated.
08744  *
08745  *  If the last argument is a hash, it specifies option for internal
08746  *  open().  The key would be the following.  open_args: is exclusive
08747  *  to others.
08748  *
08749  *   encoding: string or encoding
08750  *
08751  *    specifies encoding of the read string.  encoding will be ignored
08752  *    if length is specified.
08753  *
08754  *   mode: string
08755  *
08756  *    specifies mode argument for open().  it should start with "w" or "a" or "r+"
08757  *    otherwise it would cause error.
08758  *
08759  *   perm: fixnum
08760  *
08761  *    specifies perm argument for open().
08762  *
08763  *   open_args: array
08764  *
08765  *    specifies arguments for open() as an array.
08766  *
08767  *     IO.write("testfile", "0123456789", 20) # => 10
08768  *     # File could contain:  "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
08769  *     IO.write("testfile", "0123456789")      #=> 10
08770  *     # File would now read: "0123456789"
08771  */
08772 
08773 static VALUE
08774 rb_io_s_write(int argc, VALUE *argv, VALUE io)
08775 {
08776     return io_s_write(argc, argv, 0);
08777 }
08778 
08779 /*
08780  *  call-seq:
08781  *     IO.binwrite(name, string, [offset] )   => fixnum
08782  *
08783  *  Opens the file, optionally seeks to the given <i>offset</i>, writes
08784  *  <i>string</i> then returns the length written.
08785  *  <code>binwrite</code> ensures the file is closed before returning.
08786  *  The open mode would be "wb:ASCII-8BIT".
08787  *  If <i>offset</i> is not given, the file is truncated.  Otherwise,
08788  *  it is not truncated.
08789  *
08790  *     IO.binwrite("testfile", "0123456789", 20) # => 10
08791  *     # File could contain:  "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
08792  *     IO.binwrite("testfile", "0123456789")      #=> 10
08793  *     # File would now read: "0123456789"
08794  */
08795 
08796 static VALUE
08797 rb_io_s_binwrite(int argc, VALUE *argv, VALUE io)
08798 {
08799     return io_s_write(argc, argv, 1);
08800 }
08801 
08802 struct copy_stream_struct {
08803     VALUE src;
08804     VALUE dst;
08805     off_t copy_length; /* (off_t)-1 if not specified */
08806     off_t src_offset; /* (off_t)-1 if not specified */
08807 
08808     int src_fd;
08809     int dst_fd;
08810     int close_src;
08811     int close_dst;
08812     off_t total;
08813     const char *syserr;
08814     int error_no;
08815     const char *notimp;
08816     rb_fdset_t fds;
08817     VALUE th;
08818 };
08819 
08820 static void *
08821 exec_interrupts(void *arg)
08822 {
08823     VALUE th = (VALUE)arg;
08824     rb_thread_execute_interrupts(th);
08825     return NULL;
08826 }
08827 
08828 /*
08829  * returns TRUE if the preceding system call was interrupted
08830  * so we can continue.  If the thread was interrupted, we
08831  * reacquire the GVL to execute interrupts before continuing.
08832  */
08833 static int
08834 maygvl_copy_stream_continue_p(int has_gvl, struct copy_stream_struct *stp)
08835 {
08836     switch (errno) {
08837       case EINTR:
08838 #if defined(ERESTART)
08839       case ERESTART:
08840 #endif
08841         if (rb_thread_interrupted(stp->th)) {
08842             if (has_gvl)
08843                 rb_thread_execute_interrupts(stp->th);
08844             else
08845                 rb_thread_call_with_gvl(exec_interrupts, (void *)stp->th);
08846         }
08847         return TRUE;
08848     }
08849     return FALSE;
08850 }
08851 
08852 static int
08853 maygvl_select(int has_gvl, int n, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
08854 {
08855     if (has_gvl)
08856         return rb_thread_fd_select(n, rfds, wfds, efds, timeout);
08857     else
08858         return rb_fd_select(n, rfds, wfds, efds, timeout);
08859 }
08860 
08861 static int
08862 maygvl_copy_stream_wait_read(int has_gvl, struct copy_stream_struct *stp)
08863 {
08864     int ret;
08865 
08866     do {
08867         rb_fd_zero(&stp->fds);
08868         rb_fd_set(stp->src_fd, &stp->fds);
08869         ret = maygvl_select(has_gvl, rb_fd_max(&stp->fds), &stp->fds, NULL, NULL, NULL);
08870     } while (ret == -1 && maygvl_copy_stream_continue_p(has_gvl, stp));
08871 
08872     if (ret == -1) {
08873         stp->syserr = "select";
08874         stp->error_no = errno;
08875         return -1;
08876     }
08877     return 0;
08878 }
08879 
08880 static int
08881 nogvl_copy_stream_wait_write(struct copy_stream_struct *stp)
08882 {
08883     int ret;
08884 
08885     do {
08886         rb_fd_zero(&stp->fds);
08887         rb_fd_set(stp->dst_fd, &stp->fds);
08888         ret = rb_fd_select(rb_fd_max(&stp->fds), NULL, &stp->fds, NULL, NULL);
08889     } while (ret == -1 && maygvl_copy_stream_continue_p(0, stp));
08890 
08891     if (ret == -1) {
08892         stp->syserr = "select";
08893         stp->error_no = errno;
08894         return -1;
08895     }
08896     return 0;
08897 }
08898 
08899 #ifdef HAVE_SENDFILE
08900 
08901 # ifdef __linux__
08902 #  define USE_SENDFILE
08903 
08904 #  ifdef HAVE_SYS_SENDFILE_H
08905 #   include <sys/sendfile.h>
08906 #  endif
08907 
08908 static ssize_t
08909 simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
08910 {
08911     return sendfile(out_fd, in_fd, offset, (size_t)count);
08912 }
08913 
08914 # elif 0 /* defined(__FreeBSD__) || defined(__DragonFly__) */ || defined(__APPLE__)
08915 /* This runs on FreeBSD8.1 r30210, but sendfiles blocks its execution
08916  * without cpuset -l 0.
08917  */
08918 #  define USE_SENDFILE
08919 
08920 #  ifdef HAVE_SYS_UIO_H
08921 #   include <sys/uio.h>
08922 #  endif
08923 
08924 static ssize_t
08925 simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
08926 {
08927     int r;
08928     off_t pos = offset ? *offset : lseek(in_fd, 0, SEEK_CUR);
08929     off_t sbytes;
08930 #  ifdef __APPLE__
08931     r = sendfile(in_fd, out_fd, pos, &count, NULL, 0);
08932     sbytes = count;
08933 #  else
08934     r = sendfile(in_fd, out_fd, pos, (size_t)count, NULL, &sbytes, 0);
08935 #  endif
08936     if (r != 0 && sbytes == 0) return -1;
08937     if (offset) {
08938         *offset += sbytes;
08939     }
08940     else {
08941         lseek(in_fd, sbytes, SEEK_CUR);
08942     }
08943     return (ssize_t)sbytes;
08944 }
08945 
08946 # endif
08947 
08948 #endif
08949 
08950 #ifdef USE_SENDFILE
08951 static int
08952 nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
08953 {
08954     struct stat src_stat, dst_stat;
08955     ssize_t ss;
08956     int ret;
08957 
08958     off_t copy_length;
08959     off_t src_offset;
08960     int use_pread;
08961 
08962     ret = fstat(stp->src_fd, &src_stat);
08963     if (ret == -1) {
08964         stp->syserr = "fstat";
08965         stp->error_no = errno;
08966         return -1;
08967     }
08968     if (!S_ISREG(src_stat.st_mode))
08969         return 0;
08970 
08971     ret = fstat(stp->dst_fd, &dst_stat);
08972     if (ret == -1) {
08973         stp->syserr = "fstat";
08974         stp->error_no = errno;
08975         return -1;
08976     }
08977     if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
08978         return 0;
08979 
08980     src_offset = stp->src_offset;
08981     use_pread = src_offset != (off_t)-1;
08982 
08983     copy_length = stp->copy_length;
08984     if (copy_length == (off_t)-1) {
08985         if (use_pread)
08986             copy_length = src_stat.st_size - src_offset;
08987         else {
08988             off_t cur;
08989             errno = 0;
08990             cur = lseek(stp->src_fd, 0, SEEK_CUR);
08991             if (cur == (off_t)-1 && errno) {
08992                 stp->syserr = "lseek";
08993                 stp->error_no = errno;
08994                 return -1;
08995             }
08996             copy_length = src_stat.st_size - cur;
08997         }
08998     }
08999 
09000   retry_sendfile:
09001 # if SIZEOF_OFF_T > SIZEOF_SIZE_T
09002     /* we are limited by the 32-bit ssize_t return value on 32-bit */
09003     ss = (copy_length > (off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
09004 # else
09005     ss = (ssize_t)copy_length;
09006 # endif
09007     if (use_pread) {
09008         ss = simple_sendfile(stp->dst_fd, stp->src_fd, &src_offset, ss);
09009     }
09010     else {
09011         ss = simple_sendfile(stp->dst_fd, stp->src_fd, NULL, ss);
09012     }
09013     if (0 < ss) {
09014         stp->total += ss;
09015         copy_length -= ss;
09016         if (0 < copy_length) {
09017             goto retry_sendfile;
09018         }
09019     }
09020     if (ss == -1) {
09021         if (maygvl_copy_stream_continue_p(0, stp))
09022             goto retry_sendfile;
09023         switch (errno) {
09024           case EINVAL:
09025 #ifdef ENOSYS
09026           case ENOSYS:
09027 #endif
09028             return 0;
09029           case EAGAIN:
09030 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
09031           case EWOULDBLOCK:
09032 #endif
09033 #ifndef linux
09034            /*
09035             * Linux requires stp->src_fd to be a mmap-able (regular) file,
09036             * select() reports regular files to always be "ready", so
09037             * there is no need to select() on it.
09038             * Other OSes may have the same limitation for sendfile() which
09039             * allow us to bypass maygvl_copy_stream_wait_read()...
09040             */
09041             if (maygvl_copy_stream_wait_read(0, stp) == -1)
09042                 return -1;
09043 #endif
09044             if (nogvl_copy_stream_wait_write(stp) == -1)
09045                 return -1;
09046             goto retry_sendfile;
09047         }
09048         stp->syserr = "sendfile";
09049         stp->error_no = errno;
09050         return -1;
09051     }
09052     return 1;
09053 }
09054 #endif
09055 
09056 static ssize_t
09057 maygvl_read(int has_gvl, int fd, void *buf, size_t count)
09058 {
09059     if (has_gvl)
09060         return rb_read_internal(fd, buf, count);
09061     else
09062         return read(fd, buf, count);
09063 }
09064 
09065 static ssize_t
09066 maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf, size_t len, off_t offset)
09067 {
09068     ssize_t ss;
09069   retry_read:
09070     if (offset == (off_t)-1) {
09071         ss = maygvl_read(has_gvl, stp->src_fd, buf, len);
09072     }
09073     else {
09074 #ifdef HAVE_PREAD
09075         ss = pread(stp->src_fd, buf, len, offset);
09076 #else
09077         stp->notimp = "pread";
09078         return -1;
09079 #endif
09080     }
09081     if (ss == 0) {
09082         return 0;
09083     }
09084     if (ss == -1) {
09085         if (maygvl_copy_stream_continue_p(has_gvl, stp))
09086             goto retry_read;
09087         switch (errno) {
09088           case EAGAIN:
09089 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
09090           case EWOULDBLOCK:
09091 #endif
09092             if (maygvl_copy_stream_wait_read(has_gvl, stp) == -1)
09093                 return -1;
09094             goto retry_read;
09095 #ifdef ENOSYS
09096           case ENOSYS:
09097 #endif
09098             stp->notimp = "pread";
09099             return -1;
09100         }
09101         stp->syserr = offset == (off_t)-1 ?  "read" : "pread";
09102         stp->error_no = errno;
09103         return -1;
09104     }
09105     return ss;
09106 }
09107 
09108 static int
09109 nogvl_copy_stream_write(struct copy_stream_struct *stp, char *buf, size_t len)
09110 {
09111     ssize_t ss;
09112     int off = 0;
09113     while (len) {
09114         ss = write(stp->dst_fd, buf+off, len);
09115         if (ss == -1) {
09116             if (maygvl_copy_stream_continue_p(0, stp))
09117                 continue;
09118             if (errno == EAGAIN || errno == EWOULDBLOCK) {
09119                 if (nogvl_copy_stream_wait_write(stp) == -1)
09120                     return -1;
09121                 continue;
09122             }
09123             stp->syserr = "write";
09124             stp->error_no = errno;
09125             return -1;
09126         }
09127         off += (int)ss;
09128         len -= (int)ss;
09129         stp->total += ss;
09130     }
09131     return 0;
09132 }
09133 
09134 static void
09135 nogvl_copy_stream_read_write(struct copy_stream_struct *stp)
09136 {
09137     char buf[1024*16];
09138     size_t len;
09139     ssize_t ss;
09140     int ret;
09141     off_t copy_length;
09142     int use_eof;
09143     off_t src_offset;
09144     int use_pread;
09145 
09146     copy_length = stp->copy_length;
09147     use_eof = copy_length == (off_t)-1;
09148     src_offset = stp->src_offset;
09149     use_pread = src_offset != (off_t)-1;
09150 
09151     if (use_pread && stp->close_src) {
09152         off_t r;
09153         errno = 0;
09154         r = lseek(stp->src_fd, src_offset, SEEK_SET);
09155         if (r == (off_t)-1 && errno) {
09156             stp->syserr = "lseek";
09157             stp->error_no = errno;
09158             return;
09159         }
09160         src_offset = (off_t)-1;
09161         use_pread = 0;
09162     }
09163 
09164     while (use_eof || 0 < copy_length) {
09165         if (!use_eof && copy_length < (off_t)sizeof(buf)) {
09166             len = (size_t)copy_length;
09167         }
09168         else {
09169             len = sizeof(buf);
09170         }
09171         if (use_pread) {
09172             ss = maygvl_copy_stream_read(0, stp, buf, len, src_offset);
09173             if (0 < ss)
09174                 src_offset += ss;
09175         }
09176         else {
09177             ss = maygvl_copy_stream_read(0, stp, buf, len, (off_t)-1);
09178         }
09179         if (ss <= 0) /* EOF or error */
09180             return;
09181 
09182         ret = nogvl_copy_stream_write(stp, buf, ss);
09183         if (ret < 0)
09184             return;
09185 
09186         if (!use_eof)
09187             copy_length -= ss;
09188     }
09189 }
09190 
09191 static VALUE
09192 nogvl_copy_stream_func(void *arg)
09193 {
09194     struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
09195 #ifdef USE_SENDFILE
09196     int ret;
09197 #endif
09198 
09199 #ifdef USE_SENDFILE
09200     ret = nogvl_copy_stream_sendfile(stp);
09201     if (ret != 0)
09202         goto finish; /* error or success */
09203 #endif
09204 
09205     nogvl_copy_stream_read_write(stp);
09206 
09207 #ifdef USE_SENDFILE
09208   finish:
09209 #endif
09210     return Qnil;
09211 }
09212 
09213 static VALUE
09214 copy_stream_fallback_body(VALUE arg)
09215 {
09216     struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
09217     const int buflen = 16*1024;
09218     VALUE n;
09219     VALUE buf = rb_str_buf_new(buflen);
09220     off_t rest = stp->copy_length;
09221     off_t off = stp->src_offset;
09222     ID read_method = id_readpartial;
09223 
09224     if (stp->src_fd == -1) {
09225         if (!rb_respond_to(stp->src, read_method)) {
09226             read_method = id_read;
09227         }
09228     }
09229 
09230     while (1) {
09231         long numwrote;
09232         long l;
09233         if (stp->copy_length == (off_t)-1) {
09234             l = buflen;
09235         }
09236         else {
09237             if (rest == 0)
09238                 break;
09239             l = buflen < rest ? buflen : (long)rest;
09240         }
09241         if (stp->src_fd == -1) {
09242             rb_funcall(stp->src, read_method, 2, INT2FIX(l), buf);
09243         }
09244         else {
09245             ssize_t ss;
09246             rb_thread_wait_fd(stp->src_fd);
09247             rb_str_resize(buf, buflen);
09248             ss = maygvl_copy_stream_read(1, stp, RSTRING_PTR(buf), l, off);
09249             if (ss == -1)
09250                 return Qnil;
09251             if (ss == 0)
09252                 rb_eof_error();
09253             rb_str_resize(buf, ss);
09254             if (off != (off_t)-1)
09255                 off += ss;
09256         }
09257         n = rb_io_write(stp->dst, buf);
09258         numwrote = NUM2LONG(n);
09259         stp->total += numwrote;
09260         rest -= numwrote;
09261         if (read_method == id_read && RSTRING_LEN(buf) == 0) {
09262             break;
09263         }
09264     }
09265 
09266     return Qnil;
09267 }
09268 
09269 static VALUE
09270 copy_stream_fallback(struct copy_stream_struct *stp)
09271 {
09272     if (stp->src_fd == -1 && stp->src_offset != (off_t)-1) {
09273         rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
09274     }
09275     rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
09276                (VALUE (*) (ANYARGS))0, (VALUE)0,
09277                rb_eEOFError, (VALUE)0);
09278     return Qnil;
09279 }
09280 
09281 static VALUE
09282 copy_stream_body(VALUE arg)
09283 {
09284     struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
09285     VALUE src_io, dst_io;
09286     rb_io_t *src_fptr = 0, *dst_fptr = 0;
09287     int src_fd, dst_fd;
09288 
09289     stp->th = rb_thread_current();
09290 
09291     stp->total = 0;
09292 
09293     if (stp->src == argf ||
09294         !(TYPE(stp->src) == T_FILE ||
09295           TYPE(stp->src) == T_STRING ||
09296           rb_respond_to(stp->src, rb_intern("to_path")))) {
09297         src_fd = -1;
09298     }
09299     else {
09300         src_io = TYPE(stp->src) == T_FILE ? stp->src : Qnil;
09301         if (NIL_P(src_io)) {
09302             VALUE args[2];
09303             int oflags = O_RDONLY;
09304 #ifdef O_NOCTTY
09305             oflags |= O_NOCTTY;
09306 #endif
09307             FilePathValue(stp->src);
09308             args[0] = stp->src;
09309             args[1] = INT2NUM(oflags);
09310             src_io = rb_class_new_instance(2, args, rb_cFile);
09311             stp->src = src_io;
09312             stp->close_src = 1;
09313         }
09314         GetOpenFile(src_io, src_fptr);
09315         rb_io_check_byte_readable(src_fptr);
09316         src_fd = src_fptr->fd;
09317     }
09318     stp->src_fd = src_fd;
09319 
09320     if (stp->dst == argf ||
09321         !(TYPE(stp->dst) == T_FILE ||
09322           TYPE(stp->dst) == T_STRING ||
09323           rb_respond_to(stp->dst, rb_intern("to_path")))) {
09324         dst_fd = -1;
09325     }
09326     else {
09327         dst_io = TYPE(stp->dst) == T_FILE ? stp->dst : Qnil;
09328         if (NIL_P(dst_io)) {
09329             VALUE args[3];
09330             int oflags = O_WRONLY|O_CREAT|O_TRUNC;
09331 #ifdef O_NOCTTY
09332             oflags |= O_NOCTTY;
09333 #endif
09334             FilePathValue(stp->dst);
09335             args[0] = stp->dst;
09336             args[1] = INT2NUM(oflags);
09337             args[2] = INT2FIX(0600);
09338             dst_io = rb_class_new_instance(3, args, rb_cFile);
09339             stp->dst = dst_io;
09340             stp->close_dst = 1;
09341         }
09342         else {
09343             dst_io = GetWriteIO(dst_io);
09344             stp->dst = dst_io;
09345         }
09346         GetOpenFile(dst_io, dst_fptr);
09347         rb_io_check_writable(dst_fptr);
09348         dst_fd = dst_fptr->fd;
09349     }
09350     stp->dst_fd = dst_fd;
09351 
09352 #ifdef O_BINARY
09353     if (src_fptr)
09354         SET_BINARY_MODE_WITH_SEEK_CUR(src_fptr);
09355     if (dst_fptr)
09356         setmode(dst_fd, O_BINARY);
09357 #endif
09358 
09359     if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf.len) {
09360         size_t len = src_fptr->rbuf.len;
09361         VALUE str;
09362         if (stp->copy_length != (off_t)-1 && stp->copy_length < (off_t)len) {
09363             len = (size_t)stp->copy_length;
09364         }
09365         str = rb_str_buf_new(len);
09366         rb_str_resize(str,len);
09367         read_buffered_data(RSTRING_PTR(str), len, src_fptr);
09368         if (dst_fptr) { /* IO or filename */
09369             if (io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str), dst_fptr, 0) < 0)
09370                 rb_sys_fail(0);
09371         }
09372         else /* others such as StringIO */
09373             rb_io_write(stp->dst, str);
09374         stp->total += len;
09375         if (stp->copy_length != (off_t)-1)
09376             stp->copy_length -= len;
09377     }
09378 
09379     if (dst_fptr && io_fflush(dst_fptr) < 0) {
09380         rb_raise(rb_eIOError, "flush failed");
09381     }
09382 
09383     if (stp->copy_length == 0)
09384         return Qnil;
09385 
09386     if (src_fd == -1 || dst_fd == -1) {
09387         return copy_stream_fallback(stp);
09388     }
09389 
09390     rb_fd_set(src_fd, &stp->fds);
09391     rb_fd_set(dst_fd, &stp->fds);
09392 
09393     return rb_thread_blocking_region(nogvl_copy_stream_func, (void*)stp, RUBY_UBF_IO, 0);
09394 }
09395 
09396 static VALUE
09397 copy_stream_finalize(VALUE arg)
09398 {
09399     struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
09400     if (stp->close_src) {
09401         rb_io_close_m(stp->src);
09402     }
09403     if (stp->close_dst) {
09404         rb_io_close_m(stp->dst);
09405     }
09406     rb_fd_term(&stp->fds);
09407     if (stp->syserr) {
09408         errno = stp->error_no;
09409         rb_sys_fail(stp->syserr);
09410     }
09411     if (stp->notimp) {
09412         rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
09413     }
09414     return Qnil;
09415 }
09416 
09417 /*
09418  *  call-seq:
09419  *     IO.copy_stream(src, dst)
09420  *     IO.copy_stream(src, dst, copy_length)
09421  *     IO.copy_stream(src, dst, copy_length, src_offset)
09422  *
09423  *  IO.copy_stream copies <i>src</i> to <i>dst</i>.
09424  *  <i>src</i> and <i>dst</i> is either a filename or an IO.
09425  *
09426  *  This method returns the number of bytes copied.
09427  *
09428  *  If optional arguments are not given,
09429  *  the start position of the copy is
09430  *  the beginning of the filename or
09431  *  the current file offset of the IO.
09432  *  The end position of the copy is the end of file.
09433  *
09434  *  If <i>copy_length</i> is given,
09435  *  No more than <i>copy_length</i> bytes are copied.
09436  *
09437  *  If <i>src_offset</i> is given,
09438  *  it specifies the start position of the copy.
09439  *
09440  *  When <i>src_offset</i> is specified and
09441  *  <i>src</i> is an IO,
09442  *  IO.copy_stream doesn't move the current file offset.
09443  *
09444  */
09445 static VALUE
09446 rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
09447 {
09448     VALUE src, dst, length, src_offset;
09449     struct copy_stream_struct st;
09450 
09451     MEMZERO(&st, struct copy_stream_struct, 1);
09452 
09453     rb_scan_args(argc, argv, "22", &src, &dst, &length, &src_offset);
09454 
09455     st.src = src;
09456     st.dst = dst;
09457 
09458     if (NIL_P(length))
09459         st.copy_length = (off_t)-1;
09460     else
09461         st.copy_length = NUM2OFFT(length);
09462 
09463     if (NIL_P(src_offset))
09464         st.src_offset = (off_t)-1;
09465     else
09466         st.src_offset = NUM2OFFT(src_offset);
09467 
09468     rb_fd_init(&st.fds);
09469     rb_ensure(copy_stream_body, (VALUE)&st, copy_stream_finalize, (VALUE)&st);
09470 
09471     return OFFT2NUM(st.total);
09472 }
09473 
09474 /*
09475  *  call-seq:
09476  *     io.external_encoding   -> encoding
09477  *
09478  *  Returns the Encoding object that represents the encoding of the file.
09479  *  If io is write mode and no encoding is specified, returns <code>nil</code>.
09480  */
09481 
09482 static VALUE
09483 rb_io_external_encoding(VALUE io)
09484 {
09485     rb_io_t *fptr;
09486 
09487     GetOpenFile(io, fptr);
09488     if (fptr->encs.enc2) {
09489         return rb_enc_from_encoding(fptr->encs.enc2);
09490     }
09491     if (fptr->mode & FMODE_WRITABLE) {
09492         if (fptr->encs.enc)
09493             return rb_enc_from_encoding(fptr->encs.enc);
09494         return Qnil;
09495     }
09496     return rb_enc_from_encoding(io_read_encoding(fptr));
09497 }
09498 
09499 /*
09500  *  call-seq:
09501  *     io.internal_encoding   -> encoding
09502  *
09503  *  Returns the Encoding of the internal string if conversion is
09504  *  specified.  Otherwise returns nil.
09505  */
09506 
09507 static VALUE
09508 rb_io_internal_encoding(VALUE io)
09509 {
09510     rb_io_t *fptr;
09511 
09512     GetOpenFile(io, fptr);
09513     if (!fptr->encs.enc2) return Qnil;
09514     return rb_enc_from_encoding(io_read_encoding(fptr));
09515 }
09516 
09517 /*
09518  *  call-seq:
09519  *     io.set_encoding(ext_enc)                -> io
09520  *     io.set_encoding("ext_enc:int_enc")      -> io
09521  *     io.set_encoding(ext_enc, int_enc)       -> io
09522  *     io.set_encoding("ext_enc:int_enc", opt) -> io
09523  *     io.set_encoding(ext_enc, int_enc, opt)  -> io
09524  *
09525  *  If single argument is specified, read string from io is tagged
09526  *  with the encoding specified.  If encoding is a colon separated two
09527  *  encoding names "A:B", the read string is converted from encoding A
09528  *  (external encoding) to encoding B (internal encoding), then tagged
09529  *  with B.  If two arguments are specified, those must be encoding
09530  *  objects or encoding names, and the first one is the external encoding, and the
09531  *  second one is the internal encoding.
09532  *  If the external encoding and the internal encoding is specified,
09533  *  optional hash argument specify the conversion option.
09534  */
09535 
09536 static VALUE
09537 rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
09538 {
09539     rb_io_t *fptr;
09540     VALUE v1, v2, opt;
09541 
09542     if (TYPE(io) != T_FILE) {
09543         return rb_funcall2(io, id_set_encoding, argc, argv);
09544     }
09545 
09546     argc = rb_scan_args(argc, argv, "11:", &v1, &v2, &opt);
09547     GetOpenFile(io, fptr);
09548     io_encoding_set(fptr, v1, v2, opt);
09549     return io;
09550 }
09551 
09552 void
09553 rb_stdio_set_default_encoding(void)
09554 {
09555     extern VALUE rb_stdin, rb_stdout, rb_stderr;
09556     VALUE val = Qnil;
09557 
09558     rb_io_set_encoding(1, &val, rb_stdin);
09559     rb_io_set_encoding(1, &val, rb_stdout);
09560     rb_io_set_encoding(1, &val, rb_stderr);
09561 }
09562 
09563 /*
09564  *  call-seq:
09565  *     ARGF.external_encoding   -> encoding
09566  *
09567  *  Returns the external encoding for files read from +ARGF+ as an +Encoding+
09568  *  object. The external encoding is the encoding of the text as stored in a
09569  *  file. Contrast with +ARGF.internal_encoding+, which is the encoding used
09570  *  to represent this text within Ruby.
09571  *
09572  *  To set the external encoding use +ARGF.set_encoding+.
09573  *
09574  * For example:
09575  *
09576  *     ARGF.external_encoding  #=>  #<Encoding:UTF-8>
09577  *
09578  */
09579 static VALUE
09580 argf_external_encoding(VALUE argf)
09581 {
09582     if (!RTEST(ARGF.current_file)) {
09583         return rb_enc_from_encoding(rb_default_external_encoding());
09584     }
09585     return rb_io_external_encoding(rb_io_check_io(ARGF.current_file));
09586 }
09587 
09588 /*
09589  *  call-seq:
09590  *     ARGF.internal_encoding   -> encoding
09591  *
09592  *  Returns the internal encoding for strings read from +ARGF+ as an
09593  *  +Encoding+ object.
09594  *
09595  *  If +ARGF.set_encoding+ has been called with two encoding names, the second
09596  *  is returned. Otherwise, if +Encoding.default_external+ has been set, that
09597  *  value is returned. Failing that, if a default external encoding was
09598  *  specified on the command-line, that value is used. If the encoding is
09599  *  unknown, nil is returned.
09600  */
09601 static VALUE
09602 argf_internal_encoding(VALUE argf)
09603 {
09604     if (!RTEST(ARGF.current_file)) {
09605         return rb_enc_from_encoding(rb_default_external_encoding());
09606     }
09607     return rb_io_internal_encoding(rb_io_check_io(ARGF.current_file));
09608 }
09609 
09610 /*
09611  *  call-seq:
09612  *     ARGF.set_encoding(ext_enc)                -> ARGF
09613  *     ARGF.set_encoding("ext_enc:int_enc")      -> ARGF
09614  *     ARGF.set_encoding(ext_enc, int_enc)       -> ARGF
09615  *     ARGF.set_encoding("ext_enc:int_enc", opt) -> ARGF
09616  *     ARGF.set_encoding(ext_enc, int_enc, opt)  -> ARGF
09617  *
09618  *  If single argument is specified, strings read from ARGF are tagged with
09619  *  the encoding specified.
09620  *
09621  *  If two encoding names separated by a colon are given, e.g. "ascii:utf-8",
09622  *  the read string is converted from the first encoding (external encoding)
09623  *  to the second encoding (internal encoding), then tagged with the second
09624  *  encoding.
09625  *
09626  *  If two arguments are specified, they must be encoding objects or encoding
09627  *  names. Again, the first specifies the external encoding; the second
09628  *  specifies the internal encoding.
09629  *
09630  *  If the external encoding and the internal encoding are specified, the
09631  *  optional +Hash+ argument can be used to adjust the conversion process. The
09632  *  structure of this hash is explained in the +String#encode+ documentation.
09633  *
09634  *  For example:
09635  *
09636  *      ARGF.set_encoding('ascii')         # Tag the input as US-ASCII text
09637  *      ARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text
09638  *      ARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII
09639  *                                         # to UTF-8.
09640  */
09641 static VALUE
09642 argf_set_encoding(int argc, VALUE *argv, VALUE argf)
09643 {
09644     rb_io_t *fptr;
09645 
09646     if (!next_argv()) {
09647         rb_raise(rb_eArgError, "no stream to set encoding");
09648     }
09649     rb_io_set_encoding(argc, argv, ARGF.current_file);
09650     GetOpenFile(ARGF.current_file, fptr);
09651     ARGF.encs = fptr->encs;
09652     return argf;
09653 }
09654 
09655 /*
09656  *  call-seq:
09657  *     ARGF.tell  -> Integer
09658  *     ARGF.pos   -> Integer
09659  *
09660  *  Returns the current offset (in bytes) of the current file in +ARGF+.
09661  *
09662  *     ARGF.pos    #=> 0
09663  *     ARGF.gets   #=> "This is line one\n"
09664  *     ARGF.pos    #=> 17
09665  *
09666  */
09667 static VALUE
09668 argf_tell(VALUE argf)
09669 {
09670     if (!next_argv()) {
09671         rb_raise(rb_eArgError, "no stream to tell");
09672     }
09673     ARGF_FORWARD(0, 0);
09674     return rb_io_tell(ARGF.current_file);
09675 }
09676 
09677 /*
09678  *  call-seq:
09679  *     ARGF.seek(amount, whence=IO::SEEK_SET)  ->  0
09680  *
09681  *  Seeks to offset _amount_ (an +Integer+) in the +ARGF+ stream according to
09682  *  the value of _whence_. See +IO#seek+ for further details.
09683  */
09684 static VALUE
09685 argf_seek_m(int argc, VALUE *argv, VALUE argf)
09686 {
09687     if (!next_argv()) {
09688         rb_raise(rb_eArgError, "no stream to seek");
09689     }
09690     ARGF_FORWARD(argc, argv);
09691     return rb_io_seek_m(argc, argv, ARGF.current_file);
09692 }
09693 
09694 /*
09695  *  call-seq:
09696  *     ARGF.pos = position  -> Integer
09697  *
09698  *  Seeks to the position given by _position_ (in bytes) in +ARGF+.
09699  *
09700  *  For example:
09701  *
09702  *      ARGF.pos = 17
09703  *      ARGF.gets   #=> "This is line two\n"
09704  */
09705 static VALUE
09706 argf_set_pos(VALUE argf, VALUE offset)
09707 {
09708     if (!next_argv()) {
09709         rb_raise(rb_eArgError, "no stream to set position");
09710     }
09711     ARGF_FORWARD(1, &offset);
09712     return rb_io_set_pos(ARGF.current_file, offset);
09713 }
09714 
09715 /*
09716  *  call-seq:
09717  *     ARGF.rewind   -> 0
09718  *
09719  *  Positions the current file to the beginning of input, resetting
09720  *  +ARGF.lineno+ to zero.
09721  *
09722  *     ARGF.readline   #=> "This is line one\n"
09723  *     ARGF.rewind     #=> 0
09724  *     ARGF.lineno     #=> 0
09725  *     ARGF.readline   #=> "This is line one\n"
09726  */
09727 static VALUE
09728 argf_rewind(VALUE argf)
09729 {
09730     if (!next_argv()) {
09731         rb_raise(rb_eArgError, "no stream to rewind");
09732     }
09733     ARGF_FORWARD(0, 0);
09734     return rb_io_rewind(ARGF.current_file);
09735 }
09736 
09737 /*
09738  *  call-seq:
09739  *     ARGF.fileno    -> fixnum
09740  *     ARGF.to_i      -> fixnum
09741  *
09742  *  Returns an integer representing the numeric file descriptor for
09743  *  the current file. Raises an +ArgumentError+ if there isn't a current file.
09744  *
09745  *     ARGF.fileno    #=> 3
09746  */
09747 static VALUE
09748 argf_fileno(VALUE argf)
09749 {
09750     if (!next_argv()) {
09751         rb_raise(rb_eArgError, "no stream");
09752     }
09753     ARGF_FORWARD(0, 0);
09754     return rb_io_fileno(ARGF.current_file);
09755 }
09756 
09757 /*
09758  *  call-seq:
09759  *     ARGF.to_io     -> IO
09760  *
09761  *  Returns an +IO+ object representing the current file. This will be a
09762  *  +File+ object unless the current file is a stream such as STDIN.
09763  *
09764  *  For example:
09765  *
09766  *     ARGF.to_io    #=> #<File:glark.txt>
09767  *     ARGF.to_io    #=> #<IO:<STDIN>>
09768  */
09769 static VALUE
09770 argf_to_io(VALUE argf)
09771 {
09772     next_argv();
09773     ARGF_FORWARD(0, 0);
09774     return ARGF.current_file;
09775 }
09776 
09777 /*
09778  *  call-seq:
09779  *     ARGF.eof?  -> true or false
09780  *     ARGF.eof   -> true or false
09781  *
09782  *  Returns true if the current file in +ARGF+ is at end of file, i.e. it has
09783  *  no data to read. The stream must be opened for reading or an +IOError+
09784  *  will be raised.
09785  *
09786  *     $ echo "eof" | ruby argf.rb
09787  *
09788  *     ARGF.eof?                 #=> false
09789  *     3.times { ARGF.readchar }
09790  *     ARGF.eof?                 #=> false
09791  *     ARGF.readchar             #=> "\n"
09792  *     ARGF.eof?                 #=> true
09793  */
09794 
09795 static VALUE
09796 argf_eof(VALUE argf)
09797 {
09798     next_argv();
09799     if (RTEST(ARGF.current_file)) {
09800         if (ARGF.init_p == 0) return Qtrue;
09801         next_argv();
09802         ARGF_FORWARD(0, 0);
09803         if (rb_io_eof(ARGF.current_file)) {
09804             return Qtrue;
09805         }
09806     }
09807     return Qfalse;
09808 }
09809 
09810 /*
09811  *  call-seq:
09812  *     ARGF.read([length [, buffer]])    -> string, buffer, or nil
09813  *
09814  *  Reads _length_ bytes from ARGF. The files named on the command line
09815  *  are concatenated and treated as a single file by this method, so when
09816  *  called without arguments the contents of this pseudo file are returned in
09817  *  their entirety.
09818  *
09819  *  _length_ must be a non-negative integer or nil. If it is a positive
09820  *  integer, +read+ tries to read at most _length_ bytes. It returns nil
09821  *  if an EOF was encountered before anything could be read. Fewer than
09822  *  _length_ bytes may be returned if an EOF is encountered during the read.
09823  *
09824  *  If _length_ is omitted or is _nil_, it reads until EOF. A String is
09825  *  returned even if EOF is encountered before any data is read.
09826  *
09827  *  If _length_ is zero, it returns _""_.
09828  *
09829  *  If the optional _buffer_ argument is present, it must reference a String,
09830  *  which will receive the data.
09831  *
09832  * For example:
09833  *
09834  *     $ echo "small" > small.txt
09835  *     $ echo "large" > large.txt
09836  *     $ ./glark.rb small.txt large.txt
09837  *
09838  *     ARGF.read      #=> "small\nlarge"
09839  *     ARGF.read(200) #=> "small\nlarge"
09840  *     ARGF.read(2)   #=> "sm"
09841  *     ARGF.read(0)   #=> ""
09842  *
09843  *  Note that this method behaves like fread() function in C.  If you need the
09844  *  behavior like read(2) system call, consider +ARGF.readpartial+.
09845  */
09846 
09847 static VALUE
09848 argf_read(int argc, VALUE *argv, VALUE argf)
09849 {
09850     VALUE tmp, str, length;
09851     long len = 0;
09852 
09853     rb_scan_args(argc, argv, "02", &length, &str);
09854     if (!NIL_P(length)) {
09855         len = NUM2LONG(argv[0]);
09856     }
09857     if (!NIL_P(str)) {
09858         StringValue(str);
09859         rb_str_resize(str,0);
09860         argv[1] = Qnil;
09861     }
09862 
09863   retry:
09864     if (!next_argv()) {
09865         return str;
09866     }
09867     if (ARGF_GENERIC_INPUT_P()) {
09868         tmp = argf_forward(argc, argv, argf);
09869     }
09870     else {
09871         tmp = io_read(argc, argv, ARGF.current_file);
09872     }
09873     if (NIL_P(str)) str = tmp;
09874     else if (!NIL_P(tmp)) rb_str_append(str, tmp);
09875     if (NIL_P(tmp) || NIL_P(length)) {
09876         if (ARGF.next_p != -1) {
09877             argf_close(ARGF.current_file);
09878             ARGF.next_p = 1;
09879             goto retry;
09880         }
09881     }
09882     else if (argc >= 1) {
09883         if (RSTRING_LEN(str) < len) {
09884             len -= RSTRING_LEN(str);
09885             argv[0] = INT2NUM(len);
09886             goto retry;
09887         }
09888     }
09889     return str;
09890 }
09891 
09892 struct argf_call_arg {
09893     int argc;
09894     VALUE *argv;
09895     VALUE argf;
09896 };
09897 
09898 static VALUE
09899 argf_forward_call(VALUE arg)
09900 {
09901     struct argf_call_arg *p = (struct argf_call_arg *)arg;
09902     argf_forward(p->argc, p->argv, p->argf);
09903     return Qnil;
09904 }
09905 
09906 static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock);
09907 
09908 /*
09909  *  call-seq:
09910  *     ARGF.readpartial(maxlen)              -> string
09911  *     ARGF.readpartial(maxlen, outbuf)      -> outbuf
09912  *
09913  *  Reads at most _maxlen_ bytes from the ARGF stream. It blocks only if
09914  *  +ARGF+ has no data immediately available. If the optional _outbuf_
09915  *  argument is present, it must reference a String, which will receive the
09916  *  data. It raises <code>EOFError</code> on end of file.
09917  *
09918  *  +readpartial+ is designed for streams such as pipes, sockets, and ttys. It
09919  *  blocks only when no data is immediately available. This means that it
09920  *  blocks only when following all conditions hold:
09921  *
09922  *  * The byte buffer in the +IO+ object is empty.
09923  *  * The content of the stream is empty.
09924  *  * The stream has not reached EOF.
09925  *
09926  *  When +readpartial+ blocks, it waits for data or EOF. If some data is read,
09927  *  +readpartial+ returns with the data. If EOF is reached, readpartial raises
09928  *  an +EOFError+.
09929  *
09930  *  When +readpartial+ doesn't block, it returns or raises immediately.  If
09931  *  the byte buffer is not empty, it returns the data in the buffer. Otherwise, if
09932  *  the stream has some content, it returns the data in the stream. If the
09933  *  stream reaches EOF an +EOFError+ is raised.
09934  */
09935 
09936 static VALUE
09937 argf_readpartial(int argc, VALUE *argv, VALUE argf)
09938 {
09939     return argf_getpartial(argc, argv, argf, 0);
09940 }
09941 
09942 /*
09943  *  call-seq:
09944  *     ARGF.read_nonblock(maxlen)              -> string
09945  *     ARGF.read_nonblock(maxlen, outbuf)      -> outbuf
09946  *
09947  *  Reads at most _maxlen_ bytes from the ARGF stream in non-blocking mode.
09948  */
09949 
09950 static VALUE
09951 argf_read_nonblock(int argc, VALUE *argv, VALUE argf)
09952 {
09953     return argf_getpartial(argc, argv, argf, 1);
09954 }
09955 
09956 static VALUE
09957 argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock)
09958 {
09959     VALUE tmp, str, length;
09960 
09961     rb_scan_args(argc, argv, "11", &length, &str);
09962     if (!NIL_P(str)) {
09963         StringValue(str);
09964         argv[1] = str;
09965     }
09966 
09967     if (!next_argv()) {
09968         rb_str_resize(str, 0);
09969         rb_eof_error();
09970     }
09971     if (ARGF_GENERIC_INPUT_P()) {
09972         struct argf_call_arg arg;
09973         arg.argc = argc;
09974         arg.argv = argv;
09975         arg.argf = argf;
09976         tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
09977                          RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
09978     }
09979     else {
09980         tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock);
09981     }
09982     if (NIL_P(tmp)) {
09983         if (ARGF.next_p == -1) {
09984             rb_eof_error();
09985         }
09986         argf_close(ARGF.current_file);
09987         ARGF.next_p = 1;
09988         if (RARRAY_LEN(ARGF.argv) == 0)
09989             rb_eof_error();
09990         if (NIL_P(str))
09991             str = rb_str_new(NULL, 0);
09992         return str;
09993     }
09994     return tmp;
09995 }
09996 
09997 /*
09998  *  call-seq:
09999  *     ARGF.getc  -> String or nil
10000  *
10001  *  Reads the next character from +ARGF+ and returns it as a +String+. Returns
10002  *  +nil+ at the end of the stream.
10003  *
10004  *  +ARGF+ treats the files named on the command line as a single file created
10005  *  by concatenating their contents. After returning the last character of the
10006  *  first file, it returns the first character of the second file, and so on.
10007  *
10008  *  For example:
10009  *
10010  *     $ echo "foo" > file
10011  *     $ ruby argf.rb file
10012  *
10013  *     ARGF.getc  #=> "f"
10014  *     ARGF.getc  #=> "o"
10015  *     ARGF.getc  #=> "o"
10016  *     ARGF.getc  #=> "\n"
10017  *     ARGF.getc  #=> nil
10018  *     ARGF.getc  #=> nil
10019  */
10020 static VALUE
10021 argf_getc(VALUE argf)
10022 {
10023     VALUE ch;
10024 
10025   retry:
10026     if (!next_argv()) return Qnil;
10027     if (ARGF_GENERIC_INPUT_P()) {
10028         ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
10029     }
10030     else {
10031         ch = rb_io_getc(ARGF.current_file);
10032     }
10033     if (NIL_P(ch) && ARGF.next_p != -1) {
10034         argf_close(ARGF.current_file);
10035         ARGF.next_p = 1;
10036         goto retry;
10037     }
10038 
10039     return ch;
10040 }
10041 
10042 /*
10043  *  call-seq:
10044  *     ARGF.getbyte  -> Fixnum or nil
10045  *
10046  *  Gets the next 8-bit byte (0..255) from +ARGF+. Returns +nil+ if called at
10047  *  the end of the stream.
10048  *
10049  *  For example:
10050  *
10051  *     $ echo "foo" > file
10052  *     $ ruby argf.rb file
10053  *
10054  *     ARGF.getbyte #=> 102
10055  *     ARGF.getbyte #=> 111
10056  *     ARGF.getbyte #=> 111
10057  *     ARGF.getbyte #=> 10
10058  *     ARGF.getbyte #=> nil
10059  */
10060 static VALUE
10061 argf_getbyte(VALUE argf)
10062 {
10063     VALUE ch;
10064 
10065   retry:
10066     if (!next_argv()) return Qnil;
10067     if (TYPE(ARGF.current_file) != T_FILE) {
10068         ch = rb_funcall3(ARGF.current_file, rb_intern("getbyte"), 0, 0);
10069     }
10070     else {
10071         ch = rb_io_getbyte(ARGF.current_file);
10072     }
10073     if (NIL_P(ch) && ARGF.next_p != -1) {
10074         argf_close(ARGF.current_file);
10075         ARGF.next_p = 1;
10076         goto retry;
10077     }
10078 
10079     return ch;
10080 }
10081 
10082 /*
10083  *  call-seq:
10084  *     ARGF.readchar  -> String or nil
10085  *
10086  *  Reads the next character from +ARGF+ and returns it as a +String+. Raises
10087  *  an +EOFError+ after the last character of the last file has been read.
10088  *
10089  *  For example:
10090  *
10091  *     $ echo "foo" > file
10092  *     $ ruby argf.rb file
10093  *
10094  *     ARGF.readchar  #=> "f"
10095  *     ARGF.readchar  #=> "o"
10096  *     ARGF.readchar  #=> "o"
10097  *     ARGF.readchar  #=> "\n"
10098  *     ARGF.readchar  #=> end of file reached (EOFError)
10099  */
10100 static VALUE
10101 argf_readchar(VALUE argf)
10102 {
10103     VALUE ch;
10104 
10105   retry:
10106     if (!next_argv()) rb_eof_error();
10107     if (TYPE(ARGF.current_file) != T_FILE) {
10108         ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
10109     }
10110     else {
10111         ch = rb_io_getc(ARGF.current_file);
10112     }
10113     if (NIL_P(ch) && ARGF.next_p != -1) {
10114         argf_close(ARGF.current_file);
10115         ARGF.next_p = 1;
10116         goto retry;
10117     }
10118 
10119     return ch;
10120 }
10121 
10122 /*
10123  *  call-seq:
10124  *     ARGF.readbyte  -> Fixnum
10125  *
10126  *  Reads the next 8-bit byte from ARGF and returns it as a +Fixnum+. Raises
10127  *  an +EOFError+ after the last byte of the last file has been read.
10128  *
10129  *  For example:
10130  *
10131  *     $ echo "foo" > file
10132  *     $ ruby argf.rb file
10133  *
10134  *     ARGF.readbyte  #=> 102
10135  *     ARGF.readbyte  #=> 111
10136  *     ARGF.readbyte  #=> 111
10137  *     ARGF.readbyte  #=> 10
10138  *     ARGF.readbyte  #=> end of file reached (EOFError)
10139  */
10140 static VALUE
10141 argf_readbyte(VALUE argf)
10142 {
10143     VALUE c;
10144 
10145     NEXT_ARGF_FORWARD(0, 0);
10146     c = argf_getbyte(argf);
10147     if (NIL_P(c)) {
10148         rb_eof_error();
10149     }
10150     return c;
10151 }
10152 
10153 /*
10154  *  call-seq:
10155  *     ARGF.each(sep=$/)            {|line| block }  -> ARGF
10156  *     ARGF.each(sep=$/,limit)      {|line| block }  -> ARGF
10157  *     ARGF.each(...)                                -> an_enumerator
10158  *
10159  *     ARGF.each_line(sep=$/)       {|line| block }  -> ARGF
10160  *     ARGF.each_line(sep=$/,limit) {|line| block }  -> ARGF
10161  *     ARGF.each_line(...)                           -> an_enumerator
10162  *
10163  *     ARGF.lines(sep=$/)           {|line| block }   -> ARGF
10164  *     ARGF.lines(sep=$/,limit)     {|line| block }   -> ARGF
10165  *     ARGF.lines(...)                                -> an_enumerator
10166  *
10167  *  Returns an enumerator which iterates over each line (separated by _sep_,
10168  *  which defaults to your platform's newline character) of each file in
10169  *  +ARGV+. If a block is supplied, each line in turn will be yielded to the
10170  *  block, otherwise an enumerator is returned.
10171  *  The optional _limit_ argument is a +Fixnum+ specifying the maximum
10172  *  length of each line; longer lines will be split according to this limit.
10173  *
10174  *  This method allows you to treat the files supplied on the command line as
10175  *  a single file consisting of the concatenation of each named file. After
10176  *  the last line of the first file has been returned, the first line of the
10177  *  second file is returned. The +ARGF.filename+ and +ARGF.lineno+ methods can
10178  *  be used to determine the filename and line number, respectively, of the
10179  *  current line.
10180  *
10181  *  For example, the following code prints out each line of each named file
10182  *  prefixed with its line number, displaying the filename once per file:
10183  *
10184  *     ARGF.lines do |line|
10185  *       puts ARGF.filename if ARGF.lineno == 1
10186  *       puts "#{ARGF.lineno}: #{line}"
10187  *     end
10188  */
10189 static VALUE
10190 argf_each_line(int argc, VALUE *argv, VALUE argf)
10191 {
10192     RETURN_ENUMERATOR(argf, argc, argv);
10193     for (;;) {
10194         if (!next_argv()) return argf;
10195         rb_block_call(ARGF.current_file, rb_intern("each_line"), argc, argv, 0, 0);
10196         ARGF.next_p = 1;
10197     }
10198 }
10199 
10200 /*
10201  *  call-seq:
10202  *     ARGF.bytes     {|byte| block }  -> ARGF
10203  *     ARGF.bytes                      -> an_enumerator
10204  *
10205  *     ARGF.each_byte {|byte| block }  -> ARGF
10206  *     ARGF.each_byte                  -> an_enumerator
10207  *
10208  *  Iterates over each byte of each file in +ARGV+.
10209  *  A byte is returned as a +Fixnum+ in the range 0..255.
10210  *
10211  *  This method allows you to treat the files supplied on the command line as
10212  *  a single file consisting of the concatenation of each named file. After
10213  *  the last byte of the first file has been returned, the first byte of the
10214  *  second file is returned. The +ARGF.filename+ method can be used to
10215  *  determine the filename of the current byte.
10216  *
10217  *  If no block is given, an enumerator is returned instead.
10218  *
10219  * For example:
10220  *
10221  *     ARGF.bytes.to_a  #=> [35, 32, ... 95, 10]
10222  *
10223  */
10224 static VALUE
10225 argf_each_byte(VALUE argf)
10226 {
10227     RETURN_ENUMERATOR(argf, 0, 0);
10228     for (;;) {
10229         if (!next_argv()) return argf;
10230         rb_block_call(ARGF.current_file, rb_intern("each_byte"), 0, 0, 0, 0);
10231         ARGF.next_p = 1;
10232     }
10233 }
10234 
10235 /*
10236  *  call-seq:
10237  *     ARGF.chars      {|char| block }  -> ARGF
10238  *     ARGF.chars                       -> an_enumerator
10239  *
10240  *     ARGF.each_char  {|char| block }  -> ARGF
10241  *     ARGF.each_char                   -> an_enumerator
10242  *
10243  *  Iterates over each character of each file in +ARGF+.
10244  *
10245  *  This method allows you to treat the files supplied on the command line as
10246  *  a single file consisting of the concatenation of each named file. After
10247  *  the last character of the first file has been returned, the first
10248  *  character of the second file is returned. The +ARGF.filename+ method can
10249  *  be used to determine the name of the file in which the current character
10250  *  appears.
10251  *
10252  *  If no block is given, an enumerator is returned instead.
10253  */
10254 static VALUE
10255 argf_each_char(VALUE argf)
10256 {
10257     RETURN_ENUMERATOR(argf, 0, 0);
10258     for (;;) {
10259         if (!next_argv()) return argf;
10260         rb_block_call(ARGF.current_file, rb_intern("each_char"), 0, 0, 0, 0);
10261         ARGF.next_p = 1;
10262     }
10263 }
10264 
10265 /*
10266  *  call-seq:
10267  *     ARGF.filename  -> String
10268  *     ARGF.path      -> String
10269  *
10270  *  Returns the current filename. "-" is returned when the current file is
10271  *  STDIN.
10272  *
10273  *  For example:
10274  *
10275  *     $ echo "foo" > foo
10276  *     $ echo "bar" > bar
10277  *     $ echo "glark" > glark
10278  *
10279  *     $ ruby argf.rb foo bar glark
10280  *
10281  *     ARGF.filename  #=> "foo"
10282  *     ARGF.read(5)   #=> "foo\nb"
10283  *     ARGF.filename  #=> "bar"
10284  *     ARGF.skip
10285  *     ARGF.filename  #=> "glark"
10286  */
10287 static VALUE
10288 argf_filename(VALUE argf)
10289 {
10290     next_argv();
10291     return ARGF.filename;
10292 }
10293 
10294 static VALUE
10295 argf_filename_getter(ID id, VALUE *var)
10296 {
10297     return argf_filename(*var);
10298 }
10299 
10300 /*
10301  *  call-seq:
10302  *     ARGF.file  -> IO or File object
10303  *
10304  *  Returns the current file as an +IO+ or +File+ object. #<IO:<STDIN>> is
10305  *  returned when the current file is STDIN.
10306  *
10307  *  For example:
10308  *
10309  *     $ echo "foo" > foo
10310  *     $ echo "bar" > bar
10311  *
10312  *     $ ruby argf.rb foo bar
10313  *
10314  *     ARGF.file      #=> #<File:foo>
10315  *     ARGF.read(5)   #=> "foo\nb"
10316  *     ARGF.file      #=> #<File:bar>
10317  */
10318 static VALUE
10319 argf_file(VALUE argf)
10320 {
10321     next_argv();
10322     return ARGF.current_file;
10323 }
10324 
10325 /*
10326  *  call-seq:
10327  *     ARGF.binmode  -> ARGF
10328  *
10329  *  Puts +ARGF+ into binary mode. Once a stream is in binary mode, it cannot
10330  *  be reset to non-binary mode. This option has the following effects:
10331  *
10332  *  *  Newline conversion is disabled.
10333  *  *  Encoding conversion is disabled.
10334  *  *  Content is treated as ASCII-8BIT.
10335  */
10336 static VALUE
10337 argf_binmode_m(VALUE argf)
10338 {
10339     ARGF.binmode = 1;
10340     next_argv();
10341     ARGF_FORWARD(0, 0);
10342     rb_io_ascii8bit_binmode(ARGF.current_file);
10343     return argf;
10344 }
10345 
10346 /*
10347  *  call-seq:
10348  *     ARGF.binmode?  -> true or false
10349  *
10350  *  Returns true if +ARGF+ is being read in binary mode; false otherwise. (To
10351  *  enable binary mode use +ARGF.binmode+.
10352  *
10353  * For example:
10354  *
10355  *     ARGF.binmode?  #=> false
10356  *     ARGF.binmode
10357  *     ARGF.binmode?  #=> true
10358  */
10359 static VALUE
10360 argf_binmode_p(VALUE argf)
10361 {
10362     return ARGF.binmode ? Qtrue : Qfalse;
10363 }
10364 
10365 /*
10366  *  call-seq:
10367  *     ARGF.skip  -> ARGF
10368  *
10369  *  Sets the current file to the next file in ARGV. If there aren't any more
10370  *  files it has no effect.
10371  *
10372  * For example:
10373  *
10374  *     $ ruby argf.rb foo bar
10375  *     ARGF.filename  #=> "foo"
10376  *     ARGF.skip
10377  *     ARGF.filename  #=> "bar"
10378  */
10379 static VALUE
10380 argf_skip(VALUE argf)
10381 {
10382     if (ARGF.init_p && ARGF.next_p == 0) {
10383         argf_close(ARGF.current_file);
10384         ARGF.next_p = 1;
10385     }
10386     return argf;
10387 }
10388 
10389 /*
10390  *  call-seq:
10391  *     ARGF.close  -> ARGF
10392  *
10393  *  Closes the current file and skips to the next in the stream. Trying to
10394  *  close a file that has already been closed causes an +IOError+ to be
10395  *  raised.
10396  *
10397  * For example:
10398  *
10399  *     $ ruby argf.rb foo bar
10400  *
10401  *     ARGF.filename  #=> "foo"
10402  *     ARGF.close
10403  *     ARGF.filename  #=> "bar"
10404  *     ARGF.close
10405  *     ARGF.close     #=> closed stream (IOError)
10406  */
10407 static VALUE
10408 argf_close_m(VALUE argf)
10409 {
10410     next_argv();
10411     argf_close(ARGF.current_file);
10412     if (ARGF.next_p != -1) {
10413         ARGF.next_p = 1;
10414     }
10415     ARGF.lineno = 0;
10416     return argf;
10417 }
10418 
10419 /*
10420  *  call-seq:
10421  *     ARGF.closed?  -> true or false
10422  *
10423  *  Returns _true_ if the current file has been closed; _false_ otherwise. Use
10424  *  +ARGF.close+ to actually close the current file.
10425  */
10426 static VALUE
10427 argf_closed(VALUE argf)
10428 {
10429     next_argv();
10430     ARGF_FORWARD(0, 0);
10431     return rb_io_closed(ARGF.current_file);
10432 }
10433 
10434 /*
10435  *  call-seq:
10436  *     ARGF.to_s  -> String
10437  *
10438  *  Returns "ARGF".
10439  */
10440 static VALUE
10441 argf_to_s(VALUE argf)
10442 {
10443     return rb_str_new2("ARGF");
10444 }
10445 
10446 /*
10447  *  call-seq:
10448  *     ARGF.inplace_mode  -> String
10449  *
10450  *  Returns the file extension appended to the names of modified files under
10451  *  inplace-edit mode. This value can be set using +ARGF.inplace_mode=+ or
10452  *  passing the +-i+ switch to the Ruby binary.
10453  */
10454 static VALUE
10455 argf_inplace_mode_get(VALUE argf)
10456 {
10457     if (!ARGF.inplace) return Qnil;
10458     return rb_str_new2(ARGF.inplace);
10459 }
10460 
10461 static VALUE
10462 opt_i_get(ID id, VALUE *var)
10463 {
10464     return argf_inplace_mode_get(*var);
10465 }
10466 
10467 /*
10468  *  call-seq:
10469  *     ARGF.inplace_mode = ext  -> ARGF
10470  *
10471  *  Sets the filename extension for inplace editing mode to the given String.
10472  *  Each file being edited has this value appended to its filename. The
10473  *  modified file is saved under this new name.
10474  *
10475  *  For example:
10476  *
10477  *      $ ruby argf.rb file.txt
10478  *
10479  *      ARGF.inplace_mode = '.bak'
10480  *      ARGF.lines do |line|
10481  *        print line.sub("foo","bar")
10482  *      end
10483  *
10484  * Each line of _file.txt_ has the first occurrence of "foo" replaced with
10485  * "bar", then the new line is written out to _file.txt.bak_.
10486  */
10487 static VALUE
10488 argf_inplace_mode_set(VALUE argf, VALUE val)
10489 {
10490     if (rb_safe_level() >= 1 && OBJ_TAINTED(val))
10491         rb_insecure_operation();
10492 
10493     if (!RTEST(val)) {
10494         if (ARGF.inplace) free(ARGF.inplace);
10495         ARGF.inplace = 0;
10496     }
10497     else {
10498         StringValue(val);
10499         if (ARGF.inplace) free(ARGF.inplace);
10500         ARGF.inplace = 0;
10501         ARGF.inplace = strdup(RSTRING_PTR(val));
10502     }
10503     return argf;
10504 }
10505 
10506 static void
10507 opt_i_set(VALUE val, ID id, VALUE *var)
10508 {
10509     argf_inplace_mode_set(*var, val);
10510 }
10511 
10512 const char *
10513 ruby_get_inplace_mode(void)
10514 {
10515     return ARGF.inplace;
10516 }
10517 
10518 void
10519 ruby_set_inplace_mode(const char *suffix)
10520 {
10521     if (ARGF.inplace) free(ARGF.inplace);
10522     ARGF.inplace = 0;
10523     if (suffix) ARGF.inplace = strdup(suffix);
10524 }
10525 
10526 /*
10527  *  call-seq:
10528  *     ARGF.argv  -> ARGV
10529  *
10530  *  Returns the +ARGV+ array, which contains the arguments passed to your
10531  *  script, one per element.
10532  *
10533  *  For example:
10534  *
10535  *      $ ruby argf.rb -v glark.txt
10536  *
10537  *      ARGF.argv   #=> ["-v", "glark.txt"]
10538  *
10539  */
10540 static VALUE
10541 argf_argv(VALUE argf)
10542 {
10543     return ARGF.argv;
10544 }
10545 
10546 static VALUE
10547 argf_argv_getter(ID id, VALUE *var)
10548 {
10549     return argf_argv(*var);
10550 }
10551 
10552 VALUE
10553 rb_get_argv(void)
10554 {
10555     return ARGF.argv;
10556 }
10557 
10558 /*
10559  *  call-seq:
10560  *     ARGF.to_write_io  -> io
10561  *
10562  *  Returns IO instance tied to _ARGF_ for writing if inplace mode is
10563  *  enabled.
10564  */
10565 static VALUE
10566 argf_write_io(VALUE argf)
10567 {
10568     if (!RTEST(ARGF.current_file)) {
10569         rb_raise(rb_eIOError, "not opened for writing");
10570     }
10571     return GetWriteIO(ARGF.current_file);
10572 }
10573 
10574 /*
10575  *  call-seq:
10576  *     ARGF.write(string)   -> integer
10577  *
10578  *  Writes _string_ if inplace mode.
10579  */
10580 static VALUE
10581 argf_write(VALUE argf, VALUE str)
10582 {
10583     return rb_io_write(argf_write_io(argf), str);
10584 }
10585 
10586 /*
10587  * Document-class: IOError
10588  *
10589  * Raised when an IO operation fails.
10590  *
10591  *    File.open("/etc/hosts") {|f| f << "example"}
10592  *      #=> IOError: not opened for writing
10593  *
10594  *    File.open("/etc/hosts") {|f| f.close; f.read }
10595  *      #=> IOError: closed stream
10596  *
10597  * Note that some IO failures raise +SystemCallError+s and these are not
10598  * subclasses of IOError:
10599  *
10600  *    File.open("does/not/exist")
10601  *      #=> Errno::ENOENT: No such file or directory - does/not/exist
10602  */
10603 
10604 /*
10605  * Document-class: EOFError
10606  *
10607  * Raised by some IO operations when reaching the end of file. Many IO
10608  * methods exist in two forms,
10609  *
10610  * one that returns +nil+ when the end of file is reached, the other
10611  * raises EOFError +EOFError+.
10612  *
10613  * +EOFError+ is a subclass of +IOError+.
10614  *
10615  *    file = File.open("/etc/hosts")
10616  *    file.read
10617  *    file.gets     #=> nil
10618  *    file.readline #=> EOFError: end of file reached
10619  */
10620 
10621 /*
10622  * Document-class:  ARGF
10623  *
10624  * +ARGF+ is a stream designed for use in scripts that process files given as
10625  * command-line arguments or passed in via STDIN.
10626  *
10627  * The arguments passed to your script are stored in the +ARGV+ Array, one
10628  * argument per element. +ARGF+ assumes that any arguments that aren't
10629  * filenames have been removed from +ARGV+. For example:
10630  *
10631  *     $ ruby argf.rb --verbose file1 file2
10632  *
10633  *     ARGV  #=> ["--verbose", "file1", "file2"]
10634  *     option = ARGV.shift #=> "--verbose"
10635  *     ARGV  #=> ["file1", "file2"]
10636  *
10637  * You can now use +ARGF+ to work with a concatenation of each of these named
10638  * files. For instance, +ARGF.read+ will return the contents of _file1_
10639  * followed by the contents of _file2_.
10640  *
10641  * After a file in +ARGV+ has been read +ARGF+ removes it from the Array.
10642  * Thus, after all files have been read +ARGV+ will be empty.
10643  *
10644  * You can manipulate +ARGV+ yourself to control what +ARGF+ operates on. If
10645  * you remove a file from +ARGV+, it is ignored by +ARGF+; if you add files to
10646  * +ARGV+, they are treated as if they were named on the command line. For
10647  * example:
10648  *
10649  *     ARGV.replace ["file1"]
10650  *     ARGF.readlines # Returns the contents of file1 as an Array
10651  *     ARGV           #=> []
10652  *     ARGV.replace ["file2", "file3"]
10653  *     ARGF.read      # Returns the contents of file2 and file3
10654  *
10655  * If +ARGV+ is empty, +ARGF+ acts as if it contained STDIN, i.e. the data
10656  * piped to your script. For example:
10657  *
10658  *     $ echo "glark" | ruby -e 'p ARGF.read'
10659  *     "glark\n"
10660  */
10661 
10662 /*
10663  *  Class <code>IO</code> is the basis for all input and output in Ruby.
10664  *  An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
10665  *  so may use more than one native operating system stream.
10666  *
10667  *  Many of the examples in this section use class <code>File</code>,
10668  *  the only standard subclass of <code>IO</code>. The two classes are
10669  *  closely associated.
10670  *
10671  *  As used in this section, <em>portname</em> may take any of the
10672  *  following forms.
10673  *
10674  *  * A plain string represents a filename suitable for the underlying
10675  *    operating system.
10676  *
10677  *  * A string starting with ``<code>|</code>'' indicates a subprocess.
10678  *    The remainder of the string following the ``<code>|</code>'' is
10679  *    invoked as a process with appropriate input/output channels
10680  *    connected to it.
10681  *
10682  *  * A string equal to ``<code>|-</code>'' will create another Ruby
10683  *    instance as a subprocess.
10684  *
10685  *  Ruby will convert pathnames between different operating system
10686  *  conventions if possible. For instance, on a Windows system the
10687  *  filename ``<code>/gumby/ruby/test.rb</code>'' will be opened as
10688  *  ``<code>\gumby\ruby\test.rb</code>''. When specifying a
10689  *  Windows-style filename in a Ruby string, remember to escape the
10690  *  backslashes:
10691  *
10692  *     "c:\\gumby\\ruby\\test.rb"
10693  *
10694  *  Our examples here will use the Unix-style forward slashes;
10695  *  <code>File::SEPARATOR</code> can be used to get the
10696  *  platform-specific separator character.
10697  *
10698  *  I/O ports may be opened in any one of several different modes, which
10699  *  are shown in this section as <em>mode</em>. The mode may
10700  *  either be a Fixnum or a String. If numeric, it should be
10701  *  one of the operating system specific constants (O_RDONLY,
10702  *  O_WRONLY, O_RDWR, O_APPEND and so on). See man open(2) for
10703  *  more information.
10704  *
10705  *  If the mode is given as a String, it must be one of the
10706  *  values listed in the following table.
10707  *
10708  *    Mode |  Meaning
10709  *    -----+--------------------------------------------------------
10710  *    "r"  |  Read-only, starts at beginning of file  (default mode).
10711  *    -----+--------------------------------------------------------
10712  *    "r+" |  Read-write, starts at beginning of file.
10713  *    -----+--------------------------------------------------------
10714  *    "w"  |  Write-only, truncates existing file
10715  *         |  to zero length or creates a new file for writing.
10716  *    -----+--------------------------------------------------------
10717  *    "w+" |  Read-write, truncates existing file to zero length
10718  *         |  or creates a new file for reading and writing.
10719  *    -----+--------------------------------------------------------
10720  *    "a"  |  Write-only, starts at end of file if file exists,
10721  *         |  otherwise creates a new file for writing.
10722  *    -----+--------------------------------------------------------
10723  *    "a+" |  Read-write, starts at end of file if file exists,
10724  *         |  otherwise creates a new file for reading and
10725  *         |  writing.
10726  *    -----+--------------------------------------------------------
10727  *     "b" |  Binary file mode (may appear with
10728  *         |  any of the key letters listed above).
10729  *         |  Suppresses EOL <-> CRLF conversion on Windows. And
10730  *         |  sets external encoding to ASCII-8BIT unless explicitly
10731  *         |  specified.
10732  *    -----+--------------------------------------------------------
10733  *     "t" |  Text file mode (may appear with
10734  *         |  any of the key letters listed above except "b").
10735  *
10736  *
10737  *  The global constant ARGF (also accessible as $<) provides an
10738  *  IO-like stream which allows access to all files mentioned on the
10739  *  command line (or STDIN if no files are mentioned). ARGF provides
10740  *  the methods <code>#path</code> and <code>#filename</code> to access
10741  *  the name of the file currently being read.
10742  *
10743  *  == io/console
10744  *
10745  *  The io/console extension provides methods for interacting with the
10746  *  console.  The console can be accessed from <code>IO.console</code> or
10747  *  the standard input/output/error IO objects.
10748  *
10749  *  Requiring io/console adds the following methods:
10750  *
10751  *  * IO::console
10752  *  * IO#raw
10753  *  * IO#raw!
10754  *  * IO#cooked
10755  *  * IO#cooked!
10756  *  * IO#getch
10757  *  * IO#echo=
10758  *  * IO#echo?
10759  *  * IO#noecho
10760  *  * IO#winsize
10761  *  * IO#winsize=
10762  *  * IO#iflush
10763  *  * IO#ioflush
10764  *  * IO#oflush
10765  *
10766  *  Example:
10767  *
10768  *    require 'io/console'
10769  *    rows, columns = $stdin.winsize
10770  *    puts "You screen is #{columns} wide and #{rows} tall"
10771  */
10772 
10773 void
10774 Init_IO(void)
10775 {
10776 #undef rb_intern
10777 #define rb_intern(str) rb_intern_const(str)
10778 
10779     VALUE rb_cARGF;
10780 #ifdef __CYGWIN__
10781 #include <sys/cygwin.h>
10782     static struct __cygwin_perfile pf[] =
10783     {
10784         {"", O_RDONLY | O_BINARY},
10785         {"", O_WRONLY | O_BINARY},
10786         {"", O_RDWR | O_BINARY},
10787         {"", O_APPEND | O_BINARY},
10788         {NULL, 0}
10789     };
10790     cygwin_internal(CW_PERFILE, pf);
10791 #endif
10792 
10793     rb_eIOError = rb_define_class("IOError", rb_eStandardError);
10794     rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
10795 
10796     id_write = rb_intern("write");
10797     id_read = rb_intern("read");
10798     id_getc = rb_intern("getc");
10799     id_flush = rb_intern("flush");
10800     id_readpartial = rb_intern("readpartial");
10801     id_set_encoding = rb_intern("set_encoding");
10802 
10803     rb_define_global_function("syscall", rb_f_syscall, -1);
10804 
10805     rb_define_global_function("open", rb_f_open, -1);
10806     rb_define_global_function("printf", rb_f_printf, -1);
10807     rb_define_global_function("print", rb_f_print, -1);
10808     rb_define_global_function("putc", rb_f_putc, 1);
10809     rb_define_global_function("puts", rb_f_puts, -1);
10810     rb_define_global_function("gets", rb_f_gets, -1);
10811     rb_define_global_function("readline", rb_f_readline, -1);
10812     rb_define_global_function("select", rb_f_select, -1);
10813 
10814     rb_define_global_function("readlines", rb_f_readlines, -1);
10815 
10816     rb_define_global_function("`", rb_f_backquote, 1);
10817 
10818     rb_define_global_function("p", rb_f_p, -1);
10819     rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
10820 
10821     rb_cIO = rb_define_class("IO", rb_cObject);
10822     rb_include_module(rb_cIO, rb_mEnumerable);
10823 
10824     rb_mWaitReadable = rb_define_module_under(rb_cIO, "WaitReadable");
10825     rb_mWaitWritable = rb_define_module_under(rb_cIO, "WaitWritable");
10826 
10827 #if 0
10828     /* This is necessary only for forcing rdoc handle File::open */
10829     rb_define_singleton_method(rb_cFile, "open",  rb_io_s_open, -1);
10830 #endif
10831 
10832     rb_define_alloc_func(rb_cIO, io_alloc);
10833     rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
10834     rb_define_singleton_method(rb_cIO, "open",  rb_io_s_open, -1);
10835     rb_define_singleton_method(rb_cIO, "sysopen",  rb_io_s_sysopen, -1);
10836     rb_define_singleton_method(rb_cIO, "for_fd", rb_io_s_for_fd, -1);
10837     rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
10838     rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
10839     rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
10840     rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
10841     rb_define_singleton_method(rb_cIO, "binread", rb_io_s_binread, -1);
10842     rb_define_singleton_method(rb_cIO, "write", rb_io_s_write, -1);
10843     rb_define_singleton_method(rb_cIO, "binwrite", rb_io_s_binwrite, -1);
10844     rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
10845     rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
10846     rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
10847     rb_define_singleton_method(rb_cIO, "copy_stream", rb_io_s_copy_stream, -1);
10848 
10849     rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
10850 
10851     rb_output_fs = Qnil;
10852     rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
10853 
10854     rb_rs = rb_default_rs = rb_usascii_str_new2("\n");
10855     rb_gc_register_mark_object(rb_default_rs);
10856     rb_output_rs = Qnil;
10857     OBJ_FREEZE(rb_default_rs);  /* avoid modifying RS_default */
10858     rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
10859     rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
10860     rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
10861 
10862     rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
10863 
10864     rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
10865     rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
10866 
10867     rb_define_method(rb_cIO, "print", rb_io_print, -1);
10868     rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
10869     rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
10870     rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
10871 
10872     rb_define_method(rb_cIO, "each",  rb_io_each_line, -1);
10873     rb_define_method(rb_cIO, "each_line",  rb_io_each_line, -1);
10874     rb_define_method(rb_cIO, "each_byte",  rb_io_each_byte, 0);
10875     rb_define_method(rb_cIO, "each_char",  rb_io_each_char, 0);
10876     rb_define_method(rb_cIO, "each_codepoint",  rb_io_each_codepoint, 0);
10877     rb_define_method(rb_cIO, "lines",  rb_io_each_line, -1);
10878     rb_define_method(rb_cIO, "bytes",  rb_io_each_byte, 0);
10879     rb_define_method(rb_cIO, "chars",  rb_io_each_char, 0);
10880     rb_define_method(rb_cIO, "codepoints",  rb_io_each_codepoint, 0);
10881 
10882     rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
10883     rb_define_method(rb_cIO, "sysread",  rb_io_sysread, -1);
10884 
10885     rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
10886     rb_define_alias(rb_cIO, "to_i", "fileno");
10887     rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
10888 
10889     rb_define_method(rb_cIO, "fsync",   rb_io_fsync, 0);
10890     rb_define_method(rb_cIO, "fdatasync",   rb_io_fdatasync, 0);
10891     rb_define_method(rb_cIO, "sync",   rb_io_sync, 0);
10892     rb_define_method(rb_cIO, "sync=",  rb_io_set_sync, 1);
10893 
10894     rb_define_method(rb_cIO, "lineno",   rb_io_lineno, 0);
10895     rb_define_method(rb_cIO, "lineno=",  rb_io_set_lineno, 1);
10896 
10897     rb_define_method(rb_cIO, "readlines",  rb_io_readlines, -1);
10898 
10899     rb_define_method(rb_cIO, "read_nonblock",  io_read_nonblock, -1);
10900     rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, 1);
10901     rb_define_method(rb_cIO, "readpartial",  io_readpartial, -1);
10902     rb_define_method(rb_cIO, "read",  io_read, -1);
10903     rb_define_method(rb_cIO, "write", io_write_m, 1);
10904     rb_define_method(rb_cIO, "gets",  rb_io_gets_m, -1);
10905     rb_define_method(rb_cIO, "readline",  rb_io_readline, -1);
10906     rb_define_method(rb_cIO, "getc",  rb_io_getc, 0);
10907     rb_define_method(rb_cIO, "getbyte",  rb_io_getbyte, 0);
10908     rb_define_method(rb_cIO, "readchar",  rb_io_readchar, 0);
10909     rb_define_method(rb_cIO, "readbyte",  rb_io_readbyte, 0);
10910     rb_define_method(rb_cIO, "ungetbyte",rb_io_ungetbyte, 1);
10911     rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
10912     rb_define_method(rb_cIO, "<<",    rb_io_addstr, 1);
10913     rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
10914     rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
10915     rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
10916     rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
10917     rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
10918     rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
10919     rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
10920     rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
10921     rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
10922     rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
10923     rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
10924 
10925     rb_define_method(rb_cIO, "close_on_exec?", rb_io_close_on_exec_p, 0);
10926     rb_define_method(rb_cIO, "close_on_exec=", rb_io_set_close_on_exec, 1);
10927 
10928     rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
10929     rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
10930     rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
10931     rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
10932 
10933     rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
10934     rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
10935     rb_define_method(rb_cIO, "binmode",  rb_io_binmode_m, 0);
10936     rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
10937     rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
10938     rb_define_method(rb_cIO, "advise", rb_io_advise, -1);
10939 
10940     rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
10941     rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
10942     rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
10943     rb_define_method(rb_cIO, "inspect",  rb_io_inspect, 0);
10944 
10945     rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
10946     rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
10947     rb_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
10948 
10949     rb_define_method(rb_cIO, "autoclose?", rb_io_autoclose_p, 0);
10950     rb_define_method(rb_cIO, "autoclose=", rb_io_set_autoclose, 1);
10951 
10952     rb_define_variable("$stdin", &rb_stdin);
10953     rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
10954     rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
10955     rb_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO, "<STDOUT>");
10956     rb_define_hooked_variable("$stderr", &rb_stderr, 0, stdout_setter);
10957     rb_stderr = prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
10958     rb_define_hooked_variable("$>", &rb_stdout, 0, stdout_setter);
10959     orig_stdout = rb_stdout;
10960     rb_deferr = orig_stderr = rb_stderr;
10961 
10962     /* Holds the original stdin */
10963     rb_define_global_const("STDIN", rb_stdin);
10964     /* Holds the original stdout */
10965     rb_define_global_const("STDOUT", rb_stdout);
10966     /* Holds the original stderr */
10967     rb_define_global_const("STDERR", rb_stderr);
10968 
10969 #if 0
10970     /* Hack to get rdoc to regard ARGF as a class: */
10971     rb_cARGF = rb_define_class("ARGF", rb_cObject);
10972 #endif
10973 
10974     rb_cARGF = rb_class_new(rb_cObject);
10975     rb_set_class_path(rb_cARGF, rb_cObject, "ARGF.class");
10976     rb_define_alloc_func(rb_cARGF, argf_alloc);
10977 
10978     rb_include_module(rb_cARGF, rb_mEnumerable);
10979 
10980     rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
10981     rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
10982     rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
10983     rb_define_method(rb_cARGF, "argv", argf_argv, 0);
10984 
10985     rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
10986     rb_define_method(rb_cARGF, "to_i", argf_fileno, 0);
10987     rb_define_method(rb_cARGF, "to_io", argf_to_io, 0);
10988     rb_define_method(rb_cARGF, "to_write_io", argf_write_io, 0);
10989     rb_define_method(rb_cARGF, "each",  argf_each_line, -1);
10990     rb_define_method(rb_cARGF, "each_line",  argf_each_line, -1);
10991     rb_define_method(rb_cARGF, "each_byte",  argf_each_byte, 0);
10992     rb_define_method(rb_cARGF, "each_char",  argf_each_char, 0);
10993     rb_define_method(rb_cARGF, "lines", argf_each_line, -1);
10994     rb_define_method(rb_cARGF, "bytes", argf_each_byte, 0);
10995     rb_define_method(rb_cARGF, "chars", argf_each_char, 0);
10996 
10997     rb_define_method(rb_cARGF, "read",  argf_read, -1);
10998     rb_define_method(rb_cARGF, "readpartial",  argf_readpartial, -1);
10999     rb_define_method(rb_cARGF, "read_nonblock",  argf_read_nonblock, -1);
11000     rb_define_method(rb_cARGF, "readlines", argf_readlines, -1);
11001     rb_define_method(rb_cARGF, "to_a", argf_readlines, -1);
11002     rb_define_method(rb_cARGF, "gets", argf_gets, -1);
11003     rb_define_method(rb_cARGF, "readline", argf_readline, -1);
11004     rb_define_method(rb_cARGF, "getc", argf_getc, 0);
11005     rb_define_method(rb_cARGF, "getbyte", argf_getbyte, 0);
11006     rb_define_method(rb_cARGF, "readchar", argf_readchar, 0);
11007     rb_define_method(rb_cARGF, "readbyte", argf_readbyte, 0);
11008     rb_define_method(rb_cARGF, "tell", argf_tell, 0);
11009     rb_define_method(rb_cARGF, "seek", argf_seek_m, -1);
11010     rb_define_method(rb_cARGF, "rewind", argf_rewind, 0);
11011     rb_define_method(rb_cARGF, "pos", argf_tell, 0);
11012     rb_define_method(rb_cARGF, "pos=", argf_set_pos, 1);
11013     rb_define_method(rb_cARGF, "eof", argf_eof, 0);
11014     rb_define_method(rb_cARGF, "eof?", argf_eof, 0);
11015     rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
11016     rb_define_method(rb_cARGF, "binmode?", argf_binmode_p, 0);
11017 
11018     rb_define_method(rb_cARGF, "write", argf_write, 1);
11019     rb_define_method(rb_cARGF, "print", rb_io_print, -1);
11020     rb_define_method(rb_cARGF, "putc", rb_io_putc, 1);
11021     rb_define_method(rb_cARGF, "puts", rb_io_puts, -1);
11022     rb_define_method(rb_cARGF, "printf", rb_io_printf, -1);
11023 
11024     rb_define_method(rb_cARGF, "filename", argf_filename, 0);
11025     rb_define_method(rb_cARGF, "path", argf_filename, 0);
11026     rb_define_method(rb_cARGF, "file", argf_file, 0);
11027     rb_define_method(rb_cARGF, "skip", argf_skip, 0);
11028     rb_define_method(rb_cARGF, "close", argf_close_m, 0);
11029     rb_define_method(rb_cARGF, "closed?", argf_closed, 0);
11030 
11031     rb_define_method(rb_cARGF, "lineno",   argf_lineno, 0);
11032     rb_define_method(rb_cARGF, "lineno=",  argf_set_lineno, 1);
11033 
11034     rb_define_method(rb_cARGF, "inplace_mode", argf_inplace_mode_get, 0);
11035     rb_define_method(rb_cARGF, "inplace_mode=", argf_inplace_mode_set, 1);
11036 
11037     rb_define_method(rb_cARGF, "external_encoding", argf_external_encoding, 0);
11038     rb_define_method(rb_cARGF, "internal_encoding", argf_internal_encoding, 0);
11039     rb_define_method(rb_cARGF, "set_encoding", argf_set_encoding, -1);
11040 
11041     argf = rb_class_new_instance(0, 0, rb_cARGF);
11042 
11043     rb_define_readonly_variable("$<", &argf);
11044     /*
11045      * ARGF is a stream designed for use in scripts that process files given
11046      * as command-line arguments or passed in via STDIN.
11047      *
11048      * See ARGF (the class) for more details.
11049      */
11050     rb_define_global_const("ARGF", argf);
11051 
11052     rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
11053     rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, rb_gvar_readonly_setter);
11054     ARGF.filename = rb_str_new2("-");
11055 
11056     rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
11057     rb_define_hooked_variable("$*", &argf, argf_argv_getter, rb_gvar_readonly_setter);
11058 
11059 #if defined (_WIN32) || defined(__CYGWIN__)
11060     atexit(pipe_atexit);
11061 #endif
11062 
11063     Init_File();
11064 
11065     rb_define_method(rb_cFile, "initialize",  rb_file_initialize, -1);
11066 
11067     /* open for reading only */
11068     rb_file_const("RDONLY", INT2FIX(O_RDONLY));
11069     /* open for writing only */
11070     rb_file_const("WRONLY", INT2FIX(O_WRONLY));
11071     /* open for reading and writing */
11072     rb_file_const("RDWR", INT2FIX(O_RDWR));
11073     /* append on each write */
11074     rb_file_const("APPEND", INT2FIX(O_APPEND));
11075     /* create file if it does not exist */
11076     rb_file_const("CREAT", INT2FIX(O_CREAT));
11077     /* error if CREAT and the file exists */
11078     rb_file_const("EXCL", INT2FIX(O_EXCL));
11079 #if defined(O_NDELAY) || defined(O_NONBLOCK)
11080 # ifndef O_NONBLOCK
11081 #   define O_NONBLOCK O_NDELAY
11082 # endif
11083     /* do not block on open or for data to become available */
11084     rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK));
11085 #endif
11086     /* truncate size to 0 */
11087     rb_file_const("TRUNC", INT2FIX(O_TRUNC));
11088 #ifdef O_NOCTTY
11089     /* not to make opened IO the controlling terminal device */
11090     rb_file_const("NOCTTY", INT2FIX(O_NOCTTY));
11091 #endif
11092 #ifndef O_BINARY
11093 # define  O_BINARY 0
11094 #endif
11095     /* disable line code conversion and make ASCII-8BIT */
11096     rb_file_const("BINARY", INT2FIX(O_BINARY));
11097 #ifdef O_SYNC
11098     rb_file_const("SYNC", INT2FIX(O_SYNC));
11099 #endif
11100 #ifdef O_DSYNC
11101     rb_file_const("DSYNC", INT2FIX(O_DSYNC));
11102 #endif
11103 #ifdef O_RSYNC
11104     rb_file_const("RSYNC", INT2FIX(O_RSYNC));
11105 #endif
11106 #ifdef O_NOFOLLOW
11107     /* do not follow symlinks */
11108     rb_file_const("NOFOLLOW", INT2FIX(O_NOFOLLOW)); /* FreeBSD, Linux */
11109 #endif
11110 #ifdef O_NOATIME
11111     /* do not change atime */
11112     rb_file_const("NOATIME", INT2FIX(O_NOATIME)); /* Linux */
11113 #endif
11114 #ifdef O_DIRECT
11115     /*  Try to minimize cache effects of the I/O to and from this file. */
11116     rb_file_const("DIRECT", INT2FIX(O_DIRECT));
11117 #endif
11118 
11119     sym_mode = ID2SYM(rb_intern("mode"));
11120     sym_perm = ID2SYM(rb_intern("perm"));
11121     sym_extenc = ID2SYM(rb_intern("external_encoding"));
11122     sym_intenc = ID2SYM(rb_intern("internal_encoding"));
11123     sym_encoding = ID2SYM(rb_intern("encoding"));
11124     sym_open_args = ID2SYM(rb_intern("open_args"));
11125     sym_textmode = ID2SYM(rb_intern("textmode"));
11126     sym_binmode = ID2SYM(rb_intern("binmode"));
11127     sym_autoclose = ID2SYM(rb_intern("autoclose"));
11128     sym_normal = ID2SYM(rb_intern("normal"));
11129     sym_sequential = ID2SYM(rb_intern("sequential"));
11130     sym_random = ID2SYM(rb_intern("random"));
11131     sym_willneed = ID2SYM(rb_intern("willneed"));
11132     sym_dontneed = ID2SYM(rb_intern("dontneed"));
11133     sym_noreuse = ID2SYM(rb_intern("noreuse"));
11134 }
11135