1 /**********************************************************************
2
3 io.c -
4
5 $Author: nagachika $
6 created at: Fri Oct 15 18:08:59 JST 1993
7
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
11
12 **********************************************************************/
13
14 #include "ruby/encoding.h"
15 #include "ruby/io.h"
16 #include "ruby/thread.h"
17 #include "internal.h"
18 #include "dln.h"
19 #include "encindex.h"
20 #include "id.h"
21 #include <ctype.h>
22 #include <errno.h>
23 #include "ruby_atomic.h"
24 #include "ccan/list/list.h"
25
26 #undef free
27 #define free(x) xfree(x)
28
29 #if defined(DOSISH) || defined(__CYGWIN__)
30 #include <io.h>
31 #endif
32
33 #include <sys/types.h>
34 #if defined HAVE_NET_SOCKET_H
35 # include <net/socket.h>
36 #elif defined HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
38 #endif
39
40 #if defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32)
41 # define NO_SAFE_RENAME
42 #endif
43
44 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__sun) || defined(_nec_ews)
45 # define USE_SETVBUF
46 #endif
47
48 #ifdef __QNXNTO__
49 #include "unix.h"
50 #endif
51
52 #include <sys/types.h>
53 #if defined(HAVE_SYS_IOCTL_H) && !defined(_WIN32)
54 #include <sys/ioctl.h>
55 #endif
56 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
57 #include <fcntl.h>
58 #elif defined(HAVE_SYS_FCNTL_H)
59 #include <sys/fcntl.h>
60 #endif
61
62 #if !HAVE_OFF_T && !defined(off_t)
63 # define off_t long
64 #endif
65
66 #ifdef HAVE_SYS_TIME_H
67 # include <sys/time.h>
68 #endif
69
70 #include <sys/stat.h>
71
72 #if defined(HAVE_SYS_PARAM_H) || defined(__HIUX_MPP__)
73 # include <sys/param.h>
74 #endif
75
76 #if !defined NOFILE
77 # define NOFILE 64
78 #endif
79
80 #ifdef HAVE_UNISTD_H
81 #include <unistd.h>
82 #endif
83
84 #ifdef HAVE_SYSCALL_H
85 #include <syscall.h>
86 #elif defined HAVE_SYS_SYSCALL_H
87 #include <sys/syscall.h>
88 #endif
89
90 #ifdef HAVE_SYS_UIO_H
91 #include <sys/uio.h>
92 #endif
93
94 #ifdef HAVE_SYS_WAIT_H
95 # include <sys/wait.h> /* for WNOHANG on BSD */
96 #endif
97
98 #include "ruby/util.h"
99
100 #ifndef O_ACCMODE
101 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
102 #endif
103
104 #if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
105 # error off_t is bigger than long, but you have no long long...
106 #endif
107
108 #ifndef PIPE_BUF
109 # ifdef _POSIX_PIPE_BUF
110 # define PIPE_BUF _POSIX_PIPE_BUF
111 # else
112 # define PIPE_BUF 512 /* is this ok? */
113 # endif
114 #endif
115
116 #ifndef EWOULDBLOCK
117 # define EWOULDBLOCK EAGAIN
118 #endif
119
120 #if defined(HAVE___SYSCALL) && (defined(__APPLE__) || defined(__OpenBSD__))
121 /* Mac OS X and OpenBSD have __syscall but don't define it in headers */
122 off_t __syscall(quad_t number, ...);
123 #endif
124
125 #define IO_RBUF_CAPA_MIN 8192
126 #define IO_CBUF_CAPA_MIN (128*1024)
127 #define IO_RBUF_CAPA_FOR(fptr) (NEED_READCONV(fptr) ? IO_CBUF_CAPA_MIN : IO_RBUF_CAPA_MIN)
128 #define IO_WBUF_CAPA_MIN 8192
129
130 /* define system APIs */
131 #ifdef _WIN32
132 #undef open
133 #define open rb_w32_uopen
134 #undef rename
135 #define rename(f, t) rb_w32_urename((f), (t))
136 #endif
137
138 #if defined(_WIN32)
139 # define RUBY_PIPE_NONBLOCK_DEFAULT (0)
140 #elif defined(O_NONBLOCK)
141 /* disabled for [Bug #15356] (Rack::Deflater + rails) failure: */
142 # define RUBY_PIPE_NONBLOCK_DEFAULT (0)
143 #else /* any platforms where O_NONBLOCK does not exist? */
144 # define RUBY_PIPE_NONBLOCK_DEFAULT (0)
145 #endif
146
147 VALUE rb_cIO;
148 VALUE rb_eEOFError;
149 VALUE rb_eIOError;
150 VALUE rb_mWaitReadable;
151 VALUE rb_mWaitWritable;
152
153 static VALUE rb_eEAGAINWaitReadable;
154 static VALUE rb_eEAGAINWaitWritable;
155 static VALUE rb_eEWOULDBLOCKWaitReadable;
156 static VALUE rb_eEWOULDBLOCKWaitWritable;
157 static VALUE rb_eEINPROGRESSWaitWritable;
158 static VALUE rb_eEINPROGRESSWaitReadable;
159
160 VALUE rb_stdin, rb_stdout, rb_stderr;
161 static VALUE orig_stdout, orig_stderr;
162
163 VALUE rb_output_fs;
164 VALUE rb_rs;
165 VALUE rb_output_rs;
166 VALUE rb_default_rs;
167
168 static VALUE argf;
169
170 #define id_exception idException
171 static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
172 static VALUE sym_mode, sym_perm, sym_flags, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
173 static VALUE sym_textmode, sym_binmode, sym_autoclose;
174 static VALUE sym_SET, sym_CUR, sym_END;
175 static VALUE sym_wait_readable, sym_wait_writable;
176 #ifdef SEEK_DATA
177 static VALUE sym_DATA;
178 #endif
179 #ifdef SEEK_HOLE
180 static VALUE sym_HOLE;
181 #endif
182
183 struct argf {
184 VALUE filename, current_file;
185 long last_lineno; /* $. */
186 long lineno;
187 VALUE argv;
188 VALUE inplace;
189 struct rb_io_enc_t encs;
190 int8_t init_p, next_p, binmode;
191 };
192
193 static rb_atomic_t max_file_descriptor = NOFILE;
194 void
rb_update_max_fd(int fd)195 rb_update_max_fd(int fd)
196 {
197 rb_atomic_t afd = (rb_atomic_t)fd;
198 rb_atomic_t max_fd = max_file_descriptor;
199 int err;
200
201 if (afd <= max_fd)
202 return;
203
204 #if defined(HAVE_FCNTL) && defined(F_GETFL)
205 err = fcntl(fd, F_GETFL) == -1;
206 #else
207 {
208 struct stat buf;
209 err = fstat(fd, &buf) != 0;
210 }
211 #endif
212 if (err && errno == EBADF) {
213 rb_bug("rb_update_max_fd: invalid fd (%d) given.", fd);
214 }
215
216 while (max_fd < afd) {
217 max_fd = ATOMIC_CAS(max_file_descriptor, max_fd, afd);
218 }
219 }
220
221 void
rb_maygvl_fd_fix_cloexec(int fd)222 rb_maygvl_fd_fix_cloexec(int fd)
223 {
224 /* MinGW don't have F_GETFD and FD_CLOEXEC. [ruby-core:40281] */
225 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
226 int flags, flags2, ret;
227 flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */
228 if (flags == -1) {
229 rb_bug("rb_maygvl_fd_fix_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno));
230 }
231 if (fd <= 2)
232 flags2 = flags & ~FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */
233 else
234 flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */
235 if (flags != flags2) {
236 ret = fcntl(fd, F_SETFD, flags2);
237 if (ret != 0) {
238 rb_bug("rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2, strerror(errno));
239 }
240 }
241 #endif
242 }
243
244 void
rb_fd_fix_cloexec(int fd)245 rb_fd_fix_cloexec(int fd)
246 {
247 rb_maygvl_fd_fix_cloexec(fd);
248 rb_update_max_fd(fd);
249 }
250
251 /* this is only called once */
252 static int
rb_fix_detect_o_cloexec(int fd)253 rb_fix_detect_o_cloexec(int fd)
254 {
255 #if defined(O_CLOEXEC) && defined(F_GETFD)
256 int flags = fcntl(fd, F_GETFD);
257
258 if (flags == -1)
259 rb_bug("rb_fix_detect_o_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno));
260
261 if (flags & FD_CLOEXEC)
262 return 1;
263 #endif /* fall through if O_CLOEXEC does not work: */
264 rb_maygvl_fd_fix_cloexec(fd);
265 return 0;
266 }
267
268 int
rb_cloexec_open(const char * pathname,int flags,mode_t mode)269 rb_cloexec_open(const char *pathname, int flags, mode_t mode)
270 {
271 int ret;
272 static int o_cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
273
274 #ifdef O_CLOEXEC
275 /* O_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */
276 flags |= O_CLOEXEC;
277 #elif defined O_NOINHERIT
278 flags |= O_NOINHERIT;
279 #endif
280 ret = open(pathname, flags, mode);
281 if (ret < 0) return ret;
282 if (ret <= 2 || o_cloexec_state == 0) {
283 rb_maygvl_fd_fix_cloexec(ret);
284 }
285 else if (o_cloexec_state > 0) {
286 return ret;
287 }
288 else {
289 o_cloexec_state = rb_fix_detect_o_cloexec(ret);
290 }
291 return ret;
292 }
293
294 int
rb_cloexec_dup(int oldfd)295 rb_cloexec_dup(int oldfd)
296 {
297 /* Don't allocate standard file descriptors: 0, 1, 2 */
298 return rb_cloexec_fcntl_dupfd(oldfd, 3);
299 }
300
301 int
rb_cloexec_dup2(int oldfd,int newfd)302 rb_cloexec_dup2(int oldfd, int newfd)
303 {
304 int ret;
305
306 /* When oldfd == newfd, dup2 succeeds but dup3 fails with EINVAL.
307 * rb_cloexec_dup2 succeeds as dup2. */
308 if (oldfd == newfd) {
309 ret = newfd;
310 }
311 else {
312 #if defined(HAVE_DUP3) && defined(O_CLOEXEC)
313 static int try_dup3 = 1;
314 if (2 < newfd && try_dup3) {
315 ret = dup3(oldfd, newfd, O_CLOEXEC);
316 if (ret != -1)
317 return ret;
318 /* dup3 is available since Linux 2.6.27, glibc 2.9. */
319 if (errno == ENOSYS) {
320 try_dup3 = 0;
321 ret = dup2(oldfd, newfd);
322 }
323 }
324 else {
325 ret = dup2(oldfd, newfd);
326 }
327 #else
328 ret = dup2(oldfd, newfd);
329 #endif
330 if (ret < 0) return ret;
331 }
332 rb_maygvl_fd_fix_cloexec(ret);
333 return ret;
334 }
335
336 static int
rb_fd_set_nonblock(int fd)337 rb_fd_set_nonblock(int fd)
338 {
339 #ifdef _WIN32
340 return rb_w32_set_nonblock(fd);
341 #elif defined(F_GETFL)
342 int oflags = fcntl(fd, F_GETFL);
343
344 if (oflags == -1)
345 return -1;
346 if (oflags & O_NONBLOCK)
347 return 0;
348 oflags |= O_NONBLOCK;
349 return fcntl(fd, F_SETFL, oflags);
350 #endif
351 return 0;
352 }
353
354 int
rb_cloexec_pipe(int fildes[2])355 rb_cloexec_pipe(int fildes[2])
356 {
357 int ret;
358
359 #if defined(HAVE_PIPE2)
360 static int try_pipe2 = 1;
361 if (try_pipe2) {
362 ret = pipe2(fildes, O_CLOEXEC | RUBY_PIPE_NONBLOCK_DEFAULT);
363 if (ret != -1)
364 return ret;
365 /* pipe2 is available since Linux 2.6.27, glibc 2.9. */
366 if (errno == ENOSYS) {
367 try_pipe2 = 0;
368 ret = pipe(fildes);
369 }
370 }
371 else {
372 ret = pipe(fildes);
373 }
374 #else
375 ret = pipe(fildes);
376 #endif
377 if (ret < 0) return ret;
378 #ifdef __CYGWIN__
379 if (ret == 0 && fildes[1] == -1) {
380 close(fildes[0]);
381 fildes[0] = -1;
382 errno = ENFILE;
383 return -1;
384 }
385 #endif
386 rb_maygvl_fd_fix_cloexec(fildes[0]);
387 rb_maygvl_fd_fix_cloexec(fildes[1]);
388 if (RUBY_PIPE_NONBLOCK_DEFAULT) {
389 rb_fd_set_nonblock(fildes[0]);
390 rb_fd_set_nonblock(fildes[1]);
391 }
392 return ret;
393 }
394
395 int
rb_cloexec_fcntl_dupfd(int fd,int minfd)396 rb_cloexec_fcntl_dupfd(int fd, int minfd)
397 {
398 int ret;
399
400 #if defined(HAVE_FCNTL) && defined(F_DUPFD_CLOEXEC) && defined(F_DUPFD)
401 static int try_dupfd_cloexec = 1;
402 if (try_dupfd_cloexec) {
403 ret = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
404 if (ret != -1) {
405 if (ret <= 2)
406 rb_maygvl_fd_fix_cloexec(ret);
407 return ret;
408 }
409 /* F_DUPFD_CLOEXEC is available since Linux 2.6.24. Linux 2.6.18 fails with EINVAL */
410 if (errno == EINVAL) {
411 ret = fcntl(fd, F_DUPFD, minfd);
412 if (ret != -1) {
413 try_dupfd_cloexec = 0;
414 }
415 }
416 }
417 else {
418 ret = fcntl(fd, F_DUPFD, minfd);
419 }
420 #elif defined(HAVE_FCNTL) && defined(F_DUPFD)
421 ret = fcntl(fd, F_DUPFD, minfd);
422 #elif defined(HAVE_DUP)
423 ret = dup(fd);
424 if (ret >= 0 && ret < minfd) {
425 const int prev_fd = ret;
426 ret = rb_cloexec_fcntl_dupfd(fd, minfd);
427 close(prev_fd);
428 }
429 return ret;
430 #else
431 # error "dup() or fcntl(F_DUPFD) must be supported."
432 #endif
433 if (ret < 0) return ret;
434 rb_maygvl_fd_fix_cloexec(ret);
435 return ret;
436 }
437
438 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
439 #define ARGF argf_of(argf)
440
441 #define GetWriteIO(io) rb_io_get_write_io(io)
442
443 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf.len)
444 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf.len)
445 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf.ptr+(fptr)->rbuf.off)
446 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
447
448 #define READ_CHAR_PENDING(fptr) ((fptr)->cbuf.len)
449 #define READ_CHAR_PENDING_COUNT(fptr) ((fptr)->cbuf.len)
450 #define READ_CHAR_PENDING_PTR(fptr) ((fptr)->cbuf.ptr+(fptr)->cbuf.off)
451
452 #if defined(_WIN32)
453 #define WAIT_FD_IN_WIN32(fptr) \
454 (rb_w32_io_cancelable_p((fptr)->fd) ? 0 : rb_thread_wait_fd((fptr)->fd))
455 #else
456 #define WAIT_FD_IN_WIN32(fptr)
457 #endif
458
459 #define READ_CHECK(fptr) do {\
460 if (!READ_DATA_PENDING(fptr)) {\
461 WAIT_FD_IN_WIN32(fptr);\
462 rb_io_check_closed(fptr);\
463 }\
464 } while(0)
465
466 #ifndef S_ISSOCK
467 # ifdef _S_ISSOCK
468 # define S_ISSOCK(m) _S_ISSOCK(m)
469 # else
470 # ifdef _S_IFSOCK
471 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
472 # else
473 # ifdef S_IFSOCK
474 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
475 # endif
476 # endif
477 # endif
478 #endif
479
480 static int io_fflush(rb_io_t *);
481 static rb_io_t *flush_before_seek(rb_io_t *fptr);
482
483 #define NEED_NEWLINE_DECORATOR_ON_READ(fptr) ((fptr)->mode & FMODE_TEXTMODE)
484 #define NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) ((fptr)->mode & FMODE_TEXTMODE)
485 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
486 /* Windows */
487 # define DEFAULT_TEXTMODE FMODE_TEXTMODE
488 # define TEXTMODE_NEWLINE_DECORATOR_ON_WRITE ECONV_CRLF_NEWLINE_DECORATOR
489 /*
490 * CRLF newline is set as default newline decorator.
491 * If only CRLF newline conversion is needed, we use binary IO process
492 * with OS's text mode for IO performance improvement.
493 * If encoding conversion is needed or a user sets text mode, we use encoding
494 * conversion IO process and universal newline decorator by default.
495 */
496 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || (fptr)->encs.ecflags & ~ECONV_CRLF_NEWLINE_DECORATOR)
497 #define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || ((fptr)->encs.ecflags & ((ECONV_DECORATOR_MASK & ~ECONV_CRLF_NEWLINE_DECORATOR)|ECONV_STATEFUL_DECORATOR_MASK)))
498 #define SET_BINARY_MODE(fptr) setmode((fptr)->fd, O_BINARY)
499
500 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) do {\
501 if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {\
502 if (((fptr)->mode & FMODE_READABLE) &&\
503 !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
504 setmode((fptr)->fd, O_BINARY);\
505 }\
506 else {\
507 setmode((fptr)->fd, O_TEXT);\
508 }\
509 }\
510 } while(0)
511
512 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) do {\
513 if ((enc2) && ((ecflags) & ECONV_DEFAULT_NEWLINE_DECORATOR)) {\
514 (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
515 }\
516 } while(0)
517
518 /*
519 * IO unread with taking care of removed '\r' in text mode.
520 */
521 static void
io_unread(rb_io_t * fptr)522 io_unread(rb_io_t *fptr)
523 {
524 off_t r, pos;
525 ssize_t read_size;
526 long i;
527 long newlines = 0;
528 long extra_max;
529 char *p;
530 char *buf;
531
532 rb_io_check_closed(fptr);
533 if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
534 return;
535 }
536
537 errno = 0;
538 if (!rb_w32_fd_is_text(fptr->fd)) {
539 r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
540 if (r < 0 && errno) {
541 if (errno == ESPIPE)
542 fptr->mode |= FMODE_DUPLEX;
543 return;
544 }
545
546 fptr->rbuf.off = 0;
547 fptr->rbuf.len = 0;
548 return;
549 }
550
551 pos = lseek(fptr->fd, 0, SEEK_CUR);
552 if (pos < 0 && errno) {
553 if (errno == ESPIPE)
554 fptr->mode |= FMODE_DUPLEX;
555 return;
556 }
557
558 /* add extra offset for removed '\r' in rbuf */
559 extra_max = (long)(pos - fptr->rbuf.len);
560 p = fptr->rbuf.ptr + fptr->rbuf.off;
561
562 /* if the end of rbuf is '\r', rbuf doesn't have '\r' within rbuf.len */
563 if (*(fptr->rbuf.ptr + fptr->rbuf.capa - 1) == '\r') {
564 newlines++;
565 }
566
567 for (i = 0; i < fptr->rbuf.len; i++) {
568 if (*p == '\n') newlines++;
569 if (extra_max == newlines) break;
570 p++;
571 }
572
573 buf = ALLOC_N(char, fptr->rbuf.len + newlines);
574 while (newlines >= 0) {
575 r = lseek(fptr->fd, pos - fptr->rbuf.len - newlines, SEEK_SET);
576 if (newlines == 0) break;
577 if (r < 0) {
578 newlines--;
579 continue;
580 }
581 read_size = _read(fptr->fd, buf, fptr->rbuf.len + newlines);
582 if (read_size < 0) {
583 int e = errno;
584 free(buf);
585 rb_syserr_fail_path(e, fptr->pathv);
586 }
587 if (read_size == fptr->rbuf.len) {
588 lseek(fptr->fd, r, SEEK_SET);
589 break;
590 }
591 else {
592 newlines--;
593 }
594 }
595 free(buf);
596 fptr->rbuf.off = 0;
597 fptr->rbuf.len = 0;
598 return;
599 }
600
601 /*
602 * We use io_seek to back cursor position when changing mode from text to binary,
603 * but stdin and pipe cannot seek back. Stdin and pipe read should use encoding
604 * conversion for working properly with mode change.
605 *
606 * Return previous translation mode.
607 */
608 static inline int
set_binary_mode_with_seek_cur(rb_io_t * fptr)609 set_binary_mode_with_seek_cur(rb_io_t *fptr)
610 {
611 if (!rb_w32_fd_is_text(fptr->fd)) return O_BINARY;
612
613 if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
614 return setmode(fptr->fd, O_BINARY);
615 }
616 flush_before_seek(fptr);
617 return setmode(fptr->fd, O_BINARY);
618 }
619 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) set_binary_mode_with_seek_cur(fptr)
620
621 #else
622 /* Unix */
623 # define DEFAULT_TEXTMODE 0
624 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || NEED_NEWLINE_DECORATOR_ON_READ(fptr))
625 #define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) || ((fptr)->encs.ecflags & (ECONV_DECORATOR_MASK|ECONV_STATEFUL_DECORATOR_MASK)))
626 #define SET_BINARY_MODE(fptr) (void)(fptr)
627 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) (void)(fptr)
628 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) ((void)(enc2), (void)(ecflags))
629 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) (void)(fptr)
630 #endif
631
632 #if !defined HAVE_SHUTDOWN && !defined shutdown
633 #define shutdown(a,b) 0
634 #endif
635
636 #if defined(_WIN32)
637 #define is_socket(fd, path) rb_w32_is_socket(fd)
638 #elif !defined(S_ISSOCK)
639 #define is_socket(fd, path) 0
640 #else
641 static int
is_socket(int fd,VALUE path)642 is_socket(int fd, VALUE path)
643 {
644 struct stat sbuf;
645 if (fstat(fd, &sbuf) < 0)
646 rb_sys_fail_path(path);
647 return S_ISSOCK(sbuf.st_mode);
648 }
649 #endif
650
651 static const char closed_stream[] = "closed stream";
652
653 static void
io_fd_check_closed(int fd)654 io_fd_check_closed(int fd)
655 {
656 if (fd < 0) {
657 rb_thread_check_ints(); /* check for ruby_error_stream_closed */
658 rb_raise(rb_eIOError, closed_stream);
659 }
660 }
661
662 void
rb_eof_error(void)663 rb_eof_error(void)
664 {
665 rb_raise(rb_eEOFError, "end of file reached");
666 }
667
668 VALUE
rb_io_taint_check(VALUE io)669 rb_io_taint_check(VALUE io)
670 {
671 rb_check_frozen(io);
672 return io;
673 }
674
675 void
rb_io_check_initialized(rb_io_t * fptr)676 rb_io_check_initialized(rb_io_t *fptr)
677 {
678 if (!fptr) {
679 rb_raise(rb_eIOError, "uninitialized stream");
680 }
681 }
682
683 void
rb_io_check_closed(rb_io_t * fptr)684 rb_io_check_closed(rb_io_t *fptr)
685 {
686 rb_io_check_initialized(fptr);
687 io_fd_check_closed(fptr->fd);
688 }
689
690 static rb_io_t *
rb_io_get_fptr(VALUE io)691 rb_io_get_fptr(VALUE io)
692 {
693 rb_io_t *fptr = RFILE(io)->fptr;
694 rb_io_check_initialized(fptr);
695 return fptr;
696 }
697
698 VALUE
rb_io_get_io(VALUE io)699 rb_io_get_io(VALUE io)
700 {
701 return rb_convert_type_with_id(io, T_FILE, "IO", idTo_io);
702 }
703
704 VALUE
rb_io_check_io(VALUE io)705 rb_io_check_io(VALUE io)
706 {
707 return rb_check_convert_type_with_id(io, T_FILE, "IO", idTo_io);
708 }
709
710 VALUE
rb_io_get_write_io(VALUE io)711 rb_io_get_write_io(VALUE io)
712 {
713 VALUE write_io;
714 write_io = rb_io_get_fptr(io)->tied_io_for_writing;
715 if (write_io) {
716 return write_io;
717 }
718 return io;
719 }
720
721 VALUE
rb_io_set_write_io(VALUE io,VALUE w)722 rb_io_set_write_io(VALUE io, VALUE w)
723 {
724 VALUE write_io;
725 rb_io_t *fptr = rb_io_get_fptr(io);
726 if (!RTEST(w)) {
727 w = 0;
728 }
729 else {
730 GetWriteIO(w);
731 }
732 write_io = fptr->tied_io_for_writing;
733 fptr->tied_io_for_writing = w;
734 return write_io ? write_io : Qnil;
735 }
736
737 /*
738 * call-seq:
739 * IO.try_convert(obj) -> io or nil
740 *
741 * Try to convert <i>obj</i> into an IO, using to_io method.
742 * Returns converted IO or +nil+ if <i>obj</i> cannot be converted
743 * for any reason.
744 *
745 * IO.try_convert(STDOUT) #=> STDOUT
746 * IO.try_convert("STDOUT") #=> nil
747 *
748 * require 'zlib'
749 * f = open("/tmp/zz.gz") #=> #<File:/tmp/zz.gz>
750 * z = Zlib::GzipReader.open(f) #=> #<Zlib::GzipReader:0x81d8744>
751 * IO.try_convert(z) #=> #<File:/tmp/zz.gz>
752 *
753 */
754 static VALUE
rb_io_s_try_convert(VALUE dummy,VALUE io)755 rb_io_s_try_convert(VALUE dummy, VALUE io)
756 {
757 return rb_io_check_io(io);
758 }
759
760 #if !(defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32))
761 static void
io_unread(rb_io_t * fptr)762 io_unread(rb_io_t *fptr)
763 {
764 off_t r;
765 rb_io_check_closed(fptr);
766 if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX)
767 return;
768 /* xxx: target position may be negative if buffer is filled by ungetc */
769 errno = 0;
770 r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
771 if (r < 0 && errno) {
772 if (errno == ESPIPE)
773 fptr->mode |= FMODE_DUPLEX;
774 return;
775 }
776 fptr->rbuf.off = 0;
777 fptr->rbuf.len = 0;
778 return;
779 }
780 #endif
781
782 static rb_encoding *io_input_encoding(rb_io_t *fptr);
783
784 static void
io_ungetbyte(VALUE str,rb_io_t * fptr)785 io_ungetbyte(VALUE str, rb_io_t *fptr)
786 {
787 long len = RSTRING_LEN(str);
788
789 if (fptr->rbuf.ptr == NULL) {
790 const int min_capa = IO_RBUF_CAPA_FOR(fptr);
791 fptr->rbuf.off = 0;
792 fptr->rbuf.len = 0;
793 #if SIZEOF_LONG > SIZEOF_INT
794 if (len > INT_MAX)
795 rb_raise(rb_eIOError, "ungetbyte failed");
796 #endif
797 if (len > min_capa)
798 fptr->rbuf.capa = (int)len;
799 else
800 fptr->rbuf.capa = min_capa;
801 fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
802 }
803 if (fptr->rbuf.capa < len + fptr->rbuf.len) {
804 rb_raise(rb_eIOError, "ungetbyte failed");
805 }
806 if (fptr->rbuf.off < len) {
807 MEMMOVE(fptr->rbuf.ptr+fptr->rbuf.capa-fptr->rbuf.len,
808 fptr->rbuf.ptr+fptr->rbuf.off,
809 char, fptr->rbuf.len);
810 fptr->rbuf.off = fptr->rbuf.capa-fptr->rbuf.len;
811 }
812 fptr->rbuf.off-=(int)len;
813 fptr->rbuf.len+=(int)len;
814 MEMMOVE(fptr->rbuf.ptr+fptr->rbuf.off, RSTRING_PTR(str), char, len);
815 }
816
817 static rb_io_t *
flush_before_seek(rb_io_t * fptr)818 flush_before_seek(rb_io_t *fptr)
819 {
820 if (io_fflush(fptr) < 0)
821 rb_sys_fail(0);
822 io_unread(fptr);
823 errno = 0;
824 return fptr;
825 }
826
827 #define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, (ofs), (whence)))
828 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
829
830 #ifndef SEEK_CUR
831 # define SEEK_SET 0
832 # define SEEK_CUR 1
833 # define SEEK_END 2
834 #endif
835
836 void
rb_io_check_char_readable(rb_io_t * fptr)837 rb_io_check_char_readable(rb_io_t *fptr)
838 {
839 rb_io_check_closed(fptr);
840 if (!(fptr->mode & FMODE_READABLE)) {
841 rb_raise(rb_eIOError, "not opened for reading");
842 }
843 if (fptr->wbuf.len) {
844 if (io_fflush(fptr) < 0)
845 rb_sys_fail(0);
846 }
847 if (fptr->tied_io_for_writing) {
848 rb_io_t *wfptr;
849 GetOpenFile(fptr->tied_io_for_writing, wfptr);
850 if (io_fflush(wfptr) < 0)
851 rb_sys_fail(0);
852 }
853 }
854
855 void
rb_io_check_byte_readable(rb_io_t * fptr)856 rb_io_check_byte_readable(rb_io_t *fptr)
857 {
858 rb_io_check_char_readable(fptr);
859 if (READ_CHAR_PENDING(fptr)) {
860 rb_raise(rb_eIOError, "byte oriented read for character buffered IO");
861 }
862 }
863
864 void
rb_io_check_readable(rb_io_t * fptr)865 rb_io_check_readable(rb_io_t *fptr)
866 {
867 rb_io_check_byte_readable(fptr);
868 }
869
870 static rb_encoding*
io_read_encoding(rb_io_t * fptr)871 io_read_encoding(rb_io_t *fptr)
872 {
873 if (fptr->encs.enc) {
874 return fptr->encs.enc;
875 }
876 return rb_default_external_encoding();
877 }
878
879 static rb_encoding*
io_input_encoding(rb_io_t * fptr)880 io_input_encoding(rb_io_t *fptr)
881 {
882 if (fptr->encs.enc2) {
883 return fptr->encs.enc2;
884 }
885 return io_read_encoding(fptr);
886 }
887
888 void
rb_io_check_writable(rb_io_t * fptr)889 rb_io_check_writable(rb_io_t *fptr)
890 {
891 rb_io_check_closed(fptr);
892 if (!(fptr->mode & FMODE_WRITABLE)) {
893 rb_raise(rb_eIOError, "not opened for writing");
894 }
895 if (fptr->rbuf.len) {
896 io_unread(fptr);
897 }
898 }
899
900 int
rb_io_read_pending(rb_io_t * fptr)901 rb_io_read_pending(rb_io_t *fptr)
902 {
903 /* This function is used for bytes and chars. Confusing. */
904 if (READ_CHAR_PENDING(fptr))
905 return 1; /* should raise? */
906 return READ_DATA_PENDING(fptr);
907 }
908
909 void
rb_io_read_check(rb_io_t * fptr)910 rb_io_read_check(rb_io_t *fptr)
911 {
912 if (!READ_DATA_PENDING(fptr)) {
913 rb_thread_wait_fd(fptr->fd);
914 }
915 return;
916 }
917
918 int
rb_gc_for_fd(int err)919 rb_gc_for_fd(int err)
920 {
921 if (err == EMFILE || err == ENFILE || err == ENOMEM) {
922 rb_gc();
923 return 1;
924 }
925 return 0;
926 }
927
928 static int
ruby_dup(int orig)929 ruby_dup(int orig)
930 {
931 int fd;
932
933 fd = rb_cloexec_dup(orig);
934 if (fd < 0) {
935 int e = errno;
936 if (rb_gc_for_fd(e)) {
937 fd = rb_cloexec_dup(orig);
938 }
939 if (fd < 0) {
940 rb_syserr_fail(e, 0);
941 }
942 }
943 rb_update_max_fd(fd);
944 return fd;
945 }
946
947 static VALUE
io_alloc(VALUE klass)948 io_alloc(VALUE klass)
949 {
950 NEWOBJ_OF(io, struct RFile, klass, T_FILE);
951
952 io->fptr = 0;
953
954 return (VALUE)io;
955 }
956
957 #ifndef S_ISREG
958 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
959 #endif
960
961 struct io_internal_read_struct {
962 int fd;
963 int nonblock;
964 void *buf;
965 size_t capa;
966 };
967
968 struct io_internal_write_struct {
969 int fd;
970 const void *buf;
971 size_t capa;
972 };
973
974 #ifdef HAVE_WRITEV
975 struct io_internal_writev_struct {
976 int fd;
977 int iovcnt;
978 const struct iovec *iov;
979 };
980 #endif
981
982 static int nogvl_wait_for_single_fd(int fd, short events);
983 static VALUE
internal_read_func(void * ptr)984 internal_read_func(void *ptr)
985 {
986 struct io_internal_read_struct *iis = ptr;
987 ssize_t r;
988 retry:
989 r = read(iis->fd, iis->buf, iis->capa);
990 if (r < 0 && !iis->nonblock) {
991 int e = errno;
992 if (e == EAGAIN || e == EWOULDBLOCK) {
993 if (nogvl_wait_for_single_fd(iis->fd, RB_WAITFD_IN) != -1) {
994 goto retry;
995 }
996 errno = e;
997 }
998 }
999 return r;
1000 }
1001
1002 #if defined __APPLE__
1003 # define do_write_retry(code) do {ret = code;} while (ret == -1 && errno == EPROTOTYPE)
1004 #else
1005 # define do_write_retry(code) ret = code
1006 #endif
1007 static VALUE
internal_write_func(void * ptr)1008 internal_write_func(void *ptr)
1009 {
1010 struct io_internal_write_struct *iis = ptr;
1011 ssize_t ret;
1012 do_write_retry(write(iis->fd, iis->buf, iis->capa));
1013 return (VALUE)ret;
1014 }
1015
1016 static void*
internal_write_func2(void * ptr)1017 internal_write_func2(void *ptr)
1018 {
1019 return (void*)internal_write_func(ptr);
1020 }
1021
1022 #ifdef HAVE_WRITEV
1023 static VALUE
internal_writev_func(void * ptr)1024 internal_writev_func(void *ptr)
1025 {
1026 struct io_internal_writev_struct *iis = ptr;
1027 ssize_t ret;
1028 do_write_retry(writev(iis->fd, iis->iov, iis->iovcnt));
1029 return (VALUE)ret;
1030 }
1031 #endif
1032
1033 static ssize_t
rb_read_internal(int fd,void * buf,size_t count)1034 rb_read_internal(int fd, void *buf, size_t count)
1035 {
1036 struct io_internal_read_struct iis;
1037
1038 iis.fd = fd;
1039 iis.nonblock = 0;
1040 iis.buf = buf;
1041 iis.capa = count;
1042
1043 return (ssize_t)rb_thread_io_blocking_region(internal_read_func, &iis, fd);
1044 }
1045
1046 static ssize_t
rb_write_internal(int fd,const void * buf,size_t count)1047 rb_write_internal(int fd, const void *buf, size_t count)
1048 {
1049 struct io_internal_write_struct iis;
1050 iis.fd = fd;
1051 iis.buf = buf;
1052 iis.capa = count;
1053
1054 return (ssize_t)rb_thread_io_blocking_region(internal_write_func, &iis, fd);
1055 }
1056
1057 static ssize_t
rb_write_internal2(int fd,const void * buf,size_t count)1058 rb_write_internal2(int fd, const void *buf, size_t count)
1059 {
1060 struct io_internal_write_struct iis;
1061 iis.fd = fd;
1062 iis.buf = buf;
1063 iis.capa = count;
1064
1065 return (ssize_t)rb_thread_call_without_gvl2(internal_write_func2, &iis,
1066 RUBY_UBF_IO, NULL);
1067 }
1068
1069 #ifdef HAVE_WRITEV
1070 static ssize_t
rb_writev_internal(int fd,const struct iovec * iov,int iovcnt)1071 rb_writev_internal(int fd, const struct iovec *iov, int iovcnt)
1072 {
1073 struct io_internal_writev_struct iis;
1074 iis.fd = fd;
1075 iis.iov = iov;
1076 iis.iovcnt = iovcnt;
1077
1078 return (ssize_t)rb_thread_io_blocking_region(internal_writev_func, &iis, fd);
1079 }
1080 #endif
1081
1082 static VALUE
io_flush_buffer_sync(void * arg)1083 io_flush_buffer_sync(void *arg)
1084 {
1085 rb_io_t *fptr = arg;
1086 long l = fptr->wbuf.len;
1087 ssize_t r = write(fptr->fd, fptr->wbuf.ptr+fptr->wbuf.off, (size_t)l);
1088
1089 if (fptr->wbuf.len <= r) {
1090 fptr->wbuf.off = 0;
1091 fptr->wbuf.len = 0;
1092 return 0;
1093 }
1094 if (0 <= r) {
1095 fptr->wbuf.off += (int)r;
1096 fptr->wbuf.len -= (int)r;
1097 errno = EAGAIN;
1098 }
1099 return (VALUE)-1;
1100 }
1101
1102 static void*
io_flush_buffer_sync2(void * arg)1103 io_flush_buffer_sync2(void *arg)
1104 {
1105 VALUE result = io_flush_buffer_sync(arg);
1106
1107 /*
1108 * rb_thread_call_without_gvl2 uses 0 as interrupted.
1109 * So, we need to avoid to use 0.
1110 */
1111 return !result ? (void*)1 : (void*)result;
1112 }
1113
1114 static VALUE
io_flush_buffer_async(VALUE arg)1115 io_flush_buffer_async(VALUE arg)
1116 {
1117 rb_io_t *fptr = (rb_io_t *)arg;
1118 return rb_thread_io_blocking_region(io_flush_buffer_sync, fptr, fptr->fd);
1119 }
1120
1121 static VALUE
io_flush_buffer_async2(VALUE arg)1122 io_flush_buffer_async2(VALUE arg)
1123 {
1124 rb_io_t *fptr = (rb_io_t *)arg;
1125 VALUE ret;
1126
1127 ret = (VALUE)rb_thread_call_without_gvl2(io_flush_buffer_sync2, fptr,
1128 RUBY_UBF_IO, NULL);
1129
1130 if (!ret) {
1131 /* pending async interrupt is there. */
1132 errno = EAGAIN;
1133 return -1;
1134 }
1135 else if (ret == 1) {
1136 return 0;
1137 }
1138 return ret;
1139 }
1140
1141 static inline int
io_flush_buffer(rb_io_t * fptr)1142 io_flush_buffer(rb_io_t *fptr)
1143 {
1144 if (fptr->write_lock) {
1145 if (rb_mutex_owned_p(fptr->write_lock))
1146 return (int)io_flush_buffer_async2((VALUE)fptr);
1147 else
1148 return (int)rb_mutex_synchronize(fptr->write_lock, io_flush_buffer_async2, (VALUE)fptr);
1149 }
1150 else {
1151 return (int)io_flush_buffer_async((VALUE)fptr);
1152 }
1153 }
1154
1155 static int
io_fflush(rb_io_t * fptr)1156 io_fflush(rb_io_t *fptr)
1157 {
1158 rb_io_check_closed(fptr);
1159 if (fptr->wbuf.len == 0)
1160 return 0;
1161 while (fptr->wbuf.len > 0 && io_flush_buffer(fptr) != 0) {
1162 if (!rb_io_wait_writable(fptr->fd))
1163 return -1;
1164 rb_io_check_closed(fptr);
1165 }
1166 return 0;
1167 }
1168
1169 int
rb_io_wait_readable(int f)1170 rb_io_wait_readable(int f)
1171 {
1172 io_fd_check_closed(f);
1173 switch (errno) {
1174 case EINTR:
1175 #if defined(ERESTART)
1176 case ERESTART:
1177 #endif
1178 rb_thread_check_ints();
1179 return TRUE;
1180
1181 case EAGAIN:
1182 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1183 case EWOULDBLOCK:
1184 #endif
1185 rb_thread_wait_fd(f);
1186 return TRUE;
1187
1188 default:
1189 return FALSE;
1190 }
1191 }
1192
1193 int
rb_io_wait_writable(int f)1194 rb_io_wait_writable(int f)
1195 {
1196 io_fd_check_closed(f);
1197 switch (errno) {
1198 case EINTR:
1199 #if defined(ERESTART)
1200 case ERESTART:
1201 #endif
1202 /*
1203 * In old Linux, several special files under /proc and /sys don't handle
1204 * select properly. Thus we need avoid to call if don't use O_NONBLOCK.
1205 * Otherwise, we face nasty hang up. Sigh.
1206 * e.g. http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=31b07093c44a7a442394d44423e21d783f5523b8
1207 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=31b07093c44a7a442394d44423e21d783f5523b8
1208 * In EINTR case, we only need to call RUBY_VM_CHECK_INTS_BLOCKING().
1209 * Then rb_thread_check_ints() is enough.
1210 */
1211 rb_thread_check_ints();
1212 return TRUE;
1213
1214 case EAGAIN:
1215 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1216 case EWOULDBLOCK:
1217 #endif
1218 rb_thread_fd_writable(f);
1219 return TRUE;
1220
1221 default:
1222 return FALSE;
1223 }
1224 }
1225
1226 static void
make_writeconv(rb_io_t * fptr)1227 make_writeconv(rb_io_t *fptr)
1228 {
1229 if (!fptr->writeconv_initialized) {
1230 const char *senc, *denc;
1231 rb_encoding *enc;
1232 int ecflags;
1233 VALUE ecopts;
1234
1235 fptr->writeconv_initialized = 1;
1236
1237 ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_READ_MASK;
1238 ecopts = fptr->encs.ecopts;
1239
1240 if (!fptr->encs.enc || (fptr->encs.enc == rb_ascii8bit_encoding() && !fptr->encs.enc2)) {
1241 /* no encoding conversion */
1242 fptr->writeconv_pre_ecflags = 0;
1243 fptr->writeconv_pre_ecopts = Qnil;
1244 fptr->writeconv = rb_econv_open_opts("", "", ecflags, ecopts);
1245 if (!fptr->writeconv)
1246 rb_exc_raise(rb_econv_open_exc("", "", ecflags));
1247 fptr->writeconv_asciicompat = Qnil;
1248 }
1249 else {
1250 enc = fptr->encs.enc2 ? fptr->encs.enc2 : fptr->encs.enc;
1251 senc = rb_econv_asciicompat_encoding(rb_enc_name(enc));
1252 if (!senc && !(fptr->encs.ecflags & ECONV_STATEFUL_DECORATOR_MASK)) {
1253 /* single conversion */
1254 fptr->writeconv_pre_ecflags = ecflags;
1255 fptr->writeconv_pre_ecopts = ecopts;
1256 fptr->writeconv = NULL;
1257 fptr->writeconv_asciicompat = Qnil;
1258 }
1259 else {
1260 /* double conversion */
1261 fptr->writeconv_pre_ecflags = ecflags & ~ECONV_STATEFUL_DECORATOR_MASK;
1262 fptr->writeconv_pre_ecopts = ecopts;
1263 if (senc) {
1264 denc = rb_enc_name(enc);
1265 fptr->writeconv_asciicompat = rb_str_new2(senc);
1266 }
1267 else {
1268 senc = denc = "";
1269 fptr->writeconv_asciicompat = rb_str_new2(rb_enc_name(enc));
1270 }
1271 ecflags = fptr->encs.ecflags & (ECONV_ERROR_HANDLER_MASK|ECONV_STATEFUL_DECORATOR_MASK);
1272 ecopts = fptr->encs.ecopts;
1273 fptr->writeconv = rb_econv_open_opts(senc, denc, ecflags, ecopts);
1274 if (!fptr->writeconv)
1275 rb_exc_raise(rb_econv_open_exc(senc, denc, ecflags));
1276 }
1277 }
1278 }
1279 }
1280
1281 /* writing functions */
1282 struct binwrite_arg {
1283 rb_io_t *fptr;
1284 VALUE str;
1285 const char *ptr;
1286 long length;
1287 };
1288
1289 struct write_arg {
1290 VALUE io;
1291 VALUE str;
1292 int nosync;
1293 };
1294
1295 #ifdef HAVE_WRITEV
1296 static VALUE
io_binwrite_string(VALUE arg)1297 io_binwrite_string(VALUE arg)
1298 {
1299 struct binwrite_arg *p = (struct binwrite_arg *)arg;
1300 rb_io_t *fptr = p->fptr;
1301 long r;
1302
1303 if (fptr->wbuf.len) {
1304 struct iovec iov[2];
1305
1306 iov[0].iov_base = fptr->wbuf.ptr+fptr->wbuf.off;
1307 iov[0].iov_len = fptr->wbuf.len;
1308 iov[1].iov_base = (char *)p->ptr;
1309 iov[1].iov_len = p->length;
1310
1311 r = rb_writev_internal(fptr->fd, iov, 2);
1312
1313 if (r < 0)
1314 return r;
1315
1316 if (fptr->wbuf.len <= r) {
1317 r -= fptr->wbuf.len;
1318 fptr->wbuf.off = 0;
1319 fptr->wbuf.len = 0;
1320 }
1321 else {
1322 fptr->wbuf.off += (int)r;
1323 fptr->wbuf.len -= (int)r;
1324 r = 0L;
1325 }
1326 }
1327 else {
1328 r = rb_write_internal(fptr->fd, p->ptr, p->length);
1329 }
1330
1331 return r;
1332 }
1333 #else
1334 static VALUE
io_binwrite_string(VALUE arg)1335 io_binwrite_string(VALUE arg)
1336 {
1337 struct binwrite_arg *p = (struct binwrite_arg *)arg;
1338 rb_io_t *fptr = p->fptr;
1339 long l, len;
1340
1341 l = len = p->length;
1342
1343 if (fptr->wbuf.len) {
1344 if (fptr->wbuf.len+len <= fptr->wbuf.capa) {
1345 if (fptr->wbuf.capa < fptr->wbuf.off+fptr->wbuf.len+len) {
1346 MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
1347 fptr->wbuf.off = 0;
1348 }
1349 MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, p->ptr, char, len);
1350 fptr->wbuf.len += (int)len;
1351 l = 0;
1352 }
1353 if (io_fflush(fptr) < 0)
1354 return -2L; /* fail in fflush */
1355 if (l == 0)
1356 return len;
1357 }
1358
1359 if (fptr->stdio_file != stderr && !rb_thread_fd_writable(fptr->fd))
1360 rb_io_check_closed(fptr);
1361
1362 return rb_write_internal(p->fptr->fd, p->ptr, p->length);
1363 }
1364 #endif
1365
1366 static long
io_binwrite(VALUE str,const char * ptr,long len,rb_io_t * fptr,int nosync)1367 io_binwrite(VALUE str, const char *ptr, long len, rb_io_t *fptr, int nosync)
1368 {
1369 long n, r, offset = 0;
1370
1371 /* don't write anything if current thread has a pending interrupt. */
1372 rb_thread_check_ints();
1373
1374 if ((n = len) <= 0) return n;
1375 if (fptr->wbuf.ptr == NULL && !(!nosync && (fptr->mode & FMODE_SYNC))) {
1376 fptr->wbuf.off = 0;
1377 fptr->wbuf.len = 0;
1378 fptr->wbuf.capa = IO_WBUF_CAPA_MIN;
1379 fptr->wbuf.ptr = ALLOC_N(char, fptr->wbuf.capa);
1380 fptr->write_lock = rb_mutex_new();
1381 rb_mutex_allow_trap(fptr->write_lock, 1);
1382 }
1383 if ((!nosync && (fptr->mode & (FMODE_SYNC|FMODE_TTY))) ||
1384 (fptr->wbuf.ptr && fptr->wbuf.capa <= fptr->wbuf.len + len)) {
1385 struct binwrite_arg arg;
1386
1387 arg.fptr = fptr;
1388 arg.str = str;
1389 retry:
1390 arg.ptr = ptr + offset;
1391 arg.length = n;
1392 if (fptr->write_lock) {
1393 r = rb_mutex_synchronize(fptr->write_lock, io_binwrite_string, (VALUE)&arg);
1394 }
1395 else {
1396 r = io_binwrite_string((VALUE)&arg);
1397 }
1398 /* xxx: other threads may modify given string. */
1399 if (r == n) return len;
1400 if (0 <= r) {
1401 offset += r;
1402 n -= r;
1403 errno = EAGAIN;
1404 }
1405 if (r == -2L)
1406 return -1L;
1407 if (rb_io_wait_writable(fptr->fd)) {
1408 rb_io_check_closed(fptr);
1409 if (offset < len)
1410 goto retry;
1411 }
1412 return -1L;
1413 }
1414
1415 if (fptr->wbuf.off) {
1416 if (fptr->wbuf.len)
1417 MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
1418 fptr->wbuf.off = 0;
1419 }
1420 MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr+offset, char, len);
1421 fptr->wbuf.len += (int)len;
1422 return len;
1423 }
1424
1425 # define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \
1426 (fmode & FMODE_TEXTMODE) ? (c) : (a))
1427
1428 #define MODE_BTXMODE(a, b, c, d, e, f) ((fmode & FMODE_EXCL) ? \
1429 MODE_BTMODE(d, e, f) : \
1430 MODE_BTMODE(a, b, c))
1431
1432 static VALUE
do_writeconv(VALUE str,rb_io_t * fptr,int * converted)1433 do_writeconv(VALUE str, rb_io_t *fptr, int *converted)
1434 {
1435 if (NEED_WRITECONV(fptr)) {
1436 VALUE common_encoding = Qnil;
1437 SET_BINARY_MODE(fptr);
1438
1439 make_writeconv(fptr);
1440
1441 if (fptr->writeconv) {
1442 #define fmode (fptr->mode)
1443 if (!NIL_P(fptr->writeconv_asciicompat))
1444 common_encoding = fptr->writeconv_asciicompat;
1445 else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1) && !rb_enc_asciicompat(rb_enc_get(str))) {
1446 rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
1447 rb_enc_name(rb_enc_get(str)));
1448 }
1449 #undef fmode
1450 }
1451 else {
1452 if (fptr->encs.enc2)
1453 common_encoding = rb_enc_from_encoding(fptr->encs.enc2);
1454 else if (fptr->encs.enc != rb_ascii8bit_encoding())
1455 common_encoding = rb_enc_from_encoding(fptr->encs.enc);
1456 }
1457
1458 if (!NIL_P(common_encoding)) {
1459 str = rb_str_encode(str, common_encoding,
1460 fptr->writeconv_pre_ecflags, fptr->writeconv_pre_ecopts);
1461 *converted = 1;
1462 }
1463
1464 if (fptr->writeconv) {
1465 str = rb_econv_str_convert(fptr->writeconv, str, ECONV_PARTIAL_INPUT);
1466 *converted = 1;
1467 }
1468 }
1469 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
1470 #define fmode (fptr->mode)
1471 else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1)) {
1472 if ((fptr->mode & FMODE_READABLE) &&
1473 !(fptr->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
1474 setmode(fptr->fd, O_BINARY);
1475 }
1476 else {
1477 setmode(fptr->fd, O_TEXT);
1478 }
1479 if (!rb_enc_asciicompat(rb_enc_get(str))) {
1480 rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
1481 rb_enc_name(rb_enc_get(str)));
1482 }
1483 }
1484 #undef fmode
1485 #endif
1486 return str;
1487 }
1488
1489 static long
io_fwrite(VALUE str,rb_io_t * fptr,int nosync)1490 io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
1491 {
1492 int converted = 0;
1493 VALUE tmp;
1494 long n, len;
1495 const char *ptr;
1496 #ifdef _WIN32
1497 if (fptr->mode & FMODE_TTY) {
1498 long len = rb_w32_write_console(str, fptr->fd);
1499 if (len > 0) return len;
1500 }
1501 #endif
1502 str = do_writeconv(str, fptr, &converted);
1503 if (converted)
1504 OBJ_FREEZE(str);
1505
1506 tmp = rb_str_tmp_frozen_acquire(str);
1507 RSTRING_GETMEM(tmp, ptr, len);
1508 n = io_binwrite(tmp, ptr, len, fptr, nosync);
1509 rb_str_tmp_frozen_release(str, tmp);
1510
1511 return n;
1512 }
1513
1514 ssize_t
rb_io_bufwrite(VALUE io,const void * buf,size_t size)1515 rb_io_bufwrite(VALUE io, const void *buf, size_t size)
1516 {
1517 rb_io_t *fptr;
1518
1519 GetOpenFile(io, fptr);
1520 rb_io_check_writable(fptr);
1521 return (ssize_t)io_binwrite(0, buf, (long)size, fptr, 0);
1522 }
1523
1524 static VALUE
io_write(VALUE io,VALUE str,int nosync)1525 io_write(VALUE io, VALUE str, int nosync)
1526 {
1527 rb_io_t *fptr;
1528 long n;
1529 VALUE tmp;
1530
1531 io = GetWriteIO(io);
1532 str = rb_obj_as_string(str);
1533 tmp = rb_io_check_io(io);
1534 if (NIL_P(tmp)) {
1535 /* port is not IO, call write method for it. */
1536 return rb_funcall(io, id_write, 1, str);
1537 }
1538 io = tmp;
1539 if (RSTRING_LEN(str) == 0) return INT2FIX(0);
1540
1541 GetOpenFile(io, fptr);
1542 rb_io_check_writable(fptr);
1543
1544 n = io_fwrite(str, fptr, nosync);
1545 if (n < 0L) rb_sys_fail_path(fptr->pathv);
1546
1547 return LONG2FIX(n);
1548 }
1549
1550 #ifdef HAVE_WRITEV
1551 struct binwritev_arg {
1552 rb_io_t *fptr;
1553 const struct iovec *iov;
1554 int iovcnt;
1555 };
1556
1557 static VALUE
call_writev_internal(VALUE arg)1558 call_writev_internal(VALUE arg)
1559 {
1560 struct binwritev_arg *p = (struct binwritev_arg *)arg;
1561 return rb_writev_internal(p->fptr->fd, p->iov, p->iovcnt);
1562 }
1563
1564 static long
io_binwritev(struct iovec * iov,int iovcnt,rb_io_t * fptr)1565 io_binwritev(struct iovec *iov, int iovcnt, rb_io_t *fptr)
1566 {
1567 int i;
1568 long r, total = 0, written_len = 0;
1569
1570 /* don't write anything if current thread has a pending interrupt. */
1571 rb_thread_check_ints();
1572
1573 if (iovcnt == 0) return 0;
1574 for (i = 1; i < iovcnt; i++) total += iov[i].iov_len;
1575
1576 if (fptr->wbuf.ptr == NULL && !(fptr->mode & FMODE_SYNC)) {
1577 fptr->wbuf.off = 0;
1578 fptr->wbuf.len = 0;
1579 fptr->wbuf.capa = IO_WBUF_CAPA_MIN;
1580 fptr->wbuf.ptr = ALLOC_N(char, fptr->wbuf.capa);
1581 fptr->write_lock = rb_mutex_new();
1582 rb_mutex_allow_trap(fptr->write_lock, 1);
1583 }
1584
1585 if (fptr->wbuf.ptr && fptr->wbuf.len) {
1586 long offset = fptr->wbuf.off + fptr->wbuf.len;
1587 if (offset + total <= fptr->wbuf.capa) {
1588 for (i = 1; i < iovcnt; i++) {
1589 memcpy(fptr->wbuf.ptr+offset, iov[i].iov_base, iov[i].iov_len);
1590 offset += iov[i].iov_len;
1591 }
1592 fptr->wbuf.len += total;
1593 return total;
1594 }
1595 else {
1596 iov[0].iov_base = fptr->wbuf.ptr + fptr->wbuf.off;
1597 iov[0].iov_len = fptr->wbuf.len;
1598 }
1599 }
1600 else {
1601 iov++;
1602 if (!--iovcnt) return 0;
1603 }
1604
1605 retry:
1606 if (fptr->write_lock) {
1607 struct binwritev_arg arg;
1608 arg.fptr = fptr;
1609 arg.iov = iov;
1610 arg.iovcnt = iovcnt;
1611 r = rb_mutex_synchronize(fptr->write_lock, call_writev_internal, (VALUE)&arg);
1612 }
1613 else {
1614 r = rb_writev_internal(fptr->fd, iov, iovcnt);
1615 }
1616
1617 if (r >= 0) {
1618 written_len += r;
1619 if (fptr->wbuf.ptr && fptr->wbuf.len) {
1620 if (written_len < fptr->wbuf.len) {
1621 fptr->wbuf.off += r;
1622 fptr->wbuf.len -= r;
1623 }
1624 else {
1625 written_len -= fptr->wbuf.len;
1626 fptr->wbuf.off = 0;
1627 fptr->wbuf.len = 0;
1628 }
1629 }
1630 if (written_len == total) return total;
1631
1632 while (r >= (ssize_t)iov->iov_len) {
1633 /* iovcnt > 0 */
1634 r -= iov->iov_len;
1635 iov->iov_len = 0;
1636 iov++;
1637 if (!--iovcnt) return total;
1638 /* defensive check: written_len should == total */
1639 }
1640 iov->iov_base = (char *)iov->iov_base + r;
1641 iov->iov_len -= r;
1642
1643 errno = EAGAIN;
1644 }
1645 if (rb_io_wait_writable(fptr->fd)) {
1646 rb_io_check_closed(fptr);
1647 goto retry;
1648 }
1649
1650 return -1L;
1651 }
1652
1653 static long
io_fwritev(int argc,VALUE * argv,rb_io_t * fptr)1654 io_fwritev(int argc, VALUE *argv, rb_io_t *fptr)
1655 {
1656 int i, converted, iovcnt = argc + 1;
1657 long n;
1658 VALUE v1, v2, str, tmp, *tmp_array;
1659 struct iovec *iov;
1660
1661 iov = ALLOCV_N(struct iovec, v1, iovcnt);
1662 tmp_array = ALLOCV_N(VALUE, v2, argc);
1663
1664 for (i = 0; i < argc; i++) {
1665 str = rb_obj_as_string(argv[i]);
1666 converted = 0;
1667 str = do_writeconv(str, fptr, &converted);
1668 if (converted)
1669 OBJ_FREEZE(str);
1670
1671 tmp = rb_str_tmp_frozen_acquire(str);
1672 tmp_array[i] = tmp;
1673 /* iov[0] is reserved for buffer of fptr */
1674 iov[i+1].iov_base = RSTRING_PTR(tmp);
1675 iov[i+1].iov_len = RSTRING_LEN(tmp);
1676 }
1677
1678 n = io_binwritev(iov, iovcnt, fptr);
1679 if (v1) ALLOCV_END(v1);
1680
1681 for (i = 0; i < argc; i++) {
1682 rb_str_tmp_frozen_release(argv[i], tmp_array[i]);
1683 }
1684
1685 if (v2) ALLOCV_END(v2);
1686
1687 return n;
1688 }
1689
1690 static int
iovcnt_ok(int iovcnt)1691 iovcnt_ok(int iovcnt)
1692 {
1693 #ifdef IOV_MAX
1694 return iovcnt < IOV_MAX;
1695 #else /* GNU/Hurd has writev, but no IOV_MAX */
1696 return 1;
1697 #endif
1698 }
1699 #endif /* HAVE_WRITEV */
1700
1701 static VALUE
io_writev(int argc,VALUE * argv,VALUE io)1702 io_writev(int argc, VALUE *argv, VALUE io)
1703 {
1704 rb_io_t *fptr;
1705 long n;
1706 VALUE tmp, total = INT2FIX(0);
1707 int i, cnt = 1;
1708
1709 io = GetWriteIO(io);
1710 tmp = rb_io_check_io(io);
1711 if (NIL_P(tmp)) {
1712 /* port is not IO, call write method for it. */
1713 return rb_funcallv(io, id_write, argc, argv);
1714 }
1715 io = tmp;
1716
1717 GetOpenFile(io, fptr);
1718 rb_io_check_writable(fptr);
1719
1720 for (i = 0; i < argc; i += cnt) {
1721 #ifdef HAVE_WRITEV
1722 if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) && iovcnt_ok(cnt = argc - i)) {
1723 n = io_fwritev(cnt, &argv[i], fptr);
1724 }
1725 else
1726 #endif
1727 {
1728 cnt = 1;
1729 /* sync at last item */
1730 n = io_fwrite(rb_obj_as_string(argv[i]), fptr, (i < argc-1));
1731 }
1732 if (n < 0L) rb_sys_fail_path(fptr->pathv);
1733 total = rb_fix_plus(LONG2FIX(n), total);
1734 }
1735
1736 return total;
1737 }
1738
1739 /*
1740 * call-seq:
1741 * ios.write(string, ...) -> integer
1742 *
1743 * Writes the given strings to <em>ios</em>. The stream must be opened
1744 * for writing. Arguments that are not a string will be converted
1745 * to a string using <code>to_s</code>. Returns the number of bytes
1746 * written in total.
1747 *
1748 * count = $stdout.write("This is", " a test\n")
1749 * puts "That was #{count} bytes of data"
1750 *
1751 * <em>produces:</em>
1752 *
1753 * This is a test
1754 * That was 15 bytes of data
1755 */
1756
1757 static VALUE
io_write_m(int argc,VALUE * argv,VALUE io)1758 io_write_m(int argc, VALUE *argv, VALUE io)
1759 {
1760 if (argc != 1) {
1761 return io_writev(argc, argv, io);
1762 }
1763 else {
1764 VALUE str = argv[0];
1765 return io_write(io, str, 0);
1766 }
1767 }
1768
1769 VALUE
rb_io_write(VALUE io,VALUE str)1770 rb_io_write(VALUE io, VALUE str)
1771 {
1772 return rb_funcallv(io, id_write, 1, &str);
1773 }
1774
1775 static VALUE
rb_io_writev(VALUE io,int argc,VALUE * argv)1776 rb_io_writev(VALUE io, int argc, VALUE *argv)
1777 {
1778 if (argc > 1 && rb_obj_method_arity(io, id_write) == 1) {
1779 if (io != rb_stderr && RTEST(ruby_verbose)) {
1780 VALUE klass = CLASS_OF(io);
1781 char sep = FL_TEST(klass, FL_SINGLETON) ? (klass = io, '.') : '#';
1782 rb_warning("%+"PRIsVALUE"%c""write is outdated interface"
1783 " which accepts just one argument",
1784 klass, sep);
1785 }
1786 do rb_io_write(io, *argv++); while (--argc);
1787 return argv[0]; /* unused right now */
1788 }
1789 return rb_funcallv(io, id_write, argc, argv);
1790 }
1791
1792 /*
1793 * call-seq:
1794 * ios << obj -> ios
1795 *
1796 * String Output---Writes <i>obj</i> to <em>ios</em>.
1797 * <i>obj</i> will be converted to a string using
1798 * <code>to_s</code>.
1799 *
1800 * $stdout << "Hello " << "world!\n"
1801 *
1802 * <em>produces:</em>
1803 *
1804 * Hello world!
1805 */
1806
1807
1808 VALUE
rb_io_addstr(VALUE io,VALUE str)1809 rb_io_addstr(VALUE io, VALUE str)
1810 {
1811 rb_io_write(io, str);
1812 return io;
1813 }
1814
1815 #ifdef HAVE_FSYNC
1816 static VALUE
nogvl_fsync(void * ptr)1817 nogvl_fsync(void *ptr)
1818 {
1819 rb_io_t *fptr = ptr;
1820
1821 #ifdef _WIN32
1822 if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) != FILE_TYPE_DISK)
1823 return 0;
1824 #endif
1825 return (VALUE)fsync(fptr->fd);
1826 }
1827 #endif
1828
1829 VALUE
rb_io_flush_raw(VALUE io,int sync)1830 rb_io_flush_raw(VALUE io, int sync)
1831 {
1832 rb_io_t *fptr;
1833
1834 if (!RB_TYPE_P(io, T_FILE)) {
1835 return rb_funcall(io, id_flush, 0);
1836 }
1837
1838 io = GetWriteIO(io);
1839 GetOpenFile(io, fptr);
1840
1841 if (fptr->mode & FMODE_WRITABLE) {
1842 if (io_fflush(fptr) < 0)
1843 rb_sys_fail(0);
1844 }
1845 if (fptr->mode & FMODE_READABLE) {
1846 io_unread(fptr);
1847 }
1848
1849 return io;
1850 }
1851
1852 /*
1853 * call-seq:
1854 * ios.flush -> ios
1855 *
1856 * Flushes any buffered data within <em>ios</em> to the underlying
1857 * operating system (note that this is Ruby internal buffering only;
1858 * the OS may buffer the data as well).
1859 *
1860 * $stdout.print "no newline"
1861 * $stdout.flush
1862 *
1863 * <em>produces:</em>
1864 *
1865 * no newline
1866 */
1867
1868 VALUE
rb_io_flush(VALUE io)1869 rb_io_flush(VALUE io)
1870 {
1871 return rb_io_flush_raw(io, 1);
1872 }
1873
1874 /*
1875 * call-seq:
1876 * ios.pos -> integer
1877 * ios.tell -> integer
1878 *
1879 * Returns the current offset (in bytes) of <em>ios</em>.
1880 *
1881 * f = File.new("testfile")
1882 * f.pos #=> 0
1883 * f.gets #=> "This is line one\n"
1884 * f.pos #=> 17
1885 */
1886
1887 static VALUE
rb_io_tell(VALUE io)1888 rb_io_tell(VALUE io)
1889 {
1890 rb_io_t *fptr;
1891 off_t pos;
1892
1893 GetOpenFile(io, fptr);
1894 pos = io_tell(fptr);
1895 if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
1896 pos -= fptr->rbuf.len;
1897 return OFFT2NUM(pos);
1898 }
1899
1900 static VALUE
rb_io_seek(VALUE io,VALUE offset,int whence)1901 rb_io_seek(VALUE io, VALUE offset, int whence)
1902 {
1903 rb_io_t *fptr;
1904 off_t pos;
1905
1906 pos = NUM2OFFT(offset);
1907 GetOpenFile(io, fptr);
1908 pos = io_seek(fptr, pos, whence);
1909 if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
1910
1911 return INT2FIX(0);
1912 }
1913
1914 static int
interpret_seek_whence(VALUE vwhence)1915 interpret_seek_whence(VALUE vwhence)
1916 {
1917 if (vwhence == sym_SET)
1918 return SEEK_SET;
1919 if (vwhence == sym_CUR)
1920 return SEEK_CUR;
1921 if (vwhence == sym_END)
1922 return SEEK_END;
1923 #ifdef SEEK_DATA
1924 if (vwhence == sym_DATA)
1925 return SEEK_DATA;
1926 #endif
1927 #ifdef SEEK_HOLE
1928 if (vwhence == sym_HOLE)
1929 return SEEK_HOLE;
1930 #endif
1931 return NUM2INT(vwhence);
1932 }
1933
1934 /*
1935 * call-seq:
1936 * ios.seek(amount, whence=IO::SEEK_SET) -> 0
1937 *
1938 * Seeks to a given offset <i>anInteger</i> in the stream according to
1939 * the value of <i>whence</i>:
1940 *
1941 * :CUR or IO::SEEK_CUR | Seeks to _amount_ plus current position
1942 * ----------------------+--------------------------------------------------
1943 * :END or IO::SEEK_END | Seeks to _amount_ plus end of stream (you
1944 * | probably want a negative value for _amount_)
1945 * ----------------------+--------------------------------------------------
1946 * :SET or IO::SEEK_SET | Seeks to the absolute location given by _amount_
1947 *
1948 * Example:
1949 *
1950 * f = File.new("testfile")
1951 * f.seek(-13, IO::SEEK_END) #=> 0
1952 * f.readline #=> "And so on...\n"
1953 */
1954
1955 static VALUE
rb_io_seek_m(int argc,VALUE * argv,VALUE io)1956 rb_io_seek_m(int argc, VALUE *argv, VALUE io)
1957 {
1958 VALUE offset, ptrname;
1959 int whence = SEEK_SET;
1960
1961 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
1962 whence = interpret_seek_whence(ptrname);
1963 }
1964
1965 return rb_io_seek(io, offset, whence);
1966 }
1967
1968 /*
1969 * call-seq:
1970 * ios.pos = integer -> integer
1971 *
1972 * Seeks to the given position (in bytes) in <em>ios</em>.
1973 * It is not guaranteed that seeking to the right position when <em>ios</em>
1974 * is textmode.
1975 *
1976 * f = File.new("testfile")
1977 * f.pos = 17
1978 * f.gets #=> "This is line two\n"
1979 */
1980
1981 static VALUE
rb_io_set_pos(VALUE io,VALUE offset)1982 rb_io_set_pos(VALUE io, VALUE offset)
1983 {
1984 rb_io_t *fptr;
1985 off_t pos;
1986
1987 pos = NUM2OFFT(offset);
1988 GetOpenFile(io, fptr);
1989 pos = io_seek(fptr, pos, SEEK_SET);
1990 if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
1991
1992 return OFFT2NUM(pos);
1993 }
1994
1995 static void clear_readconv(rb_io_t *fptr);
1996
1997 /*
1998 * call-seq:
1999 * ios.rewind -> 0
2000 *
2001 * Positions <em>ios</em> to the beginning of input, resetting
2002 * <code>lineno</code> to zero.
2003 *
2004 * f = File.new("testfile")
2005 * f.readline #=> "This is line one\n"
2006 * f.rewind #=> 0
2007 * f.lineno #=> 0
2008 * f.readline #=> "This is line one\n"
2009 *
2010 * Note that it cannot be used with streams such as pipes, ttys, and sockets.
2011 */
2012
2013 static VALUE
rb_io_rewind(VALUE io)2014 rb_io_rewind(VALUE io)
2015 {
2016 rb_io_t *fptr;
2017
2018 GetOpenFile(io, fptr);
2019 if (io_seek(fptr, 0L, 0) < 0 && errno) rb_sys_fail_path(fptr->pathv);
2020 if (io == ARGF.current_file) {
2021 ARGF.lineno -= fptr->lineno;
2022 }
2023 fptr->lineno = 0;
2024 if (fptr->readconv) {
2025 clear_readconv(fptr);
2026 }
2027
2028 return INT2FIX(0);
2029 }
2030
2031 static int
fptr_wait_readable(rb_io_t * fptr)2032 fptr_wait_readable(rb_io_t *fptr)
2033 {
2034 int ret = rb_io_wait_readable(fptr->fd);
2035
2036 if (ret)
2037 rb_io_check_closed(fptr);
2038 return ret;
2039 }
2040
2041 static int
io_fillbuf(rb_io_t * fptr)2042 io_fillbuf(rb_io_t *fptr)
2043 {
2044 ssize_t r;
2045
2046 if (fptr->rbuf.ptr == NULL) {
2047 fptr->rbuf.off = 0;
2048 fptr->rbuf.len = 0;
2049 fptr->rbuf.capa = IO_RBUF_CAPA_FOR(fptr);
2050 fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
2051 #ifdef _WIN32
2052 fptr->rbuf.capa--;
2053 #endif
2054 }
2055 if (fptr->rbuf.len == 0) {
2056 retry:
2057 {
2058 r = rb_read_internal(fptr->fd, fptr->rbuf.ptr, fptr->rbuf.capa);
2059 }
2060 if (r < 0) {
2061 if (fptr_wait_readable(fptr))
2062 goto retry;
2063 {
2064 int e = errno;
2065 VALUE path = rb_sprintf("fd:%d ", fptr->fd);
2066 if (!NIL_P(fptr->pathv)) {
2067 rb_str_append(path, fptr->pathv);
2068 }
2069 rb_syserr_fail_path(e, path);
2070 }
2071 }
2072 if (r > 0) rb_io_check_closed(fptr);
2073 fptr->rbuf.off = 0;
2074 fptr->rbuf.len = (int)r; /* r should be <= rbuf_capa */
2075 if (r == 0)
2076 return -1; /* EOF */
2077 }
2078 return 0;
2079 }
2080
2081 /*
2082 * call-seq:
2083 * ios.eof -> true or false
2084 * ios.eof? -> true or false
2085 *
2086 * Returns true if <em>ios</em> is at end of file that means
2087 * there are no more data to read.
2088 * The stream must be opened for reading or an <code>IOError</code> will be
2089 * raised.
2090 *
2091 * f = File.new("testfile")
2092 * dummy = f.readlines
2093 * f.eof #=> true
2094 *
2095 * If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
2096 * blocks until the other end sends some data or closes it.
2097 *
2098 * r, w = IO.pipe
2099 * Thread.new { sleep 1; w.close }
2100 * r.eof? #=> true after 1 second blocking
2101 *
2102 * r, w = IO.pipe
2103 * Thread.new { sleep 1; w.puts "a" }
2104 * r.eof? #=> false after 1 second blocking
2105 *
2106 * r, w = IO.pipe
2107 * r.eof? # blocks forever
2108 *
2109 * Note that <code>IO#eof?</code> reads data to the input byte buffer.
2110 * So <code>IO#sysread</code> may not behave as you intend with
2111 * <code>IO#eof?</code>, unless you call <code>IO#rewind</code>
2112 * first (which is not available for some streams).
2113 */
2114
2115 VALUE
rb_io_eof(VALUE io)2116 rb_io_eof(VALUE io)
2117 {
2118 rb_io_t *fptr;
2119
2120 GetOpenFile(io, fptr);
2121 rb_io_check_char_readable(fptr);
2122
2123 if (READ_CHAR_PENDING(fptr)) return Qfalse;
2124 if (READ_DATA_PENDING(fptr)) return Qfalse;
2125 READ_CHECK(fptr);
2126 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2127 if (!NEED_READCONV(fptr) && NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
2128 return eof(fptr->fd) ? Qtrue : Qfalse;
2129 }
2130 #endif
2131 if (io_fillbuf(fptr) < 0) {
2132 return Qtrue;
2133 }
2134 return Qfalse;
2135 }
2136
2137 /*
2138 * call-seq:
2139 * ios.sync -> true or false
2140 *
2141 * Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
2142 * true, all output is immediately flushed to the underlying operating
2143 * system and is not buffered by Ruby internally. See also
2144 * <code>IO#fsync</code>.
2145 *
2146 * f = File.new("testfile")
2147 * f.sync #=> false
2148 */
2149
2150 static VALUE
rb_io_sync(VALUE io)2151 rb_io_sync(VALUE io)
2152 {
2153 rb_io_t *fptr;
2154
2155 io = GetWriteIO(io);
2156 GetOpenFile(io, fptr);
2157 return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
2158 }
2159
2160 #ifdef HAVE_FSYNC
2161
2162 /*
2163 * call-seq:
2164 * ios.sync = boolean -> boolean
2165 *
2166 * Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
2167 * When sync mode is true, all output is immediately flushed to the
2168 * underlying operating system and is not buffered internally. Returns
2169 * the new state. See also <code>IO#fsync</code>.
2170 *
2171 * f = File.new("testfile")
2172 * f.sync = true
2173 */
2174
2175 static VALUE
rb_io_set_sync(VALUE io,VALUE sync)2176 rb_io_set_sync(VALUE io, VALUE sync)
2177 {
2178 rb_io_t *fptr;
2179
2180 io = GetWriteIO(io);
2181 GetOpenFile(io, fptr);
2182 if (RTEST(sync)) {
2183 fptr->mode |= FMODE_SYNC;
2184 }
2185 else {
2186 fptr->mode &= ~FMODE_SYNC;
2187 }
2188 return sync;
2189 }
2190
2191 /*
2192 * call-seq:
2193 * ios.fsync -> 0 or nil
2194 *
2195 * Immediately writes all buffered data in <em>ios</em> to disk.
2196 * Note that <code>fsync</code> differs from
2197 * using <code>IO#sync=</code>. The latter ensures that data is flushed
2198 * from Ruby's buffers, but does not guarantee that the underlying
2199 * operating system actually writes it to disk.
2200 *
2201 * <code>NotImplementedError</code> is raised
2202 * if the underlying operating system does not support <em>fsync(2)</em>.
2203 */
2204
2205 static VALUE
rb_io_fsync(VALUE io)2206 rb_io_fsync(VALUE io)
2207 {
2208 rb_io_t *fptr;
2209
2210 io = GetWriteIO(io);
2211 GetOpenFile(io, fptr);
2212
2213 if (io_fflush(fptr) < 0)
2214 rb_sys_fail(0);
2215 if ((int)rb_thread_io_blocking_region(nogvl_fsync, fptr, fptr->fd) < 0)
2216 rb_sys_fail_path(fptr->pathv);
2217 return INT2FIX(0);
2218 }
2219 #else
2220 # define rb_io_fsync rb_f_notimplement
2221 # define rb_io_sync rb_f_notimplement
2222 static VALUE
rb_io_set_sync(VALUE io,VALUE sync)2223 rb_io_set_sync(VALUE io, VALUE sync)
2224 {
2225 rb_notimplement();
2226 UNREACHABLE;
2227 }
2228 #endif
2229
2230 #ifdef HAVE_FDATASYNC
2231 static VALUE
nogvl_fdatasync(void * ptr)2232 nogvl_fdatasync(void *ptr)
2233 {
2234 rb_io_t *fptr = ptr;
2235
2236 #ifdef _WIN32
2237 if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) != FILE_TYPE_DISK)
2238 return 0;
2239 #endif
2240 return (VALUE)fdatasync(fptr->fd);
2241 }
2242
2243 /*
2244 * call-seq:
2245 * ios.fdatasync -> 0 or nil
2246 *
2247 * Immediately writes all buffered data in <em>ios</em> to disk.
2248 *
2249 * If the underlying operating system does not support <em>fdatasync(2)</em>,
2250 * <code>IO#fsync</code> is called instead (which might raise a
2251 * <code>NotImplementedError</code>).
2252 */
2253
2254 static VALUE
rb_io_fdatasync(VALUE io)2255 rb_io_fdatasync(VALUE io)
2256 {
2257 rb_io_t *fptr;
2258
2259 io = GetWriteIO(io);
2260 GetOpenFile(io, fptr);
2261
2262 if (io_fflush(fptr) < 0)
2263 rb_sys_fail(0);
2264
2265 if ((int)rb_thread_io_blocking_region(nogvl_fdatasync, fptr, fptr->fd) == 0)
2266 return INT2FIX(0);
2267
2268 /* fall back */
2269 return rb_io_fsync(io);
2270 }
2271 #else
2272 #define rb_io_fdatasync rb_io_fsync
2273 #endif
2274
2275 /*
2276 * call-seq:
2277 * ios.fileno -> integer
2278 * ios.to_i -> integer
2279 *
2280 * Returns an integer representing the numeric file descriptor for
2281 * <em>ios</em>.
2282 *
2283 * $stdin.fileno #=> 0
2284 * $stdout.fileno #=> 1
2285 */
2286
2287 static VALUE
rb_io_fileno(VALUE io)2288 rb_io_fileno(VALUE io)
2289 {
2290 rb_io_t *fptr = RFILE(io)->fptr;
2291 int fd;
2292
2293 rb_io_check_closed(fptr);
2294 fd = fptr->fd;
2295 return INT2FIX(fd);
2296 }
2297
2298
2299 /*
2300 * call-seq:
2301 * ios.pid -> integer
2302 *
2303 * Returns the process ID of a child process associated with
2304 * <em>ios</em>. This will be set by <code>IO.popen</code>.
2305 *
2306 * pipe = IO.popen("-")
2307 * if pipe
2308 * $stderr.puts "In parent, child pid is #{pipe.pid}"
2309 * else
2310 * $stderr.puts "In child, pid is #{$$}"
2311 * end
2312 *
2313 * <em>produces:</em>
2314 *
2315 * In child, pid is 26209
2316 * In parent, child pid is 26209
2317 */
2318
2319 static VALUE
rb_io_pid(VALUE io)2320 rb_io_pid(VALUE io)
2321 {
2322 rb_io_t *fptr;
2323
2324 GetOpenFile(io, fptr);
2325 if (!fptr->pid)
2326 return Qnil;
2327 return PIDT2NUM(fptr->pid);
2328 }
2329
2330
2331 /*
2332 * call-seq:
2333 * ios.inspect -> string
2334 *
2335 * Return a string describing this IO object.
2336 */
2337
2338 static VALUE
rb_io_inspect(VALUE obj)2339 rb_io_inspect(VALUE obj)
2340 {
2341 rb_io_t *fptr;
2342 VALUE result;
2343 static const char closed[] = " (closed)";
2344
2345 fptr = RFILE(obj)->fptr;
2346 if (!fptr) return rb_any_to_s(obj);
2347 result = rb_str_new_cstr("#<");
2348 rb_str_append(result, rb_class_name(CLASS_OF(obj)));
2349 rb_str_cat2(result, ":");
2350 if (NIL_P(fptr->pathv)) {
2351 if (fptr->fd < 0) {
2352 rb_str_cat(result, closed+1, strlen(closed)-1);
2353 }
2354 else {
2355 rb_str_catf(result, "fd %d", fptr->fd);
2356 }
2357 }
2358 else {
2359 rb_str_append(result, fptr->pathv);
2360 if (fptr->fd < 0) {
2361 rb_str_cat(result, closed, strlen(closed));
2362 }
2363 }
2364 return rb_str_cat2(result, ">");
2365 }
2366
2367 /*
2368 * call-seq:
2369 * ios.to_io -> ios
2370 *
2371 * Returns <em>ios</em>.
2372 */
2373
2374 static VALUE
rb_io_to_io(VALUE io)2375 rb_io_to_io(VALUE io)
2376 {
2377 return io;
2378 }
2379
2380 /* reading functions */
2381 static long
read_buffered_data(char * ptr,long len,rb_io_t * fptr)2382 read_buffered_data(char *ptr, long len, rb_io_t *fptr)
2383 {
2384 int n;
2385
2386 n = READ_DATA_PENDING_COUNT(fptr);
2387 if (n <= 0) return 0;
2388 if (n > len) n = (int)len;
2389 MEMMOVE(ptr, fptr->rbuf.ptr+fptr->rbuf.off, char, n);
2390 fptr->rbuf.off += n;
2391 fptr->rbuf.len -= n;
2392 return n;
2393 }
2394
2395 static long
io_bufread(char * ptr,long len,rb_io_t * fptr)2396 io_bufread(char *ptr, long len, rb_io_t *fptr)
2397 {
2398 long offset = 0;
2399 long n = len;
2400 long c;
2401
2402 if (READ_DATA_PENDING(fptr) == 0) {
2403 while (n > 0) {
2404 again:
2405 c = rb_read_internal(fptr->fd, ptr+offset, n);
2406 if (c == 0) break;
2407 if (c < 0) {
2408 if (fptr_wait_readable(fptr))
2409 goto again;
2410 return -1;
2411 }
2412 offset += c;
2413 if ((n -= c) <= 0) break;
2414 }
2415 return len - n;
2416 }
2417
2418 while (n > 0) {
2419 c = read_buffered_data(ptr+offset, n, fptr);
2420 if (c > 0) {
2421 offset += c;
2422 if ((n -= c) <= 0) break;
2423 }
2424 rb_io_check_closed(fptr);
2425 if (io_fillbuf(fptr) < 0) {
2426 break;
2427 }
2428 }
2429 return len - n;
2430 }
2431
2432 static int io_setstrbuf(VALUE *str, long len);
2433
2434 struct bufread_arg {
2435 char *str_ptr;
2436 long len;
2437 rb_io_t *fptr;
2438 };
2439
2440 static VALUE
bufread_call(VALUE arg)2441 bufread_call(VALUE arg)
2442 {
2443 struct bufread_arg *p = (struct bufread_arg *)arg;
2444 p->len = io_bufread(p->str_ptr, p->len, p->fptr);
2445 return Qundef;
2446 }
2447
2448 static long
io_fread(VALUE str,long offset,long size,rb_io_t * fptr)2449 io_fread(VALUE str, long offset, long size, rb_io_t *fptr)
2450 {
2451 long len;
2452 struct bufread_arg arg;
2453
2454 io_setstrbuf(&str, offset + size);
2455 arg.str_ptr = RSTRING_PTR(str) + offset;
2456 arg.len = size;
2457 arg.fptr = fptr;
2458 rb_str_locktmp_ensure(str, bufread_call, (VALUE)&arg);
2459 len = arg.len;
2460 if (len < 0) rb_sys_fail_path(fptr->pathv);
2461 return len;
2462 }
2463
2464 ssize_t
rb_io_bufread(VALUE io,void * buf,size_t size)2465 rb_io_bufread(VALUE io, void *buf, size_t size)
2466 {
2467 rb_io_t *fptr;
2468
2469 GetOpenFile(io, fptr);
2470 rb_io_check_readable(fptr);
2471 return (ssize_t)io_bufread(buf, (long)size, fptr);
2472 }
2473
2474 static long
remain_size(rb_io_t * fptr)2475 remain_size(rb_io_t *fptr)
2476 {
2477 struct stat st;
2478 off_t siz = READ_DATA_PENDING_COUNT(fptr);
2479 off_t pos;
2480
2481 if (fstat(fptr->fd, &st) == 0 && S_ISREG(st.st_mode)
2482 #if defined(__HAIKU__)
2483 && (st.st_dev > 3)
2484 #endif
2485 )
2486 {
2487 if (io_fflush(fptr) < 0)
2488 rb_sys_fail(0);
2489 pos = lseek(fptr->fd, 0, SEEK_CUR);
2490 if (st.st_size >= pos && pos >= 0) {
2491 siz += st.st_size - pos;
2492 if (siz > LONG_MAX) {
2493 rb_raise(rb_eIOError, "file too big for single read");
2494 }
2495 }
2496 }
2497 else {
2498 siz += BUFSIZ;
2499 }
2500 return (long)siz;
2501 }
2502
2503 static VALUE
io_enc_str(VALUE str,rb_io_t * fptr)2504 io_enc_str(VALUE str, rb_io_t *fptr)
2505 {
2506 OBJ_TAINT(str);
2507 rb_enc_associate(str, io_read_encoding(fptr));
2508 return str;
2509 }
2510
2511 static void
make_readconv(rb_io_t * fptr,int size)2512 make_readconv(rb_io_t *fptr, int size)
2513 {
2514 if (!fptr->readconv) {
2515 int ecflags;
2516 VALUE ecopts;
2517 const char *sname, *dname;
2518 ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_WRITE_MASK;
2519 ecopts = fptr->encs.ecopts;
2520 if (fptr->encs.enc2) {
2521 sname = rb_enc_name(fptr->encs.enc2);
2522 dname = rb_enc_name(fptr->encs.enc);
2523 }
2524 else {
2525 sname = dname = "";
2526 }
2527 fptr->readconv = rb_econv_open_opts(sname, dname, ecflags, ecopts);
2528 if (!fptr->readconv)
2529 rb_exc_raise(rb_econv_open_exc(sname, dname, ecflags));
2530 fptr->cbuf.off = 0;
2531 fptr->cbuf.len = 0;
2532 if (size < IO_CBUF_CAPA_MIN) size = IO_CBUF_CAPA_MIN;
2533 fptr->cbuf.capa = size;
2534 fptr->cbuf.ptr = ALLOC_N(char, fptr->cbuf.capa);
2535 }
2536 }
2537
2538 #define MORE_CHAR_SUSPENDED Qtrue
2539 #define MORE_CHAR_FINISHED Qnil
2540 static VALUE
fill_cbuf(rb_io_t * fptr,int ec_flags)2541 fill_cbuf(rb_io_t *fptr, int ec_flags)
2542 {
2543 const unsigned char *ss, *sp, *se;
2544 unsigned char *ds, *dp, *de;
2545 rb_econv_result_t res;
2546 int putbackable;
2547 int cbuf_len0;
2548 VALUE exc;
2549
2550 ec_flags |= ECONV_PARTIAL_INPUT;
2551
2552 if (fptr->cbuf.len == fptr->cbuf.capa)
2553 return MORE_CHAR_SUSPENDED; /* cbuf full */
2554 if (fptr->cbuf.len == 0)
2555 fptr->cbuf.off = 0;
2556 else if (fptr->cbuf.off + fptr->cbuf.len == fptr->cbuf.capa) {
2557 memmove(fptr->cbuf.ptr, fptr->cbuf.ptr+fptr->cbuf.off, fptr->cbuf.len);
2558 fptr->cbuf.off = 0;
2559 }
2560
2561 cbuf_len0 = fptr->cbuf.len;
2562
2563 while (1) {
2564 ss = sp = (const unsigned char *)fptr->rbuf.ptr + fptr->rbuf.off;
2565 se = sp + fptr->rbuf.len;
2566 ds = dp = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.off + fptr->cbuf.len;
2567 de = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.capa;
2568 res = rb_econv_convert(fptr->readconv, &sp, se, &dp, de, ec_flags);
2569 fptr->rbuf.off += (int)(sp - ss);
2570 fptr->rbuf.len -= (int)(sp - ss);
2571 fptr->cbuf.len += (int)(dp - ds);
2572
2573 putbackable = rb_econv_putbackable(fptr->readconv);
2574 if (putbackable) {
2575 rb_econv_putback(fptr->readconv, (unsigned char *)fptr->rbuf.ptr + fptr->rbuf.off - putbackable, putbackable);
2576 fptr->rbuf.off -= putbackable;
2577 fptr->rbuf.len += putbackable;
2578 }
2579
2580 exc = rb_econv_make_exception(fptr->readconv);
2581 if (!NIL_P(exc))
2582 return exc;
2583
2584 if (cbuf_len0 != fptr->cbuf.len)
2585 return MORE_CHAR_SUSPENDED;
2586
2587 if (res == econv_finished) {
2588 return MORE_CHAR_FINISHED;
2589 }
2590
2591 if (res == econv_source_buffer_empty) {
2592 if (fptr->rbuf.len == 0) {
2593 READ_CHECK(fptr);
2594 if (io_fillbuf(fptr) < 0) {
2595 if (!fptr->readconv) {
2596 return MORE_CHAR_FINISHED;
2597 }
2598 ds = dp = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.off + fptr->cbuf.len;
2599 de = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.capa;
2600 res = rb_econv_convert(fptr->readconv, NULL, NULL, &dp, de, 0);
2601 fptr->cbuf.len += (int)(dp - ds);
2602 rb_econv_check_error(fptr->readconv);
2603 break;
2604 }
2605 }
2606 }
2607 }
2608 if (cbuf_len0 != fptr->cbuf.len)
2609 return MORE_CHAR_SUSPENDED;
2610
2611 return MORE_CHAR_FINISHED;
2612 }
2613
2614 static VALUE
more_char(rb_io_t * fptr)2615 more_char(rb_io_t *fptr)
2616 {
2617 VALUE v;
2618 v = fill_cbuf(fptr, ECONV_AFTER_OUTPUT);
2619 if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED)
2620 rb_exc_raise(v);
2621 return v;
2622 }
2623
2624 static VALUE
io_shift_cbuf(rb_io_t * fptr,int len,VALUE * strp)2625 io_shift_cbuf(rb_io_t *fptr, int len, VALUE *strp)
2626 {
2627 VALUE str = Qnil;
2628 if (strp) {
2629 str = *strp;
2630 if (NIL_P(str)) {
2631 *strp = str = rb_str_new(fptr->cbuf.ptr+fptr->cbuf.off, len);
2632 }
2633 else {
2634 rb_str_cat(str, fptr->cbuf.ptr+fptr->cbuf.off, len);
2635 }
2636 OBJ_TAINT(str);
2637 rb_enc_associate(str, fptr->encs.enc);
2638 }
2639 fptr->cbuf.off += len;
2640 fptr->cbuf.len -= len;
2641 /* xxx: set coderange */
2642 if (fptr->cbuf.len == 0)
2643 fptr->cbuf.off = 0;
2644 else if (fptr->cbuf.capa/2 < fptr->cbuf.off) {
2645 memmove(fptr->cbuf.ptr, fptr->cbuf.ptr+fptr->cbuf.off, fptr->cbuf.len);
2646 fptr->cbuf.off = 0;
2647 }
2648 return str;
2649 }
2650
2651 static int
io_setstrbuf(VALUE * str,long len)2652 io_setstrbuf(VALUE *str, long len)
2653 {
2654 #ifdef _WIN32
2655 len = (len + 1) & ~1L; /* round up for wide char */
2656 #endif
2657 if (NIL_P(*str)) {
2658 *str = rb_str_new(0, len);
2659 return TRUE;
2660 }
2661 else {
2662 VALUE s = StringValue(*str);
2663 long clen = RSTRING_LEN(s);
2664 if (clen >= len) {
2665 rb_str_modify(s);
2666 return FALSE;
2667 }
2668 len -= clen;
2669 }
2670 rb_str_modify_expand(*str, len);
2671 return FALSE;
2672 }
2673
2674 #define MAX_REALLOC_GAP 4096
2675 static void
io_shrink_read_string(VALUE str,long n)2676 io_shrink_read_string(VALUE str, long n)
2677 {
2678 if (rb_str_capacity(str) - n > MAX_REALLOC_GAP) {
2679 rb_str_resize(str, n);
2680 }
2681 }
2682
2683 static void
io_set_read_length(VALUE str,long n,int shrinkable)2684 io_set_read_length(VALUE str, long n, int shrinkable)
2685 {
2686 if (RSTRING_LEN(str) != n) {
2687 rb_str_modify(str);
2688 rb_str_set_len(str, n);
2689 if (shrinkable) io_shrink_read_string(str, n);
2690 }
2691 }
2692
2693 static VALUE
read_all(rb_io_t * fptr,long siz,VALUE str)2694 read_all(rb_io_t *fptr, long siz, VALUE str)
2695 {
2696 long bytes;
2697 long n;
2698 long pos;
2699 rb_encoding *enc;
2700 int cr;
2701 int shrinkable;
2702
2703 if (NEED_READCONV(fptr)) {
2704 int first = !NIL_P(str);
2705 SET_BINARY_MODE(fptr);
2706 shrinkable = io_setstrbuf(&str,0);
2707 make_readconv(fptr, 0);
2708 while (1) {
2709 VALUE v;
2710 if (fptr->cbuf.len) {
2711 if (first) rb_str_set_len(str, first = 0);
2712 io_shift_cbuf(fptr, fptr->cbuf.len, &str);
2713 }
2714 v = fill_cbuf(fptr, 0);
2715 if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED) {
2716 if (fptr->cbuf.len) {
2717 if (first) rb_str_set_len(str, first = 0);
2718 io_shift_cbuf(fptr, fptr->cbuf.len, &str);
2719 }
2720 rb_exc_raise(v);
2721 }
2722 if (v == MORE_CHAR_FINISHED) {
2723 clear_readconv(fptr);
2724 if (first) rb_str_set_len(str, first = 0);
2725 if (shrinkable) io_shrink_read_string(str, RSTRING_LEN(str));
2726 return io_enc_str(str, fptr);
2727 }
2728 }
2729 }
2730
2731 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
2732 bytes = 0;
2733 pos = 0;
2734
2735 enc = io_read_encoding(fptr);
2736 cr = 0;
2737
2738 if (siz == 0) siz = BUFSIZ;
2739 shrinkable = io_setstrbuf(&str, siz);
2740 for (;;) {
2741 READ_CHECK(fptr);
2742 n = io_fread(str, bytes, siz - bytes, fptr);
2743 if (n == 0 && bytes == 0) {
2744 rb_str_set_len(str, 0);
2745 break;
2746 }
2747 bytes += n;
2748 rb_str_set_len(str, bytes);
2749 if (cr != ENC_CODERANGE_BROKEN)
2750 pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
2751 if (bytes < siz) break;
2752 siz += BUFSIZ;
2753 rb_str_modify_expand(str, BUFSIZ);
2754 }
2755 if (shrinkable) io_shrink_read_string(str, RSTRING_LEN(str));
2756 str = io_enc_str(str, fptr);
2757 ENC_CODERANGE_SET(str, cr);
2758 return str;
2759 }
2760
2761 void
rb_io_set_nonblock(rb_io_t * fptr)2762 rb_io_set_nonblock(rb_io_t *fptr)
2763 {
2764 if (rb_fd_set_nonblock(fptr->fd) != 0) {
2765 rb_sys_fail_path(fptr->pathv);
2766 }
2767 }
2768
2769 static VALUE
read_internal_call(VALUE arg)2770 read_internal_call(VALUE arg)
2771 {
2772 struct io_internal_read_struct *iis = (struct io_internal_read_struct *)arg;
2773
2774 return rb_thread_io_blocking_region(internal_read_func, iis, iis->fd);
2775 }
2776
2777 static long
read_internal_locktmp(VALUE str,struct io_internal_read_struct * iis)2778 read_internal_locktmp(VALUE str, struct io_internal_read_struct *iis)
2779 {
2780 return (long)rb_str_locktmp_ensure(str, read_internal_call, (VALUE)iis);
2781 }
2782
2783 static int
no_exception_p(VALUE opts)2784 no_exception_p(VALUE opts)
2785 {
2786 VALUE except;
2787 ID id = id_exception;
2788
2789 rb_get_kwargs(opts, &id, 0, 1, &except);
2790 return except == Qfalse;
2791 }
2792
2793 static VALUE
io_getpartial(int argc,VALUE * argv,VALUE io,VALUE opts,int nonblock)2794 io_getpartial(int argc, VALUE *argv, VALUE io, VALUE opts, int nonblock)
2795 {
2796 rb_io_t *fptr;
2797 VALUE length, str;
2798 long n, len;
2799 struct io_internal_read_struct iis;
2800 int shrinkable;
2801
2802 rb_scan_args(argc, argv, "11", &length, &str);
2803
2804 if ((len = NUM2LONG(length)) < 0) {
2805 rb_raise(rb_eArgError, "negative length %ld given", len);
2806 }
2807
2808 shrinkable = io_setstrbuf(&str, len);
2809 OBJ_TAINT(str);
2810
2811 GetOpenFile(io, fptr);
2812 rb_io_check_byte_readable(fptr);
2813
2814 if (len == 0)
2815 return str;
2816
2817 if (!nonblock)
2818 READ_CHECK(fptr);
2819 n = read_buffered_data(RSTRING_PTR(str), len, fptr);
2820 if (n <= 0) {
2821 again:
2822 if (nonblock) {
2823 rb_io_set_nonblock(fptr);
2824 }
2825 io_setstrbuf(&str, len);
2826 iis.fd = fptr->fd;
2827 iis.nonblock = nonblock;
2828 iis.buf = RSTRING_PTR(str);
2829 iis.capa = len;
2830 n = read_internal_locktmp(str, &iis);
2831 if (n < 0) {
2832 int e = errno;
2833 if (!nonblock && fptr_wait_readable(fptr))
2834 goto again;
2835 if (nonblock && (e == EWOULDBLOCK || e == EAGAIN)) {
2836 if (no_exception_p(opts))
2837 return sym_wait_readable;
2838 else
2839 rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE,
2840 e, "read would block");
2841 }
2842 rb_syserr_fail_path(e, fptr->pathv);
2843 }
2844 }
2845 io_set_read_length(str, n, shrinkable);
2846
2847 if (n == 0)
2848 return Qnil;
2849 else
2850 return str;
2851 }
2852
2853 /*
2854 * call-seq:
2855 * ios.readpartial(maxlen) -> string
2856 * ios.readpartial(maxlen, outbuf) -> outbuf
2857 *
2858 * Reads at most <i>maxlen</i> bytes from the I/O stream.
2859 * It blocks only if <em>ios</em> has no data immediately available.
2860 * It doesn't block if some data available.
2861 *
2862 * If the optional _outbuf_ argument is present,
2863 * it must reference a String, which will receive the data.
2864 * The _outbuf_ will contain only the received data after the method call
2865 * even if it is not empty at the beginning.
2866 *
2867 * It raises <code>EOFError</code> on end of file.
2868 *
2869 * readpartial is designed for streams such as pipe, socket, tty, etc.
2870 * It blocks only when no data immediately available.
2871 * This means that it blocks only when following all conditions hold.
2872 * * the byte buffer in the IO object is empty.
2873 * * the content of the stream is empty.
2874 * * the stream is not reached to EOF.
2875 *
2876 * When readpartial blocks, it waits data or EOF on the stream.
2877 * If some data is reached, readpartial returns with the data.
2878 * If EOF is reached, readpartial raises EOFError.
2879 *
2880 * When readpartial doesn't blocks, it returns or raises immediately.
2881 * If the byte buffer is not empty, it returns the data in the buffer.
2882 * Otherwise if the stream has some content,
2883 * it returns the data in the stream.
2884 * Otherwise if the stream is reached to EOF, it raises EOFError.
2885 *
2886 * r, w = IO.pipe # buffer pipe content
2887 * w << "abc" # "" "abc".
2888 * r.readpartial(4096) #=> "abc" "" ""
2889 * r.readpartial(4096) # blocks because buffer and pipe is empty.
2890 *
2891 * r, w = IO.pipe # buffer pipe content
2892 * w << "abc" # "" "abc"
2893 * w.close # "" "abc" EOF
2894 * r.readpartial(4096) #=> "abc" "" EOF
2895 * r.readpartial(4096) # raises EOFError
2896 *
2897 * r, w = IO.pipe # buffer pipe content
2898 * w << "abc\ndef\n" # "" "abc\ndef\n"
2899 * r.gets #=> "abc\n" "def\n" ""
2900 * w << "ghi\n" # "def\n" "ghi\n"
2901 * r.readpartial(4096) #=> "def\n" "" "ghi\n"
2902 * r.readpartial(4096) #=> "ghi\n" "" ""
2903 *
2904 * Note that readpartial behaves similar to sysread.
2905 * The differences are:
2906 * * If the byte buffer is not empty, read from the byte buffer instead of "sysread for buffered IO (IOError)".
2907 * * It doesn't cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retry the system call.
2908 *
2909 * The latter means that readpartial is nonblocking-flag insensitive.
2910 * It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking mode.
2911 *
2912 */
2913
2914 static VALUE
io_readpartial(int argc,VALUE * argv,VALUE io)2915 io_readpartial(int argc, VALUE *argv, VALUE io)
2916 {
2917 VALUE ret;
2918
2919 ret = io_getpartial(argc, argv, io, Qnil, 0);
2920 if (NIL_P(ret))
2921 rb_eof_error();
2922 return ret;
2923 }
2924
2925 static VALUE
io_nonblock_eof(VALUE opts)2926 io_nonblock_eof(VALUE opts)
2927 {
2928 if (!no_exception_p(opts)) {
2929 rb_eof_error();
2930 }
2931 return Qnil;
2932 }
2933
2934 /* :nodoc: */
2935 static VALUE
io_read_nonblock(VALUE io,VALUE length,VALUE str,VALUE ex)2936 io_read_nonblock(VALUE io, VALUE length, VALUE str, VALUE ex)
2937 {
2938 rb_io_t *fptr;
2939 long n, len;
2940 struct io_internal_read_struct iis;
2941 int shrinkable;
2942
2943 if ((len = NUM2LONG(length)) < 0) {
2944 rb_raise(rb_eArgError, "negative length %ld given", len);
2945 }
2946
2947 shrinkable = io_setstrbuf(&str, len);
2948 OBJ_TAINT(str);
2949 GetOpenFile(io, fptr);
2950 rb_io_check_byte_readable(fptr);
2951
2952 if (len == 0)
2953 return str;
2954
2955 n = read_buffered_data(RSTRING_PTR(str), len, fptr);
2956 if (n <= 0) {
2957 rb_io_set_nonblock(fptr);
2958 shrinkable |= io_setstrbuf(&str, len);
2959 iis.fd = fptr->fd;
2960 iis.nonblock = 1;
2961 iis.buf = RSTRING_PTR(str);
2962 iis.capa = len;
2963 n = read_internal_locktmp(str, &iis);
2964 if (n < 0) {
2965 int e = errno;
2966 if ((e == EWOULDBLOCK || e == EAGAIN)) {
2967 if (ex == Qfalse) return sym_wait_readable;
2968 rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE,
2969 e, "read would block");
2970 }
2971 rb_syserr_fail_path(e, fptr->pathv);
2972 }
2973 }
2974 io_set_read_length(str, n, shrinkable);
2975
2976 if (n == 0) {
2977 if (ex == Qfalse) return Qnil;
2978 rb_eof_error();
2979 }
2980
2981 return str;
2982 }
2983
2984 /* :nodoc: */
2985 static VALUE
io_write_nonblock(VALUE io,VALUE str,VALUE ex)2986 io_write_nonblock(VALUE io, VALUE str, VALUE ex)
2987 {
2988 rb_io_t *fptr;
2989 long n;
2990
2991 if (!RB_TYPE_P(str, T_STRING))
2992 str = rb_obj_as_string(str);
2993
2994 io = GetWriteIO(io);
2995 GetOpenFile(io, fptr);
2996 rb_io_check_writable(fptr);
2997
2998 if (io_fflush(fptr) < 0)
2999 rb_sys_fail(0);
3000
3001 rb_io_set_nonblock(fptr);
3002 n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
3003 RB_GC_GUARD(str);
3004
3005 if (n < 0) {
3006 int e = errno;
3007 if (e == EWOULDBLOCK || e == EAGAIN) {
3008 if (ex == Qfalse) {
3009 return sym_wait_writable;
3010 }
3011 else {
3012 rb_readwrite_syserr_fail(RB_IO_WAIT_WRITABLE, e, "write would block");
3013 }
3014 }
3015 rb_syserr_fail_path(e, fptr->pathv);
3016 }
3017
3018 return LONG2FIX(n);
3019 }
3020
3021 /*
3022 * call-seq:
3023 * ios.read([length [, outbuf]]) -> string, outbuf, or nil
3024 *
3025 * Reads _length_ bytes from the I/O stream.
3026 *
3027 * _length_ must be a non-negative integer or +nil+.
3028 *
3029 * If _length_ is a positive integer, +read+ tries to read
3030 * _length_ bytes without any conversion (binary mode).
3031 * It returns +nil+ if an EOF is encountered before anything can be read.
3032 * Fewer than _length_ bytes are returned if an EOF is encountered during
3033 * the read.
3034 * In the case of an integer _length_, the resulting string is always
3035 * in ASCII-8BIT encoding.
3036 *
3037 * If _length_ is omitted or is +nil+, it reads until EOF
3038 * and the encoding conversion is applied, if applicable.
3039 * A string is returned even if EOF is encountered before any data is read.
3040 *
3041 * If _length_ is zero, it returns an empty string (<code>""</code>).
3042 *
3043 * If the optional _outbuf_ argument is present,
3044 * it must reference a String, which will receive the data.
3045 * The _outbuf_ will contain only the received data after the method call
3046 * even if it is not empty at the beginning.
3047 *
3048 * When this method is called at end of file, it returns +nil+
3049 * or <code>""</code>, depending on _length_:
3050 * +read+, <code>read(nil)</code>, and <code>read(0)</code> return
3051 * <code>""</code>,
3052 * <code>read(<i>positive_integer</i>)</code> returns +nil+.
3053 *
3054 * f = File.new("testfile")
3055 * f.read(16) #=> "This is line one"
3056 *
3057 * # read whole file
3058 * open("file") do |f|
3059 * data = f.read # This returns a string even if the file is empty.
3060 * # ...
3061 * end
3062 *
3063 * # iterate over fixed length records
3064 * open("fixed-record-file") do |f|
3065 * while record = f.read(256)
3066 * # ...
3067 * end
3068 * end
3069 *
3070 * # iterate over variable length records,
3071 * # each record is prefixed by its 32-bit length
3072 * open("variable-record-file") do |f|
3073 * while len = f.read(4)
3074 * len = len.unpack("N")[0] # 32-bit length
3075 * record = f.read(len) # This returns a string even if len is 0.
3076 * end
3077 * end
3078 *
3079 * Note that this method behaves like the fread() function in C.
3080 * This means it retries to invoke read(2) system calls to read data
3081 * with the specified length (or until EOF).
3082 * This behavior is preserved even if <i>ios</i> is in non-blocking mode.
3083 * (This method is non-blocking flag insensitive as other methods.)
3084 * If you need the behavior like a single read(2) system call,
3085 * consider #readpartial, #read_nonblock, and #sysread.
3086 */
3087
3088 static VALUE
io_read(int argc,VALUE * argv,VALUE io)3089 io_read(int argc, VALUE *argv, VALUE io)
3090 {
3091 rb_io_t *fptr;
3092 long n, len;
3093 VALUE length, str;
3094 int shrinkable;
3095 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
3096 int previous_mode;
3097 #endif
3098
3099 rb_scan_args(argc, argv, "02", &length, &str);
3100
3101 if (NIL_P(length)) {
3102 GetOpenFile(io, fptr);
3103 rb_io_check_char_readable(fptr);
3104 return read_all(fptr, remain_size(fptr), str);
3105 }
3106 len = NUM2LONG(length);
3107 if (len < 0) {
3108 rb_raise(rb_eArgError, "negative length %ld given", len);
3109 }
3110
3111 shrinkable = io_setstrbuf(&str,len);
3112
3113 GetOpenFile(io, fptr);
3114 rb_io_check_byte_readable(fptr);
3115 if (len == 0) {
3116 io_set_read_length(str, 0, shrinkable);
3117 return str;
3118 }
3119
3120 READ_CHECK(fptr);
3121 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
3122 previous_mode = set_binary_mode_with_seek_cur(fptr);
3123 #endif
3124 n = io_fread(str, 0, len, fptr);
3125 io_set_read_length(str, n, shrinkable);
3126 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
3127 if (previous_mode == O_TEXT) {
3128 setmode(fptr->fd, O_TEXT);
3129 }
3130 #endif
3131 if (n == 0) return Qnil;
3132 OBJ_TAINT(str);
3133
3134 return str;
3135 }
3136
3137 static void
rscheck(const char * rsptr,long rslen,VALUE rs)3138 rscheck(const char *rsptr, long rslen, VALUE rs)
3139 {
3140 if (!rs) return;
3141 if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
3142 rb_raise(rb_eRuntimeError, "rs modified");
3143 }
3144
3145 static int
appendline(rb_io_t * fptr,int delim,VALUE * strp,long * lp)3146 appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
3147 {
3148 VALUE str = *strp;
3149 long limit = *lp;
3150
3151 if (NEED_READCONV(fptr)) {
3152 SET_BINARY_MODE(fptr);
3153 make_readconv(fptr, 0);
3154 do {
3155 const char *p, *e;
3156 int searchlen = READ_CHAR_PENDING_COUNT(fptr);
3157 if (searchlen) {
3158 p = READ_CHAR_PENDING_PTR(fptr);
3159 if (0 < limit && limit < searchlen)
3160 searchlen = (int)limit;
3161 e = memchr(p, delim, searchlen);
3162 if (e) {
3163 int len = (int)(e-p+1);
3164 if (NIL_P(str))
3165 *strp = str = rb_str_new(p, len);
3166 else
3167 rb_str_buf_cat(str, p, len);
3168 fptr->cbuf.off += len;
3169 fptr->cbuf.len -= len;
3170 limit -= len;
3171 *lp = limit;
3172 return delim;
3173 }
3174
3175 if (NIL_P(str))
3176 *strp = str = rb_str_new(p, searchlen);
3177 else
3178 rb_str_buf_cat(str, p, searchlen);
3179 fptr->cbuf.off += searchlen;
3180 fptr->cbuf.len -= searchlen;
3181 limit -= searchlen;
3182
3183 if (limit == 0) {
3184 *lp = limit;
3185 return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
3186 }
3187 }
3188 } while (more_char(fptr) != MORE_CHAR_FINISHED);
3189 clear_readconv(fptr);
3190 *lp = limit;
3191 return EOF;
3192 }
3193
3194 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
3195 do {
3196 long pending = READ_DATA_PENDING_COUNT(fptr);
3197 if (pending > 0) {
3198 const char *p = READ_DATA_PENDING_PTR(fptr);
3199 const char *e;
3200 long last;
3201
3202 if (limit > 0 && pending > limit) pending = limit;
3203 e = memchr(p, delim, pending);
3204 if (e) pending = e - p + 1;
3205 if (!NIL_P(str)) {
3206 last = RSTRING_LEN(str);
3207 rb_str_resize(str, last + pending);
3208 }
3209 else {
3210 last = 0;
3211 *strp = str = rb_str_buf_new(pending);
3212 rb_str_set_len(str, pending);
3213 }
3214 read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
3215 limit -= pending;
3216 *lp = limit;
3217 if (e) return delim;
3218 if (limit == 0)
3219 return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
3220 }
3221 READ_CHECK(fptr);
3222 } while (io_fillbuf(fptr) >= 0);
3223 *lp = limit;
3224 return EOF;
3225 }
3226
3227 static inline int
swallow(rb_io_t * fptr,int term)3228 swallow(rb_io_t *fptr, int term)
3229 {
3230 if (NEED_READCONV(fptr)) {
3231 rb_encoding *enc = io_read_encoding(fptr);
3232 int needconv = rb_enc_mbminlen(enc) != 1;
3233 SET_BINARY_MODE(fptr);
3234 make_readconv(fptr, 0);
3235 do {
3236 size_t cnt;
3237 while ((cnt = READ_CHAR_PENDING_COUNT(fptr)) > 0) {
3238 const char *p = READ_CHAR_PENDING_PTR(fptr);
3239 int i;
3240 if (!needconv) {
3241 if (*p != term) return TRUE;
3242 i = (int)cnt;
3243 while (--i && *++p == term);
3244 }
3245 else {
3246 const char *e = p + cnt;
3247 if (rb_enc_ascget(p, e, &i, enc) != term) return TRUE;
3248 while ((p += i) < e && rb_enc_ascget(p, e, &i, enc) == term);
3249 i = (int)(e - p);
3250 }
3251 io_shift_cbuf(fptr, (int)cnt - i, NULL);
3252 }
3253 } while (more_char(fptr) != MORE_CHAR_FINISHED);
3254 return FALSE;
3255 }
3256
3257 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
3258 do {
3259 size_t cnt;
3260 while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
3261 char buf[1024];
3262 const char *p = READ_DATA_PENDING_PTR(fptr);
3263 int i;
3264 if (cnt > sizeof buf) cnt = sizeof buf;
3265 if (*p != term) return TRUE;
3266 i = (int)cnt;
3267 while (--i && *++p == term);
3268 if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
3269 rb_sys_fail_path(fptr->pathv);
3270 }
3271 READ_CHECK(fptr);
3272 } while (io_fillbuf(fptr) == 0);
3273 return FALSE;
3274 }
3275
3276 static VALUE
rb_io_getline_fast(rb_io_t * fptr,rb_encoding * enc,int chomp)3277 rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, int chomp)
3278 {
3279 VALUE str = Qnil;
3280 int len = 0;
3281 long pos = 0;
3282 int cr = 0;
3283
3284 do {
3285 int pending = READ_DATA_PENDING_COUNT(fptr);
3286
3287 if (pending > 0) {
3288 const char *p = READ_DATA_PENDING_PTR(fptr);
3289 const char *e;
3290 int chomplen = 0;
3291
3292 e = memchr(p, '\n', pending);
3293 if (e) {
3294 pending = (int)(e - p + 1);
3295 if (chomp) {
3296 chomplen = (pending > 1 && *(e-1) == '\r') + 1;
3297 }
3298 }
3299 if (NIL_P(str)) {
3300 str = rb_str_new(p, pending - chomplen);
3301 fptr->rbuf.off += pending;
3302 fptr->rbuf.len -= pending;
3303 }
3304 else {
3305 rb_str_resize(str, len + pending - chomplen);
3306 read_buffered_data(RSTRING_PTR(str)+len, pending - chomplen, fptr);
3307 fptr->rbuf.off += chomplen;
3308 fptr->rbuf.len -= chomplen;
3309 if (pending == 1 && chomplen == 1 && len > 0) {
3310 if (RSTRING_PTR(str)[len-1] == '\r') {
3311 rb_str_resize(str, --len);
3312 break;
3313 }
3314 }
3315 }
3316 len += pending - chomplen;
3317 if (cr != ENC_CODERANGE_BROKEN)
3318 pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
3319 if (e) break;
3320 }
3321 READ_CHECK(fptr);
3322 } while (io_fillbuf(fptr) >= 0);
3323 if (NIL_P(str)) return Qnil;
3324
3325 str = io_enc_str(str, fptr);
3326 ENC_CODERANGE_SET(str, cr);
3327 fptr->lineno++;
3328
3329 return str;
3330 }
3331
3332 struct getline_arg {
3333 VALUE io;
3334 VALUE rs;
3335 long limit;
3336 unsigned int chomp: 1;
3337 };
3338
3339 static void
extract_getline_opts(VALUE opts,struct getline_arg * args)3340 extract_getline_opts(VALUE opts, struct getline_arg *args)
3341 {
3342 int chomp = FALSE;
3343 if (!NIL_P(opts)) {
3344 static ID kwds[1];
3345 VALUE vchomp;
3346 if (!kwds[0]) {
3347 kwds[0] = rb_intern_const("chomp");
3348 }
3349 rb_get_kwargs(opts, kwds, 0, -2, &vchomp);
3350 chomp = (vchomp != Qundef) && RTEST(vchomp);
3351 }
3352 args->chomp = chomp;
3353 }
3354
3355 static void
extract_getline_args(int argc,VALUE * argv,struct getline_arg * args)3356 extract_getline_args(int argc, VALUE *argv, struct getline_arg *args)
3357 {
3358 VALUE rs = rb_rs, lim = Qnil;
3359
3360 if (argc == 1) {
3361 VALUE tmp = Qnil;
3362
3363 if (NIL_P(argv[0]) || !NIL_P(tmp = rb_check_string_type(argv[0]))) {
3364 rs = tmp;
3365 }
3366 else {
3367 lim = argv[0];
3368 }
3369 }
3370 else if (2 <= argc) {
3371 rs = argv[0], lim = argv[1];
3372 if (!NIL_P(rs))
3373 StringValue(rs);
3374 }
3375 args->rs = rs;
3376 args->limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
3377 }
3378
3379 static void
check_getline_args(VALUE * rsp,long * limit,VALUE io)3380 check_getline_args(VALUE *rsp, long *limit, VALUE io)
3381 {
3382 rb_io_t *fptr;
3383 VALUE rs = *rsp;
3384
3385 if (!NIL_P(rs)) {
3386 rb_encoding *enc_rs, *enc_io;
3387
3388 GetOpenFile(io, fptr);
3389 enc_rs = rb_enc_get(rs);
3390 enc_io = io_read_encoding(fptr);
3391 if (enc_io != enc_rs &&
3392 (rb_enc_str_coderange(rs) != ENC_CODERANGE_7BIT ||
3393 (RSTRING_LEN(rs) > 0 && !rb_enc_asciicompat(enc_io)))) {
3394 if (rs == rb_default_rs) {
3395 rs = rb_enc_str_new(0, 0, enc_io);
3396 rb_str_buf_cat_ascii(rs, "\n");
3397 *rsp = rs;
3398 }
3399 else {
3400 rb_raise(rb_eArgError, "encoding mismatch: %s IO with %s RS",
3401 rb_enc_name(enc_io),
3402 rb_enc_name(enc_rs));
3403 }
3404 }
3405 }
3406 }
3407
3408 static void
prepare_getline_args(int argc,VALUE * argv,struct getline_arg * args,VALUE io)3409 prepare_getline_args(int argc, VALUE *argv, struct getline_arg *args, VALUE io)
3410 {
3411 VALUE opts;
3412 argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opts);
3413 extract_getline_args(argc, argv, args);
3414 extract_getline_opts(opts, args);
3415 check_getline_args(&args->rs, &args->limit, io);
3416 }
3417
3418 static VALUE
rb_io_getline_0(VALUE rs,long limit,int chomp,rb_io_t * fptr)3419 rb_io_getline_0(VALUE rs, long limit, int chomp, rb_io_t *fptr)
3420 {
3421 VALUE str = Qnil;
3422 int nolimit = 0;
3423 rb_encoding *enc;
3424
3425 rb_io_check_char_readable(fptr);
3426 if (NIL_P(rs) && limit < 0) {
3427 str = read_all(fptr, 0, Qnil);
3428 if (RSTRING_LEN(str) == 0) return Qnil;
3429 if (chomp) rb_str_chomp_string(str, rb_default_rs);
3430 }
3431 else if (limit == 0) {
3432 return rb_enc_str_new(0, 0, io_read_encoding(fptr));
3433 }
3434 else if (rs == rb_default_rs && limit < 0 && !NEED_READCONV(fptr) &&
3435 rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
3436 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
3437 return rb_io_getline_fast(fptr, enc, chomp);
3438 }
3439 else {
3440 int c, newline = -1;
3441 const char *rsptr = 0;
3442 long rslen = 0;
3443 int rspara = 0;
3444 int extra_limit = 16;
3445 int chomp_cr = chomp;
3446
3447 SET_BINARY_MODE(fptr);
3448 enc = io_read_encoding(fptr);
3449
3450 if (!NIL_P(rs)) {
3451 rslen = RSTRING_LEN(rs);
3452 if (rslen == 0) {
3453 rsptr = "\n\n";
3454 rslen = 2;
3455 rspara = 1;
3456 swallow(fptr, '\n');
3457 rs = 0;
3458 if (!rb_enc_asciicompat(enc)) {
3459 rs = rb_usascii_str_new(rsptr, rslen);
3460 rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
3461 OBJ_FREEZE(rs);
3462 rsptr = RSTRING_PTR(rs);
3463 rslen = RSTRING_LEN(rs);
3464 }
3465 }
3466 else {
3467 rsptr = RSTRING_PTR(rs);
3468 }
3469 newline = (unsigned char)rsptr[rslen - 1];
3470 chomp_cr = chomp && rslen == 1 && newline == '\n';
3471 }
3472
3473 /* MS - Optimization */
3474 while ((c = appendline(fptr, newline, &str, &limit)) != EOF) {
3475 const char *s, *p, *pp, *e;
3476
3477 if (c == newline) {
3478 if (RSTRING_LEN(str) < rslen) continue;
3479 s = RSTRING_PTR(str);
3480 e = RSTRING_END(str);
3481 p = e - rslen;
3482 pp = rb_enc_left_char_head(s, p, e, enc);
3483 if (pp != p) continue;
3484 if (!rspara) rscheck(rsptr, rslen, rs);
3485 if (memcmp(p, rsptr, rslen) == 0) {
3486 if (chomp) {
3487 if (chomp_cr && p > s && *(p-1) == '\r') --p;
3488 rb_str_set_len(str, p - s);
3489 }
3490 break;
3491 }
3492 }
3493 if (limit == 0) {
3494 s = RSTRING_PTR(str);
3495 p = RSTRING_END(str);
3496 pp = rb_enc_left_char_head(s, p-1, p, enc);
3497 if (extra_limit &&
3498 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(pp, p, enc))) {
3499 /* relax the limit while incomplete character.
3500 * extra_limit limits the relax length */
3501 limit = 1;
3502 extra_limit--;
3503 }
3504 else {
3505 nolimit = 1;
3506 break;
3507 }
3508 }
3509 }
3510
3511 if (rspara && c != EOF)
3512 swallow(fptr, '\n');
3513 if (!NIL_P(str))
3514 str = io_enc_str(str, fptr);
3515 }
3516
3517 if (!NIL_P(str) && !nolimit) {
3518 fptr->lineno++;
3519 }
3520
3521 return str;
3522 }
3523
3524 static VALUE
rb_io_getline_1(VALUE rs,long limit,int chomp,VALUE io)3525 rb_io_getline_1(VALUE rs, long limit, int chomp, VALUE io)
3526 {
3527 rb_io_t *fptr;
3528 int old_lineno, new_lineno;
3529 VALUE str;
3530
3531 GetOpenFile(io, fptr);
3532 old_lineno = fptr->lineno;
3533 str = rb_io_getline_0(rs, limit, chomp, fptr);
3534 if (!NIL_P(str) && (new_lineno = fptr->lineno) != old_lineno) {
3535 if (io == ARGF.current_file) {
3536 ARGF.lineno += new_lineno - old_lineno;
3537 ARGF.last_lineno = ARGF.lineno;
3538 }
3539 else {
3540 ARGF.last_lineno = new_lineno;
3541 }
3542 }
3543
3544 return str;
3545 }
3546
3547 static VALUE
rb_io_getline(int argc,VALUE * argv,VALUE io)3548 rb_io_getline(int argc, VALUE *argv, VALUE io)
3549 {
3550 struct getline_arg args;
3551
3552 prepare_getline_args(argc, argv, &args, io);
3553 return rb_io_getline_1(args.rs, args.limit, args.chomp, io);
3554 }
3555
3556 VALUE
rb_io_gets(VALUE io)3557 rb_io_gets(VALUE io)
3558 {
3559 return rb_io_getline_1(rb_default_rs, -1, FALSE, io);
3560 }
3561
3562 VALUE
rb_io_gets_internal(VALUE io)3563 rb_io_gets_internal(VALUE io)
3564 {
3565 rb_io_t *fptr;
3566 GetOpenFile(io, fptr);
3567 return rb_io_getline_0(rb_default_rs, -1, FALSE, fptr);
3568 }
3569
3570 /*
3571 * call-seq:
3572 * ios.gets(sep=$/ [, getline_args]) -> string or nil
3573 * ios.gets(limit [, getline_args]) -> string or nil
3574 * ios.gets(sep, limit [, getline_args]) -> string or nil
3575 *
3576 * Reads the next ``line'' from the I/O stream; lines are separated by
3577 * <i>sep</i>. A separator of +nil+ reads the entire
3578 * contents, and a zero-length separator reads the input a paragraph at
3579 * a time (two successive newlines in the input separate paragraphs).
3580 * The stream must be opened for reading or an <code>IOError</code>
3581 * will be raised. The line read in will be returned and also assigned
3582 * to <code>$_</code>. Returns +nil+ if called at end of
3583 * file. If the first argument is an integer, or optional second
3584 * argument is given, the returning string would not be longer than the
3585 * given value in bytes.
3586 *
3587 * File.new("testfile").gets #=> "This is line one\n"
3588 * $_ #=> "This is line one\n"
3589 *
3590 * File.new("testfile").gets(4)#=> "This"
3591 *
3592 * If IO contains multibyte characters byte then <code>gets(1)</code>
3593 * returns character entirely:
3594 *
3595 * # Russian characters take 2 bytes
3596 * File.write("testfile", "\u{442 435 441 442}")
3597 * File.open("testfile") {|f|f.gets(1)} #=> "\u0442"
3598 * File.open("testfile") {|f|f.gets(2)} #=> "\u0442"
3599 * File.open("testfile") {|f|f.gets(3)} #=> "\u0442\u0435"
3600 * File.open("testfile") {|f|f.gets(4)} #=> "\u0442\u0435"
3601 */
3602
3603 static VALUE
rb_io_gets_m(int argc,VALUE * argv,VALUE io)3604 rb_io_gets_m(int argc, VALUE *argv, VALUE io)
3605 {
3606 VALUE str;
3607
3608 str = rb_io_getline(argc, argv, io);
3609 rb_lastline_set(str);
3610
3611 return str;
3612 }
3613
3614 /*
3615 * call-seq:
3616 * ios.lineno -> integer
3617 *
3618 * Returns the current line number in <em>ios</em>. The stream must be
3619 * opened for reading. <code>lineno</code> counts the number of times
3620 * #gets is called rather than the number of newlines encountered. The two
3621 * values will differ if #gets is called with a separator other than newline.
3622 *
3623 * Methods that use <code>$/</code> like #each, #lines and #readline will
3624 * also increment <code>lineno</code>.
3625 *
3626 * See also the <code>$.</code> variable.
3627 *
3628 * f = File.new("testfile")
3629 * f.lineno #=> 0
3630 * f.gets #=> "This is line one\n"
3631 * f.lineno #=> 1
3632 * f.gets #=> "This is line two\n"
3633 * f.lineno #=> 2
3634 */
3635
3636 static VALUE
rb_io_lineno(VALUE io)3637 rb_io_lineno(VALUE io)
3638 {
3639 rb_io_t *fptr;
3640
3641 GetOpenFile(io, fptr);
3642 rb_io_check_char_readable(fptr);
3643 return INT2NUM(fptr->lineno);
3644 }
3645
3646 /*
3647 * call-seq:
3648 * ios.lineno = integer -> integer
3649 *
3650 * Manually sets the current line number to the given value.
3651 * <code>$.</code> is updated only on the next read.
3652 *
3653 * f = File.new("testfile")
3654 * f.gets #=> "This is line one\n"
3655 * $. #=> 1
3656 * f.lineno = 1000
3657 * f.lineno #=> 1000
3658 * $. #=> 1 # lineno of last read
3659 * f.gets #=> "This is line two\n"
3660 * $. #=> 1001 # lineno of last read
3661 */
3662
3663 static VALUE
rb_io_set_lineno(VALUE io,VALUE lineno)3664 rb_io_set_lineno(VALUE io, VALUE lineno)
3665 {
3666 rb_io_t *fptr;
3667
3668 GetOpenFile(io, fptr);
3669 rb_io_check_char_readable(fptr);
3670 fptr->lineno = NUM2INT(lineno);
3671 return lineno;
3672 }
3673
3674 /*
3675 * call-seq:
3676 * ios.readline(sep=$/ [, getline_args]) -> string
3677 * ios.readline(limit [, getline_args]) -> string
3678 * ios.readline(sep, limit [, getline_args]) -> string
3679 *
3680 * Reads a line as with <code>IO#gets</code>, but raises an
3681 * <code>EOFError</code> on end of file.
3682 */
3683
3684 static VALUE
rb_io_readline(int argc,VALUE * argv,VALUE io)3685 rb_io_readline(int argc, VALUE *argv, VALUE io)
3686 {
3687 VALUE line = rb_io_gets_m(argc, argv, io);
3688
3689 if (NIL_P(line)) {
3690 rb_eof_error();
3691 }
3692 return line;
3693 }
3694
3695 static VALUE io_readlines(const struct getline_arg *arg, VALUE io);
3696
3697 /*
3698 * call-seq:
3699 * ios.readlines(sep=$/ [, getline_args]) -> array
3700 * ios.readlines(limit [, getline_args]) -> array
3701 * ios.readlines(sep, limit [, getline_args]) -> array
3702 *
3703 * Reads all of the lines in <em>ios</em>, and returns them in
3704 * an array. Lines are separated by the optional <i>sep</i>. If
3705 * <i>sep</i> is +nil+, the rest of the stream is returned
3706 * as a single record.
3707 * If the first argument is an integer, or an
3708 * optional second argument is given, the returning string would not be
3709 * longer than the given value in bytes. The stream must be opened for
3710 * reading or an <code>IOError</code> will be raised.
3711 *
3712 * f = File.new("testfile")
3713 * f.readlines[0] #=> "This is line one\n"
3714 *
3715 * f = File.new("testfile", chomp: true)
3716 * f.readlines[0] #=> "This is line one"
3717 *
3718 * See IO.readlines for details about getline_args.
3719 */
3720
3721 static VALUE
rb_io_readlines(int argc,VALUE * argv,VALUE io)3722 rb_io_readlines(int argc, VALUE *argv, VALUE io)
3723 {
3724 struct getline_arg args;
3725
3726 prepare_getline_args(argc, argv, &args, io);
3727 return io_readlines(&args, io);
3728 }
3729
3730 static VALUE
io_readlines(const struct getline_arg * arg,VALUE io)3731 io_readlines(const struct getline_arg *arg, VALUE io)
3732 {
3733 VALUE line, ary;
3734
3735 if (arg->limit == 0)
3736 rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
3737 ary = rb_ary_new();
3738 while (!NIL_P(line = rb_io_getline_1(arg->rs, arg->limit, arg->chomp, io))) {
3739 rb_ary_push(ary, line);
3740 }
3741 return ary;
3742 }
3743
3744 /*
3745 * call-seq:
3746 * ios.each(sep=$/ [, getline_args]) {|line| block } -> ios
3747 * ios.each(limit [, getline_args]) {|line| block } -> ios
3748 * ios.each(sep, limit [, getline_args]) {|line| block } -> ios
3749 * ios.each(...) -> an_enumerator
3750 *
3751 * ios.each_line(sep=$/ [, getline_args]) {|line| block } -> ios
3752 * ios.each_line(limit [, getline_args]) {|line| block } -> ios
3753 * ios.each_line(sep, limit [, getline_args]) {|line| block } -> ios
3754 * ios.each_line(...) -> an_enumerator
3755 *
3756 * Executes the block for every line in <em>ios</em>, where lines are
3757 * separated by <i>sep</i>. <em>ios</em> must be opened for
3758 * reading or an <code>IOError</code> will be raised.
3759 *
3760 * If no block is given, an enumerator is returned instead.
3761 *
3762 * f = File.new("testfile")
3763 * f.each {|line| puts "#{f.lineno}: #{line}" }
3764 *
3765 * <em>produces:</em>
3766 *
3767 * 1: This is line one
3768 * 2: This is line two
3769 * 3: This is line three
3770 * 4: And so on...
3771 *
3772 * See IO.readlines for details about getline_args.
3773 */
3774
3775 static VALUE
rb_io_each_line(int argc,VALUE * argv,VALUE io)3776 rb_io_each_line(int argc, VALUE *argv, VALUE io)
3777 {
3778 VALUE str;
3779 struct getline_arg args;
3780
3781 RETURN_ENUMERATOR(io, argc, argv);
3782 prepare_getline_args(argc, argv, &args, io);
3783 if (args.limit == 0)
3784 rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
3785 while (!NIL_P(str = rb_io_getline_1(args.rs, args.limit, args.chomp, io))) {
3786 rb_yield(str);
3787 }
3788 return io;
3789 }
3790
3791 /*
3792 * This is a deprecated alias for <code>each_line</code>.
3793 */
3794
3795 static VALUE
rb_io_lines(int argc,VALUE * argv,VALUE io)3796 rb_io_lines(int argc, VALUE *argv, VALUE io)
3797 {
3798 rb_warn("IO#lines is deprecated; use #each_line instead");
3799 if (!rb_block_given_p())
3800 return rb_enumeratorize(io, ID2SYM(rb_intern("each_line")), argc, argv);
3801 return rb_io_each_line(argc, argv, io);
3802 }
3803
3804 /*
3805 * call-seq:
3806 * ios.each_byte {|byte| block } -> ios
3807 * ios.each_byte -> an_enumerator
3808 *
3809 * Calls the given block once for each byte (0..255) in <em>ios</em>,
3810 * passing the byte as an argument. The stream must be opened for
3811 * reading or an <code>IOError</code> will be raised.
3812 *
3813 * If no block is given, an enumerator is returned instead.
3814 *
3815 * f = File.new("testfile")
3816 * checksum = 0
3817 * f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
3818 * checksum #=> 12
3819 */
3820
3821 static VALUE
rb_io_each_byte(VALUE io)3822 rb_io_each_byte(VALUE io)
3823 {
3824 rb_io_t *fptr;
3825
3826 RETURN_ENUMERATOR(io, 0, 0);
3827 GetOpenFile(io, fptr);
3828
3829 do {
3830 while (fptr->rbuf.len > 0) {
3831 char *p = fptr->rbuf.ptr + fptr->rbuf.off++;
3832 fptr->rbuf.len--;
3833 rb_yield(INT2FIX(*p & 0xff));
3834 errno = 0;
3835 }
3836 rb_io_check_byte_readable(fptr);
3837 READ_CHECK(fptr);
3838 } while (io_fillbuf(fptr) >= 0);
3839 return io;
3840 }
3841
3842 /*
3843 * This is a deprecated alias for <code>each_byte</code>.
3844 */
3845
3846 static VALUE
rb_io_bytes(VALUE io)3847 rb_io_bytes(VALUE io)
3848 {
3849 rb_warn("IO#bytes is deprecated; use #each_byte instead");
3850 if (!rb_block_given_p())
3851 return rb_enumeratorize(io, ID2SYM(rb_intern("each_byte")), 0, 0);
3852 return rb_io_each_byte(io);
3853 }
3854
3855 static VALUE
io_getc(rb_io_t * fptr,rb_encoding * enc)3856 io_getc(rb_io_t *fptr, rb_encoding *enc)
3857 {
3858 int r, n, cr = 0;
3859 VALUE str;
3860
3861 if (NEED_READCONV(fptr)) {
3862 rb_encoding *read_enc = io_read_encoding(fptr);
3863
3864 str = Qnil;
3865 SET_BINARY_MODE(fptr);
3866 make_readconv(fptr, 0);
3867
3868 while (1) {
3869 if (fptr->cbuf.len) {
3870 r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
3871 fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
3872 read_enc);
3873 if (!MBCLEN_NEEDMORE_P(r))
3874 break;
3875 if (fptr->cbuf.len == fptr->cbuf.capa) {
3876 rb_raise(rb_eIOError, "too long character");
3877 }
3878 }
3879
3880 if (more_char(fptr) == MORE_CHAR_FINISHED) {
3881 if (fptr->cbuf.len == 0) {
3882 clear_readconv(fptr);
3883 return Qnil;
3884 }
3885 /* return an unit of an incomplete character just before EOF */
3886 str = rb_enc_str_new(fptr->cbuf.ptr+fptr->cbuf.off, 1, read_enc);
3887 fptr->cbuf.off += 1;
3888 fptr->cbuf.len -= 1;
3889 if (fptr->cbuf.len == 0) clear_readconv(fptr);
3890 ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
3891 return str;
3892 }
3893 }
3894 if (MBCLEN_INVALID_P(r)) {
3895 r = rb_enc_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
3896 fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
3897 read_enc);
3898 io_shift_cbuf(fptr, r, &str);
3899 cr = ENC_CODERANGE_BROKEN;
3900 }
3901 else {
3902 io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str);
3903 cr = ENC_CODERANGE_VALID;
3904 if (MBCLEN_CHARFOUND_LEN(r) == 1 && rb_enc_asciicompat(read_enc) &&
3905 ISASCII(RSTRING_PTR(str)[0])) {
3906 cr = ENC_CODERANGE_7BIT;
3907 }
3908 }
3909 str = io_enc_str(str, fptr);
3910 ENC_CODERANGE_SET(str, cr);
3911 return str;
3912 }
3913
3914 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
3915 if (io_fillbuf(fptr) < 0) {
3916 return Qnil;
3917 }
3918 if (rb_enc_asciicompat(enc) && ISASCII(fptr->rbuf.ptr[fptr->rbuf.off])) {
3919 str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
3920 fptr->rbuf.off += 1;
3921 fptr->rbuf.len -= 1;
3922 cr = ENC_CODERANGE_7BIT;
3923 }
3924 else {
3925 r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
3926 if (MBCLEN_CHARFOUND_P(r) &&
3927 (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
3928 str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, n);
3929 fptr->rbuf.off += n;
3930 fptr->rbuf.len -= n;
3931 cr = ENC_CODERANGE_VALID;
3932 }
3933 else if (MBCLEN_NEEDMORE_P(r)) {
3934 str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.len);
3935 fptr->rbuf.len = 0;
3936 getc_needmore:
3937 if (io_fillbuf(fptr) != -1) {
3938 rb_str_cat(str, fptr->rbuf.ptr+fptr->rbuf.off, 1);
3939 fptr->rbuf.off++;
3940 fptr->rbuf.len--;
3941 r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
3942 if (MBCLEN_NEEDMORE_P(r)) {
3943 goto getc_needmore;
3944 }
3945 else if (MBCLEN_CHARFOUND_P(r)) {
3946 cr = ENC_CODERANGE_VALID;
3947 }
3948 }
3949 }
3950 else {
3951 str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
3952 fptr->rbuf.off++;
3953 fptr->rbuf.len--;
3954 }
3955 }
3956 if (!cr) cr = ENC_CODERANGE_BROKEN;
3957 str = io_enc_str(str, fptr);
3958 ENC_CODERANGE_SET(str, cr);
3959 return str;
3960 }
3961
3962 /*
3963 * call-seq:
3964 * ios.each_char {|c| block } -> ios
3965 * ios.each_char -> an_enumerator
3966 *
3967 * Calls the given block once for each character in <em>ios</em>,
3968 * passing the character as an argument. The stream must be opened for
3969 * reading or an <code>IOError</code> will be raised.
3970 *
3971 * If no block is given, an enumerator is returned instead.
3972 *
3973 * f = File.new("testfile")
3974 * f.each_char {|c| print c, ' ' } #=> #<File:testfile>
3975 */
3976
3977 static VALUE
rb_io_each_char(VALUE io)3978 rb_io_each_char(VALUE io)
3979 {
3980 rb_io_t *fptr;
3981 rb_encoding *enc;
3982 VALUE c;
3983
3984 RETURN_ENUMERATOR(io, 0, 0);
3985 GetOpenFile(io, fptr);
3986 rb_io_check_char_readable(fptr);
3987
3988 enc = io_input_encoding(fptr);
3989 READ_CHECK(fptr);
3990 while (!NIL_P(c = io_getc(fptr, enc))) {
3991 rb_yield(c);
3992 }
3993 return io;
3994 }
3995
3996 /*
3997 * This is a deprecated alias for <code>each_char</code>.
3998 */
3999
4000 static VALUE
rb_io_chars(VALUE io)4001 rb_io_chars(VALUE io)
4002 {
4003 rb_warn("IO#chars is deprecated; use #each_char instead");
4004 if (!rb_block_given_p())
4005 return rb_enumeratorize(io, ID2SYM(rb_intern("each_char")), 0, 0);
4006 return rb_io_each_char(io);
4007 }
4008
4009
4010 /*
4011 * call-seq:
4012 * ios.each_codepoint {|c| block } -> ios
4013 * ios.codepoints {|c| block } -> ios
4014 * ios.each_codepoint -> an_enumerator
4015 * ios.codepoints -> an_enumerator
4016 *
4017 * Passes the <code>Integer</code> ordinal of each character in <i>ios</i>,
4018 * passing the codepoint as an argument. The stream must be opened for
4019 * reading or an <code>IOError</code> will be raised.
4020 *
4021 * If no block is given, an enumerator is returned instead.
4022 *
4023 */
4024
4025 static VALUE
rb_io_each_codepoint(VALUE io)4026 rb_io_each_codepoint(VALUE io)
4027 {
4028 rb_io_t *fptr;
4029 rb_encoding *enc;
4030 unsigned int c;
4031 int r, n;
4032
4033 RETURN_ENUMERATOR(io, 0, 0);
4034 GetOpenFile(io, fptr);
4035 rb_io_check_char_readable(fptr);
4036
4037 READ_CHECK(fptr);
4038 if (NEED_READCONV(fptr)) {
4039 SET_BINARY_MODE(fptr);
4040 r = 1; /* no invalid char yet */
4041 for (;;) {
4042 make_readconv(fptr, 0);
4043 for (;;) {
4044 if (fptr->cbuf.len) {
4045 if (fptr->encs.enc)
4046 r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
4047 fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4048 fptr->encs.enc);
4049 else
4050 r = ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(1);
4051 if (!MBCLEN_NEEDMORE_P(r))
4052 break;
4053 if (fptr->cbuf.len == fptr->cbuf.capa) {
4054 rb_raise(rb_eIOError, "too long character");
4055 }
4056 }
4057 if (more_char(fptr) == MORE_CHAR_FINISHED) {
4058 clear_readconv(fptr);
4059 if (!MBCLEN_CHARFOUND_P(r)) {
4060 enc = fptr->encs.enc;
4061 goto invalid;
4062 }
4063 return io;
4064 }
4065 }
4066 if (MBCLEN_INVALID_P(r)) {
4067 enc = fptr->encs.enc;
4068 goto invalid;
4069 }
4070 n = MBCLEN_CHARFOUND_LEN(r);
4071 if (fptr->encs.enc) {
4072 c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
4073 fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4074 fptr->encs.enc);
4075 }
4076 else {
4077 c = (unsigned char)fptr->cbuf.ptr[fptr->cbuf.off];
4078 }
4079 fptr->cbuf.off += n;
4080 fptr->cbuf.len -= n;
4081 rb_yield(UINT2NUM(c));
4082 }
4083 }
4084 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
4085 enc = io_input_encoding(fptr);
4086 while (io_fillbuf(fptr) >= 0) {
4087 r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off,
4088 fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
4089 if (MBCLEN_CHARFOUND_P(r) &&
4090 (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
4091 c = rb_enc_codepoint(fptr->rbuf.ptr+fptr->rbuf.off,
4092 fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
4093 fptr->rbuf.off += n;
4094 fptr->rbuf.len -= n;
4095 rb_yield(UINT2NUM(c));
4096 }
4097 else if (MBCLEN_INVALID_P(r)) {
4098 invalid:
4099 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
4100 }
4101 else if (MBCLEN_NEEDMORE_P(r)) {
4102 char cbuf[8], *p = cbuf;
4103 int more = MBCLEN_NEEDMORE_LEN(r);
4104 if (more > numberof(cbuf)) goto invalid;
4105 more += n = fptr->rbuf.len;
4106 if (more > numberof(cbuf)) goto invalid;
4107 while ((n = (int)read_buffered_data(p, more, fptr)) > 0 &&
4108 (p += n, (more -= n) > 0)) {
4109 if (io_fillbuf(fptr) < 0) goto invalid;
4110 if ((n = fptr->rbuf.len) > more) n = more;
4111 }
4112 r = rb_enc_precise_mbclen(cbuf, p, enc);
4113 if (!MBCLEN_CHARFOUND_P(r)) goto invalid;
4114 c = rb_enc_codepoint(cbuf, p, enc);
4115 rb_yield(UINT2NUM(c));
4116 }
4117 else {
4118 continue;
4119 }
4120 }
4121 return io;
4122 }
4123
4124 /*
4125 * This is a deprecated alias for <code>each_codepoint</code>.
4126 */
4127
4128 static VALUE
rb_io_codepoints(VALUE io)4129 rb_io_codepoints(VALUE io)
4130 {
4131 rb_warn("IO#codepoints is deprecated; use #each_codepoint instead");
4132 if (!rb_block_given_p())
4133 return rb_enumeratorize(io, ID2SYM(rb_intern("each_codepoint")), 0, 0);
4134 return rb_io_each_codepoint(io);
4135 }
4136
4137
4138 /*
4139 * call-seq:
4140 * ios.getc -> string or nil
4141 *
4142 * Reads a one-character string from <em>ios</em>. Returns
4143 * +nil+ if called at end of file.
4144 *
4145 * f = File.new("testfile")
4146 * f.getc #=> "h"
4147 * f.getc #=> "e"
4148 */
4149
4150 static VALUE
rb_io_getc(VALUE io)4151 rb_io_getc(VALUE io)
4152 {
4153 rb_io_t *fptr;
4154 rb_encoding *enc;
4155
4156 GetOpenFile(io, fptr);
4157 rb_io_check_char_readable(fptr);
4158
4159 enc = io_input_encoding(fptr);
4160 READ_CHECK(fptr);
4161 return io_getc(fptr, enc);
4162 }
4163
4164 /*
4165 * call-seq:
4166 * ios.readchar -> string
4167 *
4168 * Reads a one-character string from <em>ios</em>. Raises an
4169 * <code>EOFError</code> on end of file.
4170 *
4171 * f = File.new("testfile")
4172 * f.readchar #=> "h"
4173 * f.readchar #=> "e"
4174 */
4175
4176 static VALUE
rb_io_readchar(VALUE io)4177 rb_io_readchar(VALUE io)
4178 {
4179 VALUE c = rb_io_getc(io);
4180
4181 if (NIL_P(c)) {
4182 rb_eof_error();
4183 }
4184 return c;
4185 }
4186
4187 /*
4188 * call-seq:
4189 * ios.getbyte -> integer or nil
4190 *
4191 * Gets the next 8-bit byte (0..255) from <em>ios</em>. Returns
4192 * +nil+ if called at end of file.
4193 *
4194 * f = File.new("testfile")
4195 * f.getbyte #=> 84
4196 * f.getbyte #=> 104
4197 */
4198
4199 VALUE
rb_io_getbyte(VALUE io)4200 rb_io_getbyte(VALUE io)
4201 {
4202 rb_io_t *fptr;
4203 int c;
4204
4205 GetOpenFile(io, fptr);
4206 rb_io_check_byte_readable(fptr);
4207 READ_CHECK(fptr);
4208 if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && RB_TYPE_P(rb_stdout, T_FILE)) {
4209 rb_io_t *ofp;
4210 GetOpenFile(rb_stdout, ofp);
4211 if (ofp->mode & FMODE_TTY) {
4212 rb_io_flush(rb_stdout);
4213 }
4214 }
4215 if (io_fillbuf(fptr) < 0) {
4216 return Qnil;
4217 }
4218 fptr->rbuf.off++;
4219 fptr->rbuf.len--;
4220 c = (unsigned char)fptr->rbuf.ptr[fptr->rbuf.off-1];
4221 return INT2FIX(c & 0xff);
4222 }
4223
4224 /*
4225 * call-seq:
4226 * ios.readbyte -> integer
4227 *
4228 * Reads a byte as with <code>IO#getbyte</code>, but raises an
4229 * <code>EOFError</code> on end of file.
4230 */
4231
4232 static VALUE
rb_io_readbyte(VALUE io)4233 rb_io_readbyte(VALUE io)
4234 {
4235 VALUE c = rb_io_getbyte(io);
4236
4237 if (NIL_P(c)) {
4238 rb_eof_error();
4239 }
4240 return c;
4241 }
4242
4243 /*
4244 * call-seq:
4245 * ios.ungetbyte(string) -> nil
4246 * ios.ungetbyte(integer) -> nil
4247 *
4248 * Pushes back bytes (passed as a parameter) onto <em>ios</em>,
4249 * such that a subsequent buffered read will return it. Only one byte
4250 * may be pushed back before a subsequent read operation (that is,
4251 * you will be able to read only the last of several bytes that have been pushed
4252 * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
4253 *
4254 * f = File.new("testfile") #=> #<File:testfile>
4255 * b = f.getbyte #=> 0x38
4256 * f.ungetbyte(b) #=> nil
4257 * f.getbyte #=> 0x38
4258 */
4259
4260 VALUE
rb_io_ungetbyte(VALUE io,VALUE b)4261 rb_io_ungetbyte(VALUE io, VALUE b)
4262 {
4263 rb_io_t *fptr;
4264 VALUE v;
4265 unsigned char c;
4266
4267 GetOpenFile(io, fptr);
4268 rb_io_check_byte_readable(fptr);
4269 switch (TYPE(b)) {
4270 case T_NIL:
4271 return Qnil;
4272 case T_FIXNUM:
4273 case T_BIGNUM: ;
4274 v = rb_int_modulo(b, INT2FIX(256));
4275 c = NUM2INT(v) & 0xFF;
4276 b = rb_str_new((const char *)&c, 1);
4277 break;
4278 default:
4279 SafeStringValue(b);
4280 }
4281 io_ungetbyte(b, fptr);
4282 return Qnil;
4283 }
4284
4285 /*
4286 * call-seq:
4287 * ios.ungetc(string) -> nil
4288 *
4289 * Pushes back one character (passed as a parameter) onto <em>ios</em>,
4290 * such that a subsequent buffered character read will return it. Only one character
4291 * may be pushed back before a subsequent read operation (that is,
4292 * you will be able to read only the last of several characters that have been pushed
4293 * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
4294 *
4295 * f = File.new("testfile") #=> #<File:testfile>
4296 * c = f.getc #=> "8"
4297 * f.ungetc(c) #=> nil
4298 * f.getc #=> "8"
4299 */
4300
4301 VALUE
rb_io_ungetc(VALUE io,VALUE c)4302 rb_io_ungetc(VALUE io, VALUE c)
4303 {
4304 rb_io_t *fptr;
4305 long len;
4306
4307 GetOpenFile(io, fptr);
4308 rb_io_check_char_readable(fptr);
4309 if (NIL_P(c)) return Qnil;
4310 if (FIXNUM_P(c)) {
4311 c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
4312 }
4313 else if (RB_TYPE_P(c, T_BIGNUM)) {
4314 c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
4315 }
4316 else {
4317 SafeStringValue(c);
4318 }
4319 if (NEED_READCONV(fptr)) {
4320 SET_BINARY_MODE(fptr);
4321 len = RSTRING_LEN(c);
4322 #if SIZEOF_LONG > SIZEOF_INT
4323 if (len > INT_MAX)
4324 rb_raise(rb_eIOError, "ungetc failed");
4325 #endif
4326 make_readconv(fptr, (int)len);
4327 if (fptr->cbuf.capa - fptr->cbuf.len < len)
4328 rb_raise(rb_eIOError, "ungetc failed");
4329 if (fptr->cbuf.off < len) {
4330 MEMMOVE(fptr->cbuf.ptr+fptr->cbuf.capa-fptr->cbuf.len,
4331 fptr->cbuf.ptr+fptr->cbuf.off,
4332 char, fptr->cbuf.len);
4333 fptr->cbuf.off = fptr->cbuf.capa-fptr->cbuf.len;
4334 }
4335 fptr->cbuf.off -= (int)len;
4336 fptr->cbuf.len += (int)len;
4337 MEMMOVE(fptr->cbuf.ptr+fptr->cbuf.off, RSTRING_PTR(c), char, len);
4338 }
4339 else {
4340 NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
4341 io_ungetbyte(c, fptr);
4342 }
4343 return Qnil;
4344 }
4345
4346 /*
4347 * call-seq:
4348 * ios.isatty -> true or false
4349 * ios.tty? -> true or false
4350 *
4351 * Returns <code>true</code> if <em>ios</em> is associated with a
4352 * terminal device (tty), <code>false</code> otherwise.
4353 *
4354 * File.new("testfile").isatty #=> false
4355 * File.new("/dev/tty").isatty #=> true
4356 */
4357
4358 static VALUE
rb_io_isatty(VALUE io)4359 rb_io_isatty(VALUE io)
4360 {
4361 rb_io_t *fptr;
4362
4363 GetOpenFile(io, fptr);
4364 if (isatty(fptr->fd) == 0)
4365 return Qfalse;
4366 return Qtrue;
4367 }
4368
4369 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
4370 /*
4371 * call-seq:
4372 * ios.close_on_exec? -> true or false
4373 *
4374 * Returns <code>true</code> if <em>ios</em> will be closed on exec.
4375 *
4376 * f = open("/dev/null")
4377 * f.close_on_exec? #=> false
4378 * f.close_on_exec = true
4379 * f.close_on_exec? #=> true
4380 * f.close_on_exec = false
4381 * f.close_on_exec? #=> false
4382 */
4383
4384 static VALUE
rb_io_close_on_exec_p(VALUE io)4385 rb_io_close_on_exec_p(VALUE io)
4386 {
4387 rb_io_t *fptr;
4388 VALUE write_io;
4389 int fd, ret;
4390
4391 write_io = GetWriteIO(io);
4392 if (io != write_io) {
4393 GetOpenFile(write_io, fptr);
4394 if (fptr && 0 <= (fd = fptr->fd)) {
4395 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
4396 if (!(ret & FD_CLOEXEC)) return Qfalse;
4397 }
4398 }
4399
4400 GetOpenFile(io, fptr);
4401 if (fptr && 0 <= (fd = fptr->fd)) {
4402 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
4403 if (!(ret & FD_CLOEXEC)) return Qfalse;
4404 }
4405 return Qtrue;
4406 }
4407 #else
4408 #define rb_io_close_on_exec_p rb_f_notimplement
4409 #endif
4410
4411 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
4412 /*
4413 * call-seq:
4414 * ios.close_on_exec = bool -> true or false
4415 *
4416 * Sets a close-on-exec flag.
4417 *
4418 * f = open("/dev/null")
4419 * f.close_on_exec = true
4420 * system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
4421 * f.closed? #=> false
4422 *
4423 * Ruby sets close-on-exec flags of all file descriptors by default
4424 * since Ruby 2.0.0.
4425 * So you don't need to set by yourself.
4426 * Also, unsetting a close-on-exec flag can cause file descriptor leak
4427 * if another thread use fork() and exec() (via system() method for example).
4428 * If you really needs file descriptor inheritance to child process,
4429 * use spawn()'s argument such as fd=>fd.
4430 */
4431
4432 static VALUE
rb_io_set_close_on_exec(VALUE io,VALUE arg)4433 rb_io_set_close_on_exec(VALUE io, VALUE arg)
4434 {
4435 int flag = RTEST(arg) ? FD_CLOEXEC : 0;
4436 rb_io_t *fptr;
4437 VALUE write_io;
4438 int fd, ret;
4439
4440 write_io = GetWriteIO(io);
4441 if (io != write_io) {
4442 GetOpenFile(write_io, fptr);
4443 if (fptr && 0 <= (fd = fptr->fd)) {
4444 if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
4445 if ((ret & FD_CLOEXEC) != flag) {
4446 ret = (ret & ~FD_CLOEXEC) | flag;
4447 ret = fcntl(fd, F_SETFD, ret);
4448 if (ret != 0) rb_sys_fail_path(fptr->pathv);
4449 }
4450 }
4451
4452 }
4453
4454 GetOpenFile(io, fptr);
4455 if (fptr && 0 <= (fd = fptr->fd)) {
4456 if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv);
4457 if ((ret & FD_CLOEXEC) != flag) {
4458 ret = (ret & ~FD_CLOEXEC) | flag;
4459 ret = fcntl(fd, F_SETFD, ret);
4460 if (ret != 0) rb_sys_fail_path(fptr->pathv);
4461 }
4462 }
4463 return Qnil;
4464 }
4465 #else
4466 #define rb_io_set_close_on_exec rb_f_notimplement
4467 #endif
4468
4469 #define FMODE_PREP (1<<16)
4470 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
4471 #define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv))
4472
4473 static VALUE
finish_writeconv(rb_io_t * fptr,int noalloc)4474 finish_writeconv(rb_io_t *fptr, int noalloc)
4475 {
4476 unsigned char *ds, *dp, *de;
4477 rb_econv_result_t res;
4478
4479 if (!fptr->wbuf.ptr) {
4480 unsigned char buf[1024];
4481 long r;
4482
4483 res = econv_destination_buffer_full;
4484 while (res == econv_destination_buffer_full) {
4485 ds = dp = buf;
4486 de = buf + sizeof(buf);
4487 res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
4488 while (dp-ds) {
4489 retry:
4490 if (fptr->write_lock && rb_mutex_owned_p(fptr->write_lock))
4491 r = rb_write_internal2(fptr->fd, ds, dp-ds);
4492 else
4493 r = rb_write_internal(fptr->fd, ds, dp-ds);
4494 if (r == dp-ds)
4495 break;
4496 if (0 <= r) {
4497 ds += r;
4498 }
4499 if (rb_io_wait_writable(fptr->fd)) {
4500 if (fptr->fd < 0)
4501 return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr(closed_stream));
4502 goto retry;
4503 }
4504 return noalloc ? Qtrue : INT2NUM(errno);
4505 }
4506 if (res == econv_invalid_byte_sequence ||
4507 res == econv_incomplete_input ||
4508 res == econv_undefined_conversion) {
4509 return noalloc ? Qtrue : rb_econv_make_exception(fptr->writeconv);
4510 }
4511 }
4512
4513 return Qnil;
4514 }
4515
4516 res = econv_destination_buffer_full;
4517 while (res == econv_destination_buffer_full) {
4518 if (fptr->wbuf.len == fptr->wbuf.capa) {
4519 if (io_fflush(fptr) < 0)
4520 return noalloc ? Qtrue : INT2NUM(errno);
4521 }
4522
4523 ds = dp = (unsigned char *)fptr->wbuf.ptr + fptr->wbuf.off + fptr->wbuf.len;
4524 de = (unsigned char *)fptr->wbuf.ptr + fptr->wbuf.capa;
4525 res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
4526 fptr->wbuf.len += (int)(dp - ds);
4527 if (res == econv_invalid_byte_sequence ||
4528 res == econv_incomplete_input ||
4529 res == econv_undefined_conversion) {
4530 return noalloc ? Qtrue : rb_econv_make_exception(fptr->writeconv);
4531 }
4532 }
4533 return Qnil;
4534 }
4535
4536 struct finish_writeconv_arg {
4537 rb_io_t *fptr;
4538 int noalloc;
4539 };
4540
4541 static VALUE
finish_writeconv_sync(VALUE arg)4542 finish_writeconv_sync(VALUE arg)
4543 {
4544 struct finish_writeconv_arg *p = (struct finish_writeconv_arg *)arg;
4545 return finish_writeconv(p->fptr, p->noalloc);
4546 }
4547
4548 static void*
nogvl_close(void * ptr)4549 nogvl_close(void *ptr)
4550 {
4551 int *fd = ptr;
4552
4553 return (void*)(intptr_t)close(*fd);
4554 }
4555
4556 static int
maygvl_close(int fd,int keepgvl)4557 maygvl_close(int fd, int keepgvl)
4558 {
4559 if (keepgvl)
4560 return close(fd);
4561
4562 /*
4563 * close() may block for certain file types (NFS, SO_LINGER sockets,
4564 * inotify), so let other threads run.
4565 */
4566 return (int)(intptr_t)rb_thread_call_without_gvl(nogvl_close, &fd, RUBY_UBF_IO, 0);
4567 }
4568
4569 static void*
nogvl_fclose(void * ptr)4570 nogvl_fclose(void *ptr)
4571 {
4572 FILE *file = ptr;
4573
4574 return (void*)(intptr_t)fclose(file);
4575 }
4576
4577 static int
maygvl_fclose(FILE * file,int keepgvl)4578 maygvl_fclose(FILE *file, int keepgvl)
4579 {
4580 if (keepgvl)
4581 return fclose(file);
4582
4583 return (int)(intptr_t)rb_thread_call_without_gvl(nogvl_fclose, file, RUBY_UBF_IO, 0);
4584 }
4585
4586 static void free_io_buffer(rb_io_buffer_t *buf);
4587 static void clear_codeconv(rb_io_t *fptr);
4588
4589 static void
fptr_finalize_flush(rb_io_t * fptr,int noraise,int keepgvl,struct list_head * busy)4590 fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl,
4591 struct list_head *busy)
4592 {
4593 VALUE err = Qnil;
4594 int fd = fptr->fd;
4595 FILE *stdio_file = fptr->stdio_file;
4596 int mode = fptr->mode;
4597
4598 if (fptr->writeconv) {
4599 if (fptr->write_lock && !noraise) {
4600 struct finish_writeconv_arg arg;
4601 arg.fptr = fptr;
4602 arg.noalloc = noraise;
4603 err = rb_mutex_synchronize(fptr->write_lock, finish_writeconv_sync, (VALUE)&arg);
4604 }
4605 else {
4606 err = finish_writeconv(fptr, noraise);
4607 }
4608 }
4609 if (fptr->wbuf.len) {
4610 if (noraise) {
4611 io_flush_buffer_sync(fptr);
4612 }
4613 else {
4614 if (io_fflush(fptr) < 0 && NIL_P(err))
4615 err = INT2NUM(errno);
4616 }
4617 }
4618
4619 fptr->fd = -1;
4620 fptr->stdio_file = 0;
4621 fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
4622
4623 /*
4624 * ensure waiting_fd users do not hit EBADF, wait for them
4625 * to exit before we call close().
4626 */
4627 if (busy) {
4628 do rb_thread_schedule(); while (!list_empty(busy));
4629 }
4630
4631 if (IS_PREP_STDIO(fptr) || fd <= 2) {
4632 /* need to keep FILE objects of stdin, stdout and stderr */
4633 }
4634 else if (stdio_file) {
4635 /* stdio_file is deallocated anyway
4636 * even if fclose failed. */
4637 if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(err))
4638 if (!noraise) err = INT2NUM(errno);
4639 }
4640 else if (0 <= fd) {
4641 /* fptr->fd may be closed even if close fails.
4642 * POSIX doesn't specify it.
4643 * We assumes it is closed. */
4644
4645 /**/
4646 keepgvl |= !(mode & FMODE_WRITABLE);
4647 keepgvl |= noraise;
4648 if ((maygvl_close(fd, keepgvl) < 0) && NIL_P(err))
4649 if (!noraise) err = INT2NUM(errno);
4650 }
4651
4652 if (!NIL_P(err) && !noraise) {
4653 if (RB_INTEGER_TYPE_P(err))
4654 rb_syserr_fail_path(NUM2INT(err), fptr->pathv);
4655 else
4656 rb_exc_raise(err);
4657 }
4658 }
4659
4660 static void
fptr_finalize(rb_io_t * fptr,int noraise)4661 fptr_finalize(rb_io_t *fptr, int noraise)
4662 {
4663 fptr_finalize_flush(fptr, noraise, FALSE, 0);
4664 free_io_buffer(&fptr->rbuf);
4665 free_io_buffer(&fptr->wbuf);
4666 clear_codeconv(fptr);
4667 }
4668
4669 static void
rb_io_fptr_cleanup(rb_io_t * fptr,int noraise)4670 rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
4671 {
4672 if (fptr->finalize) {
4673 (*fptr->finalize)(fptr, noraise);
4674 }
4675 else {
4676 fptr_finalize(fptr, noraise);
4677 }
4678 }
4679
4680 static void
free_io_buffer(rb_io_buffer_t * buf)4681 free_io_buffer(rb_io_buffer_t *buf)
4682 {
4683 if (buf->ptr) {
4684 ruby_sized_xfree(buf->ptr, (size_t)buf->capa);
4685 buf->ptr = NULL;
4686 }
4687 }
4688
4689 static void
clear_readconv(rb_io_t * fptr)4690 clear_readconv(rb_io_t *fptr)
4691 {
4692 if (fptr->readconv) {
4693 rb_econv_close(fptr->readconv);
4694 fptr->readconv = NULL;
4695 }
4696 free_io_buffer(&fptr->cbuf);
4697 }
4698
4699 static void
clear_writeconv(rb_io_t * fptr)4700 clear_writeconv(rb_io_t *fptr)
4701 {
4702 if (fptr->writeconv) {
4703 rb_econv_close(fptr->writeconv);
4704 fptr->writeconv = NULL;
4705 }
4706 fptr->writeconv_initialized = 0;
4707 }
4708
4709 static void
clear_codeconv(rb_io_t * fptr)4710 clear_codeconv(rb_io_t *fptr)
4711 {
4712 clear_readconv(fptr);
4713 clear_writeconv(fptr);
4714 }
4715
4716 void
rb_io_fptr_finalize_internal(void * ptr)4717 rb_io_fptr_finalize_internal(void *ptr)
4718 {
4719 rb_io_t *fptr = ptr;
4720
4721 if (!ptr) return;
4722 fptr->pathv = Qnil;
4723 if (0 <= fptr->fd)
4724 rb_io_fptr_cleanup(fptr, TRUE);
4725 fptr->write_lock = 0;
4726 free_io_buffer(&fptr->rbuf);
4727 free_io_buffer(&fptr->wbuf);
4728 clear_codeconv(fptr);
4729 free(fptr);
4730 }
4731
4732 #undef rb_io_fptr_finalize
4733 int
rb_io_fptr_finalize(rb_io_t * fptr)4734 rb_io_fptr_finalize(rb_io_t *fptr)
4735 {
4736 if (!fptr) {
4737 return 0;
4738 }
4739 else {
4740 rb_io_fptr_finalize_internal(fptr);
4741 return 1;
4742 }
4743 }
4744 #define rb_io_fptr_finalize(fptr) rb_io_fptr_finalize_internal(fptr)
4745
4746 RUBY_FUNC_EXPORTED size_t
rb_io_memsize(const rb_io_t * fptr)4747 rb_io_memsize(const rb_io_t *fptr)
4748 {
4749 size_t size = sizeof(rb_io_t);
4750 size += fptr->rbuf.capa;
4751 size += fptr->wbuf.capa;
4752 size += fptr->cbuf.capa;
4753 if (fptr->readconv) size += rb_econv_memsize(fptr->readconv);
4754 if (fptr->writeconv) size += rb_econv_memsize(fptr->writeconv);
4755 return size;
4756 }
4757
4758 #ifdef _WIN32
4759 /* keep GVL while closing to prevent crash on Windows */
4760 # define KEEPGVL TRUE
4761 #else
4762 # define KEEPGVL FALSE
4763 #endif
4764
4765 int rb_notify_fd_close(int fd, struct list_head *);
4766 static rb_io_t *
io_close_fptr(VALUE io)4767 io_close_fptr(VALUE io)
4768 {
4769 rb_io_t *fptr;
4770 VALUE write_io;
4771 rb_io_t *write_fptr;
4772 struct list_head busy;
4773
4774 list_head_init(&busy);
4775 write_io = GetWriteIO(io);
4776 if (io != write_io) {
4777 write_fptr = RFILE(write_io)->fptr;
4778 if (write_fptr && 0 <= write_fptr->fd) {
4779 rb_io_fptr_cleanup(write_fptr, TRUE);
4780 }
4781 }
4782
4783 fptr = RFILE(io)->fptr;
4784 if (!fptr) return 0;
4785 if (fptr->fd < 0) return 0;
4786
4787 if (rb_notify_fd_close(fptr->fd, &busy)) {
4788 /* calls close(fptr->fd): */
4789 fptr_finalize_flush(fptr, FALSE, KEEPGVL, &busy);
4790 }
4791 rb_io_fptr_cleanup(fptr, FALSE);
4792 return fptr;
4793 }
4794
4795 static void
fptr_waitpid(rb_io_t * fptr,int nohang)4796 fptr_waitpid(rb_io_t *fptr, int nohang)
4797 {
4798 int status;
4799 if (fptr->pid) {
4800 rb_last_status_clear();
4801 rb_waitpid(fptr->pid, &status, nohang ? WNOHANG : 0);
4802 fptr->pid = 0;
4803 }
4804 }
4805
4806 VALUE
rb_io_close(VALUE io)4807 rb_io_close(VALUE io)
4808 {
4809 rb_io_t *fptr = io_close_fptr(io);
4810 if (fptr) fptr_waitpid(fptr, 0);
4811 return Qnil;
4812 }
4813
4814 /*
4815 * call-seq:
4816 * ios.close -> nil
4817 *
4818 * Closes <em>ios</em> and flushes any pending writes to the operating
4819 * system. The stream is unavailable for any further data operations;
4820 * an <code>IOError</code> is raised if such an attempt is made. I/O
4821 * streams are automatically closed when they are claimed by the
4822 * garbage collector.
4823 *
4824 * If <em>ios</em> is opened by <code>IO.popen</code>,
4825 * <code>close</code> sets <code>$?</code>.
4826 *
4827 * Calling this method on closed IO object is just ignored since Ruby 2.3.
4828 */
4829
4830 static VALUE
rb_io_close_m(VALUE io)4831 rb_io_close_m(VALUE io)
4832 {
4833 rb_io_t *fptr = rb_io_get_fptr(io);
4834 if (fptr->fd < 0) {
4835 return Qnil;
4836 }
4837 rb_io_close(io);
4838 return Qnil;
4839 }
4840
4841 static VALUE
io_call_close(VALUE io)4842 io_call_close(VALUE io)
4843 {
4844 rb_check_funcall(io, rb_intern("close"), 0, 0);
4845 return io;
4846 }
4847
4848 static VALUE
ignore_closed_stream(VALUE io,VALUE exc)4849 ignore_closed_stream(VALUE io, VALUE exc)
4850 {
4851 enum {mesg_len = sizeof(closed_stream)-1};
4852 VALUE mesg = rb_attr_get(exc, idMesg);
4853 if (!RB_TYPE_P(mesg, T_STRING) ||
4854 RSTRING_LEN(mesg) != mesg_len ||
4855 memcmp(RSTRING_PTR(mesg), closed_stream, mesg_len)) {
4856 rb_exc_raise(exc);
4857 }
4858 return io;
4859 }
4860
4861 static VALUE
io_close(VALUE io)4862 io_close(VALUE io)
4863 {
4864 VALUE closed = rb_check_funcall(io, rb_intern("closed?"), 0, 0);
4865 if (closed != Qundef && RTEST(closed)) return io;
4866 rb_rescue2(io_call_close, io, ignore_closed_stream, io,
4867 rb_eIOError, (VALUE)0);
4868 return io;
4869 }
4870
4871 /*
4872 * call-seq:
4873 * ios.closed? -> true or false
4874 *
4875 * Returns <code>true</code> if <em>ios</em> is completely closed (for
4876 * duplex streams, both reader and writer), <code>false</code>
4877 * otherwise.
4878 *
4879 * f = File.new("testfile")
4880 * f.close #=> nil
4881 * f.closed? #=> true
4882 * f = IO.popen("/bin/sh","r+")
4883 * f.close_write #=> nil
4884 * f.closed? #=> false
4885 * f.close_read #=> nil
4886 * f.closed? #=> true
4887 */
4888
4889
4890 static VALUE
rb_io_closed(VALUE io)4891 rb_io_closed(VALUE io)
4892 {
4893 rb_io_t *fptr;
4894 VALUE write_io;
4895 rb_io_t *write_fptr;
4896
4897 write_io = GetWriteIO(io);
4898 if (io != write_io) {
4899 write_fptr = RFILE(write_io)->fptr;
4900 if (write_fptr && 0 <= write_fptr->fd) {
4901 return Qfalse;
4902 }
4903 }
4904
4905 fptr = rb_io_get_fptr(io);
4906 return 0 <= fptr->fd ? Qfalse : Qtrue;
4907 }
4908
4909 /*
4910 * call-seq:
4911 * ios.close_read -> nil
4912 *
4913 * Closes the read end of a duplex I/O stream (i.e., one that contains
4914 * both a read and a write stream, such as a pipe). Will raise an
4915 * <code>IOError</code> if the stream is not duplexed.
4916 *
4917 * f = IO.popen("/bin/sh","r+")
4918 * f.close_read
4919 * f.readlines
4920 *
4921 * <em>produces:</em>
4922 *
4923 * prog.rb:3:in `readlines': not opened for reading (IOError)
4924 * from prog.rb:3
4925 *
4926 * Calling this method on closed IO object is just ignored since Ruby 2.3.
4927 */
4928
4929 static VALUE
rb_io_close_read(VALUE io)4930 rb_io_close_read(VALUE io)
4931 {
4932 rb_io_t *fptr;
4933 VALUE write_io;
4934
4935 fptr = rb_io_get_fptr(rb_io_taint_check(io));
4936 if (fptr->fd < 0) return Qnil;
4937 if (is_socket(fptr->fd, fptr->pathv)) {
4938 #ifndef SHUT_RD
4939 # define SHUT_RD 0
4940 #endif
4941 if (shutdown(fptr->fd, SHUT_RD) < 0)
4942 rb_sys_fail_path(fptr->pathv);
4943 fptr->mode &= ~FMODE_READABLE;
4944 if (!(fptr->mode & FMODE_WRITABLE))
4945 return rb_io_close(io);
4946 return Qnil;
4947 }
4948
4949 write_io = GetWriteIO(io);
4950 if (io != write_io) {
4951 rb_io_t *wfptr;
4952 wfptr = rb_io_get_fptr(rb_io_taint_check(write_io));
4953 wfptr->pid = fptr->pid;
4954 fptr->pid = 0;
4955 RFILE(io)->fptr = wfptr;
4956 /* bind to write_io temporarily to get rid of memory/fd leak */
4957 fptr->tied_io_for_writing = 0;
4958 RFILE(write_io)->fptr = fptr;
4959 rb_io_fptr_cleanup(fptr, FALSE);
4960 /* should not finalize fptr because another thread may be reading it */
4961 return Qnil;
4962 }
4963
4964 if ((fptr->mode & (FMODE_DUPLEX|FMODE_WRITABLE)) == FMODE_WRITABLE) {
4965 rb_raise(rb_eIOError, "closing non-duplex IO for reading");
4966 }
4967 return rb_io_close(io);
4968 }
4969
4970 /*
4971 * call-seq:
4972 * ios.close_write -> nil
4973 *
4974 * Closes the write end of a duplex I/O stream (i.e., one that contains
4975 * both a read and a write stream, such as a pipe). Will raise an
4976 * <code>IOError</code> if the stream is not duplexed.
4977 *
4978 * f = IO.popen("/bin/sh","r+")
4979 * f.close_write
4980 * f.print "nowhere"
4981 *
4982 * <em>produces:</em>
4983 *
4984 * prog.rb:3:in `write': not opened for writing (IOError)
4985 * from prog.rb:3:in `print'
4986 * from prog.rb:3
4987 *
4988 * Calling this method on closed IO object is just ignored since Ruby 2.3.
4989 */
4990
4991 static VALUE
rb_io_close_write(VALUE io)4992 rb_io_close_write(VALUE io)
4993 {
4994 rb_io_t *fptr;
4995 VALUE write_io;
4996
4997 write_io = GetWriteIO(io);
4998 fptr = rb_io_get_fptr(rb_io_taint_check(write_io));
4999 if (fptr->fd < 0) return Qnil;
5000 if (is_socket(fptr->fd, fptr->pathv)) {
5001 #ifndef SHUT_WR
5002 # define SHUT_WR 1
5003 #endif
5004 if (shutdown(fptr->fd, SHUT_WR) < 0)
5005 rb_sys_fail_path(fptr->pathv);
5006 fptr->mode &= ~FMODE_WRITABLE;
5007 if (!(fptr->mode & FMODE_READABLE))
5008 return rb_io_close(write_io);
5009 return Qnil;
5010 }
5011
5012 if ((fptr->mode & (FMODE_DUPLEX|FMODE_READABLE)) == FMODE_READABLE) {
5013 rb_raise(rb_eIOError, "closing non-duplex IO for writing");
5014 }
5015
5016 if (io != write_io) {
5017 fptr = rb_io_get_fptr(rb_io_taint_check(io));
5018 fptr->tied_io_for_writing = 0;
5019 }
5020 rb_io_close(write_io);
5021 return Qnil;
5022 }
5023
5024 /*
5025 * call-seq:
5026 * ios.sysseek(offset, whence=IO::SEEK_SET) -> integer
5027 *
5028 * Seeks to a given <i>offset</i> in the stream according to the value
5029 * of <i>whence</i> (see <code>IO#seek</code> for values of
5030 * <i>whence</i>). Returns the new offset into the file.
5031 *
5032 * f = File.new("testfile")
5033 * f.sysseek(-13, IO::SEEK_END) #=> 53
5034 * f.sysread(10) #=> "And so on."
5035 */
5036
5037 static VALUE
rb_io_sysseek(int argc,VALUE * argv,VALUE io)5038 rb_io_sysseek(int argc, VALUE *argv, VALUE io)
5039 {
5040 VALUE offset, ptrname;
5041 int whence = SEEK_SET;
5042 rb_io_t *fptr;
5043 off_t pos;
5044
5045 if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
5046 whence = interpret_seek_whence(ptrname);
5047 }
5048 pos = NUM2OFFT(offset);
5049 GetOpenFile(io, fptr);
5050 if ((fptr->mode & FMODE_READABLE) &&
5051 (READ_DATA_BUFFERED(fptr) || READ_CHAR_PENDING(fptr))) {
5052 rb_raise(rb_eIOError, "sysseek for buffered IO");
5053 }
5054 if ((fptr->mode & FMODE_WRITABLE) && fptr->wbuf.len) {
5055 rb_warn("sysseek for buffered IO");
5056 }
5057 errno = 0;
5058 pos = lseek(fptr->fd, pos, whence);
5059 if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
5060
5061 return OFFT2NUM(pos);
5062 }
5063
5064 /*
5065 * call-seq:
5066 * ios.syswrite(string) -> integer
5067 *
5068 * Writes the given string to <em>ios</em> using a low-level write.
5069 * Returns the number of bytes written. Do not mix with other methods
5070 * that write to <em>ios</em> or you may get unpredictable results.
5071 * Raises <code>SystemCallError</code> on error.
5072 *
5073 * f = File.new("out", "w")
5074 * f.syswrite("ABCDEF") #=> 6
5075 */
5076
5077 static VALUE
rb_io_syswrite(VALUE io,VALUE str)5078 rb_io_syswrite(VALUE io, VALUE str)
5079 {
5080 VALUE tmp;
5081 rb_io_t *fptr;
5082 long n, len;
5083 const char *ptr;
5084
5085 if (!RB_TYPE_P(str, T_STRING))
5086 str = rb_obj_as_string(str);
5087
5088 io = GetWriteIO(io);
5089 GetOpenFile(io, fptr);
5090 rb_io_check_writable(fptr);
5091
5092 if (fptr->wbuf.len) {
5093 rb_warn("syswrite for buffered IO");
5094 }
5095
5096 tmp = rb_str_tmp_frozen_acquire(str);
5097 RSTRING_GETMEM(tmp, ptr, len);
5098 n = rb_write_internal(fptr->fd, ptr, len);
5099 if (n < 0) rb_sys_fail_path(fptr->pathv);
5100 rb_str_tmp_frozen_release(str, tmp);
5101
5102 return LONG2FIX(n);
5103 }
5104
5105 /*
5106 * call-seq:
5107 * ios.sysread(maxlen[, outbuf]) -> string
5108 *
5109 * Reads <i>maxlen</i> bytes from <em>ios</em> using a low-level
5110 * read and returns them as a string. Do not mix with other methods
5111 * that read from <em>ios</em> or you may get unpredictable results.
5112 *
5113 * If the optional _outbuf_ argument is present,
5114 * it must reference a String, which will receive the data.
5115 * The _outbuf_ will contain only the received data after the method call
5116 * even if it is not empty at the beginning.
5117 *
5118 * Raises <code>SystemCallError</code> on error and
5119 * <code>EOFError</code> at end of file.
5120 *
5121 * f = File.new("testfile")
5122 * f.sysread(16) #=> "This is line one"
5123 */
5124
5125 static VALUE
rb_io_sysread(int argc,VALUE * argv,VALUE io)5126 rb_io_sysread(int argc, VALUE *argv, VALUE io)
5127 {
5128 VALUE len, str;
5129 rb_io_t *fptr;
5130 long n, ilen;
5131 struct io_internal_read_struct iis;
5132 int shrinkable;
5133
5134 rb_scan_args(argc, argv, "11", &len, &str);
5135 ilen = NUM2LONG(len);
5136
5137 shrinkable = io_setstrbuf(&str, ilen);
5138 if (ilen == 0) return str;
5139
5140 GetOpenFile(io, fptr);
5141 rb_io_check_byte_readable(fptr);
5142
5143 if (READ_DATA_BUFFERED(fptr)) {
5144 rb_raise(rb_eIOError, "sysread for buffered IO");
5145 }
5146
5147 /*
5148 * FIXME: removing rb_thread_wait_fd() here changes sysread semantics
5149 * on non-blocking IOs. However, it's still currently possible
5150 * for sysread to raise Errno::EAGAIN if another thread read()s
5151 * the IO after we return from rb_thread_wait_fd() but before
5152 * we call read()
5153 */
5154 rb_thread_wait_fd(fptr->fd);
5155
5156 rb_io_check_closed(fptr);
5157
5158 io_setstrbuf(&str, ilen);
5159 iis.fd = fptr->fd;
5160 iis.nonblock = 1; /* for historical reasons, maybe (see above) */
5161 iis.buf = RSTRING_PTR(str);
5162 iis.capa = ilen;
5163 n = read_internal_locktmp(str, &iis);
5164
5165 if (n < 0) {
5166 rb_sys_fail_path(fptr->pathv);
5167 }
5168 io_set_read_length(str, n, shrinkable);
5169 if (n == 0 && ilen > 0) {
5170 rb_eof_error();
5171 }
5172 OBJ_TAINT(str);
5173
5174 return str;
5175 }
5176
5177 #if defined(HAVE_PREAD) || defined(HAVE_PWRITE)
5178 struct prdwr_internal_arg {
5179 int fd;
5180 void *buf;
5181 size_t count;
5182 off_t offset;
5183 };
5184 #endif /* HAVE_PREAD || HAVE_PWRITE */
5185
5186 #if defined(HAVE_PREAD)
5187 static VALUE
internal_pread_func(void * arg)5188 internal_pread_func(void *arg)
5189 {
5190 struct prdwr_internal_arg *p = arg;
5191 return (VALUE)pread(p->fd, p->buf, p->count, p->offset);
5192 }
5193
5194 static VALUE
pread_internal_call(VALUE arg)5195 pread_internal_call(VALUE arg)
5196 {
5197 struct prdwr_internal_arg *p = (struct prdwr_internal_arg *)arg;
5198 return rb_thread_io_blocking_region(internal_pread_func, p, p->fd);
5199 }
5200
5201 /*
5202 * call-seq:
5203 * ios.pread(maxlen, offset[, outbuf]) -> string
5204 *
5205 * Reads <i>maxlen</i> bytes from <em>ios</em> using the pread system call
5206 * and returns them as a string without modifying the underlying
5207 * descriptor offset. This is advantageous compared to combining IO#seek
5208 * and IO#read in that it is atomic, allowing multiple threads/process to
5209 * share the same IO object for reading the file at various locations.
5210 * This bypasses any userspace buffering of the IO layer.
5211 * If the optional <i>outbuf</i> argument is present, it must
5212 * reference a String, which will receive the data.
5213 * Raises <code>SystemCallError</code> on error, <code>EOFError</code>
5214 * at end of file and <code>NotImplementedError</code> if platform does not
5215 * implement the system call.
5216 *
5217 * File.write("testfile", "This is line one\nThis is line two\n")
5218 * File.open("testfile") do |f|
5219 * p f.read # => "This is line one\nThis is line two\n"
5220 * p f.pread(12, 0) # => "This is line"
5221 * p f.pread(9, 8) # => "line one\n"
5222 * end
5223 */
5224 static VALUE
rb_io_pread(int argc,VALUE * argv,VALUE io)5225 rb_io_pread(int argc, VALUE *argv, VALUE io)
5226 {
5227 VALUE len, offset, str;
5228 rb_io_t *fptr;
5229 ssize_t n;
5230 struct prdwr_internal_arg arg;
5231 int shrinkable;
5232
5233 rb_scan_args(argc, argv, "21", &len, &offset, &str);
5234 arg.count = NUM2SIZET(len);
5235 arg.offset = NUM2OFFT(offset);
5236
5237 shrinkable = io_setstrbuf(&str, (long)arg.count);
5238 if (arg.count == 0) return str;
5239 arg.buf = RSTRING_PTR(str);
5240
5241 GetOpenFile(io, fptr);
5242 rb_io_check_byte_readable(fptr);
5243
5244 arg.fd = fptr->fd;
5245 rb_io_check_closed(fptr);
5246
5247 rb_str_locktmp(str);
5248 n = (ssize_t)rb_ensure(pread_internal_call, (VALUE)&arg, rb_str_unlocktmp, str);
5249
5250 if (n < 0) {
5251 rb_sys_fail_path(fptr->pathv);
5252 }
5253 io_set_read_length(str, n, shrinkable);
5254 if (n == 0 && arg.count > 0) {
5255 rb_eof_error();
5256 }
5257 OBJ_TAINT(str);
5258
5259 return str;
5260 }
5261 #else
5262 # define rb_io_pread rb_f_notimplement
5263 #endif /* HAVE_PREAD */
5264
5265 #if defined(HAVE_PWRITE)
5266 static VALUE
internal_pwrite_func(void * ptr)5267 internal_pwrite_func(void *ptr)
5268 {
5269 struct prdwr_internal_arg *arg = ptr;
5270
5271 return (VALUE)pwrite(arg->fd, arg->buf, arg->count, arg->offset);
5272 }
5273
5274 /*
5275 * call-seq:
5276 * ios.pwrite(string, offset) -> integer
5277 *
5278 * Writes the given string to <em>ios</em> at <i>offset</i> using pwrite()
5279 * system call. This is advantageous to combining IO#seek and IO#write
5280 * in that it is atomic, allowing multiple threads/process to share the
5281 * same IO object for reading the file at various locations.
5282 * This bypasses any userspace buffering of the IO layer.
5283 * Returns the number of bytes written.
5284 * Raises <code>SystemCallError</code> on error and <code>NotImplementedError</code>
5285 * if platform does not implement the system call.
5286 *
5287 * File.open("out", "w") do |f|
5288 * f.pwrite("ABCDEF", 3) #=> 6
5289 * end
5290 *
5291 * File.read("out") #=> "\u0000\u0000\u0000ABCDEF"
5292 */
5293 static VALUE
rb_io_pwrite(VALUE io,VALUE str,VALUE offset)5294 rb_io_pwrite(VALUE io, VALUE str, VALUE offset)
5295 {
5296 rb_io_t *fptr;
5297 ssize_t n;
5298 struct prdwr_internal_arg arg;
5299 VALUE tmp;
5300
5301 if (!RB_TYPE_P(str, T_STRING))
5302 str = rb_obj_as_string(str);
5303
5304 arg.offset = NUM2OFFT(offset);
5305
5306 io = GetWriteIO(io);
5307 GetOpenFile(io, fptr);
5308 rb_io_check_writable(fptr);
5309 arg.fd = fptr->fd;
5310
5311 tmp = rb_str_tmp_frozen_acquire(str);
5312 arg.buf = RSTRING_PTR(tmp);
5313 arg.count = (size_t)RSTRING_LEN(tmp);
5314
5315 n = (ssize_t)rb_thread_io_blocking_region(internal_pwrite_func, &arg, fptr->fd);
5316 if (n < 0) rb_sys_fail_path(fptr->pathv);
5317 rb_str_tmp_frozen_release(str, tmp);
5318
5319 return SSIZET2NUM(n);
5320 }
5321 #else
5322 # define rb_io_pwrite rb_f_notimplement
5323 #endif /* HAVE_PWRITE */
5324
5325 VALUE
rb_io_binmode(VALUE io)5326 rb_io_binmode(VALUE io)
5327 {
5328 rb_io_t *fptr;
5329
5330 GetOpenFile(io, fptr);
5331 if (fptr->readconv)
5332 rb_econv_binmode(fptr->readconv);
5333 if (fptr->writeconv)
5334 rb_econv_binmode(fptr->writeconv);
5335 fptr->mode |= FMODE_BINMODE;
5336 fptr->mode &= ~FMODE_TEXTMODE;
5337 fptr->writeconv_pre_ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
5338 #ifdef O_BINARY
5339 if (!fptr->readconv) {
5340 SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
5341 }
5342 else {
5343 setmode(fptr->fd, O_BINARY);
5344 }
5345 #endif
5346 return io;
5347 }
5348
5349 static void
io_ascii8bit_binmode(rb_io_t * fptr)5350 io_ascii8bit_binmode(rb_io_t *fptr)
5351 {
5352 if (fptr->readconv) {
5353 rb_econv_close(fptr->readconv);
5354 fptr->readconv = NULL;
5355 }
5356 if (fptr->writeconv) {
5357 rb_econv_close(fptr->writeconv);
5358 fptr->writeconv = NULL;
5359 }
5360 fptr->mode |= FMODE_BINMODE;
5361 fptr->mode &= ~FMODE_TEXTMODE;
5362 SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
5363
5364 fptr->encs.enc = rb_ascii8bit_encoding();
5365 fptr->encs.enc2 = NULL;
5366 fptr->encs.ecflags = 0;
5367 fptr->encs.ecopts = Qnil;
5368 clear_codeconv(fptr);
5369 }
5370
5371 VALUE
rb_io_ascii8bit_binmode(VALUE io)5372 rb_io_ascii8bit_binmode(VALUE io)
5373 {
5374 rb_io_t *fptr;
5375
5376 GetOpenFile(io, fptr);
5377 io_ascii8bit_binmode(fptr);
5378
5379 return io;
5380 }
5381
5382 /*
5383 * call-seq:
5384 * ios.binmode -> ios
5385 *
5386 * Puts <em>ios</em> into binary mode.
5387 * Once a stream is in binary mode, it cannot be reset to nonbinary mode.
5388 *
5389 * - newline conversion disabled
5390 * - encoding conversion disabled
5391 * - content is treated as ASCII-8BIT
5392 */
5393
5394 static VALUE
rb_io_binmode_m(VALUE io)5395 rb_io_binmode_m(VALUE io)
5396 {
5397 VALUE write_io;
5398
5399 rb_io_ascii8bit_binmode(io);
5400
5401 write_io = GetWriteIO(io);
5402 if (write_io != io)
5403 rb_io_ascii8bit_binmode(write_io);
5404 return io;
5405 }
5406
5407 /*
5408 * call-seq:
5409 * ios.binmode? -> true or false
5410 *
5411 * Returns <code>true</code> if <em>ios</em> is binmode.
5412 */
5413 static VALUE
rb_io_binmode_p(VALUE io)5414 rb_io_binmode_p(VALUE io)
5415 {
5416 rb_io_t *fptr;
5417 GetOpenFile(io, fptr);
5418 return fptr->mode & FMODE_BINMODE ? Qtrue : Qfalse;
5419 }
5420
5421 static const char*
rb_io_fmode_modestr(int fmode)5422 rb_io_fmode_modestr(int fmode)
5423 {
5424 if (fmode & FMODE_APPEND) {
5425 if ((fmode & FMODE_READWRITE) == FMODE_READWRITE) {
5426 return MODE_BTMODE("a+", "ab+", "at+");
5427 }
5428 return MODE_BTMODE("a", "ab", "at");
5429 }
5430 switch (fmode & FMODE_READWRITE) {
5431 default:
5432 rb_raise(rb_eArgError, "invalid access fmode 0x%x", fmode);
5433 case FMODE_READABLE:
5434 return MODE_BTMODE("r", "rb", "rt");
5435 case FMODE_WRITABLE:
5436 return MODE_BTXMODE("w", "wb", "wt", "wx", "wbx", "wtx");
5437 case FMODE_READWRITE:
5438 if (fmode & FMODE_CREATE) {
5439 return MODE_BTXMODE("w+", "wb+", "wt+", "w+x", "wb+x", "wt+x");
5440 }
5441 return MODE_BTMODE("r+", "rb+", "rt+");
5442 }
5443 }
5444
5445 static const char bom_prefix[] = "bom|";
5446 static const char utf_prefix[] = "utf-";
5447 enum {bom_prefix_len = (int)sizeof(bom_prefix) - 1};
5448 enum {utf_prefix_len = (int)sizeof(utf_prefix) - 1};
5449
5450 static int
io_encname_bom_p(const char * name,long len)5451 io_encname_bom_p(const char *name, long len)
5452 {
5453 return len > bom_prefix_len && STRNCASECMP(name, bom_prefix, bom_prefix_len) == 0;
5454 }
5455
5456 int
rb_io_modestr_fmode(const char * modestr)5457 rb_io_modestr_fmode(const char *modestr)
5458 {
5459 int fmode = 0;
5460 const char *m = modestr, *p = NULL;
5461
5462 switch (*m++) {
5463 case 'r':
5464 fmode |= FMODE_READABLE;
5465 break;
5466 case 'w':
5467 fmode |= FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE;
5468 break;
5469 case 'a':
5470 fmode |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
5471 break;
5472 default:
5473 error:
5474 rb_raise(rb_eArgError, "invalid access mode %s", modestr);
5475 }
5476
5477 while (*m) {
5478 switch (*m++) {
5479 case 'b':
5480 fmode |= FMODE_BINMODE;
5481 break;
5482 case 't':
5483 fmode |= FMODE_TEXTMODE;
5484 break;
5485 case '+':
5486 fmode |= FMODE_READWRITE;
5487 break;
5488 case 'x':
5489 if (modestr[0] != 'w')
5490 goto error;
5491 fmode |= FMODE_EXCL;
5492 break;
5493 default:
5494 goto error;
5495 case ':':
5496 p = strchr(m, ':');
5497 if (io_encname_bom_p(m, p ? (long)(p - m) : (long)strlen(m)))
5498 fmode |= FMODE_SETENC_BY_BOM;
5499 goto finished;
5500 }
5501 }
5502
5503 finished:
5504 if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE))
5505 goto error;
5506
5507 return fmode;
5508 }
5509
5510 int
rb_io_oflags_fmode(int oflags)5511 rb_io_oflags_fmode(int oflags)
5512 {
5513 int fmode = 0;
5514
5515 switch (oflags & O_ACCMODE) {
5516 case O_RDONLY:
5517 fmode = FMODE_READABLE;
5518 break;
5519 case O_WRONLY:
5520 fmode = FMODE_WRITABLE;
5521 break;
5522 case O_RDWR:
5523 fmode = FMODE_READWRITE;
5524 break;
5525 }
5526
5527 if (oflags & O_APPEND) {
5528 fmode |= FMODE_APPEND;
5529 }
5530 if (oflags & O_TRUNC) {
5531 fmode |= FMODE_TRUNC;
5532 }
5533 if (oflags & O_CREAT) {
5534 fmode |= FMODE_CREATE;
5535 }
5536 if (oflags & O_EXCL) {
5537 fmode |= FMODE_EXCL;
5538 }
5539 #ifdef O_BINARY
5540 if (oflags & O_BINARY) {
5541 fmode |= FMODE_BINMODE;
5542 }
5543 #endif
5544
5545 return fmode;
5546 }
5547
5548 static int
rb_io_fmode_oflags(int fmode)5549 rb_io_fmode_oflags(int fmode)
5550 {
5551 int oflags = 0;
5552
5553 switch (fmode & FMODE_READWRITE) {
5554 case FMODE_READABLE:
5555 oflags |= O_RDONLY;
5556 break;
5557 case FMODE_WRITABLE:
5558 oflags |= O_WRONLY;
5559 break;
5560 case FMODE_READWRITE:
5561 oflags |= O_RDWR;
5562 break;
5563 }
5564
5565 if (fmode & FMODE_APPEND) {
5566 oflags |= O_APPEND;
5567 }
5568 if (fmode & FMODE_TRUNC) {
5569 oflags |= O_TRUNC;
5570 }
5571 if (fmode & FMODE_CREATE) {
5572 oflags |= O_CREAT;
5573 }
5574 if (fmode & FMODE_EXCL) {
5575 oflags |= O_EXCL;
5576 }
5577 #ifdef O_BINARY
5578 if (fmode & FMODE_BINMODE) {
5579 oflags |= O_BINARY;
5580 }
5581 #endif
5582
5583 return oflags;
5584 }
5585
5586 int
rb_io_modestr_oflags(const char * modestr)5587 rb_io_modestr_oflags(const char *modestr)
5588 {
5589 return rb_io_fmode_oflags(rb_io_modestr_fmode(modestr));
5590 }
5591
5592 static const char*
rb_io_oflags_modestr(int oflags)5593 rb_io_oflags_modestr(int oflags)
5594 {
5595 #ifdef O_BINARY
5596 # define MODE_BINARY(a,b) ((oflags & O_BINARY) ? (b) : (a))
5597 #else
5598 # define MODE_BINARY(a,b) (a)
5599 #endif
5600 int accmode;
5601 if (oflags & O_EXCL) {
5602 rb_raise(rb_eArgError, "exclusive access mode is not supported");
5603 }
5604 accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
5605 if (oflags & O_APPEND) {
5606 if (accmode == O_WRONLY) {
5607 return MODE_BINARY("a", "ab");
5608 }
5609 if (accmode == O_RDWR) {
5610 return MODE_BINARY("a+", "ab+");
5611 }
5612 }
5613 switch (accmode) {
5614 default:
5615 rb_raise(rb_eArgError, "invalid access oflags 0x%x", oflags);
5616 case O_RDONLY:
5617 return MODE_BINARY("r", "rb");
5618 case O_WRONLY:
5619 return MODE_BINARY("w", "wb");
5620 case O_RDWR:
5621 if (oflags & O_TRUNC) {
5622 return MODE_BINARY("w+", "wb+");
5623 }
5624 return MODE_BINARY("r+", "rb+");
5625 }
5626 }
5627
5628 /*
5629 * Convert external/internal encodings to enc/enc2
5630 * NULL => use default encoding
5631 * Qnil => no encoding specified (internal only)
5632 */
5633 static void
rb_io_ext_int_to_encs(rb_encoding * ext,rb_encoding * intern,rb_encoding ** enc,rb_encoding ** enc2,int fmode)5634 rb_io_ext_int_to_encs(rb_encoding *ext, rb_encoding *intern, rb_encoding **enc, rb_encoding **enc2, int fmode)
5635 {
5636 int default_ext = 0;
5637
5638 if (ext == NULL) {
5639 ext = rb_default_external_encoding();
5640 default_ext = 1;
5641 }
5642 if (ext == rb_ascii8bit_encoding()) {
5643 /* If external is ASCII-8BIT, no transcoding */
5644 intern = NULL;
5645 }
5646 else if (intern == NULL) {
5647 intern = rb_default_internal_encoding();
5648 }
5649 if (intern == NULL || intern == (rb_encoding *)Qnil ||
5650 (!(fmode & FMODE_SETENC_BY_BOM) && (intern == ext))) {
5651 /* No internal encoding => use external + no transcoding */
5652 *enc = (default_ext && intern != ext) ? NULL : ext;
5653 *enc2 = NULL;
5654 }
5655 else {
5656 *enc = intern;
5657 *enc2 = ext;
5658 }
5659 }
5660
5661 static void
unsupported_encoding(const char * name,rb_encoding * enc)5662 unsupported_encoding(const char *name, rb_encoding *enc)
5663 {
5664 rb_enc_warn(enc, "Unsupported encoding %s ignored", name);
5665 }
5666
5667 static void
parse_mode_enc(const char * estr,rb_encoding * estr_enc,rb_encoding ** enc_p,rb_encoding ** enc2_p,int * fmode_p)5668 parse_mode_enc(const char *estr, rb_encoding *estr_enc,
5669 rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
5670 {
5671 const char *p;
5672 char encname[ENCODING_MAXNAMELEN+1];
5673 int idx, idx2;
5674 int fmode = fmode_p ? *fmode_p : 0;
5675 rb_encoding *ext_enc, *int_enc;
5676 long len;
5677
5678 /* parse estr as "enc" or "enc2:enc" or "enc:-" */
5679
5680 p = strrchr(estr, ':');
5681 len = p ? (p++ - estr) : (long)strlen(estr);
5682 if ((fmode & FMODE_SETENC_BY_BOM) || io_encname_bom_p(estr, len)) {
5683 estr += bom_prefix_len;
5684 len -= bom_prefix_len;
5685 if (!STRNCASECMP(estr, utf_prefix, utf_prefix_len)) {
5686 fmode |= FMODE_SETENC_BY_BOM;
5687 }
5688 else {
5689 rb_enc_warn(estr_enc, "BOM with non-UTF encoding %s is nonsense", estr);
5690 fmode &= ~FMODE_SETENC_BY_BOM;
5691 }
5692 }
5693 if (len == 0 || len > ENCODING_MAXNAMELEN) {
5694 idx = -1;
5695 }
5696 else {
5697 if (p) {
5698 memcpy(encname, estr, len);
5699 encname[len] = '\0';
5700 estr = encname;
5701 }
5702 idx = rb_enc_find_index(estr);
5703 }
5704 if (fmode_p) *fmode_p = fmode;
5705
5706 if (idx >= 0)
5707 ext_enc = rb_enc_from_index(idx);
5708 else {
5709 if (idx != -2)
5710 unsupported_encoding(estr, estr_enc);
5711 ext_enc = NULL;
5712 }
5713
5714 int_enc = NULL;
5715 if (p) {
5716 if (*p == '-' && *(p+1) == '\0') {
5717 /* Special case - "-" => no transcoding */
5718 int_enc = (rb_encoding *)Qnil;
5719 }
5720 else {
5721 idx2 = rb_enc_find_index(p);
5722 if (idx2 < 0)
5723 unsupported_encoding(p, estr_enc);
5724 else if (!(fmode & FMODE_SETENC_BY_BOM) && (idx2 == idx)) {
5725 int_enc = (rb_encoding *)Qnil;
5726 }
5727 else
5728 int_enc = rb_enc_from_index(idx2);
5729 }
5730 }
5731
5732 rb_io_ext_int_to_encs(ext_enc, int_enc, enc_p, enc2_p, fmode);
5733 }
5734
5735 int
rb_io_extract_encoding_option(VALUE opt,rb_encoding ** enc_p,rb_encoding ** enc2_p,int * fmode_p)5736 rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
5737 {
5738 VALUE encoding=Qnil, extenc=Qundef, intenc=Qundef, tmp;
5739 int extracted = 0;
5740 rb_encoding *extencoding = NULL;
5741 rb_encoding *intencoding = NULL;
5742
5743 if (!NIL_P(opt)) {
5744 VALUE v;
5745 v = rb_hash_lookup2(opt, sym_encoding, Qnil);
5746 if (v != Qnil) encoding = v;
5747 v = rb_hash_lookup2(opt, sym_extenc, Qundef);
5748 if (v != Qnil) extenc = v;
5749 v = rb_hash_lookup2(opt, sym_intenc, Qundef);
5750 if (v != Qundef) intenc = v;
5751 }
5752 if ((extenc != Qundef || intenc != Qundef) && !NIL_P(encoding)) {
5753 if (!NIL_P(ruby_verbose)) {
5754 int idx = rb_to_encoding_index(encoding);
5755 if (idx >= 0) encoding = rb_enc_from_encoding(rb_enc_from_index(idx));
5756 rb_warn("Ignoring encoding parameter '%"PRIsVALUE"': %s_encoding is used",
5757 encoding, extenc == Qundef ? "internal" : "external");
5758 }
5759 encoding = Qnil;
5760 }
5761 if (extenc != Qundef && !NIL_P(extenc)) {
5762 extencoding = rb_to_encoding(extenc);
5763 }
5764 if (intenc != Qundef) {
5765 if (NIL_P(intenc)) {
5766 /* internal_encoding: nil => no transcoding */
5767 intencoding = (rb_encoding *)Qnil;
5768 }
5769 else if (!NIL_P(tmp = rb_check_string_type(intenc))) {
5770 char *p = StringValueCStr(tmp);
5771
5772 if (*p == '-' && *(p+1) == '\0') {
5773 /* Special case - "-" => no transcoding */
5774 intencoding = (rb_encoding *)Qnil;
5775 }
5776 else {
5777 intencoding = rb_to_encoding(intenc);
5778 }
5779 }
5780 else {
5781 intencoding = rb_to_encoding(intenc);
5782 }
5783 if (extencoding == intencoding) {
5784 intencoding = (rb_encoding *)Qnil;
5785 }
5786 }
5787 if (!NIL_P(encoding)) {
5788 extracted = 1;
5789 if (!NIL_P(tmp = rb_check_string_type(encoding))) {
5790 parse_mode_enc(StringValueCStr(tmp), rb_enc_get(tmp),
5791 enc_p, enc2_p, fmode_p);
5792 }
5793 else {
5794 rb_io_ext_int_to_encs(rb_to_encoding(encoding), NULL, enc_p, enc2_p, 0);
5795 }
5796 }
5797 else if (extenc != Qundef || intenc != Qundef) {
5798 extracted = 1;
5799 rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p, 0);
5800 }
5801 return extracted;
5802 }
5803
5804 typedef struct rb_io_enc_t convconfig_t;
5805
5806 static void
validate_enc_binmode(int * fmode_p,int ecflags,rb_encoding * enc,rb_encoding * enc2)5807 validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *enc2)
5808 {
5809 int fmode = *fmode_p;
5810
5811 if ((fmode & FMODE_READABLE) &&
5812 !enc2 &&
5813 !(fmode & FMODE_BINMODE) &&
5814 !rb_enc_asciicompat(enc ? enc : rb_default_external_encoding()))
5815 rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode");
5816
5817 if ((fmode & FMODE_BINMODE) && (ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
5818 rb_raise(rb_eArgError, "newline decorator with binary mode");
5819 }
5820 if (!(fmode & FMODE_BINMODE) &&
5821 (DEFAULT_TEXTMODE || (ecflags & ECONV_NEWLINE_DECORATOR_MASK))) {
5822 fmode |= FMODE_TEXTMODE;
5823 *fmode_p = fmode;
5824 }
5825 #if !DEFAULT_TEXTMODE
5826 else if (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
5827 fmode &= ~FMODE_TEXTMODE;
5828 *fmode_p = fmode;
5829 }
5830 #endif
5831 }
5832
5833 static void
extract_binmode(VALUE opthash,int * fmode)5834 extract_binmode(VALUE opthash, int *fmode)
5835 {
5836 if (!NIL_P(opthash)) {
5837 VALUE v;
5838 v = rb_hash_aref(opthash, sym_textmode);
5839 if (!NIL_P(v)) {
5840 if (*fmode & FMODE_TEXTMODE)
5841 rb_raise(rb_eArgError, "textmode specified twice");
5842 if (*fmode & FMODE_BINMODE)
5843 rb_raise(rb_eArgError, "both textmode and binmode specified");
5844 if (RTEST(v))
5845 *fmode |= FMODE_TEXTMODE;
5846 }
5847 v = rb_hash_aref(opthash, sym_binmode);
5848 if (!NIL_P(v)) {
5849 if (*fmode & FMODE_BINMODE)
5850 rb_raise(rb_eArgError, "binmode specified twice");
5851 if (*fmode & FMODE_TEXTMODE)
5852 rb_raise(rb_eArgError, "both textmode and binmode specified");
5853 if (RTEST(v))
5854 *fmode |= FMODE_BINMODE;
5855 }
5856
5857 if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE))
5858 rb_raise(rb_eArgError, "both textmode and binmode specified");
5859 }
5860 }
5861
5862 static void
rb_io_extract_modeenc(VALUE * vmode_p,VALUE * vperm_p,VALUE opthash,int * oflags_p,int * fmode_p,convconfig_t * convconfig_p)5863 rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
5864 int *oflags_p, int *fmode_p, convconfig_t *convconfig_p)
5865 {
5866 VALUE vmode;
5867 int oflags, fmode;
5868 rb_encoding *enc, *enc2;
5869 int ecflags;
5870 VALUE ecopts;
5871 int has_enc = 0, has_vmode = 0;
5872 VALUE intmode;
5873
5874 vmode = *vmode_p;
5875
5876 /* Set to defaults */
5877 rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2, 0);
5878
5879 vmode_handle:
5880 if (NIL_P(vmode)) {
5881 fmode = FMODE_READABLE;
5882 oflags = O_RDONLY;
5883 }
5884 else if (!NIL_P(intmode = rb_check_to_integer(vmode, "to_int"))) {
5885 vmode = intmode;
5886 oflags = NUM2INT(intmode);
5887 fmode = rb_io_oflags_fmode(oflags);
5888 }
5889 else {
5890 const char *p;
5891
5892 SafeStringValue(vmode);
5893 p = StringValueCStr(vmode);
5894 fmode = rb_io_modestr_fmode(p);
5895 oflags = rb_io_fmode_oflags(fmode);
5896 p = strchr(p, ':');
5897 if (p) {
5898 has_enc = 1;
5899 parse_mode_enc(p+1, rb_enc_get(vmode), &enc, &enc2, &fmode);
5900 }
5901 else {
5902 rb_encoding *e;
5903
5904 e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
5905 rb_io_ext_int_to_encs(e, NULL, &enc, &enc2, fmode);
5906 }
5907 }
5908
5909 if (NIL_P(opthash)) {
5910 ecflags = (fmode & FMODE_READABLE) ?
5911 MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
5912 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
5913 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5914 ecflags |= (fmode & FMODE_WRITABLE) ?
5915 MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
5916 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5917 #endif
5918 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
5919 ecopts = Qnil;
5920 }
5921 else {
5922 VALUE v;
5923 if (!has_vmode) {
5924 v = rb_hash_aref(opthash, sym_mode);
5925 if (!NIL_P(v)) {
5926 if (!NIL_P(vmode)) {
5927 rb_raise(rb_eArgError, "mode specified twice");
5928 }
5929 has_vmode = 1;
5930 vmode = v;
5931 goto vmode_handle;
5932 }
5933 }
5934 v = rb_hash_aref(opthash, sym_flags);
5935 if (!NIL_P(v)) {
5936 v = rb_to_int(v);
5937 oflags |= NUM2INT(v);
5938 vmode = INT2NUM(oflags);
5939 fmode = rb_io_oflags_fmode(oflags);
5940 }
5941 extract_binmode(opthash, &fmode);
5942 if (fmode & FMODE_BINMODE) {
5943 #ifdef O_BINARY
5944 oflags |= O_BINARY;
5945 #endif
5946 if (!has_enc)
5947 rb_io_ext_int_to_encs(rb_ascii8bit_encoding(), NULL, &enc, &enc2, fmode);
5948 }
5949 #if DEFAULT_TEXTMODE
5950 else if (NIL_P(vmode)) {
5951 fmode |= DEFAULT_TEXTMODE;
5952 }
5953 #endif
5954 v = rb_hash_aref(opthash, sym_perm);
5955 if (!NIL_P(v)) {
5956 if (vperm_p) {
5957 if (!NIL_P(*vperm_p)) {
5958 rb_raise(rb_eArgError, "perm specified twice");
5959 }
5960 *vperm_p = v;
5961 }
5962 else {
5963 /* perm no use, just ignore */
5964 }
5965 }
5966 ecflags = (fmode & FMODE_READABLE) ?
5967 MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
5968 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
5969 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5970 ecflags |= (fmode & FMODE_WRITABLE) ?
5971 MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
5972 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5973 #endif
5974
5975 if (rb_io_extract_encoding_option(opthash, &enc, &enc2, &fmode)) {
5976 if (has_enc) {
5977 rb_raise(rb_eArgError, "encoding specified twice");
5978 }
5979 }
5980 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
5981 ecflags = rb_econv_prepare_options(opthash, &ecopts, ecflags);
5982 }
5983
5984 validate_enc_binmode(&fmode, ecflags, enc, enc2);
5985
5986 *vmode_p = vmode;
5987
5988 *oflags_p = oflags;
5989 *fmode_p = fmode;
5990 convconfig_p->enc = enc;
5991 convconfig_p->enc2 = enc2;
5992 convconfig_p->ecflags = ecflags;
5993 convconfig_p->ecopts = ecopts;
5994 }
5995
5996 struct sysopen_struct {
5997 VALUE fname;
5998 int oflags;
5999 mode_t perm;
6000 };
6001
6002 static void *
sysopen_func(void * ptr)6003 sysopen_func(void *ptr)
6004 {
6005 const struct sysopen_struct *data = ptr;
6006 const char *fname = RSTRING_PTR(data->fname);
6007 return (void *)(VALUE)rb_cloexec_open(fname, data->oflags, data->perm);
6008 }
6009
6010 static inline int
rb_sysopen_internal(struct sysopen_struct * data)6011 rb_sysopen_internal(struct sysopen_struct *data)
6012 {
6013 int fd;
6014 fd = (int)(VALUE)rb_thread_call_without_gvl(sysopen_func, data, RUBY_UBF_IO, 0);
6015 if (0 <= fd)
6016 rb_update_max_fd(fd);
6017 return fd;
6018 }
6019
6020 static int
rb_sysopen(VALUE fname,int oflags,mode_t perm)6021 rb_sysopen(VALUE fname, int oflags, mode_t perm)
6022 {
6023 int fd;
6024 struct sysopen_struct data;
6025
6026 data.fname = rb_str_encode_ospath(fname);
6027 StringValueCStr(data.fname);
6028 data.oflags = oflags;
6029 data.perm = perm;
6030
6031 fd = rb_sysopen_internal(&data);
6032 if (fd < 0) {
6033 int e = errno;
6034 if (rb_gc_for_fd(e)) {
6035 fd = rb_sysopen_internal(&data);
6036 }
6037 if (fd < 0) {
6038 rb_syserr_fail_path(e, fname);
6039 }
6040 }
6041 return fd;
6042 }
6043
6044 FILE *
rb_fdopen(int fd,const char * modestr)6045 rb_fdopen(int fd, const char *modestr)
6046 {
6047 FILE *file;
6048
6049 #if defined(__sun)
6050 errno = 0;
6051 #endif
6052 file = fdopen(fd, modestr);
6053 if (!file) {
6054 int e = errno;
6055 #if defined(__sun)
6056 if (e == 0) {
6057 rb_gc();
6058 errno = 0;
6059 file = fdopen(fd, modestr);
6060 }
6061 else
6062 #endif
6063 if (rb_gc_for_fd(e)) {
6064 file = fdopen(fd, modestr);
6065 }
6066 if (!file) {
6067 #ifdef _WIN32
6068 if (e == 0) e = EINVAL;
6069 #elif defined(__sun)
6070 if (e == 0) e = EMFILE;
6071 #endif
6072 rb_syserr_fail(e, 0);
6073 }
6074 }
6075
6076 /* xxx: should be _IONBF? A buffer in FILE may have trouble. */
6077 #ifdef USE_SETVBUF
6078 if (setvbuf(file, NULL, _IOFBF, 0) != 0)
6079 rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
6080 #endif
6081 return file;
6082 }
6083
6084 static int
io_check_tty(rb_io_t * fptr)6085 io_check_tty(rb_io_t *fptr)
6086 {
6087 int t = isatty(fptr->fd);
6088 if (t)
6089 fptr->mode |= FMODE_TTY|FMODE_DUPLEX;
6090 return t;
6091 }
6092
6093 static VALUE rb_io_internal_encoding(VALUE);
6094 static void io_encoding_set(rb_io_t *, VALUE, VALUE, VALUE);
6095
6096 static int
io_strip_bom(VALUE io)6097 io_strip_bom(VALUE io)
6098 {
6099 VALUE b1, b2, b3, b4;
6100 rb_io_t *fptr;
6101
6102 GetOpenFile(io, fptr);
6103 if (!(fptr->mode & FMODE_READABLE)) return 0;
6104 if (NIL_P(b1 = rb_io_getbyte(io))) return 0;
6105 switch (b1) {
6106 case INT2FIX(0xEF):
6107 if (NIL_P(b2 = rb_io_getbyte(io))) break;
6108 if (b2 == INT2FIX(0xBB) && !NIL_P(b3 = rb_io_getbyte(io))) {
6109 if (b3 == INT2FIX(0xBF)) {
6110 return rb_utf8_encindex();
6111 }
6112 rb_io_ungetbyte(io, b3);
6113 }
6114 rb_io_ungetbyte(io, b2);
6115 break;
6116
6117 case INT2FIX(0xFE):
6118 if (NIL_P(b2 = rb_io_getbyte(io))) break;
6119 if (b2 == INT2FIX(0xFF)) {
6120 return ENCINDEX_UTF_16BE;
6121 }
6122 rb_io_ungetbyte(io, b2);
6123 break;
6124
6125 case INT2FIX(0xFF):
6126 if (NIL_P(b2 = rb_io_getbyte(io))) break;
6127 if (b2 == INT2FIX(0xFE)) {
6128 b3 = rb_io_getbyte(io);
6129 if (b3 == INT2FIX(0) && !NIL_P(b4 = rb_io_getbyte(io))) {
6130 if (b4 == INT2FIX(0)) {
6131 return ENCINDEX_UTF_32LE;
6132 }
6133 rb_io_ungetbyte(io, b4);
6134 }
6135 rb_io_ungetbyte(io, b3);
6136 return ENCINDEX_UTF_16LE;
6137 }
6138 rb_io_ungetbyte(io, b2);
6139 break;
6140
6141 case INT2FIX(0):
6142 if (NIL_P(b2 = rb_io_getbyte(io))) break;
6143 if (b2 == INT2FIX(0) && !NIL_P(b3 = rb_io_getbyte(io))) {
6144 if (b3 == INT2FIX(0xFE) && !NIL_P(b4 = rb_io_getbyte(io))) {
6145 if (b4 == INT2FIX(0xFF)) {
6146 return ENCINDEX_UTF_32BE;
6147 }
6148 rb_io_ungetbyte(io, b4);
6149 }
6150 rb_io_ungetbyte(io, b3);
6151 }
6152 rb_io_ungetbyte(io, b2);
6153 break;
6154 }
6155 rb_io_ungetbyte(io, b1);
6156 return 0;
6157 }
6158
6159 static void
io_set_encoding_by_bom(VALUE io)6160 io_set_encoding_by_bom(VALUE io)
6161 {
6162 int idx = io_strip_bom(io);
6163 rb_io_t *fptr;
6164
6165 GetOpenFile(io, fptr);
6166 if (idx) {
6167 io_encoding_set(fptr, rb_enc_from_encoding(rb_enc_from_index(idx)),
6168 rb_io_internal_encoding(io), Qnil);
6169 }
6170 else {
6171 fptr->encs.enc2 = NULL;
6172 }
6173 }
6174
6175 static VALUE
rb_file_open_generic(VALUE io,VALUE filename,int oflags,int fmode,const convconfig_t * convconfig,mode_t perm)6176 rb_file_open_generic(VALUE io, VALUE filename, int oflags, int fmode,
6177 const convconfig_t *convconfig, mode_t perm)
6178 {
6179 VALUE pathv;
6180 rb_io_t *fptr;
6181 convconfig_t cc;
6182 if (!convconfig) {
6183 /* Set to default encodings */
6184 rb_io_ext_int_to_encs(NULL, NULL, &cc.enc, &cc.enc2, fmode);
6185 cc.ecflags = 0;
6186 cc.ecopts = Qnil;
6187 convconfig = &cc;
6188 }
6189 validate_enc_binmode(&fmode, convconfig->ecflags,
6190 convconfig->enc, convconfig->enc2);
6191
6192 MakeOpenFile(io, fptr);
6193 fptr->mode = fmode;
6194 fptr->encs = *convconfig;
6195 pathv = rb_str_new_frozen(filename);
6196 #ifdef O_TMPFILE
6197 if (!(oflags & O_TMPFILE)) {
6198 fptr->pathv = pathv;
6199 }
6200 #else
6201 fptr->pathv = pathv;
6202 #endif
6203 fptr->fd = rb_sysopen(pathv, oflags, perm);
6204 io_check_tty(fptr);
6205 if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
6206
6207 return io;
6208 }
6209
6210 static VALUE
rb_file_open_internal(VALUE io,VALUE filename,const char * modestr)6211 rb_file_open_internal(VALUE io, VALUE filename, const char *modestr)
6212 {
6213 int fmode = rb_io_modestr_fmode(modestr);
6214 const char *p = strchr(modestr, ':');
6215 convconfig_t convconfig;
6216
6217 if (p) {
6218 parse_mode_enc(p+1, rb_usascii_encoding(),
6219 &convconfig.enc, &convconfig.enc2, &fmode);
6220 }
6221 else {
6222 rb_encoding *e;
6223 /* Set to default encodings */
6224
6225 e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
6226 rb_io_ext_int_to_encs(e, NULL, &convconfig.enc, &convconfig.enc2, fmode);
6227 convconfig.ecflags = 0;
6228 convconfig.ecopts = Qnil;
6229 }
6230
6231 return rb_file_open_generic(io, filename,
6232 rb_io_fmode_oflags(fmode),
6233 fmode,
6234 &convconfig,
6235 0666);
6236 }
6237
6238 VALUE
rb_file_open_str(VALUE fname,const char * modestr)6239 rb_file_open_str(VALUE fname, const char *modestr)
6240 {
6241 FilePathValue(fname);
6242 return rb_file_open_internal(io_alloc(rb_cFile), fname, modestr);
6243 }
6244
6245 VALUE
rb_file_open(const char * fname,const char * modestr)6246 rb_file_open(const char *fname, const char *modestr)
6247 {
6248 return rb_file_open_internal(io_alloc(rb_cFile), rb_str_new_cstr(fname), modestr);
6249 }
6250
6251 #if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
6252 static struct pipe_list {
6253 rb_io_t *fptr;
6254 struct pipe_list *next;
6255 } *pipe_list;
6256
6257 static void
pipe_add_fptr(rb_io_t * fptr)6258 pipe_add_fptr(rb_io_t *fptr)
6259 {
6260 struct pipe_list *list;
6261
6262 list = ALLOC(struct pipe_list);
6263 list->fptr = fptr;
6264 list->next = pipe_list;
6265 pipe_list = list;
6266 }
6267
6268 static void
pipe_del_fptr(rb_io_t * fptr)6269 pipe_del_fptr(rb_io_t *fptr)
6270 {
6271 struct pipe_list **prev = &pipe_list;
6272 struct pipe_list *tmp;
6273
6274 while ((tmp = *prev) != 0) {
6275 if (tmp->fptr == fptr) {
6276 *prev = tmp->next;
6277 free(tmp);
6278 return;
6279 }
6280 prev = &tmp->next;
6281 }
6282 }
6283
6284 #if defined (_WIN32) || defined(__CYGWIN__)
6285 static void
pipe_atexit(void)6286 pipe_atexit(void)
6287 {
6288 struct pipe_list *list = pipe_list;
6289 struct pipe_list *tmp;
6290
6291 while (list) {
6292 tmp = list->next;
6293 rb_io_fptr_finalize(list->fptr);
6294 list = tmp;
6295 }
6296 }
6297 #endif
6298
6299 static void
pipe_finalize(rb_io_t * fptr,int noraise)6300 pipe_finalize(rb_io_t *fptr, int noraise)
6301 {
6302 #if !defined(HAVE_WORKING_FORK) && !defined(_WIN32)
6303 int status = 0;
6304 if (fptr->stdio_file) {
6305 status = pclose(fptr->stdio_file);
6306 }
6307 fptr->fd = -1;
6308 fptr->stdio_file = 0;
6309 rb_last_status_set(status, fptr->pid);
6310 #else
6311 fptr_finalize(fptr, noraise);
6312 #endif
6313 pipe_del_fptr(fptr);
6314 }
6315 #endif
6316
6317 static void
fptr_copy_finalizer(rb_io_t * fptr,const rb_io_t * orig)6318 fptr_copy_finalizer(rb_io_t *fptr, const rb_io_t *orig)
6319 {
6320 #if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
6321 void (*const old_finalize)(struct rb_io_t*,int) = fptr->finalize;
6322
6323 if (old_finalize == orig->finalize) return;
6324 #endif
6325
6326 fptr->finalize = orig->finalize;
6327
6328 #if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
6329 if (old_finalize != pipe_finalize) {
6330 struct pipe_list *list;
6331 for (list = pipe_list; list; list = list->next) {
6332 if (list->fptr == fptr) break;
6333 }
6334 if (!list) pipe_add_fptr(fptr);
6335 }
6336 else {
6337 pipe_del_fptr(fptr);
6338 }
6339 #endif
6340 }
6341
6342 void
rb_io_synchronized(rb_io_t * fptr)6343 rb_io_synchronized(rb_io_t *fptr)
6344 {
6345 rb_io_check_initialized(fptr);
6346 fptr->mode |= FMODE_SYNC;
6347 }
6348
6349 void
rb_io_unbuffered(rb_io_t * fptr)6350 rb_io_unbuffered(rb_io_t *fptr)
6351 {
6352 rb_io_synchronized(fptr);
6353 }
6354
6355 int
rb_pipe(int * pipes)6356 rb_pipe(int *pipes)
6357 {
6358 int ret;
6359 ret = rb_cloexec_pipe(pipes);
6360 if (ret < 0) {
6361 if (rb_gc_for_fd(errno)) {
6362 ret = rb_cloexec_pipe(pipes);
6363 }
6364 }
6365 if (ret == 0) {
6366 rb_update_max_fd(pipes[0]);
6367 rb_update_max_fd(pipes[1]);
6368 }
6369 return ret;
6370 }
6371
6372 #ifdef _WIN32
6373 #define HAVE_SPAWNV 1
6374 #define spawnv(mode, cmd, args) rb_w32_uaspawn((mode), (cmd), (args))
6375 #define spawn(mode, cmd) rb_w32_uspawn((mode), (cmd), 0)
6376 #endif
6377
6378 #if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
6379 struct popen_arg {
6380 VALUE execarg_obj;
6381 struct rb_execarg *eargp;
6382 int modef;
6383 int pair[2];
6384 int write_pair[2];
6385 };
6386 #endif
6387
6388 #ifdef HAVE_WORKING_FORK
6389 static void
popen_redirect(struct popen_arg * p)6390 popen_redirect(struct popen_arg *p)
6391 {
6392 if ((p->modef & FMODE_READABLE) && (p->modef & FMODE_WRITABLE)) {
6393 close(p->write_pair[1]);
6394 if (p->write_pair[0] != 0) {
6395 dup2(p->write_pair[0], 0);
6396 close(p->write_pair[0]);
6397 }
6398 close(p->pair[0]);
6399 if (p->pair[1] != 1) {
6400 dup2(p->pair[1], 1);
6401 close(p->pair[1]);
6402 }
6403 }
6404 else if (p->modef & FMODE_READABLE) {
6405 close(p->pair[0]);
6406 if (p->pair[1] != 1) {
6407 dup2(p->pair[1], 1);
6408 close(p->pair[1]);
6409 }
6410 }
6411 else {
6412 close(p->pair[1]);
6413 if (p->pair[0] != 0) {
6414 dup2(p->pair[0], 0);
6415 close(p->pair[0]);
6416 }
6417 }
6418 }
6419
6420 #if defined(__linux__)
6421 /* Linux /proc/self/status contains a line: "FDSize:\t<nnn>\n"
6422 * Since /proc may not be available, linux_get_maxfd is just a hint.
6423 * This function, linux_get_maxfd, must be async-signal-safe.
6424 * I.e. opendir() is not usable.
6425 *
6426 * Note that memchr() and memcmp is *not* async-signal-safe in POSIX.
6427 * However they are easy to re-implement in async-signal-safe manner.
6428 * (Also note that there is missing/memcmp.c.)
6429 */
6430 static int
linux_get_maxfd(void)6431 linux_get_maxfd(void)
6432 {
6433 int fd;
6434 char buf[4096], *p, *np, *e;
6435 ssize_t ss;
6436 fd = rb_cloexec_open("/proc/self/status", O_RDONLY|O_NOCTTY, 0);
6437 if (fd < 0) return fd;
6438 ss = read(fd, buf, sizeof(buf));
6439 if (ss < 0) goto err;
6440 p = buf;
6441 e = buf + ss;
6442 while ((int)sizeof("FDSize:\t0\n")-1 <= e-p &&
6443 (np = memchr(p, '\n', e-p)) != NULL) {
6444 if (memcmp(p, "FDSize:", sizeof("FDSize:")-1) == 0) {
6445 int fdsize;
6446 p += sizeof("FDSize:")-1;
6447 *np = '\0';
6448 fdsize = (int)ruby_strtoul(p, (char **)NULL, 10);
6449 close(fd);
6450 return fdsize;
6451 }
6452 p = np+1;
6453 }
6454 /* fall through */
6455
6456 err:
6457 close(fd);
6458 return (int)ss;
6459 }
6460 #endif
6461
6462 /* This function should be async-signal-safe. */
6463 void
rb_close_before_exec(int lowfd,int maxhint,VALUE noclose_fds)6464 rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
6465 {
6466 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
6467 int fd, ret;
6468 int max = (int)max_file_descriptor;
6469 # ifdef F_MAXFD
6470 /* F_MAXFD is available since NetBSD 2.0. */
6471 ret = fcntl(0, F_MAXFD); /* async-signal-safe */
6472 if (ret != -1)
6473 maxhint = max = ret;
6474 # elif defined(__linux__)
6475 ret = linux_get_maxfd();
6476 if (maxhint < ret)
6477 maxhint = ret;
6478 /* maxhint = max = ret; if (ret == -1) abort(); // test */
6479 # endif
6480 if (max < maxhint)
6481 max = maxhint;
6482 for (fd = lowfd; fd <= max; fd++) {
6483 if (!NIL_P(noclose_fds) &&
6484 RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd)))) /* async-signal-safe */
6485 continue;
6486 ret = fcntl(fd, F_GETFD); /* async-signal-safe */
6487 if (ret != -1 && !(ret & FD_CLOEXEC)) {
6488 fcntl(fd, F_SETFD, ret|FD_CLOEXEC); /* async-signal-safe */
6489 }
6490 # define CONTIGUOUS_CLOSED_FDS 20
6491 if (ret != -1) {
6492 if (max < fd + CONTIGUOUS_CLOSED_FDS)
6493 max = fd + CONTIGUOUS_CLOSED_FDS;
6494 }
6495 }
6496 #endif
6497 }
6498
6499 static int
popen_exec(void * pp,char * errmsg,size_t errmsg_len)6500 popen_exec(void *pp, char *errmsg, size_t errmsg_len)
6501 {
6502 struct popen_arg *p = (struct popen_arg*)pp;
6503
6504 return rb_exec_async_signal_safe(p->eargp, errmsg, errmsg_len);
6505 }
6506 #endif
6507
6508 #if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
6509 static VALUE
rb_execarg_fixup_v(VALUE execarg_obj)6510 rb_execarg_fixup_v(VALUE execarg_obj)
6511 {
6512 rb_execarg_parent_start(execarg_obj);
6513 return Qnil;
6514 }
6515 #else
6516 char *rb_execarg_commandline(const struct rb_execarg *eargp, VALUE *prog);
6517 #endif
6518
6519 static VALUE
pipe_open(VALUE execarg_obj,const char * modestr,int fmode,const convconfig_t * convconfig)6520 pipe_open(VALUE execarg_obj, const char *modestr, int fmode,
6521 const convconfig_t *convconfig)
6522 {
6523 struct rb_execarg *eargp = NIL_P(execarg_obj) ? NULL : rb_execarg_get(execarg_obj);
6524 VALUE prog = eargp ? (eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name) : Qfalse ;
6525 rb_pid_t pid = 0;
6526 rb_io_t *fptr;
6527 VALUE port;
6528 rb_io_t *write_fptr;
6529 VALUE write_port;
6530 #if defined(HAVE_WORKING_FORK)
6531 int status;
6532 char errmsg[80] = { '\0' };
6533 #endif
6534 #if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
6535 int state;
6536 struct popen_arg arg;
6537 #endif
6538 int e = 0;
6539 #if defined(HAVE_SPAWNV)
6540 # if defined(HAVE_SPAWNVE)
6541 # define DO_SPAWN(cmd, args, envp) ((args) ? \
6542 spawnve(P_NOWAIT, (cmd), (args), (envp)) : \
6543 spawne(P_NOWAIT, (cmd), (envp)))
6544 # else
6545 # define DO_SPAWN(cmd, args, envp) ((args) ? \
6546 spawnv(P_NOWAIT, (cmd), (args)) : \
6547 spawn(P_NOWAIT, (cmd)))
6548 # endif
6549 # if !defined(HAVE_WORKING_FORK)
6550 char **args = NULL;
6551 # if defined(HAVE_SPAWNVE)
6552 char **envp = NULL;
6553 # endif
6554 # endif
6555 #endif
6556 #if !defined(HAVE_WORKING_FORK)
6557 struct rb_execarg sarg, *sargp = &sarg;
6558 #endif
6559 FILE *fp = 0;
6560 int fd = -1;
6561 int write_fd = -1;
6562 #if !defined(HAVE_WORKING_FORK)
6563 const char *cmd = 0;
6564
6565 if (prog)
6566 cmd = StringValueCStr(prog);
6567 #endif
6568
6569 #if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
6570 arg.execarg_obj = execarg_obj;
6571 arg.eargp = eargp;
6572 arg.modef = fmode;
6573 arg.pair[0] = arg.pair[1] = -1;
6574 arg.write_pair[0] = arg.write_pair[1] = -1;
6575 # if !defined(HAVE_WORKING_FORK)
6576 if (eargp && !eargp->use_shell) {
6577 args = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
6578 }
6579 # endif
6580 switch (fmode & (FMODE_READABLE|FMODE_WRITABLE)) {
6581 case FMODE_READABLE|FMODE_WRITABLE:
6582 if (rb_pipe(arg.write_pair) < 0)
6583 rb_sys_fail_str(prog);
6584 if (rb_pipe(arg.pair) < 0) {
6585 e = errno;
6586 close(arg.write_pair[0]);
6587 close(arg.write_pair[1]);
6588 rb_syserr_fail_str(e, prog);
6589 }
6590 if (eargp) {
6591 rb_execarg_addopt(execarg_obj, INT2FIX(0), INT2FIX(arg.write_pair[0]));
6592 rb_execarg_addopt(execarg_obj, INT2FIX(1), INT2FIX(arg.pair[1]));
6593 }
6594 break;
6595 case FMODE_READABLE:
6596 if (rb_pipe(arg.pair) < 0)
6597 rb_sys_fail_str(prog);
6598 if (eargp)
6599 rb_execarg_addopt(execarg_obj, INT2FIX(1), INT2FIX(arg.pair[1]));
6600 break;
6601 case FMODE_WRITABLE:
6602 if (rb_pipe(arg.pair) < 0)
6603 rb_sys_fail_str(prog);
6604 if (eargp)
6605 rb_execarg_addopt(execarg_obj, INT2FIX(0), INT2FIX(arg.pair[0]));
6606 break;
6607 default:
6608 rb_sys_fail_str(prog);
6609 }
6610 if (!NIL_P(execarg_obj)) {
6611 rb_protect(rb_execarg_fixup_v, execarg_obj, &state);
6612 if (state) {
6613 if (0 <= arg.write_pair[0]) close(arg.write_pair[0]);
6614 if (0 <= arg.write_pair[1]) close(arg.write_pair[1]);
6615 if (0 <= arg.pair[0]) close(arg.pair[0]);
6616 if (0 <= arg.pair[1]) close(arg.pair[1]);
6617 rb_execarg_parent_end(execarg_obj);
6618 rb_jump_tag(state);
6619 }
6620
6621 # if defined(HAVE_WORKING_FORK)
6622 pid = rb_fork_async_signal_safe(&status, popen_exec, &arg, arg.eargp->redirect_fds, errmsg, sizeof(errmsg));
6623 # else
6624 rb_execarg_run_options(eargp, sargp, NULL, 0);
6625 # if defined(HAVE_SPAWNVE)
6626 if (eargp->envp_str) envp = (char **)RSTRING_PTR(eargp->envp_str);
6627 # endif
6628 while ((pid = DO_SPAWN(cmd, args, envp)) < 0) {
6629 /* exec failed */
6630 switch (e = errno) {
6631 case EAGAIN:
6632 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6633 case EWOULDBLOCK:
6634 # endif
6635 rb_thread_sleep(1);
6636 continue;
6637 }
6638 break;
6639 }
6640 if (eargp)
6641 rb_execarg_run_options(sargp, NULL, NULL, 0);
6642 # endif
6643 rb_execarg_parent_end(execarg_obj);
6644 }
6645 else {
6646 # if defined(HAVE_WORKING_FORK)
6647 pid = rb_fork_ruby(&status);
6648 if (pid == 0) { /* child */
6649 rb_thread_atfork();
6650 popen_redirect(&arg);
6651 rb_io_synchronized(RFILE(orig_stdout)->fptr);
6652 rb_io_synchronized(RFILE(orig_stderr)->fptr);
6653 return Qnil;
6654 }
6655 # else
6656 rb_notimplement();
6657 # endif
6658 }
6659
6660 /* parent */
6661 if (pid < 0) {
6662 # if defined(HAVE_WORKING_FORK)
6663 e = errno;
6664 # endif
6665 close(arg.pair[0]);
6666 close(arg.pair[1]);
6667 if ((fmode & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
6668 close(arg.write_pair[0]);
6669 close(arg.write_pair[1]);
6670 }
6671 # if defined(HAVE_WORKING_FORK)
6672 if (errmsg[0])
6673 rb_syserr_fail(e, errmsg);
6674 # endif
6675 rb_syserr_fail_str(e, prog);
6676 }
6677 if ((fmode & FMODE_READABLE) && (fmode & FMODE_WRITABLE)) {
6678 close(arg.pair[1]);
6679 fd = arg.pair[0];
6680 close(arg.write_pair[0]);
6681 write_fd = arg.write_pair[1];
6682 }
6683 else if (fmode & FMODE_READABLE) {
6684 close(arg.pair[1]);
6685 fd = arg.pair[0];
6686 }
6687 else {
6688 close(arg.pair[0]);
6689 fd = arg.pair[1];
6690 }
6691 #else
6692 cmd = rb_execarg_commandline(eargp, &prog);
6693 if (!NIL_P(execarg_obj)) {
6694 rb_execarg_parent_start(execarg_obj);
6695 rb_execarg_run_options(eargp, sargp, NULL, 0);
6696 }
6697 fp = popen(cmd, modestr);
6698 e = errno;
6699 if (eargp) {
6700 rb_execarg_parent_end(execarg_obj);
6701 rb_execarg_run_options(sargp, NULL, NULL, 0);
6702 }
6703 if (!fp) rb_syserr_fail_path(e, prog);
6704 fd = fileno(fp);
6705 #endif
6706
6707 port = io_alloc(rb_cIO);
6708 MakeOpenFile(port, fptr);
6709 fptr->fd = fd;
6710 fptr->stdio_file = fp;
6711 fptr->mode = fmode | FMODE_SYNC|FMODE_DUPLEX;
6712 if (convconfig) {
6713 fptr->encs = *convconfig;
6714 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
6715 if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
6716 fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
6717 }
6718 #endif
6719 }
6720 else {
6721 if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
6722 fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
6723 }
6724 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
6725 if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr)) {
6726 fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
6727 }
6728 #endif
6729 }
6730 fptr->pid = pid;
6731
6732 if (0 <= write_fd) {
6733 write_port = io_alloc(rb_cIO);
6734 MakeOpenFile(write_port, write_fptr);
6735 write_fptr->fd = write_fd;
6736 write_fptr->mode = (fmode & ~FMODE_READABLE)| FMODE_SYNC|FMODE_DUPLEX;
6737 fptr->mode &= ~FMODE_WRITABLE;
6738 fptr->tied_io_for_writing = write_port;
6739 rb_ivar_set(port, rb_intern("@tied_io_for_writing"), write_port);
6740 }
6741
6742 #if defined (__CYGWIN__) || !defined(HAVE_WORKING_FORK)
6743 fptr->finalize = pipe_finalize;
6744 pipe_add_fptr(fptr);
6745 #endif
6746 return port;
6747 }
6748
6749 static int
is_popen_fork(VALUE prog)6750 is_popen_fork(VALUE prog)
6751 {
6752 if (RSTRING_LEN(prog) == 1 && RSTRING_PTR(prog)[0] == '-') {
6753 #if !defined(HAVE_WORKING_FORK)
6754 rb_raise(rb_eNotImpError,
6755 "fork() function is unimplemented on this machine");
6756 #else
6757 return TRUE;
6758 #endif
6759 }
6760 return FALSE;
6761 }
6762
6763 static VALUE
pipe_open_s(VALUE prog,const char * modestr,int fmode,const convconfig_t * convconfig)6764 pipe_open_s(VALUE prog, const char *modestr, int fmode,
6765 const convconfig_t *convconfig)
6766 {
6767 int argc = 1;
6768 VALUE *argv = &prog;
6769 VALUE execarg_obj = Qnil;
6770
6771 if (!is_popen_fork(prog))
6772 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
6773 return pipe_open(execarg_obj, modestr, fmode, convconfig);
6774 }
6775
6776 static VALUE
pipe_close(VALUE io)6777 pipe_close(VALUE io)
6778 {
6779 rb_io_t *fptr = io_close_fptr(io);
6780 if (fptr) {
6781 fptr_waitpid(fptr, rb_thread_to_be_killed(rb_thread_current()));
6782 }
6783 return Qnil;
6784 }
6785
6786 /*
6787 * call-seq:
6788 * IO.popen([env,] cmd, mode="r" [, opt]) -> io
6789 * IO.popen([env,] cmd, mode="r" [, opt]) {|io| block } -> obj
6790 *
6791 * Runs the specified command as a subprocess; the subprocess's
6792 * standard input and output will be connected to the returned
6793 * <code>IO</code> object.
6794 *
6795 * The PID of the started process can be obtained by IO#pid method.
6796 *
6797 * _cmd_ is a string or an array as follows.
6798 *
6799 * cmd:
6800 * "-" : fork
6801 * commandline : command line string which is passed to a shell
6802 * [env, cmdname, arg1, ..., opts] : command name and zero or more arguments (no shell)
6803 * [env, [cmdname, argv0], arg1, ..., opts] : command name, argv[0] and zero or more arguments (no shell)
6804 * (env and opts are optional.)
6805 *
6806 * If _cmd_ is a +String+ ``<code>-</code>'',
6807 * then a new instance of Ruby is started as the subprocess.
6808 *
6809 * If <i>cmd</i> is an +Array+ of +String+,
6810 * then it will be used as the subprocess's +argv+ bypassing a shell.
6811 * The array can contain a hash at first for environments and
6812 * a hash at last for options similar to <code>spawn</code>.
6813 *
6814 * The default mode for the new file object is ``r'',
6815 * but <i>mode</i> may be set to any of the modes listed in the description for class IO.
6816 * The last argument <i>opt</i> qualifies <i>mode</i>.
6817 *
6818 * # set IO encoding
6819 * IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
6820 * euc_jp_string = nkf_io.read
6821 * }
6822 *
6823 * # merge standard output and standard error using
6824 * # spawn option. See the document of Kernel.spawn.
6825 * IO.popen(["ls", "/", :err=>[:child, :out]]) {|ls_io|
6826 * ls_result_with_error = ls_io.read
6827 * }
6828 *
6829 * # spawn options can be mixed with IO options
6830 * IO.popen(["ls", "/"], :err=>[:child, :out]) {|ls_io|
6831 * ls_result_with_error = ls_io.read
6832 * }
6833 *
6834 * Raises exceptions which <code>IO.pipe</code> and
6835 * <code>Kernel.spawn</code> raise.
6836 *
6837 * If a block is given, Ruby will run the command as a child connected
6838 * to Ruby with a pipe. Ruby's end of the pipe will be passed as a
6839 * parameter to the block.
6840 * At the end of block, Ruby closes the pipe and sets <code>$?</code>.
6841 * In this case <code>IO.popen</code> returns
6842 * the value of the block.
6843 *
6844 * If a block is given with a _cmd_ of ``<code>-</code>'',
6845 * the block will be run in two separate processes: once in the parent,
6846 * and once in a child. The parent process will be passed the pipe
6847 * object as a parameter to the block, the child version of the block
6848 * will be passed +nil+, and the child's standard in and
6849 * standard out will be connected to the parent through the pipe. Not
6850 * available on all platforms.
6851 *
6852 * f = IO.popen("uname")
6853 * p f.readlines
6854 * f.close
6855 * puts "Parent is #{Process.pid}"
6856 * IO.popen("date") {|f| puts f.gets }
6857 * IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
6858 * p $?
6859 * IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
6860 * f.puts "bar"; f.close_write; puts f.gets
6861 * }
6862 *
6863 * <em>produces:</em>
6864 *
6865 * ["Linux\n"]
6866 * Parent is 21346
6867 * Thu Jan 15 22:41:19 JST 2009
6868 * 21346 is here, f is #<IO:fd 3>
6869 * 21352 is here, f is nil
6870 * #<Process::Status: pid 21352 exit 0>
6871 * <foo>bar;zot;
6872 */
6873
6874 static VALUE
rb_io_s_popen(int argc,VALUE * argv,VALUE klass)6875 rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
6876 {
6877 const char *modestr;
6878 VALUE pname, pmode = Qnil, port, tmp, opt = Qnil, env = Qnil, execarg_obj = Qnil;
6879 int oflags, fmode;
6880 convconfig_t convconfig;
6881
6882 if (argc > 1 && !NIL_P(opt = rb_check_hash_type(argv[argc-1]))) --argc;
6883 if (argc > 1 && !NIL_P(env = rb_check_hash_type(argv[0]))) --argc, ++argv;
6884 switch (argc) {
6885 case 2:
6886 pmode = argv[1];
6887 case 1:
6888 pname = argv[0];
6889 break;
6890 default:
6891 {
6892 int ex = !NIL_P(opt);
6893 rb_error_arity(argc + ex, 1 + ex, 2 + ex);
6894 }
6895 }
6896
6897 tmp = rb_check_array_type(pname);
6898 if (!NIL_P(tmp)) {
6899 long len = RARRAY_LEN(tmp);
6900 #if SIZEOF_LONG > SIZEOF_INT
6901 if (len > INT_MAX) {
6902 rb_raise(rb_eArgError, "too many arguments");
6903 }
6904 #endif
6905 execarg_obj = rb_execarg_new((int)len, RARRAY_CONST_PTR(tmp), FALSE, FALSE);
6906 RB_GC_GUARD(tmp);
6907 }
6908 else {
6909 SafeStringValue(pname);
6910 execarg_obj = Qnil;
6911 if (!is_popen_fork(pname))
6912 execarg_obj = rb_execarg_new(1, &pname, TRUE, FALSE);
6913 }
6914 if (!NIL_P(execarg_obj)) {
6915 if (!NIL_P(opt))
6916 opt = rb_execarg_extract_options(execarg_obj, opt);
6917 if (!NIL_P(env))
6918 rb_execarg_setenv(execarg_obj, env);
6919 }
6920 rb_io_extract_modeenc(&pmode, 0, opt, &oflags, &fmode, &convconfig);
6921 modestr = rb_io_oflags_modestr(oflags);
6922
6923 port = pipe_open(execarg_obj, modestr, fmode, &convconfig);
6924 if (NIL_P(port)) {
6925 /* child */
6926 if (rb_block_given_p()) {
6927 rb_yield(Qnil);
6928 rb_io_flush(rb_stdout);
6929 rb_io_flush(rb_stderr);
6930 _exit(0);
6931 }
6932 return Qnil;
6933 }
6934 RBASIC_SET_CLASS(port, klass);
6935 if (rb_block_given_p()) {
6936 return rb_ensure(rb_yield, port, pipe_close, port);
6937 }
6938 return port;
6939 }
6940
6941 static void
rb_scan_open_args(int argc,const VALUE * argv,VALUE * fname_p,int * oflags_p,int * fmode_p,convconfig_t * convconfig_p,mode_t * perm_p)6942 rb_scan_open_args(int argc, const VALUE *argv,
6943 VALUE *fname_p, int *oflags_p, int *fmode_p,
6944 convconfig_t *convconfig_p, mode_t *perm_p)
6945 {
6946 VALUE opt, fname, vmode, vperm;
6947 int oflags, fmode;
6948 mode_t perm;
6949
6950 argc = rb_scan_args(argc, argv, "12:", &fname, &vmode, &vperm, &opt);
6951 FilePathValue(fname);
6952
6953 rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, convconfig_p);
6954
6955 perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
6956
6957 *fname_p = fname;
6958 *oflags_p = oflags;
6959 *fmode_p = fmode;
6960 *perm_p = perm;
6961 }
6962
6963 static VALUE
rb_open_file(int argc,const VALUE * argv,VALUE io)6964 rb_open_file(int argc, const VALUE *argv, VALUE io)
6965 {
6966 VALUE fname;
6967 int oflags, fmode;
6968 convconfig_t convconfig;
6969 mode_t perm;
6970
6971 rb_scan_open_args(argc, argv, &fname, &oflags, &fmode, &convconfig, &perm);
6972 rb_file_open_generic(io, fname, oflags, fmode, &convconfig, perm);
6973
6974 return io;
6975 }
6976
6977
6978 /*
6979 * Document-method: File::open
6980 *
6981 * call-seq:
6982 * File.open(filename, mode="r" [, opt]) -> file
6983 * File.open(filename [, mode [, perm]] [, opt]) -> file
6984 * File.open(filename, mode="r" [, opt]) {|file| block } -> obj
6985 * File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj
6986 *
6987 * With no associated block, <code>File.open</code> is a synonym for
6988 * File.new. If the optional code block is given, it will
6989 * be passed the opened +file+ as an argument and the File object will
6990 * automatically be closed when the block terminates. The value of the block
6991 * will be returned from <code>File.open</code>.
6992 *
6993 * If a file is being created, its initial permissions may be set using the
6994 * +perm+ parameter. See File.new for further discussion.
6995 *
6996 * See IO.new for a description of the +mode+ and +opt+ parameters.
6997 */
6998
6999 /*
7000 * Document-method: IO::open
7001 *
7002 * call-seq:
7003 * IO.open(fd, mode="r" [, opt]) -> io
7004 * IO.open(fd, mode="r" [, opt]) {|io| block } -> obj
7005 *
7006 * With no associated block, <code>IO.open</code> is a synonym for IO.new. If
7007 * the optional code block is given, it will be passed +io+ as an argument,
7008 * and the IO object will automatically be closed when the block terminates.
7009 * In this instance, IO.open returns the value of the block.
7010 *
7011 * See IO.new for a description of the +fd+, +mode+ and +opt+ parameters.
7012 */
7013
7014 static VALUE
rb_io_s_open(int argc,VALUE * argv,VALUE klass)7015 rb_io_s_open(int argc, VALUE *argv, VALUE klass)
7016 {
7017 VALUE io = rb_class_new_instance(argc, argv, klass);
7018
7019 if (rb_block_given_p()) {
7020 return rb_ensure(rb_yield, io, io_close, io);
7021 }
7022
7023 return io;
7024 }
7025
7026 /*
7027 * call-seq:
7028 * IO.sysopen(path, [mode, [perm]]) -> integer
7029 *
7030 * Opens the given path, returning the underlying file descriptor as a
7031 * <code>Integer</code>.
7032 *
7033 * IO.sysopen("testfile") #=> 3
7034 */
7035
7036 static VALUE
rb_io_s_sysopen(int argc,VALUE * argv)7037 rb_io_s_sysopen(int argc, VALUE *argv)
7038 {
7039 VALUE fname, vmode, vperm;
7040 VALUE intmode;
7041 int oflags, fd;
7042 mode_t perm;
7043
7044 rb_scan_args(argc, argv, "12", &fname, &vmode, &vperm);
7045 FilePathValue(fname);
7046
7047 if (NIL_P(vmode))
7048 oflags = O_RDONLY;
7049 else if (!NIL_P(intmode = rb_check_to_integer(vmode, "to_int")))
7050 oflags = NUM2INT(intmode);
7051 else {
7052 SafeStringValue(vmode);
7053 oflags = rb_io_modestr_oflags(StringValueCStr(vmode));
7054 }
7055 if (NIL_P(vperm)) perm = 0666;
7056 else perm = NUM2MODET(vperm);
7057
7058 RB_GC_GUARD(fname) = rb_str_new4(fname);
7059 fd = rb_sysopen(fname, oflags, perm);
7060 return INT2NUM(fd);
7061 }
7062
7063 static VALUE
check_pipe_command(VALUE filename_or_command)7064 check_pipe_command(VALUE filename_or_command)
7065 {
7066 char *s = RSTRING_PTR(filename_or_command);
7067 long l = RSTRING_LEN(filename_or_command);
7068 char *e = s + l;
7069 int chlen;
7070
7071 if (rb_enc_ascget(s, e, &chlen, rb_enc_get(filename_or_command)) == '|') {
7072 VALUE cmd = rb_str_new(s+chlen, l-chlen);
7073 OBJ_INFECT(cmd, filename_or_command);
7074 return cmd;
7075 }
7076 return Qnil;
7077 }
7078
7079 /*
7080 * call-seq:
7081 * open(path [, mode [, perm]] [, opt]) -> io or nil
7082 * open(path [, mode [, perm]] [, opt]) {|io| block } -> obj
7083 *
7084 * Creates an IO object connected to the given stream, file, or subprocess.
7085 *
7086 * If +path+ does not start with a pipe character (<code>|</code>), treat it
7087 * as the name of a file to open using the specified mode (defaulting to
7088 * "r").
7089 *
7090 * The +mode+ is either a string or an integer. If it is an integer, it
7091 * must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If
7092 * it is a string, it is either "fmode", "fmode:ext_enc", or
7093 * "fmode:ext_enc:int_enc".
7094 *
7095 * See the documentation of IO.new for full documentation of the +mode+ string
7096 * directives.
7097 *
7098 * If a file is being created, its initial permissions may be set using the
7099 * +perm+ parameter. See File.new and the open(2) and chmod(2) man pages for
7100 * a description of permissions.
7101 *
7102 * If a block is specified, it will be invoked with the IO object as a
7103 * parameter, and the IO will be automatically closed when the block
7104 * terminates. The call returns the value of the block.
7105 *
7106 * If +path+ starts with a pipe character (<code>"|"</code>), a subprocess is
7107 * created, connected to the caller by a pair of pipes. The returned IO
7108 * object may be used to write to the standard input and read from the
7109 * standard output of this subprocess.
7110 *
7111 * If the command following the pipe is a single minus sign
7112 * (<code>"|-"</code>), Ruby forks, and this subprocess is connected to the
7113 * parent. If the command is not <code>"-"</code>, the subprocess runs the
7114 * command.
7115 *
7116 * When the subprocess is Ruby (opened via <code>"|-"</code>), the +open+
7117 * call returns +nil+. If a block is associated with the open call, that
7118 * block will run twice --- once in the parent and once in the child.
7119 *
7120 * The block parameter will be an IO object in the parent and +nil+ in the
7121 * child. The parent's +IO+ object will be connected to the child's $stdin
7122 * and $stdout. The subprocess will be terminated at the end of the block.
7123 *
7124 * === Examples
7125 *
7126 * Reading from "testfile":
7127 *
7128 * open("testfile") do |f|
7129 * print f.gets
7130 * end
7131 *
7132 * Produces:
7133 *
7134 * This is line one
7135 *
7136 * Open a subprocess and read its output:
7137 *
7138 * cmd = open("|date")
7139 * print cmd.gets
7140 * cmd.close
7141 *
7142 * Produces:
7143 *
7144 * Wed Apr 9 08:56:31 CDT 2003
7145 *
7146 * Open a subprocess running the same Ruby program:
7147 *
7148 * f = open("|-", "w+")
7149 * if f.nil?
7150 * puts "in Child"
7151 * exit
7152 * else
7153 * puts "Got: #{f.gets}"
7154 * end
7155 *
7156 * Produces:
7157 *
7158 * Got: in Child
7159 *
7160 * Open a subprocess using a block to receive the IO object:
7161 *
7162 * open "|-" do |f|
7163 * if f then
7164 * # parent process
7165 * puts "Got: #{f.gets}"
7166 * else
7167 * # child process
7168 * puts "in Child"
7169 * end
7170 * end
7171 *
7172 * Produces:
7173 *
7174 * Got: in Child
7175 */
7176
7177 static VALUE
rb_f_open(int argc,VALUE * argv)7178 rb_f_open(int argc, VALUE *argv)
7179 {
7180 ID to_open = 0;
7181 int redirect = FALSE;
7182
7183 if (argc >= 1) {
7184 CONST_ID(to_open, "to_open");
7185 if (rb_respond_to(argv[0], to_open)) {
7186 redirect = TRUE;
7187 }
7188 else {
7189 VALUE tmp = argv[0];
7190 FilePathValue(tmp);
7191 if (NIL_P(tmp)) {
7192 redirect = TRUE;
7193 }
7194 else {
7195 VALUE cmd = check_pipe_command(tmp);
7196 if (!NIL_P(cmd)) {
7197 argv[0] = cmd;
7198 return rb_io_s_popen(argc, argv, rb_cIO);
7199 }
7200 }
7201 }
7202 }
7203 if (redirect) {
7204 VALUE io = rb_funcallv(argv[0], to_open, argc-1, argv+1);
7205
7206 if (rb_block_given_p()) {
7207 return rb_ensure(rb_yield, io, io_close, io);
7208 }
7209 return io;
7210 }
7211 return rb_io_s_open(argc, argv, rb_cFile);
7212 }
7213
7214 static VALUE rb_io_open_generic(VALUE, VALUE, int, int, const convconfig_t *, mode_t);
7215
7216 static VALUE
rb_io_open(VALUE io,VALUE filename,VALUE vmode,VALUE vperm,VALUE opt)7217 rb_io_open(VALUE io, VALUE filename, VALUE vmode, VALUE vperm, VALUE opt)
7218 {
7219 int oflags, fmode;
7220 convconfig_t convconfig;
7221 mode_t perm;
7222
7223 rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, &convconfig);
7224 perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
7225 return rb_io_open_generic(io, filename, oflags, fmode, &convconfig, perm);
7226 }
7227
7228 static VALUE
rb_io_open_generic(VALUE klass,VALUE filename,int oflags,int fmode,const convconfig_t * convconfig,mode_t perm)7229 rb_io_open_generic(VALUE klass, VALUE filename, int oflags, int fmode,
7230 const convconfig_t *convconfig, mode_t perm)
7231 {
7232 VALUE cmd;
7233 if (klass == rb_cIO && !NIL_P(cmd = check_pipe_command(filename))) {
7234 return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, convconfig);
7235 }
7236 else {
7237 return rb_file_open_generic(io_alloc(klass), filename,
7238 oflags, fmode, convconfig, perm);
7239 }
7240 }
7241
7242 static VALUE
io_reopen(VALUE io,VALUE nfile)7243 io_reopen(VALUE io, VALUE nfile)
7244 {
7245 rb_io_t *fptr, *orig;
7246 int fd, fd2;
7247 off_t pos = 0;
7248
7249 nfile = rb_io_get_io(nfile);
7250 GetOpenFile(io, fptr);
7251 GetOpenFile(nfile, orig);
7252
7253 if (fptr == orig) return io;
7254 if (IS_PREP_STDIO(fptr)) {
7255 if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
7256 (fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
7257 (fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
7258 rb_raise(rb_eArgError,
7259 "%s can't change access mode from \"%s\" to \"%s\"",
7260 PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
7261 rb_io_fmode_modestr(orig->mode));
7262 }
7263 }
7264 if (fptr->mode & FMODE_WRITABLE) {
7265 if (io_fflush(fptr) < 0)
7266 rb_sys_fail(0);
7267 }
7268 else {
7269 io_tell(fptr);
7270 }
7271 if (orig->mode & FMODE_READABLE) {
7272 pos = io_tell(orig);
7273 }
7274 if (orig->mode & FMODE_WRITABLE) {
7275 if (io_fflush(orig) < 0)
7276 rb_sys_fail(0);
7277 }
7278
7279 /* copy rb_io_t structure */
7280 fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
7281 fptr->pid = orig->pid;
7282 fptr->lineno = orig->lineno;
7283 if (RTEST(orig->pathv)) fptr->pathv = orig->pathv;
7284 else if (!IS_PREP_STDIO(fptr)) fptr->pathv = Qnil;
7285 fptr_copy_finalizer(fptr, orig);
7286
7287 fd = fptr->fd;
7288 fd2 = orig->fd;
7289 if (fd != fd2) {
7290 if (IS_PREP_STDIO(fptr) || fd <= 2 || !fptr->stdio_file) {
7291 /* need to keep FILE objects of stdin, stdout and stderr */
7292 if (rb_cloexec_dup2(fd2, fd) < 0)
7293 rb_sys_fail_path(orig->pathv);
7294 rb_update_max_fd(fd);
7295 }
7296 else {
7297 fclose(fptr->stdio_file);
7298 fptr->stdio_file = 0;
7299 fptr->fd = -1;
7300 if (rb_cloexec_dup2(fd2, fd) < 0)
7301 rb_sys_fail_path(orig->pathv);
7302 rb_update_max_fd(fd);
7303 fptr->fd = fd;
7304 }
7305 rb_thread_fd_close(fd);
7306 if ((orig->mode & FMODE_READABLE) && pos >= 0) {
7307 if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) {
7308 rb_sys_fail_path(fptr->pathv);
7309 }
7310 if (io_seek(orig, pos, SEEK_SET) < 0 && errno) {
7311 rb_sys_fail_path(orig->pathv);
7312 }
7313 }
7314 }
7315
7316 if (fptr->mode & FMODE_BINMODE) {
7317 rb_io_binmode(io);
7318 }
7319
7320 RBASIC_SET_CLASS(io, rb_obj_class(nfile));
7321 return io;
7322 }
7323
7324 #ifdef _WIN32
7325 int rb_freopen(VALUE fname, const char *mode, FILE *fp);
7326 #else
7327 static int
rb_freopen(VALUE fname,const char * mode,FILE * fp)7328 rb_freopen(VALUE fname, const char *mode, FILE *fp)
7329 {
7330 if (!freopen(RSTRING_PTR(fname), mode, fp)) {
7331 RB_GC_GUARD(fname);
7332 return errno;
7333 }
7334 return 0;
7335 }
7336 #endif
7337
7338 /*
7339 * call-seq:
7340 * ios.reopen(other_IO) -> ios
7341 * ios.reopen(path, mode [, opt]) -> ios
7342 *
7343 * Reassociates <em>ios</em> with the I/O stream given in
7344 * <i>other_IO</i> or to a new stream opened on <i>path</i>. This may
7345 * dynamically change the actual class of this stream.
7346 * The +mode+ and +opt+ parameters accept the same values as IO.open.
7347 *
7348 * f1 = File.new("testfile")
7349 * f2 = File.new("testfile")
7350 * f2.readlines[0] #=> "This is line one\n"
7351 * f2.reopen(f1) #=> #<File:testfile>
7352 * f2.readlines[0] #=> "This is line one\n"
7353 */
7354
7355 static VALUE
rb_io_reopen(int argc,VALUE * argv,VALUE file)7356 rb_io_reopen(int argc, VALUE *argv, VALUE file)
7357 {
7358 VALUE fname, nmode, opt;
7359 int oflags;
7360 rb_io_t *fptr;
7361
7362 if (rb_scan_args(argc, argv, "11:", &fname, &nmode, &opt) == 1) {
7363 VALUE tmp = rb_io_check_io(fname);
7364 if (!NIL_P(tmp)) {
7365 return io_reopen(file, tmp);
7366 }
7367 }
7368
7369 FilePathValue(fname);
7370 rb_io_taint_check(file);
7371 fptr = RFILE(file)->fptr;
7372 if (!fptr) {
7373 fptr = RFILE(file)->fptr = ZALLOC(rb_io_t);
7374 }
7375
7376 if (!NIL_P(nmode) || !NIL_P(opt)) {
7377 int fmode;
7378 convconfig_t convconfig;
7379
7380 rb_io_extract_modeenc(&nmode, 0, opt, &oflags, &fmode, &convconfig);
7381 if (IS_PREP_STDIO(fptr) &&
7382 ((fptr->mode & FMODE_READWRITE) & (fmode & FMODE_READWRITE)) !=
7383 (fptr->mode & FMODE_READWRITE)) {
7384 rb_raise(rb_eArgError,
7385 "%s can't change access mode from \"%s\" to \"%s\"",
7386 PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
7387 rb_io_fmode_modestr(fmode));
7388 }
7389 fptr->mode = fmode;
7390 fptr->encs = convconfig;
7391 }
7392 else {
7393 oflags = rb_io_fmode_oflags(fptr->mode);
7394 }
7395
7396 fptr->pathv = fname;
7397 if (fptr->fd < 0) {
7398 fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
7399 fptr->stdio_file = 0;
7400 return file;
7401 }
7402
7403 if (fptr->mode & FMODE_WRITABLE) {
7404 if (io_fflush(fptr) < 0)
7405 rb_sys_fail(0);
7406 }
7407 fptr->rbuf.off = fptr->rbuf.len = 0;
7408
7409 if (fptr->stdio_file) {
7410 int e = rb_freopen(rb_str_encode_ospath(fptr->pathv),
7411 rb_io_oflags_modestr(oflags),
7412 fptr->stdio_file);
7413 if (e) rb_syserr_fail_path(e, fptr->pathv);
7414 fptr->fd = fileno(fptr->stdio_file);
7415 rb_fd_fix_cloexec(fptr->fd);
7416 #ifdef USE_SETVBUF
7417 if (setvbuf(fptr->stdio_file, NULL, _IOFBF, 0) != 0)
7418 rb_warn("setvbuf() can't be honoured for %"PRIsVALUE, fptr->pathv);
7419 #endif
7420 if (fptr->stdio_file == stderr) {
7421 if (setvbuf(fptr->stdio_file, NULL, _IONBF, BUFSIZ) != 0)
7422 rb_warn("setvbuf() can't be honoured for %"PRIsVALUE, fptr->pathv);
7423 }
7424 else if (fptr->stdio_file == stdout && isatty(fptr->fd)) {
7425 if (setvbuf(fptr->stdio_file, NULL, _IOLBF, BUFSIZ) != 0)
7426 rb_warn("setvbuf() can't be honoured for %"PRIsVALUE, fptr->pathv);
7427 }
7428 }
7429 else {
7430 int tmpfd = rb_sysopen(fptr->pathv, oflags, 0666);
7431 int err = 0;
7432 if (rb_cloexec_dup2(tmpfd, fptr->fd) < 0)
7433 err = errno;
7434 (void)close(tmpfd);
7435 if (err) {
7436 rb_syserr_fail_path(err, fptr->pathv);
7437 }
7438 }
7439
7440 return file;
7441 }
7442
7443 /* :nodoc: */
7444 static VALUE
rb_io_init_copy(VALUE dest,VALUE io)7445 rb_io_init_copy(VALUE dest, VALUE io)
7446 {
7447 rb_io_t *fptr, *orig;
7448 int fd;
7449 VALUE write_io;
7450 off_t pos;
7451
7452 io = rb_io_get_io(io);
7453 if (!OBJ_INIT_COPY(dest, io)) return dest;
7454 GetOpenFile(io, orig);
7455 MakeOpenFile(dest, fptr);
7456
7457 rb_io_flush(io);
7458
7459 /* copy rb_io_t structure */
7460 fptr->mode = orig->mode & ~FMODE_PREP;
7461 fptr->encs = orig->encs;
7462 fptr->pid = orig->pid;
7463 fptr->lineno = orig->lineno;
7464 if (!NIL_P(orig->pathv)) fptr->pathv = orig->pathv;
7465 fptr_copy_finalizer(fptr, orig);
7466
7467 fd = ruby_dup(orig->fd);
7468 fptr->fd = fd;
7469 pos = io_tell(orig);
7470 if (0 <= pos)
7471 io_seek(fptr, pos, SEEK_SET);
7472 if (fptr->mode & FMODE_BINMODE) {
7473 rb_io_binmode(dest);
7474 }
7475
7476 write_io = GetWriteIO(io);
7477 if (io != write_io) {
7478 write_io = rb_obj_dup(write_io);
7479 fptr->tied_io_for_writing = write_io;
7480 rb_ivar_set(dest, rb_intern("@tied_io_for_writing"), write_io);
7481 }
7482
7483 return dest;
7484 }
7485
7486 /*
7487 * call-seq:
7488 * ios.printf(format_string [, obj, ...]) -> nil
7489 *
7490 * Formats and writes to <em>ios</em>, converting parameters under
7491 * control of the format string. See <code>Kernel#sprintf</code>
7492 * for details.
7493 */
7494
7495 VALUE
rb_io_printf(int argc,const VALUE * argv,VALUE out)7496 rb_io_printf(int argc, const VALUE *argv, VALUE out)
7497 {
7498 rb_io_write(out, rb_f_sprintf(argc, argv));
7499 return Qnil;
7500 }
7501
7502 /*
7503 * call-seq:
7504 * printf(io, string [, obj ... ]) -> nil
7505 * printf(string [, obj ... ]) -> nil
7506 *
7507 * Equivalent to:
7508 * io.write(sprintf(string, obj, ...))
7509 * or
7510 * $stdout.write(sprintf(string, obj, ...))
7511 */
7512
7513 static VALUE
rb_f_printf(int argc,VALUE * argv)7514 rb_f_printf(int argc, VALUE *argv)
7515 {
7516 VALUE out;
7517
7518 if (argc == 0) return Qnil;
7519 if (RB_TYPE_P(argv[0], T_STRING)) {
7520 out = rb_stdout;
7521 }
7522 else {
7523 out = argv[0];
7524 argv++;
7525 argc--;
7526 }
7527 rb_io_write(out, rb_f_sprintf(argc, argv));
7528
7529 return Qnil;
7530 }
7531
7532 /*
7533 * call-seq:
7534 * ios.print -> nil
7535 * ios.print(obj, ...) -> nil
7536 *
7537 * Writes the given object(s) to <em>ios</em>. Returns +nil+.
7538 *
7539 * The stream must be opened for writing.
7540 * Each given object that isn't a string will be converted by calling
7541 * its <code>to_s</code> method.
7542 * When called without arguments, prints the contents of <code>$_</code>.
7543 *
7544 * If the output field separator (<code>$,</code>) is not +nil+,
7545 * it is inserted between objects.
7546 * If the output record separator (<code>$\\</code>) is not +nil+,
7547 * it is appended to the output.
7548 *
7549 * $stdout.print("This is ", 100, " percent.\n")
7550 *
7551 * <em>produces:</em>
7552 *
7553 * This is 100 percent.
7554 */
7555
7556 VALUE
rb_io_print(int argc,const VALUE * argv,VALUE out)7557 rb_io_print(int argc, const VALUE *argv, VALUE out)
7558 {
7559 int i;
7560 VALUE line;
7561
7562 /* if no argument given, print `$_' */
7563 if (argc == 0) {
7564 argc = 1;
7565 line = rb_lastline_get();
7566 argv = &line;
7567 }
7568 for (i=0; i<argc; i++) {
7569 if (!NIL_P(rb_output_fs) && i>0) {
7570 rb_io_write(out, rb_output_fs);
7571 }
7572 rb_io_write(out, argv[i]);
7573 }
7574 if (argc > 0 && !NIL_P(rb_output_rs)) {
7575 rb_io_write(out, rb_output_rs);
7576 }
7577
7578 return Qnil;
7579 }
7580
7581 /*
7582 * call-seq:
7583 * print(obj, ...) -> nil
7584 *
7585 * Prints each object in turn to <code>$stdout</code>. If the output
7586 * field separator (<code>$,</code>) is not +nil+, its
7587 * contents will appear between each field. If the output record
7588 * separator (<code>$\\</code>) is not +nil+, it will be
7589 * appended to the output. If no arguments are given, prints
7590 * <code>$_</code>. Objects that aren't strings will be converted by
7591 * calling their <code>to_s</code> method.
7592 *
7593 * print "cat", [1,2,3], 99, "\n"
7594 * $, = ", "
7595 * $\ = "\n"
7596 * print "cat", [1,2,3], 99
7597 *
7598 * <em>produces:</em>
7599 *
7600 * cat12399
7601 * cat, 1, 2, 3, 99
7602 */
7603
7604 static VALUE
rb_f_print(int argc,const VALUE * argv)7605 rb_f_print(int argc, const VALUE *argv)
7606 {
7607 rb_io_print(argc, argv, rb_stdout);
7608 return Qnil;
7609 }
7610
7611 /*
7612 * call-seq:
7613 * ios.putc(obj) -> obj
7614 *
7615 * If <i>obj</i> is <code>Numeric</code>, write the character whose code is
7616 * the least-significant byte of <i>obj</i>.
7617 * If <i>obj</i> is <code>String</code>, write the first character
7618 * of <i>obj</i> to <em>ios</em>.
7619 * Otherwise, raise <code>TypeError</code>.
7620 *
7621 * $stdout.putc "A"
7622 * $stdout.putc 65
7623 *
7624 * <em>produces:</em>
7625 *
7626 * AA
7627 */
7628
7629 static VALUE
rb_io_putc(VALUE io,VALUE ch)7630 rb_io_putc(VALUE io, VALUE ch)
7631 {
7632 VALUE str;
7633 if (RB_TYPE_P(ch, T_STRING)) {
7634 str = rb_str_substr(ch, 0, 1);
7635 }
7636 else {
7637 char c = NUM2CHR(ch);
7638 str = rb_str_new(&c, 1);
7639 }
7640 rb_io_write(io, str);
7641 return ch;
7642 }
7643
7644 /*
7645 * call-seq:
7646 * putc(int) -> int
7647 *
7648 * Equivalent to:
7649 *
7650 * $stdout.putc(int)
7651 *
7652 * Refer to the documentation for IO#putc for important information regarding
7653 * multi-byte characters.
7654 */
7655
7656 static VALUE
rb_f_putc(VALUE recv,VALUE ch)7657 rb_f_putc(VALUE recv, VALUE ch)
7658 {
7659 if (recv == rb_stdout) {
7660 return rb_io_putc(recv, ch);
7661 }
7662 return rb_funcallv(rb_stdout, rb_intern("putc"), 1, &ch);
7663 }
7664
7665
7666 int
rb_str_end_with_asciichar(VALUE str,int c)7667 rb_str_end_with_asciichar(VALUE str, int c)
7668 {
7669 long len = RSTRING_LEN(str);
7670 const char *ptr = RSTRING_PTR(str);
7671 rb_encoding *enc = rb_enc_from_index(ENCODING_GET(str));
7672 int n;
7673
7674 if (len == 0) return 0;
7675 if ((n = rb_enc_mbminlen(enc)) == 1) {
7676 return ptr[len - 1] == c;
7677 }
7678 return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) == c;
7679 }
7680
7681 static VALUE
io_puts_ary(VALUE ary,VALUE out,int recur)7682 io_puts_ary(VALUE ary, VALUE out, int recur)
7683 {
7684 VALUE tmp;
7685 long i;
7686
7687 if (recur) {
7688 tmp = rb_str_new2("[...]");
7689 rb_io_puts(1, &tmp, out);
7690 return Qtrue;
7691 }
7692 ary = rb_check_array_type(ary);
7693 if (NIL_P(ary)) return Qfalse;
7694 for (i=0; i<RARRAY_LEN(ary); i++) {
7695 tmp = RARRAY_AREF(ary, i);
7696 rb_io_puts(1, &tmp, out);
7697 }
7698 return Qtrue;
7699 }
7700
7701 /*
7702 * call-seq:
7703 * ios.puts(obj, ...) -> nil
7704 *
7705 * Writes the given object(s) to <em>ios</em>.
7706 * Writes a newline after any that do not already end
7707 * with a newline sequence. Returns +nil+.
7708 *
7709 * The stream must be opened for writing.
7710 * If called with an array argument, writes each element on a new line.
7711 * Each given object that isn't a string or array will be converted
7712 * by calling its +to_s+ method.
7713 * If called without arguments, outputs a single newline.
7714 *
7715 * $stdout.puts("this", "is", ["a", "test"])
7716 *
7717 * <em>produces:</em>
7718 *
7719 * this
7720 * is
7721 * a
7722 * test
7723 *
7724 * Note that +puts+ always uses newlines and is not affected
7725 * by the output record separator (<code>$\\</code>).
7726 */
7727
7728 VALUE
rb_io_puts(int argc,const VALUE * argv,VALUE out)7729 rb_io_puts(int argc, const VALUE *argv, VALUE out)
7730 {
7731 int i, n;
7732 VALUE line, args[2];
7733
7734 /* if no argument given, print newline. */
7735 if (argc == 0) {
7736 rb_io_write(out, rb_default_rs);
7737 return Qnil;
7738 }
7739 for (i=0; i<argc; i++) {
7740 if (RB_TYPE_P(argv[i], T_STRING)) {
7741 line = argv[i];
7742 goto string;
7743 }
7744 if (rb_exec_recursive(io_puts_ary, argv[i], out)) {
7745 continue;
7746 }
7747 line = rb_obj_as_string(argv[i]);
7748 string:
7749 n = 0;
7750 args[n++] = line;
7751 if (RSTRING_LEN(line) == 0 ||
7752 !rb_str_end_with_asciichar(line, '\n')) {
7753 args[n++] = rb_default_rs;
7754 }
7755 rb_io_writev(out, n, args);
7756 }
7757
7758 return Qnil;
7759 }
7760
7761 /*
7762 * call-seq:
7763 * puts(obj, ...) -> nil
7764 *
7765 * Equivalent to
7766 *
7767 * $stdout.puts(obj, ...)
7768 */
7769
7770 static VALUE
rb_f_puts(int argc,VALUE * argv,VALUE recv)7771 rb_f_puts(int argc, VALUE *argv, VALUE recv)
7772 {
7773 if (recv == rb_stdout) {
7774 return rb_io_puts(argc, argv, recv);
7775 }
7776 return rb_funcallv(rb_stdout, rb_intern("puts"), argc, argv);
7777 }
7778
7779 void
rb_p(VALUE obj)7780 rb_p(VALUE obj) /* for debug print within C code */
7781 {
7782 VALUE args[2];
7783 args[0] = rb_obj_as_string(rb_inspect(obj));
7784 args[1] = rb_default_rs;
7785 if (RB_TYPE_P(rb_stdout, T_FILE) &&
7786 rb_method_basic_definition_p(CLASS_OF(rb_stdout), id_write)) {
7787 io_writev(2, args, rb_stdout);
7788 }
7789 else {
7790 rb_io_writev(rb_stdout, 2, args);
7791 }
7792 }
7793
7794 struct rb_f_p_arg {
7795 int argc;
7796 VALUE *argv;
7797 };
7798
7799 static VALUE
rb_f_p_internal(VALUE arg)7800 rb_f_p_internal(VALUE arg)
7801 {
7802 struct rb_f_p_arg *arg1 = (struct rb_f_p_arg*)arg;
7803 int argc = arg1->argc;
7804 VALUE *argv = arg1->argv;
7805 int i;
7806 VALUE ret = Qnil;
7807
7808 for (i=0; i<argc; i++) {
7809 rb_p(argv[i]);
7810 }
7811 if (argc == 1) {
7812 ret = argv[0];
7813 }
7814 else if (argc > 1) {
7815 ret = rb_ary_new4(argc, argv);
7816 }
7817 if (RB_TYPE_P(rb_stdout, T_FILE)) {
7818 rb_io_flush(rb_stdout);
7819 }
7820 return ret;
7821 }
7822
7823 /*
7824 * call-seq:
7825 * p(obj) -> obj
7826 * p(obj1, obj2, ...) -> [obj, ...]
7827 * p() -> nil
7828 *
7829 * For each object, directly writes _obj_.+inspect+ followed by a
7830 * newline to the program's standard output.
7831 *
7832 * S = Struct.new(:name, :state)
7833 * s = S['dave', 'TX']
7834 * p s
7835 *
7836 * <em>produces:</em>
7837 *
7838 * #<S name="dave", state="TX">
7839 */
7840
7841 static VALUE
rb_f_p(int argc,VALUE * argv,VALUE self)7842 rb_f_p(int argc, VALUE *argv, VALUE self)
7843 {
7844 struct rb_f_p_arg arg;
7845 arg.argc = argc;
7846 arg.argv = argv;
7847
7848 return rb_uninterruptible(rb_f_p_internal, (VALUE)&arg);
7849 }
7850
7851 /*
7852 * call-seq:
7853 * obj.display(port=$>) -> nil
7854 *
7855 * Prints <i>obj</i> on the given port (default <code>$></code>).
7856 * Equivalent to:
7857 *
7858 * def display(port=$>)
7859 * port.write self
7860 * nil
7861 * end
7862 *
7863 * For example:
7864 *
7865 * 1.display
7866 * "cat".display
7867 * [ 4, 5, 6 ].display
7868 * puts
7869 *
7870 * <em>produces:</em>
7871 *
7872 * 1cat[4, 5, 6]
7873 */
7874
7875 static VALUE
rb_obj_display(int argc,VALUE * argv,VALUE self)7876 rb_obj_display(int argc, VALUE *argv, VALUE self)
7877 {
7878 VALUE out;
7879
7880 out = (!rb_check_arity(argc, 0, 1) ? rb_stdout : argv[0]);
7881 rb_io_write(out, self);
7882
7883 return Qnil;
7884 }
7885
7886 static int
rb_stderr_to_original_p(void)7887 rb_stderr_to_original_p(void)
7888 {
7889 return (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0);
7890 }
7891
7892 void
rb_write_error2(const char * mesg,long len)7893 rb_write_error2(const char *mesg, long len)
7894 {
7895 if (rb_stderr_to_original_p()) {
7896 #ifdef _WIN32
7897 if (isatty(fileno(stderr))) {
7898 if (rb_w32_write_console(rb_str_new(mesg, len), fileno(stderr)) > 0) return;
7899 }
7900 #endif
7901 if (fwrite(mesg, sizeof(char), (size_t)len, stderr) < (size_t)len) {
7902 /* failed to write to stderr, what can we do? */
7903 return;
7904 }
7905 }
7906 else {
7907 rb_io_write(rb_stderr, rb_str_new(mesg, len));
7908 }
7909 }
7910
7911 void
rb_write_error(const char * mesg)7912 rb_write_error(const char *mesg)
7913 {
7914 rb_write_error2(mesg, strlen(mesg));
7915 }
7916
7917 void
rb_write_error_str(VALUE mesg)7918 rb_write_error_str(VALUE mesg)
7919 {
7920 /* a stopgap measure for the time being */
7921 if (rb_stderr_to_original_p()) {
7922 size_t len = (size_t)RSTRING_LEN(mesg);
7923 #ifdef _WIN32
7924 if (isatty(fileno(stderr))) {
7925 if (rb_w32_write_console(mesg, fileno(stderr)) > 0) return;
7926 }
7927 #endif
7928 if (fwrite(RSTRING_PTR(mesg), sizeof(char), len, stderr) < len) {
7929 RB_GC_GUARD(mesg);
7930 return;
7931 }
7932 }
7933 else {
7934 /* may unlock GVL, and */
7935 rb_io_write(rb_stderr, mesg);
7936 }
7937 }
7938
7939 int
rb_stderr_tty_p(void)7940 rb_stderr_tty_p(void)
7941 {
7942 if (rb_stderr_to_original_p())
7943 return isatty(fileno(stderr));
7944 return 0;
7945 }
7946
7947 static void
must_respond_to(ID mid,VALUE val,ID id)7948 must_respond_to(ID mid, VALUE val, ID id)
7949 {
7950 if (!rb_respond_to(val, mid)) {
7951 rb_raise(rb_eTypeError, "%"PRIsVALUE" must have %"PRIsVALUE" method, %"PRIsVALUE" given",
7952 rb_id2str(id), rb_id2str(mid),
7953 rb_obj_class(val));
7954 }
7955 }
7956
7957 static void
stdout_setter(VALUE val,ID id,VALUE * variable)7958 stdout_setter(VALUE val, ID id, VALUE *variable)
7959 {
7960 must_respond_to(id_write, val, id);
7961 *variable = val;
7962 }
7963
7964 static VALUE
prep_io(int fd,int fmode,VALUE klass,const char * path)7965 prep_io(int fd, int fmode, VALUE klass, const char *path)
7966 {
7967 rb_io_t *fp;
7968 VALUE io = io_alloc(klass);
7969
7970 MakeOpenFile(io, fp);
7971 fp->fd = fd;
7972 fp->mode = fmode;
7973 if (!io_check_tty(fp)) {
7974 #ifdef __CYGWIN__
7975 fp->mode |= FMODE_BINMODE;
7976 setmode(fd, O_BINARY);
7977 #endif
7978 }
7979 if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path));
7980 rb_update_max_fd(fd);
7981
7982 return io;
7983 }
7984
7985 VALUE
rb_io_fdopen(int fd,int oflags,const char * path)7986 rb_io_fdopen(int fd, int oflags, const char *path)
7987 {
7988 VALUE klass = rb_cIO;
7989
7990 if (path && strcmp(path, "-")) klass = rb_cFile;
7991 return prep_io(fd, rb_io_oflags_fmode(oflags), klass, path);
7992 }
7993
7994 static VALUE
prep_stdio(FILE * f,int fmode,VALUE klass,const char * path)7995 prep_stdio(FILE *f, int fmode, VALUE klass, const char *path)
7996 {
7997 rb_io_t *fptr;
7998 VALUE io = prep_io(fileno(f), fmode|FMODE_PREP|DEFAULT_TEXTMODE, klass, path);
7999
8000 GetOpenFile(io, fptr);
8001 fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
8002 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
8003 fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
8004 if (fmode & FMODE_READABLE) {
8005 fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
8006 }
8007 #endif
8008 fptr->stdio_file = f;
8009
8010 return io;
8011 }
8012
8013 FILE *
rb_io_stdio_file(rb_io_t * fptr)8014 rb_io_stdio_file(rb_io_t *fptr)
8015 {
8016 if (!fptr->stdio_file) {
8017 int oflags = rb_io_fmode_oflags(fptr->mode);
8018 fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_oflags_modestr(oflags));
8019 }
8020 return fptr->stdio_file;
8021 }
8022
8023 static inline void
rb_io_buffer_init(rb_io_buffer_t * buf)8024 rb_io_buffer_init(rb_io_buffer_t *buf)
8025 {
8026 buf->ptr = NULL;
8027 buf->off = 0;
8028 buf->len = 0;
8029 buf->capa = 0;
8030 }
8031
8032 static inline rb_io_t *
rb_io_fptr_new(void)8033 rb_io_fptr_new(void)
8034 {
8035 rb_io_t *fp = ALLOC(rb_io_t);
8036 fp->fd = -1;
8037 fp->stdio_file = NULL;
8038 fp->mode = 0;
8039 fp->pid = 0;
8040 fp->lineno = 0;
8041 fp->pathv = Qnil;
8042 fp->finalize = 0;
8043 rb_io_buffer_init(&fp->wbuf);
8044 rb_io_buffer_init(&fp->rbuf);
8045 rb_io_buffer_init(&fp->cbuf);
8046 fp->readconv = NULL;
8047 fp->writeconv = NULL;
8048 fp->writeconv_asciicompat = Qnil;
8049 fp->writeconv_pre_ecflags = 0;
8050 fp->writeconv_pre_ecopts = Qnil;
8051 fp->writeconv_initialized = 0;
8052 fp->tied_io_for_writing = 0;
8053 fp->encs.enc = NULL;
8054 fp->encs.enc2 = NULL;
8055 fp->encs.ecflags = 0;
8056 fp->encs.ecopts = Qnil;
8057 fp->write_lock = 0;
8058 return fp;
8059 }
8060
8061 rb_io_t *
rb_io_make_open_file(VALUE obj)8062 rb_io_make_open_file(VALUE obj)
8063 {
8064 rb_io_t *fp = 0;
8065
8066 Check_Type(obj, T_FILE);
8067 if (RFILE(obj)->fptr) {
8068 rb_io_close(obj);
8069 rb_io_fptr_finalize(RFILE(obj)->fptr);
8070 RFILE(obj)->fptr = 0;
8071 }
8072 fp = rb_io_fptr_new();
8073 RFILE(obj)->fptr = fp;
8074 return fp;
8075 }
8076
8077 /*
8078 * call-seq:
8079 * IO.new(fd [, mode] [, opt]) -> io
8080 *
8081 * Returns a new IO object (a stream) for the given integer file descriptor
8082 * +fd+ and +mode+ string. +opt+ may be used to specify parts of +mode+ in a
8083 * more readable fashion. See also IO.sysopen and IO.for_fd.
8084 *
8085 * IO.new is called by various File and IO opening methods such as IO::open,
8086 * Kernel#open, and File::open.
8087 *
8088 * === Open Mode
8089 *
8090 * When +mode+ is an integer it must be combination of the modes defined in
8091 * File::Constants (+File::RDONLY+, <code>File::WRONLY|File::CREAT</code>).
8092 * See the open(2) man page for more information.
8093 *
8094 * When +mode+ is a string it must be in one of the following forms:
8095 *
8096 * fmode
8097 * fmode ":" ext_enc
8098 * fmode ":" ext_enc ":" int_enc
8099 * fmode ":" "BOM|UTF-*"
8100 *
8101 * +fmode+ is an IO open mode string, +ext_enc+ is the external encoding for
8102 * the IO and +int_enc+ is the internal encoding.
8103 *
8104 * ==== IO Open Mode
8105 *
8106 * Ruby allows the following open modes:
8107 *
8108 * "r" Read-only, starts at beginning of file (default mode).
8109 *
8110 * "r+" Read-write, starts at beginning of file.
8111 *
8112 * "w" Write-only, truncates existing file
8113 * to zero length or creates a new file for writing.
8114 *
8115 * "w+" Read-write, truncates existing file to zero length
8116 * or creates a new file for reading and writing.
8117 *
8118 * "a" Write-only, each write call appends data at end of file.
8119 * Creates a new file for writing if file does not exist.
8120 *
8121 * "a+" Read-write, each write call appends data at end of file.
8122 * Creates a new file for reading and writing if file does
8123 * not exist.
8124 *
8125 * The following modes must be used separately, and along with one or more of
8126 * the modes seen above.
8127 *
8128 * "b" Binary file mode
8129 * Suppresses EOL <-> CRLF conversion on Windows. And
8130 * sets external encoding to ASCII-8BIT unless explicitly
8131 * specified.
8132 *
8133 * "t" Text file mode
8134 *
8135 * The exclusive access mode ("x") can be used together with "w" to ensure
8136 * the file is created. <code>Errno::EEXIST</code> is raised when it already
8137 * exists. It may not be supported with all kinds of streams (e.g. pipes).
8138 *
8139 * When the open mode of original IO is read only, the mode cannot be
8140 * changed to be writable. Similarly, the open mode cannot be changed from
8141 * write only to readable.
8142 *
8143 * When such a change is attempted the error is raised in different locations
8144 * according to the platform.
8145 *
8146 * === IO Encoding
8147 *
8148 * When +ext_enc+ is specified, strings read will be tagged by the encoding
8149 * when reading, and strings output will be converted to the specified
8150 * encoding when writing.
8151 *
8152 * When +ext_enc+ and +int_enc+ are specified read strings will be converted
8153 * from +ext_enc+ to +int_enc+ upon input, and written strings will be
8154 * converted from +int_enc+ to +ext_enc+ upon output. See Encoding for
8155 * further details of transcoding on input and output.
8156 *
8157 * If "BOM|UTF-8", "BOM|UTF-16LE" or "BOM|UTF16-BE" are used, Ruby checks for
8158 * a Unicode BOM in the input document to help determine the encoding. For
8159 * UTF-16 encodings the file open mode must be binary. When present, the BOM
8160 * is stripped and the external encoding from the BOM is used. When the BOM
8161 * is missing the given Unicode encoding is used as +ext_enc+. (The BOM-set
8162 * encoding option is case insensitive, so "bom|utf-8" is also valid.)
8163 *
8164 * === Options
8165 *
8166 * +opt+ can be used instead of +mode+ for improved readability. The
8167 * following keys are supported:
8168 *
8169 * :mode ::
8170 * Same as +mode+ parameter
8171 *
8172 * :flags ::
8173 * Specifies file open flags as integer.
8174 * If +mode+ parameter is given, this parameter will be bitwise-ORed.
8175 *
8176 * :\external_encoding ::
8177 * External encoding for the IO.
8178 *
8179 * :\internal_encoding ::
8180 * Internal encoding for the IO. "-" is a synonym for the default internal
8181 * encoding.
8182 *
8183 * If the value is +nil+ no conversion occurs.
8184 *
8185 * :encoding ::
8186 * Specifies external and internal encodings as "extern:intern".
8187 *
8188 * :textmode ::
8189 * If the value is truth value, same as "t" in argument +mode+.
8190 *
8191 * :binmode ::
8192 * If the value is truth value, same as "b" in argument +mode+.
8193 *
8194 * :autoclose ::
8195 * If the value is +false+, the +fd+ will be kept open after this IO
8196 * instance gets finalized.
8197 *
8198 * Also, +opt+ can have same keys in String#encode for controlling conversion
8199 * between the external encoding and the internal encoding.
8200 *
8201 * === Example 1
8202 *
8203 * fd = IO.sysopen("/dev/tty", "w")
8204 * a = IO.new(fd,"w")
8205 * $stderr.puts "Hello"
8206 * a.puts "World"
8207 *
8208 * Produces:
8209 *
8210 * Hello
8211 * World
8212 *
8213 * === Example 2
8214 *
8215 * require 'fcntl'
8216 *
8217 * fd = STDERR.fcntl(Fcntl::F_DUPFD)
8218 * io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true)
8219 * io.puts "Hello, World!"
8220 *
8221 * fd = STDERR.fcntl(Fcntl::F_DUPFD)
8222 * io = IO.new(fd, mode: 'w', cr_newline: true,
8223 * external_encoding: Encoding::UTF_16LE)
8224 * io.puts "Hello, World!"
8225 *
8226 * Both of above print "Hello, World!" in UTF-16LE to standard error output
8227 * with converting EOL generated by <code>puts</code> to CR.
8228 */
8229
8230 static VALUE
rb_io_initialize(int argc,VALUE * argv,VALUE io)8231 rb_io_initialize(int argc, VALUE *argv, VALUE io)
8232 {
8233 VALUE fnum, vmode;
8234 rb_io_t *fp;
8235 int fd, fmode, oflags = O_RDONLY;
8236 convconfig_t convconfig;
8237 VALUE opt;
8238 #if defined(HAVE_FCNTL) && defined(F_GETFL)
8239 int ofmode;
8240 #else
8241 struct stat st;
8242 #endif
8243
8244
8245 argc = rb_scan_args(argc, argv, "11:", &fnum, &vmode, &opt);
8246 rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
8247
8248 fd = NUM2INT(fnum);
8249 if (rb_reserved_fd_p(fd)) {
8250 rb_raise(rb_eArgError, "The given fd is not accessible because RubyVM reserves it");
8251 }
8252 #if defined(HAVE_FCNTL) && defined(F_GETFL)
8253 oflags = fcntl(fd, F_GETFL);
8254 if (oflags == -1) rb_sys_fail(0);
8255 #else
8256 if (fstat(fd, &st) < 0) rb_sys_fail(0);
8257 #endif
8258 rb_update_max_fd(fd);
8259 #if defined(HAVE_FCNTL) && defined(F_GETFL)
8260 ofmode = rb_io_oflags_fmode(oflags);
8261 if (NIL_P(vmode)) {
8262 fmode = ofmode;
8263 }
8264 else if ((~ofmode & fmode) & FMODE_READWRITE) {
8265 VALUE error = INT2FIX(EINVAL);
8266 rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError));
8267 }
8268 #endif
8269 if (!NIL_P(opt) && rb_hash_aref(opt, sym_autoclose) == Qfalse) {
8270 fmode |= FMODE_PREP;
8271 }
8272 MakeOpenFile(io, fp);
8273 fp->fd = fd;
8274 fp->mode = fmode;
8275 fp->encs = convconfig;
8276 clear_codeconv(fp);
8277 io_check_tty(fp);
8278 if (fileno(stdin) == fd)
8279 fp->stdio_file = stdin;
8280 else if (fileno(stdout) == fd)
8281 fp->stdio_file = stdout;
8282 else if (fileno(stderr) == fd)
8283 fp->stdio_file = stderr;
8284
8285 if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
8286 return io;
8287 }
8288
8289 /*
8290 * call-seq:
8291 * File.new(filename, mode="r" [, opt]) -> file
8292 * File.new(filename [, mode [, perm]] [, opt]) -> file
8293 *
8294 * Opens the file named by +filename+ according to the given +mode+ and
8295 * returns a new File object.
8296 *
8297 * See IO.new for a description of +mode+ and +opt+.
8298 *
8299 * If a file is being created, permission bits may be given in +perm+. These
8300 * mode and permission bits are platform dependent; on Unix systems, see
8301 * open(2) and chmod(2) man pages for details.
8302 *
8303 * The new File object is buffered mode (or non-sync mode), unless
8304 * +filename+ is a tty.
8305 * See IO#flush, IO#fsync, IO#fdatasync, and <code>IO#sync=</code>
8306 * about sync mode.
8307 *
8308 * === Examples
8309 *
8310 * f = File.new("testfile", "r")
8311 * f = File.new("newfile", "w+")
8312 * f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
8313 */
8314
8315 static VALUE
rb_file_initialize(int argc,VALUE * argv,VALUE io)8316 rb_file_initialize(int argc, VALUE *argv, VALUE io)
8317 {
8318 if (RFILE(io)->fptr) {
8319 rb_raise(rb_eRuntimeError, "reinitializing File");
8320 }
8321 if (0 < argc && argc < 3) {
8322 VALUE fd = rb_check_to_int(argv[0]);
8323
8324 if (!NIL_P(fd)) {
8325 argv[0] = fd;
8326 return rb_io_initialize(argc, argv, io);
8327 }
8328 }
8329 rb_open_file(argc, argv, io);
8330
8331 return io;
8332 }
8333
8334 /* :nodoc: */
8335 static VALUE
rb_io_s_new(int argc,VALUE * argv,VALUE klass)8336 rb_io_s_new(int argc, VALUE *argv, VALUE klass)
8337 {
8338 if (rb_block_given_p()) {
8339 VALUE cname = rb_obj_as_string(klass);
8340
8341 rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
8342 cname, cname);
8343 }
8344 return rb_class_new_instance(argc, argv, klass);
8345 }
8346
8347
8348 /*
8349 * call-seq:
8350 * IO.for_fd(fd, mode [, opt]) -> io
8351 *
8352 * Synonym for <code>IO.new</code>.
8353 *
8354 */
8355
8356 static VALUE
rb_io_s_for_fd(int argc,VALUE * argv,VALUE klass)8357 rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
8358 {
8359 VALUE io = rb_obj_alloc(klass);
8360 rb_io_initialize(argc, argv, io);
8361 return io;
8362 }
8363
8364 /*
8365 * call-seq:
8366 * ios.autoclose? -> true or false
8367 *
8368 * Returns +true+ if the underlying file descriptor of _ios_ will be
8369 * closed automatically at its finalization, otherwise +false+.
8370 */
8371
8372 static VALUE
rb_io_autoclose_p(VALUE io)8373 rb_io_autoclose_p(VALUE io)
8374 {
8375 rb_io_t *fptr = RFILE(io)->fptr;
8376 rb_io_check_closed(fptr);
8377 return (fptr->mode & FMODE_PREP) ? Qfalse : Qtrue;
8378 }
8379
8380 /*
8381 * call-seq:
8382 * io.autoclose = bool -> true or false
8383 *
8384 * Sets auto-close flag.
8385 *
8386 * f = open("/dev/null")
8387 * IO.for_fd(f.fileno)
8388 * # ...
8389 * f.gets # may cause IOError
8390 *
8391 * f = open("/dev/null")
8392 * IO.for_fd(f.fileno).autoclose = true
8393 * # ...
8394 * f.gets # won't cause IOError
8395 */
8396
8397 static VALUE
rb_io_set_autoclose(VALUE io,VALUE autoclose)8398 rb_io_set_autoclose(VALUE io, VALUE autoclose)
8399 {
8400 rb_io_t *fptr;
8401 GetOpenFile(io, fptr);
8402 if (!RTEST(autoclose))
8403 fptr->mode |= FMODE_PREP;
8404 else
8405 fptr->mode &= ~FMODE_PREP;
8406 return io;
8407 }
8408
8409 static void
argf_mark(void * ptr)8410 argf_mark(void *ptr)
8411 {
8412 struct argf *p = ptr;
8413 rb_gc_mark(p->filename);
8414 rb_gc_mark(p->current_file);
8415 rb_gc_mark(p->argv);
8416 rb_gc_mark(p->inplace);
8417 rb_gc_mark(p->encs.ecopts);
8418 }
8419
8420 static size_t
argf_memsize(const void * ptr)8421 argf_memsize(const void *ptr)
8422 {
8423 const struct argf *p = ptr;
8424 size_t size = sizeof(*p);
8425 return size;
8426 }
8427
8428 static const rb_data_type_t argf_type = {
8429 "ARGF",
8430 {argf_mark, RUBY_TYPED_DEFAULT_FREE, argf_memsize},
8431 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
8432 };
8433
8434 static inline void
argf_init(struct argf * p,VALUE v)8435 argf_init(struct argf *p, VALUE v)
8436 {
8437 p->filename = Qnil;
8438 p->current_file = Qnil;
8439 p->lineno = 0;
8440 p->argv = v;
8441 }
8442
8443 static VALUE
argf_alloc(VALUE klass)8444 argf_alloc(VALUE klass)
8445 {
8446 struct argf *p;
8447 VALUE argf = TypedData_Make_Struct(klass, struct argf, &argf_type, p);
8448
8449 argf_init(p, Qnil);
8450 return argf;
8451 }
8452
8453 #undef rb_argv
8454
8455 /* :nodoc: */
8456 static VALUE
argf_initialize(VALUE argf,VALUE argv)8457 argf_initialize(VALUE argf, VALUE argv)
8458 {
8459 memset(&ARGF, 0, sizeof(ARGF));
8460 argf_init(&ARGF, argv);
8461
8462 return argf;
8463 }
8464
8465 /* :nodoc: */
8466 static VALUE
argf_initialize_copy(VALUE argf,VALUE orig)8467 argf_initialize_copy(VALUE argf, VALUE orig)
8468 {
8469 if (!OBJ_INIT_COPY(argf, orig)) return argf;
8470 ARGF = argf_of(orig);
8471 ARGF.argv = rb_obj_dup(ARGF.argv);
8472 return argf;
8473 }
8474
8475 /*
8476 * call-seq:
8477 * ARGF.lineno = integer -> integer
8478 *
8479 * Sets the line number of +ARGF+ as a whole to the given +Integer+.
8480 *
8481 * +ARGF+ sets the line number automatically as you read data, so normally
8482 * you will not need to set it explicitly. To access the current line number
8483 * use +ARGF.lineno+.
8484 *
8485 * For example:
8486 *
8487 * ARGF.lineno #=> 0
8488 * ARGF.readline #=> "This is line 1\n"
8489 * ARGF.lineno #=> 1
8490 * ARGF.lineno = 0 #=> 0
8491 * ARGF.lineno #=> 0
8492 */
8493 static VALUE
argf_set_lineno(VALUE argf,VALUE val)8494 argf_set_lineno(VALUE argf, VALUE val)
8495 {
8496 ARGF.lineno = NUM2INT(val);
8497 ARGF.last_lineno = ARGF.lineno;
8498 return Qnil;
8499 }
8500
8501 /*
8502 * call-seq:
8503 * ARGF.lineno -> integer
8504 *
8505 * Returns the current line number of ARGF as a whole. This value
8506 * can be set manually with +ARGF.lineno=+.
8507 *
8508 * For example:
8509 *
8510 * ARGF.lineno #=> 0
8511 * ARGF.readline #=> "This is line 1\n"
8512 * ARGF.lineno #=> 1
8513 */
8514 static VALUE
argf_lineno(VALUE argf)8515 argf_lineno(VALUE argf)
8516 {
8517 return INT2FIX(ARGF.lineno);
8518 }
8519
8520 static VALUE
argf_forward(int argc,VALUE * argv,VALUE argf)8521 argf_forward(int argc, VALUE *argv, VALUE argf)
8522 {
8523 return rb_funcall3(ARGF.current_file, rb_frame_this_func(), argc, argv);
8524 }
8525
8526 #define next_argv() argf_next_argv(argf)
8527 #define ARGF_GENERIC_INPUT_P() \
8528 (ARGF.current_file == rb_stdin && !RB_TYPE_P(ARGF.current_file, T_FILE))
8529 #define ARGF_FORWARD(argc, argv) do {\
8530 if (ARGF_GENERIC_INPUT_P())\
8531 return argf_forward((argc), (argv), argf);\
8532 } while (0)
8533 #define NEXT_ARGF_FORWARD(argc, argv) do {\
8534 if (!next_argv()) return Qnil;\
8535 ARGF_FORWARD((argc), (argv));\
8536 } while (0)
8537
8538 static void
argf_close(VALUE argf)8539 argf_close(VALUE argf)
8540 {
8541 VALUE file = ARGF.current_file;
8542 if (file == rb_stdin) return;
8543 if (RB_TYPE_P(file, T_FILE)) {
8544 rb_io_set_write_io(file, Qnil);
8545 }
8546 io_close(file);
8547 ARGF.init_p = -1;
8548 }
8549
8550 static int
argf_next_argv(VALUE argf)8551 argf_next_argv(VALUE argf)
8552 {
8553 char *fn;
8554 rb_io_t *fptr;
8555 int stdout_binmode = 0;
8556 int fmode;
8557
8558 if (RB_TYPE_P(rb_stdout, T_FILE)) {
8559 GetOpenFile(rb_stdout, fptr);
8560 if (fptr->mode & FMODE_BINMODE)
8561 stdout_binmode = 1;
8562 }
8563
8564 if (ARGF.init_p == 0) {
8565 if (!NIL_P(ARGF.argv) && RARRAY_LEN(ARGF.argv) > 0) {
8566 ARGF.next_p = 1;
8567 }
8568 else {
8569 ARGF.next_p = -1;
8570 }
8571 ARGF.init_p = 1;
8572 }
8573 else {
8574 if (NIL_P(ARGF.argv)) {
8575 ARGF.next_p = -1;
8576 }
8577 else if (ARGF.next_p == -1 && RARRAY_LEN(ARGF.argv) > 0) {
8578 ARGF.next_p = 1;
8579 }
8580 }
8581
8582 if (ARGF.next_p == 1) {
8583 if (ARGF.init_p == 1) argf_close(argf);
8584 retry:
8585 if (RARRAY_LEN(ARGF.argv) > 0) {
8586 VALUE filename = rb_ary_shift(ARGF.argv);
8587 FilePathValue(filename);
8588 ARGF.filename = filename;
8589 filename = rb_str_encode_ospath(filename);
8590 fn = StringValueCStr(filename);
8591 if (RSTRING_LEN(filename) == 1 && fn[0] == '-') {
8592 ARGF.current_file = rb_stdin;
8593 if (ARGF.inplace) {
8594 rb_warn("Can't do inplace edit for stdio; skipping");
8595 goto retry;
8596 }
8597 }
8598 else {
8599 VALUE write_io = Qnil;
8600 int fr = rb_sysopen(filename, O_RDONLY, 0);
8601
8602 if (ARGF.inplace) {
8603 struct stat st;
8604 #ifndef NO_SAFE_RENAME
8605 struct stat st2;
8606 #endif
8607 VALUE str;
8608 int fw;
8609
8610 if (RB_TYPE_P(rb_stdout, T_FILE) && rb_stdout != orig_stdout) {
8611 rb_io_close(rb_stdout);
8612 }
8613 fstat(fr, &st);
8614 str = filename;
8615 if (!NIL_P(ARGF.inplace)) {
8616 VALUE suffix = ARGF.inplace;
8617 str = rb_str_dup(str);
8618 if (NIL_P(rb_str_cat_conv_enc_opts(str, RSTRING_LEN(str),
8619 RSTRING_PTR(suffix), RSTRING_LEN(suffix),
8620 rb_enc_get(suffix), 0, Qnil))) {
8621 rb_str_append(str, suffix);
8622 }
8623 #ifdef NO_SAFE_RENAME
8624 (void)close(fr);
8625 (void)unlink(RSTRING_PTR(str));
8626 if (rename(fn, RSTRING_PTR(str)) < 0) {
8627 rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
8628 filename, str, strerror(errno));
8629 goto retry;
8630 }
8631 fr = rb_sysopen(str, O_RDONLY, 0);
8632 #else
8633 if (rename(fn, RSTRING_PTR(str)) < 0) {
8634 rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
8635 filename, str, strerror(errno));
8636 close(fr);
8637 goto retry;
8638 }
8639 #endif
8640 }
8641 else {
8642 #ifdef NO_SAFE_RENAME
8643 rb_fatal("Can't do inplace edit without backup");
8644 #else
8645 if (unlink(fn) < 0) {
8646 rb_warn("Can't remove %"PRIsVALUE": %s, skipping file",
8647 filename, strerror(errno));
8648 close(fr);
8649 goto retry;
8650 }
8651 #endif
8652 }
8653 fw = rb_sysopen(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
8654 #ifndef NO_SAFE_RENAME
8655 fstat(fw, &st2);
8656 #ifdef HAVE_FCHMOD
8657 fchmod(fw, st.st_mode);
8658 #else
8659 chmod(fn, st.st_mode);
8660 #endif
8661 if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
8662 int err;
8663 #ifdef HAVE_FCHOWN
8664 err = fchown(fw, st.st_uid, st.st_gid);
8665 #else
8666 err = chown(fn, st.st_uid, st.st_gid);
8667 #endif
8668 if (err && getuid() == 0 && st2.st_uid == 0) {
8669 const char *wkfn = RSTRING_PTR(filename);
8670 rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file",
8671 filename, str, strerror(errno));
8672 (void)close(fr);
8673 (void)close(fw);
8674 (void)unlink(wkfn);
8675 goto retry;
8676 }
8677 }
8678 #endif
8679 write_io = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
8680 rb_stdout = write_io;
8681 if (stdout_binmode) rb_io_binmode(rb_stdout);
8682 }
8683 fmode = FMODE_READABLE;
8684 if (!ARGF.binmode) {
8685 fmode |= DEFAULT_TEXTMODE;
8686 }
8687 ARGF.current_file = prep_io(fr, fmode, rb_cFile, fn);
8688 if (!NIL_P(write_io)) {
8689 rb_io_set_write_io(ARGF.current_file, write_io);
8690 }
8691 RB_GC_GUARD(filename);
8692 }
8693 if (ARGF.binmode) rb_io_ascii8bit_binmode(ARGF.current_file);
8694 GetOpenFile(ARGF.current_file, fptr);
8695 if (ARGF.encs.enc) {
8696 fptr->encs = ARGF.encs;
8697 clear_codeconv(fptr);
8698 }
8699 else {
8700 fptr->encs.ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
8701 if (!ARGF.binmode) {
8702 fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
8703 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
8704 fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
8705 #endif
8706 }
8707 }
8708 ARGF.next_p = 0;
8709 }
8710 else {
8711 ARGF.next_p = 1;
8712 return FALSE;
8713 }
8714 }
8715 else if (ARGF.next_p == -1) {
8716 ARGF.current_file = rb_stdin;
8717 ARGF.filename = rb_str_new2("-");
8718 if (ARGF.inplace) {
8719 rb_warn("Can't do inplace edit for stdio");
8720 rb_stdout = orig_stdout;
8721 }
8722 }
8723 if (ARGF.init_p == -1) ARGF.init_p = 1;
8724 return TRUE;
8725 }
8726
8727 static VALUE
argf_getline(int argc,VALUE * argv,VALUE argf)8728 argf_getline(int argc, VALUE *argv, VALUE argf)
8729 {
8730 VALUE line;
8731 long lineno = ARGF.lineno;
8732
8733 retry:
8734 if (!next_argv()) return Qnil;
8735 if (ARGF_GENERIC_INPUT_P()) {
8736 line = rb_funcall3(ARGF.current_file, idGets, argc, argv);
8737 }
8738 else {
8739 if (argc == 0 && rb_rs == rb_default_rs) {
8740 line = rb_io_gets(ARGF.current_file);
8741 }
8742 else {
8743 line = rb_io_getline(argc, argv, ARGF.current_file);
8744 }
8745 if (NIL_P(line) && ARGF.next_p != -1) {
8746 argf_close(argf);
8747 ARGF.next_p = 1;
8748 goto retry;
8749 }
8750 }
8751 if (!NIL_P(line)) {
8752 ARGF.lineno = ++lineno;
8753 ARGF.last_lineno = ARGF.lineno;
8754 }
8755 return line;
8756 }
8757
8758 static VALUE
argf_lineno_getter(ID id,VALUE * var)8759 argf_lineno_getter(ID id, VALUE *var)
8760 {
8761 VALUE argf = *var;
8762 return INT2FIX(ARGF.last_lineno);
8763 }
8764
8765 static void
argf_lineno_setter(VALUE val,ID id,VALUE * var)8766 argf_lineno_setter(VALUE val, ID id, VALUE *var)
8767 {
8768 VALUE argf = *var;
8769 int n = NUM2INT(val);
8770 ARGF.last_lineno = ARGF.lineno = n;
8771 }
8772
8773 static VALUE argf_gets(int, VALUE *, VALUE);
8774
8775 /*
8776 * call-seq:
8777 * gets(sep=$/ [, getline_args]) -> string or nil
8778 * gets(limit [, getline_args]) -> string or nil
8779 * gets(sep, limit [, getline_args]) -> string or nil
8780 *
8781 * Returns (and assigns to <code>$_</code>) the next line from the list
8782 * of files in +ARGV+ (or <code>$*</code>), or from standard input if
8783 * no files are present on the command line. Returns +nil+ at end of
8784 * file. The optional argument specifies the record separator. The
8785 * separator is included with the contents of each record. A separator
8786 * of +nil+ reads the entire contents, and a zero-length separator
8787 * reads the input one paragraph at a time, where paragraphs are
8788 * divided by two consecutive newlines. If the first argument is an
8789 * integer, or optional second argument is given, the returning string
8790 * would not be longer than the given value in bytes. If multiple
8791 * filenames are present in +ARGV+, <code>gets(nil)</code> will read
8792 * the contents one file at a time.
8793 *
8794 * ARGV << "testfile"
8795 * print while gets
8796 *
8797 * <em>produces:</em>
8798 *
8799 * This is line one
8800 * This is line two
8801 * This is line three
8802 * And so on...
8803 *
8804 * The style of programming using <code>$_</code> as an implicit
8805 * parameter is gradually losing favor in the Ruby community.
8806 */
8807
8808 static VALUE
rb_f_gets(int argc,VALUE * argv,VALUE recv)8809 rb_f_gets(int argc, VALUE *argv, VALUE recv)
8810 {
8811 if (recv == argf) {
8812 return argf_gets(argc, argv, argf);
8813 }
8814 return rb_funcallv(argf, idGets, argc, argv);
8815 }
8816
8817 /*
8818 * call-seq:
8819 * ARGF.gets(sep=$/ [, getline_args]) -> string or nil
8820 * ARGF.gets(limit [, getline_args]) -> string or nil
8821 * ARGF.gets(sep, limit [, getline_args]) -> string or nil
8822 *
8823 * Returns the next line from the current file in +ARGF+.
8824 *
8825 * By default lines are assumed to be separated by <code>$/</code>;
8826 * to use a different character as a separator, supply it as a +String+
8827 * for the _sep_ argument.
8828 *
8829 * The optional _limit_ argument specifies how many characters of each line
8830 * to return. By default all characters are returned.
8831 *
8832 * See IO.readlines for details about getline_args.
8833 *
8834 */
8835 static VALUE
argf_gets(int argc,VALUE * argv,VALUE argf)8836 argf_gets(int argc, VALUE *argv, VALUE argf)
8837 {
8838 VALUE line;
8839
8840 line = argf_getline(argc, argv, argf);
8841 rb_lastline_set(line);
8842
8843 return line;
8844 }
8845
8846 VALUE
rb_gets(void)8847 rb_gets(void)
8848 {
8849 VALUE line;
8850
8851 if (rb_rs != rb_default_rs) {
8852 return rb_f_gets(0, 0, argf);
8853 }
8854
8855 retry:
8856 if (!next_argv()) return Qnil;
8857 line = rb_io_gets(ARGF.current_file);
8858 if (NIL_P(line) && ARGF.next_p != -1) {
8859 rb_io_close(ARGF.current_file);
8860 ARGF.next_p = 1;
8861 goto retry;
8862 }
8863 rb_lastline_set(line);
8864 if (!NIL_P(line)) {
8865 ARGF.lineno++;
8866 ARGF.last_lineno = ARGF.lineno;
8867 }
8868
8869 return line;
8870 }
8871
8872 static VALUE argf_readline(int, VALUE *, VALUE);
8873
8874 /*
8875 * call-seq:
8876 * readline(sep=$/) -> string
8877 * readline(limit) -> string
8878 * readline(sep, limit) -> string
8879 *
8880 * Equivalent to <code>Kernel::gets</code>, except
8881 * +readline+ raises +EOFError+ at end of file.
8882 */
8883
8884 static VALUE
rb_f_readline(int argc,VALUE * argv,VALUE recv)8885 rb_f_readline(int argc, VALUE *argv, VALUE recv)
8886 {
8887 if (recv == argf) {
8888 return argf_readline(argc, argv, argf);
8889 }
8890 return rb_funcallv(argf, rb_intern("readline"), argc, argv);
8891 }
8892
8893
8894 /*
8895 * call-seq:
8896 * ARGF.readline(sep=$/) -> string
8897 * ARGF.readline(limit) -> string
8898 * ARGF.readline(sep, limit) -> string
8899 *
8900 * Returns the next line from the current file in +ARGF+.
8901 *
8902 * By default lines are assumed to be separated by <code>$/</code>;
8903 * to use a different character as a separator, supply it as a +String+
8904 * for the _sep_ argument.
8905 *
8906 * The optional _limit_ argument specifies how many characters of each line
8907 * to return. By default all characters are returned.
8908 *
8909 * An +EOFError+ is raised at the end of the file.
8910 */
8911 static VALUE
argf_readline(int argc,VALUE * argv,VALUE argf)8912 argf_readline(int argc, VALUE *argv, VALUE argf)
8913 {
8914 VALUE line;
8915
8916 if (!next_argv()) rb_eof_error();
8917 ARGF_FORWARD(argc, argv);
8918 line = argf_gets(argc, argv, argf);
8919 if (NIL_P(line)) {
8920 rb_eof_error();
8921 }
8922
8923 return line;
8924 }
8925
8926 static VALUE argf_readlines(int, VALUE *, VALUE);
8927
8928 /*
8929 * call-seq:
8930 * readlines(sep=$/) -> array
8931 * readlines(limit) -> array
8932 * readlines(sep, limit) -> array
8933 *
8934 * Returns an array containing the lines returned by calling
8935 * <code>Kernel.gets(<i>sep</i>)</code> until the end of file.
8936 */
8937
8938 static VALUE
rb_f_readlines(int argc,VALUE * argv,VALUE recv)8939 rb_f_readlines(int argc, VALUE *argv, VALUE recv)
8940 {
8941 if (recv == argf) {
8942 return argf_readlines(argc, argv, argf);
8943 }
8944 return rb_funcallv(argf, rb_intern("readlines"), argc, argv);
8945 }
8946
8947 /*
8948 * call-seq:
8949 * ARGF.readlines(sep=$/) -> array
8950 * ARGF.readlines(limit) -> array
8951 * ARGF.readlines(sep, limit) -> array
8952 *
8953 * ARGF.to_a(sep=$/) -> array
8954 * ARGF.to_a(limit) -> array
8955 * ARGF.to_a(sep, limit) -> array
8956 *
8957 * Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
8958 * lines, one line per element. Lines are assumed to be separated by _sep_.
8959 *
8960 * lines = ARGF.readlines
8961 * lines[0] #=> "This is line one\n"
8962 */
8963 static VALUE
argf_readlines(int argc,VALUE * argv,VALUE argf)8964 argf_readlines(int argc, VALUE *argv, VALUE argf)
8965 {
8966 long lineno = ARGF.lineno;
8967 VALUE lines, ary;
8968
8969 ary = rb_ary_new();
8970 while (next_argv()) {
8971 if (ARGF_GENERIC_INPUT_P()) {
8972 lines = rb_funcall3(ARGF.current_file, rb_intern("readlines"), argc, argv);
8973 }
8974 else {
8975 lines = rb_io_readlines(argc, argv, ARGF.current_file);
8976 argf_close(argf);
8977 }
8978 ARGF.next_p = 1;
8979 rb_ary_concat(ary, lines);
8980 ARGF.lineno = lineno + RARRAY_LEN(ary);
8981 ARGF.last_lineno = ARGF.lineno;
8982 }
8983 ARGF.init_p = 0;
8984 return ary;
8985 }
8986
8987 /*
8988 * call-seq:
8989 * `cmd` -> string
8990 *
8991 * Returns the standard output of running _cmd_ in a subshell.
8992 * The built-in syntax <code>%x{...}</code> uses
8993 * this method. Sets <code>$?</code> to the process status.
8994 *
8995 * `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
8996 * `ls testdir`.split[1] #=> "main.rb"
8997 * `echo oops && exit 99` #=> "oops\n"
8998 * $?.exitstatus #=> 99
8999 */
9000
9001 static VALUE
rb_f_backquote(VALUE obj,VALUE str)9002 rb_f_backquote(VALUE obj, VALUE str)
9003 {
9004 VALUE port;
9005 VALUE result;
9006 rb_io_t *fptr;
9007
9008 SafeStringValue(str);
9009 rb_last_status_clear();
9010 port = pipe_open_s(str, "r", FMODE_READABLE|DEFAULT_TEXTMODE, NULL);
9011 if (NIL_P(port)) return rb_str_new(0,0);
9012
9013 GetOpenFile(port, fptr);
9014 result = read_all(fptr, remain_size(fptr), Qnil);
9015 rb_io_close(port);
9016 RFILE(port)->fptr = NULL;
9017 rb_io_fptr_finalize(fptr);
9018 rb_gc_force_recycle(port); /* also guards from premature GC */
9019
9020 return result;
9021 }
9022
9023 #ifdef HAVE_SYS_SELECT_H
9024 #include <sys/select.h>
9025 #endif
9026
9027 static VALUE
select_internal(VALUE read,VALUE write,VALUE except,struct timeval * tp,rb_fdset_t * fds)9028 select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fdset_t *fds)
9029 {
9030 VALUE res, list;
9031 rb_fdset_t *rp, *wp, *ep;
9032 rb_io_t *fptr;
9033 long i;
9034 int max = 0, n;
9035 int pending = 0;
9036 struct timeval timerec;
9037
9038 if (!NIL_P(read)) {
9039 Check_Type(read, T_ARRAY);
9040 for (i=0; i<RARRAY_LEN(read); i++) {
9041 GetOpenFile(rb_io_get_io(RARRAY_AREF(read, i)), fptr);
9042 rb_fd_set(fptr->fd, &fds[0]);
9043 if (READ_DATA_PENDING(fptr) || READ_CHAR_PENDING(fptr)) { /* check for buffered data */
9044 pending++;
9045 rb_fd_set(fptr->fd, &fds[3]);
9046 }
9047 if (max < fptr->fd) max = fptr->fd;
9048 }
9049 if (pending) { /* no blocking if there's buffered data */
9050 timerec.tv_sec = timerec.tv_usec = 0;
9051 tp = &timerec;
9052 }
9053 rp = &fds[0];
9054 }
9055 else
9056 rp = 0;
9057
9058 if (!NIL_P(write)) {
9059 Check_Type(write, T_ARRAY);
9060 for (i=0; i<RARRAY_LEN(write); i++) {
9061 VALUE write_io = GetWriteIO(rb_io_get_io(RARRAY_AREF(write, i)));
9062 GetOpenFile(write_io, fptr);
9063 rb_fd_set(fptr->fd, &fds[1]);
9064 if (max < fptr->fd) max = fptr->fd;
9065 }
9066 wp = &fds[1];
9067 }
9068 else
9069 wp = 0;
9070
9071 if (!NIL_P(except)) {
9072 Check_Type(except, T_ARRAY);
9073 for (i=0; i<RARRAY_LEN(except); i++) {
9074 VALUE io = rb_io_get_io(RARRAY_AREF(except, i));
9075 VALUE write_io = GetWriteIO(io);
9076 GetOpenFile(io, fptr);
9077 rb_fd_set(fptr->fd, &fds[2]);
9078 if (max < fptr->fd) max = fptr->fd;
9079 if (io != write_io) {
9080 GetOpenFile(write_io, fptr);
9081 rb_fd_set(fptr->fd, &fds[2]);
9082 if (max < fptr->fd) max = fptr->fd;
9083 }
9084 }
9085 ep = &fds[2];
9086 }
9087 else {
9088 ep = 0;
9089 }
9090
9091 max++;
9092
9093 n = rb_thread_fd_select(max, rp, wp, ep, tp);
9094 if (n < 0) {
9095 rb_sys_fail(0);
9096 }
9097 if (!pending && n == 0) return Qnil; /* returns nil on timeout */
9098
9099 res = rb_ary_new2(3);
9100 rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
9101 rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
9102 rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
9103
9104 if (rp) {
9105 list = RARRAY_AREF(res, 0);
9106 for (i=0; i< RARRAY_LEN(read); i++) {
9107 VALUE obj = rb_ary_entry(read, i);
9108 VALUE io = rb_io_get_io(obj);
9109 GetOpenFile(io, fptr);
9110 if (rb_fd_isset(fptr->fd, &fds[0]) ||
9111 rb_fd_isset(fptr->fd, &fds[3])) {
9112 rb_ary_push(list, obj);
9113 }
9114 }
9115 }
9116
9117 if (wp) {
9118 list = RARRAY_AREF(res, 1);
9119 for (i=0; i< RARRAY_LEN(write); i++) {
9120 VALUE obj = rb_ary_entry(write, i);
9121 VALUE io = rb_io_get_io(obj);
9122 VALUE write_io = GetWriteIO(io);
9123 GetOpenFile(write_io, fptr);
9124 if (rb_fd_isset(fptr->fd, &fds[1])) {
9125 rb_ary_push(list, obj);
9126 }
9127 }
9128 }
9129
9130 if (ep) {
9131 list = RARRAY_AREF(res, 2);
9132 for (i=0; i< RARRAY_LEN(except); i++) {
9133 VALUE obj = rb_ary_entry(except, i);
9134 VALUE io = rb_io_get_io(obj);
9135 VALUE write_io = GetWriteIO(io);
9136 GetOpenFile(io, fptr);
9137 if (rb_fd_isset(fptr->fd, &fds[2])) {
9138 rb_ary_push(list, obj);
9139 }
9140 else if (io != write_io) {
9141 GetOpenFile(write_io, fptr);
9142 if (rb_fd_isset(fptr->fd, &fds[2])) {
9143 rb_ary_push(list, obj);
9144 }
9145 }
9146 }
9147 }
9148
9149 return res; /* returns an empty array on interrupt */
9150 }
9151
9152 struct select_args {
9153 VALUE read, write, except;
9154 struct timeval *timeout;
9155 rb_fdset_t fdsets[4];
9156 };
9157
9158 static VALUE
select_call(VALUE arg)9159 select_call(VALUE arg)
9160 {
9161 struct select_args *p = (struct select_args *)arg;
9162
9163 return select_internal(p->read, p->write, p->except, p->timeout, p->fdsets);
9164 }
9165
9166 static VALUE
select_end(VALUE arg)9167 select_end(VALUE arg)
9168 {
9169 struct select_args *p = (struct select_args *)arg;
9170 int i;
9171
9172 for (i = 0; i < numberof(p->fdsets); ++i)
9173 rb_fd_term(&p->fdsets[i]);
9174 return Qnil;
9175 }
9176
9177 static VALUE sym_normal, sym_sequential, sym_random,
9178 sym_willneed, sym_dontneed, sym_noreuse;
9179
9180 #ifdef HAVE_POSIX_FADVISE
9181 struct io_advise_struct {
9182 int fd;
9183 int advice;
9184 off_t offset;
9185 off_t len;
9186 };
9187
9188 static VALUE
io_advise_internal(void * arg)9189 io_advise_internal(void *arg)
9190 {
9191 struct io_advise_struct *ptr = arg;
9192 return posix_fadvise(ptr->fd, ptr->offset, ptr->len, ptr->advice);
9193 }
9194
9195 static VALUE
io_advise_sym_to_const(VALUE sym)9196 io_advise_sym_to_const(VALUE sym)
9197 {
9198 #ifdef POSIX_FADV_NORMAL
9199 if (sym == sym_normal)
9200 return INT2NUM(POSIX_FADV_NORMAL);
9201 #endif
9202
9203 #ifdef POSIX_FADV_RANDOM
9204 if (sym == sym_random)
9205 return INT2NUM(POSIX_FADV_RANDOM);
9206 #endif
9207
9208 #ifdef POSIX_FADV_SEQUENTIAL
9209 if (sym == sym_sequential)
9210 return INT2NUM(POSIX_FADV_SEQUENTIAL);
9211 #endif
9212
9213 #ifdef POSIX_FADV_WILLNEED
9214 if (sym == sym_willneed)
9215 return INT2NUM(POSIX_FADV_WILLNEED);
9216 #endif
9217
9218 #ifdef POSIX_FADV_DONTNEED
9219 if (sym == sym_dontneed)
9220 return INT2NUM(POSIX_FADV_DONTNEED);
9221 #endif
9222
9223 #ifdef POSIX_FADV_NOREUSE
9224 if (sym == sym_noreuse)
9225 return INT2NUM(POSIX_FADV_NOREUSE);
9226 #endif
9227
9228 return Qnil;
9229 }
9230
9231 static VALUE
do_io_advise(rb_io_t * fptr,VALUE advice,off_t offset,off_t len)9232 do_io_advise(rb_io_t *fptr, VALUE advice, off_t offset, off_t len)
9233 {
9234 int rv;
9235 struct io_advise_struct ias;
9236 VALUE num_adv;
9237
9238 num_adv = io_advise_sym_to_const(advice);
9239
9240 /*
9241 * The platform doesn't support this hint. We don't raise exception, instead
9242 * silently ignore it. Because IO::advise is only hint.
9243 */
9244 if (NIL_P(num_adv))
9245 return Qnil;
9246
9247 ias.fd = fptr->fd;
9248 ias.advice = NUM2INT(num_adv);
9249 ias.offset = offset;
9250 ias.len = len;
9251
9252 rv = (int)rb_thread_io_blocking_region(io_advise_internal, &ias, fptr->fd);
9253 if (rv && rv != ENOSYS) {
9254 /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
9255 it returns the error code. */
9256 VALUE message = rb_sprintf("%"PRIsVALUE" "
9257 "(%"PRI_OFFT_PREFIX"d, "
9258 "%"PRI_OFFT_PREFIX"d, "
9259 "%"PRIsVALUE")",
9260 fptr->pathv, offset, len, advice);
9261 rb_syserr_fail_str(rv, message);
9262 }
9263
9264 return Qnil;
9265 }
9266
9267 #endif /* HAVE_POSIX_FADVISE */
9268
9269 static void
advice_arg_check(VALUE advice)9270 advice_arg_check(VALUE advice)
9271 {
9272 if (!SYMBOL_P(advice))
9273 rb_raise(rb_eTypeError, "advice must be a Symbol");
9274
9275 if (advice != sym_normal &&
9276 advice != sym_sequential &&
9277 advice != sym_random &&
9278 advice != sym_willneed &&
9279 advice != sym_dontneed &&
9280 advice != sym_noreuse) {
9281 rb_raise(rb_eNotImpError, "Unsupported advice: %+"PRIsVALUE, advice);
9282 }
9283 }
9284
9285 /*
9286 * call-seq:
9287 * ios.advise(advice, offset=0, len=0) -> nil
9288 *
9289 * Announce an intention to access data from the current file in a
9290 * specific pattern. On platforms that do not support the
9291 * <em>posix_fadvise(2)</em> system call, this method is a no-op.
9292 *
9293 * _advice_ is one of the following symbols:
9294 *
9295 * :normal:: No advice to give; the default assumption for an open file.
9296 * :sequential:: The data will be accessed sequentially
9297 * with lower offsets read before higher ones.
9298 * :random:: The data will be accessed in random order.
9299 * :willneed:: The data will be accessed in the near future.
9300 * :dontneed:: The data will not be accessed in the near future.
9301 * :noreuse:: The data will only be accessed once.
9302 *
9303 * The semantics of a piece of advice are platform-dependent. See
9304 * <em>man 2 posix_fadvise</em> for details.
9305 *
9306 * "data" means the region of the current file that begins at
9307 * _offset_ and extends for _len_ bytes. If _len_ is 0, the region
9308 * ends at the last byte of the file. By default, both _offset_ and
9309 * _len_ are 0, meaning that the advice applies to the entire file.
9310 *
9311 * If an error occurs, one of the following exceptions will be raised:
9312 *
9313 * <code>IOError</code>:: The <code>IO</code> stream is closed.
9314 * <code>Errno::EBADF</code>::
9315 * The file descriptor of the current file is invalid.
9316 * <code>Errno::EINVAL</code>:: An invalid value for _advice_ was given.
9317 * <code>Errno::ESPIPE</code>::
9318 * The file descriptor of the current file refers to a FIFO or
9319 * pipe. (Linux raises <code>Errno::EINVAL</code> in this case).
9320 * <code>TypeError</code>::
9321 * Either _advice_ was not a Symbol, or one of the
9322 * other arguments was not an <code>Integer</code>.
9323 * <code>RangeError</code>:: One of the arguments given was too big/small.
9324 *
9325 * This list is not exhaustive; other Errno:: exceptions are also possible.
9326 */
9327 static VALUE
rb_io_advise(int argc,VALUE * argv,VALUE io)9328 rb_io_advise(int argc, VALUE *argv, VALUE io)
9329 {
9330 VALUE advice, offset, len;
9331 off_t off, l;
9332 rb_io_t *fptr;
9333
9334 rb_scan_args(argc, argv, "12", &advice, &offset, &len);
9335 advice_arg_check(advice);
9336
9337 io = GetWriteIO(io);
9338 GetOpenFile(io, fptr);
9339
9340 off = NIL_P(offset) ? 0 : NUM2OFFT(offset);
9341 l = NIL_P(len) ? 0 : NUM2OFFT(len);
9342
9343 #ifdef HAVE_POSIX_FADVISE
9344 return do_io_advise(fptr, advice, off, l);
9345 #else
9346 ((void)off, (void)l); /* Ignore all hint */
9347 return Qnil;
9348 #endif
9349 }
9350
9351 /*
9352 * call-seq:
9353 * IO.select(read_array [, write_array [, error_array [, timeout]]]) -> array or nil
9354 *
9355 * Calls select(2) system call.
9356 * It monitors given arrays of <code>IO</code> objects, waits until one or more
9357 * of <code>IO</code> objects are ready for reading, are ready for writing,
9358 * and have pending exceptions respectively, and returns an array that
9359 * contains arrays of those IO objects. It will return +nil+
9360 * if optional <i>timeout</i> value is given and no <code>IO</code> object
9361 * is ready in <i>timeout</i> seconds.
9362 *
9363 * <code>IO.select</code> peeks the buffer of <code>IO</code> objects for testing readability.
9364 * If the <code>IO</code> buffer is not empty,
9365 * <code>IO.select</code> immediately notifies readability.
9366 * This "peek" only happens for <code>IO</code> objects.
9367 * It does not happen for IO-like objects such as OpenSSL::SSL::SSLSocket.
9368 *
9369 * The best way to use <code>IO.select</code> is invoking it
9370 * after nonblocking methods such as <code>read_nonblock</code>, <code>write_nonblock</code>, etc.
9371 * The methods raise an exception which is extended by
9372 * <code>IO::WaitReadable</code> or <code>IO::WaitWritable</code>.
9373 * The modules notify how the caller should wait with <code>IO.select</code>.
9374 * If <code>IO::WaitReadable</code> is raised, the caller should wait for reading.
9375 * If <code>IO::WaitWritable</code> is raised, the caller should wait for writing.
9376 *
9377 * So, blocking read (<code>readpartial</code>) can be emulated using
9378 * <code>read_nonblock</code> and <code>IO.select</code> as follows:
9379 *
9380 * begin
9381 * result = io_like.read_nonblock(maxlen)
9382 * rescue IO::WaitReadable
9383 * IO.select([io_like])
9384 * retry
9385 * rescue IO::WaitWritable
9386 * IO.select(nil, [io_like])
9387 * retry
9388 * end
9389 *
9390 * Especially, the combination of nonblocking methods and
9391 * <code>IO.select</code> is preferred for <code>IO</code> like
9392 * objects such as <code>OpenSSL::SSL::SSLSocket</code>.
9393 * It has <code>to_io</code> method to return underlying <code>IO</code> object.
9394 * <code>IO.select</code> calls <code>to_io</code> to obtain the file descriptor to wait.
9395 *
9396 * This means that readability notified by <code>IO.select</code> doesn't mean
9397 * readability from <code>OpenSSL::SSL::SSLSocket</code> object.
9398 *
9399 * The most likely situation is that <code>OpenSSL::SSL::SSLSocket</code> buffers some data.
9400 * <code>IO.select</code> doesn't see the buffer.
9401 * So <code>IO.select</code> can block when <code>OpenSSL::SSL::SSLSocket#readpartial</code> doesn't block.
9402 *
9403 * However, several more complicated situations exist.
9404 *
9405 * SSL is a protocol which is sequence of records.
9406 * The record consists of multiple bytes.
9407 * So, the remote side of SSL sends a partial record,
9408 * <code>IO.select</code> notifies readability but
9409 * <code>OpenSSL::SSL::SSLSocket</code> cannot decrypt a byte and
9410 * <code>OpenSSL::SSL::SSLSocket#readpartial</code> will block.
9411 *
9412 * Also, the remote side can request SSL renegotiation which forces
9413 * the local SSL engine to write some data.
9414 * This means <code>OpenSSL::SSL::SSLSocket#readpartial</code> may
9415 * invoke <code>write</code> system call and it can block.
9416 * In such a situation, <code>OpenSSL::SSL::SSLSocket#read_nonblock</code>
9417 * raises IO::WaitWritable instead of blocking.
9418 * So, the caller should wait for ready for writability as above example.
9419 *
9420 * The combination of nonblocking methods and <code>IO.select</code> is
9421 * also useful for streams such as tty, pipe socket socket when
9422 * multiple processes read from a stream.
9423 *
9424 * Finally, Linux kernel developers don't guarantee that
9425 * readability of select(2) means readability of following read(2) even
9426 * for a single process.
9427 * See select(2) manual on GNU/Linux system.
9428 *
9429 * Invoking <code>IO.select</code> before <code>IO#readpartial</code> works well as usual.
9430 * However it is not the best way to use <code>IO.select</code>.
9431 *
9432 * The writability notified by select(2) doesn't show
9433 * how many bytes are writable.
9434 * <code>IO#write</code> method blocks until given whole string is written.
9435 * So, <code>IO#write(two or more bytes)</code> can block after writability is notified by <code>IO.select</code>.
9436 * <code>IO#write_nonblock</code> is required to avoid the blocking.
9437 *
9438 * Blocking write (<code>write</code>) can be emulated using
9439 * <code>write_nonblock</code> and <code>IO.select</code> as follows:
9440 * IO::WaitReadable should also be rescued for SSL renegotiation in <code>OpenSSL::SSL::SSLSocket</code>.
9441 *
9442 * while 0 < string.bytesize
9443 * begin
9444 * written = io_like.write_nonblock(string)
9445 * rescue IO::WaitReadable
9446 * IO.select([io_like])
9447 * retry
9448 * rescue IO::WaitWritable
9449 * IO.select(nil, [io_like])
9450 * retry
9451 * end
9452 * string = string.byteslice(written..-1)
9453 * end
9454 *
9455 * === Parameters
9456 * read_array:: an array of <code>IO</code> objects that wait until ready for read
9457 * write_array:: an array of <code>IO</code> objects that wait until ready for write
9458 * error_array:: an array of <code>IO</code> objects that wait for exceptions
9459 * timeout:: a numeric value in second
9460 *
9461 * === Example
9462 *
9463 * rp, wp = IO.pipe
9464 * mesg = "ping "
9465 * 100.times {
9466 * # IO.select follows IO#read. Not the best way to use IO.select.
9467 * rs, ws, = IO.select([rp], [wp])
9468 * if r = rs[0]
9469 * ret = r.read(5)
9470 * print ret
9471 * case ret
9472 * when /ping/
9473 * mesg = "pong\n"
9474 * when /pong/
9475 * mesg = "ping "
9476 * end
9477 * end
9478 * if w = ws[0]
9479 * w.write(mesg)
9480 * end
9481 * }
9482 *
9483 * <em>produces:</em>
9484 *
9485 * ping pong
9486 * ping pong
9487 * ping pong
9488 * (snipped)
9489 * ping
9490 */
9491
9492 static VALUE
rb_f_select(int argc,VALUE * argv,VALUE obj)9493 rb_f_select(int argc, VALUE *argv, VALUE obj)
9494 {
9495 VALUE timeout;
9496 struct select_args args;
9497 struct timeval timerec;
9498 int i;
9499
9500 rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
9501 if (NIL_P(timeout)) {
9502 args.timeout = 0;
9503 }
9504 else {
9505 timerec = rb_time_interval(timeout);
9506 args.timeout = &timerec;
9507 }
9508
9509 for (i = 0; i < numberof(args.fdsets); ++i)
9510 rb_fd_init(&args.fdsets[i]);
9511
9512 return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
9513 }
9514
9515 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
9516 typedef unsigned long ioctl_req_t;
9517 # define NUM2IOCTLREQ(num) NUM2ULONG(num)
9518 #else
9519 typedef int ioctl_req_t;
9520 # define NUM2IOCTLREQ(num) NUM2INT(num)
9521 #endif
9522
9523 #ifdef HAVE_IOCTL
9524 struct ioctl_arg {
9525 int fd;
9526 ioctl_req_t cmd;
9527 long narg;
9528 };
9529
9530 static VALUE
nogvl_ioctl(void * ptr)9531 nogvl_ioctl(void *ptr)
9532 {
9533 struct ioctl_arg *arg = ptr;
9534
9535 return (VALUE)ioctl(arg->fd, arg->cmd, arg->narg);
9536 }
9537
9538 static int
do_ioctl(int fd,ioctl_req_t cmd,long narg)9539 do_ioctl(int fd, ioctl_req_t cmd, long narg)
9540 {
9541 int retval;
9542 struct ioctl_arg arg;
9543
9544 arg.fd = fd;
9545 arg.cmd = cmd;
9546 arg.narg = narg;
9547
9548 retval = (int)rb_thread_io_blocking_region(nogvl_ioctl, &arg, fd);
9549
9550 return retval;
9551 }
9552 #endif
9553
9554 #define DEFULT_IOCTL_NARG_LEN (256)
9555
9556 #if defined(__linux__) && defined(_IOC_SIZE)
9557 static long
linux_iocparm_len(ioctl_req_t cmd)9558 linux_iocparm_len(ioctl_req_t cmd)
9559 {
9560 long len;
9561
9562 if ((cmd & 0xFFFF0000) == 0) {
9563 /* legacy and unstructured ioctl number. */
9564 return DEFULT_IOCTL_NARG_LEN;
9565 }
9566
9567 len = _IOC_SIZE(cmd);
9568
9569 /* paranoia check for silly drivers which don't keep ioctl convention */
9570 if (len < DEFULT_IOCTL_NARG_LEN)
9571 len = DEFULT_IOCTL_NARG_LEN;
9572
9573 return len;
9574 }
9575 #endif
9576
9577 static long
ioctl_narg_len(ioctl_req_t cmd)9578 ioctl_narg_len(ioctl_req_t cmd)
9579 {
9580 long len;
9581
9582 #ifdef IOCPARM_MASK
9583 #ifndef IOCPARM_LEN
9584 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
9585 #endif
9586 #endif
9587 #ifdef IOCPARM_LEN
9588 len = IOCPARM_LEN(cmd); /* on BSDish systems we're safe */
9589 #elif defined(__linux__) && defined(_IOC_SIZE)
9590 len = linux_iocparm_len(cmd);
9591 #else
9592 /* otherwise guess at what's safe */
9593 len = DEFULT_IOCTL_NARG_LEN;
9594 #endif
9595
9596 return len;
9597 }
9598
9599 #ifdef HAVE_FCNTL
9600 #ifdef __linux__
9601 typedef long fcntl_arg_t;
9602 #else
9603 /* posix */
9604 typedef int fcntl_arg_t;
9605 #endif
9606
9607 static long
fcntl_narg_len(int cmd)9608 fcntl_narg_len(int cmd)
9609 {
9610 long len;
9611
9612 switch (cmd) {
9613 #ifdef F_DUPFD
9614 case F_DUPFD:
9615 len = sizeof(fcntl_arg_t);
9616 break;
9617 #endif
9618 #ifdef F_DUP2FD /* bsd specific */
9619 case F_DUP2FD:
9620 len = sizeof(int);
9621 break;
9622 #endif
9623 #ifdef F_DUPFD_CLOEXEC /* linux specific */
9624 case F_DUPFD_CLOEXEC:
9625 len = sizeof(fcntl_arg_t);
9626 break;
9627 #endif
9628 #ifdef F_GETFD
9629 case F_GETFD:
9630 len = 1;
9631 break;
9632 #endif
9633 #ifdef F_SETFD
9634 case F_SETFD:
9635 len = sizeof(fcntl_arg_t);
9636 break;
9637 #endif
9638 #ifdef F_GETFL
9639 case F_GETFL:
9640 len = 1;
9641 break;
9642 #endif
9643 #ifdef F_SETFL
9644 case F_SETFL:
9645 len = sizeof(fcntl_arg_t);
9646 break;
9647 #endif
9648 #ifdef F_GETOWN
9649 case F_GETOWN:
9650 len = 1;
9651 break;
9652 #endif
9653 #ifdef F_SETOWN
9654 case F_SETOWN:
9655 len = sizeof(fcntl_arg_t);
9656 break;
9657 #endif
9658 #ifdef F_GETOWN_EX /* linux specific */
9659 case F_GETOWN_EX:
9660 len = sizeof(struct f_owner_ex);
9661 break;
9662 #endif
9663 #ifdef F_SETOWN_EX /* linux specific */
9664 case F_SETOWN_EX:
9665 len = sizeof(struct f_owner_ex);
9666 break;
9667 #endif
9668 #ifdef F_GETLK
9669 case F_GETLK:
9670 len = sizeof(struct flock);
9671 break;
9672 #endif
9673 #ifdef F_SETLK
9674 case F_SETLK:
9675 len = sizeof(struct flock);
9676 break;
9677 #endif
9678 #ifdef F_SETLKW
9679 case F_SETLKW:
9680 len = sizeof(struct flock);
9681 break;
9682 #endif
9683 #ifdef F_READAHEAD /* bsd specific */
9684 case F_READAHEAD:
9685 len = sizeof(int);
9686 break;
9687 #endif
9688 #ifdef F_RDAHEAD /* Darwin specific */
9689 case F_RDAHEAD:
9690 len = sizeof(int);
9691 break;
9692 #endif
9693 #ifdef F_GETSIG /* linux specific */
9694 case F_GETSIG:
9695 len = 1;
9696 break;
9697 #endif
9698 #ifdef F_SETSIG /* linux specific */
9699 case F_SETSIG:
9700 len = sizeof(fcntl_arg_t);
9701 break;
9702 #endif
9703 #ifdef F_GETLEASE /* linux specific */
9704 case F_GETLEASE:
9705 len = 1;
9706 break;
9707 #endif
9708 #ifdef F_SETLEASE /* linux specific */
9709 case F_SETLEASE:
9710 len = sizeof(fcntl_arg_t);
9711 break;
9712 #endif
9713 #ifdef F_NOTIFY /* linux specific */
9714 case F_NOTIFY:
9715 len = sizeof(fcntl_arg_t);
9716 break;
9717 #endif
9718
9719 default:
9720 len = 256;
9721 break;
9722 }
9723
9724 return len;
9725 }
9726 #else /* HAVE_FCNTL */
9727 static long
fcntl_narg_len(int cmd)9728 fcntl_narg_len(int cmd)
9729 {
9730 return 0;
9731 }
9732 #endif /* HAVE_FCNTL */
9733
9734 static long
setup_narg(ioctl_req_t cmd,VALUE * argp,int io_p)9735 setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p)
9736 {
9737 long narg = 0;
9738 VALUE arg = *argp;
9739
9740 if (NIL_P(arg) || arg == Qfalse) {
9741 narg = 0;
9742 }
9743 else if (FIXNUM_P(arg)) {
9744 narg = FIX2LONG(arg);
9745 }
9746 else if (arg == Qtrue) {
9747 narg = 1;
9748 }
9749 else {
9750 VALUE tmp = rb_check_string_type(arg);
9751
9752 if (NIL_P(tmp)) {
9753 narg = NUM2LONG(arg);
9754 }
9755 else {
9756 char *ptr;
9757 long len, slen;
9758
9759 *argp = arg = tmp;
9760 if (io_p)
9761 len = ioctl_narg_len(cmd);
9762 else
9763 len = fcntl_narg_len((int)cmd);
9764 rb_str_modify(arg);
9765
9766 slen = RSTRING_LEN(arg);
9767 /* expand for data + sentinel. */
9768 if (slen < len+1) {
9769 rb_str_resize(arg, len+1);
9770 MEMZERO(RSTRING_PTR(arg)+slen, char, len-slen);
9771 slen = len+1;
9772 }
9773 /* a little sanity check here */
9774 ptr = RSTRING_PTR(arg);
9775 ptr[slen - 1] = 17;
9776 narg = (long)(SIGNED_VALUE)ptr;
9777 }
9778 }
9779
9780 return narg;
9781 }
9782
9783 #ifdef HAVE_IOCTL
9784 static VALUE
rb_ioctl(VALUE io,VALUE req,VALUE arg)9785 rb_ioctl(VALUE io, VALUE req, VALUE arg)
9786 {
9787 ioctl_req_t cmd = NUM2IOCTLREQ(req);
9788 rb_io_t *fptr;
9789 long narg;
9790 int retval;
9791
9792 narg = setup_narg(cmd, &arg, 1);
9793 GetOpenFile(io, fptr);
9794 retval = do_ioctl(fptr->fd, cmd, narg);
9795 if (retval < 0) rb_sys_fail_path(fptr->pathv);
9796 if (RB_TYPE_P(arg, T_STRING)) {
9797 char *ptr;
9798 long slen;
9799 RSTRING_GETMEM(arg, ptr, slen);
9800 if (ptr[slen-1] != 17)
9801 rb_raise(rb_eArgError, "return value overflowed string");
9802 ptr[slen-1] = '\0';
9803 }
9804
9805 return INT2NUM(retval);
9806 }
9807
9808 /*
9809 * call-seq:
9810 * ios.ioctl(integer_cmd, arg) -> integer
9811 *
9812 * Provides a mechanism for issuing low-level commands to control or
9813 * query I/O devices. Arguments and results are platform dependent. If
9814 * <i>arg</i> is a number, its value is passed directly. If it is a
9815 * string, it is interpreted as a binary sequence of bytes. On Unix
9816 * platforms, see <code>ioctl(2)</code> for details. Not implemented on
9817 * all platforms.
9818 */
9819
9820 static VALUE
rb_io_ioctl(int argc,VALUE * argv,VALUE io)9821 rb_io_ioctl(int argc, VALUE *argv, VALUE io)
9822 {
9823 VALUE req, arg;
9824
9825 rb_scan_args(argc, argv, "11", &req, &arg);
9826 return rb_ioctl(io, req, arg);
9827 }
9828 #else
9829 #define rb_io_ioctl rb_f_notimplement
9830 #endif
9831
9832 #ifdef HAVE_FCNTL
9833 struct fcntl_arg {
9834 int fd;
9835 int cmd;
9836 long narg;
9837 };
9838
9839 static VALUE
nogvl_fcntl(void * ptr)9840 nogvl_fcntl(void *ptr)
9841 {
9842 struct fcntl_arg *arg = ptr;
9843
9844 #if defined(F_DUPFD)
9845 if (arg->cmd == F_DUPFD)
9846 return (VALUE)rb_cloexec_fcntl_dupfd(arg->fd, (int)arg->narg);
9847 #endif
9848 return (VALUE)fcntl(arg->fd, arg->cmd, arg->narg);
9849 }
9850
9851 static int
do_fcntl(int fd,int cmd,long narg)9852 do_fcntl(int fd, int cmd, long narg)
9853 {
9854 int retval;
9855 struct fcntl_arg arg;
9856
9857 arg.fd = fd;
9858 arg.cmd = cmd;
9859 arg.narg = narg;
9860
9861 retval = (int)rb_thread_io_blocking_region(nogvl_fcntl, &arg, fd);
9862 if (retval != -1) {
9863 switch (cmd) {
9864 #if defined(F_DUPFD)
9865 case F_DUPFD:
9866 #endif
9867 #if defined(F_DUPFD_CLOEXEC)
9868 case F_DUPFD_CLOEXEC:
9869 #endif
9870 rb_update_max_fd(retval);
9871 }
9872 }
9873
9874 return retval;
9875 }
9876
9877 static VALUE
rb_fcntl(VALUE io,VALUE req,VALUE arg)9878 rb_fcntl(VALUE io, VALUE req, VALUE arg)
9879 {
9880 int cmd = NUM2INT(req);
9881 rb_io_t *fptr;
9882 long narg;
9883 int retval;
9884
9885 narg = setup_narg(cmd, &arg, 0);
9886 GetOpenFile(io, fptr);
9887 retval = do_fcntl(fptr->fd, cmd, narg);
9888 if (retval < 0) rb_sys_fail_path(fptr->pathv);
9889 if (RB_TYPE_P(arg, T_STRING)) {
9890 char *ptr;
9891 long slen;
9892 RSTRING_GETMEM(arg, ptr, slen);
9893 if (ptr[slen-1] != 17)
9894 rb_raise(rb_eArgError, "return value overflowed string");
9895 ptr[slen-1] = '\0';
9896 }
9897
9898 return INT2NUM(retval);
9899 }
9900
9901 /*
9902 * call-seq:
9903 * ios.fcntl(integer_cmd, arg) -> integer
9904 *
9905 * Provides a mechanism for issuing low-level commands to control or
9906 * query file-oriented I/O streams. Arguments and results are platform
9907 * dependent. If <i>arg</i> is a number, its value is passed
9908 * directly. If it is a string, it is interpreted as a binary sequence
9909 * of bytes (<code>Array#pack</code> might be a useful way to build this
9910 * string). On Unix platforms, see <code>fcntl(2)</code> for details.
9911 * Not implemented on all platforms.
9912 */
9913
9914 static VALUE
rb_io_fcntl(int argc,VALUE * argv,VALUE io)9915 rb_io_fcntl(int argc, VALUE *argv, VALUE io)
9916 {
9917 VALUE req, arg;
9918
9919 rb_scan_args(argc, argv, "11", &req, &arg);
9920 return rb_fcntl(io, req, arg);
9921 }
9922 #else
9923 #define rb_io_fcntl rb_f_notimplement
9924 #endif
9925
9926 #if defined(HAVE_SYSCALL) || defined(HAVE___SYSCALL)
9927 /*
9928 * call-seq:
9929 * syscall(num [, args...]) -> integer
9930 *
9931 * Calls the operating system function identified by _num_ and
9932 * returns the result of the function or raises SystemCallError if
9933 * it failed.
9934 *
9935 * Arguments for the function can follow _num_. They must be either
9936 * +String+ objects or +Integer+ objects. A +String+ object is passed
9937 * as a pointer to the byte sequence. An +Integer+ object is passed
9938 * as an integer whose bit size is same as a pointer.
9939 * Up to nine parameters may be passed.
9940 *
9941 * The function identified by _num_ is system
9942 * dependent. On some Unix systems, the numbers may be obtained from a
9943 * header file called <code>syscall.h</code>.
9944 *
9945 * syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
9946 *
9947 * <em>produces:</em>
9948 *
9949 * hello
9950 *
9951 * Calling +syscall+ on a platform which does not have any way to
9952 * an arbitrary system function just fails with NotImplementedError.
9953 *
9954 * *Note:*
9955 * +syscall+ is essentially unsafe and unportable.
9956 * Feel free to shoot your foot.
9957 * The DL (Fiddle) library is preferred for safer and a bit
9958 * more portable programming.
9959 */
9960
9961 static VALUE
rb_f_syscall(int argc,VALUE * argv)9962 rb_f_syscall(int argc, VALUE *argv)
9963 {
9964 VALUE arg[8];
9965 #if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */
9966 # define SYSCALL __syscall
9967 # define NUM2SYSCALLID(x) NUM2LONG(x)
9968 # define RETVAL2NUM(x) LONG2NUM(x)
9969 # if SIZEOF_LONG == 8
9970 long num, retval = -1;
9971 # elif SIZEOF_LONG_LONG == 8
9972 long long num, retval = -1;
9973 # else
9974 # error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
9975 # endif
9976 #elif defined(__linux__)
9977 # define SYSCALL syscall
9978 # define NUM2SYSCALLID(x) NUM2LONG(x)
9979 # define RETVAL2NUM(x) LONG2NUM(x)
9980 /*
9981 * Linux man page says, syscall(2) function prototype is below.
9982 *
9983 * int syscall(int number, ...);
9984 *
9985 * But, it's incorrect. Actual one takes and returned long. (see unistd.h)
9986 */
9987 long num, retval = -1;
9988 #else
9989 # define SYSCALL syscall
9990 # define NUM2SYSCALLID(x) NUM2INT(x)
9991 # define RETVAL2NUM(x) INT2NUM(x)
9992 int num, retval = -1;
9993 #endif
9994 int i;
9995
9996 if (RTEST(ruby_verbose)) {
9997 rb_warning("We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
9998 }
9999
10000 if (argc == 0)
10001 rb_raise(rb_eArgError, "too few arguments for syscall");
10002 if (argc > numberof(arg))
10003 rb_raise(rb_eArgError, "too many arguments for syscall");
10004 num = NUM2SYSCALLID(argv[0]); ++argv;
10005 for (i = argc - 1; i--; ) {
10006 VALUE v = rb_check_string_type(argv[i]);
10007
10008 if (!NIL_P(v)) {
10009 SafeStringValue(v);
10010 rb_str_modify(v);
10011 arg[i] = (VALUE)StringValueCStr(v);
10012 }
10013 else {
10014 arg[i] = (VALUE)NUM2LONG(argv[i]);
10015 }
10016 }
10017
10018 switch (argc) {
10019 case 1:
10020 retval = SYSCALL(num);
10021 break;
10022 case 2:
10023 retval = SYSCALL(num, arg[0]);
10024 break;
10025 case 3:
10026 retval = SYSCALL(num, arg[0],arg[1]);
10027 break;
10028 case 4:
10029 retval = SYSCALL(num, arg[0],arg[1],arg[2]);
10030 break;
10031 case 5:
10032 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
10033 break;
10034 case 6:
10035 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
10036 break;
10037 case 7:
10038 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
10039 break;
10040 case 8:
10041 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
10042 break;
10043 }
10044
10045 if (retval == -1)
10046 rb_sys_fail(0);
10047 return RETVAL2NUM(retval);
10048 #undef SYSCALL
10049 #undef NUM2SYSCALLID
10050 #undef RETVAL2NUM
10051 }
10052 #else
10053 #define rb_f_syscall rb_f_notimplement
10054 #endif
10055
10056 static VALUE
io_new_instance(VALUE args)10057 io_new_instance(VALUE args)
10058 {
10059 return rb_class_new_instance(2, (VALUE*)args+1, *(VALUE*)args);
10060 }
10061
10062 static rb_encoding *
find_encoding(VALUE v)10063 find_encoding(VALUE v)
10064 {
10065 rb_encoding *enc = rb_find_encoding(v);
10066 if (!enc) rb_warn("Unsupported encoding %"PRIsVALUE" ignored", v);
10067 return enc;
10068 }
10069
10070 static void
io_encoding_set(rb_io_t * fptr,VALUE v1,VALUE v2,VALUE opt)10071 io_encoding_set(rb_io_t *fptr, VALUE v1, VALUE v2, VALUE opt)
10072 {
10073 rb_encoding *enc, *enc2;
10074 int ecflags = fptr->encs.ecflags;
10075 VALUE ecopts, tmp;
10076
10077 if (!NIL_P(v2)) {
10078 enc2 = find_encoding(v1);
10079 tmp = rb_check_string_type(v2);
10080 if (!NIL_P(tmp)) {
10081 if (RSTRING_LEN(tmp) == 1 && RSTRING_PTR(tmp)[0] == '-') {
10082 /* Special case - "-" => no transcoding */
10083 enc = enc2;
10084 enc2 = NULL;
10085 }
10086 else
10087 enc = find_encoding(v2);
10088 if (enc == enc2) {
10089 /* Special case - "-" => no transcoding */
10090 enc2 = NULL;
10091 }
10092 }
10093 else {
10094 enc = find_encoding(v2);
10095 if (enc == enc2) {
10096 /* Special case - "-" => no transcoding */
10097 enc2 = NULL;
10098 }
10099 }
10100 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
10101 ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
10102 }
10103 else {
10104 if (NIL_P(v1)) {
10105 /* Set to default encodings */
10106 rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2, 0);
10107 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
10108 ecopts = Qnil;
10109 }
10110 else {
10111 tmp = rb_check_string_type(v1);
10112 if (!NIL_P(tmp) && rb_enc_asciicompat(enc = rb_enc_get(tmp))) {
10113 parse_mode_enc(RSTRING_PTR(tmp), enc, &enc, &enc2, NULL);
10114 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
10115 ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
10116 }
10117 else {
10118 rb_io_ext_int_to_encs(find_encoding(v1), NULL, &enc, &enc2, 0);
10119 SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
10120 ecopts = Qnil;
10121 }
10122 }
10123 }
10124 validate_enc_binmode(&fptr->mode, ecflags, enc, enc2);
10125 fptr->encs.enc = enc;
10126 fptr->encs.enc2 = enc2;
10127 fptr->encs.ecflags = ecflags;
10128 fptr->encs.ecopts = ecopts;
10129 clear_codeconv(fptr);
10130
10131 }
10132
10133 struct io_encoding_set_args {
10134 rb_io_t *fptr;
10135 VALUE v1;
10136 VALUE v2;
10137 VALUE opt;
10138 };
10139
10140 static VALUE
io_encoding_set_v(VALUE v)10141 io_encoding_set_v(VALUE v)
10142 {
10143 struct io_encoding_set_args *arg = (struct io_encoding_set_args *)v;
10144 io_encoding_set(arg->fptr, arg->v1, arg->v2, arg->opt);
10145 return Qnil;
10146 }
10147
10148 static VALUE
pipe_pair_close(VALUE rw)10149 pipe_pair_close(VALUE rw)
10150 {
10151 VALUE *rwp = (VALUE *)rw;
10152 return rb_ensure(io_close, rwp[0], io_close, rwp[1]);
10153 }
10154
10155 /*
10156 * call-seq:
10157 * IO.pipe -> [read_io, write_io]
10158 * IO.pipe(ext_enc) -> [read_io, write_io]
10159 * IO.pipe("ext_enc:int_enc" [, opt]) -> [read_io, write_io]
10160 * IO.pipe(ext_enc, int_enc [, opt]) -> [read_io, write_io]
10161 *
10162 * IO.pipe(...) {|read_io, write_io| ... }
10163 *
10164 * Creates a pair of pipe endpoints (connected to each other) and
10165 * returns them as a two-element array of <code>IO</code> objects:
10166 * <code>[</code> <i>read_io</i>, <i>write_io</i> <code>]</code>.
10167 *
10168 * If a block is given, the block is called and
10169 * returns the value of the block.
10170 * <i>read_io</i> and <i>write_io</i> are sent to the block as arguments.
10171 * If read_io and write_io are not closed when the block exits, they are closed.
10172 * i.e. closing read_io and/or write_io doesn't cause an error.
10173 *
10174 * Not available on all platforms.
10175 *
10176 * If an encoding (encoding name or encoding object) is specified as an optional argument,
10177 * read string from pipe is tagged with the encoding specified.
10178 * If the argument is a colon separated two encoding names "A:B",
10179 * the read string is converted from encoding A (external encoding)
10180 * to encoding B (internal encoding), then tagged with B.
10181 * If two optional arguments are specified, those must be
10182 * encoding objects or encoding names,
10183 * and the first one is the external encoding,
10184 * and the second one is the internal encoding.
10185 * If the external encoding and the internal encoding is specified,
10186 * optional hash argument specify the conversion option.
10187 *
10188 * In the example below, the two processes close the ends of the pipe
10189 * that they are not using. This is not just a cosmetic nicety. The
10190 * read end of a pipe will not generate an end of file condition if
10191 * there are any writers with the pipe still open. In the case of the
10192 * parent process, the <code>rd.read</code> will never return if it
10193 * does not first issue a <code>wr.close</code>.
10194 *
10195 * rd, wr = IO.pipe
10196 *
10197 * if fork
10198 * wr.close
10199 * puts "Parent got: <#{rd.read}>"
10200 * rd.close
10201 * Process.wait
10202 * else
10203 * rd.close
10204 * puts "Sending message to parent"
10205 * wr.write "Hi Dad"
10206 * wr.close
10207 * end
10208 *
10209 * <em>produces:</em>
10210 *
10211 * Sending message to parent
10212 * Parent got: <Hi Dad>
10213 */
10214
10215 static VALUE
rb_io_s_pipe(int argc,VALUE * argv,VALUE klass)10216 rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
10217 {
10218 int pipes[2], state;
10219 VALUE r, w, args[3], v1, v2;
10220 VALUE opt;
10221 rb_io_t *fptr, *fptr2;
10222 struct io_encoding_set_args ies_args;
10223 int fmode = 0;
10224 VALUE ret;
10225
10226 argc = rb_scan_args(argc, argv, "02:", &v1, &v2, &opt);
10227 if (rb_pipe(pipes) < 0)
10228 rb_sys_fail(0);
10229
10230 args[0] = klass;
10231 args[1] = INT2NUM(pipes[0]);
10232 args[2] = INT2FIX(O_RDONLY);
10233 r = rb_protect(io_new_instance, (VALUE)args, &state);
10234 if (state) {
10235 close(pipes[0]);
10236 close(pipes[1]);
10237 rb_jump_tag(state);
10238 }
10239 GetOpenFile(r, fptr);
10240
10241 ies_args.fptr = fptr;
10242 ies_args.v1 = v1;
10243 ies_args.v2 = v2;
10244 ies_args.opt = opt;
10245 rb_protect(io_encoding_set_v, (VALUE)&ies_args, &state);
10246 if (state) {
10247 close(pipes[1]);
10248 io_close(r);
10249 rb_jump_tag(state);
10250 }
10251
10252 args[1] = INT2NUM(pipes[1]);
10253 args[2] = INT2FIX(O_WRONLY);
10254 w = rb_protect(io_new_instance, (VALUE)args, &state);
10255 if (state) {
10256 close(pipes[1]);
10257 if (!NIL_P(r)) rb_io_close(r);
10258 rb_jump_tag(state);
10259 }
10260 GetOpenFile(w, fptr2);
10261 rb_io_synchronized(fptr2);
10262
10263 extract_binmode(opt, &fmode);
10264 #if DEFAULT_TEXTMODE
10265 if ((fptr->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
10266 fptr->mode &= ~FMODE_TEXTMODE;
10267 setmode(fptr->fd, O_BINARY);
10268 }
10269 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
10270 if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
10271 fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
10272 }
10273 #endif
10274 #endif
10275 fptr->mode |= fmode;
10276 #if DEFAULT_TEXTMODE
10277 if ((fptr2->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
10278 fptr2->mode &= ~FMODE_TEXTMODE;
10279 setmode(fptr2->fd, O_BINARY);
10280 }
10281 #endif
10282 fptr2->mode |= fmode;
10283
10284 ret = rb_assoc_new(r, w);
10285 if (rb_block_given_p()) {
10286 VALUE rw[2];
10287 rw[0] = r;
10288 rw[1] = w;
10289 return rb_ensure(rb_yield, ret, pipe_pair_close, (VALUE)rw);
10290 }
10291 return ret;
10292 }
10293
10294 struct foreach_arg {
10295 int argc;
10296 VALUE *argv;
10297 VALUE io;
10298 };
10299
10300 static void
open_key_args(VALUE klass,int argc,VALUE * argv,VALUE opt,struct foreach_arg * arg)10301 open_key_args(VALUE klass, int argc, VALUE *argv, VALUE opt, struct foreach_arg *arg)
10302 {
10303 VALUE path, v;
10304 VALUE vmode = Qnil, vperm = Qnil;
10305
10306 path = *argv++;
10307 argc--;
10308 FilePathValue(path);
10309 arg->io = 0;
10310 arg->argc = argc;
10311 arg->argv = argv;
10312 if (NIL_P(opt)) {
10313 vmode = INT2NUM(O_RDONLY);
10314 vperm = INT2FIX(0666);
10315 }
10316 else if (!NIL_P(v = rb_hash_aref(opt, sym_open_args))) {
10317 int n;
10318
10319 v = rb_to_array_type(v);
10320 n = RARRAY_LENINT(v);
10321 rb_check_arity(n, 0, 3); /* rb_io_open */
10322 rb_scan_args(n, RARRAY_CONST_PTR(v), "02:", &vmode, &vperm, &opt);
10323 }
10324 arg->io = rb_io_open(klass, path, vmode, vperm, opt);
10325 }
10326
10327 static VALUE
io_s_foreach(struct getline_arg * arg)10328 io_s_foreach(struct getline_arg *arg)
10329 {
10330 VALUE str;
10331
10332 while (!NIL_P(str = rb_io_getline_1(arg->rs, arg->limit, arg->chomp, arg->io))) {
10333 rb_lastline_set(str);
10334 rb_yield(str);
10335 }
10336 rb_lastline_set(Qnil);
10337 return Qnil;
10338 }
10339
10340 /*
10341 * call-seq:
10342 * IO.foreach(name, sep=$/ [, getline_args, open_args]) {|line| block } -> nil
10343 * IO.foreach(name, limit [, getline_args, open_args]) {|line| block } -> nil
10344 * IO.foreach(name, sep, limit [, getline_args, open_args]) {|line| block } -> nil
10345 * IO.foreach(...) -> an_enumerator
10346 *
10347 * Executes the block for every line in the named I/O port, where lines
10348 * are separated by <em>sep</em>.
10349 *
10350 * If no block is given, an enumerator is returned instead.
10351 *
10352 * IO.foreach("testfile") {|x| print "GOT ", x }
10353 *
10354 * <em>produces:</em>
10355 *
10356 * GOT This is line one
10357 * GOT This is line two
10358 * GOT This is line three
10359 * GOT And so on...
10360 *
10361 * If the last argument is a hash, it's the keyword argument to open.
10362 * See IO.readlines for details about getline_args.
10363 * And see also IO.read for details about open_args.
10364 *
10365 */
10366
10367 static VALUE
rb_io_s_foreach(int argc,VALUE * argv,VALUE self)10368 rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
10369 {
10370 VALUE opt;
10371 int orig_argc = argc;
10372 struct foreach_arg arg;
10373 struct getline_arg garg;
10374
10375 argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
10376 RETURN_ENUMERATOR(self, orig_argc, argv);
10377 extract_getline_args(argc-1, argv+1, &garg);
10378 open_key_args(self, argc, argv, opt, &arg);
10379 if (NIL_P(arg.io)) return Qnil;
10380 extract_getline_opts(opt, &garg);
10381 check_getline_args(&garg.rs, &garg.limit, garg.io = arg.io);
10382 return rb_ensure(io_s_foreach, (VALUE)&garg, rb_io_close, arg.io);
10383 }
10384
10385 static VALUE
io_s_readlines(struct getline_arg * arg)10386 io_s_readlines(struct getline_arg *arg)
10387 {
10388 return io_readlines(arg, arg->io);
10389 }
10390
10391 /*
10392 * call-seq:
10393 * IO.readlines(name, sep=$/ [, getline_args, open_args]) -> array
10394 * IO.readlines(name, limit [, getline_args, open_args]) -> array
10395 * IO.readlines(name, sep, limit [, getline_args, open_args]) -> array
10396 *
10397 * Reads the entire file specified by <i>name</i> as individual
10398 * lines, and returns those lines in an array. Lines are separated by
10399 * <i>sep</i>.
10400 *
10401 * a = IO.readlines("testfile")
10402 * a[0] #=> "This is line one\n"
10403 *
10404 * b = IO.readlines("testfile", chomp: true)
10405 * b[0] #=> "This is line one"
10406 *
10407 * If the last argument is a hash, it's the keyword argument to open.
10408 *
10409 * === Options for getline
10410 *
10411 * The options hash accepts the following keys:
10412 *
10413 * :chomp::
10414 * When the optional +chomp+ keyword argument has a true value,
10415 * <code>\n</code>, <code>\r</code>, and <code>\r\n</code>
10416 * will be removed from the end of each line.
10417 *
10418 * See also IO.read for details about open_args.
10419 */
10420
10421 static VALUE
rb_io_s_readlines(int argc,VALUE * argv,VALUE io)10422 rb_io_s_readlines(int argc, VALUE *argv, VALUE io)
10423 {
10424 VALUE opt;
10425 struct foreach_arg arg;
10426 struct getline_arg garg;
10427
10428 argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
10429 extract_getline_args(argc-1, argv+1, &garg);
10430 open_key_args(io, argc, argv, opt, &arg);
10431 if (NIL_P(arg.io)) return Qnil;
10432 extract_getline_opts(opt, &garg);
10433 check_getline_args(&garg.rs, &garg.limit, garg.io = arg.io);
10434 return rb_ensure(io_s_readlines, (VALUE)&garg, rb_io_close, arg.io);
10435 }
10436
10437 static VALUE
io_s_read(struct foreach_arg * arg)10438 io_s_read(struct foreach_arg *arg)
10439 {
10440 return io_read(arg->argc, arg->argv, arg->io);
10441 }
10442
10443 struct seek_arg {
10444 VALUE io;
10445 VALUE offset;
10446 int mode;
10447 };
10448
10449 static VALUE
seek_before_access(VALUE argp)10450 seek_before_access(VALUE argp)
10451 {
10452 struct seek_arg *arg = (struct seek_arg *)argp;
10453 rb_io_binmode(arg->io);
10454 return rb_io_seek(arg->io, arg->offset, arg->mode);
10455 }
10456
10457 /*
10458 * call-seq:
10459 * IO.read(name, [length [, offset]] [, opt] ) -> string
10460 *
10461 * Opens the file, optionally seeks to the given +offset+, then returns
10462 * +length+ bytes (defaulting to the rest of the file). <code>read</code>
10463 * ensures the file is closed before returning.
10464 *
10465 * If +name+ starts with a pipe character (<code>"|"</code>), a subprocess is
10466 * created in the same way as Kernel#open, and its output is returned.
10467 *
10468 * === Options
10469 *
10470 * The options hash accepts the following keys:
10471 *
10472 * :encoding::
10473 * string or encoding
10474 *
10475 * Specifies the encoding of the read string. +:encoding+ will be ignored
10476 * if +length+ is specified. See Encoding.aliases for possible encodings.
10477 *
10478 * :mode::
10479 * string or integer
10480 *
10481 * Specifies the <i>mode</i> argument for open(). It must start
10482 * with an "r", otherwise it will cause an error.
10483 * See IO.new for the list of possible modes.
10484 *
10485 * :open_args::
10486 * array
10487 *
10488 * Specifies arguments for open() as an array. This key can not be used
10489 * in combination with either +:encoding+ or +:mode+.
10490 *
10491 * Examples:
10492 *
10493 * IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
10494 * IO.read("testfile", 20) #=> "This is line one\nThi"
10495 * IO.read("testfile", 20, 10) #=> "ne one\nThis is line "
10496 * IO.read("binfile", mode: "rb") #=> "\xF7\x00\x00\x0E\x12"
10497 */
10498
10499 static VALUE
rb_io_s_read(int argc,VALUE * argv,VALUE io)10500 rb_io_s_read(int argc, VALUE *argv, VALUE io)
10501 {
10502 VALUE opt, offset;
10503 struct foreach_arg arg;
10504
10505 argc = rb_scan_args(argc, argv, "13:", NULL, NULL, &offset, NULL, &opt);
10506 open_key_args(io, argc, argv, opt, &arg);
10507 if (NIL_P(arg.io)) return Qnil;
10508 if (!NIL_P(offset)) {
10509 struct seek_arg sarg;
10510 int state = 0;
10511 sarg.io = arg.io;
10512 sarg.offset = offset;
10513 sarg.mode = SEEK_SET;
10514 rb_protect(seek_before_access, (VALUE)&sarg, &state);
10515 if (state) {
10516 rb_io_close(arg.io);
10517 rb_jump_tag(state);
10518 }
10519 if (arg.argc == 2) arg.argc = 1;
10520 }
10521 return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
10522 }
10523
10524 /*
10525 * call-seq:
10526 * IO.binread(name, [length [, offset]] ) -> string
10527 *
10528 * Opens the file, optionally seeks to the given <i>offset</i>, then returns
10529 * <i>length</i> bytes (defaulting to the rest of the file).
10530 * <code>binread</code> ensures the file is closed before returning.
10531 * The open mode would be "rb:ASCII-8BIT".
10532 *
10533 * IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
10534 * IO.binread("testfile", 20) #=> "This is line one\nThi"
10535 * IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
10536 */
10537
10538 static VALUE
rb_io_s_binread(int argc,VALUE * argv,VALUE io)10539 rb_io_s_binread(int argc, VALUE *argv, VALUE io)
10540 {
10541 VALUE offset;
10542 struct foreach_arg arg;
10543 enum {
10544 fmode = FMODE_READABLE|FMODE_BINMODE,
10545 oflags = O_RDONLY
10546 #ifdef O_BINARY
10547 |O_BINARY
10548 #endif
10549 };
10550 convconfig_t convconfig = {NULL, NULL, 0, Qnil};
10551
10552 rb_scan_args(argc, argv, "12", NULL, NULL, &offset);
10553 FilePathValue(argv[0]);
10554 convconfig.enc = rb_ascii8bit_encoding();
10555 arg.io = rb_io_open_generic(io, argv[0], oflags, fmode, &convconfig, 0);
10556 if (NIL_P(arg.io)) return Qnil;
10557 arg.argv = argv+1;
10558 arg.argc = (argc > 1) ? 1 : 0;
10559 if (!NIL_P(offset)) {
10560 struct seek_arg sarg;
10561 int state = 0;
10562 sarg.io = arg.io;
10563 sarg.offset = offset;
10564 sarg.mode = SEEK_SET;
10565 rb_protect(seek_before_access, (VALUE)&sarg, &state);
10566 if (state) {
10567 rb_io_close(arg.io);
10568 rb_jump_tag(state);
10569 }
10570 }
10571 return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
10572 }
10573
10574 static VALUE
io_s_write0(struct write_arg * arg)10575 io_s_write0(struct write_arg *arg)
10576 {
10577 return io_write(arg->io,arg->str,arg->nosync);
10578 }
10579
10580 static VALUE
io_s_write(int argc,VALUE * argv,VALUE klass,int binary)10581 io_s_write(int argc, VALUE *argv, VALUE klass, int binary)
10582 {
10583 VALUE string, offset, opt;
10584 struct foreach_arg arg;
10585 struct write_arg warg;
10586
10587 rb_scan_args(argc, argv, "21:", NULL, &string, &offset, &opt);
10588
10589 if (NIL_P(opt)) opt = rb_hash_new();
10590 else opt = rb_hash_dup(opt);
10591
10592
10593 if (NIL_P(rb_hash_aref(opt,sym_mode))) {
10594 int mode = O_WRONLY|O_CREAT;
10595 #ifdef O_BINARY
10596 if (binary) mode |= O_BINARY;
10597 #endif
10598 if (NIL_P(offset)) mode |= O_TRUNC;
10599 rb_hash_aset(opt,sym_mode,INT2NUM(mode));
10600 }
10601 open_key_args(klass, argc, argv, opt, &arg);
10602
10603 #ifndef O_BINARY
10604 if (binary) rb_io_binmode_m(arg.io);
10605 #endif
10606
10607 if (NIL_P(arg.io)) return Qnil;
10608 if (!NIL_P(offset)) {
10609 struct seek_arg sarg;
10610 int state = 0;
10611 sarg.io = arg.io;
10612 sarg.offset = offset;
10613 sarg.mode = SEEK_SET;
10614 rb_protect(seek_before_access, (VALUE)&sarg, &state);
10615 if (state) {
10616 rb_io_close(arg.io);
10617 rb_jump_tag(state);
10618 }
10619 }
10620
10621 warg.io = arg.io;
10622 warg.str = string;
10623 warg.nosync = 0;
10624
10625 return rb_ensure(io_s_write0, (VALUE)&warg, rb_io_close, arg.io);
10626 }
10627
10628 /*
10629 * call-seq:
10630 * IO.write(name, string [, offset]) -> integer
10631 * IO.write(name, string [, offset] [, opt]) -> integer
10632 *
10633 * Opens the file, optionally seeks to the given <i>offset</i>, writes
10634 * <i>string</i>, then returns the length written.
10635 * <code>write</code> ensures the file is closed before returning.
10636 * If <i>offset</i> is not given in write mode, the file is truncated.
10637 * Otherwise, it is not truncated.
10638 *
10639 * IO.write("testfile", "0123456789", 20) #=> 10
10640 * # File could contain: "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
10641 * IO.write("testfile", "0123456789") #=> 10
10642 * # File would now read: "0123456789"
10643 *
10644 * If the last argument is a hash, it specifies options for the internal
10645 * open(). It accepts the following keys:
10646 *
10647 * :encoding::
10648 * string or encoding
10649 *
10650 * Specifies the encoding of the read string.
10651 * See Encoding.aliases for possible encodings.
10652 *
10653 * :mode::
10654 * string or integer
10655 *
10656 * Specifies the <i>mode</i> argument for open(). It must start
10657 * with "w", "a", or "r+", otherwise it will cause an error.
10658 * See IO.new for the list of possible modes.
10659 *
10660 * :perm::
10661 * integer
10662 *
10663 * Specifies the <i>perm</i> argument for open().
10664 *
10665 * :open_args::
10666 * array
10667 *
10668 * Specifies arguments for open() as an array.
10669 * This key can not be used in combination with other keys.
10670 */
10671
10672 static VALUE
rb_io_s_write(int argc,VALUE * argv,VALUE io)10673 rb_io_s_write(int argc, VALUE *argv, VALUE io)
10674 {
10675 return io_s_write(argc, argv, io, 0);
10676 }
10677
10678 /*
10679 * call-seq:
10680 * IO.binwrite(name, string, [offset] ) -> integer
10681 * IO.binwrite(name, string, [offset], open_args ) -> integer
10682 *
10683 * Same as <code>IO.write</code> except opening the file in binary mode
10684 * and ASCII-8BIT encoding ("wb:ASCII-8BIT").
10685 */
10686
10687 static VALUE
rb_io_s_binwrite(int argc,VALUE * argv,VALUE io)10688 rb_io_s_binwrite(int argc, VALUE *argv, VALUE io)
10689 {
10690 return io_s_write(argc, argv, io, 1);
10691 }
10692
10693 struct copy_stream_struct {
10694 VALUE src;
10695 VALUE dst;
10696 off_t copy_length; /* (off_t)-1 if not specified */
10697 off_t src_offset; /* (off_t)-1 if not specified */
10698
10699 int src_fd;
10700 int dst_fd;
10701 unsigned close_src : 1;
10702 unsigned close_dst : 1;
10703 int error_no;
10704 off_t total;
10705 const char *syserr;
10706 const char *notimp;
10707 VALUE th;
10708 };
10709
10710 static void *
exec_interrupts(void * arg)10711 exec_interrupts(void *arg)
10712 {
10713 VALUE th = (VALUE)arg;
10714 rb_thread_execute_interrupts(th);
10715 return NULL;
10716 }
10717
10718 /*
10719 * returns TRUE if the preceding system call was interrupted
10720 * so we can continue. If the thread was interrupted, we
10721 * reacquire the GVL to execute interrupts before continuing.
10722 */
10723 static int
maygvl_copy_stream_continue_p(int has_gvl,struct copy_stream_struct * stp)10724 maygvl_copy_stream_continue_p(int has_gvl, struct copy_stream_struct *stp)
10725 {
10726 switch (errno) {
10727 case EINTR:
10728 #if defined(ERESTART)
10729 case ERESTART:
10730 #endif
10731 if (rb_thread_interrupted(stp->th)) {
10732 if (has_gvl)
10733 rb_thread_execute_interrupts(stp->th);
10734 else
10735 rb_thread_call_with_gvl(exec_interrupts, (void *)stp->th);
10736 }
10737 return TRUE;
10738 }
10739 return FALSE;
10740 }
10741
10742 /* non-Linux poll may not work on all FDs */
10743 #if defined(HAVE_POLL)
10744 # if defined(__linux__)
10745 # define USE_POLL 1
10746 # endif
10747 # if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
10748 # define USE_POLL 1
10749 # endif
10750 #endif
10751
10752 #ifndef USE_POLL
10753 # define USE_POLL 0
10754 #endif
10755
10756 #if USE_POLL
10757 # define IOWAIT_SYSCALL "poll"
10758 STATIC_ASSERT(pollin_expected, POLLIN == RB_WAITFD_IN);
10759 STATIC_ASSERT(pollout_expected, POLLOUT == RB_WAITFD_OUT);
10760 static int
nogvl_wait_for_single_fd(int fd,short events)10761 nogvl_wait_for_single_fd(int fd, short events)
10762 {
10763 struct pollfd fds;
10764
10765 fds.fd = fd;
10766 fds.events = events;
10767
10768 return poll(&fds, 1, -1);
10769 }
10770 #else /* !USE_POLL */
10771 # include "vm_core.h"
10772 # define IOWAIT_SYSCALL "select"
10773 static int
nogvl_wait_for_single_fd(int fd,short events)10774 nogvl_wait_for_single_fd(int fd, short events)
10775 {
10776 rb_fdset_t fds;
10777 int ret;
10778
10779 rb_fd_init(&fds);
10780 rb_fd_set(fd, &fds);
10781
10782 switch (events) {
10783 case RB_WAITFD_IN:
10784 ret = rb_fd_select(fd + 1, &fds, 0, 0, 0);
10785 break;
10786 case RB_WAITFD_OUT:
10787 ret = rb_fd_select(fd + 1, 0, &fds, 0, 0);
10788 break;
10789 default:
10790 VM_UNREACHABLE(nogvl_wait_for_single_fd);
10791 }
10792
10793 rb_fd_term(&fds);
10794 return ret;
10795 }
10796 #endif /* !USE_POLL */
10797
10798 static int
maygvl_copy_stream_wait_read(int has_gvl,struct copy_stream_struct * stp)10799 maygvl_copy_stream_wait_read(int has_gvl, struct copy_stream_struct *stp)
10800 {
10801 int ret;
10802
10803 do {
10804 if (has_gvl) {
10805 ret = rb_wait_for_single_fd(stp->src_fd, RB_WAITFD_IN, NULL);
10806 }
10807 else {
10808 ret = nogvl_wait_for_single_fd(stp->src_fd, RB_WAITFD_IN);
10809 }
10810 } while (ret < 0 && maygvl_copy_stream_continue_p(has_gvl, stp));
10811
10812 if (ret < 0) {
10813 stp->syserr = IOWAIT_SYSCALL;
10814 stp->error_no = errno;
10815 return ret;
10816 }
10817 return 0;
10818 }
10819
10820 static int
nogvl_copy_stream_wait_write(struct copy_stream_struct * stp)10821 nogvl_copy_stream_wait_write(struct copy_stream_struct *stp)
10822 {
10823 int ret;
10824
10825 do {
10826 ret = nogvl_wait_for_single_fd(stp->dst_fd, RB_WAITFD_OUT);
10827 } while (ret < 0 && maygvl_copy_stream_continue_p(0, stp));
10828
10829 if (ret < 0) {
10830 stp->syserr = IOWAIT_SYSCALL;
10831 stp->error_no = errno;
10832 return ret;
10833 }
10834 return 0;
10835 }
10836
10837 #if defined __linux__ && defined __NR_copy_file_range
10838 # define USE_COPY_FILE_RANGE
10839 #endif
10840
10841 #ifdef USE_COPY_FILE_RANGE
10842
10843 static ssize_t
simple_copy_file_range(int in_fd,off_t * in_offset,int out_fd,off_t * out_offset,size_t count,unsigned int flags)10844 simple_copy_file_range(int in_fd, off_t *in_offset, int out_fd, off_t *out_offset, size_t count, unsigned int flags)
10845 {
10846 return syscall(__NR_copy_file_range, in_fd, in_offset, out_fd, out_offset, count, flags);
10847 }
10848
10849 static int
nogvl_copy_file_range(struct copy_stream_struct * stp)10850 nogvl_copy_file_range(struct copy_stream_struct *stp)
10851 {
10852 struct stat sb;
10853 ssize_t ss;
10854 off_t src_size;
10855 int ret;
10856 off_t copy_length, src_offset, *src_offset_ptr;
10857
10858 ret = fstat(stp->src_fd, &sb);
10859 if (ret < 0) {
10860 stp->syserr = "fstat";
10861 stp->error_no = errno;
10862 return ret;
10863 }
10864 if (!S_ISREG(sb.st_mode))
10865 return 0;
10866
10867 src_size = sb.st_size;
10868 ret = fstat(stp->dst_fd, &sb);
10869 if (ret < 0) {
10870 stp->syserr = "fstat";
10871 stp->error_no = errno;
10872 return ret;
10873 }
10874
10875 src_offset = stp->src_offset;
10876 if (src_offset >= (off_t)0) {
10877 src_offset_ptr = &src_offset;
10878 }
10879 else {
10880 src_offset_ptr = NULL; /* if src_offset_ptr is NULL, then bytes are read from in_fd starting from the file offset */
10881 }
10882
10883 copy_length = stp->copy_length;
10884 if (copy_length < (off_t)0) {
10885 if (src_offset < (off_t)0) {
10886 off_t current_offset;
10887 errno = 0;
10888 current_offset = lseek(stp->src_fd, 0, SEEK_CUR);
10889 if (current_offset < (off_t)0 && errno) {
10890 stp->syserr = "lseek";
10891 stp->error_no = errno;
10892 return (int)current_offset;
10893 }
10894 copy_length = src_size - current_offset;
10895 }
10896 else {
10897 copy_length = src_size - src_offset;
10898 }
10899 }
10900
10901 retry_copy_file_range:
10902 # if SIZEOF_OFF_T > SIZEOF_SIZE_T
10903 /* we are limited by the 32-bit ssize_t return value on 32-bit */
10904 ss = (copy_length > (off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
10905 # else
10906 ss = (ssize_t)copy_length;
10907 # endif
10908 ss = simple_copy_file_range(stp->src_fd, src_offset_ptr, stp->dst_fd, NULL, ss, 0);
10909 if (0 < ss) {
10910 stp->total += ss;
10911 copy_length -= ss;
10912 if (0 < copy_length) {
10913 goto retry_copy_file_range;
10914 }
10915 }
10916 if (ss < 0) {
10917 if (maygvl_copy_stream_continue_p(0, stp)) {
10918 goto retry_copy_file_range;
10919 }
10920 switch (errno) {
10921 case EINVAL:
10922 case EPERM: /* copy_file_range(2) doesn't exist (may happen in
10923 docker container) */
10924 #ifdef ENOSYS
10925 case ENOSYS:
10926 #endif
10927 #ifdef EXDEV
10928 case EXDEV: /* in_fd and out_fd are not on the same filesystem */
10929 #endif
10930 return 0;
10931 case EAGAIN:
10932 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
10933 case EWOULDBLOCK:
10934 #endif
10935 {
10936 int ret = nogvl_copy_stream_wait_write(stp);
10937 if (ret < 0) return ret;
10938 }
10939 goto retry_copy_file_range;
10940 case EBADF:
10941 {
10942 int e = errno;
10943 int flags = fcntl(stp->dst_fd, F_GETFL);
10944
10945 if (flags != -1 && flags & O_APPEND) {
10946 return 0;
10947 }
10948 errno = e;
10949 }
10950 }
10951 stp->syserr = "copy_file_range";
10952 stp->error_no = errno;
10953 return (int)ss;
10954 }
10955 return 1;
10956 }
10957 #endif
10958
10959 #ifdef HAVE_SENDFILE
10960
10961 # ifdef __linux__
10962 # define USE_SENDFILE
10963
10964 # ifdef HAVE_SYS_SENDFILE_H
10965 # include <sys/sendfile.h>
10966 # endif
10967
10968 static ssize_t
simple_sendfile(int out_fd,int in_fd,off_t * offset,off_t count)10969 simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
10970 {
10971 return sendfile(out_fd, in_fd, offset, (size_t)count);
10972 }
10973
10974 # elif 0 /* defined(__FreeBSD__) || defined(__DragonFly__) */ || defined(__APPLE__)
10975 /* This runs on FreeBSD8.1 r30210, but sendfiles blocks its execution
10976 * without cpuset -l 0.
10977 */
10978 # define USE_SENDFILE
10979
10980 static ssize_t
simple_sendfile(int out_fd,int in_fd,off_t * offset,off_t count)10981 simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
10982 {
10983 int r;
10984 off_t pos = offset ? *offset : lseek(in_fd, 0, SEEK_CUR);
10985 off_t sbytes;
10986 # ifdef __APPLE__
10987 r = sendfile(in_fd, out_fd, pos, &count, NULL, 0);
10988 sbytes = count;
10989 # else
10990 r = sendfile(in_fd, out_fd, pos, (size_t)count, NULL, &sbytes, 0);
10991 # endif
10992 if (r != 0 && sbytes == 0) return r;
10993 if (offset) {
10994 *offset += sbytes;
10995 }
10996 else {
10997 lseek(in_fd, sbytes, SEEK_CUR);
10998 }
10999 return (ssize_t)sbytes;
11000 }
11001
11002 # endif
11003
11004 #endif
11005
11006 #ifdef USE_SENDFILE
11007 static int
nogvl_copy_stream_sendfile(struct copy_stream_struct * stp)11008 nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
11009 {
11010 struct stat sb;
11011 ssize_t ss;
11012 int ret;
11013 off_t src_size;
11014 off_t copy_length;
11015 off_t src_offset;
11016 int use_pread;
11017
11018 ret = fstat(stp->src_fd, &sb);
11019 if (ret < 0) {
11020 stp->syserr = "fstat";
11021 stp->error_no = errno;
11022 return ret;
11023 }
11024 if (!S_ISREG(sb.st_mode))
11025 return 0;
11026
11027 src_size = sb.st_size;
11028 ret = fstat(stp->dst_fd, &sb);
11029 if (ret < 0) {
11030 stp->syserr = "fstat";
11031 stp->error_no = errno;
11032 return ret;
11033 }
11034 #ifndef __linux__
11035 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
11036 return 0;
11037 #endif
11038
11039 src_offset = stp->src_offset;
11040 use_pread = src_offset >= (off_t)0;
11041
11042 copy_length = stp->copy_length;
11043 if (copy_length < (off_t)0) {
11044 if (use_pread)
11045 copy_length = src_size - src_offset;
11046 else {
11047 off_t cur;
11048 errno = 0;
11049 cur = lseek(stp->src_fd, 0, SEEK_CUR);
11050 if (cur < (off_t)0 && errno) {
11051 stp->syserr = "lseek";
11052 stp->error_no = errno;
11053 return (int)cur;
11054 }
11055 copy_length = src_size - cur;
11056 }
11057 }
11058
11059 retry_sendfile:
11060 # if SIZEOF_OFF_T > SIZEOF_SIZE_T
11061 /* we are limited by the 32-bit ssize_t return value on 32-bit */
11062 ss = (copy_length > (off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
11063 # else
11064 ss = (ssize_t)copy_length;
11065 # endif
11066 if (use_pread) {
11067 ss = simple_sendfile(stp->dst_fd, stp->src_fd, &src_offset, ss);
11068 }
11069 else {
11070 ss = simple_sendfile(stp->dst_fd, stp->src_fd, NULL, ss);
11071 }
11072 if (0 < ss) {
11073 stp->total += ss;
11074 copy_length -= ss;
11075 if (0 < copy_length) {
11076 goto retry_sendfile;
11077 }
11078 }
11079 if (ss < 0) {
11080 if (maygvl_copy_stream_continue_p(0, stp))
11081 goto retry_sendfile;
11082 switch (errno) {
11083 case EINVAL:
11084 #ifdef ENOSYS
11085 case ENOSYS:
11086 #endif
11087 return 0;
11088 case EAGAIN:
11089 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
11090 case EWOULDBLOCK:
11091 #endif
11092 {
11093 int ret;
11094 #ifndef __linux__
11095 /*
11096 * Linux requires stp->src_fd to be a mmap-able (regular) file,
11097 * select() reports regular files to always be "ready", so
11098 * there is no need to select() on it.
11099 * Other OSes may have the same limitation for sendfile() which
11100 * allow us to bypass maygvl_copy_stream_wait_read()...
11101 */
11102 ret = maygvl_copy_stream_wait_read(0, stp);
11103 if (ret < 0) return ret;
11104 #endif
11105 ret = nogvl_copy_stream_wait_write(stp);
11106 if (ret < 0) return ret;
11107 }
11108 goto retry_sendfile;
11109 }
11110 stp->syserr = "sendfile";
11111 stp->error_no = errno;
11112 return (int)ss;
11113 }
11114 return 1;
11115 }
11116 #endif
11117
11118 static ssize_t
maygvl_read(int has_gvl,int fd,void * buf,size_t count)11119 maygvl_read(int has_gvl, int fd, void *buf, size_t count)
11120 {
11121 if (has_gvl)
11122 return rb_read_internal(fd, buf, count);
11123 else
11124 return read(fd, buf, count);
11125 }
11126
11127 static ssize_t
maygvl_copy_stream_read(int has_gvl,struct copy_stream_struct * stp,char * buf,size_t len,off_t offset)11128 maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf, size_t len, off_t offset)
11129 {
11130 ssize_t ss;
11131 retry_read:
11132 if (offset < (off_t)0) {
11133 ss = maygvl_read(has_gvl, stp->src_fd, buf, len);
11134 }
11135 else {
11136 #ifdef HAVE_PREAD
11137 ss = pread(stp->src_fd, buf, len, offset);
11138 #else
11139 stp->notimp = "pread";
11140 return -1;
11141 #endif
11142 }
11143 if (ss == 0) {
11144 return 0;
11145 }
11146 if (ss < 0) {
11147 if (maygvl_copy_stream_continue_p(has_gvl, stp))
11148 goto retry_read;
11149 switch (errno) {
11150 case EAGAIN:
11151 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
11152 case EWOULDBLOCK:
11153 #endif
11154 {
11155 int ret = maygvl_copy_stream_wait_read(has_gvl, stp);
11156 if (ret < 0) return ret;
11157 }
11158 goto retry_read;
11159 #ifdef ENOSYS
11160 case ENOSYS:
11161 stp->notimp = "pread";
11162 return ss;
11163 #endif
11164 }
11165 stp->syserr = offset < (off_t)0 ? "read" : "pread";
11166 stp->error_no = errno;
11167 }
11168 return ss;
11169 }
11170
11171 static int
nogvl_copy_stream_write(struct copy_stream_struct * stp,char * buf,size_t len)11172 nogvl_copy_stream_write(struct copy_stream_struct *stp, char *buf, size_t len)
11173 {
11174 ssize_t ss;
11175 int off = 0;
11176 while (len) {
11177 ss = write(stp->dst_fd, buf+off, len);
11178 if (ss < 0) {
11179 if (maygvl_copy_stream_continue_p(0, stp))
11180 continue;
11181 if (errno == EAGAIN || errno == EWOULDBLOCK) {
11182 int ret = nogvl_copy_stream_wait_write(stp);
11183 if (ret < 0) return ret;
11184 continue;
11185 }
11186 stp->syserr = "write";
11187 stp->error_no = errno;
11188 return (int)ss;
11189 }
11190 off += (int)ss;
11191 len -= (int)ss;
11192 stp->total += ss;
11193 }
11194 return 0;
11195 }
11196
11197 static void
nogvl_copy_stream_read_write(struct copy_stream_struct * stp)11198 nogvl_copy_stream_read_write(struct copy_stream_struct *stp)
11199 {
11200 char buf[1024*16];
11201 size_t len;
11202 ssize_t ss;
11203 int ret;
11204 off_t copy_length;
11205 int use_eof;
11206 off_t src_offset;
11207 int use_pread;
11208
11209 copy_length = stp->copy_length;
11210 use_eof = copy_length < (off_t)0;
11211 src_offset = stp->src_offset;
11212 use_pread = src_offset >= (off_t)0;
11213
11214 if (use_pread && stp->close_src) {
11215 off_t r;
11216 errno = 0;
11217 r = lseek(stp->src_fd, src_offset, SEEK_SET);
11218 if (r < (off_t)0 && errno) {
11219 stp->syserr = "lseek";
11220 stp->error_no = errno;
11221 return;
11222 }
11223 src_offset = (off_t)-1;
11224 use_pread = 0;
11225 }
11226
11227 while (use_eof || 0 < copy_length) {
11228 if (!use_eof && copy_length < (off_t)sizeof(buf)) {
11229 len = (size_t)copy_length;
11230 }
11231 else {
11232 len = sizeof(buf);
11233 }
11234 if (use_pread) {
11235 ss = maygvl_copy_stream_read(0, stp, buf, len, src_offset);
11236 if (0 < ss)
11237 src_offset += ss;
11238 }
11239 else {
11240 ss = maygvl_copy_stream_read(0, stp, buf, len, (off_t)-1);
11241 }
11242 if (ss <= 0) /* EOF or error */
11243 return;
11244
11245 ret = nogvl_copy_stream_write(stp, buf, ss);
11246 if (ret < 0)
11247 return;
11248
11249 if (!use_eof)
11250 copy_length -= ss;
11251 }
11252 }
11253
11254 static void *
nogvl_copy_stream_func(void * arg)11255 nogvl_copy_stream_func(void *arg)
11256 {
11257 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
11258 #if defined(USE_SENDFILE) || defined(USE_COPY_FILE_RANGE)
11259 int ret;
11260 #endif
11261
11262 #ifdef USE_COPY_FILE_RANGE
11263 ret = nogvl_copy_file_range(stp);
11264 if (ret != 0)
11265 goto finish; /* error or success */
11266 #endif
11267
11268 #ifdef USE_SENDFILE
11269 ret = nogvl_copy_stream_sendfile(stp);
11270 if (ret != 0)
11271 goto finish; /* error or success */
11272 #endif
11273
11274 nogvl_copy_stream_read_write(stp);
11275
11276 #if defined(USE_SENDFILE) || defined(USE_COPY_FILE_RANGE)
11277 finish:
11278 #endif
11279 return 0;
11280 }
11281
11282 static VALUE
copy_stream_fallback_body(VALUE arg)11283 copy_stream_fallback_body(VALUE arg)
11284 {
11285 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
11286 const int buflen = 16*1024;
11287 VALUE n;
11288 VALUE buf = rb_str_buf_new(buflen);
11289 off_t rest = stp->copy_length;
11290 off_t off = stp->src_offset;
11291 ID read_method = id_readpartial;
11292
11293 if (stp->src_fd < 0) {
11294 if (!rb_respond_to(stp->src, read_method)) {
11295 read_method = id_read;
11296 }
11297 }
11298
11299 while (1) {
11300 long numwrote;
11301 long l;
11302 if (stp->copy_length < (off_t)0) {
11303 l = buflen;
11304 }
11305 else {
11306 if (rest == 0) {
11307 rb_str_resize(buf, 0);
11308 break;
11309 }
11310 l = buflen < rest ? buflen : (long)rest;
11311 }
11312 if (stp->src_fd < 0) {
11313 VALUE rc = rb_funcall(stp->src, read_method, 2, INT2FIX(l), buf);
11314
11315 if (read_method == id_read && NIL_P(rc))
11316 break;
11317 }
11318 else {
11319 ssize_t ss;
11320 rb_str_resize(buf, buflen);
11321 ss = maygvl_copy_stream_read(1, stp, RSTRING_PTR(buf), l, off);
11322 rb_str_resize(buf, ss > 0 ? ss : 0);
11323 if (ss < 0)
11324 return Qnil;
11325 if (ss == 0)
11326 rb_eof_error();
11327 if (off >= (off_t)0)
11328 off += ss;
11329 }
11330 n = rb_io_write(stp->dst, buf);
11331 numwrote = NUM2LONG(n);
11332 stp->total += numwrote;
11333 rest -= numwrote;
11334 if (read_method == id_read && RSTRING_LEN(buf) == 0) {
11335 break;
11336 }
11337 }
11338
11339 return Qnil;
11340 }
11341
11342 static VALUE
copy_stream_fallback(struct copy_stream_struct * stp)11343 copy_stream_fallback(struct copy_stream_struct *stp)
11344 {
11345 if (stp->src_fd < 0 && stp->src_offset >= (off_t)0) {
11346 rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
11347 }
11348 rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
11349 (VALUE (*) (ANYARGS))0, (VALUE)0,
11350 rb_eEOFError, (VALUE)0);
11351 return Qnil;
11352 }
11353
11354 static VALUE
copy_stream_body(VALUE arg)11355 copy_stream_body(VALUE arg)
11356 {
11357 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
11358 VALUE src_io = stp->src, dst_io = stp->dst;
11359 rb_io_t *src_fptr = 0, *dst_fptr = 0;
11360 int src_fd, dst_fd;
11361 const int common_oflags = 0
11362 #ifdef O_NOCTTY
11363 | O_NOCTTY
11364 #endif
11365 ;
11366
11367 stp->th = rb_thread_current();
11368
11369 stp->total = 0;
11370
11371 if (src_io == argf ||
11372 !(RB_TYPE_P(src_io, T_FILE) ||
11373 RB_TYPE_P(src_io, T_STRING) ||
11374 rb_respond_to(src_io, rb_intern("to_path")))) {
11375 src_fd = -1;
11376 }
11377 else {
11378 VALUE tmp_io = rb_io_check_io(src_io);
11379 if (!NIL_P(tmp_io)) {
11380 src_io = tmp_io;
11381 }
11382 else if (!RB_TYPE_P(src_io, T_FILE)) {
11383 VALUE args[2];
11384 FilePathValue(src_io);
11385 args[0] = src_io;
11386 args[1] = INT2NUM(O_RDONLY|common_oflags);
11387 src_io = rb_class_new_instance(2, args, rb_cFile);
11388 stp->src = src_io;
11389 stp->close_src = 1;
11390 }
11391 GetOpenFile(src_io, src_fptr);
11392 rb_io_check_byte_readable(src_fptr);
11393 src_fd = src_fptr->fd;
11394 }
11395 stp->src_fd = src_fd;
11396
11397 if (dst_io == argf ||
11398 !(RB_TYPE_P(dst_io, T_FILE) ||
11399 RB_TYPE_P(dst_io, T_STRING) ||
11400 rb_respond_to(dst_io, rb_intern("to_path")))) {
11401 dst_fd = -1;
11402 }
11403 else {
11404 VALUE tmp_io = rb_io_check_io(dst_io);
11405 if (!NIL_P(tmp_io)) {
11406 dst_io = GetWriteIO(tmp_io);
11407 }
11408 else if (!RB_TYPE_P(dst_io, T_FILE)) {
11409 VALUE args[3];
11410 FilePathValue(dst_io);
11411 args[0] = dst_io;
11412 args[1] = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC|common_oflags);
11413 args[2] = INT2FIX(0666);
11414 dst_io = rb_class_new_instance(3, args, rb_cFile);
11415 stp->dst = dst_io;
11416 stp->close_dst = 1;
11417 }
11418 else {
11419 dst_io = GetWriteIO(dst_io);
11420 stp->dst = dst_io;
11421 }
11422 GetOpenFile(dst_io, dst_fptr);
11423 rb_io_check_writable(dst_fptr);
11424 dst_fd = dst_fptr->fd;
11425 }
11426 stp->dst_fd = dst_fd;
11427
11428 #ifdef O_BINARY
11429 if (src_fptr)
11430 SET_BINARY_MODE_WITH_SEEK_CUR(src_fptr);
11431 #endif
11432 if (dst_fptr)
11433 io_ascii8bit_binmode(dst_fptr);
11434
11435 if (stp->src_offset < (off_t)0 && src_fptr && src_fptr->rbuf.len) {
11436 size_t len = src_fptr->rbuf.len;
11437 VALUE str;
11438 if (stp->copy_length >= (off_t)0 && stp->copy_length < (off_t)len) {
11439 len = (size_t)stp->copy_length;
11440 }
11441 str = rb_str_buf_new(len);
11442 rb_str_resize(str,len);
11443 read_buffered_data(RSTRING_PTR(str), len, src_fptr);
11444 if (dst_fptr) { /* IO or filename */
11445 if (io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str), dst_fptr, 0) < 0)
11446 rb_sys_fail(0);
11447 }
11448 else /* others such as StringIO */
11449 rb_io_write(dst_io, str);
11450 rb_str_resize(str, 0);
11451 stp->total += len;
11452 if (stp->copy_length >= (off_t)0)
11453 stp->copy_length -= len;
11454 }
11455
11456 if (dst_fptr && io_fflush(dst_fptr) < 0) {
11457 rb_raise(rb_eIOError, "flush failed");
11458 }
11459
11460 if (stp->copy_length == 0)
11461 return Qnil;
11462
11463 if (src_fd < 0 || dst_fd < 0) {
11464 return copy_stream_fallback(stp);
11465 }
11466
11467 rb_thread_call_without_gvl(nogvl_copy_stream_func, (void*)stp, RUBY_UBF_IO, 0);
11468 return Qnil;
11469 }
11470
11471 static VALUE
copy_stream_finalize(VALUE arg)11472 copy_stream_finalize(VALUE arg)
11473 {
11474 struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
11475 if (stp->close_src) {
11476 rb_io_close_m(stp->src);
11477 }
11478 if (stp->close_dst) {
11479 rb_io_close_m(stp->dst);
11480 }
11481 if (stp->syserr) {
11482 rb_syserr_fail(stp->error_no, stp->syserr);
11483 }
11484 if (stp->notimp) {
11485 rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
11486 }
11487 return Qnil;
11488 }
11489
11490 /*
11491 * call-seq:
11492 * IO.copy_stream(src, dst)
11493 * IO.copy_stream(src, dst, copy_length)
11494 * IO.copy_stream(src, dst, copy_length, src_offset)
11495 *
11496 * IO.copy_stream copies <i>src</i> to <i>dst</i>.
11497 * <i>src</i> and <i>dst</i> is either a filename or an IO-like object.
11498 * IO-like object for <i>src</i> should have <code>readpartial</code> or
11499 * <code>read</code> method.
11500 * IO-like object for <i>dst</i> should have <code>write</code> method.
11501 * (Specialized mechanisms, such as sendfile system call, may be used
11502 * on appropriate situation.)
11503 *
11504 * This method returns the number of bytes copied.
11505 *
11506 * If optional arguments are not given,
11507 * the start position of the copy is
11508 * the beginning of the filename or
11509 * the current file offset of the IO.
11510 * The end position of the copy is the end of file.
11511 *
11512 * If <i>copy_length</i> is given,
11513 * No more than <i>copy_length</i> bytes are copied.
11514 *
11515 * If <i>src_offset</i> is given,
11516 * it specifies the start position of the copy.
11517 *
11518 * When <i>src_offset</i> is specified and
11519 * <i>src</i> is an IO,
11520 * IO.copy_stream doesn't move the current file offset.
11521 *
11522 */
11523 static VALUE
rb_io_s_copy_stream(int argc,VALUE * argv,VALUE io)11524 rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
11525 {
11526 VALUE src, dst, length, src_offset;
11527 struct copy_stream_struct st;
11528
11529 MEMZERO(&st, struct copy_stream_struct, 1);
11530
11531 rb_scan_args(argc, argv, "22", &src, &dst, &length, &src_offset);
11532
11533 st.src = src;
11534 st.dst = dst;
11535
11536 if (NIL_P(length))
11537 st.copy_length = (off_t)-1;
11538 else
11539 st.copy_length = NUM2OFFT(length);
11540
11541 if (NIL_P(src_offset))
11542 st.src_offset = (off_t)-1;
11543 else
11544 st.src_offset = NUM2OFFT(src_offset);
11545
11546 rb_ensure(copy_stream_body, (VALUE)&st, copy_stream_finalize, (VALUE)&st);
11547
11548 return OFFT2NUM(st.total);
11549 }
11550
11551 /*
11552 * call-seq:
11553 * io.external_encoding -> encoding
11554 *
11555 * Returns the Encoding object that represents the encoding of the file.
11556 * If _io_ is in write mode and no encoding is specified, returns +nil+.
11557 */
11558
11559 static VALUE
rb_io_external_encoding(VALUE io)11560 rb_io_external_encoding(VALUE io)
11561 {
11562 rb_io_t *fptr;
11563
11564 GetOpenFile(io, fptr);
11565 if (fptr->encs.enc2) {
11566 return rb_enc_from_encoding(fptr->encs.enc2);
11567 }
11568 if (fptr->mode & FMODE_WRITABLE) {
11569 if (fptr->encs.enc)
11570 return rb_enc_from_encoding(fptr->encs.enc);
11571 return Qnil;
11572 }
11573 return rb_enc_from_encoding(io_read_encoding(fptr));
11574 }
11575
11576 /*
11577 * call-seq:
11578 * io.internal_encoding -> encoding
11579 *
11580 * Returns the Encoding of the internal string if conversion is
11581 * specified. Otherwise returns +nil+.
11582 */
11583
11584 static VALUE
rb_io_internal_encoding(VALUE io)11585 rb_io_internal_encoding(VALUE io)
11586 {
11587 rb_io_t *fptr;
11588
11589 GetOpenFile(io, fptr);
11590 if (!fptr->encs.enc2) return Qnil;
11591 return rb_enc_from_encoding(io_read_encoding(fptr));
11592 }
11593
11594 /*
11595 * call-seq:
11596 * io.set_encoding(ext_enc) -> io
11597 * io.set_encoding("ext_enc:int_enc") -> io
11598 * io.set_encoding(ext_enc, int_enc) -> io
11599 * io.set_encoding("ext_enc:int_enc", opt) -> io
11600 * io.set_encoding(ext_enc, int_enc, opt) -> io
11601 *
11602 * If single argument is specified, read string from io is tagged
11603 * with the encoding specified. If encoding is a colon separated two
11604 * encoding names "A:B", the read string is converted from encoding A
11605 * (external encoding) to encoding B (internal encoding), then tagged
11606 * with B. If two arguments are specified, those must be encoding
11607 * objects or encoding names, and the first one is the external encoding, and the
11608 * second one is the internal encoding.
11609 * If the external encoding and the internal encoding is specified,
11610 * optional hash argument specify the conversion option.
11611 */
11612
11613 static VALUE
rb_io_set_encoding(int argc,VALUE * argv,VALUE io)11614 rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
11615 {
11616 rb_io_t *fptr;
11617 VALUE v1, v2, opt;
11618
11619 if (!RB_TYPE_P(io, T_FILE)) {
11620 return rb_funcallv(io, id_set_encoding, argc, argv);
11621 }
11622
11623 argc = rb_scan_args(argc, argv, "11:", &v1, &v2, &opt);
11624 GetOpenFile(io, fptr);
11625 io_encoding_set(fptr, v1, v2, opt);
11626 return io;
11627 }
11628
11629 void
rb_stdio_set_default_encoding(void)11630 rb_stdio_set_default_encoding(void)
11631 {
11632 VALUE val = Qnil;
11633
11634 rb_io_set_encoding(1, &val, rb_stdin);
11635 rb_io_set_encoding(1, &val, rb_stdout);
11636 rb_io_set_encoding(1, &val, rb_stderr);
11637 }
11638
11639 static inline int
global_argf_p(VALUE arg)11640 global_argf_p(VALUE arg)
11641 {
11642 return arg == argf;
11643 }
11644
11645 /*
11646 * call-seq:
11647 * ARGF.external_encoding -> encoding
11648 *
11649 * Returns the external encoding for files read from +ARGF+ as an +Encoding+
11650 * object. The external encoding is the encoding of the text as stored in a
11651 * file. Contrast with +ARGF.internal_encoding+, which is the encoding used
11652 * to represent this text within Ruby.
11653 *
11654 * To set the external encoding use +ARGF.set_encoding+.
11655 *
11656 * For example:
11657 *
11658 * ARGF.external_encoding #=> #<Encoding:UTF-8>
11659 *
11660 */
11661 static VALUE
argf_external_encoding(VALUE argf)11662 argf_external_encoding(VALUE argf)
11663 {
11664 if (!RTEST(ARGF.current_file)) {
11665 return rb_enc_from_encoding(rb_default_external_encoding());
11666 }
11667 return rb_io_external_encoding(rb_io_check_io(ARGF.current_file));
11668 }
11669
11670 /*
11671 * call-seq:
11672 * ARGF.internal_encoding -> encoding
11673 *
11674 * Returns the internal encoding for strings read from +ARGF+ as an
11675 * +Encoding+ object.
11676 *
11677 * If +ARGF.set_encoding+ has been called with two encoding names, the second
11678 * is returned. Otherwise, if +Encoding.default_external+ has been set, that
11679 * value is returned. Failing that, if a default external encoding was
11680 * specified on the command-line, that value is used. If the encoding is
11681 * unknown, +nil+ is returned.
11682 */
11683 static VALUE
argf_internal_encoding(VALUE argf)11684 argf_internal_encoding(VALUE argf)
11685 {
11686 if (!RTEST(ARGF.current_file)) {
11687 return rb_enc_from_encoding(rb_default_external_encoding());
11688 }
11689 return rb_io_internal_encoding(rb_io_check_io(ARGF.current_file));
11690 }
11691
11692 /*
11693 * call-seq:
11694 * ARGF.set_encoding(ext_enc) -> ARGF
11695 * ARGF.set_encoding("ext_enc:int_enc") -> ARGF
11696 * ARGF.set_encoding(ext_enc, int_enc) -> ARGF
11697 * ARGF.set_encoding("ext_enc:int_enc", opt) -> ARGF
11698 * ARGF.set_encoding(ext_enc, int_enc, opt) -> ARGF
11699 *
11700 * If single argument is specified, strings read from ARGF are tagged with
11701 * the encoding specified.
11702 *
11703 * If two encoding names separated by a colon are given, e.g. "ascii:utf-8",
11704 * the read string is converted from the first encoding (external encoding)
11705 * to the second encoding (internal encoding), then tagged with the second
11706 * encoding.
11707 *
11708 * If two arguments are specified, they must be encoding objects or encoding
11709 * names. Again, the first specifies the external encoding; the second
11710 * specifies the internal encoding.
11711 *
11712 * If the external encoding and the internal encoding are specified, the
11713 * optional +Hash+ argument can be used to adjust the conversion process. The
11714 * structure of this hash is explained in the String#encode documentation.
11715 *
11716 * For example:
11717 *
11718 * ARGF.set_encoding('ascii') # Tag the input as US-ASCII text
11719 * ARGF.set_encoding(Encoding::UTF_8) # Tag the input as UTF-8 text
11720 * ARGF.set_encoding('utf-8','ascii') # Transcode the input from US-ASCII
11721 * # to UTF-8.
11722 */
11723 static VALUE
argf_set_encoding(int argc,VALUE * argv,VALUE argf)11724 argf_set_encoding(int argc, VALUE *argv, VALUE argf)
11725 {
11726 rb_io_t *fptr;
11727
11728 if (!next_argv()) {
11729 rb_raise(rb_eArgError, "no stream to set encoding");
11730 }
11731 rb_io_set_encoding(argc, argv, ARGF.current_file);
11732 GetOpenFile(ARGF.current_file, fptr);
11733 ARGF.encs = fptr->encs;
11734 return argf;
11735 }
11736
11737 /*
11738 * call-seq:
11739 * ARGF.tell -> Integer
11740 * ARGF.pos -> Integer
11741 *
11742 * Returns the current offset (in bytes) of the current file in +ARGF+.
11743 *
11744 * ARGF.pos #=> 0
11745 * ARGF.gets #=> "This is line one\n"
11746 * ARGF.pos #=> 17
11747 *
11748 */
11749 static VALUE
argf_tell(VALUE argf)11750 argf_tell(VALUE argf)
11751 {
11752 if (!next_argv()) {
11753 rb_raise(rb_eArgError, "no stream to tell");
11754 }
11755 ARGF_FORWARD(0, 0);
11756 return rb_io_tell(ARGF.current_file);
11757 }
11758
11759 /*
11760 * call-seq:
11761 * ARGF.seek(amount, whence=IO::SEEK_SET) -> 0
11762 *
11763 * Seeks to offset _amount_ (an +Integer+) in the +ARGF+ stream according to
11764 * the value of _whence_. See IO#seek for further details.
11765 */
11766 static VALUE
argf_seek_m(int argc,VALUE * argv,VALUE argf)11767 argf_seek_m(int argc, VALUE *argv, VALUE argf)
11768 {
11769 if (!next_argv()) {
11770 rb_raise(rb_eArgError, "no stream to seek");
11771 }
11772 ARGF_FORWARD(argc, argv);
11773 return rb_io_seek_m(argc, argv, ARGF.current_file);
11774 }
11775
11776 /*
11777 * call-seq:
11778 * ARGF.pos = position -> Integer
11779 *
11780 * Seeks to the position given by _position_ (in bytes) in +ARGF+.
11781 *
11782 * For example:
11783 *
11784 * ARGF.pos = 17
11785 * ARGF.gets #=> "This is line two\n"
11786 */
11787 static VALUE
argf_set_pos(VALUE argf,VALUE offset)11788 argf_set_pos(VALUE argf, VALUE offset)
11789 {
11790 if (!next_argv()) {
11791 rb_raise(rb_eArgError, "no stream to set position");
11792 }
11793 ARGF_FORWARD(1, &offset);
11794 return rb_io_set_pos(ARGF.current_file, offset);
11795 }
11796
11797 /*
11798 * call-seq:
11799 * ARGF.rewind -> 0
11800 *
11801 * Positions the current file to the beginning of input, resetting
11802 * +ARGF.lineno+ to zero.
11803 *
11804 * ARGF.readline #=> "This is line one\n"
11805 * ARGF.rewind #=> 0
11806 * ARGF.lineno #=> 0
11807 * ARGF.readline #=> "This is line one\n"
11808 */
11809 static VALUE
argf_rewind(VALUE argf)11810 argf_rewind(VALUE argf)
11811 {
11812 VALUE ret;
11813 int old_lineno;
11814
11815 if (!next_argv()) {
11816 rb_raise(rb_eArgError, "no stream to rewind");
11817 }
11818 ARGF_FORWARD(0, 0);
11819 old_lineno = RFILE(ARGF.current_file)->fptr->lineno;
11820 ret = rb_io_rewind(ARGF.current_file);
11821 if (!global_argf_p(argf)) {
11822 ARGF.last_lineno = ARGF.lineno -= old_lineno;
11823 }
11824 return ret;
11825 }
11826
11827 /*
11828 * call-seq:
11829 * ARGF.fileno -> integer
11830 * ARGF.to_i -> integer
11831 *
11832 * Returns an integer representing the numeric file descriptor for
11833 * the current file. Raises an +ArgumentError+ if there isn't a current file.
11834 *
11835 * ARGF.fileno #=> 3
11836 */
11837 static VALUE
argf_fileno(VALUE argf)11838 argf_fileno(VALUE argf)
11839 {
11840 if (!next_argv()) {
11841 rb_raise(rb_eArgError, "no stream");
11842 }
11843 ARGF_FORWARD(0, 0);
11844 return rb_io_fileno(ARGF.current_file);
11845 }
11846
11847 /*
11848 * call-seq:
11849 * ARGF.to_io -> IO
11850 *
11851 * Returns an +IO+ object representing the current file. This will be a
11852 * +File+ object unless the current file is a stream such as STDIN.
11853 *
11854 * For example:
11855 *
11856 * ARGF.to_io #=> #<File:glark.txt>
11857 * ARGF.to_io #=> #<IO:<STDIN>>
11858 */
11859 static VALUE
argf_to_io(VALUE argf)11860 argf_to_io(VALUE argf)
11861 {
11862 next_argv();
11863 ARGF_FORWARD(0, 0);
11864 return ARGF.current_file;
11865 }
11866
11867 /*
11868 * call-seq:
11869 * ARGF.eof? -> true or false
11870 * ARGF.eof -> true or false
11871 *
11872 * Returns true if the current file in +ARGF+ is at end of file, i.e. it has
11873 * no data to read. The stream must be opened for reading or an +IOError+
11874 * will be raised.
11875 *
11876 * $ echo "eof" | ruby argf.rb
11877 *
11878 * ARGF.eof? #=> false
11879 * 3.times { ARGF.readchar }
11880 * ARGF.eof? #=> false
11881 * ARGF.readchar #=> "\n"
11882 * ARGF.eof? #=> true
11883 */
11884
11885 static VALUE
argf_eof(VALUE argf)11886 argf_eof(VALUE argf)
11887 {
11888 next_argv();
11889 if (RTEST(ARGF.current_file)) {
11890 if (ARGF.init_p == 0) return Qtrue;
11891 next_argv();
11892 ARGF_FORWARD(0, 0);
11893 if (rb_io_eof(ARGF.current_file)) {
11894 return Qtrue;
11895 }
11896 }
11897 return Qfalse;
11898 }
11899
11900 /*
11901 * call-seq:
11902 * ARGF.read([length [, outbuf]]) -> string, outbuf, or nil
11903 *
11904 * Reads _length_ bytes from ARGF. The files named on the command line
11905 * are concatenated and treated as a single file by this method, so when
11906 * called without arguments the contents of this pseudo file are returned in
11907 * their entirety.
11908 *
11909 * _length_ must be a non-negative integer or +nil+.
11910 *
11911 * If _length_ is a positive integer, +read+ tries to read
11912 * _length_ bytes without any conversion (binary mode).
11913 * It returns +nil+ if an EOF is encountered before anything can be read.
11914 * Fewer than _length_ bytes are returned if an EOF is encountered during
11915 * the read.
11916 * In the case of an integer _length_, the resulting string is always
11917 * in ASCII-8BIT encoding.
11918 *
11919 * If _length_ is omitted or is +nil+, it reads until EOF
11920 * and the encoding conversion is applied, if applicable.
11921 * A string is returned even if EOF is encountered before any data is read.
11922 *
11923 * If _length_ is zero, it returns an empty string (<code>""</code>).
11924 *
11925 * If the optional _outbuf_ argument is present,
11926 * it must reference a String, which will receive the data.
11927 * The _outbuf_ will contain only the received data after the method call
11928 * even if it is not empty at the beginning.
11929 *
11930 * For example:
11931 *
11932 * $ echo "small" > small.txt
11933 * $ echo "large" > large.txt
11934 * $ ./glark.rb small.txt large.txt
11935 *
11936 * ARGF.read #=> "small\nlarge"
11937 * ARGF.read(200) #=> "small\nlarge"
11938 * ARGF.read(2) #=> "sm"
11939 * ARGF.read(0) #=> ""
11940 *
11941 * Note that this method behaves like the fread() function in C.
11942 * This means it retries to invoke read(2) system calls to read data
11943 * with the specified length.
11944 * If you need the behavior like a single read(2) system call,
11945 * consider ARGF#readpartial or ARGF#read_nonblock.
11946 */
11947
11948 static VALUE
argf_read(int argc,VALUE * argv,VALUE argf)11949 argf_read(int argc, VALUE *argv, VALUE argf)
11950 {
11951 VALUE tmp, str, length;
11952 long len = 0;
11953
11954 rb_scan_args(argc, argv, "02", &length, &str);
11955 if (!NIL_P(length)) {
11956 len = NUM2LONG(argv[0]);
11957 }
11958 if (!NIL_P(str)) {
11959 StringValue(str);
11960 rb_str_resize(str,0);
11961 argv[1] = Qnil;
11962 }
11963
11964 retry:
11965 if (!next_argv()) {
11966 return str;
11967 }
11968 if (ARGF_GENERIC_INPUT_P()) {
11969 tmp = argf_forward(argc, argv, argf);
11970 }
11971 else {
11972 tmp = io_read(argc, argv, ARGF.current_file);
11973 }
11974 if (NIL_P(str)) str = tmp;
11975 else if (!NIL_P(tmp)) rb_str_append(str, tmp);
11976 if (NIL_P(tmp) || NIL_P(length)) {
11977 if (ARGF.next_p != -1) {
11978 argf_close(argf);
11979 ARGF.next_p = 1;
11980 goto retry;
11981 }
11982 }
11983 else if (argc >= 1) {
11984 long slen = RSTRING_LEN(str);
11985 if (slen < len) {
11986 len -= slen;
11987 argv[0] = INT2NUM(len);
11988 goto retry;
11989 }
11990 }
11991 return str;
11992 }
11993
11994 struct argf_call_arg {
11995 int argc;
11996 VALUE *argv;
11997 VALUE argf;
11998 };
11999
12000 static VALUE
argf_forward_call(VALUE arg)12001 argf_forward_call(VALUE arg)
12002 {
12003 struct argf_call_arg *p = (struct argf_call_arg *)arg;
12004 argf_forward(p->argc, p->argv, p->argf);
12005 return Qnil;
12006 }
12007
12008 static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts,
12009 int nonblock);
12010
12011 /*
12012 * call-seq:
12013 * ARGF.readpartial(maxlen) -> string
12014 * ARGF.readpartial(maxlen, outbuf) -> outbuf
12015 *
12016 * Reads at most _maxlen_ bytes from the ARGF stream.
12017 *
12018 * If the optional _outbuf_ argument is present,
12019 * it must reference a String, which will receive the data.
12020 * The _outbuf_ will contain only the received data after the method call
12021 * even if it is not empty at the beginning.
12022 *
12023 * It raises <code>EOFError</code> on end of ARGF stream.
12024 * Since ARGF stream is a concatenation of multiple files,
12025 * internally EOF is occur for each file.
12026 * ARGF.readpartial returns empty strings for EOFs except the last one and
12027 * raises <code>EOFError</code> for the last one.
12028 *
12029 */
12030
12031 static VALUE
argf_readpartial(int argc,VALUE * argv,VALUE argf)12032 argf_readpartial(int argc, VALUE *argv, VALUE argf)
12033 {
12034 return argf_getpartial(argc, argv, argf, Qnil, 0);
12035 }
12036
12037 /*
12038 * call-seq:
12039 * ARGF.read_nonblock(maxlen[, options]) -> string
12040 * ARGF.read_nonblock(maxlen, outbuf[, options]) -> outbuf
12041 *
12042 * Reads at most _maxlen_ bytes from the ARGF stream in non-blocking mode.
12043 */
12044
12045 static VALUE
argf_read_nonblock(int argc,VALUE * argv,VALUE argf)12046 argf_read_nonblock(int argc, VALUE *argv, VALUE argf)
12047 {
12048 VALUE opts;
12049
12050 rb_scan_args(argc, argv, "11:", NULL, NULL, &opts);
12051
12052 if (!NIL_P(opts))
12053 argc--;
12054
12055 return argf_getpartial(argc, argv, argf, opts, 1);
12056 }
12057
12058 static VALUE
argf_getpartial(int argc,VALUE * argv,VALUE argf,VALUE opts,int nonblock)12059 argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts, int nonblock)
12060 {
12061 VALUE tmp, str, length;
12062
12063 rb_scan_args(argc, argv, "11", &length, &str);
12064 if (!NIL_P(str)) {
12065 StringValue(str);
12066 argv[1] = str;
12067 }
12068
12069 if (!next_argv()) {
12070 if (!NIL_P(str)) {
12071 rb_str_resize(str, 0);
12072 }
12073 rb_eof_error();
12074 }
12075 if (ARGF_GENERIC_INPUT_P()) {
12076 struct argf_call_arg arg;
12077 arg.argc = argc;
12078 arg.argv = argv;
12079 arg.argf = argf;
12080 tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
12081 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
12082 }
12083 else {
12084 tmp = io_getpartial(argc, argv, ARGF.current_file, opts, nonblock);
12085 }
12086 if (NIL_P(tmp)) {
12087 if (ARGF.next_p == -1) {
12088 return io_nonblock_eof(opts);
12089 }
12090 argf_close(argf);
12091 ARGF.next_p = 1;
12092 if (RARRAY_LEN(ARGF.argv) == 0) {
12093 return io_nonblock_eof(opts);
12094 }
12095 if (NIL_P(str))
12096 str = rb_str_new(NULL, 0);
12097 return str;
12098 }
12099 return tmp;
12100 }
12101
12102 /*
12103 * call-seq:
12104 * ARGF.getc -> String or nil
12105 *
12106 * Reads the next character from +ARGF+ and returns it as a +String+. Returns
12107 * +nil+ at the end of the stream.
12108 *
12109 * +ARGF+ treats the files named on the command line as a single file created
12110 * by concatenating their contents. After returning the last character of the
12111 * first file, it returns the first character of the second file, and so on.
12112 *
12113 * For example:
12114 *
12115 * $ echo "foo" > file
12116 * $ ruby argf.rb file
12117 *
12118 * ARGF.getc #=> "f"
12119 * ARGF.getc #=> "o"
12120 * ARGF.getc #=> "o"
12121 * ARGF.getc #=> "\n"
12122 * ARGF.getc #=> nil
12123 * ARGF.getc #=> nil
12124 */
12125 static VALUE
argf_getc(VALUE argf)12126 argf_getc(VALUE argf)
12127 {
12128 VALUE ch;
12129
12130 retry:
12131 if (!next_argv()) return Qnil;
12132 if (ARGF_GENERIC_INPUT_P()) {
12133 ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
12134 }
12135 else {
12136 ch = rb_io_getc(ARGF.current_file);
12137 }
12138 if (NIL_P(ch) && ARGF.next_p != -1) {
12139 argf_close(argf);
12140 ARGF.next_p = 1;
12141 goto retry;
12142 }
12143
12144 return ch;
12145 }
12146
12147 /*
12148 * call-seq:
12149 * ARGF.getbyte -> Integer or nil
12150 *
12151 * Gets the next 8-bit byte (0..255) from +ARGF+. Returns +nil+ if called at
12152 * the end of the stream.
12153 *
12154 * For example:
12155 *
12156 * $ echo "foo" > file
12157 * $ ruby argf.rb file
12158 *
12159 * ARGF.getbyte #=> 102
12160 * ARGF.getbyte #=> 111
12161 * ARGF.getbyte #=> 111
12162 * ARGF.getbyte #=> 10
12163 * ARGF.getbyte #=> nil
12164 */
12165 static VALUE
argf_getbyte(VALUE argf)12166 argf_getbyte(VALUE argf)
12167 {
12168 VALUE ch;
12169
12170 retry:
12171 if (!next_argv()) return Qnil;
12172 if (!RB_TYPE_P(ARGF.current_file, T_FILE)) {
12173 ch = rb_funcall3(ARGF.current_file, rb_intern("getbyte"), 0, 0);
12174 }
12175 else {
12176 ch = rb_io_getbyte(ARGF.current_file);
12177 }
12178 if (NIL_P(ch) && ARGF.next_p != -1) {
12179 argf_close(argf);
12180 ARGF.next_p = 1;
12181 goto retry;
12182 }
12183
12184 return ch;
12185 }
12186
12187 /*
12188 * call-seq:
12189 * ARGF.readchar -> String or nil
12190 *
12191 * Reads the next character from +ARGF+ and returns it as a +String+. Raises
12192 * an +EOFError+ after the last character of the last file has been read.
12193 *
12194 * For example:
12195 *
12196 * $ echo "foo" > file
12197 * $ ruby argf.rb file
12198 *
12199 * ARGF.readchar #=> "f"
12200 * ARGF.readchar #=> "o"
12201 * ARGF.readchar #=> "o"
12202 * ARGF.readchar #=> "\n"
12203 * ARGF.readchar #=> end of file reached (EOFError)
12204 */
12205 static VALUE
argf_readchar(VALUE argf)12206 argf_readchar(VALUE argf)
12207 {
12208 VALUE ch;
12209
12210 retry:
12211 if (!next_argv()) rb_eof_error();
12212 if (!RB_TYPE_P(ARGF.current_file, T_FILE)) {
12213 ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
12214 }
12215 else {
12216 ch = rb_io_getc(ARGF.current_file);
12217 }
12218 if (NIL_P(ch) && ARGF.next_p != -1) {
12219 argf_close(argf);
12220 ARGF.next_p = 1;
12221 goto retry;
12222 }
12223
12224 return ch;
12225 }
12226
12227 /*
12228 * call-seq:
12229 * ARGF.readbyte -> Integer
12230 *
12231 * Reads the next 8-bit byte from ARGF and returns it as an +Integer+. Raises
12232 * an +EOFError+ after the last byte of the last file has been read.
12233 *
12234 * For example:
12235 *
12236 * $ echo "foo" > file
12237 * $ ruby argf.rb file
12238 *
12239 * ARGF.readbyte #=> 102
12240 * ARGF.readbyte #=> 111
12241 * ARGF.readbyte #=> 111
12242 * ARGF.readbyte #=> 10
12243 * ARGF.readbyte #=> end of file reached (EOFError)
12244 */
12245 static VALUE
argf_readbyte(VALUE argf)12246 argf_readbyte(VALUE argf)
12247 {
12248 VALUE c;
12249
12250 NEXT_ARGF_FORWARD(0, 0);
12251 c = argf_getbyte(argf);
12252 if (NIL_P(c)) {
12253 rb_eof_error();
12254 }
12255 return c;
12256 }
12257
12258 #define FOREACH_ARGF() while (next_argv())
12259
12260 static VALUE
argf_block_call_i(RB_BLOCK_CALL_FUNC_ARGLIST (i,argf))12261 argf_block_call_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
12262 {
12263 const VALUE current = ARGF.current_file;
12264 rb_yield_values2(argc, argv);
12265 if (ARGF.init_p == -1 || current != ARGF.current_file) {
12266 rb_iter_break_value(Qundef);
12267 }
12268 return Qnil;
12269 }
12270
12271 static void
argf_block_call(ID mid,int argc,VALUE * argv,VALUE argf)12272 argf_block_call(ID mid, int argc, VALUE *argv, VALUE argf)
12273 {
12274 VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_i, argf);
12275 if (ret != Qundef) ARGF.next_p = 1;
12276 }
12277
12278 static VALUE
argf_block_call_line_i(RB_BLOCK_CALL_FUNC_ARGLIST (i,argf))12279 argf_block_call_line_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
12280 {
12281 if (!global_argf_p(argf)) {
12282 ARGF.last_lineno = ++ARGF.lineno;
12283 }
12284 return argf_block_call_i(i, argf, argc, argv, blockarg);
12285 }
12286
12287 static void
argf_block_call_line(ID mid,int argc,VALUE * argv,VALUE argf)12288 argf_block_call_line(ID mid, int argc, VALUE *argv, VALUE argf)
12289 {
12290 VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_line_i, argf);
12291 if (ret != Qundef) ARGF.next_p = 1;
12292 }
12293
12294 /*
12295 * call-seq:
12296 * ARGF.each(sep=$/) {|line| block } -> ARGF
12297 * ARGF.each(sep=$/, limit) {|line| block } -> ARGF
12298 * ARGF.each(...) -> an_enumerator
12299 *
12300 * ARGF.each_line(sep=$/) {|line| block } -> ARGF
12301 * ARGF.each_line(sep=$/, limit) {|line| block } -> ARGF
12302 * ARGF.each_line(...) -> an_enumerator
12303 *
12304 * Returns an enumerator which iterates over each line (separated by _sep_,
12305 * which defaults to your platform's newline character) of each file in
12306 * +ARGV+. If a block is supplied, each line in turn will be yielded to the
12307 * block, otherwise an enumerator is returned.
12308 * The optional _limit_ argument is an +Integer+ specifying the maximum
12309 * length of each line; longer lines will be split according to this limit.
12310 *
12311 * This method allows you to treat the files supplied on the command line as
12312 * a single file consisting of the concatenation of each named file. After
12313 * the last line of the first file has been returned, the first line of the
12314 * second file is returned. The +ARGF.filename+ and +ARGF.lineno+ methods can
12315 * be used to determine the filename of the current line and line number of
12316 * the whole input, respectively.
12317 *
12318 * For example, the following code prints out each line of each named file
12319 * prefixed with its line number, displaying the filename once per file:
12320 *
12321 * ARGF.each_line do |line|
12322 * puts ARGF.filename if ARGF.file.lineno == 1
12323 * puts "#{ARGF.file.lineno}: #{line}"
12324 * end
12325 *
12326 * While the following code prints only the first file's name at first, and
12327 * the contents with line number counted through all named files.
12328 *
12329 * ARGF.each_line do |line|
12330 * puts ARGF.filename if ARGF.lineno == 1
12331 * puts "#{ARGF.lineno}: #{line}"
12332 * end
12333 */
12334 static VALUE
argf_each_line(int argc,VALUE * argv,VALUE argf)12335 argf_each_line(int argc, VALUE *argv, VALUE argf)
12336 {
12337 RETURN_ENUMERATOR(argf, argc, argv);
12338 FOREACH_ARGF() {
12339 argf_block_call_line(rb_intern("each_line"), argc, argv, argf);
12340 }
12341 return argf;
12342 }
12343
12344 /*
12345 * This is a deprecated alias for <code>each_line</code>.
12346 */
12347
12348 static VALUE
argf_lines(int argc,VALUE * argv,VALUE argf)12349 argf_lines(int argc, VALUE *argv, VALUE argf)
12350 {
12351 rb_warn("ARGF#lines is deprecated; use #each_line instead");
12352 if (!rb_block_given_p())
12353 return rb_enumeratorize(argf, ID2SYM(rb_intern("each_line")), argc, argv);
12354 return argf_each_line(argc, argv, argf);
12355 }
12356
12357 /*
12358 * call-seq:
12359 * ARGF.bytes {|byte| block } -> ARGF
12360 * ARGF.bytes -> an_enumerator
12361 *
12362 * ARGF.each_byte {|byte| block } -> ARGF
12363 * ARGF.each_byte -> an_enumerator
12364 *
12365 * Iterates over each byte of each file in +ARGV+.
12366 * A byte is returned as an +Integer+ in the range 0..255.
12367 *
12368 * This method allows you to treat the files supplied on the command line as
12369 * a single file consisting of the concatenation of each named file. After
12370 * the last byte of the first file has been returned, the first byte of the
12371 * second file is returned. The +ARGF.filename+ method can be used to
12372 * determine the filename of the current byte.
12373 *
12374 * If no block is given, an enumerator is returned instead.
12375 *
12376 * For example:
12377 *
12378 * ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
12379 *
12380 */
12381 static VALUE
argf_each_byte(VALUE argf)12382 argf_each_byte(VALUE argf)
12383 {
12384 RETURN_ENUMERATOR(argf, 0, 0);
12385 FOREACH_ARGF() {
12386 argf_block_call(rb_intern("each_byte"), 0, 0, argf);
12387 }
12388 return argf;
12389 }
12390
12391 /*
12392 * This is a deprecated alias for <code>each_byte</code>.
12393 */
12394
12395 static VALUE
argf_bytes(VALUE argf)12396 argf_bytes(VALUE argf)
12397 {
12398 rb_warn("ARGF#bytes is deprecated; use #each_byte instead");
12399 if (!rb_block_given_p())
12400 return rb_enumeratorize(argf, ID2SYM(rb_intern("each_byte")), 0, 0);
12401 return argf_each_byte(argf);
12402 }
12403
12404 /*
12405 * call-seq:
12406 * ARGF.each_char {|char| block } -> ARGF
12407 * ARGF.each_char -> an_enumerator
12408 *
12409 * Iterates over each character of each file in +ARGF+.
12410 *
12411 * This method allows you to treat the files supplied on the command line as
12412 * a single file consisting of the concatenation of each named file. After
12413 * the last character of the first file has been returned, the first
12414 * character of the second file is returned. The +ARGF.filename+ method can
12415 * be used to determine the name of the file in which the current character
12416 * appears.
12417 *
12418 * If no block is given, an enumerator is returned instead.
12419 */
12420 static VALUE
argf_each_char(VALUE argf)12421 argf_each_char(VALUE argf)
12422 {
12423 RETURN_ENUMERATOR(argf, 0, 0);
12424 FOREACH_ARGF() {
12425 argf_block_call(rb_intern("each_char"), 0, 0, argf);
12426 }
12427 return argf;
12428 }
12429
12430 /*
12431 * This is a deprecated alias for <code>each_char</code>.
12432 */
12433
12434 static VALUE
argf_chars(VALUE argf)12435 argf_chars(VALUE argf)
12436 {
12437 rb_warn("ARGF#chars is deprecated; use #each_char instead");
12438 if (!rb_block_given_p())
12439 return rb_enumeratorize(argf, ID2SYM(rb_intern("each_char")), 0, 0);
12440 return argf_each_char(argf);
12441 }
12442
12443 /*
12444 * call-seq:
12445 * ARGF.each_codepoint {|codepoint| block } -> ARGF
12446 * ARGF.each_codepoint -> an_enumerator
12447 *
12448 * Iterates over each codepoint of each file in +ARGF+.
12449 *
12450 * This method allows you to treat the files supplied on the command line as
12451 * a single file consisting of the concatenation of each named file. After
12452 * the last codepoint of the first file has been returned, the first
12453 * codepoint of the second file is returned. The +ARGF.filename+ method can
12454 * be used to determine the name of the file in which the current codepoint
12455 * appears.
12456 *
12457 * If no block is given, an enumerator is returned instead.
12458 */
12459 static VALUE
argf_each_codepoint(VALUE argf)12460 argf_each_codepoint(VALUE argf)
12461 {
12462 RETURN_ENUMERATOR(argf, 0, 0);
12463 FOREACH_ARGF() {
12464 argf_block_call(rb_intern("each_codepoint"), 0, 0, argf);
12465 }
12466 return argf;
12467 }
12468
12469 /*
12470 * This is a deprecated alias for <code>each_codepoint</code>.
12471 */
12472
12473 static VALUE
argf_codepoints(VALUE argf)12474 argf_codepoints(VALUE argf)
12475 {
12476 rb_warn("ARGF#codepoints is deprecated; use #each_codepoint instead");
12477 if (!rb_block_given_p())
12478 return rb_enumeratorize(argf, ID2SYM(rb_intern("each_codepoint")), 0, 0);
12479 return argf_each_codepoint(argf);
12480 }
12481
12482 /*
12483 * call-seq:
12484 * ARGF.filename -> String
12485 * ARGF.path -> String
12486 *
12487 * Returns the current filename. "-" is returned when the current file is
12488 * STDIN.
12489 *
12490 * For example:
12491 *
12492 * $ echo "foo" > foo
12493 * $ echo "bar" > bar
12494 * $ echo "glark" > glark
12495 *
12496 * $ ruby argf.rb foo bar glark
12497 *
12498 * ARGF.filename #=> "foo"
12499 * ARGF.read(5) #=> "foo\nb"
12500 * ARGF.filename #=> "bar"
12501 * ARGF.skip
12502 * ARGF.filename #=> "glark"
12503 */
12504 static VALUE
argf_filename(VALUE argf)12505 argf_filename(VALUE argf)
12506 {
12507 next_argv();
12508 return ARGF.filename;
12509 }
12510
12511 static VALUE
argf_filename_getter(ID id,VALUE * var)12512 argf_filename_getter(ID id, VALUE *var)
12513 {
12514 return argf_filename(*var);
12515 }
12516
12517 /*
12518 * call-seq:
12519 * ARGF.file -> IO or File object
12520 *
12521 * Returns the current file as an +IO+ or +File+ object.
12522 * <code>$stdin</code> is returned when the current file is STDIN.
12523 *
12524 * For example:
12525 *
12526 * $ echo "foo" > foo
12527 * $ echo "bar" > bar
12528 *
12529 * $ ruby argf.rb foo bar
12530 *
12531 * ARGF.file #=> #<File:foo>
12532 * ARGF.read(5) #=> "foo\nb"
12533 * ARGF.file #=> #<File:bar>
12534 */
12535 static VALUE
argf_file(VALUE argf)12536 argf_file(VALUE argf)
12537 {
12538 next_argv();
12539 return ARGF.current_file;
12540 }
12541
12542 /*
12543 * call-seq:
12544 * ARGF.binmode -> ARGF
12545 *
12546 * Puts +ARGF+ into binary mode. Once a stream is in binary mode, it cannot
12547 * be reset to non-binary mode. This option has the following effects:
12548 *
12549 * * Newline conversion is disabled.
12550 * * Encoding conversion is disabled.
12551 * * Content is treated as ASCII-8BIT.
12552 */
12553 static VALUE
argf_binmode_m(VALUE argf)12554 argf_binmode_m(VALUE argf)
12555 {
12556 ARGF.binmode = 1;
12557 next_argv();
12558 ARGF_FORWARD(0, 0);
12559 rb_io_ascii8bit_binmode(ARGF.current_file);
12560 return argf;
12561 }
12562
12563 /*
12564 * call-seq:
12565 * ARGF.binmode? -> true or false
12566 *
12567 * Returns true if +ARGF+ is being read in binary mode; false otherwise.
12568 * To enable binary mode use +ARGF.binmode+.
12569 *
12570 * For example:
12571 *
12572 * ARGF.binmode? #=> false
12573 * ARGF.binmode
12574 * ARGF.binmode? #=> true
12575 */
12576 static VALUE
argf_binmode_p(VALUE argf)12577 argf_binmode_p(VALUE argf)
12578 {
12579 return ARGF.binmode ? Qtrue : Qfalse;
12580 }
12581
12582 /*
12583 * call-seq:
12584 * ARGF.skip -> ARGF
12585 *
12586 * Sets the current file to the next file in ARGV. If there aren't any more
12587 * files it has no effect.
12588 *
12589 * For example:
12590 *
12591 * $ ruby argf.rb foo bar
12592 * ARGF.filename #=> "foo"
12593 * ARGF.skip
12594 * ARGF.filename #=> "bar"
12595 */
12596 static VALUE
argf_skip(VALUE argf)12597 argf_skip(VALUE argf)
12598 {
12599 if (ARGF.init_p && ARGF.next_p == 0) {
12600 argf_close(argf);
12601 ARGF.next_p = 1;
12602 }
12603 return argf;
12604 }
12605
12606 /*
12607 * call-seq:
12608 * ARGF.close -> ARGF
12609 *
12610 * Closes the current file and skips to the next file in ARGV. If there are
12611 * no more files to open, just closes the current file. +STDIN+ will not be
12612 * closed.
12613 *
12614 * For example:
12615 *
12616 * $ ruby argf.rb foo bar
12617 *
12618 * ARGF.filename #=> "foo"
12619 * ARGF.close
12620 * ARGF.filename #=> "bar"
12621 * ARGF.close
12622 */
12623 static VALUE
argf_close_m(VALUE argf)12624 argf_close_m(VALUE argf)
12625 {
12626 next_argv();
12627 argf_close(argf);
12628 if (ARGF.next_p != -1) {
12629 ARGF.next_p = 1;
12630 }
12631 ARGF.lineno = 0;
12632 return argf;
12633 }
12634
12635 /*
12636 * call-seq:
12637 * ARGF.closed? -> true or false
12638 *
12639 * Returns _true_ if the current file has been closed; _false_ otherwise. Use
12640 * +ARGF.close+ to actually close the current file.
12641 */
12642 static VALUE
argf_closed(VALUE argf)12643 argf_closed(VALUE argf)
12644 {
12645 next_argv();
12646 ARGF_FORWARD(0, 0);
12647 return rb_io_closed(ARGF.current_file);
12648 }
12649
12650 /*
12651 * call-seq:
12652 * ARGF.to_s -> String
12653 *
12654 * Returns "ARGF".
12655 */
12656 static VALUE
argf_to_s(VALUE argf)12657 argf_to_s(VALUE argf)
12658 {
12659 return rb_str_new2("ARGF");
12660 }
12661
12662 /*
12663 * call-seq:
12664 * ARGF.inplace_mode -> String
12665 *
12666 * Returns the file extension appended to the names of modified files under
12667 * in-place edit mode. This value can be set using +ARGF.inplace_mode=+ or
12668 * passing the +-i+ switch to the Ruby binary.
12669 */
12670 static VALUE
argf_inplace_mode_get(VALUE argf)12671 argf_inplace_mode_get(VALUE argf)
12672 {
12673 if (!ARGF.inplace) return Qnil;
12674 if (NIL_P(ARGF.inplace)) return rb_str_new(0, 0);
12675 return rb_str_dup(ARGF.inplace);
12676 }
12677
12678 static VALUE
opt_i_get(ID id,VALUE * var)12679 opt_i_get(ID id, VALUE *var)
12680 {
12681 return argf_inplace_mode_get(*var);
12682 }
12683
12684 /*
12685 * call-seq:
12686 * ARGF.inplace_mode = ext -> ARGF
12687 *
12688 * Sets the filename extension for in-place editing mode to the given String.
12689 * Each file being edited has this value appended to its filename. The
12690 * modified file is saved under this new name.
12691 *
12692 * For example:
12693 *
12694 * $ ruby argf.rb file.txt
12695 *
12696 * ARGF.inplace_mode = '.bak'
12697 * ARGF.each_line do |line|
12698 * print line.sub("foo","bar")
12699 * end
12700 *
12701 * Each line of _file.txt_ has the first occurrence of "foo" replaced with
12702 * "bar", then the new line is written out to _file.txt.bak_.
12703 */
12704 static VALUE
argf_inplace_mode_set(VALUE argf,VALUE val)12705 argf_inplace_mode_set(VALUE argf, VALUE val)
12706 {
12707 if (rb_safe_level() >= 1 && OBJ_TAINTED(val))
12708 rb_insecure_operation();
12709
12710 if (!RTEST(val)) {
12711 ARGF.inplace = Qfalse;
12712 }
12713 else if (StringValueCStr(val), !RSTRING_LEN(val)) {
12714 ARGF.inplace = Qnil;
12715 }
12716 else {
12717 ARGF.inplace = rb_str_new_frozen(val);
12718 }
12719 return argf;
12720 }
12721
12722 static void
opt_i_set(VALUE val,ID id,VALUE * var)12723 opt_i_set(VALUE val, ID id, VALUE *var)
12724 {
12725 argf_inplace_mode_set(*var, val);
12726 }
12727
12728 const char *
ruby_get_inplace_mode(void)12729 ruby_get_inplace_mode(void)
12730 {
12731 return RSTRING_PTR(ARGF.inplace);
12732 }
12733
12734 void
ruby_set_inplace_mode(const char * suffix)12735 ruby_set_inplace_mode(const char *suffix)
12736 {
12737 ARGF.inplace = !suffix ? Qfalse : !*suffix ? Qnil : rb_fstring_cstr(suffix);
12738 }
12739
12740 /*
12741 * call-seq:
12742 * ARGF.argv -> ARGV
12743 *
12744 * Returns the +ARGV+ array, which contains the arguments passed to your
12745 * script, one per element.
12746 *
12747 * For example:
12748 *
12749 * $ ruby argf.rb -v glark.txt
12750 *
12751 * ARGF.argv #=> ["-v", "glark.txt"]
12752 *
12753 */
12754 static VALUE
argf_argv(VALUE argf)12755 argf_argv(VALUE argf)
12756 {
12757 return ARGF.argv;
12758 }
12759
12760 static VALUE
argf_argv_getter(ID id,VALUE * var)12761 argf_argv_getter(ID id, VALUE *var)
12762 {
12763 return argf_argv(*var);
12764 }
12765
12766 VALUE
rb_get_argv(void)12767 rb_get_argv(void)
12768 {
12769 return ARGF.argv;
12770 }
12771
12772 /*
12773 * call-seq:
12774 * ARGF.to_write_io -> io
12775 *
12776 * Returns IO instance tied to _ARGF_ for writing if inplace mode is
12777 * enabled.
12778 */
12779 static VALUE
argf_write_io(VALUE argf)12780 argf_write_io(VALUE argf)
12781 {
12782 if (!RTEST(ARGF.current_file)) {
12783 rb_raise(rb_eIOError, "not opened for writing");
12784 }
12785 return GetWriteIO(ARGF.current_file);
12786 }
12787
12788 /*
12789 * call-seq:
12790 * ARGF.write(string) -> integer
12791 *
12792 * Writes _string_ if inplace mode.
12793 */
12794 static VALUE
argf_write(VALUE argf,VALUE str)12795 argf_write(VALUE argf, VALUE str)
12796 {
12797 return rb_io_write(argf_write_io(argf), str);
12798 }
12799
12800 void
rb_readwrite_sys_fail(enum rb_io_wait_readwrite writable,const char * mesg)12801 rb_readwrite_sys_fail(enum rb_io_wait_readwrite writable, const char *mesg)
12802 {
12803 rb_readwrite_syserr_fail(writable, errno, mesg);
12804 }
12805
12806 void
rb_readwrite_syserr_fail(enum rb_io_wait_readwrite writable,int n,const char * mesg)12807 rb_readwrite_syserr_fail(enum rb_io_wait_readwrite writable, int n, const char *mesg)
12808 {
12809 VALUE arg;
12810 arg = mesg ? rb_str_new2(mesg) : Qnil;
12811 if (writable == RB_IO_WAIT_WRITABLE) {
12812 switch (n) {
12813 case EAGAIN:
12814 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINWaitWritable));
12815 break;
12816 #if EAGAIN != EWOULDBLOCK
12817 case EWOULDBLOCK:
12818 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKWaitWritable));
12819 break;
12820 #endif
12821 case EINPROGRESS:
12822 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEINPROGRESSWaitWritable));
12823 break;
12824 default:
12825 rb_mod_sys_fail_str(rb_mWaitWritable, arg);
12826 }
12827 }
12828 else if (writable == RB_IO_WAIT_READABLE) {
12829 switch (n) {
12830 case EAGAIN:
12831 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINWaitReadable));
12832 break;
12833 #if EAGAIN != EWOULDBLOCK
12834 case EWOULDBLOCK:
12835 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKWaitReadable));
12836 break;
12837 #endif
12838 case EINPROGRESS:
12839 rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEINPROGRESSWaitReadable));
12840 break;
12841 default:
12842 rb_mod_sys_fail_str(rb_mWaitReadable, arg);
12843 }
12844 }
12845 else {
12846 rb_bug("invalid read/write type passed to rb_readwrite_sys_fail: %d", writable);
12847 }
12848 }
12849
12850 /*
12851 * Document-class: IOError
12852 *
12853 * Raised when an IO operation fails.
12854 *
12855 * File.open("/etc/hosts") {|f| f << "example"}
12856 * #=> IOError: not opened for writing
12857 *
12858 * File.open("/etc/hosts") {|f| f.close; f.read }
12859 * #=> IOError: closed stream
12860 *
12861 * Note that some IO failures raise <code>SystemCallError</code>s
12862 * and these are not subclasses of IOError:
12863 *
12864 * File.open("does/not/exist")
12865 * #=> Errno::ENOENT: No such file or directory - does/not/exist
12866 */
12867
12868 /*
12869 * Document-class: EOFError
12870 *
12871 * Raised by some IO operations when reaching the end of file. Many IO
12872 * methods exist in two forms,
12873 *
12874 * one that returns +nil+ when the end of file is reached, the other
12875 * raises +EOFError+.
12876 *
12877 * +EOFError+ is a subclass of +IOError+.
12878 *
12879 * file = File.open("/etc/hosts")
12880 * file.read
12881 * file.gets #=> nil
12882 * file.readline #=> EOFError: end of file reached
12883 */
12884
12885 /*
12886 * Document-class: ARGF
12887 *
12888 * +ARGF+ is a stream designed for use in scripts that process files given as
12889 * command-line arguments or passed in via STDIN.
12890 *
12891 * The arguments passed to your script are stored in the +ARGV+ Array, one
12892 * argument per element. +ARGF+ assumes that any arguments that aren't
12893 * filenames have been removed from +ARGV+. For example:
12894 *
12895 * $ ruby argf.rb --verbose file1 file2
12896 *
12897 * ARGV #=> ["--verbose", "file1", "file2"]
12898 * option = ARGV.shift #=> "--verbose"
12899 * ARGV #=> ["file1", "file2"]
12900 *
12901 * You can now use +ARGF+ to work with a concatenation of each of these named
12902 * files. For instance, +ARGF.read+ will return the contents of _file1_
12903 * followed by the contents of _file2_.
12904 *
12905 * After a file in +ARGV+ has been read +ARGF+ removes it from the Array.
12906 * Thus, after all files have been read +ARGV+ will be empty.
12907 *
12908 * You can manipulate +ARGV+ yourself to control what +ARGF+ operates on. If
12909 * you remove a file from +ARGV+, it is ignored by +ARGF+; if you add files to
12910 * +ARGV+, they are treated as if they were named on the command line. For
12911 * example:
12912 *
12913 * ARGV.replace ["file1"]
12914 * ARGF.readlines # Returns the contents of file1 as an Array
12915 * ARGV #=> []
12916 * ARGV.replace ["file2", "file3"]
12917 * ARGF.read # Returns the contents of file2 and file3
12918 *
12919 * If +ARGV+ is empty, +ARGF+ acts as if it contained STDIN, i.e. the data
12920 * piped to your script. For example:
12921 *
12922 * $ echo "glark" | ruby -e 'p ARGF.read'
12923 * "glark\n"
12924 */
12925
12926 /*
12927 * The IO class is the basis for all input and output in Ruby.
12928 * An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
12929 * so may use more than one native operating system stream.
12930 *
12931 * Many of the examples in this section use the File class, the only standard
12932 * subclass of IO. The two classes are closely associated. Like the File
12933 * class, the Socket library subclasses from IO (such as TCPSocket or
12934 * UDPSocket).
12935 *
12936 * The Kernel#open method can create an IO (or File) object for these types
12937 * of arguments:
12938 *
12939 * * A plain string represents a filename suitable for the underlying
12940 * operating system.
12941 *
12942 * * A string starting with <code>"|"</code> indicates a subprocess.
12943 * The remainder of the string following the <code>"|"</code> is
12944 * invoked as a process with appropriate input/output channels
12945 * connected to it.
12946 *
12947 * * A string equal to <code>"|-"</code> will create another Ruby
12948 * instance as a subprocess.
12949 *
12950 * The IO may be opened with different file modes (read-only, write-only) and
12951 * encodings for proper conversion. See IO.new for these options. See
12952 * Kernel#open for details of the various command formats described above.
12953 *
12954 * IO.popen, the Open3 library, or Process#spawn may also be used to
12955 * communicate with subprocesses through an IO.
12956 *
12957 * Ruby will convert pathnames between different operating system
12958 * conventions if possible. For instance, on a Windows system the
12959 * filename <code>"/gumby/ruby/test.rb"</code> will be opened as
12960 * <code>"\gumby\ruby\test.rb"</code>. When specifying a Windows-style
12961 * filename in a Ruby string, remember to escape the backslashes:
12962 *
12963 * "C:\\gumby\\ruby\\test.rb"
12964 *
12965 * Our examples here will use the Unix-style forward slashes;
12966 * File::ALT_SEPARATOR can be used to get the platform-specific separator
12967 * character.
12968 *
12969 * The global constant ARGF (also accessible as <code>$<</code>) provides an
12970 * IO-like stream which allows access to all files mentioned on the
12971 * command line (or STDIN if no files are mentioned). ARGF#path and its alias
12972 * ARGF#filename are provided to access the name of the file currently being
12973 * read.
12974 *
12975 * == io/console
12976 *
12977 * The io/console extension provides methods for interacting with the
12978 * console. The console can be accessed from IO.console or the standard
12979 * input/output/error IO objects.
12980 *
12981 * Requiring io/console adds the following methods:
12982 *
12983 * * IO::console
12984 * * IO#raw
12985 * * IO#raw!
12986 * * IO#cooked
12987 * * IO#cooked!
12988 * * IO#getch
12989 * * IO#echo=
12990 * * IO#echo?
12991 * * IO#noecho
12992 * * IO#winsize
12993 * * IO#winsize=
12994 * * IO#iflush
12995 * * IO#ioflush
12996 * * IO#oflush
12997 *
12998 * Example:
12999 *
13000 * require 'io/console'
13001 * rows, columns = $stdout.winsize
13002 * puts "Your screen is #{columns} wide and #{rows} tall"
13003 */
13004
13005 void
Init_IO(void)13006 Init_IO(void)
13007 {
13008 #undef rb_intern
13009 #define rb_intern(str) rb_intern_const(str)
13010
13011 VALUE rb_cARGF;
13012 #ifdef __CYGWIN__
13013 #include <sys/cygwin.h>
13014 static struct __cygwin_perfile pf[] =
13015 {
13016 {"", O_RDONLY | O_BINARY},
13017 {"", O_WRONLY | O_BINARY},
13018 {"", O_RDWR | O_BINARY},
13019 {"", O_APPEND | O_BINARY},
13020 {NULL, 0}
13021 };
13022 cygwin_internal(CW_PERFILE, pf);
13023 #endif
13024
13025 rb_eIOError = rb_define_class("IOError", rb_eStandardError);
13026 rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
13027
13028 id_write = rb_intern("write");
13029 id_read = rb_intern("read");
13030 id_getc = rb_intern("getc");
13031 id_flush = rb_intern("flush");
13032 id_readpartial = rb_intern("readpartial");
13033 id_set_encoding = rb_intern("set_encoding");
13034
13035 rb_define_global_function("syscall", rb_f_syscall, -1);
13036
13037 rb_define_global_function("open", rb_f_open, -1);
13038 rb_define_global_function("printf", rb_f_printf, -1);
13039 rb_define_global_function("print", rb_f_print, -1);
13040 rb_define_global_function("putc", rb_f_putc, 1);
13041 rb_define_global_function("puts", rb_f_puts, -1);
13042 rb_define_global_function("gets", rb_f_gets, -1);
13043 rb_define_global_function("readline", rb_f_readline, -1);
13044 rb_define_global_function("select", rb_f_select, -1);
13045
13046 rb_define_global_function("readlines", rb_f_readlines, -1);
13047
13048 rb_define_global_function("`", rb_f_backquote, 1);
13049
13050 rb_define_global_function("p", rb_f_p, -1);
13051 rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
13052
13053 rb_cIO = rb_define_class("IO", rb_cObject);
13054 rb_include_module(rb_cIO, rb_mEnumerable);
13055
13056 /* exception to wait for reading. see IO.select. */
13057 rb_mWaitReadable = rb_define_module_under(rb_cIO, "WaitReadable");
13058 /* exception to wait for writing. see IO.select. */
13059 rb_mWaitWritable = rb_define_module_under(rb_cIO, "WaitWritable");
13060 /* exception to wait for reading by EAGAIN. see IO.select. */
13061 rb_eEAGAINWaitReadable = rb_define_class_under(rb_cIO, "EAGAINWaitReadable", rb_eEAGAIN);
13062 rb_include_module(rb_eEAGAINWaitReadable, rb_mWaitReadable);
13063 /* exception to wait for writing by EAGAIN. see IO.select. */
13064 rb_eEAGAINWaitWritable = rb_define_class_under(rb_cIO, "EAGAINWaitWritable", rb_eEAGAIN);
13065 rb_include_module(rb_eEAGAINWaitWritable, rb_mWaitWritable);
13066 #if EAGAIN == EWOULDBLOCK
13067 rb_eEWOULDBLOCKWaitReadable = rb_eEAGAINWaitReadable;
13068 /* same as IO::EAGAINWaitReadable */
13069 rb_define_const(rb_cIO, "EWOULDBLOCKWaitReadable", rb_eEAGAINWaitReadable);
13070 rb_eEWOULDBLOCKWaitWritable = rb_eEAGAINWaitWritable;
13071 /* same as IO::EAGAINWaitWritable */
13072 rb_define_const(rb_cIO, "EWOULDBLOCKWaitWritable", rb_eEAGAINWaitWritable);
13073 #else
13074 /* exception to wait for reading by EWOULDBLOCK. see IO.select. */
13075 rb_eEWOULDBLOCKWaitReadable = rb_define_class_under(rb_cIO, "EWOULDBLOCKWaitReadable", rb_eEWOULDBLOCK);
13076 rb_include_module(rb_eEWOULDBLOCKWaitReadable, rb_mWaitReadable);
13077 /* exception to wait for writing by EWOULDBLOCK. see IO.select. */
13078 rb_eEWOULDBLOCKWaitWritable = rb_define_class_under(rb_cIO, "EWOULDBLOCKWaitWritable", rb_eEWOULDBLOCK);
13079 rb_include_module(rb_eEWOULDBLOCKWaitWritable, rb_mWaitWritable);
13080 #endif
13081 /* exception to wait for reading by EINPROGRESS. see IO.select. */
13082 rb_eEINPROGRESSWaitReadable = rb_define_class_under(rb_cIO, "EINPROGRESSWaitReadable", rb_eEINPROGRESS);
13083 rb_include_module(rb_eEINPROGRESSWaitReadable, rb_mWaitReadable);
13084 /* exception to wait for writing by EINPROGRESS. see IO.select. */
13085 rb_eEINPROGRESSWaitWritable = rb_define_class_under(rb_cIO, "EINPROGRESSWaitWritable", rb_eEINPROGRESS);
13086 rb_include_module(rb_eEINPROGRESSWaitWritable, rb_mWaitWritable);
13087
13088 #if 0
13089 /* This is necessary only for forcing rdoc handle File::open */
13090 rb_define_singleton_method(rb_cFile, "open", rb_io_s_open, -1);
13091 #endif
13092
13093 rb_define_alloc_func(rb_cIO, io_alloc);
13094 rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
13095 rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1);
13096 rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
13097 rb_define_singleton_method(rb_cIO, "for_fd", rb_io_s_for_fd, -1);
13098 rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
13099 rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
13100 rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
13101 rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
13102 rb_define_singleton_method(rb_cIO, "binread", rb_io_s_binread, -1);
13103 rb_define_singleton_method(rb_cIO, "write", rb_io_s_write, -1);
13104 rb_define_singleton_method(rb_cIO, "binwrite", rb_io_s_binwrite, -1);
13105 rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
13106 rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
13107 rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
13108 rb_define_singleton_method(rb_cIO, "copy_stream", rb_io_s_copy_stream, -1);
13109
13110 rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
13111
13112 rb_output_fs = Qnil;
13113 rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
13114
13115 rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */
13116 rb_gc_register_mark_object(rb_default_rs);
13117 rb_rs = rb_default_rs;
13118 rb_output_rs = Qnil;
13119 rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
13120 rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
13121 rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
13122
13123 rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
13124
13125 rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
13126 rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
13127
13128 rb_define_method(rb_cIO, "print", rb_io_print, -1);
13129 rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
13130 rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
13131 rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
13132
13133 rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
13134 rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
13135 rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
13136 rb_define_method(rb_cIO, "each_char", rb_io_each_char, 0);
13137 rb_define_method(rb_cIO, "each_codepoint", rb_io_each_codepoint, 0);
13138 rb_define_method(rb_cIO, "lines", rb_io_lines, -1);
13139 rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0);
13140 rb_define_method(rb_cIO, "chars", rb_io_chars, 0);
13141 rb_define_method(rb_cIO, "codepoints", rb_io_codepoints, 0);
13142
13143 rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
13144 rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
13145
13146 rb_define_method(rb_cIO, "pread", rb_io_pread, -1);
13147 rb_define_method(rb_cIO, "pwrite", rb_io_pwrite, 2);
13148
13149 rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
13150 rb_define_alias(rb_cIO, "to_i", "fileno");
13151 rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
13152
13153 rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
13154 rb_define_method(rb_cIO, "fdatasync", rb_io_fdatasync, 0);
13155 rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
13156 rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
13157
13158 rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
13159 rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
13160
13161 rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
13162
13163 /* for prelude.rb use only: */
13164 rb_define_private_method(rb_cIO, "__read_nonblock", io_read_nonblock, 3);
13165 rb_define_private_method(rb_cIO, "__write_nonblock", io_write_nonblock, 2);
13166
13167 rb_define_method(rb_cIO, "readpartial", io_readpartial, -1);
13168 rb_define_method(rb_cIO, "read", io_read, -1);
13169 rb_define_method(rb_cIO, "write", io_write_m, -1);
13170 rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
13171 rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
13172 rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
13173 rb_define_method(rb_cIO, "getbyte", rb_io_getbyte, 0);
13174 rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
13175 rb_define_method(rb_cIO, "readbyte", rb_io_readbyte, 0);
13176 rb_define_method(rb_cIO, "ungetbyte",rb_io_ungetbyte, 1);
13177 rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
13178 rb_define_method(rb_cIO, "<<", rb_io_addstr, 1);
13179 rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
13180 rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
13181 rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
13182 /* Set I/O position from the beginning */
13183 rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
13184 /* Set I/O position from the current position */
13185 rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
13186 /* Set I/O position from the end */
13187 rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
13188 #ifdef SEEK_DATA
13189 /* Set I/O position to the next location containing data */
13190 rb_define_const(rb_cIO, "SEEK_DATA", INT2FIX(SEEK_DATA));
13191 #endif
13192 #ifdef SEEK_HOLE
13193 /* Set I/O position to the next hole */
13194 rb_define_const(rb_cIO, "SEEK_HOLE", INT2FIX(SEEK_HOLE));
13195 #endif
13196 rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
13197 rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
13198 rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
13199 rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
13200 rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
13201
13202 rb_define_method(rb_cIO, "close_on_exec?", rb_io_close_on_exec_p, 0);
13203 rb_define_method(rb_cIO, "close_on_exec=", rb_io_set_close_on_exec, 1);
13204
13205 rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
13206 rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
13207 rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
13208 rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
13209
13210 rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
13211 rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
13212 rb_define_method(rb_cIO, "binmode", rb_io_binmode_m, 0);
13213 rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
13214 rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
13215 rb_define_method(rb_cIO, "advise", rb_io_advise, -1);
13216
13217 rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
13218 rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
13219 rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
13220 rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
13221
13222 rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
13223 rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
13224 rb_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
13225
13226 rb_define_method(rb_cIO, "autoclose?", rb_io_autoclose_p, 0);
13227 rb_define_method(rb_cIO, "autoclose=", rb_io_set_autoclose, 1);
13228
13229 rb_define_variable("$stdin", &rb_stdin);
13230 rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
13231 rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
13232 rb_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO, "<STDOUT>");
13233 rb_define_hooked_variable("$stderr", &rb_stderr, 0, stdout_setter);
13234 rb_stderr = prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
13235 rb_define_hooked_variable("$>", &rb_stdout, 0, stdout_setter);
13236 orig_stdout = rb_stdout;
13237 orig_stderr = rb_stderr;
13238
13239 /* Holds the original stdin */
13240 rb_define_global_const("STDIN", rb_stdin);
13241 /* Holds the original stdout */
13242 rb_define_global_const("STDOUT", rb_stdout);
13243 /* Holds the original stderr */
13244 rb_define_global_const("STDERR", rb_stderr);
13245
13246 #if 0
13247 /* Hack to get rdoc to regard ARGF as a class: */
13248 rb_cARGF = rb_define_class("ARGF", rb_cObject);
13249 #endif
13250
13251 rb_cARGF = rb_class_new(rb_cObject);
13252 rb_set_class_path(rb_cARGF, rb_cObject, "ARGF.class");
13253 rb_define_alloc_func(rb_cARGF, argf_alloc);
13254
13255 rb_include_module(rb_cARGF, rb_mEnumerable);
13256
13257 rb_define_method(rb_cARGF, "initialize", argf_initialize, -2);
13258 rb_define_method(rb_cARGF, "initialize_copy", argf_initialize_copy, 1);
13259 rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
13260 rb_define_alias(rb_cARGF, "inspect", "to_s");
13261 rb_define_method(rb_cARGF, "argv", argf_argv, 0);
13262
13263 rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
13264 rb_define_method(rb_cARGF, "to_i", argf_fileno, 0);
13265 rb_define_method(rb_cARGF, "to_io", argf_to_io, 0);
13266 rb_define_method(rb_cARGF, "to_write_io", argf_write_io, 0);
13267 rb_define_method(rb_cARGF, "each", argf_each_line, -1);
13268 rb_define_method(rb_cARGF, "each_line", argf_each_line, -1);
13269 rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0);
13270 rb_define_method(rb_cARGF, "each_char", argf_each_char, 0);
13271 rb_define_method(rb_cARGF, "each_codepoint", argf_each_codepoint, 0);
13272 rb_define_method(rb_cARGF, "lines", argf_lines, -1);
13273 rb_define_method(rb_cARGF, "bytes", argf_bytes, 0);
13274 rb_define_method(rb_cARGF, "chars", argf_chars, 0);
13275 rb_define_method(rb_cARGF, "codepoints", argf_codepoints, 0);
13276
13277 rb_define_method(rb_cARGF, "read", argf_read, -1);
13278 rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1);
13279 rb_define_method(rb_cARGF, "read_nonblock", argf_read_nonblock, -1);
13280 rb_define_method(rb_cARGF, "readlines", argf_readlines, -1);
13281 rb_define_method(rb_cARGF, "to_a", argf_readlines, -1);
13282 rb_define_method(rb_cARGF, "gets", argf_gets, -1);
13283 rb_define_method(rb_cARGF, "readline", argf_readline, -1);
13284 rb_define_method(rb_cARGF, "getc", argf_getc, 0);
13285 rb_define_method(rb_cARGF, "getbyte", argf_getbyte, 0);
13286 rb_define_method(rb_cARGF, "readchar", argf_readchar, 0);
13287 rb_define_method(rb_cARGF, "readbyte", argf_readbyte, 0);
13288 rb_define_method(rb_cARGF, "tell", argf_tell, 0);
13289 rb_define_method(rb_cARGF, "seek", argf_seek_m, -1);
13290 rb_define_method(rb_cARGF, "rewind", argf_rewind, 0);
13291 rb_define_method(rb_cARGF, "pos", argf_tell, 0);
13292 rb_define_method(rb_cARGF, "pos=", argf_set_pos, 1);
13293 rb_define_method(rb_cARGF, "eof", argf_eof, 0);
13294 rb_define_method(rb_cARGF, "eof?", argf_eof, 0);
13295 rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
13296 rb_define_method(rb_cARGF, "binmode?", argf_binmode_p, 0);
13297
13298 rb_define_method(rb_cARGF, "write", argf_write, 1);
13299 rb_define_method(rb_cARGF, "print", rb_io_print, -1);
13300 rb_define_method(rb_cARGF, "putc", rb_io_putc, 1);
13301 rb_define_method(rb_cARGF, "puts", rb_io_puts, -1);
13302 rb_define_method(rb_cARGF, "printf", rb_io_printf, -1);
13303
13304 rb_define_method(rb_cARGF, "filename", argf_filename, 0);
13305 rb_define_method(rb_cARGF, "path", argf_filename, 0);
13306 rb_define_method(rb_cARGF, "file", argf_file, 0);
13307 rb_define_method(rb_cARGF, "skip", argf_skip, 0);
13308 rb_define_method(rb_cARGF, "close", argf_close_m, 0);
13309 rb_define_method(rb_cARGF, "closed?", argf_closed, 0);
13310
13311 rb_define_method(rb_cARGF, "lineno", argf_lineno, 0);
13312 rb_define_method(rb_cARGF, "lineno=", argf_set_lineno, 1);
13313
13314 rb_define_method(rb_cARGF, "inplace_mode", argf_inplace_mode_get, 0);
13315 rb_define_method(rb_cARGF, "inplace_mode=", argf_inplace_mode_set, 1);
13316
13317 rb_define_method(rb_cARGF, "external_encoding", argf_external_encoding, 0);
13318 rb_define_method(rb_cARGF, "internal_encoding", argf_internal_encoding, 0);
13319 rb_define_method(rb_cARGF, "set_encoding", argf_set_encoding, -1);
13320
13321 argf = rb_class_new_instance(0, 0, rb_cARGF);
13322
13323 rb_define_readonly_variable("$<", &argf);
13324 /*
13325 * ARGF is a stream designed for use in scripts that process files given
13326 * as command-line arguments or passed in via STDIN.
13327 *
13328 * See ARGF (the class) for more details.
13329 */
13330 rb_define_global_const("ARGF", argf);
13331
13332 rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
13333 rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, rb_gvar_readonly_setter);
13334 ARGF.filename = rb_str_new2("-");
13335
13336 rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
13337 rb_define_hooked_variable("$*", &argf, argf_argv_getter, rb_gvar_readonly_setter);
13338
13339 #if defined (_WIN32) || defined(__CYGWIN__)
13340 atexit(pipe_atexit);
13341 #endif
13342
13343 Init_File();
13344
13345 rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
13346
13347 sym_mode = ID2SYM(rb_intern("mode"));
13348 sym_perm = ID2SYM(rb_intern("perm"));
13349 sym_flags = ID2SYM(rb_intern("flags"));
13350 sym_extenc = ID2SYM(rb_intern("external_encoding"));
13351 sym_intenc = ID2SYM(rb_intern("internal_encoding"));
13352 sym_encoding = ID2SYM(rb_id_encoding());
13353 sym_open_args = ID2SYM(rb_intern("open_args"));
13354 sym_textmode = ID2SYM(rb_intern("textmode"));
13355 sym_binmode = ID2SYM(rb_intern("binmode"));
13356 sym_autoclose = ID2SYM(rb_intern("autoclose"));
13357 sym_normal = ID2SYM(rb_intern("normal"));
13358 sym_sequential = ID2SYM(rb_intern("sequential"));
13359 sym_random = ID2SYM(rb_intern("random"));
13360 sym_willneed = ID2SYM(rb_intern("willneed"));
13361 sym_dontneed = ID2SYM(rb_intern("dontneed"));
13362 sym_noreuse = ID2SYM(rb_intern("noreuse"));
13363 sym_SET = ID2SYM(rb_intern("SET"));
13364 sym_CUR = ID2SYM(rb_intern("CUR"));
13365 sym_END = ID2SYM(rb_intern("END"));
13366 #ifdef SEEK_DATA
13367 sym_DATA = ID2SYM(rb_intern("DATA"));
13368 #endif
13369 #ifdef SEEK_HOLE
13370 sym_HOLE = ID2SYM(rb_intern("HOLE"));
13371 #endif
13372 sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
13373 sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
13374 }
13375