add a test.
[ruby-svn.git] / io.c
blob08578cf0355647fda519833dfb0429f0a79cb2ba
1 /**********************************************************************
3 io.c -
5 $Author$
6 created at: Fri Oct 15 18:08:59 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/io.h"
16 #include "ruby/signal.h"
17 #include "vm_core.h"
18 #include <ctype.h>
19 #include <errno.h>
21 #define free(x) xfree(x)
23 #if defined(DOSISH) || defined(__CYGWIN__)
24 #include <io.h>
25 #endif
27 #include <sys/types.h>
28 #if defined HAVE_NET_SOCKET_H
29 # include <net/socket.h>
30 #elif defined HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
34 #if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
35 # define NO_SAFE_RENAME
36 #endif
38 #if defined(MSDOS) || defined(__CYGWIN__) || defined(_WIN32)
39 # define NO_LONG_FNAME
40 #endif
42 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(sun) || defined(_nec_ews)
43 # define USE_SETVBUF
44 #endif
46 #ifdef __QNXNTO__
47 #include "unix.h"
48 #endif
50 #include <sys/types.h>
51 #if defined(HAVE_SYS_IOCTL_H) && !defined(DJGPP) && !defined(_WIN32) && !defined(__human68k__)
52 #include <sys/ioctl.h>
53 #endif
54 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
55 #include <fcntl.h>
56 #elif defined(HAVE_SYS_FCNTL_H)
57 #include <sys/fcntl.h>
58 #endif
60 #if !HAVE_OFF_T && !defined(off_t)
61 # define off_t long
62 #endif
64 #include <sys/stat.h>
66 /* EMX has sys/param.h, but.. */
67 #if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
68 # include <sys/param.h>
69 #endif
71 #if !defined NOFILE
72 # define NOFILE 64
73 #endif
75 #ifdef HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif
79 #ifdef HAVE_SYSCALL_H
80 #include <syscall.h>
81 #elif defined HAVE_SYS_SYSCALL_H
82 #include <sys/syscall.h>
83 #endif
85 extern void Init_File(void);
87 #ifdef __BEOS__
88 # ifndef NOFILE
89 # define NOFILE (OPEN_MAX)
90 # endif
91 #endif
93 #include "ruby/util.h"
95 #ifndef O_ACCMODE
96 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
97 #endif
99 #if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
100 # error off_t is bigger than long, but you have no long long...
101 #endif
103 #ifndef PIPE_BUF
104 # ifdef _POSIX_PIPE_BUF
105 # define PIPE_BUF _POSIX_PIPE_BUF
106 # else
107 # define PIPE_BUF 512 /* is this ok? */
108 # endif
109 #endif
111 VALUE rb_cIO;
112 VALUE rb_eEOFError;
113 VALUE rb_eIOError;
115 VALUE rb_stdin, rb_stdout, rb_stderr;
116 VALUE rb_deferr; /* rescue VIM plugin */
117 static VALUE orig_stdout, orig_stderr;
119 VALUE rb_output_fs;
120 VALUE rb_rs;
121 VALUE rb_output_rs;
122 VALUE rb_default_rs;
124 static VALUE argf;
126 static ID id_write, id_read, id_getc, id_flush, id_encode, id_readpartial;
127 static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
129 struct timeval rb_time_interval(VALUE);
131 struct argf {
132 VALUE filename, current_file;
133 int gets_lineno;
134 int init_p, next_p;
135 VALUE lineno;
136 VALUE argv;
137 char *inplace;
138 int binmode;
139 rb_encoding *enc, *enc2;
142 static int max_file_descriptor = NOFILE;
143 #define UPDATE_MAXFD(fd) \
144 do { \
145 if (max_file_descriptor < (fd)) max_file_descriptor = (fd); \
146 } while (0)
148 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
149 #define ARGF argf_of(argf)
151 #ifdef _STDIO_USES_IOSTREAM /* GNU libc */
152 # ifdef _IO_fpos_t
153 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
154 # else
155 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
156 # endif
157 #elif defined(FILE_COUNT)
158 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
159 #elif defined(FILE_READEND)
160 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
161 #elif defined(__BEOS__)
162 # define STDIO_READ_DATA_PENDING(fp) (fp->_state._eof == 0)
163 #elif defined(__VMS)
164 # define STDIO_READ_DATA_PENDING(fp) (((unsigned int)(*(fp))->_cnt) > 0)
165 #else
166 # define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
167 #endif
169 #if defined(__VMS)
170 #define fopen(file_spec, mode) fopen(file_spec, mode, "rfm=stmlf")
171 #define open(file_spec, flags, mode) open(file_spec, flags, mode, "rfm=stmlf")
172 #endif
174 #define GetWriteIO(io) rb_io_get_write_io(io)
176 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf_len)
177 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf_len)
178 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf+(fptr)->rbuf_off)
179 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
181 #define READ_CHECK(fptr) do {\
182 if (!READ_DATA_PENDING(fptr)) {\
183 rb_thread_wait_fd((fptr)->fd);\
184 rb_io_check_closed(fptr);\
186 } while(0)
188 #ifndef S_ISSOCK
189 # ifdef _S_ISSOCK
190 # define S_ISSOCK(m) _S_ISSOCK(m)
191 # else
192 # ifdef _S_IFSOCK
193 # define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
194 # else
195 # ifdef S_IFSOCK
196 # define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
197 # endif
198 # endif
199 # endif
200 #endif
202 #if !defined HAVE_SHUTDOWN && !defined shutdown
203 #define shutdown(a,b) 0
204 #endif
206 #if defined(_WIN32)
207 #define is_socket(fd, path) rb_w32_is_socket(fd)
208 #elif !defined(S_ISSOCK)
209 #define is_socket(fd, path) 0
210 #else
211 static int
212 is_socket(int fd, const char *path)
214 struct stat sbuf;
215 if (fstat(fd, &sbuf) < 0)
216 rb_sys_fail(path);
217 return S_ISSOCK(sbuf.st_mode);
219 #endif
221 void
222 rb_eof_error(void)
224 rb_raise(rb_eEOFError, "end of file reached");
227 VALUE
228 rb_io_taint_check(VALUE io)
230 if (!OBJ_UNTRUSTED(io) && rb_safe_level() >= 4)
231 rb_raise(rb_eSecurityError, "Insecure: operation on trusted IO");
232 rb_check_frozen(io);
233 return io;
236 void
237 rb_io_check_initialized(rb_io_t *fptr)
239 if (!fptr) {
240 rb_raise(rb_eIOError, "uninitialized stream");
244 void
245 rb_io_check_closed(rb_io_t *fptr)
247 rb_io_check_initialized(fptr);
248 if (fptr->fd < 0) {
249 rb_raise(rb_eIOError, "closed stream");
253 static int io_fflush(rb_io_t *);
255 VALUE
256 rb_io_get_io(VALUE io)
258 return rb_convert_type(io, T_FILE, "IO", "to_io");
261 static VALUE
262 rb_io_check_io(VALUE io)
264 return rb_check_convert_type(io, T_FILE, "IO", "to_io");
267 VALUE
268 rb_io_get_write_io(VALUE io)
270 VALUE write_io;
271 rb_io_check_initialized(RFILE(io)->fptr);
272 write_io = RFILE(io)->fptr->tied_io_for_writing;
273 if (write_io) {
274 return write_io;
276 return io;
280 * call-seq:
281 * IO.try_convert(obj) -> io or nil
283 * Try to convert <i>obj</i> into an IO, using to_io method.
284 * Returns converted IO or nil if <i>obj</i> cannot be converted
285 * for any reason.
287 * IO.try_convert(STDOUT) # => STDOUT
288 * IO.try_convert("STDOUT") # => nil
290 static VALUE
291 rb_io_s_try_convert(VALUE dummy, VALUE io)
293 return rb_io_check_io(io);
296 static void
297 io_unread(rb_io_t *fptr)
299 off_t r;
300 rb_io_check_closed(fptr);
301 if (fptr->rbuf_len == 0 || fptr->mode & FMODE_DUPLEX)
302 return;
303 /* xxx: target position may be negative if buffer is filled by ungetc */
304 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
305 if (!(fptr->mode & FMODE_BINMODE)) {
306 int len = fptr->rbuf_len;
307 while (fptr->rbuf_len-- > 0) {
308 if (fptr->rbuf[fptr->rbuf_len] == '\n')
309 ++len;
311 r = lseek(fptr->fd, -len, SEEK_CUR);
313 else
314 #endif
315 r = lseek(fptr->fd, -fptr->rbuf_len, SEEK_CUR);
316 if (r < 0) {
317 if (errno == ESPIPE)
318 fptr->mode |= FMODE_DUPLEX;
319 return;
321 fptr->rbuf_off = 0;
322 fptr->rbuf_len = 0;
323 return;
326 static rb_encoding *io_input_encoding(rb_io_t *fptr);
328 static void
329 io_ungetc(VALUE str, rb_io_t *fptr)
331 int len = RSTRING_LEN(str);
333 if (rb_enc_dummy_p(io_input_encoding(fptr))) {
334 rb_raise(rb_eNotImpError, "ungetc against dummy encoding is not currently supported");
337 if (fptr->rbuf == NULL) {
338 fptr->rbuf_off = 0;
339 fptr->rbuf_len = 0;
340 if (len > 8192)
341 fptr->rbuf_capa = len;
342 else
343 fptr->rbuf_capa = 8192;
344 fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
346 if (fptr->rbuf_capa < len + fptr->rbuf_len) {
347 rb_raise(rb_eIOError, "ungetc failed");
349 if (fptr->rbuf_off < len) {
350 MEMMOVE(fptr->rbuf+fptr->rbuf_capa-fptr->rbuf_len,
351 fptr->rbuf+fptr->rbuf_off,
352 char, fptr->rbuf_len);
353 fptr->rbuf_off = fptr->rbuf_capa-fptr->rbuf_len;
355 fptr->rbuf_off-=len;
356 fptr->rbuf_len+=len;
357 MEMMOVE(fptr->rbuf+fptr->rbuf_off, RSTRING_PTR(str), char, len);
360 static rb_io_t *
361 flush_before_seek(rb_io_t *fptr)
363 io_fflush(fptr);
364 io_unread(fptr);
365 errno = 0;
366 return fptr;
369 #define io_seek(fptr, ofs, whence) lseek(flush_before_seek(fptr)->fd, ofs, whence)
370 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
372 #ifndef SEEK_CUR
373 # define SEEK_SET 0
374 # define SEEK_CUR 1
375 # define SEEK_END 2
376 #endif
378 #define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
380 void
381 rb_io_check_readable(rb_io_t *fptr)
383 rb_io_check_closed(fptr);
384 if (!(fptr->mode & FMODE_READABLE)) {
385 rb_raise(rb_eIOError, "not opened for reading");
387 if (fptr->wbuf_len) {
388 io_fflush(fptr);
390 if (fptr->tied_io_for_writing) {
391 rb_io_t *wfptr;
392 GetOpenFile(fptr->tied_io_for_writing, wfptr);
393 io_fflush(wfptr);
395 if (!fptr->enc && fptr->fd == 0) {
396 fptr->enc = rb_default_external_encoding();
400 static rb_encoding*
401 io_read_encoding(rb_io_t *fptr)
403 if (fptr->enc) {
404 return fptr->enc;
406 return rb_default_external_encoding();
409 static rb_encoding*
410 io_input_encoding(rb_io_t *fptr)
412 if (fptr->enc2) {
413 return fptr->enc2;
415 return io_read_encoding(fptr);
418 void
419 rb_io_check_writable(rb_io_t *fptr)
421 rb_io_check_closed(fptr);
422 if (!(fptr->mode & FMODE_WRITABLE)) {
423 rb_raise(rb_eIOError, "not opened for writing");
425 if (fptr->rbuf_len) {
426 io_unread(fptr);
431 rb_read_pending(FILE *fp)
433 return STDIO_READ_DATA_PENDING(fp);
437 rb_io_read_pending(rb_io_t *fptr)
439 return READ_DATA_PENDING(fptr);
442 void
443 rb_read_check(FILE *fp)
445 if (!STDIO_READ_DATA_PENDING(fp)) {
446 rb_thread_wait_fd(fileno(fp));
450 void
451 rb_io_read_check(rb_io_t *fptr)
453 if (!READ_DATA_PENDING(fptr)) {
454 rb_thread_wait_fd(fptr->fd);
456 return;
459 static int
460 ruby_dup(int orig)
462 int fd;
464 fd = dup(orig);
465 if (fd < 0) {
466 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
467 rb_gc();
468 fd = dup(orig);
470 if (fd < 0) {
471 rb_sys_fail(0);
474 return fd;
477 static VALUE
478 io_alloc(VALUE klass)
480 NEWOBJ(io, struct RFile);
481 OBJSETUP(io, klass, T_FILE);
483 io->fptr = 0;
485 return (VALUE)io;
488 #ifndef S_ISREG
489 # define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
490 #endif
492 static int
493 wsplit_p(rb_io_t *fptr)
495 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
496 int r;
497 #endif
499 if (!(fptr->mode & FMODE_WSPLIT_INITIALIZED)) {
500 struct stat buf;
501 if (fstat(fptr->fd, &buf) == 0 &&
502 !S_ISREG(buf.st_mode)
503 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
504 && (r = fcntl(fptr->fd, F_GETFL)) != -1 &&
505 !(r & O_NONBLOCK)
506 #endif
508 fptr->mode |= FMODE_WSPLIT;
510 fptr->mode |= FMODE_WSPLIT_INITIALIZED;
512 return fptr->mode & FMODE_WSPLIT;
515 struct io_internal_struct {
516 int fd;
517 void *buf;
518 size_t capa;
521 static VALUE
522 internal_read_func(void *ptr)
524 struct io_internal_struct *iis = (struct io_internal_struct*)ptr;
525 return read(iis->fd, iis->buf, iis->capa);
528 static VALUE
529 internal_write_func(void *ptr)
531 struct io_internal_struct *iis = (struct io_internal_struct*)ptr;
532 return write(iis->fd, iis->buf, iis->capa);
535 static int
536 rb_read_internal(int fd, void *buf, size_t count)
538 struct io_internal_struct iis;
539 iis.fd = fd;
540 iis.buf = buf;
541 iis.capa = count;
543 return rb_thread_blocking_region(internal_read_func, &iis, RB_UBF_DFL, 0);
546 static int
547 rb_write_internal(int fd, void *buf, size_t count)
549 struct io_internal_struct iis;
550 iis.fd = fd;
551 iis.buf = buf;
552 iis.capa = count;
554 return rb_thread_blocking_region(internal_write_func, &iis, RB_UBF_DFL, 0);
557 static int
558 io_fflush(rb_io_t *fptr)
560 int r, l;
561 int wbuf_off, wbuf_len;
563 rb_io_check_closed(fptr);
564 if (fptr->wbuf_len == 0)
565 return 0;
566 if (!rb_thread_fd_writable(fptr->fd)) {
567 rb_io_check_closed(fptr);
569 retry:
570 if (fptr->wbuf_len == 0)
571 return 0;
572 wbuf_off = fptr->wbuf_off;
573 wbuf_len = fptr->wbuf_len;
574 l = wbuf_len;
575 if (PIPE_BUF < l &&
576 !rb_thread_critical &&
577 !rb_thread_alone() &&
578 wsplit_p(fptr)) {
579 l = PIPE_BUF;
581 r = rb_write_internal(fptr->fd, fptr->wbuf+wbuf_off, l);
582 /* xxx: Other threads may modify wbuf.
583 * A lock is required, definitely. */
584 rb_io_check_closed(fptr);
585 if (fptr->wbuf_len <= r) {
586 fptr->wbuf_off = 0;
587 fptr->wbuf_len = 0;
588 return 0;
590 if (0 <= r) {
591 fptr->wbuf_off += r;
592 fptr->wbuf_len -= r;
593 errno = EAGAIN;
595 if (rb_io_wait_writable(fptr->fd)) {
596 rb_io_check_closed(fptr);
597 goto retry;
599 return -1;
602 #ifdef HAVE_RB_FD_INIT
603 static VALUE
604 wait_readable(VALUE p)
606 rb_fdset_t *rfds = (rb_fdset_t *)p;
608 return rb_thread_select(rb_fd_max(rfds), rb_fd_ptr(rfds), NULL, NULL, NULL);
610 #endif
613 rb_io_wait_readable(int f)
615 rb_fdset_t rfds;
617 if (f < 0) {
618 rb_raise(rb_eIOError, "closed stream");
620 switch (errno) {
621 case EINTR:
622 #if defined(ERESTART)
623 case ERESTART:
624 #endif
625 rb_thread_wait_fd(f);
626 return Qtrue;
628 case EAGAIN:
629 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
630 case EWOULDBLOCK:
631 #endif
632 rb_fd_init(&rfds);
633 rb_fd_set(f, &rfds);
634 #ifdef HAVE_RB_FD_INIT
635 rb_ensure(wait_readable, (VALUE)&rfds,
636 (VALUE (*)(VALUE))rb_fd_term, (VALUE)&rfds);
637 #else
638 rb_thread_select(f + 1, &rfds, NULL, NULL, NULL);
639 #endif
640 return Qtrue;
642 default:
643 return Qfalse;
647 #ifdef HAVE_RB_FD_INIT
648 static VALUE
649 wait_writable(VALUE p)
651 rb_fdset_t *wfds = (rb_fdset_t *)p;
653 return rb_thread_select(rb_fd_max(wfds), NULL, rb_fd_ptr(wfds), NULL, NULL);
655 #endif
658 rb_io_wait_writable(int f)
660 rb_fdset_t wfds;
662 if (f < 0) {
663 rb_raise(rb_eIOError, "closed stream");
665 switch (errno) {
666 case EINTR:
667 #if defined(ERESTART)
668 case ERESTART:
669 #endif
670 rb_thread_fd_writable(f);
671 return Qtrue;
673 case EAGAIN:
674 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
675 case EWOULDBLOCK:
676 #endif
677 rb_fd_init(&wfds);
678 rb_fd_set(f, &wfds);
679 #ifdef HAVE_RB_FD_INIT
680 rb_ensure(wait_writable, (VALUE)&wfds,
681 (VALUE (*)(VALUE))rb_fd_term, (VALUE)&wfds);
682 #else
683 rb_thread_select(f + 1, NULL, &wfds, NULL, NULL);
684 #endif
685 return Qtrue;
687 default:
688 return Qfalse;
692 /* writing functions */
693 static long
694 io_fwrite(VALUE str, rb_io_t *fptr)
696 long len, n, r, l, offset = 0;
699 * If an external encoding was specified and it differs from
700 * the strings encoding then we must transcode before writing.
701 * We must also transcode if two encodings were specified
703 if (fptr->enc) {
704 /* transcode str before output */
705 /* the methods in transcode.c are static, so call indirectly */
706 /* Can't use encode! because puts writes a frozen newline */
707 if (fptr->enc2) {
708 str = rb_funcall(str, id_encode, 2,
709 rb_enc_from_encoding(fptr->enc2),
710 rb_enc_from_encoding(fptr->enc));
712 else {
713 str = rb_funcall(str, id_encode, 1,
714 rb_enc_from_encoding(fptr->enc));
718 len = RSTRING_LEN(str);
719 if ((n = len) <= 0) return n;
720 if (fptr->wbuf == NULL && !(fptr->mode & FMODE_SYNC)) {
721 fptr->wbuf_off = 0;
722 fptr->wbuf_len = 0;
723 fptr->wbuf_capa = 8192;
724 fptr->wbuf = ALLOC_N(char, fptr->wbuf_capa);
726 if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) ||
727 (fptr->wbuf && fptr->wbuf_capa <= fptr->wbuf_len + len)) {
728 /* xxx: use writev to avoid double write if available */
729 if (fptr->wbuf_len && fptr->wbuf_len+len <= fptr->wbuf_capa) {
730 if (fptr->wbuf_capa < fptr->wbuf_off+fptr->wbuf_len+len) {
731 MEMMOVE(fptr->wbuf, fptr->wbuf+fptr->wbuf_off, char, fptr->wbuf_len);
732 fptr->wbuf_off = 0;
734 MEMMOVE(fptr->wbuf+fptr->wbuf_off+fptr->wbuf_len, RSTRING_PTR(str)+offset, char, len);
735 fptr->wbuf_len += len;
736 n = 0;
738 if (io_fflush(fptr) < 0)
739 return -1L;
740 if (n == 0)
741 return len;
742 /* avoid context switch between "a" and "\n" in STDERR.puts "a".
743 [ruby-dev:25080] */
744 if (fptr->stdio_file != stderr && !rb_thread_fd_writable(fptr->fd)) {
745 rb_io_check_closed(fptr);
747 retry:
748 l = n;
749 if (PIPE_BUF < l &&
750 !rb_thread_critical &&
751 !rb_thread_alone() &&
752 wsplit_p(fptr)) {
753 l = PIPE_BUF;
755 r = rb_write_internal(fptr->fd, RSTRING_PTR(str)+offset, l);
756 /* xxx: other threads may modify given string. */
757 if (r == n) return len;
758 if (0 <= r) {
759 offset += r;
760 n -= r;
761 errno = EAGAIN;
763 if (rb_io_wait_writable(fptr->fd)) {
764 rb_io_check_closed(fptr);
765 if (offset < RSTRING_LEN(str))
766 goto retry;
768 return -1L;
771 if (fptr->wbuf_off) {
772 if (fptr->wbuf_len)
773 MEMMOVE(fptr->wbuf, fptr->wbuf+fptr->wbuf_off, char, fptr->wbuf_len);
774 fptr->wbuf_off = 0;
776 MEMMOVE(fptr->wbuf+fptr->wbuf_off+fptr->wbuf_len, RSTRING_PTR(str)+offset, char, len);
777 fptr->wbuf_len += len;
778 return len;
781 long
782 rb_io_fwrite(const char *ptr, long len, FILE *f)
784 rb_io_t of;
786 of.fd = fileno(f);
787 of.stdio_file = f;
788 of.mode = FMODE_WRITABLE;
789 of.path = NULL;
790 return io_fwrite(rb_str_new(ptr, len), &of);
794 * call-seq:
795 * ios.write(string) => integer
797 * Writes the given string to <em>ios</em>. The stream must be opened
798 * for writing. If the argument is not a string, it will be converted
799 * to a string using <code>to_s</code>. Returns the number of bytes
800 * written.
802 * count = $stdout.write( "This is a test\n" )
803 * puts "That was #{count} bytes of data"
805 * <em>produces:</em>
807 * This is a test
808 * That was 15 bytes of data
811 static VALUE
812 io_write(VALUE io, VALUE str)
814 rb_io_t *fptr;
815 long n;
816 VALUE tmp;
818 rb_secure(4);
819 io = GetWriteIO(io);
820 str = rb_obj_as_string(str);
821 tmp = rb_io_check_io(io);
822 if (NIL_P(tmp)) {
823 /* port is not IO, call write method for it. */
824 return rb_funcall(io, id_write, 1, str);
826 io = tmp;
827 if (RSTRING_LEN(str) == 0) return INT2FIX(0);
829 GetOpenFile(io, fptr);
830 rb_io_check_writable(fptr);
832 n = io_fwrite(str, fptr);
833 if (n == -1L) rb_sys_fail(fptr->path);
835 return LONG2FIX(n);
838 VALUE
839 rb_io_write(VALUE io, VALUE str)
841 return rb_funcall(io, id_write, 1, str);
845 * call-seq:
846 * ios << obj => ios
848 * String Output---Writes <i>obj</i> to <em>ios</em>.
849 * <i>obj</i> will be converted to a string using
850 * <code>to_s</code>.
852 * $stdout << "Hello " << "world!\n"
854 * <em>produces:</em>
856 * Hello world!
860 VALUE
861 rb_io_addstr(VALUE io, VALUE str)
863 rb_io_write(io, str);
864 return io;
868 * call-seq:
869 * ios.flush => ios
871 * Flushes any buffered data within <em>ios</em> to the underlying
872 * operating system (note that this is Ruby internal buffering only;
873 * the OS may buffer the data as well).
875 * $stdout.print "no newline"
876 * $stdout.flush
878 * <em>produces:</em>
880 * no newline
883 VALUE
884 rb_io_flush(VALUE io)
886 rb_io_t *fptr;
888 if (TYPE(io) != T_FILE) {
889 return rb_funcall(io, id_flush, 0);
892 io = GetWriteIO(io);
893 GetOpenFile(io, fptr);
895 if (fptr->mode & FMODE_WRITABLE) {
896 io_fflush(fptr);
898 if (fptr->mode & FMODE_READABLE) {
899 io_unread(fptr);
902 return io;
906 * call-seq:
907 * ios.pos => integer
908 * ios.tell => integer
910 * Returns the current offset (in bytes) of <em>ios</em>.
912 * f = File.new("testfile")
913 * f.pos #=> 0
914 * f.gets #=> "This is line one\n"
915 * f.pos #=> 17
918 static VALUE
919 rb_io_tell(VALUE io)
921 rb_io_t *fptr;
922 off_t pos;
924 GetOpenFile(io, fptr);
925 pos = io_tell(fptr);
926 if (pos < 0 && errno) rb_sys_fail(fptr->path);
927 return OFFT2NUM(pos);
930 static VALUE
931 rb_io_seek(VALUE io, VALUE offset, int whence)
933 rb_io_t *fptr;
934 off_t pos;
936 pos = NUM2OFFT(offset);
937 GetOpenFile(io, fptr);
938 pos = io_seek(fptr, pos, whence);
939 if (pos < 0 && errno) rb_sys_fail(fptr->path);
941 return INT2FIX(0);
945 * call-seq:
946 * ios.seek(amount, whence=SEEK_SET) -> 0
948 * Seeks to a given offset <i>anInteger</i> in the stream according to
949 * the value of <i>whence</i>:
951 * IO::SEEK_CUR | Seeks to _amount_ plus current position
952 * --------------+----------------------------------------------------
953 * IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
954 * | want a negative value for _amount_)
955 * --------------+----------------------------------------------------
956 * IO::SEEK_SET | Seeks to the absolute location given by _amount_
958 * Example:
960 * f = File.new("testfile")
961 * f.seek(-13, IO::SEEK_END) #=> 0
962 * f.readline #=> "And so on...\n"
965 static VALUE
966 rb_io_seek_m(int argc, VALUE *argv, VALUE io)
968 VALUE offset, ptrname;
969 int whence = SEEK_SET;
971 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
972 whence = NUM2INT(ptrname);
975 return rb_io_seek(io, offset, whence);
979 * call-seq:
980 * ios.pos = integer => integer
982 * Seeks to the given position (in bytes) in <em>ios</em>.
984 * f = File.new("testfile")
985 * f.pos = 17
986 * f.gets #=> "This is line two\n"
989 static VALUE
990 rb_io_set_pos(VALUE io, VALUE offset)
992 rb_io_t *fptr;
993 off_t pos;
995 pos = NUM2OFFT(offset);
996 GetOpenFile(io, fptr);
997 pos = io_seek(fptr, pos, SEEK_SET);
998 if (pos < 0) rb_sys_fail(fptr->path);
1000 return OFFT2NUM(pos);
1004 * call-seq:
1005 * ios.rewind => 0
1007 * Positions <em>ios</em> to the beginning of input, resetting
1008 * <code>lineno</code> to zero.
1010 * f = File.new("testfile")
1011 * f.readline #=> "This is line one\n"
1012 * f.rewind #=> 0
1013 * f.lineno #=> 0
1014 * f.readline #=> "This is line one\n"
1017 static VALUE
1018 rb_io_rewind(VALUE io)
1020 rb_io_t *fptr;
1022 GetOpenFile(io, fptr);
1023 if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path);
1024 if (io == ARGF.current_file) {
1025 ARGF.gets_lineno -= fptr->lineno;
1027 fptr->lineno = 0;
1029 return INT2FIX(0);
1032 static int
1033 io_fillbuf(rb_io_t *fptr)
1035 int r;
1037 if (fptr->rbuf == NULL) {
1038 fptr->rbuf_off = 0;
1039 fptr->rbuf_len = 0;
1040 fptr->rbuf_capa = 8192;
1041 fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
1043 if (fptr->rbuf_len == 0) {
1044 retry:
1046 r = rb_read_internal(fptr->fd, fptr->rbuf, fptr->rbuf_capa);
1048 if (r < 0) {
1049 if (rb_io_wait_readable(fptr->fd))
1050 goto retry;
1051 rb_sys_fail(fptr->path);
1053 fptr->rbuf_off = 0;
1054 fptr->rbuf_len = r;
1055 if (r == 0)
1056 return -1; /* EOF */
1058 return 0;
1062 * call-seq:
1063 * ios.eof => true or false
1064 * ios.eof? => true or false
1066 * Returns true if <em>ios</em> is at end of file that means
1067 * there are no more data to read.
1068 * The stream must be opened for reading or an <code>IOError</code> will be
1069 * raised.
1071 * f = File.new("testfile")
1072 * dummy = f.readlines
1073 * f.eof #=> true
1075 * If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
1076 * blocks until the other end sends some data or closes it.
1078 * r, w = IO.pipe
1079 * Thread.new { sleep 1; w.close }
1080 * r.eof? #=> true after 1 second blocking
1082 * r, w = IO.pipe
1083 * Thread.new { sleep 1; w.puts "a" }
1084 * r.eof? #=> false after 1 second blocking
1086 * r, w = IO.pipe
1087 * r.eof? # blocks forever
1089 * Note that <code>IO#eof?</code> reads data to a input buffer.
1090 * So <code>IO#sysread</code> doesn't work with <code>IO#eof?</code>.
1093 VALUE
1094 rb_io_eof(VALUE io)
1096 rb_io_t *fptr;
1098 GetOpenFile(io, fptr);
1099 rb_io_check_readable(fptr);
1101 if (READ_DATA_PENDING(fptr)) return Qfalse;
1102 READ_CHECK(fptr);
1103 if (io_fillbuf(fptr) < 0) {
1104 return Qtrue;
1106 return Qfalse;
1110 * call-seq:
1111 * ios.sync => true or false
1113 * Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
1114 * true, all output is immediately flushed to the underlying operating
1115 * system and is not buffered by Ruby internally. See also
1116 * <code>IO#fsync</code>.
1118 * f = File.new("testfile")
1119 * f.sync #=> false
1122 static VALUE
1123 rb_io_sync(VALUE io)
1125 rb_io_t *fptr;
1127 io = GetWriteIO(io);
1128 GetOpenFile(io, fptr);
1129 return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
1133 * call-seq:
1134 * ios.sync = boolean => boolean
1136 * Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
1137 * When sync mode is true, all output is immediately flushed to the
1138 * underlying operating system and is not buffered internally. Returns
1139 * the new state. See also <code>IO#fsync</code>.
1141 * f = File.new("testfile")
1142 * f.sync = true
1144 * <em>(produces no output)</em>
1147 static VALUE
1148 rb_io_set_sync(VALUE io, VALUE mode)
1150 rb_io_t *fptr;
1152 io = GetWriteIO(io);
1153 GetOpenFile(io, fptr);
1154 if (RTEST(mode)) {
1155 fptr->mode |= FMODE_SYNC;
1157 else {
1158 fptr->mode &= ~FMODE_SYNC;
1160 return mode;
1164 * call-seq:
1165 * ios.fsync => 0 or nil
1167 * Immediately writes all buffered data in <em>ios</em> to disk.
1168 * Returns <code>nil</code> if the underlying operating system does not
1169 * support <em>fsync(2)</em>. Note that <code>fsync</code> differs from
1170 * using <code>IO#sync=</code>. The latter ensures that data is flushed
1171 * from Ruby's buffers, but doesn't not guarantee that the underlying
1172 * operating system actually writes it to disk.
1175 static VALUE
1176 rb_io_fsync(VALUE io)
1178 #ifdef HAVE_FSYNC
1179 rb_io_t *fptr;
1181 io = GetWriteIO(io);
1182 GetOpenFile(io, fptr);
1184 io_fflush(fptr);
1185 if (fsync(fptr->fd) < 0)
1186 rb_sys_fail(fptr->path);
1187 return INT2FIX(0);
1188 #else
1189 rb_notimplement();
1190 return Qnil; /* not reached */
1191 #endif
1195 * call-seq:
1196 * ios.fileno => fixnum
1197 * ios.to_i => fixnum
1199 * Returns an integer representing the numeric file descriptor for
1200 * <em>ios</em>.
1202 * $stdin.fileno #=> 0
1203 * $stdout.fileno #=> 1
1206 static VALUE
1207 rb_io_fileno(VALUE io)
1209 rb_io_t *fptr;
1210 int fd;
1212 GetOpenFile(io, fptr);
1213 fd = fptr->fd;
1214 return INT2FIX(fd);
1219 * call-seq:
1220 * ios.pid => fixnum
1222 * Returns the process ID of a child process associated with
1223 * <em>ios</em>. This will be set by <code>IO::popen</code>.
1225 * pipe = IO.popen("-")
1226 * if pipe
1227 * $stderr.puts "In parent, child pid is #{pipe.pid}"
1228 * else
1229 * $stderr.puts "In child, pid is #{$$}"
1230 * end
1232 * <em>produces:</em>
1234 * In child, pid is 26209
1235 * In parent, child pid is 26209
1238 static VALUE
1239 rb_io_pid(VALUE io)
1241 rb_io_t *fptr;
1243 GetOpenFile(io, fptr);
1244 if (!fptr->pid)
1245 return Qnil;
1246 return INT2FIX(fptr->pid);
1251 * call-seq:
1252 * ios.inspect => string
1254 * Return a string describing this IO object.
1257 static VALUE
1258 rb_io_inspect(VALUE obj)
1260 rb_io_t *fptr;
1261 const char *cname;
1262 const char *st = "";
1264 fptr = RFILE(rb_io_taint_check(obj))->fptr;
1265 if (!fptr || !fptr->path) return rb_any_to_s(obj);
1266 cname = rb_obj_classname(obj);
1267 if (fptr->fd < 0) {
1268 st = " (closed)";
1270 return rb_sprintf("#<%s:%s%s>", cname, fptr->path, st);
1274 * call-seq:
1275 * ios.to_io -> ios
1277 * Returns <em>ios</em>.
1280 static VALUE
1281 rb_io_to_io(VALUE io)
1283 return io;
1286 /* reading functions */
1287 static long
1288 read_buffered_data(char *ptr, long len, rb_io_t *fptr)
1290 long n;
1292 n = READ_DATA_PENDING_COUNT(fptr);
1293 if (n <= 0) return 0;
1294 if (n > len) n = len;
1295 MEMMOVE(ptr, fptr->rbuf+fptr->rbuf_off, char, n);
1296 fptr->rbuf_off += n;
1297 fptr->rbuf_len -= n;
1298 return n;
1301 static long
1302 io_fread(VALUE str, long offset, rb_io_t *fptr)
1304 long len = RSTRING_LEN(str) - offset;
1305 long n = len;
1306 int c;
1308 if (READ_DATA_PENDING(fptr) == 0) {
1309 while (n > 0) {
1310 c = rb_read_internal(fptr->fd, RSTRING_PTR(str)+offset, n);
1311 if (c == 0) break;
1312 if (c < 0) {
1313 rb_sys_fail(fptr->path);
1315 offset += c;
1316 if ((n -= c) <= 0) break;
1317 rb_thread_wait_fd(fptr->fd);
1319 return len - n;
1322 while (n > 0) {
1323 c = read_buffered_data(RSTRING_PTR(str)+offset, n, fptr);
1324 if (c > 0) {
1325 offset += c;
1326 if ((n -= c) <= 0) break;
1328 rb_thread_wait_fd(fptr->fd);
1329 rb_io_check_closed(fptr);
1330 if (io_fillbuf(fptr) < 0) {
1331 break;
1334 return len - n;
1337 long
1338 rb_io_fread(char *ptr, long len, FILE *f)
1340 rb_io_t of;
1341 VALUE str;
1342 long n;
1344 of.fd = fileno(f);
1345 of.stdio_file = f;
1346 of.mode = FMODE_READABLE;
1347 str = rb_str_new(ptr, len);
1348 n = io_fread(str, 0, &of);
1349 MEMCPY(ptr, RSTRING_PTR(str), char, n);
1350 return n;
1353 #define SMALLBUF 100
1355 static long
1356 remain_size(rb_io_t *fptr)
1358 struct stat st;
1359 off_t siz = READ_DATA_PENDING_COUNT(fptr);
1360 off_t pos;
1362 if (fstat(fptr->fd, &st) == 0 && S_ISREG(st.st_mode)
1363 #ifdef __BEOS__
1364 && (st.st_dev > 3)
1365 #endif
1368 io_fflush(fptr);
1369 pos = lseek(fptr->fd, 0, SEEK_CUR);
1370 if (st.st_size >= pos && pos >= 0) {
1371 siz += st.st_size - pos;
1372 if (siz > LONG_MAX) {
1373 rb_raise(rb_eIOError, "file too big for single read");
1377 else {
1378 siz += BUFSIZ;
1380 return (long)siz;
1383 static VALUE
1384 io_enc_str(VALUE str, rb_io_t *fptr)
1386 OBJ_TAINT(str);
1387 if (fptr->enc2) {
1388 /* two encodings, so transcode from enc2 to enc */
1389 /* the methods in transcode.c are static, so call indirectly */
1390 str = rb_funcall(str, id_encode, 2,
1391 rb_enc_from_encoding(fptr->enc),
1392 rb_enc_from_encoding(fptr->enc2));
1394 else {
1395 /* just one encoding, so associate it with the string */
1396 rb_enc_associate(str, io_read_encoding(fptr));
1398 return str;
1401 static VALUE
1402 read_all(rb_io_t *fptr, long siz, VALUE str)
1404 long bytes = 0;
1405 long n;
1406 long pos = 0;
1407 rb_encoding *enc = io_read_encoding(fptr);
1408 int cr = fptr->enc2 ? ENC_CODERANGE_BROKEN : 0;
1410 if (siz == 0) siz = BUFSIZ;
1411 if (NIL_P(str)) {
1412 str = rb_str_new(0, siz);
1414 else {
1415 rb_str_resize(str, siz);
1417 for (;;) {
1418 READ_CHECK(fptr);
1419 n = io_fread(str, bytes, fptr);
1420 if (n == 0 && bytes == 0) {
1421 break;
1423 bytes += n;
1424 if (cr != ENC_CODERANGE_BROKEN)
1425 pos = rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
1426 if (bytes < siz) break;
1427 siz += BUFSIZ;
1428 rb_str_resize(str, siz);
1430 if (bytes != siz) rb_str_resize(str, bytes);
1431 str = io_enc_str(str, fptr);
1432 if (!fptr->enc2) {
1433 ENC_CODERANGE_SET(str, cr);
1435 return str;
1438 void
1439 rb_io_set_nonblock(rb_io_t *fptr)
1441 int flags;
1442 #ifdef F_GETFL
1443 flags = fcntl(fptr->fd, F_GETFL);
1444 if (flags == -1) {
1445 rb_sys_fail(fptr->path);
1447 #else
1448 flags = 0;
1449 #endif
1450 if ((flags & O_NONBLOCK) == 0) {
1451 flags |= O_NONBLOCK;
1452 if (fcntl(fptr->fd, F_SETFL, flags) == -1) {
1453 rb_sys_fail(fptr->path);
1458 static VALUE
1459 io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
1461 rb_io_t *fptr;
1462 VALUE length, str;
1463 long n, len;
1465 rb_scan_args(argc, argv, "11", &length, &str);
1467 if ((len = NUM2LONG(length)) < 0) {
1468 rb_raise(rb_eArgError, "negative length %ld given", len);
1471 if (NIL_P(str)) {
1472 str = rb_str_new(0, len);
1474 else {
1475 StringValue(str);
1476 rb_str_modify(str);
1477 rb_str_resize(str, len);
1479 OBJ_TAINT(str);
1481 GetOpenFile(io, fptr);
1482 rb_io_check_readable(fptr);
1484 if (len == 0)
1485 return str;
1487 if (!nonblock)
1488 READ_CHECK(fptr);
1489 if (RSTRING_LEN(str) != len) {
1490 modified:
1491 rb_raise(rb_eRuntimeError, "buffer string modified");
1493 n = read_buffered_data(RSTRING_PTR(str), len, fptr);
1494 if (n <= 0) {
1495 again:
1496 if (RSTRING_LEN(str) != len) goto modified;
1497 if (nonblock) {
1498 rb_io_set_nonblock(fptr);
1500 n = rb_read_internal(fptr->fd, RSTRING_PTR(str), len);
1501 if (n < 0) {
1502 if (!nonblock && rb_io_wait_readable(fptr->fd))
1503 goto again;
1504 rb_sys_fail(fptr->path);
1507 rb_str_resize(str, n);
1509 if (n == 0)
1510 return Qnil;
1511 else
1512 return str;
1516 * call-seq:
1517 * ios.readpartial(maxlen) => string
1518 * ios.readpartial(maxlen, outbuf) => outbuf
1520 * Reads at most <i>maxlen</i> bytes from the I/O stream.
1521 * It blocks only if <em>ios</em> has no data immediately available.
1522 * It doesn't block if some data available.
1523 * If the optional <i>outbuf</i> argument is present,
1524 * it must reference a String, which will receive the data.
1525 * It raises <code>EOFError</code> on end of file.
1527 * readpartial is designed for streams such as pipe, socket, tty, etc.
1528 * It blocks only when no data immediately available.
1529 * This means that it blocks only when following all conditions hold.
1530 * * the buffer in the IO object is empty.
1531 * * the content of the stream is empty.
1532 * * the stream is not reached to EOF.
1534 * When readpartial blocks, it waits data or EOF on the stream.
1535 * If some data is reached, readpartial returns with the data.
1536 * If EOF is reached, readpartial raises EOFError.
1538 * When readpartial doesn't blocks, it returns or raises immediately.
1539 * If the buffer is not empty, it returns the data in the buffer.
1540 * Otherwise if the stream has some content,
1541 * it returns the data in the stream.
1542 * Otherwise if the stream is reached to EOF, it raises EOFError.
1544 * r, w = IO.pipe # buffer pipe content
1545 * w << "abc" # "" "abc".
1546 * r.readpartial(4096) #=> "abc" "" ""
1547 * r.readpartial(4096) # blocks because buffer and pipe is empty.
1549 * r, w = IO.pipe # buffer pipe content
1550 * w << "abc" # "" "abc"
1551 * w.close # "" "abc" EOF
1552 * r.readpartial(4096) #=> "abc" "" EOF
1553 * r.readpartial(4096) # raises EOFError
1555 * r, w = IO.pipe # buffer pipe content
1556 * w << "abc\ndef\n" # "" "abc\ndef\n"
1557 * r.gets #=> "abc\n" "def\n" ""
1558 * w << "ghi\n" # "def\n" "ghi\n"
1559 * r.readpartial(4096) #=> "def\n" "" "ghi\n"
1560 * r.readpartial(4096) #=> "ghi\n" "" ""
1562 * Note that readpartial behaves similar to sysread.
1563 * The differences are:
1564 * * If the buffer is not empty, read from the buffer instead of "sysread for buffered IO (IOError)".
1565 * * It doesn't cause Errno::EAGAIN and Errno::EINTR. When readpartial meets EAGAIN and EINTR by read system call, readpartial retry the system call.
1567 * The later means that readpartial is nonblocking-flag insensitive.
1568 * It blocks on the situation IO#sysread causes Errno::EAGAIN as if the fd is blocking mode.
1572 static VALUE
1573 io_readpartial(int argc, VALUE *argv, VALUE io)
1575 VALUE ret;
1577 ret = io_getpartial(argc, argv, io, 0);
1578 if (NIL_P(ret))
1579 rb_eof_error();
1580 else
1581 return ret;
1585 * call-seq:
1586 * ios.read_nonblock(maxlen) => string
1587 * ios.read_nonblock(maxlen, outbuf) => outbuf
1589 * Reads at most <i>maxlen</i> bytes from <em>ios</em> using
1590 * read(2) system call after O_NONBLOCK is set for
1591 * the underlying file descriptor.
1593 * If the optional <i>outbuf</i> argument is present,
1594 * it must reference a String, which will receive the data.
1596 * read_nonblock just calls read(2).
1597 * It causes all errors read(2) causes: EAGAIN, EINTR, etc.
1598 * The caller should care such errors.
1600 * read_nonblock causes EOFError on EOF.
1602 * If the read buffer is not empty,
1603 * read_nonblock reads from the buffer like readpartial.
1604 * In this case, read(2) is not called.
1608 static VALUE
1609 io_read_nonblock(int argc, VALUE *argv, VALUE io)
1611 VALUE ret;
1613 ret = io_getpartial(argc, argv, io, 1);
1614 if (NIL_P(ret))
1615 rb_eof_error();
1616 else
1617 return ret;
1621 * call-seq:
1622 * ios.write_nonblock(string) => integer
1624 * Writes the given string to <em>ios</em> using
1625 * write(2) system call after O_NONBLOCK is set for
1626 * the underlying file descriptor.
1628 * write_nonblock just calls write(2).
1629 * It causes all errors write(2) causes: EAGAIN, EINTR, etc.
1630 * The result may also be smaller than string.length (partial write).
1631 * The caller should care such errors and partial write.
1633 * If the write buffer is not empty, it is flushed at first.
1637 static VALUE
1638 rb_io_write_nonblock(VALUE io, VALUE str)
1640 rb_io_t *fptr;
1641 long n;
1643 rb_secure(4);
1644 if (TYPE(str) != T_STRING)
1645 str = rb_obj_as_string(str);
1647 io = GetWriteIO(io);
1648 GetOpenFile(io, fptr);
1649 rb_io_check_writable(fptr);
1651 io_fflush(fptr);
1653 rb_io_set_nonblock(fptr);
1654 n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
1656 if (n == -1) rb_sys_fail(fptr->path);
1658 return LONG2FIX(n);
1662 * call-seq:
1663 * ios.read([length [, buffer]]) => string, buffer, or nil
1665 * Reads at most <i>length</i> bytes from the I/O stream, or to the
1666 * end of file if <i>length</i> is omitted or is <code>nil</code>.
1667 * <i>length</i> must be a non-negative integer or nil.
1668 * If the optional <i>buffer</i> argument is present, it must reference
1669 * a String, which will receive the data.
1671 * At end of file, it returns <code>nil</code> or <code>""</code>
1672 * depend on <i>length</i>.
1673 * <code><i>ios</i>.read()</code> and
1674 * <code><i>ios</i>.read(nil)</code> returns <code>""</code>.
1675 * <code><i>ios</i>.read(<i>positive-integer</i>)</code> returns nil.
1677 * <code><i>ios</i>.read(0)</code> returns <code>""</code>.
1679 * f = File.new("testfile")
1680 * f.read(16) #=> "This is line one"
1683 static VALUE
1684 io_read(int argc, VALUE *argv, VALUE io)
1686 rb_io_t *fptr;
1687 long n, len;
1688 VALUE length, str;
1690 rb_scan_args(argc, argv, "02", &length, &str);
1692 if (NIL_P(length)) {
1693 if (!NIL_P(str)) StringValue(str);
1694 GetOpenFile(io, fptr);
1695 rb_io_check_readable(fptr);
1696 return read_all(fptr, remain_size(fptr), str);
1698 len = NUM2LONG(length);
1699 if (len < 0) {
1700 rb_raise(rb_eArgError, "negative length %ld given", len);
1703 if (NIL_P(str)) {
1704 str = rb_str_new(0, len);
1706 else {
1707 StringValue(str);
1708 rb_str_modify(str);
1709 rb_str_resize(str,len);
1712 GetOpenFile(io, fptr);
1713 rb_io_check_readable(fptr);
1714 if (len == 0) return str;
1716 READ_CHECK(fptr);
1717 if (RSTRING_LEN(str) != len) {
1718 rb_raise(rb_eRuntimeError, "buffer string modified");
1720 n = io_fread(str, 0, fptr);
1721 if (n == 0) {
1722 if (fptr->fd < 0) return Qnil;
1723 rb_str_resize(str, 0);
1724 return Qnil;
1726 rb_str_resize(str, n);
1728 return str;
1731 static void
1732 rscheck(const char *rsptr, long rslen, VALUE rs)
1734 if (!rs) return;
1735 if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
1736 rb_raise(rb_eRuntimeError, "rs modified");
1739 static int
1740 appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
1742 VALUE str = *strp;
1743 int c = EOF;
1744 long limit = *lp;
1746 do {
1747 long pending = READ_DATA_PENDING_COUNT(fptr);
1748 if (pending > 0) {
1749 const char *p = READ_DATA_PENDING_PTR(fptr);
1750 const char *e;
1751 long last = 0, len = (c != EOF);
1752 rb_encoding *enc = io_read_encoding(fptr);
1754 if (limit > 0 && pending > limit) pending = limit;
1755 e = memchr(p, delim, pending);
1756 if (e) pending = e - p + 1;
1757 len += pending;
1758 if (!NIL_P(str)) {
1759 last = RSTRING_LEN(str);
1760 rb_str_resize(str, last + len);
1762 else {
1763 *strp = str = rb_str_buf_new(len);
1764 rb_str_set_len(str, len);
1766 if (c != EOF) {
1767 RSTRING_PTR(str)[last++] = c;
1769 if (limit > 0 && limit == pending) {
1770 char *p = fptr->rbuf+fptr->rbuf_off;
1771 char *pp = p + limit - 1;
1772 char *pl = rb_enc_left_char_head(p, pp, enc);
1774 if (pl < pp) {
1775 int diff = pp - pl;
1776 pending -= diff;
1777 limit = pending;
1778 rb_str_set_len(str, RSTRING_LEN(str)-diff);
1781 read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
1782 limit -= pending;
1783 *lp = limit;
1784 if (limit == 0) return RSTRING_PTR(str)[RSTRING_LEN(str)-1];
1785 if (e) return delim;
1787 else if (c != EOF) {
1788 if (!NIL_P(str)) {
1789 char ch = c;
1790 rb_str_buf_cat(str, &ch, 1);
1792 else {
1793 *strp = str = rb_str_buf_new(1);
1794 rb_str_resize(str, 1);
1795 RSTRING_PTR(str)[0] = c;
1798 rb_thread_wait_fd(fptr->fd);
1799 rb_io_check_closed(fptr);
1800 if (io_fillbuf(fptr) < 0) {
1801 *lp = limit;
1802 return c;
1804 } while (c != delim);
1807 char ch = c;
1808 if (!NIL_P(str)) {
1809 rb_str_cat(str, &ch, 1);
1811 else {
1812 *strp = str = rb_str_new(&ch, 1);
1816 return c;
1819 static inline int
1820 swallow(rb_io_t *fptr, int term)
1822 do {
1823 long cnt;
1824 while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
1825 char buf[1024];
1826 const char *p = READ_DATA_PENDING_PTR(fptr);
1827 int i;
1828 if (cnt > sizeof buf) cnt = sizeof buf;
1829 if (*p != term) return Qtrue;
1830 i = cnt;
1831 while (--i && *++p == term);
1832 if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
1833 rb_sys_fail(fptr->path);
1835 rb_thread_wait_fd(fptr->fd);
1836 rb_io_check_closed(fptr);
1837 } while (io_fillbuf(fptr) == 0);
1838 return Qfalse;
1841 static VALUE
1842 rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc)
1844 VALUE str = Qnil;
1845 int len = 0;
1846 long pos = 0;
1847 int cr = fptr->enc2 ? ENC_CODERANGE_BROKEN : 0;
1849 for (;;) {
1850 long pending = READ_DATA_PENDING_COUNT(fptr);
1852 if (pending > 0) {
1853 const char *p = READ_DATA_PENDING_PTR(fptr);
1854 const char *e;
1856 e = memchr(p, '\n', pending);
1857 if (e) {
1858 pending = e - p + 1;
1860 if (NIL_P(str)) {
1861 str = rb_str_new(p, pending);
1862 fptr->rbuf_off += pending;
1863 fptr->rbuf_len -= pending;
1865 else {
1866 rb_str_resize(str, len + pending);
1867 read_buffered_data(RSTRING_PTR(str)+len, pending, fptr);
1869 len += pending;
1870 if (cr != ENC_CODERANGE_BROKEN)
1871 pos = rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
1872 if (e) break;
1874 rb_thread_wait_fd(fptr->fd);
1875 rb_io_check_closed(fptr);
1876 if (io_fillbuf(fptr) < 0) {
1877 if (NIL_P(str)) return Qnil;
1878 break;
1882 str = io_enc_str(str, fptr);
1883 if (!fptr->enc2) ENC_CODERANGE_SET(str, cr);
1884 fptr->lineno++;
1885 ARGF.lineno = INT2FIX(fptr->lineno);
1886 return str;
1889 static void
1890 prepare_getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit, VALUE io)
1892 VALUE lim, rs;
1893 rb_io_t *fptr;
1895 if (argc == 0) {
1896 rs = rb_rs;
1897 lim = Qnil;
1899 else {
1900 rb_scan_args(argc, argv, "11", &rs, &lim);
1901 if (!NIL_P(lim)) {
1902 StringValue(rs);
1904 else if (!NIL_P(rs) && TYPE(rs) != T_STRING) {
1905 VALUE tmp = rb_check_string_type(rs);
1907 if (NIL_P(tmp)) {
1908 lim = rs;
1909 rs = rb_rs;
1911 else {
1912 rs = tmp;
1916 if (!NIL_P(rs)) {
1917 rb_encoding *enc_rs, *enc_io;
1919 GetOpenFile(io, fptr);
1920 enc_rs = rb_enc_get(rs);
1921 enc_io = io_read_encoding(fptr);
1922 if (enc_io != enc_rs &&
1923 (rb_enc_str_coderange(rs) != ENC_CODERANGE_7BIT ||
1924 !rb_enc_asciicompat(enc_io))) {
1925 if (rs == rb_default_rs) {
1926 rs = rb_enc_str_new(0, 0, enc_io);
1927 rb_str_buf_cat_ascii(rs, "\n");
1929 else {
1930 rb_raise(rb_eArgError, "encoding mismatch: %s IO with %s RS",
1931 rb_enc_name(enc_io),
1932 rb_enc_name(enc_rs));
1935 if (fptr->enc2) {
1936 VALUE rs2;
1937 rs2 = rb_funcall(rs, id_encode, 2,
1938 rb_enc_from_encoding(fptr->enc2),
1939 rb_enc_from_encoding(fptr->enc));
1940 if (!RTEST(rb_str_equal(rs, rs2))) {
1941 rs = rs2;
1945 *rsp = rs;
1946 *limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
1949 static VALUE
1950 rb_io_getline_1(VALUE rs, long limit, VALUE io)
1952 VALUE str = Qnil;
1953 rb_io_t *fptr;
1954 int nolimit = 0;
1955 rb_encoding *enc;
1957 GetOpenFile(io, fptr);
1958 rb_io_check_readable(fptr);
1959 if (rb_enc_dummy_p(io_input_encoding(fptr)) && rs != rb_default_rs) {
1960 rb_raise(rb_eNotImpError, "gets with delimiter against dummy encoding is not currently supported");
1962 if (NIL_P(rs)) {
1963 str = read_all(fptr, 0, Qnil);
1964 if (RSTRING_LEN(str) == 0) return Qnil;
1966 else if (limit == 0) {
1967 return rb_enc_str_new(0, 0, io_read_encoding(fptr));
1969 else if (rs == rb_default_rs && limit < 0 &&
1970 rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
1971 return rb_io_getline_fast(fptr, enc);
1973 else {
1974 int c, newline;
1975 const char *rsptr;
1976 long rslen;
1977 int rspara = 0;
1979 rslen = RSTRING_LEN(rs);
1980 if (rslen == 0) {
1981 rsptr = "\n\n";
1982 rslen = 2;
1983 rspara = 1;
1984 swallow(fptr, '\n');
1985 rs = 0;
1987 else {
1988 rsptr = RSTRING_PTR(rs);
1990 newline = rsptr[rslen - 1];
1992 enc = io_input_encoding(fptr);
1993 while ((c = appendline(fptr, newline, &str, &limit)) != EOF) {
1994 if (c == newline) {
1995 const char *s, *p, *pp;
1997 if (RSTRING_LEN(str) < rslen) continue;
1998 s = RSTRING_PTR(str);
1999 p = s + RSTRING_LEN(str) - rslen;
2000 pp = rb_enc_left_char_head(s, p, enc);
2001 if (pp != p) continue;
2002 if (!rspara) rscheck(rsptr, rslen, rs);
2003 if (memcmp(p, rsptr, rslen) == 0) break;
2005 if (limit == 0) {
2006 nolimit = 1;
2007 break;
2011 if (rspara) {
2012 if (c != EOF) {
2013 swallow(fptr, '\n');
2016 if (!NIL_P(str)) str = io_enc_str(str, fptr);
2019 if (!NIL_P(str)) {
2020 if (!nolimit) {
2021 fptr->lineno++;
2022 ARGF.lineno = INT2FIX(fptr->lineno);
2026 return str;
2029 static VALUE
2030 rb_io_getline(int argc, VALUE *argv, VALUE io)
2032 VALUE rs;
2033 long limit;
2035 prepare_getline_args(argc, argv, &rs, &limit, io);
2036 return rb_io_getline_1(rs, limit, io);
2039 VALUE
2040 rb_io_gets(VALUE io)
2042 rb_io_t *fptr;
2044 GetOpenFile(io, fptr);
2045 rb_io_check_readable(fptr);
2046 return rb_io_getline_fast(fptr, io_read_encoding(fptr));
2050 * call-seq:
2051 * ios.gets(sep=$/) => string or nil
2052 * ios.gets(limit) => string or nil
2053 * ios.gets(sep, limit) => string or nil
2055 * Reads the next ``line'' from the I/O stream; lines are separated by
2056 * <i>sep</i>. A separator of <code>nil</code> reads the entire
2057 * contents, and a zero-length separator reads the input a paragraph at
2058 * a time (two successive newlines in the input separate paragraphs).
2059 * The stream must be opened for reading or an <code>IOError</code>
2060 * will be raised. The line read in will be returned and also assigned
2061 * to <code>$_</code>. Returns <code>nil</code> if called at end of
2062 * file. If the first argument is an integer, or optional second
2063 * argument is given, the returning string would not be longer than the
2064 * given value.
2066 * File.new("testfile").gets #=> "This is line one\n"
2067 * $_ #=> "This is line one\n"
2070 static VALUE
2071 rb_io_gets_m(int argc, VALUE *argv, VALUE io)
2073 VALUE str;
2075 str = rb_io_getline(argc, argv, io);
2076 rb_lastline_set(str);
2078 return str;
2082 * call-seq:
2083 * ios.lineno => integer
2085 * Returns the current line number in <em>ios</em>. The stream must be
2086 * opened for reading. <code>lineno</code> counts the number of times
2087 * <code>gets</code> is called, rather than the number of newlines
2088 * encountered. The two values will differ if <code>gets</code> is
2089 * called with a separator other than newline. See also the
2090 * <code>$.</code> variable.
2092 * f = File.new("testfile")
2093 * f.lineno #=> 0
2094 * f.gets #=> "This is line one\n"
2095 * f.lineno #=> 1
2096 * f.gets #=> "This is line two\n"
2097 * f.lineno #=> 2
2100 static VALUE
2101 rb_io_lineno(VALUE io)
2103 rb_io_t *fptr;
2105 GetOpenFile(io, fptr);
2106 rb_io_check_readable(fptr);
2107 return INT2NUM(fptr->lineno);
2111 * call-seq:
2112 * ios.lineno = integer => integer
2114 * Manually sets the current line number to the given value.
2115 * <code>$.</code> is updated only on the next read.
2117 * f = File.new("testfile")
2118 * f.gets #=> "This is line one\n"
2119 * $. #=> 1
2120 * f.lineno = 1000
2121 * f.lineno #=> 1000
2122 * $. #=> 1 # lineno of last read
2123 * f.gets #=> "This is line two\n"
2124 * $. #=> 1001 # lineno of last read
2127 static VALUE
2128 rb_io_set_lineno(VALUE io, VALUE lineno)
2130 rb_io_t *fptr;
2132 GetOpenFile(io, fptr);
2133 rb_io_check_readable(fptr);
2134 fptr->lineno = NUM2INT(lineno);
2135 return lineno;
2139 * call-seq:
2140 * ios.readline(sep=$/) => string
2141 * ios.readline(limit) => string
2142 * ios.readline(sep, limit) => string
2144 * Reads a line as with <code>IO#gets</code>, but raises an
2145 * <code>EOFError</code> on end of file.
2148 static VALUE
2149 rb_io_readline(int argc, VALUE *argv, VALUE io)
2151 VALUE line = rb_io_gets_m(argc, argv, io);
2153 if (NIL_P(line)) {
2154 rb_eof_error();
2156 return line;
2160 * call-seq:
2161 * ios.readlines(sep=$/) => array
2162 * ios.readlines(limit) => array
2163 * ios.readlines(sep, limit) => array
2165 * Reads all of the lines in <em>ios</em>, and returns them in
2166 * <i>anArray</i>. Lines are separated by the optional <i>sep</i>. If
2167 * <i>sep</i> is <code>nil</code>, the rest of the stream is returned
2168 * as a single record. If the first argument is an integer, or
2169 * optional second argument is given, the returning string would not be
2170 * longer than the given value. The stream must be opened for reading
2171 * or an <code>IOError</code> will be raised.
2173 * f = File.new("testfile")
2174 * f.readlines[0] #=> "This is line one\n"
2177 static VALUE
2178 rb_io_readlines(int argc, VALUE *argv, VALUE io)
2180 VALUE line, ary, rs;
2181 long limit;
2183 prepare_getline_args(argc, argv, &rs, &limit, io);
2184 ary = rb_ary_new();
2185 while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) {
2186 rb_ary_push(ary, line);
2188 return ary;
2192 * call-seq:
2193 * ios.each(sep=$/) {|line| block } => ios
2194 * ios.each(limit) {|line| block } => ios
2195 * ios.each(sep,limit) {|line| block } => ios
2196 * ios.each_line(sep=$/) {|line| block } => ios
2197 * ios.each_line(limit) {|line| block } => ios
2198 * ios.each_line(sep,limit) {|line| block } => ios
2200 * Executes the block for every line in <em>ios</em>, where lines are
2201 * separated by <i>sep</i>. <em>ios</em> must be opened for
2202 * reading or an <code>IOError</code> will be raised.
2204 * f = File.new("testfile")
2205 * f.each {|line| puts "#{f.lineno}: #{line}" }
2207 * <em>produces:</em>
2209 * 1: This is line one
2210 * 2: This is line two
2211 * 3: This is line three
2212 * 4: And so on...
2215 static VALUE
2216 rb_io_each_line(int argc, VALUE *argv, VALUE io)
2218 VALUE str, rs;
2219 long limit;
2221 RETURN_ENUMERATOR(io, argc, argv);
2222 prepare_getline_args(argc, argv, &rs, &limit, io);
2223 while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) {
2224 rb_yield(str);
2226 return io;
2230 * call-seq:
2231 * ios.each_byte {|byte| block } => ios
2233 * Calls the given block once for each byte (0..255) in <em>ios</em>,
2234 * passing the byte as an argument. The stream must be opened for
2235 * reading or an <code>IOError</code> will be raised.
2237 * f = File.new("testfile")
2238 * checksum = 0
2239 * f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
2240 * checksum #=> 12
2243 static VALUE
2244 rb_io_each_byte(VALUE io)
2246 rb_io_t *fptr;
2247 char *p, *e;
2249 RETURN_ENUMERATOR(io, 0, 0);
2250 GetOpenFile(io, fptr);
2252 for (;;) {
2253 p = fptr->rbuf+fptr->rbuf_off;
2254 e = p + fptr->rbuf_len;
2255 while (p < e) {
2256 fptr->rbuf_off++;
2257 fptr->rbuf_len--;
2258 rb_yield(INT2FIX(*p & 0xff));
2259 p++;
2260 errno = 0;
2262 rb_io_check_readable(fptr);
2263 READ_CHECK(fptr);
2264 if (io_fillbuf(fptr) < 0) {
2265 break;
2268 return io;
2271 static VALUE
2272 io_getc(rb_io_t *fptr, rb_encoding *enc)
2274 int r, n, cr = 0;
2275 VALUE str;
2277 if (rb_enc_dummy_p(enc)) {
2278 rb_raise(rb_eNotImpError, "getc against dummy encoding is not currently supported");
2281 if (io_fillbuf(fptr) < 0) {
2282 return Qnil;
2284 if (rb_enc_asciicompat(enc) && ISASCII(fptr->rbuf[fptr->rbuf_off])) {
2285 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, 1);
2286 fptr->rbuf_off += 1;
2287 fptr->rbuf_len -= 1;
2288 cr = ENC_CODERANGE_7BIT;
2290 else {
2291 r = rb_enc_precise_mbclen(fptr->rbuf+fptr->rbuf_off, fptr->rbuf+fptr->rbuf_off+fptr->rbuf_len, enc);
2292 if (MBCLEN_CHARFOUND_P(r) &&
2293 (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf_len) {
2294 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, n);
2295 fptr->rbuf_off += n;
2296 fptr->rbuf_len -= n;
2297 cr = ENC_CODERANGE_VALID;
2299 else if (MBCLEN_NEEDMORE_P(r)) {
2300 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, fptr->rbuf_len);
2301 fptr->rbuf_len = 0;
2302 getc_needmore:
2303 if (io_fillbuf(fptr) != -1) {
2304 rb_str_cat(str, fptr->rbuf+fptr->rbuf_off, 1);
2305 fptr->rbuf_off++;
2306 fptr->rbuf_len--;
2307 r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
2308 if (MBCLEN_NEEDMORE_P(r)) {
2309 goto getc_needmore;
2311 else if (MBCLEN_CHARFOUND_P(r)) {
2312 cr = ENC_CODERANGE_VALID;
2316 else {
2317 str = rb_str_new(fptr->rbuf+fptr->rbuf_off, 1);
2318 fptr->rbuf_off++;
2319 fptr->rbuf_len--;
2322 if (!cr) cr = ENC_CODERANGE_BROKEN;
2323 str = io_enc_str(str, fptr);
2324 if (!fptr->enc2) {
2325 ENC_CODERANGE_SET(str, cr);
2327 return str;
2331 * call-seq:
2332 * ios.each_char {|c| block } => ios
2334 * Calls the given block once for each character in <em>ios</em>,
2335 * passing the character as an argument. The stream must be opened for
2336 * reading or an <code>IOError</code> will be raised.
2338 * f = File.new("testfile")
2339 * f.each_char {|c| print c, ' ' } #=> #<File:testfile>
2342 static VALUE
2343 rb_io_each_char(VALUE io)
2345 rb_io_t *fptr;
2346 rb_encoding *enc;
2347 VALUE c;
2349 RETURN_ENUMERATOR(io, 0, 0);
2350 GetOpenFile(io, fptr);
2351 rb_io_check_readable(fptr);
2353 enc = io_input_encoding(fptr);
2354 READ_CHECK(fptr);
2355 while (!NIL_P(c = io_getc(fptr, enc))) {
2356 rb_yield(c);
2358 return io;
2364 * call-seq:
2365 * ios.lines(sep=$/) => anEnumerator
2366 * ios.lines(limit) => anEnumerator
2367 * ios.lines(sep, limit) => anEnumerator
2369 * Returns an enumerator that gives each line in <em>ios</em>.
2370 * The stream must be opened for reading or an <code>IOError</code>
2371 * will be raised.
2373 * f = File.new("testfile")
2374 * f.lines.to_a #=> ["foo\n", "bar\n"]
2375 * f.rewind
2376 * f.lines.sort #=> ["bar\n", "foo\n"]
2379 static VALUE
2380 rb_io_lines(int argc, VALUE *argv, VALUE io)
2382 return rb_enumeratorize(io, ID2SYM(rb_intern("each_line")), argc, argv);
2386 * call-seq:
2387 * ios.bytes => anEnumerator
2389 * Returns an enumerator that gives each byte (0..255) in <em>ios</em>.
2390 * The stream must be opened for reading or an <code>IOError</code>
2391 * will be raised.
2393 * f = File.new("testfile")
2394 * f.bytes.to_a #=> [104, 101, 108, 108, 111]
2395 * f.rewind
2396 * f.bytes.sort #=> [101, 104, 108, 108, 111]
2399 static VALUE
2400 rb_io_bytes(VALUE io)
2402 return rb_enumeratorize(io, ID2SYM(rb_intern("each_byte")), 0, 0);
2406 * call-seq:
2407 * ios.chars => anEnumerator
2409 * Returns an enumerator that gives each character in <em>ios</em>.
2410 * The stream must be opened for reading or an <code>IOError</code>
2411 * will be raised.
2413 * f = File.new("testfile")
2414 * f.chars.to_a #=> ["h", "e", "l", "l", "o"]
2415 * f.rewind
2416 * f.chars.sort #=> ["e", "h", "l", "l", "o"]
2419 static VALUE
2420 rb_io_chars(VALUE io)
2422 return rb_enumeratorize(io, ID2SYM(rb_intern("each_char")), 0, 0);
2426 * call-seq:
2427 * ios.getc => fixnum or nil
2429 * Reads a one-character string from <em>ios</em>. Returns
2430 * <code>nil</code> if called at end of file.
2432 * f = File.new("testfile")
2433 * f.getc #=> "8"
2434 * f.getc #=> "1"
2437 static VALUE
2438 rb_io_getc(VALUE io)
2440 rb_io_t *fptr;
2441 rb_encoding *enc;
2443 GetOpenFile(io, fptr);
2444 rb_io_check_readable(fptr);
2446 enc = io_input_encoding(fptr);
2447 READ_CHECK(fptr);
2448 return io_getc(fptr, enc);
2451 rb_getc(FILE *f)
2453 int c;
2455 rb_read_check(f);
2456 TRAP_BEG;
2457 c = getc(f);
2458 TRAP_END;
2460 return c;
2464 * call-seq:
2465 * ios.readchar => string
2467 * Reads a one-character string from <em>ios</em>. Raises an
2468 * <code>EOFError</code> on end of file.
2470 * f = File.new("testfile")
2471 * f.readchar #=> "8"
2472 * f.readchar #=> "1"
2475 static VALUE
2476 rb_io_readchar(VALUE io)
2478 VALUE c = rb_io_getc(io);
2480 if (NIL_P(c)) {
2481 rb_eof_error();
2483 return c;
2487 * call-seq:
2488 * ios.getbyte => fixnum or nil
2490 * Gets the next 8-bit byte (0..255) from <em>ios</em>. Returns
2491 * <code>nil</code> if called at end of file.
2493 * f = File.new("testfile")
2494 * f.getbyte #=> 84
2495 * f.getbyte #=> 104
2498 VALUE
2499 rb_io_getbyte(VALUE io)
2501 rb_io_t *fptr;
2502 int c;
2504 GetOpenFile(io, fptr);
2505 rb_io_check_readable(fptr);
2506 READ_CHECK(fptr);
2507 if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && TYPE(rb_stdout) == T_FILE) {
2508 rb_io_t *ofp;
2509 GetOpenFile(rb_stdout, ofp);
2510 if (ofp->mode & FMODE_TTY) {
2511 rb_io_flush(rb_stdout);
2514 if (io_fillbuf(fptr) < 0) {
2515 return Qnil;
2517 fptr->rbuf_off++;
2518 fptr->rbuf_len--;
2519 c = (unsigned char)fptr->rbuf[fptr->rbuf_off-1];
2520 return INT2FIX(c & 0xff);
2524 * call-seq:
2525 * ios.readbyte => fixnum
2527 * Reads a character as with <code>IO#getc</code>, but raises an
2528 * <code>EOFError</code> on end of file.
2531 static VALUE
2532 rb_io_readbyte(VALUE io)
2534 VALUE c = rb_io_getbyte(io);
2536 if (NIL_P(c)) {
2537 rb_eof_error();
2539 return c;
2543 * call-seq:
2544 * ios.ungetc(string) => nil
2546 * Pushes back one character (passed as a parameter) onto <em>ios</em>,
2547 * such that a subsequent buffered read will return it. Only one character
2548 * may be pushed back before a subsequent read operation (that is,
2549 * you will be able to read only the last of several characters that have been pushed
2550 * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
2552 * f = File.new("testfile") #=> #<File:testfile>
2553 * c = f.getc #=> "8"
2554 * f.ungetc(c) #=> nil
2555 * f.getc #=> "8"
2558 VALUE
2559 rb_io_ungetc(VALUE io, VALUE c)
2561 rb_io_t *fptr;
2563 GetOpenFile(io, fptr);
2564 rb_io_check_readable(fptr);
2565 if (NIL_P(c)) return Qnil;
2566 if (FIXNUM_P(c)) {
2567 int cc = FIX2INT(c);
2568 rb_encoding *enc = io_read_encoding(fptr);
2569 char buf[16];
2571 c = rb_str_new(buf, rb_enc_mbcput(cc, buf, enc));
2573 else {
2574 SafeStringValue(c);
2576 io_ungetc(c, fptr);
2577 return Qnil;
2581 * call-seq:
2582 * ios.isatty => true or false
2583 * ios.tty? => true or false
2585 * Returns <code>true</code> if <em>ios</em> is associated with a
2586 * terminal device (tty), <code>false</code> otherwise.
2588 * File.new("testfile").isatty #=> false
2589 * File.new("/dev/tty").isatty #=> true
2592 static VALUE
2593 rb_io_isatty(VALUE io)
2595 rb_io_t *fptr;
2597 GetOpenFile(io, fptr);
2598 if (isatty(fptr->fd) == 0)
2599 return Qfalse;
2600 return Qtrue;
2604 * call-seq:
2605 * ios.close_on_exec? => true or false
2607 * Returns <code>true</code> if <em>ios</em> will be closed on exec.
2609 * f = open("/dev/null")
2610 * f.close_on_exec? #=> false
2611 * f.close_on_exec = true
2612 * f.close_on_exec? #=> true
2613 * f.close_on_exec = false
2614 * f.close_on_exec? #=> false
2617 static VALUE
2618 rb_io_close_on_exec_p(VALUE io)
2620 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
2621 rb_io_t *fptr;
2622 VALUE write_io;
2623 int fd, ret;
2625 write_io = GetWriteIO(io);
2626 if (io != write_io) {
2627 GetOpenFile(write_io, fptr);
2628 if (fptr && 0 <= (fd = fptr->fd)) {
2629 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2630 if (!(ret & FD_CLOEXEC)) return Qfalse;
2634 GetOpenFile(io, fptr);
2635 if (fptr && 0 <= (fd = fptr->fd)) {
2636 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2637 if (!(ret & FD_CLOEXEC)) return Qfalse;
2639 return Qtrue;
2640 #else
2641 rb_notimplement();
2642 return Qnil; /* not reached */
2643 #endif
2647 * call-seq:
2648 * ios.close_on_exec = bool => true or false
2650 * Sets a close-on-exec flag.
2652 * f = open("/dev/null")
2653 * f.close_on_exec = true
2654 * system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
2655 * f.closed? #=> false
2658 static VALUE
2659 rb_io_set_close_on_exec(VALUE io, VALUE arg)
2661 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
2662 int flag = RTEST(arg) ? FD_CLOEXEC : 0;
2663 rb_io_t *fptr;
2664 VALUE write_io;
2665 int fd, ret;
2667 write_io = GetWriteIO(io);
2668 if (io != write_io) {
2669 GetOpenFile(write_io, fptr);
2670 if (fptr && 0 <= (fd = fptr->fd)) {
2671 if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2672 if ((ret & FD_CLOEXEC) != flag) {
2673 ret = (ret & ~FD_CLOEXEC) | flag;
2674 ret = fcntl(fd, F_SETFD, ret);
2675 if (ret == -1) rb_sys_fail(fptr->path);
2681 GetOpenFile(io, fptr);
2682 if (fptr && 0 <= (fd = fptr->fd)) {
2683 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
2684 if ((ret & FD_CLOEXEC) != flag) {
2685 ret = (ret & ~FD_CLOEXEC) | flag;
2686 ret = fcntl(fd, F_SETFD, ret);
2687 if (ret == -1) rb_sys_fail(fptr->path);
2690 #else
2691 rb_notimplement();
2692 #endif
2693 return Qnil;
2696 #define FMODE_PREP (1<<16)
2697 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
2698 #define PREP_STDIO_NAME(f) ((f)->path)
2700 static void
2701 fptr_finalize(rb_io_t *fptr, int noraise)
2703 int ebadf = 0;
2704 if (fptr->wbuf_len) {
2705 io_fflush(fptr);
2707 if (IS_PREP_STDIO(fptr) ||
2708 fptr->fd <= 2) {
2709 return;
2711 if (fptr->stdio_file) {
2712 if (fclose(fptr->stdio_file) < 0 && !noraise) {
2713 /* fptr->stdio_file is deallocated anyway */
2714 fptr->stdio_file = 0;
2715 fptr->fd = -1;
2716 rb_sys_fail(fptr->path);
2719 else if (0 <= fptr->fd) {
2720 if (close(fptr->fd) < 0 && !noraise) {
2721 if (errno != EBADF) {
2722 /* fptr->fd is still not closed */
2723 rb_sys_fail(fptr->path);
2725 else {
2726 /* fptr->fd is already closed. */
2727 ebadf = 1;
2731 fptr->fd = -1;
2732 fptr->stdio_file = 0;
2733 fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
2734 if (ebadf) {
2735 rb_sys_fail(fptr->path);
2739 static void
2740 rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
2742 if (fptr->finalize) {
2743 (*fptr->finalize)(fptr, noraise);
2745 else {
2746 fptr_finalize(fptr, noraise);
2751 rb_io_fptr_finalize(rb_io_t *fptr)
2753 if (!fptr) return 0;
2754 if (fptr->refcnt <= 0 || --fptr->refcnt) return 0;
2755 if (fptr->path) {
2756 free(fptr->path);
2757 fptr->path = 0;
2759 if (0 <= fptr->fd)
2760 rb_io_fptr_cleanup(fptr, Qtrue);
2761 if (fptr->rbuf) {
2762 free(fptr->rbuf);
2763 fptr->rbuf = 0;
2765 if (fptr->wbuf) {
2766 free(fptr->wbuf);
2767 fptr->wbuf = 0;
2769 free(fptr);
2770 return 1;
2773 VALUE
2774 rb_io_close(VALUE io)
2776 rb_io_t *fptr;
2777 int fd;
2778 VALUE write_io;
2779 rb_io_t *write_fptr;
2781 write_io = GetWriteIO(io);
2782 if (io != write_io) {
2783 write_fptr = RFILE(write_io)->fptr;
2784 if (write_fptr && 0 <= write_fptr->fd) {
2785 rb_io_fptr_cleanup(write_fptr, Qtrue);
2789 fptr = RFILE(io)->fptr;
2790 if (!fptr) return Qnil;
2791 if (fptr->fd < 0) return Qnil;
2793 fd = fptr->fd;
2794 rb_io_fptr_cleanup(fptr, Qfalse);
2795 rb_thread_fd_close(fd);
2797 if (fptr->pid) {
2798 rb_syswait(fptr->pid);
2799 fptr->pid = 0;
2802 return Qnil;
2806 * call-seq:
2807 * ios.close => nil
2809 * Closes <em>ios</em> and flushes any pending writes to the operating
2810 * system. The stream is unavailable for any further data operations;
2811 * an <code>IOError</code> is raised if such an attempt is made. I/O
2812 * streams are automatically closed when they are claimed by the
2813 * garbage collector.
2815 * If <em>ios</em> is opened by <code>IO.popen</code>,
2816 * <code>close</code> sets <code>$?</code>.
2819 static VALUE
2820 rb_io_close_m(VALUE io)
2822 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
2823 rb_raise(rb_eSecurityError, "Insecure: can't close");
2825 rb_io_check_closed(RFILE(io)->fptr);
2826 rb_io_close(io);
2827 return Qnil;
2830 static VALUE
2831 io_call_close(VALUE io)
2833 return rb_funcall(io, rb_intern("close"), 0, 0);
2836 static VALUE
2837 io_close(VALUE io)
2839 return rb_rescue(io_call_close, io, 0, 0);
2843 * call-seq:
2844 * ios.closed? => true or false
2846 * Returns <code>true</code> if <em>ios</em> is completely closed (for
2847 * duplex streams, both reader and writer), <code>false</code>
2848 * otherwise.
2850 * f = File.new("testfile")
2851 * f.close #=> nil
2852 * f.closed? #=> true
2853 * f = IO.popen("/bin/sh","r+")
2854 * f.close_write #=> nil
2855 * f.closed? #=> false
2856 * f.close_read #=> nil
2857 * f.closed? #=> true
2861 static VALUE
2862 rb_io_closed(VALUE io)
2864 rb_io_t *fptr;
2865 VALUE write_io;
2866 rb_io_t *write_fptr;
2868 write_io = GetWriteIO(io);
2869 if (io != write_io) {
2870 write_fptr = RFILE(write_io)->fptr;
2871 if (write_fptr && 0 <= write_fptr->fd) {
2872 return Qfalse;
2876 fptr = RFILE(io)->fptr;
2877 rb_io_check_initialized(fptr);
2878 return 0 <= fptr->fd ? Qfalse : Qtrue;
2882 * call-seq:
2883 * ios.close_read => nil
2885 * Closes the read end of a duplex I/O stream (i.e., one that contains
2886 * both a read and a write stream, such as a pipe). Will raise an
2887 * <code>IOError</code> if the stream is not duplexed.
2889 * f = IO.popen("/bin/sh","r+")
2890 * f.close_read
2891 * f.readlines
2893 * <em>produces:</em>
2895 * prog.rb:3:in `readlines': not opened for reading (IOError)
2896 * from prog.rb:3
2899 static VALUE
2900 rb_io_close_read(VALUE io)
2902 rb_io_t *fptr;
2903 VALUE write_io;
2905 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
2906 rb_raise(rb_eSecurityError, "Insecure: can't close");
2908 GetOpenFile(io, fptr);
2909 if (is_socket(fptr->fd, fptr->path)) {
2910 #ifndef SHUT_RD
2911 # define SHUT_RD 0
2912 #endif
2913 if (shutdown(fptr->fd, SHUT_RD) < 0)
2914 rb_sys_fail(fptr->path);
2915 fptr->mode &= ~FMODE_READABLE;
2916 if (!(fptr->mode & FMODE_WRITABLE))
2917 return rb_io_close(io);
2918 return Qnil;
2921 write_io = GetWriteIO(io);
2922 if (io != write_io) {
2923 rb_io_t *wfptr;
2924 fptr_finalize(fptr, Qfalse);
2925 GetOpenFile(write_io, wfptr);
2926 if (fptr->refcnt < LONG_MAX) {
2927 wfptr->refcnt++;
2928 RFILE(io)->fptr = wfptr;
2929 rb_io_fptr_finalize(fptr);
2931 return Qnil;
2934 if (fptr->mode & FMODE_WRITABLE) {
2935 rb_raise(rb_eIOError, "closing non-duplex IO for reading");
2937 return rb_io_close(io);
2941 * call-seq:
2942 * ios.close_write => nil
2944 * Closes the write end of a duplex I/O stream (i.e., one that contains
2945 * both a read and a write stream, such as a pipe). Will raise an
2946 * <code>IOError</code> if the stream is not duplexed.
2948 * f = IO.popen("/bin/sh","r+")
2949 * f.close_write
2950 * f.print "nowhere"
2952 * <em>produces:</em>
2954 * prog.rb:3:in `write': not opened for writing (IOError)
2955 * from prog.rb:3:in `print'
2956 * from prog.rb:3
2959 static VALUE
2960 rb_io_close_write(VALUE io)
2962 rb_io_t *fptr;
2963 VALUE write_io;
2965 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(io)) {
2966 rb_raise(rb_eSecurityError, "Insecure: can't close");
2968 write_io = GetWriteIO(io);
2969 GetOpenFile(write_io, fptr);
2970 if (is_socket(fptr->fd, fptr->path)) {
2971 #ifndef SHUT_WR
2972 # define SHUT_WR 1
2973 #endif
2974 if (shutdown(fptr->fd, SHUT_WR) < 0)
2975 rb_sys_fail(fptr->path);
2976 fptr->mode &= ~FMODE_WRITABLE;
2977 if (!(fptr->mode & FMODE_READABLE))
2978 return rb_io_close(write_io);
2979 return Qnil;
2982 if (fptr->mode & FMODE_READABLE) {
2983 rb_raise(rb_eIOError, "closing non-duplex IO for writing");
2986 rb_io_close(write_io);
2987 if (io != write_io) {
2988 GetOpenFile(io, fptr);
2989 fptr->tied_io_for_writing = 0;
2990 fptr->mode &= ~FMODE_DUPLEX;
2992 return Qnil;
2996 * call-seq:
2997 * ios.sysseek(offset, whence=SEEK_SET) => integer
2999 * Seeks to a given <i>offset</i> in the stream according to the value
3000 * of <i>whence</i> (see <code>IO#seek</code> for values of
3001 * <i>whence</i>). Returns the new offset into the file.
3003 * f = File.new("testfile")
3004 * f.sysseek(-13, IO::SEEK_END) #=> 53
3005 * f.sysread(10) #=> "And so on."
3008 static VALUE
3009 rb_io_sysseek(int argc, VALUE *argv, VALUE io)
3011 VALUE offset, ptrname;
3012 int whence = SEEK_SET;
3013 rb_io_t *fptr;
3014 off_t pos;
3016 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
3017 whence = NUM2INT(ptrname);
3019 pos = NUM2OFFT(offset);
3020 GetOpenFile(io, fptr);
3021 if ((fptr->mode & FMODE_READABLE) && READ_DATA_BUFFERED(fptr)) {
3022 rb_raise(rb_eIOError, "sysseek for buffered IO");
3024 if ((fptr->mode & FMODE_WRITABLE) && fptr->wbuf_len) {
3025 rb_warn("sysseek for buffered IO");
3027 pos = lseek(fptr->fd, pos, whence);
3028 if (pos == -1) rb_sys_fail(fptr->path);
3030 return OFFT2NUM(pos);
3034 * call-seq:
3035 * ios.syswrite(string) => integer
3037 * Writes the given string to <em>ios</em> using a low-level write.
3038 * Returns the number of bytes written. Do not mix with other methods
3039 * that write to <em>ios</em> or you may get unpredictable results.
3040 * Raises <code>SystemCallError</code> on error.
3042 * f = File.new("out", "w")
3043 * f.syswrite("ABCDEF") #=> 6
3046 static VALUE
3047 rb_io_syswrite(VALUE io, VALUE str)
3049 rb_io_t *fptr;
3050 long n;
3052 rb_secure(4);
3053 if (TYPE(str) != T_STRING)
3054 str = rb_obj_as_string(str);
3056 io = GetWriteIO(io);
3057 GetOpenFile(io, fptr);
3058 rb_io_check_writable(fptr);
3060 if (fptr->wbuf_len) {
3061 rb_warn("syswrite for buffered IO");
3063 if (!rb_thread_fd_writable(fptr->fd)) {
3064 rb_io_check_closed(fptr);
3066 TRAP_BEG;
3067 n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
3068 TRAP_END;
3070 if (n == -1) rb_sys_fail(fptr->path);
3072 return LONG2FIX(n);
3076 * call-seq:
3077 * ios.sysread(integer[, outbuf]) => string
3079 * Reads <i>integer</i> bytes from <em>ios</em> using a low-level
3080 * read and returns them as a string. Do not mix with other methods
3081 * that read from <em>ios</em> or you may get unpredictable results.
3082 * If the optional <i>outbuf</i> argument is present, it must reference
3083 * a String, which will receive the data.
3084 * Raises <code>SystemCallError</code> on error and
3085 * <code>EOFError</code> at end of file.
3087 * f = File.new("testfile")
3088 * f.sysread(16) #=> "This is line one"
3091 static VALUE
3092 rb_io_sysread(int argc, VALUE *argv, VALUE io)
3094 VALUE len, str;
3095 rb_io_t *fptr;
3096 long n, ilen;
3098 rb_scan_args(argc, argv, "11", &len, &str);
3099 ilen = NUM2LONG(len);
3101 if (NIL_P(str)) {
3102 str = rb_str_new(0, ilen);
3104 else {
3105 StringValue(str);
3106 rb_str_modify(str);
3107 rb_str_resize(str, ilen);
3109 if (ilen == 0) return str;
3111 GetOpenFile(io, fptr);
3112 rb_io_check_readable(fptr);
3114 if (READ_DATA_BUFFERED(fptr)) {
3115 rb_raise(rb_eIOError, "sysread for buffered IO");
3118 n = fptr->fd;
3119 rb_thread_wait_fd(fptr->fd);
3120 rb_io_check_closed(fptr);
3121 if (RSTRING_LEN(str) != ilen) {
3122 rb_raise(rb_eRuntimeError, "buffer string modified");
3125 n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen);
3127 if (n == -1) {
3128 rb_sys_fail(fptr->path);
3130 rb_str_set_len(str, n);
3131 if (n == 0 && ilen > 0) {
3132 rb_eof_error();
3134 rb_str_resize(str, n);
3135 OBJ_TAINT(str);
3137 return str;
3140 VALUE
3141 rb_io_binmode(VALUE io)
3143 rb_io_t *fptr;
3145 GetOpenFile(io, fptr);
3146 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3147 if (!(fptr->mode & FMODE_BINMODE) && READ_DATA_BUFFERED(fptr)) {
3148 rb_raise(rb_eIOError, "buffer already filled with text-mode content");
3150 if (0 <= fptr->fd && setmode(fptr->fd, O_BINARY) == -1)
3151 rb_sys_fail(fptr->path);
3152 #endif
3153 fptr->mode |= FMODE_BINMODE;
3154 return io;
3158 * call-seq:
3159 * ios.binmode => ios
3161 * Puts <em>ios</em> into binary mode. This is useful only in
3162 * MS-DOS/Windows environments. Once a stream is in binary mode, it
3163 * cannot be reset to nonbinary mode.
3166 static VALUE
3167 rb_io_binmode_m(VALUE io)
3169 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3170 VALUE write_io;
3171 #endif
3173 rb_io_binmode(io);
3175 #if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
3176 write_io = GetWriteIO(io);
3177 if (write_io != io)
3178 rb_io_binmode(write_io);
3179 #endif
3180 return io;
3184 * call-seq:
3185 * ios.binmode? => true or false
3187 * Returns <code>true</code> if <em>ios</em> is binmode.
3189 static VALUE
3190 rb_io_binmode_p(VALUE io)
3192 rb_io_t *fptr;
3193 GetOpenFile(io, fptr);
3194 return fptr->mode & FMODE_BINMODE ? Qtrue : Qfalse;
3197 static const char*
3198 rb_io_flags_mode(int flags)
3200 #ifdef O_BINARY
3201 # define MODE_BINMODE(a,b) ((flags & FMODE_BINMODE) ? (b) : (a))
3202 #else
3203 # define MODE_BINMODE(a,b) (a)
3204 #endif
3205 if (flags & FMODE_APPEND) {
3206 if ((flags & FMODE_READWRITE) == FMODE_READWRITE) {
3207 return MODE_BINMODE("a+", "ab+");
3209 return MODE_BINMODE("a", "ab");
3211 switch (flags & FMODE_READWRITE) {
3212 case FMODE_READABLE:
3213 return MODE_BINMODE("r", "rb");
3214 case FMODE_WRITABLE:
3215 return MODE_BINMODE("w", "wb");
3216 case FMODE_READWRITE:
3217 if (flags & FMODE_CREATE) {
3218 return MODE_BINMODE("w+", "wb+");
3220 return MODE_BINMODE("r+", "rb+");
3222 rb_raise(rb_eArgError, "invalid access modenum %o", flags);
3223 return NULL; /* not reached */
3227 rb_io_mode_flags(const char *mode)
3229 int flags = 0;
3230 const char *m = mode;
3232 switch (*m++) {
3233 case 'r':
3234 flags |= FMODE_READABLE;
3235 break;
3236 case 'w':
3237 flags |= FMODE_WRITABLE | FMODE_CREATE;
3238 break;
3239 case 'a':
3240 flags |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
3241 break;
3242 default:
3243 error:
3244 rb_raise(rb_eArgError, "invalid access mode %s", mode);
3247 while (*m) {
3248 switch (*m++) {
3249 case 'b':
3250 flags |= FMODE_BINMODE;
3251 break;
3252 case '+':
3253 flags |= FMODE_READWRITE;
3254 break;
3255 default:
3256 goto error;
3257 case ':':
3258 return flags;
3262 return flags;
3266 rb_io_modenum_flags(int mode)
3268 int flags = 0;
3270 switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
3271 case O_RDONLY:
3272 flags = FMODE_READABLE;
3273 break;
3274 case O_WRONLY:
3275 flags = FMODE_WRITABLE;
3276 break;
3277 case O_RDWR:
3278 flags = FMODE_READWRITE;
3279 break;
3282 if (mode & O_APPEND) {
3283 flags |= FMODE_APPEND;
3285 if (mode & O_CREAT) {
3286 flags |= FMODE_CREATE;
3288 #ifdef O_BINARY
3289 if (mode & O_BINARY) {
3290 flags |= FMODE_BINMODE;
3292 #endif
3294 return flags;
3298 rb_io_mode_modenum(const char *mode)
3300 int flags = 0;
3301 const char *m = mode;
3303 switch (*m++) {
3304 case 'r':
3305 flags |= O_RDONLY;
3306 break;
3307 case 'w':
3308 flags |= O_WRONLY | O_CREAT | O_TRUNC;
3309 break;
3310 case 'a':
3311 flags |= O_WRONLY | O_CREAT | O_APPEND;
3312 break;
3313 default:
3314 error:
3315 rb_raise(rb_eArgError, "invalid access mode %s", mode);
3318 while (*m) {
3319 switch (*m++) {
3320 case 'b':
3321 #ifdef O_BINARY
3322 flags |= O_BINARY;
3323 #endif
3324 break;
3325 case '+':
3326 flags = (flags & ~O_ACCMODE) | O_RDWR;
3327 break;
3328 default:
3329 goto error;
3330 case ':':
3331 return flags;
3335 return flags;
3338 #define MODENUM_MAX 4
3340 static const char*
3341 rb_io_modenum_mode(int flags)
3343 #ifdef O_BINARY
3344 # define MODE_BINARY(a,b) ((flags & O_BINARY) ? (b) : (a))
3345 #else
3346 # define MODE_BINARY(a,b) (a)
3347 #endif
3348 if (flags & O_APPEND) {
3349 if ((flags & O_RDWR) == O_RDWR) {
3350 return MODE_BINARY("a+", "ab+");
3352 return MODE_BINARY("a", "ab");
3354 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
3355 case O_RDONLY:
3356 return MODE_BINARY("r", "rb");
3357 case O_WRONLY:
3358 return MODE_BINARY("w", "wb");
3359 case O_RDWR:
3360 return MODE_BINARY("r+", "rb+");
3362 rb_raise(rb_eArgError, "invalid access modenum %o", flags);
3363 return NULL; /* not reached */
3366 static void
3367 mode_enc(rb_io_t *fptr, const char *estr)
3369 const char *p0, *p1;
3370 char *enc2name;
3371 int idx, idx2;
3373 p0 = strrchr(estr, ':');
3374 if (!p0) p1 = estr;
3375 else p1 = p0 + 1;
3376 idx = rb_enc_find_index(p1);
3377 if (idx >= 0) {
3378 fptr->enc = rb_enc_from_index(idx);
3380 else {
3381 rb_warn("Unsupported encoding %s ignored", p1);
3384 if (p0) {
3385 int n = p0 - estr;
3386 if (n > ENCODING_MAXNAMELEN) {
3387 idx2 = -1;
3389 else {
3390 enc2name = ALLOCA_N(char, n+1);
3391 memcpy(enc2name, estr, n);
3392 enc2name[n] = '\0';
3393 estr = enc2name;
3394 idx2 = rb_enc_find_index(enc2name);
3396 if (idx2 < 0) {
3397 rb_warn("Unsupported encoding %.*s ignored", n, estr);
3399 else if (idx2 == idx) {
3400 rb_warn("Ignoring internal encoding %.*s: it is identical to external encoding %s",
3401 n, estr, p1);
3403 else {
3404 fptr->enc2 = rb_enc_from_index(idx2);
3409 void
3410 rb_io_mode_enc(rb_io_t *fptr, const char *mode)
3412 const char *p = strchr(mode, ':');
3413 if (p) {
3414 mode_enc(fptr, p+1);
3418 struct sysopen_struct {
3419 char *fname;
3420 int flag;
3421 unsigned int mode;
3424 static VALUE
3425 sysopen_func(void *ptr)
3427 struct sysopen_struct *data = ptr;
3428 return (VALUE)open(data->fname, data->flag, data->mode);
3431 static int
3432 rb_sysopen_internal(char *fname, int flags, unsigned int mode)
3434 struct sysopen_struct data;
3435 data.fname = fname;
3436 data.flag = flags;
3437 data.mode = mode;
3438 return (int)rb_thread_blocking_region(sysopen_func, &data, RB_UBF_DFL, 0);
3441 static int
3442 rb_sysopen(char *fname, int flags, unsigned int mode)
3444 int fd;
3446 fd = rb_sysopen_internal(fname, flags, mode);
3447 if (fd < 0) {
3448 if (errno == EMFILE || errno == ENFILE) {
3449 rb_gc();
3450 fd = rb_sysopen_internal(fname, flags, mode);
3452 if (fd < 0) {
3453 rb_sys_fail(fname);
3456 UPDATE_MAXFD(fd);
3457 return fd;
3460 FILE *
3461 rb_fopen(const char *fname, const char *mode)
3463 FILE *file;
3465 file = fopen(fname, mode);
3466 if (!file) {
3467 if (errno == EMFILE || errno == ENFILE) {
3468 rb_gc();
3469 file = fopen(fname, mode);
3471 if (!file) {
3472 rb_sys_fail(fname);
3475 #ifdef USE_SETVBUF
3476 if (setvbuf(file, NULL, _IOFBF, 0) != 0)
3477 rb_warn("setvbuf() can't be honoured for %s", fname);
3478 #endif
3479 #ifdef __human68k__
3480 setmode(fileno(file), O_TEXT);
3481 #endif
3482 return file;
3485 FILE *
3486 rb_fdopen(int fd, const char *mode)
3488 FILE *file;
3490 #if defined(sun)
3491 errno = 0;
3492 #endif
3493 file = fdopen(fd, mode);
3494 if (!file) {
3495 if (
3496 #if defined(sun)
3497 errno == 0 ||
3498 #endif
3499 errno == EMFILE || errno == ENFILE) {
3500 rb_gc();
3501 #if defined(sun)
3502 errno = 0;
3503 #endif
3504 file = fdopen(fd, mode);
3506 if (!file) {
3507 #ifdef _WIN32
3508 if (errno == 0) errno = EINVAL;
3509 #elif defined(sun)
3510 if (errno == 0) errno = EMFILE;
3511 #endif
3512 rb_sys_fail(0);
3516 /* xxx: should be _IONBF? A buffer in FILE may have trouble. */
3517 #ifdef USE_SETVBUF
3518 if (setvbuf(file, NULL, _IOFBF, 0) != 0)
3519 rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
3520 #endif
3521 return file;
3524 static void
3525 io_check_tty(rb_io_t *fptr)
3527 if (isatty(fptr->fd))
3528 fptr->mode |= FMODE_TTY|FMODE_DUPLEX;
3531 static VALUE
3532 rb_file_open_internal(VALUE io, const char *fname, const char *mode)
3534 rb_io_t *fptr;
3536 MakeOpenFile(io, fptr);
3537 fptr->mode = rb_io_mode_flags(mode);
3538 rb_io_mode_enc(fptr, mode);
3539 fptr->path = strdup(fname);
3540 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(rb_io_flags_mode(fptr->mode)), 0666);
3541 io_check_tty(fptr);
3543 return io;
3546 VALUE
3547 rb_file_open(const char *fname, const char *mode)
3549 return rb_file_open_internal(io_alloc(rb_cFile), fname, mode);
3552 static VALUE
3553 rb_file_sysopen_internal(VALUE io, const char *fname, int flags, int mode)
3555 rb_io_t *fptr;
3557 MakeOpenFile(io, fptr);
3559 fptr->path = strdup(fname);
3560 fptr->mode = rb_io_modenum_flags(flags);
3561 fptr->fd = rb_sysopen(fptr->path, flags, mode);
3562 io_check_tty(fptr);
3564 return io;
3567 VALUE
3568 rb_file_sysopen(const char *fname, int flags, int mode)
3570 return rb_file_sysopen_internal(io_alloc(rb_cFile), fname, flags, mode);
3573 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
3574 static struct pipe_list {
3575 rb_io_t *fptr;
3576 struct pipe_list *next;
3577 } *pipe_list;
3579 static void
3580 pipe_add_fptr(rb_io_t *fptr)
3582 struct pipe_list *list;
3584 list = ALLOC(struct pipe_list);
3585 list->fptr = fptr;
3586 list->next = pipe_list;
3587 pipe_list = list;
3590 static void
3591 pipe_del_fptr(rb_io_t *fptr)
3593 struct pipe_list *list = pipe_list;
3594 struct pipe_list *tmp;
3596 if (list->fptr == fptr) {
3597 pipe_list = list->next;
3598 free(list);
3599 return;
3602 while (list->next) {
3603 if (list->next->fptr == fptr) {
3604 tmp = list->next;
3605 list->next = list->next->next;
3606 free(tmp);
3607 return;
3609 list = list->next;
3613 static void
3614 pipe_atexit(void)
3616 struct pipe_list *list = pipe_list;
3617 struct pipe_list *tmp;
3619 while (list) {
3620 tmp = list->next;
3621 rb_io_fptr_finalize(list->fptr);
3622 list = tmp;
3626 static void
3627 pipe_finalize(rb_io_t *fptr, int noraise)
3629 #if !defined(HAVE_FORK) && !defined(_WIN32)
3630 int status;
3631 if (fptr->stdio_file) {
3632 status = pclose(fptr->stdio_file);
3634 fptr->fd = -1;
3635 fptr->stdio_file = 0;
3636 #if defined DJGPP
3637 status <<= 8;
3638 #endif
3639 rb_last_status_set(status, fptr->pid);
3640 #else
3641 fptr_finalize(fptr, noraise);
3642 #endif
3643 pipe_del_fptr(fptr);
3645 #endif
3647 void
3648 rb_io_synchronized(rb_io_t *fptr)
3650 rb_io_check_initialized(fptr);
3651 fptr->mode |= FMODE_SYNC;
3654 void
3655 rb_io_unbuffered(rb_io_t *fptr)
3657 rb_io_synchronized(fptr);
3661 rb_pipe(int *pipes)
3663 int ret;
3664 ret = pipe(pipes);
3665 if (ret == -1) {
3666 if (errno == EMFILE || errno == ENFILE) {
3667 rb_gc();
3668 ret = pipe(pipes);
3671 if (ret == 0) {
3672 UPDATE_MAXFD(pipes[0]);
3673 UPDATE_MAXFD(pipes[1]);
3675 return ret;
3678 #ifdef HAVE_FORK
3679 struct popen_arg {
3680 struct rb_exec_arg *execp;
3681 int modef;
3682 int pair[2];
3683 int write_pair[2];
3686 static void
3687 popen_redirect(struct popen_arg *p)
3689 if ((p->modef & FMODE_READABLE) && (p->modef & FMODE_WRITABLE)) {
3690 close(p->write_pair[1]);
3691 if (p->write_pair[0] != 0) {
3692 dup2(p->write_pair[0], 0);
3693 close(p->write_pair[0]);
3695 close(p->pair[0]);
3696 if (p->pair[1] != 1) {
3697 dup2(p->pair[1], 1);
3698 close(p->pair[1]);
3701 else if (p->modef & FMODE_READABLE) {
3702 close(p->pair[0]);
3703 if (p->pair[1] != 1) {
3704 dup2(p->pair[1], 1);
3705 close(p->pair[1]);
3708 else {
3709 close(p->pair[1]);
3710 if (p->pair[0] != 0) {
3711 dup2(p->pair[0], 0);
3712 close(p->pair[0]);
3717 void
3718 rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
3720 int fd, ret;
3721 int max = max_file_descriptor;
3722 if (max < maxhint)
3723 max = maxhint;
3724 for (fd = lowfd; fd <= max; fd++) {
3725 if (!NIL_P(noclose_fds) &&
3726 RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd))))
3727 continue;
3728 #ifdef FD_CLOEXEC
3729 ret = fcntl(fd, F_GETFD);
3730 if (ret != -1 && !(ret & FD_CLOEXEC)) {
3731 fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
3733 #else
3734 close(fd);
3735 #endif
3739 static int
3740 popen_exec(void *pp)
3742 struct popen_arg *p = (struct popen_arg*)pp;
3744 rb_thread_atfork_before_exec();
3745 return rb_exec(p->execp);
3747 #endif
3749 static VALUE
3750 pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *mode)
3752 int modef = rb_io_mode_flags(mode);
3753 int pid = 0;
3754 rb_io_t *fptr;
3755 VALUE port;
3756 rb_io_t *write_fptr;
3757 VALUE write_port;
3758 #if defined(HAVE_FORK)
3759 int status;
3760 struct popen_arg arg;
3761 #elif defined(_WIN32)
3762 int openmode = rb_io_mode_modenum(mode);
3763 const char *exename = NULL;
3764 volatile VALUE cmdbuf;
3765 struct rb_exec_arg sarg;
3766 #endif
3767 FILE *fp = 0;
3768 int fd = -1;
3769 int write_fd = -1;
3770 const char *cmd = 0;
3771 int argc;
3772 VALUE *argv;
3774 if (prog)
3775 cmd = StringValueCStr(prog);
3777 if (!eargp) {
3778 /* fork : IO.popen("-") */
3779 argc = 0;
3780 argv = 0;
3782 else if (eargp->argc) {
3783 /* no shell : IO.popen([prog, arg0], arg1, ...) */
3784 argc = eargp->argc;
3785 argv = eargp->argv;
3787 else {
3788 /* with shell : IO.popen(prog) */
3789 argc = 0;
3790 argv = 0;
3793 #if defined(HAVE_FORK)
3794 arg.execp = eargp;
3795 arg.modef = modef;
3796 arg.pair[0] = arg.pair[1] = -1;
3797 arg.write_pair[0] = arg.write_pair[1] = -1;
3798 switch (modef & (FMODE_READABLE|FMODE_WRITABLE)) {
3799 case FMODE_READABLE|FMODE_WRITABLE:
3800 if (rb_pipe(arg.write_pair) < 0)
3801 rb_sys_fail(cmd);
3802 if (rb_pipe(arg.pair) < 0) {
3803 int e = errno;
3804 close(arg.write_pair[0]);
3805 close(arg.write_pair[1]);
3806 errno = e;
3807 rb_sys_fail(cmd);
3809 if (eargp) {
3810 rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.write_pair[0]));
3811 rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
3813 break;
3814 case FMODE_READABLE:
3815 if (rb_pipe(arg.pair) < 0)
3816 rb_sys_fail(cmd);
3817 if (eargp)
3818 rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1]));
3819 break;
3820 case FMODE_WRITABLE:
3821 if (rb_pipe(arg.pair) < 0)
3822 rb_sys_fail(cmd);
3823 if (eargp)
3824 rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.pair[0]));
3825 break;
3826 default:
3827 rb_sys_fail(cmd);
3829 if (eargp) {
3830 rb_exec_arg_fixup(arg.execp);
3831 pid = rb_fork(&status, popen_exec, &arg, arg.execp->redirect_fds);
3833 else {
3834 fflush(stdin); /* is it really needed? */
3835 rb_io_flush(rb_stdout);
3836 rb_io_flush(rb_stderr);
3837 pid = rb_fork(&status, 0, 0, Qnil);
3838 if (pid == 0) { /* child */
3839 popen_redirect(&arg);
3840 rb_io_synchronized(RFILE(orig_stdout)->fptr);
3841 rb_io_synchronized(RFILE(orig_stderr)->fptr);
3842 return Qnil;
3846 /* parent */
3847 if (pid == -1) {
3848 int e = errno;
3849 close(arg.pair[0]);
3850 close(arg.pair[1]);
3851 if ((modef & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
3852 close(arg.write_pair[0]);
3853 close(arg.write_pair[1]);
3855 errno = e;
3856 rb_sys_fail(cmd);
3858 if ((modef & FMODE_READABLE) && (modef & FMODE_WRITABLE)) {
3859 close(arg.pair[1]);
3860 fd = arg.pair[0];
3861 close(arg.write_pair[0]);
3862 write_fd = arg.write_pair[1];
3864 else if (modef & FMODE_READABLE) {
3865 close(arg.pair[1]);
3866 fd = arg.pair[0];
3868 else {
3869 close(arg.pair[0]);
3870 fd = arg.pair[1];
3872 #elif defined(_WIN32)
3873 if (argc) {
3874 volatile VALUE argbuf;
3875 char **args;
3876 int i;
3878 if (argc >= FIXNUM_MAX / sizeof(char *)) {
3879 rb_raise(rb_eArgError, "too many arguments");
3881 argbuf = rb_str_tmp_new((argc+1) * sizeof(char *));
3882 args = (void *)RSTRING_PTR(argbuf);
3883 for (i = 0; i < argc; ++i) {
3884 args[i] = StringValueCStr(argv[i]);
3886 args[i] = NULL;
3887 exename = cmd;
3888 cmdbuf = rb_str_tmp_new(rb_w32_argv_size(args));
3889 cmd = rb_w32_join_argv(RSTRING_PTR(cmdbuf), args);
3890 rb_str_resize(argbuf, 0);
3892 if (eargp) {
3893 rb_exec_arg_fixup(eargp);
3894 rb_run_exec_options(eargp, &sarg);
3896 while ((pid = rb_w32_pipe_exec(cmd, exename, openmode, &fd, &write_fd)) == -1) {
3897 /* exec failed */
3898 switch (errno) {
3899 case EAGAIN:
3900 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3901 case EWOULDBLOCK:
3902 #endif
3903 rb_thread_sleep(1);
3904 break;
3905 default:
3906 if (eargp)
3907 rb_run_exec_options(&sarg, NULL);
3908 rb_sys_fail(cmd);
3909 break;
3912 if (eargp)
3913 rb_run_exec_options(&sarg, NULL);
3914 #else
3915 if (argc) {
3916 prog = rb_ary_join(rb_ary_new4(argc, argv), rb_str_new2(" "));
3917 cmd = StringValueCStr(prog);
3919 if (eargp) {
3920 rb_exec_arg_fixup(eargp);
3921 rb_run_exec_options(eargp, &sarg);
3923 fp = popen(cmd, mode);
3924 if (eargp)
3925 rb_run_exec_options(&sarg, NULL);
3926 if (!fp) rb_sys_fail(RSTRING_PTR(prog));
3927 fd = fileno(fp);
3928 #endif
3930 port = io_alloc(rb_cIO);
3931 MakeOpenFile(port, fptr);
3932 fptr->fd = fd;
3933 fptr->stdio_file = fp;
3934 fptr->mode = modef | FMODE_SYNC|FMODE_DUPLEX;
3935 rb_io_mode_enc(fptr, mode);
3936 fptr->pid = pid;
3938 if (0 <= write_fd) {
3939 write_port = io_alloc(rb_cIO);
3940 MakeOpenFile(write_port, write_fptr);
3941 write_fptr->fd = write_fd;
3942 write_fptr->mode = (modef & ~FMODE_READABLE)| FMODE_SYNC|FMODE_DUPLEX;
3943 fptr->mode &= ~FMODE_WRITABLE;
3944 fptr->tied_io_for_writing = write_port;
3945 rb_ivar_set(port, rb_intern("@tied_io_for_writing"), write_port);
3948 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
3949 fptr->finalize = pipe_finalize;
3950 pipe_add_fptr(fptr);
3951 #endif
3952 return port;
3955 static VALUE
3956 pipe_open_v(int argc, VALUE *argv, const char *mode)
3958 VALUE prog;
3959 struct rb_exec_arg earg;
3960 prog = rb_exec_arg_init(argc, argv, Qfalse, &earg);
3961 return pipe_open(&earg, prog, mode);
3964 static VALUE
3965 pipe_open_s(VALUE prog, const char *mode)
3967 const char *cmd = RSTRING_PTR(prog);
3968 int argc = 1;
3969 VALUE *argv = &prog;
3970 struct rb_exec_arg earg;
3972 if (RSTRING_LEN(prog) == 1 && cmd[0] == '-') {
3973 #if !defined(HAVE_FORK)
3974 rb_raise(rb_eNotImpError,
3975 "fork() function is unimplemented on this machine");
3976 #endif
3977 return pipe_open(0, 0, mode);
3980 rb_exec_arg_init(argc, argv, Qtrue, &earg);
3981 return pipe_open(&earg, prog, mode);
3985 * call-seq:
3986 * IO.popen(cmd, mode="r") => io
3987 * IO.popen(cmd, mode="r") {|io| block } => obj
3989 * Runs the specified command as a subprocess; the subprocess's
3990 * standard input and output will be connected to the returned
3991 * <code>IO</code> object. If _cmd_ is a +String+
3992 * ``<code>-</code>'', then a new instance of Ruby is started as the
3993 * subprocess. If <i>cmd</i> is an +Array+ of +String+, then it will
3994 * be used as the subprocess's +argv+ bypassing a shell.
3995 * The array can contains a hash at first for environments and
3996 * a hash at last for options similar to <code>spawn</code>. The default
3997 * mode for the new file object is ``r'', but <i>mode</i> may be set
3998 * to any of the modes listed in the description for class IO.
4000 * Raises exceptions which <code>IO::pipe</code> and
4001 * <code>Kernel::system</code> raise.
4003 * If a block is given, Ruby will run the command as a child connected
4004 * to Ruby with a pipe. Ruby's end of the pipe will be passed as a
4005 * parameter to the block.
4006 * At the end of block, Ruby close the pipe and sets <code>$?</code>.
4007 * In this case <code>IO::popen</code> returns
4008 * the value of the block.
4010 * If a block is given with a _cmd_ of ``<code>-</code>'',
4011 * the block will be run in two separate processes: once in the parent,
4012 * and once in a child. The parent process will be passed the pipe
4013 * object as a parameter to the block, the child version of the block
4014 * will be passed <code>nil</code>, and the child's standard in and
4015 * standard out will be connected to the parent through the pipe. Not
4016 * available on all platforms.
4018 * f = IO.popen("uname")
4019 * p f.readlines
4020 * puts "Parent is #{Process.pid}"
4021 * IO.popen("date") { |f| puts f.gets }
4022 * IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f}"}
4023 * p $?
4024 * IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
4025 * f.puts "bar"; f.close_write; puts f.gets
4028 * <em>produces:</em>
4030 * ["Linux\n"]
4031 * Parent is 26166
4032 * Wed Apr 9 08:53:52 CDT 2003
4033 * 26169 is here, f is
4034 * 26166 is here, f is #<IO:0x401b3d44>
4035 * #<Process::Status: pid=26166,exited(0)>
4036 * <foo>bar;zot;
4039 static VALUE
4040 rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
4042 const char *mode;
4043 VALUE pname, pmode, port, tmp;
4045 if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
4046 mode = "r";
4048 else if (FIXNUM_P(pmode)) {
4049 mode = rb_io_modenum_mode(FIX2INT(pmode));
4051 else {
4052 mode = StringValueCStr(pmode);
4054 tmp = rb_check_array_type(pname);
4055 if (!NIL_P(tmp)) {
4056 tmp = rb_ary_dup(tmp);
4057 RBASIC(tmp)->klass = 0;
4058 port = pipe_open_v(RARRAY_LEN(tmp), RARRAY_PTR(tmp), mode);
4059 rb_ary_clear(tmp);
4061 else {
4062 SafeStringValue(pname);
4063 port = pipe_open_s(pname, mode);
4065 if (NIL_P(port)) {
4066 /* child */
4067 if (rb_block_given_p()) {
4068 rb_yield(Qnil);
4069 rb_io_flush(rb_stdout);
4070 rb_io_flush(rb_stderr);
4071 _exit(0);
4073 return Qnil;
4075 RBASIC(port)->klass = klass;
4076 if (rb_block_given_p()) {
4077 return rb_ensure(rb_yield, port, io_close, port);
4079 return port;
4082 static void
4083 io_set_encoding(VALUE io, VALUE opt)
4085 rb_io_t *fptr;
4086 VALUE encoding=Qnil, extenc=Qnil, intenc=Qnil;
4087 if (!NIL_P(opt)) {
4088 VALUE v;
4089 v = rb_hash_aref(opt, sym_encoding);
4090 if (!NIL_P(v)) encoding = v;
4091 v = rb_hash_aref(opt, sym_extenc);
4092 if (!NIL_P(v)) extenc = v;
4093 v = rb_hash_aref(opt, sym_intenc);
4094 if (!NIL_P(v)) intenc = v;
4096 if (!NIL_P(extenc)) {
4097 rb_encoding *extencoding = rb_to_encoding(extenc);
4098 GetOpenFile(io, fptr);
4099 if (!NIL_P(encoding)) {
4100 rb_warn("Ignoring encoding parameter '%s': external_encoding is used",
4101 RSTRING_PTR(encoding));
4103 if (!NIL_P(intenc)) {
4104 rb_encoding *intencoding = rb_to_encoding(intenc);
4105 if (extencoding == intencoding) {
4106 rb_warn("Ignoring internal encoding '%s': it is identical to external encoding '%s'",
4107 RSTRING_PTR(rb_inspect(intenc)),
4108 RSTRING_PTR(rb_inspect(extenc)));
4110 else {
4111 fptr->enc2 = intencoding;
4114 fptr->enc = extencoding;
4116 else {
4117 if (!NIL_P(intenc)) {
4118 rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given");
4120 if (!NIL_P(encoding)) {
4121 GetOpenFile(io, fptr);
4122 mode_enc(fptr, StringValueCStr(encoding));
4127 static VALUE
4128 rb_open_file(int argc, VALUE *argv, VALUE io)
4130 VALUE opt, fname, vmode, perm;
4131 const char *mode;
4132 int flags;
4133 unsigned int fmode;
4135 opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
4136 if (!NIL_P(opt)) {
4137 VALUE v;
4138 v = rb_hash_aref(opt, sym_mode);
4139 if (!NIL_P(v)) vmode = v;
4140 v = rb_hash_aref(opt, sym_perm);
4141 if (!NIL_P(v)) perm = v;
4142 argc -= 1;
4145 rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
4146 #if defined _WIN32 || defined __APPLE__
4148 static rb_encoding *fs_encoding;
4149 rb_encoding *fname_encoding = rb_enc_get(fname);
4150 if (!fs_encoding)
4151 fs_encoding = rb_filesystem_encoding();
4152 if (rb_usascii_encoding() != fname_encoding
4153 && rb_ascii8bit_encoding() != fname_encoding
4154 #if defined __APPLE__
4155 && rb_utf8_encoding() != fname_encoding
4156 #endif
4157 && fs_encoding != fname_encoding) {
4158 static VALUE fs_enc;
4159 if (!fs_enc)
4160 fs_enc = rb_enc_from_encoding(fs_encoding);
4161 fname = rb_str_transcode(fname, fs_enc);
4164 #endif
4165 FilePathValue(fname);
4167 if (FIXNUM_P(vmode) || !NIL_P(perm)) {
4168 if (FIXNUM_P(vmode)) {
4169 flags = FIX2INT(vmode);
4171 else {
4172 SafeStringValue(vmode);
4173 flags = rb_io_mode_modenum(StringValueCStr(vmode));
4175 fmode = NIL_P(perm) ? 0666 : NUM2UINT(perm);
4177 rb_file_sysopen_internal(io, RSTRING_PTR(fname), flags, fmode);
4179 else {
4180 mode = NIL_P(vmode) ? "r" : StringValueCStr(vmode);
4181 rb_file_open_internal(io, RSTRING_PTR(fname), mode);
4184 io_set_encoding(io, opt);
4185 return io;
4189 * call-seq:
4190 * IO.open(fd, mode_string="r" ) => io
4191 * IO.open(fd, mode_string="r" ) {|io| block } => obj
4193 * With no associated block, <code>open</code> is a synonym for
4194 * <code>IO::new</code>. If the optional code block is given, it will
4195 * be passed <i>io</i> as an argument, and the IO object will
4196 * automatically be closed when the block terminates. In this instance,
4197 * <code>IO::open</code> returns the value of the block.
4201 static VALUE
4202 rb_io_s_open(int argc, VALUE *argv, VALUE klass)
4204 VALUE io = rb_class_new_instance(argc, argv, klass);
4206 if (rb_block_given_p()) {
4207 return rb_ensure(rb_yield, io, io_close, io);
4210 return io;
4214 * call-seq:
4215 * IO.sysopen(path, [mode, [perm]]) => fixnum
4217 * Opens the given path, returning the underlying file descriptor as a
4218 * <code>Fixnum</code>.
4220 * IO.sysopen("testfile") #=> 3
4224 static VALUE
4225 rb_io_s_sysopen(int argc, VALUE *argv)
4227 VALUE fname, vmode, perm;
4228 int flags, fd;
4229 unsigned int fmode;
4230 char *path;
4232 rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
4233 FilePathValue(fname);
4235 if (NIL_P(vmode)) flags = O_RDONLY;
4236 else if (FIXNUM_P(vmode)) flags = FIX2INT(vmode);
4237 else {
4238 SafeStringValue(vmode);
4239 flags = rb_io_mode_modenum(StringValueCStr(vmode));
4241 if (NIL_P(perm)) fmode = 0666;
4242 else fmode = NUM2UINT(perm);
4244 RB_GC_GUARD(fname) = rb_str_new4(fname);
4245 path = RSTRING_PTR(fname);
4246 fd = rb_sysopen(path, flags, fmode);
4247 return INT2NUM(fd);
4251 * call-seq:
4252 * open(path [, mode_enc [, perm]] ) => io or nil
4253 * open(path [, mode_enc [, perm]] ) {|io| block } => obj
4255 * Creates an <code>IO</code> object connected to the given stream,
4256 * file, or subprocess.
4258 * If <i>path</i> does not start with a pipe character
4259 * (``<code>|</code>''), treat it as the name of a file to open using
4260 * the specified mode (defaulting to ``<code>r</code>'').
4262 * The mode_enc is
4263 * either a string or an integer. If it is an integer, it must be
4264 * bitwise-or of open(2) flags, such as File::RDWR or File::EXCL.
4265 * If it is a string, it is either "mode", "mode:ext_enc", or
4266 * "mode:ext_enc:int_enc".
4267 * The mode is one of the following:
4269 * r: read (default)
4270 * w: write
4271 * a: append
4273 * The mode can be followed by "b" (means binary-mode), or "+"
4274 * (means both reading and writing allowed) or both.
4275 * If ext_enc (external encoding) is specified,
4276 * read string will be tagged by the encoding in reading,
4277 * and output string will be converted
4278 * to the specified encoding in writing.
4279 * If two encoding names,
4280 * ext_enc and int_enc (external encoding and internal encoding),
4281 * are specified, the read string is converted from ext_enc
4282 * to int_enc then tagged with the int_enc in read mode,
4283 * and in write mode, the output string will be
4284 * converted from int_enc to ext_enc before writing.
4286 * If a file is being created, its initial permissions may be
4287 * set using the integer third parameter.
4289 * If a block is specified, it will be invoked with the
4290 * <code>File</code> object as a parameter, and the file will be
4291 * automatically closed when the block terminates. The call
4292 * returns the value of the block.
4294 * If <i>path</i> starts with a pipe character, a subprocess is
4295 * created, connected to the caller by a pair of pipes. The returned
4296 * <code>IO</code> object may be used to write to the standard input
4297 * and read from the standard output of this subprocess. If the command
4298 * following the ``<code>|</code>'' is a single minus sign, Ruby forks,
4299 * and this subprocess is connected to the parent. In the subprocess,
4300 * the <code>open</code> call returns <code>nil</code>. If the command
4301 * is not ``<code>-</code>'', the subprocess runs the command. If a
4302 * block is associated with an <code>open("|-")</code> call, that block
4303 * will be run twice---once in the parent and once in the child. The
4304 * block parameter will be an <code>IO</code> object in the parent and
4305 * <code>nil</code> in the child. The parent's <code>IO</code> object
4306 * will be connected to the child's <code>$stdin</code> and
4307 * <code>$stdout</code>. The subprocess will be terminated at the end
4308 * of the block.
4310 * open("testfile") do |f|
4311 * print f.gets
4312 * end
4314 * <em>produces:</em>
4316 * This is line one
4318 * Open a subprocess and read its output:
4320 * cmd = open("|date")
4321 * print cmd.gets
4322 * cmd.close
4324 * <em>produces:</em>
4326 * Wed Apr 9 08:56:31 CDT 2003
4328 * Open a subprocess running the same Ruby program:
4330 * f = open("|-", "w+")
4331 * if f == nil
4332 * puts "in Child"
4333 * exit
4334 * else
4335 * puts "Got: #{f.gets}"
4336 * end
4338 * <em>produces:</em>
4340 * Got: in Child
4342 * Open a subprocess using a block to receive the I/O object:
4344 * open("|-") do |f|
4345 * if f == nil
4346 * puts "in Child"
4347 * else
4348 * puts "Got: #{f.gets}"
4349 * end
4350 * end
4352 * <em>produces:</em>
4354 * Got: in Child
4357 static VALUE
4358 rb_f_open(int argc, VALUE *argv)
4360 ID to_open = 0;
4361 int redirect = Qfalse;
4363 if (argc >= 1) {
4364 CONST_ID(to_open, "to_open");
4365 if (rb_respond_to(argv[0], to_open)) {
4366 redirect = Qtrue;
4368 else {
4369 VALUE tmp = argv[0];
4370 FilePathValue(tmp);
4371 if (NIL_P(tmp)) {
4372 redirect = Qtrue;
4374 else {
4375 char *str = StringValuePtr(tmp);
4376 if (str && str[0] == '|') {
4377 argv[0] = rb_str_new(str+1, RSTRING_LEN(tmp)-1);
4378 OBJ_INFECT(argv[0], tmp);
4379 return rb_io_s_popen(argc, argv, rb_cIO);
4384 if (redirect) {
4385 VALUE io = rb_funcall2(argv[0], to_open, argc-1, argv+1);
4387 if (rb_block_given_p()) {
4388 return rb_ensure(rb_yield, io, io_close, io);
4390 return io;
4392 return rb_io_s_open(argc, argv, rb_cFile);
4395 static VALUE
4396 rb_io_open(const char *fname, const char *mode)
4398 if (fname[0] == '|') {
4399 VALUE cmd = rb_str_new2(fname+1);
4400 return pipe_open_s(cmd, mode);
4402 else {
4403 return rb_file_open(fname, mode);
4407 static VALUE
4408 rb_io_open_with_args(int argc, VALUE *argv)
4410 const char *mode;
4411 VALUE pname, pmode;
4413 if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
4414 mode = "r";
4416 else if (FIXNUM_P(pmode)) {
4417 mode = rb_io_modenum_mode(FIX2INT(pmode));
4419 else {
4420 mode = StringValueCStr(pmode);
4422 return rb_io_open(StringValueCStr(pname), mode);
4425 static VALUE
4426 io_reopen(VALUE io, VALUE nfile)
4428 rb_io_t *fptr, *orig;
4429 int fd, fd2;
4430 off_t pos = 0;
4432 nfile = rb_io_get_io(nfile);
4433 if (rb_safe_level() >= 4 &&
4434 (!OBJ_UNTRUSTED(io) || !OBJ_UNTRUSTED(nfile))) {
4435 rb_raise(rb_eSecurityError, "Insecure: can't reopen");
4437 GetOpenFile(io, fptr);
4438 GetOpenFile(nfile, orig);
4440 if (fptr == orig) return io;
4441 if (IS_PREP_STDIO(fptr)) {
4442 if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
4443 (fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
4444 (fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
4445 rb_raise(rb_eArgError,
4446 "%s can't change access mode from \"%s\" to \"%s\"",
4447 PREP_STDIO_NAME(fptr), rb_io_flags_mode(fptr->mode),
4448 rb_io_flags_mode(orig->mode));
4451 if (orig->mode & FMODE_READABLE) {
4452 pos = io_tell(orig);
4454 if (orig->mode & FMODE_WRITABLE) {
4455 io_fflush(orig);
4457 if (fptr->mode & FMODE_WRITABLE) {
4458 io_fflush(fptr);
4461 /* copy rb_io_t structure */
4462 fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
4463 fptr->pid = orig->pid;
4464 fptr->lineno = orig->lineno;
4465 if (fptr->path) free(fptr->path);
4466 if (orig->path) fptr->path = strdup(orig->path);
4467 else fptr->path = 0;
4468 fptr->finalize = orig->finalize;
4470 fd = fptr->fd;
4471 fd2 = orig->fd;
4472 if (fd != fd2) {
4473 if (IS_PREP_STDIO(fptr)) {
4474 /* need to keep stdio objects */
4475 if (dup2(fd2, fd) < 0)
4476 rb_sys_fail(orig->path);
4478 else {
4479 if (fptr->stdio_file)
4480 fclose(fptr->stdio_file);
4481 else
4482 close(fptr->fd);
4483 fptr->stdio_file = 0;
4484 fptr->fd = -1;
4485 if (dup2(fd2, fd) < 0)
4486 rb_sys_fail(orig->path);
4487 fptr->fd = fd;
4489 rb_thread_fd_close(fd);
4490 if ((orig->mode & FMODE_READABLE) && pos >= 0) {
4491 if (io_seek(fptr, pos, SEEK_SET) < 0) {
4492 rb_sys_fail(fptr->path);
4494 if (io_seek(orig, pos, SEEK_SET) < 0) {
4495 rb_sys_fail(orig->path);
4500 if (fptr->mode & FMODE_BINMODE) {
4501 rb_io_binmode(io);
4504 RBASIC(io)->klass = rb_obj_class(nfile);
4505 return io;
4509 * call-seq:
4510 * ios.reopen(other_IO) => ios
4511 * ios.reopen(path, mode_str) => ios
4513 * Reassociates <em>ios</em> with the I/O stream given in
4514 * <i>other_IO</i> or to a new stream opened on <i>path</i>. This may
4515 * dynamically change the actual class of this stream.
4517 * f1 = File.new("testfile")
4518 * f2 = File.new("testfile")
4519 * f2.readlines[0] #=> "This is line one\n"
4520 * f2.reopen(f1) #=> #<File:testfile>
4521 * f2.readlines[0] #=> "This is line one\n"
4524 static VALUE
4525 rb_io_reopen(int argc, VALUE *argv, VALUE file)
4527 VALUE fname, nmode;
4528 const char *mode;
4529 rb_io_t *fptr;
4531 rb_secure(4);
4532 if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
4533 VALUE tmp = rb_io_check_io(fname);
4534 if (!NIL_P(tmp)) {
4535 return io_reopen(file, tmp);
4539 FilePathValue(fname);
4540 rb_io_taint_check(file);
4541 fptr = RFILE(file)->fptr;
4542 if (!fptr) {
4543 fptr = RFILE(file)->fptr = ALLOC(rb_io_t);
4544 MEMZERO(fptr, rb_io_t, 1);
4547 if (!NIL_P(nmode)) {
4548 int flags = rb_io_mode_flags(StringValueCStr(nmode));
4549 if (IS_PREP_STDIO(fptr) &&
4550 ((fptr->mode & FMODE_READWRITE) & (flags & FMODE_READWRITE)) !=
4551 (fptr->mode & FMODE_READWRITE)) {
4552 rb_raise(rb_eArgError,
4553 "%s can't change access mode from \"%s\" to \"%s\"",
4554 PREP_STDIO_NAME(fptr), rb_io_flags_mode(fptr->mode),
4555 rb_io_flags_mode(flags));
4557 fptr->mode = flags;
4558 rb_io_mode_enc(fptr, StringValueCStr(nmode));
4561 if (fptr->path) {
4562 free(fptr->path);
4563 fptr->path = 0;
4566 fptr->path = strdup(StringValueCStr(fname));
4567 mode = rb_io_flags_mode(fptr->mode);
4568 if (fptr->fd < 0) {
4569 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(mode), 0666);
4570 fptr->stdio_file = 0;
4571 return file;
4574 if (fptr->mode & FMODE_WRITABLE) {
4575 io_fflush(fptr);
4577 fptr->rbuf_off = fptr->rbuf_len = 0;
4579 if (fptr->stdio_file) {
4580 if (freopen(fptr->path, mode, fptr->stdio_file) == 0) {
4581 rb_sys_fail(fptr->path);
4583 fptr->fd = fileno(fptr->stdio_file);
4584 #ifdef USE_SETVBUF
4585 if (setvbuf(fptr->stdio_file, NULL, _IOFBF, 0) != 0)
4586 rb_warn("setvbuf() can't be honoured for %s", fptr->path);
4587 #endif
4589 else {
4590 if (close(fptr->fd) < 0)
4591 rb_sys_fail(fptr->path);
4592 fptr->fd = -1;
4593 fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(mode), 0666);
4596 return file;
4599 /* :nodoc: */
4600 static VALUE
4601 rb_io_init_copy(VALUE dest, VALUE io)
4603 rb_io_t *fptr, *orig;
4604 int fd;
4605 VALUE write_io;
4607 io = rb_io_get_io(io);
4608 if (dest == io) return dest;
4609 GetOpenFile(io, orig);
4610 MakeOpenFile(dest, fptr);
4612 rb_io_flush(io);
4614 /* copy rb_io_t structure */
4615 fptr->mode = orig->mode & ~FMODE_PREP;
4616 fptr->pid = orig->pid;
4617 fptr->lineno = orig->lineno;
4618 if (orig->path) fptr->path = strdup(orig->path);
4619 fptr->finalize = orig->finalize;
4621 fd = ruby_dup(orig->fd);
4622 fptr->fd = fd;
4623 io_seek(fptr, io_tell(orig), SEEK_SET);
4624 if (fptr->mode & FMODE_BINMODE) {
4625 rb_io_binmode(dest);
4628 write_io = GetWriteIO(io);
4629 if (io != write_io) {
4630 write_io = rb_obj_dup(write_io);
4631 fptr->tied_io_for_writing = write_io;
4632 rb_ivar_set(dest, rb_intern("@tied_io_for_writing"), write_io);
4635 return dest;
4639 * call-seq:
4640 * ios.printf(format_string [, obj, ...] ) => nil
4642 * Formats and writes to <em>ios</em>, converting parameters under
4643 * control of the format string. See <code>Kernel#sprintf</code>
4644 * for details.
4647 VALUE
4648 rb_io_printf(int argc, VALUE *argv, VALUE out)
4650 rb_io_write(out, rb_f_sprintf(argc, argv));
4651 return Qnil;
4655 * call-seq:
4656 * printf(io, string [, obj ... ] ) => nil
4657 * printf(string [, obj ... ] ) => nil
4659 * Equivalent to:
4660 * io.write(sprintf(string, obj, ...)
4661 * or
4662 * $stdout.write(sprintf(string, obj, ...)
4665 static VALUE
4666 rb_f_printf(int argc, VALUE *argv)
4668 VALUE out;
4670 if (argc == 0) return Qnil;
4671 if (TYPE(argv[0]) == T_STRING) {
4672 out = rb_stdout;
4674 else {
4675 out = argv[0];
4676 argv++;
4677 argc--;
4679 rb_io_write(out, rb_f_sprintf(argc, argv));
4681 return Qnil;
4685 * call-seq:
4686 * ios.print() => nil
4687 * ios.print(obj, ...) => nil
4689 * Writes the given object(s) to <em>ios</em>. The stream must be
4690 * opened for writing. If the output record separator (<code>$\\</code>)
4691 * is not <code>nil</code>, it will be appended to the output. If no
4692 * arguments are given, prints <code>$_</code>. Objects that aren't
4693 * strings will be converted by calling their <code>to_s</code> method.
4694 * With no argument, prints the contents of the variable <code>$_</code>.
4695 * Returns <code>nil</code>.
4697 * $stdout.print("This is ", 100, " percent.\n")
4699 * <em>produces:</em>
4701 * This is 100 percent.
4704 VALUE
4705 rb_io_print(int argc, VALUE *argv, VALUE out)
4707 int i;
4708 VALUE line;
4710 /* if no argument given, print `$_' */
4711 if (argc == 0) {
4712 argc = 1;
4713 line = rb_lastline_get();
4714 argv = &line;
4716 for (i=0; i<argc; i++) {
4717 rb_io_write(out, argv[i]);
4718 if (!NIL_P(rb_output_fs)) {
4719 rb_io_write(out, rb_output_fs);
4722 if (argc > 0 && !NIL_P(rb_output_rs)) {
4723 rb_io_write(out, rb_output_rs);
4726 return Qnil;
4730 * call-seq:
4731 * print(obj, ...) => nil
4733 * Prints each object in turn to <code>$stdout</code>. If the output
4734 * field separator (<code>$,</code>) is not +nil+, its
4735 * contents will appear between each field. If the output record
4736 * separator (<code>$\\</code>) is not +nil+, it will be
4737 * appended to the output. If no arguments are given, prints
4738 * <code>$_</code>. Objects that aren't strings will be converted by
4739 * calling their <code>to_s</code> method.
4741 * print "cat", [1,2,3], 99, "\n"
4742 * $, = ", "
4743 * $\ = "\n"
4744 * print "cat", [1,2,3], 99
4746 * <em>produces:</em>
4748 * cat12399
4749 * cat, 1, 2, 3, 99
4752 static VALUE
4753 rb_f_print(int argc, VALUE *argv)
4755 rb_io_print(argc, argv, rb_stdout);
4756 return Qnil;
4760 * call-seq:
4761 * ios.putc(obj) => obj
4763 * If <i>obj</i> is <code>Numeric</code>, write the character whose
4764 * code is <i>obj</i>, otherwise write the first character of the
4765 * string representation of <i>obj</i> to <em>ios</em>.
4767 * $stdout.putc "A"
4768 * $stdout.putc 65
4770 * <em>produces:</em>
4772 * AA
4775 static VALUE
4776 rb_io_putc(VALUE io, VALUE ch)
4778 char c = NUM2CHR(ch);
4780 rb_io_write(io, rb_str_new(&c, 1));
4781 return ch;
4785 * call-seq:
4786 * putc(int) => int
4788 * Equivalent to:
4790 * $stdout.putc(int)
4793 static VALUE
4794 rb_f_putc(VALUE recv, VALUE ch)
4796 if (recv == rb_stdout) {
4797 return rb_io_putc(recv, ch);
4799 return rb_funcall2(rb_stdout, rb_intern("putc"), 1, &ch);
4802 static VALUE
4803 io_puts_ary(VALUE ary, VALUE out, int recur)
4805 VALUE tmp;
4806 long i;
4808 if (recur) {
4809 tmp = rb_str_new2("[...]");
4810 rb_io_puts(1, &tmp, out);
4811 return Qnil;
4813 for (i=0; i<RARRAY_LEN(ary); i++) {
4814 tmp = RARRAY_PTR(ary)[i];
4815 rb_io_puts(1, &tmp, out);
4817 return Qnil;
4821 * call-seq:
4822 * ios.puts(obj, ...) => nil
4824 * Writes the given objects to <em>ios</em> as with
4825 * <code>IO#print</code>. Writes a record separator (typically a
4826 * newline) after any that do not already end with a newline sequence.
4827 * If called with an array argument, writes each element on a new line.
4828 * If called without arguments, outputs a single record separator.
4830 * $stdout.puts("this", "is", "a", "test")
4832 * <em>produces:</em>
4834 * this
4835 * is
4837 * test
4840 VALUE
4841 rb_io_puts(int argc, VALUE *argv, VALUE out)
4843 int i;
4844 VALUE line;
4846 /* if no argument given, print newline. */
4847 if (argc == 0) {
4848 rb_io_write(out, rb_default_rs);
4849 return Qnil;
4851 for (i=0; i<argc; i++) {
4852 line = rb_check_array_type(argv[i]);
4853 if (!NIL_P(line)) {
4854 rb_exec_recursive(io_puts_ary, line, out);
4855 continue;
4857 line = rb_obj_as_string(argv[i]);
4858 rb_io_write(out, line);
4859 if (RSTRING_LEN(line) == 0 ||
4860 RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') {
4861 rb_io_write(out, rb_default_rs);
4865 return Qnil;
4869 * call-seq:
4870 * puts(obj, ...) => nil
4872 * Equivalent to
4874 * $stdout.puts(obj, ...)
4877 static VALUE
4878 rb_f_puts(int argc, VALUE *argv, VALUE recv)
4880 if (recv == rb_stdout) {
4881 return rb_io_puts(argc, argv, recv);
4883 return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv);
4886 void
4887 rb_p(VALUE obj) /* for debug print within C code */
4889 VALUE str = rb_obj_as_string(rb_inspect(obj));
4890 rb_str_buf_append(str, rb_default_rs);
4891 rb_io_write(rb_stdout, str);
4895 * call-seq:
4896 * p(obj) => obj
4897 * p(obj1, obj2, ...) => [obj, ...]
4898 * p() => nil
4900 * For each object, directly writes
4901 * _obj_.+inspect+ followed by the current output
4902 * record separator to the program's standard output.
4904 * S = Struct.new(:name, :state)
4905 * s = S['dave', 'TX']
4906 * p s
4908 * <em>produces:</em>
4910 * #<S name="dave", state="TX">
4913 static VALUE
4914 rb_f_p(int argc, VALUE *argv, VALUE self)
4916 int i;
4917 VALUE ret = Qnil;
4919 for (i=0; i<argc; i++) {
4920 rb_p(argv[i]);
4922 if (argc == 1) {
4923 ret = argv[0];
4925 else if (argc > 1) {
4926 ret = rb_ary_new4(argc, argv);
4928 if (TYPE(rb_stdout) == T_FILE) {
4929 rb_io_flush(rb_stdout);
4931 return ret;
4935 * call-seq:
4936 * obj.display(port=$>) => nil
4938 * Prints <i>obj</i> on the given port (default <code>$></code>).
4939 * Equivalent to:
4941 * def display(port=$>)
4942 * port.write self
4943 * end
4945 * For example:
4947 * 1.display
4948 * "cat".display
4949 * [ 4, 5, 6 ].display
4950 * puts
4952 * <em>produces:</em>
4954 * 1cat456
4957 static VALUE
4958 rb_obj_display(int argc, VALUE *argv, VALUE self)
4960 VALUE out;
4962 if (argc == 0) {
4963 out = rb_stdout;
4965 else {
4966 rb_scan_args(argc, argv, "01", &out);
4968 rb_io_write(out, self);
4970 return Qnil;
4973 void
4974 rb_write_error2(const char *mesg, long len)
4976 if (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0) {
4977 fwrite(mesg, sizeof(char), len, stderr);
4979 else {
4980 rb_io_write(rb_stderr, rb_str_new(mesg, len));
4984 void
4985 rb_write_error(const char *mesg)
4987 rb_write_error2(mesg, strlen(mesg));
4990 static void
4991 must_respond_to(ID mid, VALUE val, ID id)
4993 if (!rb_respond_to(val, mid)) {
4994 rb_raise(rb_eTypeError, "%s must have %s method, %s given",
4995 rb_id2name(id), rb_id2name(mid),
4996 rb_obj_classname(val));
5000 static void
5001 stdout_setter(VALUE val, ID id, VALUE *variable)
5003 must_respond_to(id_write, val, id);
5004 *variable = val;
5007 static VALUE
5008 prep_io(int fd, int mode, VALUE klass, const char *path)
5010 rb_io_t *fp;
5011 VALUE io = io_alloc(klass);
5013 MakeOpenFile(io, fp);
5014 fp->fd = fd;
5015 #ifdef __CYGWIN__
5016 if (!isatty(fd)) {
5017 mode |= O_BINARY;
5018 setmode(fd, O_BINARY);
5020 #endif
5021 fp->mode = mode;
5022 io_check_tty(fp);
5023 if (path) fp->path = strdup(path);
5025 return io;
5028 VALUE
5029 rb_io_fdopen(int fd, int mode, const char *path)
5031 VALUE klass = rb_cIO;
5033 if (path && strcmp(path, "-")) klass = rb_cFile;
5034 return prep_io(fd, rb_io_modenum_flags(mode), klass, path);
5037 static VALUE
5038 prep_stdio(FILE *f, int mode, VALUE klass, const char *path)
5040 rb_io_t *fptr;
5041 VALUE io = prep_io(fileno(f), mode|FMODE_PREP, klass, path);
5043 GetOpenFile(io, fptr);
5044 fptr->stdio_file = f;
5046 return io;
5049 FILE *
5050 rb_io_stdio_file(rb_io_t *fptr)
5052 if (!fptr->stdio_file) {
5053 fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_flags_mode(fptr->mode));
5055 return fptr->stdio_file;
5059 * call-seq:
5060 * IO.new(fd, mode) => io
5062 * Returns a new <code>IO</code> object (a stream) for the given
5063 * <code>IO</code> object or integer file descriptor and mode
5064 * string. See also <code>IO#fileno</code> and
5065 * <code>IO::for_fd</code>.
5067 * puts IO.new($stdout).fileno # => 1
5069 * a = IO.new(2,"w") # '2' is standard error
5070 * $stderr.puts "Hello"
5071 * a.puts "World"
5073 * <em>produces:</em>
5075 * Hello
5076 * World
5079 static VALUE
5080 rb_io_initialize(int argc, VALUE *argv, VALUE io)
5082 VALUE fnum, mode, orig;
5083 rb_io_t *fp, *ofp = NULL;
5084 int fd, fmode, flags = O_RDONLY;
5086 rb_secure(4);
5087 rb_scan_args(argc, argv, "11", &fnum, &mode);
5088 if (argc == 2) {
5089 if (FIXNUM_P(mode)) {
5090 flags = FIX2LONG(mode);
5092 else {
5093 SafeStringValue(mode);
5094 flags = rb_io_mode_modenum(StringValueCStr(mode));
5097 orig = rb_io_check_io(fnum);
5098 if (NIL_P(orig)) {
5099 fd = NUM2INT(fnum);
5100 UPDATE_MAXFD(fd);
5101 if (argc != 2) {
5102 #if defined(HAVE_FCNTL) && defined(F_GETFL)
5103 flags = fcntl(fd, F_GETFL);
5104 if (flags == -1) rb_sys_fail(0);
5105 #endif
5107 MakeOpenFile(io, fp);
5108 fp->fd = fd;
5109 fp->mode = rb_io_modenum_flags(flags);
5110 io_check_tty(fp);
5112 else if (RFILE(io)->fptr) {
5113 rb_raise(rb_eRuntimeError, "reinitializing IO");
5115 else {
5116 GetOpenFile(orig, ofp);
5117 if (ofp->refcnt == LONG_MAX) {
5118 VALUE s = rb_inspect(orig);
5119 rb_raise(rb_eIOError, "too many shared IO for %s", StringValueCStr(s));
5121 if (argc == 2) {
5122 fmode = rb_io_modenum_flags(flags);
5123 if ((ofp->mode ^ fmode) & (FMODE_READWRITE|FMODE_BINMODE)) {
5124 if (FIXNUM_P(mode)) {
5125 rb_raise(rb_eArgError, "incompatible mode 0%o", flags);
5127 else {
5128 rb_raise(rb_eArgError, "incompatible mode \"%s\"", RSTRING_PTR(mode));
5132 ofp->refcnt++;
5133 RFILE(io)->fptr = ofp;
5136 return io;
5141 * call-seq:
5142 * File.new(filename, mode="r") => file
5143 * File.new(filename [, mode [, perm]]) => file
5146 * Opens the file named by _filename_ according to
5147 * _mode_ (default is ``r'') and returns a new
5148 * <code>File</code> object. See the description of class +IO+ for
5149 * a description of _mode_. The file mode may optionally be
5150 * specified as a +Fixnum+ by _or_-ing together the
5151 * flags (O_RDONLY etc, again described under +IO+). Optional
5152 * permission bits may be given in _perm_. These mode and permission
5153 * bits are platform dependent; on Unix systems, see
5154 * <code>open(2)</code> for details.
5156 * f = File.new("testfile", "r")
5157 * f = File.new("newfile", "w+")
5158 * f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
5161 static VALUE
5162 rb_file_initialize(int argc, VALUE *argv, VALUE io)
5164 if (RFILE(io)->fptr) {
5165 rb_raise(rb_eRuntimeError, "reinitializing File");
5167 if (0 < argc && argc < 3) {
5168 VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
5170 if (!NIL_P(fd)) {
5171 argv[0] = fd;
5172 return rb_io_initialize(argc, argv, io);
5175 rb_open_file(argc, argv, io);
5177 return io;
5181 * call-seq:
5182 * IO.new(fd, mode_string) => io
5184 * Returns a new <code>IO</code> object (a stream) for the given
5185 * integer file descriptor and mode string. See also
5186 * <code>IO#fileno</code> and <code>IO::for_fd</code>.
5188 * a = IO.new(2,"w") # '2' is standard error
5189 * $stderr.puts "Hello"
5190 * a.puts "World"
5192 * <em>produces:</em>
5194 * Hello
5195 * World
5198 static VALUE
5199 rb_io_s_new(int argc, VALUE *argv, VALUE klass)
5201 if (rb_block_given_p()) {
5202 const char *cname = rb_class2name(klass);
5204 rb_warn("%s::new() does not take block; use %s::open() instead",
5205 cname, cname);
5207 return rb_class_new_instance(argc, argv, klass);
5212 * call-seq:
5213 * IO.for_fd(fd, mode) => io
5215 * Synonym for <code>IO::new</code>.
5219 static VALUE
5220 rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
5222 VALUE io = rb_obj_alloc(klass);
5223 rb_io_initialize(argc, argv, io);
5224 return io;
5227 static void
5228 argf_mark(void *ptr)
5230 struct argf *p = ptr;
5231 rb_gc_mark(p->filename);
5232 rb_gc_mark(p->current_file);
5233 rb_gc_mark(p->lineno);
5234 rb_gc_mark(p->argv);
5237 static void
5238 argf_free(void *ptr)
5240 struct argf *p = ptr;
5241 free(p->inplace);
5244 static inline void
5245 argf_init(struct argf *p, VALUE v)
5247 p->filename = Qnil;
5248 p->current_file = Qnil;
5249 p->lineno = Qnil;
5250 p->argv = v;
5253 static VALUE
5254 argf_alloc(VALUE klass)
5256 struct argf *p;
5257 VALUE argf = Data_Make_Struct(klass, struct argf, argf_mark, argf_free, p);
5259 argf_init(p, Qnil);
5260 return argf;
5263 #undef rb_argv
5264 #define filename ARGF.filename
5265 #define current_file ARGF.current_file
5266 #define gets_lineno ARGF.gets_lineno
5267 #define init_p ARGF.init_p
5268 #define next_p ARGF.next_p
5269 #define lineno ARGF.lineno
5270 #define ruby_inplace_mode ARGF.inplace
5271 #define argf_binmode ARGF.binmode
5272 #define argf_enc ARGF.enc
5273 #define argf_enc2 ARGF.enc2
5274 #define rb_argv ARGF.argv
5276 static VALUE
5277 argf_initialize(VALUE argf, VALUE argv)
5279 memset(&ARGF, 0, sizeof(ARGF));
5280 argf_init(&ARGF, argv);
5282 return argf;
5285 static VALUE
5286 argf_initialize_copy(VALUE argf, VALUE orig)
5288 ARGF = argf_of(orig);
5289 rb_argv = rb_obj_dup(rb_argv);
5290 if (ARGF.inplace) {
5291 const char *inplace = ARGF.inplace;
5292 ARGF.inplace = 0;
5293 ARGF.inplace = ruby_strdup(inplace);
5295 return argf;
5298 static VALUE
5299 argf_set_lineno(VALUE argf, VALUE val)
5301 gets_lineno = NUM2INT(val);
5302 lineno = INT2FIX(gets_lineno);
5303 return Qnil;
5306 static VALUE
5307 argf_lineno(VALUE argf)
5309 return lineno;
5312 static VALUE
5313 argf_forward(int argc, VALUE *argv, VALUE argf)
5315 return rb_funcall3(current_file, rb_frame_this_func(), argc, argv);
5318 #define next_argv() argf_next_argv(argf)
5319 #define ARGF_GENERIC_INPUT_P() \
5320 (current_file == rb_stdin && TYPE(current_file) != T_FILE)
5321 #define ARGF_FORWARD(argc, argv) do {\
5322 if (ARGF_GENERIC_INPUT_P())\
5323 return argf_forward(argc, argv, argf);\
5324 } while (0)
5325 #define NEXT_ARGF_FORWARD(argc, argv) do {\
5326 if (!next_argv()) return Qnil;\
5327 ARGF_FORWARD(argc, argv);\
5328 } while (0)
5330 static void
5331 argf_close(VALUE file)
5333 rb_funcall3(file, rb_intern("close"), 0, 0);
5336 static int
5337 argf_next_argv(VALUE argf)
5339 char *fn;
5340 rb_io_t *fptr;
5341 int stdout_binmode = 0;
5343 if (TYPE(rb_stdout) == T_FILE) {
5344 GetOpenFile(rb_stdout, fptr);
5345 if (fptr->mode & FMODE_BINMODE)
5346 stdout_binmode = 1;
5349 if (init_p == 0) {
5350 if (!NIL_P(rb_argv) && RARRAY_LEN(rb_argv) > 0) {
5351 next_p = 1;
5353 else {
5354 next_p = -1;
5356 init_p = 1;
5357 gets_lineno = 0;
5360 if (next_p == 1) {
5361 next_p = 0;
5362 retry:
5363 if (RARRAY_LEN(rb_argv) > 0) {
5364 filename = rb_ary_shift(rb_argv);
5365 fn = StringValueCStr(filename);
5366 if (strlen(fn) == 1 && fn[0] == '-') {
5367 current_file = rb_stdin;
5368 if (ruby_inplace_mode) {
5369 rb_warn("Can't do inplace edit for stdio; skipping");
5370 goto retry;
5373 else {
5374 int fr = rb_sysopen(fn, O_RDONLY, 0);
5376 if (ruby_inplace_mode) {
5377 struct stat st;
5378 #ifndef NO_SAFE_RENAME
5379 struct stat st2;
5380 #endif
5381 VALUE str;
5382 int fw;
5384 if (TYPE(rb_stdout) == T_FILE && rb_stdout != orig_stdout) {
5385 rb_io_close(rb_stdout);
5387 fstat(fr, &st);
5388 if (*ruby_inplace_mode) {
5389 str = rb_str_new2(fn);
5390 #ifdef NO_LONG_FNAME
5391 ruby_add_suffix(str, ruby_inplace_mode);
5392 #else
5393 rb_str_cat2(str, ruby_inplace_mode);
5394 #endif
5395 #ifdef NO_SAFE_RENAME
5396 (void)close(fr);
5397 (void)unlink(RSTRING_PTR(str));
5398 (void)rename(fn, RSTRING_PTR(str));
5399 fr = rb_sysopen(RSTRING_PTR(str), O_RDONLY, 0);
5400 #else
5401 if (rename(fn, RSTRING_PTR(str)) < 0) {
5402 rb_warn("Can't rename %s to %s: %s, skipping file",
5403 fn, RSTRING_PTR(str), strerror(errno));
5404 close(fr);
5405 goto retry;
5407 #endif
5409 else {
5410 #ifdef NO_SAFE_RENAME
5411 rb_fatal("Can't do inplace edit without backup");
5412 #else
5413 if (unlink(fn) < 0) {
5414 rb_warn("Can't remove %s: %s, skipping file",
5415 fn, strerror(errno));
5416 close(fr);
5417 goto retry;
5419 #endif
5421 fw = rb_sysopen(fn, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5422 #ifndef NO_SAFE_RENAME
5423 fstat(fw, &st2);
5424 #ifdef HAVE_FCHMOD
5425 fchmod(fw, st.st_mode);
5426 #else
5427 chmod(fn, st.st_mode);
5428 #endif
5429 if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
5430 fchown(fw, st.st_uid, st.st_gid);
5432 #endif
5433 rb_stdout = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
5434 if (stdout_binmode) rb_io_binmode(rb_stdout);
5436 current_file = prep_io(fr, FMODE_READABLE, rb_cFile, fn);
5438 if (argf_binmode) rb_io_binmode(current_file);
5439 if (argf_enc) {
5440 rb_io_t *fptr;
5442 GetOpenFile(current_file, fptr);
5443 fptr->enc = argf_enc;
5444 fptr->enc2 = argf_enc2;
5447 else {
5448 next_p = 1;
5449 return Qfalse;
5452 else if (next_p == -1) {
5453 current_file = rb_stdin;
5454 filename = rb_str_new2("-");
5455 if (ruby_inplace_mode) {
5456 rb_warn("Can't do inplace edit for stdio");
5457 rb_stdout = orig_stdout;
5460 return Qtrue;
5463 static VALUE
5464 argf_getline(int argc, VALUE *argv, VALUE argf)
5466 VALUE line;
5468 retry:
5469 if (!next_argv()) return Qnil;
5470 if (ARGF_GENERIC_INPUT_P()) {
5471 line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
5473 else {
5474 if (argc == 0 && rb_rs == rb_default_rs) {
5475 line = rb_io_gets(current_file);
5477 else {
5478 line = rb_io_getline(argc, argv, current_file);
5480 if (NIL_P(line) && next_p != -1) {
5481 argf_close(current_file);
5482 next_p = 1;
5483 goto retry;
5486 if (!NIL_P(line)) {
5487 gets_lineno++;
5488 lineno = INT2FIX(gets_lineno);
5490 return line;
5493 static VALUE
5494 argf_lineno_getter(ID id, VALUE *var)
5496 VALUE argf = *var;
5497 return lineno;
5500 static void
5501 argf_lineno_setter(VALUE val, ID id, VALUE *var)
5503 VALUE argf = *var;
5504 int n = NUM2INT(val);
5505 gets_lineno = n;
5506 lineno = INT2FIX(n);
5509 static VALUE argf_gets(int, VALUE *, VALUE);
5512 * call-seq:
5513 * gets(sep=$/) => string or nil
5514 * gets(limit) => string or nil
5515 * gets(sep,limit) => string or nil
5517 * Returns (and assigns to <code>$_</code>) the next line from the list
5518 * of files in +ARGV+ (or <code>$*</code>), or from standard input if
5519 * no files are present on the command line. Returns +nil+ at end of
5520 * file. The optional argument specifies the record separator. The
5521 * separator is included with the contents of each record. A separator
5522 * of +nil+ reads the entire contents, and a zero-length separator
5523 * reads the input one paragraph at a time, where paragraphs are
5524 * divided by two consecutive newlines. If the first argument is an
5525 * integer, or optional second argument is given, the returning string
5526 * would not be longer than the given value. If multiple filenames are
5527 * present in +ARGV+, +gets(nil)+ will read the contents one file at a
5528 * time.
5530 * ARGV << "testfile"
5531 * print while gets
5533 * <em>produces:</em>
5535 * This is line one
5536 * This is line two
5537 * This is line three
5538 * And so on...
5540 * The style of programming using <code>$_</code> as an implicit
5541 * parameter is gradually losing favor in the Ruby community.
5544 static VALUE
5545 rb_f_gets(int argc, VALUE *argv, VALUE recv)
5547 if (recv == argf) {
5548 return argf_gets(argc, argv, argf);
5550 return rb_funcall2(argf, rb_intern("gets"), argc, argv);
5553 static VALUE
5554 argf_gets(int argc, VALUE *argv, VALUE argf)
5556 VALUE line;
5558 line = argf_getline(argc, argv, argf);
5559 rb_lastline_set(line);
5560 return line;
5563 VALUE
5564 rb_gets(void)
5566 VALUE line;
5568 if (rb_rs != rb_default_rs) {
5569 return rb_f_gets(0, 0, argf);
5572 retry:
5573 if (!next_argv()) return Qnil;
5574 line = rb_io_gets(current_file);
5575 if (NIL_P(line) && next_p != -1) {
5576 rb_io_close(current_file);
5577 next_p = 1;
5578 goto retry;
5580 rb_lastline_set(line);
5581 if (!NIL_P(line)) {
5582 gets_lineno++;
5583 lineno = INT2FIX(gets_lineno);
5586 return line;
5589 static VALUE argf_readline(int, VALUE *, VALUE);
5592 * call-seq:
5593 * readline(sep=$/) => string
5594 * readline(limit) => string
5595 * readline(sep, limit) => string
5597 * Equivalent to <code>Kernel::gets</code>, except
5598 * +readline+ raises +EOFError+ at end of file.
5601 static VALUE
5602 rb_f_readline(int argc, VALUE *argv, VALUE recv)
5604 if (recv == argf) {
5605 return argf_readline(argc, argv, argf);
5607 return rb_funcall2(argf, rb_intern("readline"), argc, argv);
5610 static VALUE
5611 argf_readline(int argc, VALUE *argv, VALUE argf)
5613 VALUE line;
5615 if (!next_argv()) rb_eof_error();
5616 ARGF_FORWARD(argc, argv);
5617 line = argf_gets(argc, argv, argf);
5618 if (NIL_P(line)) {
5619 rb_eof_error();
5622 return line;
5625 static VALUE argf_readlines(int, VALUE *, VALUE);
5628 * call-seq:
5629 * readlines(sep=$/) => array
5630 * readlines(limit) => array
5631 * readlines(sep,limit) => array
5633 * Returns an array containing the lines returned by calling
5634 * <code>Kernel.gets(<i>sep</i>)</code> until the end of file.
5637 static VALUE
5638 rb_f_readlines(int argc, VALUE *argv, VALUE recv)
5640 if (recv == argf) {
5641 return argf_readlines(argc, argv, argf);
5643 return rb_funcall2(argf, rb_intern("readlines"), argc, argv);
5646 static VALUE
5647 argf_readlines(int argc, VALUE *argv, VALUE argf)
5649 VALUE line, ary;
5651 ary = rb_ary_new();
5652 while (!NIL_P(line = argf_getline(argc, argv, argf))) {
5653 rb_ary_push(ary, line);
5656 return ary;
5660 * call-seq:
5661 * `cmd` => string
5663 * Returns the standard output of running _cmd_ in a subshell.
5664 * The built-in syntax <code>%x{...}</code> uses
5665 * this method. Sets <code>$?</code> to the process status.
5667 * `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
5668 * `ls testdir`.split[1] #=> "main.rb"
5669 * `echo oops && exit 99` #=> "oops\n"
5670 * $?.exitstatus #=> 99
5673 static VALUE
5674 rb_f_backquote(VALUE obj, VALUE str)
5676 volatile VALUE port;
5677 VALUE result;
5678 rb_io_t *fptr;
5680 SafeStringValue(str);
5681 port = pipe_open_s(str, "r");
5682 if (NIL_P(port)) return rb_str_new(0,0);
5684 GetOpenFile(port, fptr);
5685 result = read_all(fptr, remain_size(fptr), Qnil);
5686 rb_io_close(port);
5688 return result;
5691 #ifdef HAVE_SYS_SELECT_H
5692 #include <sys/select.h>
5693 #endif
5695 static VALUE
5696 select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fdset_t *fds)
5698 VALUE res, list;
5699 fd_set *rp, *wp, *ep;
5700 rb_io_t *fptr;
5701 long i;
5702 int max = 0, n;
5703 int interrupt_flag = 0;
5704 int pending = 0;
5705 struct timeval timerec;
5707 if (!NIL_P(read)) {
5708 Check_Type(read, T_ARRAY);
5709 for (i=0; i<RARRAY_LEN(read); i++) {
5710 GetOpenFile(rb_io_get_io(RARRAY_PTR(read)[i]), fptr);
5711 rb_fd_set(fptr->fd, &fds[0]);
5712 if (READ_DATA_PENDING(fptr)) { /* check for buffered data */
5713 pending++;
5714 rb_fd_set(fptr->fd, &fds[3]);
5716 if (max < fptr->fd) max = fptr->fd;
5718 if (pending) { /* no blocking if there's buffered data */
5719 timerec.tv_sec = timerec.tv_usec = 0;
5720 tp = &timerec;
5722 rp = rb_fd_ptr(&fds[0]);
5724 else
5725 rp = 0;
5727 if (!NIL_P(write)) {
5728 Check_Type(write, T_ARRAY);
5729 for (i=0; i<RARRAY_LEN(write); i++) {
5730 VALUE write_io = GetWriteIO(rb_io_get_io(RARRAY_PTR(write)[i]));
5731 GetOpenFile(write_io, fptr);
5732 rb_fd_set(fptr->fd, &fds[1]);
5733 if (max < fptr->fd) max = fptr->fd;
5735 wp = rb_fd_ptr(&fds[1]);
5737 else
5738 wp = 0;
5740 if (!NIL_P(except)) {
5741 Check_Type(except, T_ARRAY);
5742 for (i=0; i<RARRAY_LEN(except); i++) {
5743 VALUE io = rb_io_get_io(RARRAY_PTR(except)[i]);
5744 VALUE write_io = GetWriteIO(io);
5745 GetOpenFile(io, fptr);
5746 rb_fd_set(fptr->fd, &fds[2]);
5747 if (max < fptr->fd) max = fptr->fd;
5748 if (io != write_io) {
5749 GetOpenFile(write_io, fptr);
5750 rb_fd_set(fptr->fd, &fds[2]);
5751 if (max < fptr->fd) max = fptr->fd;
5754 ep = rb_fd_ptr(&fds[2]);
5756 else {
5757 ep = 0;
5760 max++;
5762 n = rb_thread_select(max, rp, wp, ep, tp);
5763 if (n < 0) {
5764 rb_sys_fail(0);
5766 if (!pending && n == 0) return Qnil; /* returns nil on timeout */
5768 res = rb_ary_new2(3);
5769 rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
5770 rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
5771 rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
5773 if (interrupt_flag == 0) {
5774 if (rp) {
5775 list = RARRAY_PTR(res)[0];
5776 for (i=0; i< RARRAY_LEN(read); i++) {
5777 VALUE obj = rb_ary_entry(read, i);
5778 VALUE io = rb_io_get_io(obj);
5779 GetOpenFile(io, fptr);
5780 if (rb_fd_isset(fptr->fd, &fds[0]) ||
5781 rb_fd_isset(fptr->fd, &fds[3])) {
5782 rb_ary_push(list, obj);
5787 if (wp) {
5788 list = RARRAY_PTR(res)[1];
5789 for (i=0; i< RARRAY_LEN(write); i++) {
5790 VALUE obj = rb_ary_entry(write, i);
5791 VALUE io = rb_io_get_io(obj);
5792 VALUE write_io = GetWriteIO(io);
5793 GetOpenFile(write_io, fptr);
5794 if (rb_fd_isset(fptr->fd, &fds[1])) {
5795 rb_ary_push(list, obj);
5800 if (ep) {
5801 list = RARRAY_PTR(res)[2];
5802 for (i=0; i< RARRAY_LEN(except); i++) {
5803 VALUE obj = rb_ary_entry(except, i);
5804 VALUE io = rb_io_get_io(obj);
5805 VALUE write_io = GetWriteIO(io);
5806 GetOpenFile(io, fptr);
5807 if (rb_fd_isset(fptr->fd, &fds[2])) {
5808 rb_ary_push(list, obj);
5810 else if (io != write_io) {
5811 GetOpenFile(write_io, fptr);
5812 if (rb_fd_isset(fptr->fd, &fds[2])) {
5813 rb_ary_push(list, obj);
5820 return res; /* returns an empty array on interrupt */
5823 struct select_args {
5824 VALUE read, write, except;
5825 struct timeval *timeout;
5826 rb_fdset_t fdsets[4];
5829 #ifdef HAVE_RB_FD_INIT
5830 static VALUE
5831 select_call(VALUE arg)
5833 struct select_args *p = (struct select_args *)arg;
5835 return select_internal(p->read, p->write, p->except, p->timeout, p->fdsets);
5838 static VALUE
5839 select_end(VALUE arg)
5841 struct select_args *p = (struct select_args *)arg;
5842 int i;
5844 for (i = 0; i < sizeof(p->fdsets) / sizeof(p->fdsets[0]); ++i)
5845 rb_fd_term(&p->fdsets[i]);
5846 return Qnil;
5848 #endif
5851 * call-seq:
5852 * IO.select(read_array
5853 * [, write_array
5854 * [, error_array
5855 * [, timeout]]] ) => array or nil
5857 * See <code>Kernel#select</code>.
5860 static VALUE
5861 rb_f_select(int argc, VALUE *argv, VALUE obj)
5863 VALUE timeout;
5864 struct select_args args;
5865 struct timeval timerec;
5866 int i;
5868 rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
5869 if (NIL_P(timeout)) {
5870 args.timeout = 0;
5872 else {
5873 timerec = rb_time_interval(timeout);
5874 args.timeout = &timerec;
5877 for (i = 0; i < sizeof(args.fdsets) / sizeof(args.fdsets[0]); ++i)
5878 rb_fd_init(&args.fdsets[i]);
5880 #ifdef HAVE_RB_FD_INIT
5881 return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
5882 #else
5883 return select_internal(args.read, args.write, args.except,
5884 args.timeout, args.fdsets);
5885 #endif
5889 #if !defined(MSDOS) && !defined(__human68k__)
5890 static int
5891 io_cntl(int fd, int cmd, long narg, int io_p)
5893 int retval;
5895 #ifdef HAVE_FCNTL
5896 TRAP_BEG;
5897 # if defined(__CYGWIN__)
5898 retval = io_p?ioctl(fd, cmd, (void*)narg):fcntl(fd, cmd, narg);
5899 # else
5900 retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
5901 # endif
5902 TRAP_END;
5903 #else
5904 if (!io_p) {
5905 rb_notimplement();
5907 TRAP_BEG;
5908 retval = ioctl(fd, cmd, narg);
5909 TRAP_END;
5910 #endif
5911 return retval;
5913 #endif
5915 static VALUE
5916 rb_io_ctl(VALUE io, VALUE req, VALUE arg, int io_p)
5918 #if !defined(MSDOS) && !defined(__human68k__)
5919 int cmd = NUM2ULONG(req);
5920 rb_io_t *fptr;
5921 long len = 0;
5922 long narg = 0;
5923 int retval;
5925 rb_secure(2);
5927 if (NIL_P(arg) || arg == Qfalse) {
5928 narg = 0;
5930 else if (FIXNUM_P(arg)) {
5931 narg = FIX2LONG(arg);
5933 else if (arg == Qtrue) {
5934 narg = 1;
5936 else {
5937 VALUE tmp = rb_check_string_type(arg);
5939 if (NIL_P(tmp)) {
5940 narg = NUM2LONG(arg);
5942 else {
5943 arg = tmp;
5944 #ifdef IOCPARM_MASK
5945 #ifndef IOCPARM_LEN
5946 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
5947 #endif
5948 #endif
5949 #ifdef IOCPARM_LEN
5950 len = IOCPARM_LEN(cmd); /* on BSDish systems we're safe */
5951 #else
5952 len = 256; /* otherwise guess at what's safe */
5953 #endif
5954 rb_str_modify(arg);
5956 if (len <= RSTRING_LEN(arg)) {
5957 len = RSTRING_LEN(arg);
5959 if (RSTRING_LEN(arg) < len) {
5960 rb_str_resize(arg, len+1);
5962 RSTRING_PTR(arg)[len] = 17; /* a little sanity check here */
5963 narg = (long)RSTRING_PTR(arg);
5966 GetOpenFile(io, fptr);
5967 retval = io_cntl(fptr->fd, cmd, narg, io_p);
5968 if (retval < 0) rb_sys_fail(fptr->path);
5969 if (TYPE(arg) == T_STRING && RSTRING_PTR(arg)[len] != 17) {
5970 rb_raise(rb_eArgError, "return value overflowed string");
5973 if (!io_p && cmd == F_SETFL) {
5974 if (narg & O_NONBLOCK) {
5975 fptr->mode |= FMODE_WSPLIT_INITIALIZED;
5976 fptr->mode &= ~FMODE_WSPLIT;
5978 else {
5979 fptr->mode &= ~(FMODE_WSPLIT_INITIALIZED|FMODE_WSPLIT);
5983 return INT2NUM(retval);
5984 #else
5985 rb_notimplement();
5986 return Qnil; /* not reached */
5987 #endif
5992 * call-seq:
5993 * ios.ioctl(integer_cmd, arg) => integer
5995 * Provides a mechanism for issuing low-level commands to control or
5996 * query I/O devices. Arguments and results are platform dependent. If
5997 * <i>arg</i> is a number, its value is passed directly. If it is a
5998 * string, it is interpreted as a binary sequence of bytes. On Unix
5999 * platforms, see <code>ioctl(2)</code> for details. Not implemented on
6000 * all platforms.
6003 static VALUE
6004 rb_io_ioctl(int argc, VALUE *argv, VALUE io)
6006 VALUE req, arg;
6008 rb_scan_args(argc, argv, "11", &req, &arg);
6009 return rb_io_ctl(io, req, arg, 1);
6013 * call-seq:
6014 * ios.fcntl(integer_cmd, arg) => integer
6016 * Provides a mechanism for issuing low-level commands to control or
6017 * query file-oriented I/O streams. Arguments and results are platform
6018 * dependent. If <i>arg</i> is a number, its value is passed
6019 * directly. If it is a string, it is interpreted as a binary sequence
6020 * of bytes (<code>Array#pack</code> might be a useful way to build this
6021 * string). On Unix platforms, see <code>fcntl(2)</code> for details.
6022 * Not implemented on all platforms.
6025 static VALUE
6026 rb_io_fcntl(int argc, VALUE *argv, VALUE io)
6028 #ifdef HAVE_FCNTL
6029 VALUE req, arg;
6031 rb_scan_args(argc, argv, "11", &req, &arg);
6032 return rb_io_ctl(io, req, arg, 0);
6033 #else
6034 rb_notimplement();
6035 return Qnil; /* not reached */
6036 #endif
6040 * call-seq:
6041 * syscall(fixnum [, args...]) => integer
6043 * Calls the operating system function identified by _fixnum_,
6044 * passing in the arguments, which must be either +String+
6045 * objects, or +Integer+ objects that ultimately fit within
6046 * a native +long+. Up to nine parameters may be passed (14
6047 * on the Atari-ST). The function identified by _fixnum_ is system
6048 * dependent. On some Unix systems, the numbers may be obtained from a
6049 * header file called <code>syscall.h</code>.
6051 * syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
6053 * <em>produces:</em>
6055 * hello
6058 static VALUE
6059 rb_f_syscall(int argc, VALUE *argv)
6061 #if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
6062 #ifdef atarist
6063 unsigned long arg[14]; /* yes, we really need that many ! */
6064 #else
6065 unsigned long arg[8];
6066 #endif
6067 int retval = -1;
6068 int i = 1;
6069 int items = argc - 1;
6071 /* This probably won't work on machines where sizeof(long) != sizeof(int)
6072 * or where sizeof(long) != sizeof(char*). But such machines will
6073 * not likely have syscall implemented either, so who cares?
6076 rb_secure(2);
6077 if (argc == 0)
6078 rb_raise(rb_eArgError, "too few arguments for syscall");
6079 if (argc > sizeof(arg) / sizeof(arg[0]))
6080 rb_raise(rb_eArgError, "too many arguments for syscall");
6081 arg[0] = NUM2LONG(argv[0]); argv++;
6082 while (items--) {
6083 VALUE v = rb_check_string_type(*argv);
6085 if (!NIL_P(v)) {
6086 StringValue(v);
6087 rb_str_modify(v);
6088 arg[i] = (unsigned long)StringValueCStr(v);
6090 else {
6091 arg[i] = (unsigned long)NUM2LONG(*argv);
6093 argv++;
6094 i++;
6096 TRAP_BEG;
6097 switch (argc) {
6098 case 1:
6099 retval = syscall(arg[0]);
6100 break;
6101 case 2:
6102 retval = syscall(arg[0],arg[1]);
6103 break;
6104 case 3:
6105 retval = syscall(arg[0],arg[1],arg[2]);
6106 break;
6107 case 4:
6108 retval = syscall(arg[0],arg[1],arg[2],arg[3]);
6109 break;
6110 case 5:
6111 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4]);
6112 break;
6113 case 6:
6114 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
6115 break;
6116 case 7:
6117 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
6118 break;
6119 case 8:
6120 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6121 arg[7]);
6122 break;
6123 #ifdef atarist
6124 case 9:
6125 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6126 arg[7], arg[8]);
6127 break;
6128 case 10:
6129 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6130 arg[7], arg[8], arg[9]);
6131 break;
6132 case 11:
6133 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6134 arg[7], arg[8], arg[9], arg[10]);
6135 break;
6136 case 12:
6137 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6138 arg[7], arg[8], arg[9], arg[10], arg[11]);
6139 break;
6140 case 13:
6141 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6142 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
6143 break;
6144 case 14:
6145 retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
6146 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12], arg[13]);
6147 break;
6148 #endif /* atarist */
6150 TRAP_END;
6151 if (retval < 0) rb_sys_fail(0);
6152 return INT2NUM(retval);
6153 #else
6154 rb_notimplement();
6155 return Qnil; /* not reached */
6156 #endif
6159 static VALUE
6160 io_new_instance(VALUE args)
6162 return rb_class_new_instance(2, (VALUE*)args+1, *(VALUE*)args);
6165 static void
6166 io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2)
6168 if (NIL_P(v2)) argc = 1;
6169 if (argc == 2) {
6170 fptr->enc2 = rb_to_encoding(v1);
6171 fptr->enc = rb_to_encoding(v2);
6173 else if (argc == 1) {
6174 if (NIL_P(v1)) {
6175 fptr->enc = 0;
6177 else {
6178 VALUE tmp = rb_check_string_type(v1);
6179 if (!NIL_P(tmp)) {
6180 mode_enc(fptr, StringValueCStr(tmp));
6182 else {
6183 fptr->enc = rb_to_encoding(v1);
6190 * call-seq:
6191 * IO.pipe -> [read_io, write_io]
6192 * IO.pipe(ext_enc) -> [read_io, write_io]
6193 * IO.pipe("ext_enc:int_enc") -> [read_io, write_io]
6194 * IO.pipe(ext_enc, int_enc) -> [read_io, write_io]
6196 * Creates a pair of pipe endpoints (connected to each other) and
6197 * returns them as a two-element array of <code>IO</code> objects:
6198 * <code>[</code> <i>read_io</i>, <i>write_io</i> <code>]</code>. Not
6199 * available on all platforms.
6201 * If an encoding (encoding name or encoding object) is specified as an optional argument,
6202 * read string from pipe is tagged with the encoding specified.
6203 * If the argument is a colon separated two encoding names "A:B",
6204 * the read string is converted from encoding A (external encoding)
6205 * to encoding B (internal encoding), then tagged with B.
6206 * If two optional arguments are specified, those must be
6207 * encoding objects or encoding names,
6208 * and the first one is the external encoding,
6209 * and the second one is the internal encoding.
6211 * In the example below, the two processes close the ends of the pipe
6212 * that they are not using. This is not just a cosmetic nicety. The
6213 * read end of a pipe will not generate an end of file condition if
6214 * there are any writers with the pipe still open. In the case of the
6215 * parent process, the <code>rd.read</code> will never return if it
6216 * does not first issue a <code>wr.close</code>.
6218 * rd, wr = IO.pipe
6220 * if fork
6221 * wr.close
6222 * puts "Parent got: <#{rd.read}>"
6223 * rd.close
6224 * Process.wait
6225 * else
6226 * rd.close
6227 * puts "Sending message to parent"
6228 * wr.write "Hi Dad"
6229 * wr.close
6230 * end
6232 * <em>produces:</em>
6234 * Sending message to parent
6235 * Parent got: <Hi Dad>
6238 static VALUE
6239 rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
6241 #ifdef __human68k__
6242 rb_notimplement();
6243 return Qnil; /* not reached */
6244 #else
6245 int pipes[2], state;
6246 VALUE r, w, args[3], v1, v2;
6247 rb_io_t *fptr;
6249 rb_scan_args(argc, argv, "02", &v1, &v2);
6250 if (rb_pipe(pipes) == -1)
6251 rb_sys_fail(0);
6253 args[0] = klass;
6254 args[1] = INT2NUM(pipes[0]);
6255 args[2] = INT2FIX(O_RDONLY);
6256 r = rb_protect(io_new_instance, (VALUE)args, &state);
6257 if (state) {
6258 close(pipes[0]);
6259 close(pipes[1]);
6260 rb_jump_tag(state);
6262 GetOpenFile(r, fptr);
6263 io_encoding_set(fptr, argc, v1, v2);
6264 args[1] = INT2NUM(pipes[1]);
6265 args[2] = INT2FIX(O_WRONLY);
6266 w = rb_protect(io_new_instance, (VALUE)args, &state);
6267 if (state) {
6268 close(pipes[1]);
6269 if (!NIL_P(r)) rb_io_close(r);
6270 rb_jump_tag(state);
6272 rb_io_synchronized(RFILE(w)->fptr);
6274 return rb_assoc_new(r, w);
6275 #endif
6278 struct foreach_arg {
6279 int argc;
6280 VALUE *argv;
6281 VALUE io;
6284 static void
6285 open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
6287 VALUE opt, v;
6289 FilePathValue(argv[0]);
6290 arg->io = 0;
6291 arg->argc = argc > 1 ? 1 : 0;
6292 arg->argv = argv + 1;
6293 if (argc == 1) {
6294 no_key:
6295 arg->io = rb_io_open(RSTRING_PTR(argv[0]), "r");
6296 return;
6298 opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
6299 if (NIL_P(opt)) goto no_key;
6300 if (argc > 2) arg->argc = 1;
6301 else arg->argc = 0;
6303 v = rb_hash_aref(opt, sym_open_args);
6304 if (!NIL_P(v)) {
6305 VALUE args;
6307 v = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
6308 args = rb_ary_new2(RARRAY_LEN(v)+1);
6309 rb_ary_push(args, argv[0]);
6310 rb_ary_concat(args, v);
6311 MEMCPY(RARRAY_PTR(args)+1, RARRAY_PTR(v), VALUE, RARRAY_LEN(v));
6313 arg->io = rb_io_open_with_args(RARRAY_LEN(args), RARRAY_PTR(args));
6314 return;
6316 v = rb_hash_aref(opt, sym_mode);
6317 if (!NIL_P(v)) {
6318 arg->io = rb_io_open(RSTRING_PTR(argv[0]), StringValueCStr(v));
6320 else {
6321 arg->io = rb_io_open(RSTRING_PTR(argv[0]), "r");
6324 io_set_encoding(arg->io, opt);
6327 static VALUE
6328 io_s_foreach(struct foreach_arg *arg)
6330 VALUE str;
6332 while (!NIL_P(str = rb_io_gets_m(arg->argc, arg->argv, arg->io))) {
6333 rb_yield(str);
6335 return Qnil;
6339 * call-seq:
6340 * IO.foreach(name, sep=$/) {|line| block } => nil
6341 * IO.foreach(name, limit) {|line| block } => nil
6342 * IO.foreach(name, sep, limit) {|line| block } => nil
6344 * Executes the block for every line in the named I/O port, where lines
6345 * are separated by <em>sep</em>.
6347 * IO.foreach("testfile") {|x| print "GOT ", x }
6349 * <em>produces:</em>
6351 * GOT This is line one
6352 * GOT This is line two
6353 * GOT This is line three
6354 * GOT And so on...
6356 * If the last argument is a hash, it's the keyword argument to open.
6357 * See <code>IO.read</code> for detail.
6361 static VALUE
6362 rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
6364 struct foreach_arg arg;
6366 rb_scan_args(argc, argv, "13", NULL, NULL, NULL, NULL);
6367 RETURN_ENUMERATOR(self, argc, argv);
6368 open_key_args(argc, argv, &arg);
6369 if (NIL_P(arg.io)) return Qnil;
6370 return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);
6373 static VALUE
6374 io_s_readlines(struct foreach_arg *arg)
6376 return rb_io_readlines(arg->argc, arg->argv, arg->io);
6380 * call-seq:
6381 * IO.readlines(name, sep=$/) => array
6382 * IO.readlines(name, limit) => array
6383 * IO.readlines(name, sep, limit) => array
6385 * Reads the entire file specified by <i>name</i> as individual
6386 * lines, and returns those lines in an array. Lines are separated by
6387 * <i>sep</i>.
6389 * a = IO.readlines("testfile")
6390 * a[0] #=> "This is line one\n"
6392 * If the last argument is a hash, it's the keyword argument to open.
6393 * See <code>IO.read</code> for detail.
6397 static VALUE
6398 rb_io_s_readlines(int argc, VALUE *argv, VALUE io)
6400 struct foreach_arg arg;
6402 rb_scan_args(argc, argv, "13", NULL, NULL, NULL, NULL);
6403 open_key_args(argc, argv, &arg);
6404 if (NIL_P(arg.io)) return Qnil;
6405 return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
6408 static VALUE
6409 io_s_read(struct foreach_arg *arg)
6411 return io_read(arg->argc, arg->argv, arg->io);
6415 * call-seq:
6416 * IO.read(name, [length [, offset]] ) => string
6417 * IO.read(name, [length [, offset]], opt) => string
6419 * Opens the file, optionally seeks to the given offset, then returns
6420 * <i>length</i> bytes (defaulting to the rest of the file).
6421 * <code>read</code> ensures the file is closed before returning.
6423 * If the last argument is a hash, it specifies option for internal
6424 * open(). The key would be the following. open_args: is exclusive
6425 * to others.
6427 * encoding: string or encoding
6429 * specifies encoding of the read string. encoding will be ignored
6430 * if length is specified.
6432 * mode: string
6434 * specifies mode argument for open(). it should start with "r"
6435 * otherwise it would cause error.
6437 * open_args: array of strings
6439 * specifies arguments for open() as an array.
6441 * IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
6442 * IO.read("testfile", 20) #=> "This is line one\nThi"
6443 * IO.read("testfile", 20, 10) #=> "ne one\nThis is line "
6446 static VALUE
6447 rb_io_s_read(int argc, VALUE *argv, VALUE io)
6449 VALUE offset;
6450 struct foreach_arg arg;
6452 rb_scan_args(argc, argv, "13", NULL, NULL, &offset, NULL);
6453 open_key_args(argc, argv, &arg);
6454 if (NIL_P(arg.io)) return Qnil;
6455 if (!NIL_P(offset)) {
6456 rb_io_binmode(arg.io);
6457 rb_io_seek(arg.io, offset, SEEK_SET);
6458 if (arg.argc == 2) arg.argc = 1;
6460 return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
6463 struct copy_stream_struct {
6464 VALUE src;
6465 VALUE dst;
6466 off_t copy_length; /* (off_t)-1 if not specified */
6467 off_t src_offset; /* (off_t)-1 if not specified */
6469 int src_fd;
6470 int dst_fd;
6471 int close_src;
6472 int close_dst;
6473 off_t total;
6474 const char *syserr;
6475 int error_no;
6476 const char *notimp;
6477 rb_fdset_t fds;
6478 rb_thread_t *th;
6481 static int
6482 copy_stream_wait_read(struct copy_stream_struct *stp)
6484 int ret;
6485 rb_fd_zero(&stp->fds);
6486 rb_fd_set(stp->src_fd, &stp->fds);
6487 ret = rb_fd_select(rb_fd_max(&stp->fds), &stp->fds, NULL, NULL, NULL);
6488 if (ret == -1) {
6489 stp->syserr = "select";
6490 stp->error_no = errno;
6491 return -1;
6493 return 0;
6496 static int
6497 copy_stream_wait_write(struct copy_stream_struct *stp)
6499 int ret;
6500 rb_fd_zero(&stp->fds);
6501 rb_fd_set(stp->dst_fd, &stp->fds);
6502 ret = rb_fd_select(rb_fd_max(&stp->fds), NULL, &stp->fds, NULL, NULL);
6503 if (ret == -1) {
6504 stp->syserr = "select";
6505 stp->error_no = errno;
6506 return -1;
6508 return 0;
6511 #ifdef HAVE_SENDFILE
6513 #ifdef __linux__
6514 #define USE_SENDFILE
6516 #ifdef HAVE_SYS_SENDFILE_H
6517 #include <sys/sendfile.h>
6518 #endif
6520 static ssize_t
6521 simple_sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
6523 return sendfile(out_fd, in_fd, offset, count);
6526 #endif
6528 #endif
6530 #ifdef USE_SENDFILE
6531 static int
6532 copy_stream_sendfile(struct copy_stream_struct *stp)
6534 struct stat src_stat, dst_stat;
6535 ssize_t ss;
6536 int ret;
6538 off_t copy_length;
6539 off_t src_offset;
6540 int use_pread;
6542 ret = fstat(stp->src_fd, &src_stat);
6543 if (ret == -1) {
6544 stp->syserr = "fstat";
6545 stp->error_no = errno;
6546 return -1;
6548 if (!S_ISREG(src_stat.st_mode))
6549 return 0;
6551 ret = fstat(stp->dst_fd, &dst_stat);
6552 if (ret == -1) {
6553 stp->syserr = "fstat";
6554 stp->error_no = errno;
6555 return -1;
6557 if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
6558 return 0;
6560 src_offset = stp->src_offset;
6561 use_pread = src_offset != (off_t)-1;
6563 copy_length = stp->copy_length;
6564 if (copy_length == (off_t)-1) {
6565 if (use_pread)
6566 copy_length = src_stat.st_size - src_offset;
6567 else {
6568 off_t cur = lseek(stp->src_fd, 0, SEEK_CUR);
6569 if (cur == (off_t)-1) {
6570 stp->syserr = "lseek";
6571 stp->error_no = errno;
6572 return -1;
6574 copy_length = src_stat.st_size - cur;
6578 retry_sendfile:
6579 if (use_pread) {
6580 ss = simple_sendfile(stp->dst_fd, stp->src_fd, &src_offset, copy_length);
6582 else {
6583 ss = simple_sendfile(stp->dst_fd, stp->src_fd, NULL, copy_length);
6585 if (0 < ss) {
6586 stp->total += ss;
6587 copy_length -= ss;
6588 if (0 < copy_length) {
6589 ss = -1;
6590 errno = EAGAIN;
6593 if (ss == -1) {
6594 switch (errno) {
6595 case EINVAL:
6596 #ifdef ENOSYS
6597 case ENOSYS:
6598 #endif
6599 return 0;
6600 case EAGAIN:
6601 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6602 case EWOULDBLOCK:
6603 #endif
6604 if (copy_stream_wait_write(stp) == -1)
6605 return -1;
6606 if (RUBY_VM_INTERRUPTED(stp->th))
6607 return -1;
6608 goto retry_sendfile;
6610 stp->syserr = "sendfile";
6611 stp->error_no = errno;
6612 return -1;
6614 return 1;
6616 #endif
6618 static ssize_t
6619 copy_stream_read(struct copy_stream_struct *stp, char *buf, int len, off_t offset)
6621 ssize_t ss;
6622 retry_read:
6623 if (offset == (off_t)-1)
6624 ss = read(stp->src_fd, buf, len);
6625 else {
6626 #ifdef HAVE_PREAD
6627 ss = pread(stp->src_fd, buf, len, offset);
6628 #else
6629 stp->notimp = "pread";
6630 return -1;
6631 #endif
6633 if (ss == 0) {
6634 return 0;
6636 if (ss == -1) {
6637 switch (errno) {
6638 case EAGAIN:
6639 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6640 case EWOULDBLOCK:
6641 #endif
6642 if (copy_stream_wait_read(stp) == -1)
6643 return -1;
6644 goto retry_read;
6645 #ifdef ENOSYS
6646 case ENOSYS:
6647 #endif
6648 stp->notimp = "pread";
6649 return -1;
6651 stp->syserr = offset == (off_t)-1 ? "read" : "pread";
6652 stp->error_no = errno;
6653 return -1;
6655 return ss;
6658 static int
6659 copy_stream_write(struct copy_stream_struct *stp, char *buf, int len)
6661 ssize_t ss;
6662 int off = 0;
6663 while (len) {
6664 ss = write(stp->dst_fd, buf+off, len);
6665 if (ss == -1) {
6666 if (errno == EAGAIN || errno == EWOULDBLOCK) {
6667 if (copy_stream_wait_write(stp) == -1)
6668 return -1;
6669 continue;
6671 stp->syserr = "write";
6672 stp->error_no = errno;
6673 return -1;
6675 off += ss;
6676 len -= ss;
6677 stp->total += ss;
6679 return 0;
6682 static void
6683 copy_stream_read_write(struct copy_stream_struct *stp)
6685 char buf[1024*16];
6686 int len;
6687 ssize_t ss;
6688 int ret;
6689 off_t copy_length;
6690 int use_eof;
6691 off_t src_offset;
6692 int use_pread;
6694 copy_length = stp->copy_length;
6695 use_eof = copy_length == (off_t)-1;
6696 src_offset = stp->src_offset;
6697 use_pread = src_offset != (off_t)-1;
6699 if (use_pread && stp->close_src) {
6700 off_t r;
6701 r = lseek(stp->src_fd, src_offset, SEEK_SET);
6702 if (r == (off_t)-1) {
6703 stp->syserr = "lseek";
6704 stp->error_no = errno;
6705 return;
6707 src_offset = (off_t)-1;
6708 use_pread = 0;
6711 while (use_eof || 0 < copy_length) {
6712 if (!use_eof && copy_length < sizeof(buf)) {
6713 len = copy_length;
6715 else {
6716 len = sizeof(buf);
6718 if (use_pread) {
6719 ss = copy_stream_read(stp, buf, len, src_offset);
6720 if (0 < ss)
6721 src_offset += ss;
6723 else {
6724 ss = copy_stream_read(stp, buf, len, (off_t)-1);
6726 if (ss <= 0) /* EOF or error */
6727 return;
6729 ret = copy_stream_write(stp, buf, ss);
6730 if (ret < 0)
6731 return;
6733 if (!use_eof)
6734 copy_length -= ss;
6736 if (RUBY_VM_INTERRUPTED(stp->th))
6737 return;
6741 static VALUE
6742 copy_stream_func(void *arg)
6744 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
6745 #ifdef USE_SENDFILE
6746 int ret;
6747 #endif
6749 #ifdef USE_SENDFILE
6750 ret = copy_stream_sendfile(stp);
6751 if (ret != 0)
6752 goto finish; /* error or success */
6753 #endif
6755 copy_stream_read_write(stp);
6757 #ifdef USE_SENDFILE
6758 finish:
6759 #endif
6760 return Qnil;
6763 static VALUE
6764 copy_stream_fallback_body(VALUE arg)
6766 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
6767 const int buflen = 16*1024;
6768 VALUE n;
6769 VALUE buf = rb_str_buf_new(buflen);
6770 long rest = stp->copy_length;
6771 off_t off = stp->src_offset;
6773 while (1) {
6774 long numwrote;
6775 long l;
6776 if (stp->copy_length == (off_t)-1) {
6777 l = buflen;
6779 else {
6780 if (rest == 0)
6781 break;
6782 l = buflen < rest ? buflen : rest;
6784 if (stp->src_fd == -1) {
6785 rb_funcall(stp->src, id_readpartial, 2, INT2FIX(l), buf);
6787 else {
6788 ssize_t ss;
6789 rb_thread_wait_fd(stp->src_fd);
6790 rb_str_resize(buf, buflen);
6791 ss = copy_stream_read(stp, RSTRING_PTR(buf), l, off);
6792 if (ss == -1)
6793 return Qnil;
6794 if (ss == 0)
6795 rb_eof_error();
6796 rb_str_resize(buf, ss);
6797 if (off != (off_t)-1)
6798 off += ss;
6800 n = rb_io_write(stp->dst, buf);
6801 numwrote = NUM2LONG(n);
6802 stp->total += numwrote;
6803 rest -= numwrote;
6806 return Qnil;
6809 static VALUE
6810 copy_stream_fallback(struct copy_stream_struct *stp)
6812 if (stp->src_fd == -1 && stp->src_offset != (off_t)-1) {
6813 rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
6815 rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
6816 (VALUE (*) (ANYARGS))0, (VALUE)0,
6817 rb_eEOFError, (VALUE)0);
6818 return Qnil;
6821 static VALUE
6822 copy_stream_body(VALUE arg)
6824 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
6825 VALUE src_io, dst_io;
6826 rb_io_t *src_fptr = 0, *dst_fptr = 0;
6827 int src_fd, dst_fd;
6829 stp->th = GET_THREAD();
6831 stp->total = 0;
6833 if (stp->src == argf ||
6834 !(TYPE(stp->src) == T_FILE ||
6835 rb_respond_to(stp->src, rb_intern("to_io")) ||
6836 TYPE(stp->src) == T_STRING ||
6837 rb_respond_to(stp->src, rb_intern("to_path")))) {
6838 src_fd = -1;
6840 else {
6841 src_io = rb_check_convert_type(stp->src, T_FILE, "IO", "to_io");
6842 if (NIL_P(src_io)) {
6843 VALUE args[2];
6844 int flags = O_RDONLY;
6845 #ifdef O_NOCTTY
6846 flags |= O_NOCTTY;
6847 #endif
6848 FilePathValue(stp->src);
6849 args[0] = stp->src;
6850 args[1] = INT2NUM(flags);
6851 src_io = rb_class_new_instance(2, args, rb_cFile);
6852 stp->src = src_io;
6853 stp->close_src = 1;
6855 GetOpenFile(src_io, src_fptr);
6856 rb_io_check_readable(src_fptr);
6857 src_fd = src_fptr->fd;
6859 stp->src_fd = src_fd;
6861 if (stp->dst == argf ||
6862 !(TYPE(stp->dst) == T_FILE ||
6863 rb_respond_to(stp->dst, rb_intern("to_io")) ||
6864 TYPE(stp->dst) == T_STRING ||
6865 rb_respond_to(stp->dst, rb_intern("to_path")))) {
6866 dst_fd = -1;
6868 else {
6869 dst_io = rb_check_convert_type(stp->dst, T_FILE, "IO", "to_io");
6870 if (NIL_P(dst_io)) {
6871 VALUE args[3];
6872 int flags = O_WRONLY|O_CREAT|O_TRUNC;
6873 #ifdef O_NOCTTY
6874 flags |= O_NOCTTY;
6875 #endif
6876 FilePathValue(stp->dst);
6877 args[0] = stp->dst;
6878 args[1] = INT2NUM(flags);
6879 args[2] = INT2FIX(0600);
6880 dst_io = rb_class_new_instance(3, args, rb_cFile);
6881 stp->dst = dst_io;
6882 stp->close_dst = 1;
6884 else {
6885 dst_io = GetWriteIO(dst_io);
6886 stp->dst = dst_io;
6888 GetOpenFile(dst_io, dst_fptr);
6889 rb_io_check_writable(dst_fptr);
6890 dst_fd = dst_fptr->fd;
6892 stp->dst_fd = dst_fd;
6894 if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf_len) {
6895 long len = src_fptr->rbuf_len;
6896 VALUE str;
6897 if (stp->copy_length != (off_t)-1 && stp->copy_length < len) {
6898 len = stp->copy_length;
6900 str = rb_str_buf_new(len);
6901 rb_str_resize(str,len);
6902 read_buffered_data(RSTRING_PTR(str), len, src_fptr);
6903 if (dst_fptr) /* IO or filename */
6904 io_fwrite(str, dst_fptr);
6905 else /* others such as StringIO */
6906 rb_io_write(stp->dst, str);
6907 stp->total += len;
6908 if (stp->copy_length != (off_t)-1)
6909 stp->copy_length -= len;
6912 if (dst_fptr && io_fflush(dst_fptr) < 0) {
6913 rb_raise(rb_eIOError, "flush failed");
6916 if (stp->copy_length == 0)
6917 return Qnil;
6919 if (src_fd == -1 || dst_fd == -1) {
6920 return copy_stream_fallback(stp);
6923 rb_fd_init(&stp->fds);
6924 rb_fd_set(src_fd, &stp->fds);
6925 rb_fd_set(dst_fd, &stp->fds);
6927 return rb_thread_blocking_region(copy_stream_func, (void*)stp, RB_UBF_DFL, 0);
6930 static VALUE
6931 copy_stream_finalize(VALUE arg)
6933 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
6934 if (stp->close_src) {
6935 rb_io_close_m(stp->src);
6937 if (stp->close_dst) {
6938 rb_io_close_m(stp->dst);
6940 rb_fd_term(&stp->fds);
6941 if (stp->syserr) {
6942 errno = stp->error_no;
6943 rb_sys_fail(stp->syserr);
6945 if (stp->notimp) {
6946 rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
6948 return Qnil;
6952 * call-seq:
6953 * IO.copy_stream(src, dst)
6954 * IO.copy_stream(src, dst, copy_length)
6955 * IO.copy_stream(src, dst, copy_length, src_offset)
6957 * IO.copy_stream copies <i>src</i> to <i>dst</i>.
6958 * <i>src</i> and <i>dst</i> is either a filename or an IO.
6960 * This method returns the number of bytes copied.
6962 * If optional arguments are not given,
6963 * the start position of the copy is
6964 * the beginning of the filename or
6965 * the current file offset of the IO.
6966 * The end position of the copy is the end of file.
6968 * If <i>copy_length</i> is given,
6969 * No more than <i>copy_length</i> bytes are copied.
6971 * If <i>src_offset</i> is given,
6972 * it specifies the start position of the copy.
6974 * When <i>src_offset</i> is specified and
6975 * <i>src</i> is an IO,
6976 * IO.copy_stream doesn't move the current file offset.
6979 static VALUE
6980 rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
6982 VALUE src, dst, length, src_offset;
6983 struct copy_stream_struct st;
6985 MEMZERO(&st, struct copy_stream_struct, 1);
6987 rb_scan_args(argc, argv, "22", &src, &dst, &length, &src_offset);
6989 st.src = src;
6990 st.dst = dst;
6992 if (NIL_P(length))
6993 st.copy_length = (off_t)-1;
6994 else
6995 st.copy_length = NUM2OFFT(length);
6997 if (NIL_P(src_offset))
6998 st.src_offset = (off_t)-1;
6999 else
7000 st.src_offset = NUM2OFFT(src_offset);
7002 rb_ensure(copy_stream_body, (VALUE)&st, copy_stream_finalize, (VALUE)&st);
7004 return OFFT2NUM(st.total);
7008 * call-seq:
7009 * io.external_encoding => encoding
7011 * Returns the Encoding object that represents the encoding of the file.
7012 * If io is write mode and no encoding is specified, returns <code>nil</code>.
7015 static VALUE
7016 rb_io_external_encoding(VALUE io)
7018 rb_io_t *fptr;
7020 GetOpenFile(io, fptr);
7021 if (fptr->enc2) {
7022 return rb_enc_from_encoding(fptr->enc2);
7024 if (!fptr->enc && fptr->fd == 0) {
7025 fptr->enc = rb_default_external_encoding();
7027 if (fptr->mode & FMODE_WRITABLE) {
7028 if (fptr->enc)
7029 return rb_enc_from_encoding(fptr->enc);
7030 return Qnil;
7032 return rb_enc_from_encoding(io_read_encoding(fptr));
7036 * call-seq:
7037 * io.internal_encoding => encoding
7039 * Returns the Encoding of the internal string if conversion is
7040 * specified. Otherwise returns nil.
7043 static VALUE
7044 rb_io_internal_encoding(VALUE io)
7046 rb_io_t *fptr;
7048 GetOpenFile(io, fptr);
7049 if (!fptr->enc2) return Qnil;
7050 return rb_enc_from_encoding(io_read_encoding(fptr));
7054 * call-seq:
7055 * io.set_encoding(ext_enc) => io
7056 * io.set_encoding("ext_enc:int_enc") => io
7057 * io.set_encoding(ext_enc, int_enc) => io
7059 * If single argument is specified, read string from io is tagged
7060 * with the encoding specified. If encoding is a colon separated two
7061 * encoding names "A:B", the read string is converted from encoding A
7062 * (external encoding) to encoding B (internal encoding), then tagged
7063 * with B. If two arguments are specified, those must be encoding
7064 * objects or encoding names, and the first one is the external encoding, and the
7065 * second one is the internal encoding.
7068 static VALUE
7069 rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
7071 rb_io_t *fptr;
7072 VALUE v1, v2;
7074 rb_scan_args(argc, argv, "11", &v1, &v2);
7075 GetOpenFile(io, fptr);
7076 io_encoding_set(fptr, argc, v1, v2);
7077 return io;
7080 static VALUE
7081 argf_external_encoding(VALUE argf)
7083 if (!RTEST(current_file)) {
7084 return rb_enc_from_encoding(rb_default_external_encoding());
7086 return rb_io_external_encoding(rb_io_check_io(current_file));
7089 static VALUE
7090 argf_internal_encoding(VALUE argf)
7092 if (!RTEST(current_file)) {
7093 return rb_enc_from_encoding(rb_default_external_encoding());
7095 return rb_io_internal_encoding(rb_io_check_io(current_file));
7098 static VALUE
7099 argf_set_encoding(int argc, VALUE *argv, VALUE argf)
7101 rb_io_t *fptr;
7103 if (!next_argv()) {
7104 rb_raise(rb_eArgError, "no stream to set encoding");
7106 rb_io_set_encoding(argc, argv, current_file);
7107 GetOpenFile(current_file, fptr);
7108 argf_enc = fptr->enc;
7109 argf_enc2 = fptr->enc2;
7110 return argf;
7113 static VALUE
7114 argf_tell(VALUE argf)
7116 if (!next_argv()) {
7117 rb_raise(rb_eArgError, "no stream to tell");
7119 ARGF_FORWARD(0, 0);
7120 return rb_io_tell(current_file);
7123 static VALUE
7124 argf_seek_m(int argc, VALUE *argv, VALUE argf)
7126 if (!next_argv()) {
7127 rb_raise(rb_eArgError, "no stream to seek");
7129 ARGF_FORWARD(argc, argv);
7130 return rb_io_seek_m(argc, argv, current_file);
7133 static VALUE
7134 argf_set_pos(VALUE argf, VALUE offset)
7136 if (!next_argv()) {
7137 rb_raise(rb_eArgError, "no stream to set position");
7139 ARGF_FORWARD(1, &offset);
7140 return rb_io_set_pos(current_file, offset);
7143 static VALUE
7144 argf_rewind(VALUE argf)
7146 if (!next_argv()) {
7147 rb_raise(rb_eArgError, "no stream to rewind");
7149 ARGF_FORWARD(0, 0);
7150 return rb_io_rewind(current_file);
7153 static VALUE
7154 argf_fileno(VALUE argf)
7156 if (!next_argv()) {
7157 rb_raise(rb_eArgError, "no stream");
7159 ARGF_FORWARD(0, 0);
7160 return rb_io_fileno(current_file);
7163 static VALUE
7164 argf_to_io(VALUE argf)
7166 next_argv();
7167 ARGF_FORWARD(0, 0);
7168 return current_file;
7171 static VALUE
7172 argf_eof(VALUE argf)
7174 if (current_file) {
7175 if (init_p == 0) return Qtrue;
7176 ARGF_FORWARD(0, 0);
7177 if (rb_io_eof(current_file)) {
7178 return Qtrue;
7181 return Qfalse;
7184 static VALUE
7185 argf_read(int argc, VALUE *argv, VALUE argf)
7187 VALUE tmp, str, length;
7188 long len = 0;
7190 rb_scan_args(argc, argv, "02", &length, &str);
7191 if (!NIL_P(length)) {
7192 len = NUM2LONG(argv[0]);
7194 if (!NIL_P(str)) {
7195 StringValue(str);
7196 rb_str_resize(str,0);
7197 argv[1] = Qnil;
7200 retry:
7201 if (!next_argv()) {
7202 return str;
7204 if (ARGF_GENERIC_INPUT_P()) {
7205 tmp = argf_forward(argc, argv, argf);
7207 else {
7208 tmp = io_read(argc, argv, current_file);
7210 if (NIL_P(str)) str = tmp;
7211 else if (!NIL_P(tmp)) rb_str_append(str, tmp);
7212 if (NIL_P(tmp) || NIL_P(length)) {
7213 if (next_p != -1) {
7214 argf_close(current_file);
7215 next_p = 1;
7216 goto retry;
7219 else if (argc >= 1) {
7220 if (RSTRING_LEN(str) < len) {
7221 len -= RSTRING_LEN(str);
7222 argv[0] = INT2NUM(len);
7223 goto retry;
7226 return str;
7229 struct argf_call_arg {
7230 int argc;
7231 VALUE *argv;
7232 VALUE argf;
7235 static VALUE
7236 argf_forward_call(VALUE arg)
7238 struct argf_call_arg *p = (struct argf_call_arg *)arg;
7239 argf_forward(p->argc, p->argv, p->argf);
7240 return Qnil;
7243 static VALUE
7244 argf_readpartial(int argc, VALUE *argv, VALUE argf)
7246 VALUE tmp, str, length;
7248 rb_scan_args(argc, argv, "11", &length, &str);
7249 if (!NIL_P(str)) {
7250 StringValue(str);
7251 argv[1] = str;
7254 if (!next_argv()) {
7255 rb_str_resize(str, 0);
7256 rb_eof_error();
7258 if (ARGF_GENERIC_INPUT_P()) {
7259 struct argf_call_arg arg;
7260 arg.argc = argc;
7261 arg.argv = argv;
7262 arg.argf = argf;
7263 tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
7264 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
7266 else {
7267 tmp = io_getpartial(argc, argv, current_file, 0);
7269 if (NIL_P(tmp)) {
7270 if (next_p == -1) {
7271 rb_eof_error();
7273 argf_close(current_file);
7274 next_p = 1;
7275 if (RARRAY_LEN(rb_argv) == 0)
7276 rb_eof_error();
7277 if (NIL_P(str))
7278 str = rb_str_new(NULL, 0);
7279 return str;
7281 return tmp;
7284 static VALUE
7285 argf_getc(VALUE argf)
7287 VALUE ch;
7289 retry:
7290 if (!next_argv()) return Qnil;
7291 if (ARGF_GENERIC_INPUT_P()) {
7292 ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
7294 else {
7295 ch = rb_io_getc(current_file);
7297 if (NIL_P(ch) && next_p != -1) {
7298 argf_close(current_file);
7299 next_p = 1;
7300 goto retry;
7303 return ch;
7306 static VALUE
7307 argf_getbyte(VALUE argf)
7309 VALUE ch;
7311 retry:
7312 if (!next_argv()) return Qnil;
7313 if (TYPE(current_file) != T_FILE) {
7314 ch = rb_funcall3(current_file, rb_intern("getbyte"), 0, 0);
7316 else {
7317 ch = rb_io_getbyte(current_file);
7319 if (NIL_P(ch) && next_p != -1) {
7320 argf_close(current_file);
7321 next_p = 1;
7322 goto retry;
7325 return ch;
7328 static VALUE
7329 argf_readchar(VALUE argf)
7331 VALUE ch;
7333 retry:
7334 if (!next_argv()) rb_eof_error();
7335 if (TYPE(current_file) != T_FILE) {
7336 ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
7338 else {
7339 ch = rb_io_getc(current_file);
7341 if (NIL_P(ch) && next_p != -1) {
7342 argf_close(current_file);
7343 next_p = 1;
7344 goto retry;
7347 return ch;
7350 static VALUE
7351 argf_readbyte(VALUE argf)
7353 VALUE c;
7355 NEXT_ARGF_FORWARD(0, 0);
7356 c = argf_getbyte(argf);
7357 if (NIL_P(c)) {
7358 rb_eof_error();
7360 return c;
7363 static VALUE
7364 argf_each_line(int argc, VALUE *argv, VALUE argf)
7366 RETURN_ENUMERATOR(argf, argc, argv);
7367 for (;;) {
7368 if (!next_argv()) return Qnil;
7369 rb_block_call(current_file, rb_intern("each_line"), argc, argv, rb_yield, 0);
7370 next_p = 1;
7372 return argf;
7375 static VALUE
7376 argf_each_byte(VALUE argf)
7378 RETURN_ENUMERATOR(argf, 0, 0);
7379 for (;;) {
7380 if (!next_argv()) return Qnil;
7381 rb_block_call(current_file, rb_intern("each_byte"), 0, 0, rb_yield, 0);
7382 next_p = 1;
7386 static VALUE
7387 argf_each_char(VALUE argf)
7389 RETURN_ENUMERATOR(argf, 0, 0);
7390 for (;;) {
7391 if (!next_argv()) return Qnil;
7392 rb_block_call(current_file, rb_intern("each_char"), 0, 0, rb_yield, 0);
7393 next_p = 1;
7397 static VALUE
7398 argf_filename(VALUE argf)
7400 next_argv();
7401 return filename;
7404 static VALUE
7405 argf_filename_getter(ID id, VALUE *var)
7407 return argf_filename(*var);
7410 static VALUE
7411 argf_file(VALUE argf)
7413 next_argv();
7414 return current_file;
7417 static VALUE
7418 argf_binmode_m(VALUE argf)
7420 argf_binmode = 1;
7421 next_argv();
7422 ARGF_FORWARD(0, 0);
7423 rb_io_binmode(current_file);
7424 return argf;
7427 static VALUE
7428 argf_binmode_p(VALUE argf)
7430 return argf_binmode ? Qtrue : Qfalse;
7433 static VALUE
7434 argf_skip(VALUE argf)
7436 if (next_p != -1) {
7437 argf_close(current_file);
7438 next_p = 1;
7440 return argf;
7443 static VALUE
7444 argf_close_m(VALUE argf)
7446 next_argv();
7447 argf_close(current_file);
7448 if (next_p != -1) {
7449 next_p = 1;
7451 gets_lineno = 0;
7452 return argf;
7455 static VALUE
7456 argf_closed(VALUE argf)
7458 next_argv();
7459 ARGF_FORWARD(0, 0);
7460 return rb_io_closed(current_file);
7463 static VALUE
7464 argf_to_s(VALUE argf)
7466 return rb_str_new2("ARGF");
7469 static VALUE
7470 argf_inplace_mode_get(VALUE argf)
7472 if (!ruby_inplace_mode) return Qnil;
7473 return rb_str_new2(ruby_inplace_mode);
7476 static VALUE
7477 opt_i_get(ID id, VALUE *var)
7479 return argf_inplace_mode_get(*var);
7482 static VALUE
7483 argf_inplace_mode_set(VALUE argf, VALUE val)
7485 if (!RTEST(val)) {
7486 if (ruby_inplace_mode) free(ruby_inplace_mode);
7487 ruby_inplace_mode = 0;
7489 else {
7490 StringValue(val);
7491 if (ruby_inplace_mode) free(ruby_inplace_mode);
7492 ruby_inplace_mode = 0;
7493 ruby_inplace_mode = strdup(RSTRING_PTR(val));
7495 return argf;
7498 static void
7499 opt_i_set(VALUE val, ID id, VALUE *var)
7501 argf_inplace_mode_set(*var, val);
7504 const char *
7505 ruby_get_inplace_mode(void)
7507 return ruby_inplace_mode;
7510 void
7511 ruby_set_inplace_mode(const char *suffix)
7513 if (ruby_inplace_mode) free(ruby_inplace_mode);
7514 ruby_inplace_mode = 0;
7515 if (suffix) ruby_inplace_mode = strdup(suffix);
7518 static VALUE
7519 argf_argv(VALUE argf)
7521 return rb_argv;
7524 static VALUE
7525 argf_argv_getter(ID id, VALUE *var)
7527 return argf_argv(*var);
7530 VALUE
7531 rb_get_argv(void)
7533 return rb_argv;
7537 * Class <code>IO</code> is the basis for all input and output in Ruby.
7538 * An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
7539 * so may use more than one native operating system stream.
7541 * Many of the examples in this section use class <code>File</code>,
7542 * the only standard subclass of <code>IO</code>. The two classes are
7543 * closely associated.
7545 * As used in this section, <em>portname</em> may take any of the
7546 * following forms.
7548 * * A plain string represents a filename suitable for the underlying
7549 * operating system.
7551 * * A string starting with ``<code>|</code>'' indicates a subprocess.
7552 * The remainder of the string following the ``<code>|</code>'' is
7553 * invoked as a process with appropriate input/output channels
7554 * connected to it.
7556 * * A string equal to ``<code>|-</code>'' will create another Ruby
7557 * instance as a subprocess.
7559 * Ruby will convert pathnames between different operating system
7560 * conventions if possible. For instance, on a Windows system the
7561 * filename ``<code>/gumby/ruby/test.rb</code>'' will be opened as
7562 * ``<code>\gumby\ruby\test.rb</code>''. When specifying a
7563 * Windows-style filename in a Ruby string, remember to escape the
7564 * backslashes:
7566 * "c:\\gumby\\ruby\\test.rb"
7568 * Our examples here will use the Unix-style forward slashes;
7569 * <code>File::SEPARATOR</code> can be used to get the
7570 * platform-specific separator character.
7572 * I/O ports may be opened in any one of several different modes, which
7573 * are shown in this section as <em>mode</em>. The mode may
7574 * either be a Fixnum or a String. If numeric, it should be
7575 * one of the operating system specific constants (O_RDONLY,
7576 * O_WRONLY, O_RDWR, O_APPEND and so on). See man open(2) for
7577 * more information.
7579 * If the mode is given as a String, it must be one of the
7580 * values listed in the following table.
7582 * Mode | Meaning
7583 * -----+--------------------------------------------------------
7584 * "r" | Read-only, starts at beginning of file (default mode).
7585 * -----+--------------------------------------------------------
7586 * "r+" | Read-write, starts at beginning of file.
7587 * -----+--------------------------------------------------------
7588 * "w" | Write-only, truncates existing file
7589 * | to zero length or creates a new file for writing.
7590 * -----+--------------------------------------------------------
7591 * "w+" | Read-write, truncates existing file to zero length
7592 * | or creates a new file for reading and writing.
7593 * -----+--------------------------------------------------------
7594 * "a" | Write-only, starts at end of file if file exists,
7595 * | otherwise creates a new file for writing.
7596 * -----+--------------------------------------------------------
7597 * "a+" | Read-write, starts at end of file if file exists,
7598 * | otherwise creates a new file for reading and
7599 * | writing.
7600 * -----+--------------------------------------------------------
7601 * "b" | (DOS/Windows only) Binary file mode (may appear with
7602 * | any of the key letters listed above).
7605 * The global constant ARGF (also accessible as $<) provides an
7606 * IO-like stream which allows access to all files mentioned on the
7607 * command line (or STDIN if no files are mentioned). ARGF provides
7608 * the methods <code>#path</code> and <code>#filename</code> to access
7609 * the name of the file currently being read.
7612 void
7613 Init_IO(void)
7615 #undef rb_intern
7617 VALUE rb_cARGF;
7618 #ifdef __CYGWIN__
7619 #include <sys/cygwin.h>
7620 static struct __cygwin_perfile pf[] =
7622 {"", O_RDONLY | O_BINARY},
7623 {"", O_WRONLY | O_BINARY},
7624 {"", O_RDWR | O_BINARY},
7625 {"", O_APPEND | O_BINARY},
7626 {NULL, 0}
7628 cygwin_internal(CW_PERFILE, pf);
7629 #endif
7631 rb_eIOError = rb_define_class("IOError", rb_eStandardError);
7632 rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
7634 id_write = rb_intern("write");
7635 id_read = rb_intern("read");
7636 id_getc = rb_intern("getc");
7637 id_flush = rb_intern("flush");
7638 id_encode = rb_intern("encode");
7639 id_readpartial = rb_intern("readpartial");
7641 rb_define_global_function("syscall", rb_f_syscall, -1);
7643 rb_define_global_function("open", rb_f_open, -1);
7644 rb_define_global_function("printf", rb_f_printf, -1);
7645 rb_define_global_function("print", rb_f_print, -1);
7646 rb_define_global_function("putc", rb_f_putc, 1);
7647 rb_define_global_function("puts", rb_f_puts, -1);
7648 rb_define_global_function("gets", rb_f_gets, -1);
7649 rb_define_global_function("readline", rb_f_readline, -1);
7650 rb_define_global_function("select", rb_f_select, -1);
7652 rb_define_global_function("readlines", rb_f_readlines, -1);
7654 rb_define_global_function("`", rb_f_backquote, 1);
7656 rb_define_global_function("p", rb_f_p, -1);
7657 rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
7659 rb_cIO = rb_define_class("IO", rb_cObject);
7660 rb_include_module(rb_cIO, rb_mEnumerable);
7662 rb_define_alloc_func(rb_cIO, io_alloc);
7663 rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
7664 rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1);
7665 rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
7666 rb_define_singleton_method(rb_cIO, "for_fd", rb_io_s_for_fd, -1);
7667 rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
7668 rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
7669 rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
7670 rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
7671 rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
7672 rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
7673 rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
7674 rb_define_singleton_method(rb_cIO, "copy_stream", rb_io_s_copy_stream, -1);
7676 rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
7678 rb_output_fs = Qnil;
7679 rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
7681 rb_global_variable(&rb_default_rs);
7682 rb_rs = rb_default_rs = rb_str_new2("\n");
7683 rb_output_rs = Qnil;
7684 OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
7685 rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
7686 rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
7687 rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
7689 rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
7691 rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
7692 rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
7694 rb_define_method(rb_cIO, "print", rb_io_print, -1);
7695 rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
7696 rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
7697 rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
7699 rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
7700 rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
7701 rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
7702 rb_define_method(rb_cIO, "each_char", rb_io_each_char, 0);
7703 rb_define_method(rb_cIO, "lines", rb_io_lines, -1);
7704 rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0);
7705 rb_define_method(rb_cIO, "chars", rb_io_chars, 0);
7707 rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
7708 rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
7710 rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
7711 rb_define_alias(rb_cIO, "to_i", "fileno");
7712 rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
7714 rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
7715 rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
7716 rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
7718 rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
7719 rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
7721 rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
7723 rb_define_method(rb_cIO, "read_nonblock", io_read_nonblock, -1);
7724 rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, 1);
7725 rb_define_method(rb_cIO, "readpartial", io_readpartial, -1);
7726 rb_define_method(rb_cIO, "read", io_read, -1);
7727 rb_define_method(rb_cIO, "write", io_write, 1);
7728 rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
7729 rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
7730 rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
7731 rb_define_method(rb_cIO, "getbyte", rb_io_getbyte, 0);
7732 rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
7733 rb_define_method(rb_cIO, "readbyte", rb_io_readbyte, 0);
7734 rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
7735 rb_define_method(rb_cIO, "<<", rb_io_addstr, 1);
7736 rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
7737 rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
7738 rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
7739 rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
7740 rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
7741 rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
7742 rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
7743 rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
7744 rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
7745 rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
7746 rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
7748 rb_define_method(rb_cIO, "close_on_exec?", rb_io_close_on_exec_p, 0);
7749 rb_define_method(rb_cIO, "close_on_exec=", rb_io_set_close_on_exec, 1);
7751 rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
7752 rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
7753 rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
7754 rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
7756 rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
7757 rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
7758 rb_define_method(rb_cIO, "binmode", rb_io_binmode_m, 0);
7759 rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
7760 rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
7762 rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
7763 rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
7764 rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
7765 rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
7767 rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
7768 rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
7769 rb_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
7771 rb_define_variable("$stdin", &rb_stdin);
7772 rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
7773 rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
7774 rb_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO, "<STDOUT>");
7775 rb_define_hooked_variable("$stderr", &rb_stderr, 0, stdout_setter);
7776 rb_stderr = prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
7777 rb_define_hooked_variable("$>", &rb_stdout, 0, stdout_setter);
7778 orig_stdout = rb_stdout;
7779 rb_deferr = orig_stderr = rb_stderr;
7781 /* constants to hold original stdin/stdout/stderr */
7782 rb_define_global_const("STDIN", rb_stdin);
7783 rb_define_global_const("STDOUT", rb_stdout);
7784 rb_define_global_const("STDERR", rb_stderr);
7786 rb_cARGF = rb_class_new(rb_cObject);
7787 rb_set_class_path(rb_cARGF, rb_cObject, "ARGF.class");
7788 rb_define_alloc_func(rb_cARGF, argf_alloc);
7790 rb_include_module(rb_cARGF, rb_mEnumerable);
7792 rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
7793 rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
7794 rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
7795 rb_define_method(rb_cARGF, "argv", argf_argv, 0);
7797 rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
7798 rb_define_method(rb_cARGF, "to_i", argf_fileno, 0);
7799 rb_define_method(rb_cARGF, "to_io", argf_to_io, 0);
7800 rb_define_method(rb_cARGF, "each", argf_each_line, -1);
7801 rb_define_method(rb_cARGF, "each_line", argf_each_line, -1);
7802 rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0);
7803 rb_define_method(rb_cARGF, "each_char", argf_each_char, 0);
7804 rb_define_method(rb_cARGF, "lines", argf_each_line, -1);
7805 rb_define_method(rb_cARGF, "bytes", argf_each_byte, 0);
7806 rb_define_method(rb_cARGF, "chars", argf_each_char, 0);
7808 rb_define_method(rb_cARGF, "read", argf_read, -1);
7809 rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1);
7810 rb_define_method(rb_cARGF, "readlines", argf_readlines, -1);
7811 rb_define_method(rb_cARGF, "to_a", argf_readlines, -1);
7812 rb_define_method(rb_cARGF, "gets", argf_gets, -1);
7813 rb_define_method(rb_cARGF, "readline", argf_readline, -1);
7814 rb_define_method(rb_cARGF, "getc", argf_getc, 0);
7815 rb_define_method(rb_cARGF, "getbyte", argf_getbyte, 0);
7816 rb_define_method(rb_cARGF, "readchar", argf_readchar, 0);
7817 rb_define_method(rb_cARGF, "readbyte", argf_readbyte, 0);
7818 rb_define_method(rb_cARGF, "tell", argf_tell, 0);
7819 rb_define_method(rb_cARGF, "seek", argf_seek_m, -1);
7820 rb_define_method(rb_cARGF, "rewind", argf_rewind, 0);
7821 rb_define_method(rb_cARGF, "pos", argf_tell, 0);
7822 rb_define_method(rb_cARGF, "pos=", argf_set_pos, 1);
7823 rb_define_method(rb_cARGF, "eof", argf_eof, 0);
7824 rb_define_method(rb_cARGF, "eof?", argf_eof, 0);
7825 rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
7826 rb_define_method(rb_cARGF, "binmode?", argf_binmode_p, 0);
7828 rb_define_method(rb_cARGF, "filename", argf_filename, 0);
7829 rb_define_method(rb_cARGF, "path", argf_filename, 0);
7830 rb_define_method(rb_cARGF, "file", argf_file, 0);
7831 rb_define_method(rb_cARGF, "skip", argf_skip, 0);
7832 rb_define_method(rb_cARGF, "close", argf_close_m, 0);
7833 rb_define_method(rb_cARGF, "closed?", argf_closed, 0);
7835 rb_define_method(rb_cARGF, "lineno", argf_lineno, 0);
7836 rb_define_method(rb_cARGF, "lineno=", argf_set_lineno, 1);
7838 rb_define_method(rb_cARGF, "inplace_mode", argf_inplace_mode_get, 0);
7839 rb_define_method(rb_cARGF, "inplace_mode=", argf_inplace_mode_set, 1);
7841 rb_define_method(rb_cARGF, "external_encoding", argf_external_encoding, 0);
7842 rb_define_method(rb_cARGF, "internal_encoding", argf_internal_encoding, 0);
7843 rb_define_method(rb_cARGF, "set_encoding", argf_set_encoding, -1);
7845 argf = rb_class_new_instance(0, 0, rb_cARGF);
7847 rb_define_readonly_variable("$<", &argf);
7848 rb_define_global_const("ARGF", argf);
7850 rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
7851 rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, 0);
7852 filename = rb_str_new2("-");
7854 rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
7855 rb_define_hooked_variable("$*", &argf, argf_argv_getter, 0);
7857 #if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
7858 atexit(pipe_atexit);
7859 #endif
7861 Init_File();
7863 rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
7865 rb_file_const("RDONLY", INT2FIX(O_RDONLY));
7866 rb_file_const("WRONLY", INT2FIX(O_WRONLY));
7867 rb_file_const("RDWR", INT2FIX(O_RDWR));
7868 rb_file_const("APPEND", INT2FIX(O_APPEND));
7869 rb_file_const("CREAT", INT2FIX(O_CREAT));
7870 rb_file_const("EXCL", INT2FIX(O_EXCL));
7871 #if defined(O_NDELAY) || defined(O_NONBLOCK)
7872 # ifdef O_NONBLOCK
7873 rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK));
7874 # else
7875 rb_file_const("NONBLOCK", INT2FIX(O_NDELAY));
7876 # endif
7877 #endif
7878 rb_file_const("TRUNC", INT2FIX(O_TRUNC));
7879 #ifdef O_NOCTTY
7880 rb_file_const("NOCTTY", INT2FIX(O_NOCTTY));
7881 #endif
7882 #ifdef O_BINARY
7883 rb_file_const("BINARY", INT2FIX(O_BINARY));
7884 #else
7885 rb_file_const("BINARY", INT2FIX(0));
7886 #endif
7887 #ifdef O_SYNC
7888 rb_file_const("SYNC", INT2FIX(O_SYNC));
7889 #endif
7891 sym_mode = ID2SYM(rb_intern("mode"));
7892 sym_perm = ID2SYM(rb_intern("perm"));
7893 sym_extenc = ID2SYM(rb_intern("external_encoding"));
7894 sym_intenc = ID2SYM(rb_intern("internal_encoding"));
7895 sym_encoding = ID2SYM(rb_intern("encoding"));
7896 sym_open_args = ID2SYM(rb_intern("open_args"));