24 #define free(x) xfree(x)
26 #if defined(DOSISH) || defined(__CYGWIN__)
30 #include <sys/types.h>
31 #if defined HAVE_NET_SOCKET_H
32 # include <net/socket.h>
33 #elif defined HAVE_SYS_SOCKET_H
34 # ifndef __native_client__
35 # include <sys/socket.h>
39 #if defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__EMX__) || defined(__BEOS__) || defined(__HAIKU__)
40 # define NO_SAFE_RENAME
43 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__sun) || defined(_nec_ews)
51 #include <sys/types.h>
52 #if defined(HAVE_SYS_IOCTL_H) && !defined(_WIN32)
53 #include <sys/ioctl.h>
55 #if defined(__native_client__) && defined(NACL_NEWLIB)
58 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
60 #elif defined(HAVE_SYS_FCNTL_H)
61 #include <sys/fcntl.h>
64 #if !HAVE_OFF_T && !defined(off_t)
71 #if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
72 # include <sys/param.h>
85 #elif defined HAVE_SYS_SYSCALL_H
86 #include <sys/syscall.h>
89 #if defined(__BEOS__) || defined(__HAIKU__)
91 # define NOFILE (OPEN_MAX)
98 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
101 #if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
102 # error off_t is bigger than long, but you have no long long...
106 # ifdef _POSIX_PIPE_BUF
107 # define PIPE_BUF _POSIX_PIPE_BUF
109 # define PIPE_BUF 512
113 #if defined(HAVE___SYSCALL) && (defined(__APPLE__) || defined(__OpenBSD__))
115 off_t __syscall(quad_t number, ...);
118 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
120 #define IO_RBUF_CAPA_MIN 8192
121 #define IO_CBUF_CAPA_MIN (128*1024)
122 #define IO_RBUF_CAPA_FOR(fptr) (NEED_READCONV(fptr) ? IO_CBUF_CAPA_MIN : IO_RBUF_CAPA_MIN)
123 #define IO_WBUF_CAPA_MIN 8192
128 #define open rb_w32_uopen
170 rb_bug(
"rb_update_max_fd: invalid fd (%d) given.", fd);
182 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
183 int flags, flags2, ret;
184 flags =
fcntl(fd, F_GETFD);
189 flags2 = flags & ~FD_CLOEXEC;
191 flags2 = flags | FD_CLOEXEC;
192 if (flags != flags2) {
193 ret =
fcntl(fd, F_SETFD, flags2);
195 rb_bug(
"rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2,
strerror(
errno));
215 #elif defined O_NOINHERIT
216 flags |= O_NOINHERIT;
218 ret = open(pathname, flags, mode);
219 if (ret == -1)
return -1;
238 if (oldfd == newfd) {
242 #if defined(HAVE_DUP3) && defined(O_CLOEXEC)
243 static int try_dup3 = 1;
244 if (2 < newfd && try_dup3) {
249 if (
errno == ENOSYS) {
251 ret =
dup2(oldfd, newfd);
255 ret =
dup2(oldfd, newfd);
258 ret =
dup2(oldfd, newfd);
260 if (newfd >= 0 && newfd <= 2)
261 SetStdHandle(newfd == 0 ? STD_INPUT_HANDLE : newfd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE, (HANDLE)
rb_w32_get_osfhandle(newfd));
264 if (ret == -1)
return -1;
275 #if defined(HAVE_PIPE2)
276 static int try_pipe2 = 1;
282 if (
errno == ENOSYS) {
293 if (ret == -1)
return -1;
295 if (ret == 0 && fildes[1] == -1) {
312 #if defined(HAVE_FCNTL) && defined(F_DUPFD_CLOEXEC) && defined(F_DUPFD)
313 static int try_dupfd_cloexec = 1;
314 if (try_dupfd_cloexec) {
315 ret =
fcntl(fd, F_DUPFD_CLOEXEC, minfd);
322 if (
errno == EINVAL) {
325 try_dupfd_cloexec = 0;
332 #elif defined(HAVE_FCNTL) && defined(F_DUPFD)
334 #elif defined(HAVE_DUP)
336 if (ret != -1 && ret < minfd) {
337 const int prev_fd = ret;
343 # error "dup() or fcntl(F_DUPFD) must be supported."
345 if (ret == -1)
return -1;
350 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
351 #define ARGF argf_of(argf)
353 #ifdef _STDIO_USES_IOSTREAM
355 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
357 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
359 #elif defined(FILE_COUNT)
360 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
361 #elif defined(FILE_READEND)
362 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
363 #elif defined(__BEOS__) || defined(__HAIKU__)
364 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_state._eof == 0)
366 # define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
369 #define GetWriteIO(io) rb_io_get_write_io(io)
371 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf.len)
372 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf.len)
373 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf.ptr+(fptr)->rbuf.off)
374 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
376 #define READ_CHAR_PENDING(fptr) ((fptr)->cbuf.len)
377 #define READ_CHAR_PENDING_COUNT(fptr) ((fptr)->cbuf.len)
378 #define READ_CHAR_PENDING_PTR(fptr) ((fptr)->cbuf.ptr+(fptr)->cbuf.off)
381 #define WAIT_FD_IN_WIN32(fptr) \
382 (rb_w32_io_cancelable_p((fptr)->fd) ? 0 : rb_thread_wait_fd((fptr)->fd))
384 #define WAIT_FD_IN_WIN32(fptr)
387 #define READ_CHECK(fptr) do {\
388 if (!READ_DATA_PENDING(fptr)) {\
389 WAIT_FD_IN_WIN32(fptr);\
390 rb_io_check_closed(fptr);\
396 # define S_ISSOCK(m) _S_ISSOCK(m)
399 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
402 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
408 #define rb_sys_fail_path(path) rb_sys_fail_str(path)
413 #define NEED_NEWLINE_DECORATOR_ON_READ(fptr) ((fptr)->mode & FMODE_TEXTMODE)
414 #define NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) ((fptr)->mode & FMODE_TEXTMODE)
415 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
417 # define DEFAULT_TEXTMODE FMODE_TEXTMODE
418 # define TEXTMODE_NEWLINE_DECORATOR_ON_WRITE ECONV_CRLF_NEWLINE_DECORATOR
426 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || (fptr)->encs.ecflags & ~ECONV_CRLF_NEWLINE_DECORATOR)
427 #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)))
428 #define SET_BINARY_MODE(fptr) setmode((fptr)->fd, O_BINARY)
430 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) do {\
431 if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {\
432 if (((fptr)->mode & FMODE_READABLE) &&\
433 !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
434 setmode((fptr)->fd, O_BINARY);\
437 setmode((fptr)->fd, O_TEXT);\
442 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) do {\
443 if ((enc2) && ((ecflags) & ECONV_DEFAULT_NEWLINE_DECORATOR)) {\
444 (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
470 if (r < 0 &&
errno) {
482 if (pos < 0 &&
errno) {
489 extra_max = (long)(pos - fptr->
rbuf.
len);
497 for (i = 0; i < fptr->
rbuf.
len; i++) {
498 if (*p ==
'\n') newlines++;
499 if (extra_max == newlines)
break;
504 while (newlines >= 0) {
506 if (newlines == 0)
break;
511 read_size = _read(fptr->
fd, buf, fptr->
rbuf.
len + newlines);
516 if (read_size == fptr->
rbuf.
len) {
538 set_binary_mode_with_seek_cur(
rb_io_t *fptr)
548 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) set_binary_mode_with_seek_cur(fptr)
552 # define DEFAULT_TEXTMODE 0
553 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || NEED_NEWLINE_DECORATOR_ON_READ(fptr))
554 #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)))
555 #define SET_BINARY_MODE(fptr) (void)(fptr)
556 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) (void)(fptr)
557 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) ((void)(enc2), (void)(ecflags))
558 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) (void)(fptr)
561 #if !defined HAVE_SHUTDOWN && !defined shutdown
562 #define shutdown(a,b) 0
566 #define is_socket(fd, path) rb_w32_is_socket(fd)
567 #elif !defined(S_ISSOCK)
568 #define is_socket(fd, path) 0
574 if (
fstat(fd, &sbuf) < 0)
576 return S_ISSOCK(sbuf.st_mode);
630 write_io =
RFILE(io)->fptr->tied_io_for_writing;
648 write_io =
RFILE(io)->fptr->tied_io_for_writing;
649 RFILE(io)->fptr->tied_io_for_writing = w;
650 return write_io ? write_io :
Qnil;
676 #if !(defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32))
687 if (r < 0 &&
errno) {
709 #if SIZEOF_LONG > SIZEOF_INT
743 #define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, (ofs), (whence)))
744 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
872 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
878 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
884 if (
fstat(fptr->
fd, &buf) == 0 &&
886 #
if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(
O_NONBLOCK)
887 && (r =
fcntl(fptr->
fd, F_GETFL)) != -1 &&
914 return read(iis->
fd, iis->
buf, iis->
capa);
921 return write(iis->
fd, iis->
buf, iis->
capa);
1005 return !result ? (
void*)1 : (
void*)
result;
1028 }
else if (ret == 1) {
1071 #if defined(ERESTART)
1078 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1097 #if defined(ERESTART)
1113 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1128 const char *senc, *denc;
1204 long n, r, offset = 0;
1209 if ((n = len) <= 0)
return n;
1246 arg.
ptr = ptr + offset;
1256 if (r == n)
return len;
1280 # define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \
1281 (fmode & FMODE_TEXTMODE) ? (c) : (a))
1292 #define fmode (fptr->mode)
1296 rb_raise(
rb_eArgError,
"ASCII incompatible string written for text mode IO without encoding conversion: %s",
1308 if (!
NIL_P(common_encoding)) {
1317 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
1318 #define fmode (fptr->mode)
1325 setmode(fptr->
fd, O_TEXT);
1328 rb_raise(
rb_eArgError,
"ASCII incompatible string written for text mode IO without encoding conversion: %s",
1343 if (len > 0)
return len;
1358 return (ssize_t)
io_binwrite(0, buf, (
long)size, fptr, 0);
1445 nogvl_fsync(
void *
ptr)
1531 pos =
io_seek(fptr, pos, whence);
1561 VALUE offset, ptrname;
1564 if (
rb_scan_args(argc, argv,
"11", &offset, &ptrname) == 2) {
1628 if (io ==
ARGF.current_file) {
1716 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
1814 # define rb_io_fsync rb_f_notimplement
1815 # define rb_io_sync rb_f_notimplement
1824 #ifdef HAVE_FDATASYNC
1826 nogvl_fdatasync(
void *ptr)
1830 return (
VALUE)fdatasync(fptr->
fd);
1862 #define rb_io_fdatasync rb_io_fsync
1933 static const char closed[] =
" (closed)";
1977 if (n <= 0)
return 0;
1978 if (n > len) n = (int)len;
2003 if ((n -= c) <= 0)
break;
2012 if ((n -= c) <= 0)
break;
2044 return (ssize_t)
io_bufread(buf, (
long)size, fptr);
2047 #define SMALLBUF 100
2057 #
if defined(__BEOS__) || defined(__HAIKU__)
2065 if (st.st_size >= pos && pos >= 0) {
2066 siz += st.st_size - pos;
2092 const char *sname, *dname;
2113 #define MORE_CHAR_SUSPENDED Qtrue
2114 #define MORE_CHAR_FINISHED Qnil
2118 const unsigned char *ss, *sp, *se;
2119 unsigned char *ds, *
dp, *de;
2139 ss = sp = (
const unsigned char *)fptr->
rbuf.
ptr + fptr->
rbuf.
off;
2144 fptr->
rbuf.
off += (int)(sp - ss);
2145 fptr->
rbuf.
len -= (int)(sp - ss);
2146 fptr->
cbuf.
len += (int)(dp - ds);
2151 fptr->
rbuf.
off -= putbackable;
2152 fptr->
rbuf.
len += putbackable;
2159 if (cbuf_len0 != fptr->
cbuf.
len)
2176 fptr->
cbuf.
len += (int)(dp - ds);
2183 if (cbuf_len0 != fptr->
cbuf.
len)
2230 len = (len + 1) & ~1L;
2298 if (siz == 0) siz = BUFSIZ;
2302 n =
io_fread(str, bytes, siz - bytes, fptr);
2303 if (n == 0 && bytes == 0) {
2311 if (bytes < siz)
break;
2325 oflags =
fcntl(fptr->
fd, F_GETFL);
2349 if ((len =
NUM2LONG(length)) < 0) {
2676 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2682 if (
NIL_P(length)) {
2696 if (len == 0)
return str;
2699 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2700 previous_mode = set_binary_mode_with_seek_cur(fptr);
2704 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2705 if (previous_mode == O_TEXT) {
2706 setmode(fptr->
fd, O_TEXT);
2709 if (n == 0)
return Qnil;
2738 if (0 < limit && limit < searchlen)
2739 searchlen = (int)limit;
2740 e = memchr(p, delim, searchlen);
2742 int len = (int)(e-p+1);
2781 if (limit > 0 && pending > limit) pending = limit;
2782 e = memchr(p, delim, pending);
2783 if (e) pending = e - p + 1;
2796 if (e)
return delim;
2820 if (*p != term)
return TRUE;
2822 while (--i && *++p == term);
2825 const char *e = p +
cnt;
2827 while ((p += i) < e &&
rb_enc_ascget(p, e, &i, enc) == term);
2843 if (cnt >
sizeof buf) cnt =
sizeof buf;
2844 if (*p != term)
return TRUE;
2846 while (--i && *++p == term);
2870 e = memchr(p,
'\n', pending);
2872 pending = (int)(e - p + 1);
2898 if (io ==
ARGF.current_file) {
2925 else if (2 <= argc) {
2936 if (enc_io != enc_rs &&
2964 if (
NIL_P(rs) && limit < 0) {
2968 else if (limit == 0) {
2977 int c, newline = -1;
2978 const char *rsptr = 0;
2981 int extra_limit = 16;
3005 newline = (
unsigned char)rsptr[rslen - 1];
3009 while ((c =
appendline(fptr, newline, &str, &limit)) !=
EOF) {
3010 const char *s, *
p, *pp, *e;
3018 if (pp != p)
continue;
3019 if (!rspara)
rscheck(rsptr, rslen, rs);
3020 if (
memcmp(p, rsptr, rslen) == 0)
break;
3052 if (io ==
ARGF.current_file) {
3215 VALUE line, ary, rs;
3280 rb_warn(
"IO#lines is deprecated; use #each_line instead");
3334 rb_warn(
"IO#bytes is deprecated; use #each_byte instead");
3484 rb_warn(
"IO#chars is deprecated; use #each_char instead");
3594 rb_warn(
"IO#codepoints is deprecated; use #each_codepoint instead");
3779 #if SIZEOF_LONG > SIZEOF_INT
3821 if (isatty(fptr->
fd) == 0)
3826 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
3849 if (io != write_io) {
3851 if (fptr && 0 <= (fd = fptr->
fd)) {
3853 if (!(ret & FD_CLOEXEC))
return Qfalse;
3858 if (fptr && 0 <= (fd = fptr->
fd)) {
3860 if (!(ret & FD_CLOEXEC))
return Qfalse;
3865 #define rb_io_close_on_exec_p rb_f_notimplement
3868 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
3892 int flag =
RTEST(arg) ? FD_CLOEXEC : 0;
3898 if (io != write_io) {
3900 if (fptr && 0 <= (fd = fptr->
fd)) {
3902 if ((ret & FD_CLOEXEC) != flag) {
3903 ret = (ret & ~FD_CLOEXEC) | flag;
3904 ret =
fcntl(fd, F_SETFD, ret);
3912 if (fptr && 0 <= (fd = fptr->
fd)) {
3914 if ((ret & FD_CLOEXEC) != flag) {
3915 ret = (ret & ~FD_CLOEXEC) | flag;
3916 ret =
fcntl(fd, F_SETFD, ret);
3923 #define rb_io_set_close_on_exec rb_f_notimplement
3926 #define FMODE_PREP (1<<16)
3927 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
3928 #define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv))
3933 unsigned char *ds, *
dp, *de;
3937 unsigned char buf[1024];
3943 de = buf +
sizeof(
buf);
3983 fptr->
wbuf.
len += (int)(dp - ds);
4010 return (
void*)(
intptr_t)close(*fd);
4031 return (
void*)(
intptr_t)fclose(file);
4038 return fclose(file);
4079 else if (stdio_file) {
4093 if (!
NIL_P(err) && !noraise) {
4094 switch (
TYPE(err)) {
4150 if (!fptr)
return 0;
4191 if (io != write_io) {
4192 write_fptr =
RFILE(write_io)->fptr;
4193 if (write_fptr && 0 <= write_fptr->
fd) {
4198 fptr =
RFILE(io)->fptr;
4199 if (!fptr)
return Qnil;
4200 if (fptr->
fd < 0)
return Qnil;
4279 if (io != write_io) {
4280 write_fptr =
RFILE(write_io)->fptr;
4281 if (write_fptr && 0 <= write_fptr->
fd) {
4286 fptr =
RFILE(io)->fptr;
4332 if (io != write_io) {
4336 RFILE(io)->fptr = wfptr;
4395 if (io != write_io) {
4419 VALUE offset, ptrname;
4424 if (
rb_scan_args(argc, argv,
"11", &offset, &ptrname) == 2) {
4434 rb_warn(
"sysseek for buffered IO");
4437 pos = lseek(fptr->
fd, pos, whence);
4473 rb_warn(
"syswrite for buffered IO");
4512 if (ilen == 0)
return str;
4543 if (n == 0 && ilen > 0) {
4658 case FMODE_READWRITE:
4669 static const char bom_prefix[] =
"bom|utf-";
4670 enum {bom_prefix_len = (int)
sizeof(bom_prefix) - 1};
4672 const char *
p =
strchr(name,
':');
4673 len = p ? (long)(p - name) : (long)
strlen(name);
4675 return len > bom_prefix_len &&
STRNCASECMP(name, bom_prefix, bom_prefix_len) == 0;
4682 const char *m = modestr, *
p =
NULL;
4732 switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
4744 if (oflags & O_APPEND) {
4747 if (oflags & O_TRUNC) {
4750 if (oflags & O_CREAT) {
4774 case FMODE_READWRITE:
4807 # define MODE_BINARY(a,b) ((oflags & O_BINARY) ? (b) : (a))
4809 # define MODE_BINARY(a,b) (a)
4811 int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
4812 if (oflags & O_APPEND) {
4813 if (accmode == O_WRONLY) {
4816 if (accmode == O_RDWR) {
4820 switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
4840 int default_ext = 0;
4852 *enc = (default_ext && intern != ext) ?
NULL : ext;
4864 rb_warn(
"Unsupported encoding %s ignored", name);
4873 int fmode = fmode_p ? *fmode_p : 0;
4880 long len = (p++) - estr;
4889 memcpy(encname, estr, len);
4890 encname[len] =
'\0';
4901 memcpy(encname, estr, len);
4902 encname[len] =
'\0';
4907 if (fmode_p) *fmode_p = fmode;
4919 if (*p ==
'-' && *(p+1) ==
'\0') {
4949 if (v !=
Qnil) encoding =
v;
4951 if (v !=
Qnil) extenc =
v;
4958 rb_warn(
"Ignoring encoding parameter '%s': %s_encoding is used",
4960 extenc ==
Qundef ?
"internal" :
"external");
4968 if (
NIL_P(intenc)) {
4975 if (*p ==
'-' && *(p+1) ==
'\0') {
4986 if (extencoding == intencoding) {
4990 if (!
NIL_P(encoding)) {
5011 int fmode = *fmode_p;
5019 if (!(fmode & FMODE_BINMODE) &&
5024 #if !DEFAULT_TEXTMODE
5025 else if (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
5035 if (!
NIL_P(opthash)) {
5059 int *oflags_p,
int *fmode_p,
convconfig_t *convconfig_p)
5066 int has_enc = 0, has_vmode = 0;
5104 if (
NIL_P(opthash)) {
5108 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5111 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5126 #if DEFAULT_TEXTMODE
5127 else if (
NIL_P(vmode)) {
5134 if (!
NIL_P(vmode)) {
5145 if (!
NIL_P(*vperm_p)) {
5157 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5160 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5178 convconfig_p->enc = enc;
5179 convconfig_p->enc2 = enc2;
5180 convconfig_p->ecflags = ecflags;
5181 convconfig_p->ecopts = ecopts;
5239 file = fdopen(fd, modestr);
5250 file = fdopen(fd, modestr);
5255 #elif defined(__sun)
5264 if (setvbuf(file,
NULL, _IOFBF, 0) != 0)
5265 rb_warn(
"setvbuf() can't be honoured (fd=%d)", fd);
5273 if (isatty(fptr->
fd))
5283 VALUE b1, b2, b3, b4;
5372 convconfig->enc, convconfig->enc2);
5376 fptr->
encs = *convconfig;
5389 const char *
p =
strchr(modestr,
':');
5401 convconfig.ecflags = 0;
5402 convconfig.ecopts =
Qnil;
5425 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
5448 if (list->
fptr == fptr) {
5454 while (list->
next) {
5481 #if !defined(HAVE_FORK) && !defined(_WIN32)
5528 #define HAVE_SPAWNV 1
5529 #define spawnv(mode, cmd, args) rb_w32_aspawn((mode), (cmd), (args))
5530 #define spawn(mode, cmd) rb_w32_spawn((mode), (cmd), 0)
5533 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5545 popen_redirect(
struct popen_arg *
p)
5548 close(p->write_pair[1]);
5549 if (p->write_pair[0] != 0) {
5550 dup2(p->write_pair[0], 0);
5551 close(p->write_pair[0]);
5554 if (p->pair[1] != 1) {
5555 dup2(p->pair[1], 1);
5561 if (p->pair[1] != 1) {
5562 dup2(p->pair[1], 1);
5568 if (p->pair[0] != 0) {
5569 dup2(p->pair[0], 0);
5575 #if defined(__linux__)
5586 linux_get_maxfd(
void)
5589 char buf[4096], *
p, *np, *e;
5592 if (fd == -1)
return -1;
5593 ss = read(fd, buf,
sizeof(buf));
5594 if (ss == -1)
goto err;
5597 while ((
int)
sizeof(
"FDSize:\t0\n")-1 <= e-p &&
5598 (np = memchr(p,
'\n', e-p)) !=
NULL) {
5599 if (
memcmp(p,
"FDSize:",
sizeof(
"FDSize:")-1) == 0) {
5601 p +=
sizeof(
"FDSize:")-1;
5625 ret =
fcntl(0, F_MAXFD);
5627 maxhint = max = ret;
5628 #elif defined(__linux__)
5629 ret = linux_get_maxfd();
5636 for (fd = lowfd; fd <=
max; fd++) {
5637 if (!
NIL_P(noclose_fds) &&
5640 ret =
fcntl(fd, F_GETFD);
5641 if (ret != -1 && !(ret & FD_CLOEXEC)) {
5642 fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
5644 #define CONTIGUOUS_CLOSED_FDS 20
5646 if (max < fd + CONTIGUOUS_CLOSED_FDS)
5647 max = fd + CONTIGUOUS_CLOSED_FDS;
5653 popen_exec(
void *pp,
char *errmsg,
size_t errmsg_len)
5655 struct popen_arg *p = (
struct popen_arg*)pp;
5671 #if defined(HAVE_FORK)
5673 char errmsg[80] = {
'\0' };
5675 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5676 struct popen_arg arg;
5679 #if defined(HAVE_SPAWNV)
5680 # if defined(HAVE_SPAWNVE)
5681 # define DO_SPAWN(cmd, args, envp) ((args) ? \
5682 spawnve(P_NOWAIT, (cmd), (args), (envp)) : \
5683 spawne(P_NOWAIT, (cmd), (envp)))
5685 # define DO_SPAWN(cmd, args, envp) ((args) ? \
5686 spawnv(P_NOWAIT, (cmd), (args)) : \
5687 spawn(P_NOWAIT, (cmd)))
5689 # if !defined(HAVE_FORK)
5691 # if defined(HAVE_SPAWNVE)
5696 #if !defined(HAVE_FORK)
5702 #if !defined(HAVE_FORK)
5703 const char *
cmd = 0;
5704 #if !defined(HAVE_SPAWNV)
5713 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5714 arg.execarg_obj = execarg_obj;
5717 arg.pair[0] = arg.pair[1] = -1;
5718 arg.write_pair[0] = arg.write_pair[1] = -1;
5719 # if !defined(HAVE_FORK)
5726 if (
rb_pipe(arg.write_pair) < 0)
5730 close(arg.write_pair[0]);
5731 close(arg.write_pair[1]);
5755 if (!
NIL_P(execarg_obj)) {
5757 # if defined(HAVE_FORK)
5761 # if defined(HAVE_SPAWNVE)
5764 while ((pid = DO_SPAWN(cmd, args, envp)) == -1) {
5766 switch (e =
errno) {
5768 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5781 # if defined(HAVE_FORK)
5785 popen_redirect(&arg);
5797 # if defined(HAVE_FORK)
5803 close(arg.write_pair[0]);
5804 close(arg.write_pair[1]);
5807 # if defined(HAVE_FORK)
5816 close(arg.write_pair[0]);
5817 write_fd = arg.write_pair[1];
5819 else if (fmode & FMODE_READABLE) {
5832 if (!
NIL_P(execarg_obj)) {
5836 fp = popen(cmd, modestr);
5849 fptr->
encs = *convconfig;
5850 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
5860 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5862 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
5868 if (0 <= write_fd) {
5871 write_fptr->
fd = write_fd;
5873 fptr->
mode &= ~FMODE_WRITABLE;
5878 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
5889 #if !defined(HAVE_FORK)
5891 "fork() function is unimplemented on this machine");
5908 return pipe_open(execarg_obj, modestr, fmode, convconfig);
6002 const char *modestr;
6017 int ex = !
NIL_P(opt);
6025 #if SIZEOF_LONG > SIZEOF_INT
6026 if (len > INT_MAX) {
6041 if (!
NIL_P(execarg_obj)) {
6050 port =
pipe_open(execarg_obj, modestr, fmode, &convconfig);
6061 RBASIC(port)->klass = klass;
6070 VALUE *fname_p,
int *oflags_p,
int *fmode_p,
6073 VALUE opt, fname, vmode, vperm;
6077 argc =
rb_scan_args(argc, argv,
"12:", &fname, &vmode, &vperm, &opt);
6166 VALUE fname, vmode, vperm;
6171 rb_scan_args(argc, argv,
"12", &fname, &vmode, &vperm);
6182 if (
NIL_P(vperm)) perm = 0666;
6308 int redirect =
FALSE;
6316 VALUE tmp = argv[0];
6357 oflags, fmode, &convconfig, perm);
6386 if (fptr == orig)
return io;
6392 "%s can't change access mode from \"%s\" to \"%s\"",
6419 #
if defined (__CYGWIN__) || !defined(HAVE_FORK)
6480 VALUE fname, nmode, opt;
6485 if (
rb_scan_args(argc, argv,
"11:", &fname, &nmode, &opt) == 1) {
6494 fptr =
RFILE(file)->fptr;
6507 (fptr->
mode & FMODE_READWRITE)) {
6509 "%s can't change access mode from \"%s\" to \"%s\"",
6514 fptr->
encs = convconfig;
6547 else if (fptr->
stdio_file == stdout && isatty(fptr->
fd)) {
6553 if (close(fptr->
fd) < 0)
6585 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
6600 if (io != write_io) {
6641 if (argc == 0)
return Qnil;
6689 for (i=0; i<
argc; i++) {
6795 if (len == 0)
return 0;
6797 return ptr[len - 1] ==
c;
6799 return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) ==
c;
6853 for (i=0; i<
argc; i++) {
6920 for (i=0; i<
argc; i++) {
6926 else if (argc > 1) {
7006 if (fwrite(mesg,
sizeof(
char), (
size_t)len, stderr) < (
size_t)len) {
7028 if (fwrite(
RSTRING_PTR(mesg),
sizeof(
char), len, stderr) < len) {
7083 if (path && strcmp(path,
"-")) klass =
rb_cFile;
7095 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
7096 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
7267 int fd,
fmode, oflags = O_RDONLY;
7270 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7278 argc =
rb_scan_args(argc, argv,
"11:", &fnum, &vmode, &opt);
7285 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7286 oflags =
fcntl(fd, F_GETFL);
7292 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7308 fp->
encs = convconfig;
7313 else if (
fileno(stdout) == fd)
7315 else if (
fileno(stderr) == fd)
7346 if (
RFILE(io)->fptr) {
7349 if (0 < argc && argc < 3) {
7369 rb_warn(
"%s::new() does not take block; use %s::open() instead",
7432 if (!
RTEST(autoclose))
7442 struct argf *p = ptr;
7452 struct argf *p = ptr;
7460 const struct argf *p = ptr;
7461 size_t size =
sizeof(*p);
7569 #define next_argv() argf_next_argv(argf)
7570 #define ARGF_GENERIC_INPUT_P() \
7571 (ARGF.current_file == rb_stdin && !RB_TYPE_P(ARGF.current_file, T_FILE))
7572 #define ARGF_FORWARD(argc, argv) do {\
7573 if (ARGF_GENERIC_INPUT_P())\
7574 return argf_forward((argc), (argv), argf);\
7576 #define NEXT_ARGF_FORWARD(argc, argv) do {\
7577 if (!next_argv()) return Qnil;\
7578 ARGF_FORWARD((argc), (argv));\
7596 int stdout_binmode = 0;
7605 if (
ARGF.init_p == 0) {
7623 if (
ARGF.next_p == 1) {
7628 if (
strlen(fn) == 1 && fn[0] ==
'-') {
7631 rb_warn(
"Can't do inplace edit for stdio; skipping");
7641 #ifndef NO_SAFE_RENAME
7651 if (*
ARGF.inplace) {
7654 #ifdef NO_SAFE_RENAME
7658 rb_warn(
"Can't rename %s to %s: %s, skipping file",
7665 rb_warn(
"Can't rename %s to %s: %s, skipping file",
7673 #ifdef NO_SAFE_RENAME
7674 rb_fatal(
"Can't do inplace edit without backup");
7676 if (unlink(fn) < 0) {
7677 rb_warn(
"Can't remove %s: %s, skipping file",
7685 #ifndef NO_SAFE_RENAME
7688 fchmod(fw, st.st_mode);
7690 chmod(fn, st.st_mode);
7692 if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
7695 err = fchown(fw, st.st_uid, st.st_gid);
7697 err =
chown(fn, st.st_uid, st.st_gid);
7699 if (err &&
getuid() == 0 && st2.st_uid == 0) {
7701 rb_warn(
"Can't set owner/group of %s to same as %s: %s, skipping file",
7715 if (!
ARGF.binmode) {
7719 if (!
NIL_P(write_io)) {
7725 if (
ARGF.encs.enc) {
7731 if (!
ARGF.binmode) {
7733 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
7734 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
7745 else if (
ARGF.next_p == -1) {
7749 rb_warn(
"Can't do inplace edit for stdio");
7760 long lineno =
ARGF.lineno;
7781 ARGF.lineno = ++lineno;
7799 ARGF.last_lineno =
ARGF.lineno = n;
7991 long lineno =
ARGF.lineno;
8029 volatile VALUE port;
8045 #ifdef HAVE_SYS_SELECT_H
8046 #include <sys/select.h>
8069 if (max < fptr->fd) max = fptr->
fd;
8080 if (!
NIL_P(write)) {
8086 if (max < fptr->fd) max = fptr->
fd;
8093 if (!
NIL_P(except)) {
8100 if (max < fptr->fd) max = fptr->
fd;
8101 if (io != write_io) {
8104 if (max < fptr->fd) max = fptr->
fd;
8119 if (!pending && n == 0)
return Qnil;
8162 else if (io != write_io) {
8202 #ifdef HAVE_POSIX_FADVISE
8203 struct io_advise_struct {
8211 io_advise_internal(
void *
arg)
8213 struct io_advise_struct *ptr =
arg;
8214 return posix_fadvise(ptr->fd, ptr->offset, ptr->len, ptr->advice);
8220 #ifdef POSIX_FADV_NORMAL
8221 if (sym == sym_normal)
8222 return INT2NUM(POSIX_FADV_NORMAL);
8225 #ifdef POSIX_FADV_RANDOM
8226 if (sym == sym_random)
8227 return INT2NUM(POSIX_FADV_RANDOM);
8230 #ifdef POSIX_FADV_SEQUENTIAL
8231 if (sym == sym_sequential)
8232 return INT2NUM(POSIX_FADV_SEQUENTIAL);
8235 #ifdef POSIX_FADV_WILLNEED
8236 if (sym == sym_willneed)
8237 return INT2NUM(POSIX_FADV_WILLNEED);
8240 #ifdef POSIX_FADV_DONTNEED
8241 if (sym == sym_dontneed)
8242 return INT2NUM(POSIX_FADV_DONTNEED);
8245 #ifdef POSIX_FADV_NOREUSE
8246 if (sym == sym_noreuse)
8247 return INT2NUM(POSIX_FADV_NOREUSE);
8257 struct io_advise_struct ias;
8260 num_adv = io_advise_sym_to_const(advice);
8270 ias.advice =
NUM2INT(num_adv);
8271 ias.offset = offset;
8292 if (advice != sym_normal &&
8293 advice != sym_sequential &&
8294 advice != sym_random &&
8295 advice != sym_willneed &&
8296 advice != sym_dontneed &&
8297 advice != sym_noreuse) {
8348 VALUE advice, offset, len;
8352 rb_scan_args(argc, argv,
"12", &advice, &offset, &len);
8361 #ifdef HAVE_POSIX_FADVISE
8362 return do_io_advise(fptr, advice, off, l);
8364 ((void)off, (
void)l);
8429 if (
NIL_P(timeout)) {
8443 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
8445 # define NUM2IOCTLREQ(num) NUM2ULONG(num)
8448 # define NUM2IOCTLREQ(num) NUM2INT(num)
8480 #define DEFULT_IOCTL_NARG_LEN (256)
8484 linux_iocparm_len(ioctl_req_t
cmd)
8488 if ((cmd & 0xFFFF0000) == 0) {
8493 len = _IOC_SIZE(cmd);
8510 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
8514 len = IOCPARM_LEN(cmd);
8515 #elif defined(__linux__)
8516 len = linux_iocparm_len(cmd);
8527 typedef long fcntl_arg_t;
8530 typedef int fcntl_arg_t;
8541 len =
sizeof(fcntl_arg_t);
8549 #ifdef F_DUPFD_CLOEXEC
8550 case F_DUPFD_CLOEXEC:
8551 len =
sizeof(fcntl_arg_t);
8561 len =
sizeof(fcntl_arg_t);
8571 len =
sizeof(fcntl_arg_t);
8581 len =
sizeof(fcntl_arg_t);
8586 len =
sizeof(
struct f_owner_ex);
8591 len =
sizeof(
struct f_owner_ex);
8596 len =
sizeof(
struct flock);
8601 len =
sizeof(
struct flock);
8606 len =
sizeof(
struct flock);
8626 len =
sizeof(fcntl_arg_t);
8636 len =
sizeof(fcntl_arg_t);
8641 len =
sizeof(fcntl_arg_t);
8672 else if (arg ==
Qtrue) {
8756 nogvl_fcntl(
void *ptr)
8758 struct fcntl_arg *
arg = ptr;
8760 #if defined(F_DUPFD)
8764 return (
VALUE)
fcntl(arg->fd, arg->cmd, arg->narg);
8768 do_fcntl(
int fd,
int cmd,
long narg)
8771 struct fcntl_arg arg;
8778 #if defined(F_DUPFD)
8779 if (retval != -1 && cmd ==
F_DUPFD) {
8799 retval = do_fcntl(fptr->
fd, cmd, narg);
8839 return rb_fcntl(io, req, arg);
8842 #define rb_io_fcntl rb_f_notimplement
8845 #if defined(HAVE_SYSCALL) || defined(HAVE___SYSCALL)
8887 #if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8
8888 # define SYSCALL __syscall
8889 # define NUM2SYSCALLID(x) NUM2LONG(x)
8890 # define RETVAL2NUM(x) LONG2NUM(x)
8891 # if SIZEOF_LONG == 8
8892 long num, retval = -1;
8893 # elif SIZEOF_LONG_LONG == 8
8894 long long num, retval = -1;
8896 # error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
8898 #elif defined(__linux__)
8899 # define SYSCALL syscall
8900 # define NUM2SYSCALLID(x) NUM2LONG(x)
8901 # define RETVAL2NUM(x) LONG2NUM(x)
8909 long num, retval = -1;
8911 # define SYSCALL syscall
8912 # define NUM2SYSCALLID(x) NUM2INT(x)
8913 # define RETVAL2NUM(x) INT2NUM(x)
8914 int num, retval = -1;
8919 rb_warning(
"We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
8927 num = NUM2SYSCALLID(argv[0]); ++
argv;
8928 for (i = argc - 1; i--; ) {
8943 retval = SYSCALL(num);
8946 retval = SYSCALL(num, arg[0]);
8949 retval = SYSCALL(num, arg[0],arg[1]);
8952 retval = SYSCALL(num, arg[0],arg[1],arg[2]);
8955 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
8958 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
8961 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
8964 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
8968 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8972 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8976 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8977 arg[7], arg[8], arg[9]);
8980 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8981 arg[7], arg[8], arg[9], arg[10]);
8984 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8985 arg[7], arg[8], arg[9], arg[10], arg[11]);
8988 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
8989 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
8996 return RETVAL2NUM(retval);
8998 #undef NUM2SYSCALLID
9002 #define rb_f_syscall rb_f_notimplement
9152 int pipes[2], state;
9159 argc =
rb_scan_args(argc, argv,
"02:", &v1, &v2, &opt);
9186 #if DEFAULT_TEXTMODE
9191 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
9197 fptr->
mode |= fmode;
9198 #if DEFAULT_TEXTMODE
9204 fptr2->
mode |= fmode;
9244 #if SIZEOF_LONG > SIZEOF_INT
9300 int orig_argc =
argc;
9411 if (!
NIL_P(offset)) {
9452 arg.
argc = (argc > 1) ? 1 : 0;
9453 if (!
NIL_P(offset)) {
9468 VALUE string, offset, opt;
9479 int mode = O_WRONLY|O_CREAT;
9483 if (
NIL_P(offset)) mode |= O_TRUNC;
9493 if (!
NIL_P(offset)) {
9610 #if defined(ERESTART)
9671 #ifdef HAVE_SENDFILE
9674 # define USE_SENDFILE
9676 # ifdef HAVE_SYS_SENDFILE_H
9677 # include <sys/sendfile.h>
9683 return sendfile(out_fd, in_fd, offset, (
size_t)count);
9686 # elif 0 || defined(__APPLE__)
9690 # define USE_SENDFILE
9692 # ifdef HAVE_SYS_UIO_H
9693 # include <sys/uio.h>
9697 simple_sendfile(
int out_fd,
int in_fd,
off_t *offset,
off_t count)
9703 r = sendfile(in_fd, out_fd, pos, &count,
NULL, 0);
9706 r = sendfile(in_fd, out_fd, pos, (
size_t)count,
NULL, &sbytes, 0);
9708 if (r != 0 && sbytes == 0)
return -1;
9715 return (ssize_t)sbytes;
9726 struct stat src_stat, dst_stat;
9740 if (!
S_ISREG(src_stat.st_mode))
9749 if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
9753 use_pread = src_offset != (
off_t)-1;
9756 if (copy_length == (
off_t)-1) {
9758 copy_length = src_stat.st_size - src_offset;
9763 if (cur == (
off_t)-1 && errno) {
9768 copy_length = src_stat.st_size - cur;
9773 # if SIZEOF_OFF_T > SIZEOF_SIZE_T
9777 ss = (ssize_t)copy_length;
9780 ss = simple_sendfile(stp->
dst_fd, stp->
src_fd, &src_offset, ss);
9788 if (0 < copy_length) {
9789 goto retry_sendfile;
9794 goto retry_sendfile;
9802 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
9818 goto retry_sendfile;
9820 stp->
syserr =
"sendfile";
9834 return read(fd, buf, count);
9842 if (offset == (
off_t)-1) {
9847 ss = pread(stp->
src_fd, buf, len, offset);
9861 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
9873 stp->
syserr = offset == (
off_t)-1 ?
"read" :
"pread";
9886 ss = write(stp->
dst_fd, buf+off, len);
9919 use_eof = copy_length == (
off_t)-1;
9921 use_pread = src_offset != (
off_t)-1;
9932 src_offset = (
off_t)-1;
9936 while (use_eof || 0 < copy_length) {
9937 if (!use_eof && copy_length < (
off_t)
sizeof(
buf)) {
9938 len = (size_t)copy_length;
9972 ret = nogvl_copy_stream_sendfile(stp);
9989 const int buflen = 16*1024;
10011 l = buflen < rest ? buflen : (long)rest;
10013 if (stp->
src_fd == -1) {
10028 if (off != (
off_t)-1)
10033 stp->
total += numwrote;
10059 VALUE src_io, dst_io;
10060 rb_io_t *src_fptr = 0, *dst_fptr = 0;
10075 if (
NIL_P(src_io)) {
10077 int oflags = O_RDONLY;
10079 oflags |= O_NOCTTY;
10082 args[0] = stp->
src;
10090 src_fd = src_fptr->
fd;
10102 if (
NIL_P(dst_io)) {
10104 int oflags = O_WRONLY|O_CREAT|O_TRUNC;
10106 oflags |= O_NOCTTY;
10109 args[0] = stp->
dst;
10122 dst_fd = dst_fptr->fd;
10134 size_t len = src_fptr->
rbuf.
len;
10153 if (dst_fptr &&
io_fflush(dst_fptr) < 0) {
10160 if (src_fd == -1 || dst_fd == -1) {
10228 rb_scan_args(argc, argv,
"22", &src, &dst, &length, &src_offset);
10238 if (
NIL_P(src_offset))
10321 argc =
rb_scan_args(argc, argv,
"11:", &v1, &v2, &opt);
10549 return ARGF.current_file;
10627 VALUE tmp, str, length;
10631 if (!
NIL_P(length)) {
10650 if (
NIL_P(str)) str = tmp;
10653 if (
ARGF.next_p != -1) {
10659 else if (argc >= 1) {
10739 VALUE tmp, str, length;
10763 if (
ARGF.next_p == -1) {
10983 rb_warn(
"ARGF#lines is deprecated; use #each_line instead");
11031 rb_warn(
"ARGF#bytes is deprecated; use #each_byte instead");
11071 rb_warn(
"ARGF#chars is deprecated; use #each_char instead");
11111 rb_warn(
"ARGF#codepoints is deprecated; use #each_codepoint instead");
11143 return ARGF.filename;
11174 return ARGF.current_file;
11234 if (
ARGF.init_p &&
ARGF.next_p == 0) {
11264 if (
ARGF.next_p != -1) {
11367 return ARGF.inplace;
11597 #define rb_intern(str) rb_intern_const(str)
11601 #include <sys/cygwin.h>
11602 static struct __cygwin_perfile pf[] =
11610 cygwin_internal(CW_PERFILE, pf);
11882 #if defined (_WIN32) || defined(__CYGWIN__)