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
114 # define EWOULDBLOCK EAGAIN
117 #if defined(HAVE___SYSCALL) && (defined(__APPLE__) || defined(__OpenBSD__))
119 off_t __syscall(quad_t number, ...);
122 #define IO_RBUF_CAPA_MIN 8192
123 #define IO_CBUF_CAPA_MIN (128*1024)
124 #define IO_RBUF_CAPA_FOR(fptr) (NEED_READCONV(fptr) ? IO_CBUF_CAPA_MIN : IO_RBUF_CAPA_MIN)
125 #define IO_WBUF_CAPA_MIN 8192
130 #define open rb_w32_uopen
165 static VALUE sym_DATA;
168 static VALUE sym_HOLE;
189 rb_bug(
"rb_update_max_fd: invalid fd (%d) given.", fd);
201 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
202 int flags, flags2, ret;
203 flags =
fcntl(fd, F_GETFD);
208 flags2 = flags & ~FD_CLOEXEC;
210 flags2 = flags | FD_CLOEXEC;
211 if (flags != flags2) {
212 ret =
fcntl(fd, F_SETFD, flags2);
214 rb_bug(
"rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2,
strerror(
errno));
234 #elif defined O_NOINHERIT
235 flags |= O_NOINHERIT;
237 ret = open(pathname, flags, mode);
238 if (ret == -1)
return -1;
257 if (oldfd == newfd) {
261 #if defined(HAVE_DUP3) && defined(O_CLOEXEC)
262 static int try_dup3 = 1;
263 if (2 < newfd && try_dup3) {
268 if (
errno == ENOSYS) {
270 ret =
dup2(oldfd, newfd);
274 ret =
dup2(oldfd, newfd);
277 ret =
dup2(oldfd, newfd);
279 if (ret == -1)
return -1;
290 #if defined(HAVE_PIPE2)
291 static int try_pipe2 = 1;
297 if (
errno == ENOSYS) {
308 if (ret == -1)
return -1;
310 if (ret == 0 && fildes[1] == -1) {
327 #if defined(HAVE_FCNTL) && defined(F_DUPFD_CLOEXEC) && defined(F_DUPFD)
328 static int try_dupfd_cloexec = 1;
329 if (try_dupfd_cloexec) {
330 ret =
fcntl(fd, F_DUPFD_CLOEXEC, minfd);
337 if (
errno == EINVAL) {
340 try_dupfd_cloexec = 0;
347 #elif defined(HAVE_FCNTL) && defined(F_DUPFD)
349 #elif defined(HAVE_DUP)
351 if (ret != -1 && ret < minfd) {
352 const int prev_fd = ret;
358 # error "dup() or fcntl(F_DUPFD) must be supported."
360 if (ret == -1)
return -1;
365 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
366 #define ARGF argf_of(argf)
368 #ifdef _STDIO_USES_IOSTREAM
370 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
372 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
374 #elif defined(FILE_COUNT)
375 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
376 #elif defined(FILE_READEND)
377 # define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
378 #elif defined(__BEOS__) || defined(__HAIKU__)
379 # define STDIO_READ_DATA_PENDING(fp) ((fp)->_state._eof == 0)
381 # define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
384 #define GetWriteIO(io) rb_io_get_write_io(io)
386 #define READ_DATA_PENDING(fptr) ((fptr)->rbuf.len)
387 #define READ_DATA_PENDING_COUNT(fptr) ((fptr)->rbuf.len)
388 #define READ_DATA_PENDING_PTR(fptr) ((fptr)->rbuf.ptr+(fptr)->rbuf.off)
389 #define READ_DATA_BUFFERED(fptr) READ_DATA_PENDING(fptr)
391 #define READ_CHAR_PENDING(fptr) ((fptr)->cbuf.len)
392 #define READ_CHAR_PENDING_COUNT(fptr) ((fptr)->cbuf.len)
393 #define READ_CHAR_PENDING_PTR(fptr) ((fptr)->cbuf.ptr+(fptr)->cbuf.off)
396 #define WAIT_FD_IN_WIN32(fptr) \
397 (rb_w32_io_cancelable_p((fptr)->fd) ? 0 : rb_thread_wait_fd((fptr)->fd))
399 #define WAIT_FD_IN_WIN32(fptr)
402 #define READ_CHECK(fptr) do {\
403 if (!READ_DATA_PENDING(fptr)) {\
404 WAIT_FD_IN_WIN32(fptr);\
405 rb_io_check_closed(fptr);\
411 # define S_ISSOCK(m) _S_ISSOCK(m)
414 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
417 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
426 #define NEED_NEWLINE_DECORATOR_ON_READ(fptr) ((fptr)->mode & FMODE_TEXTMODE)
427 #define NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) ((fptr)->mode & FMODE_TEXTMODE)
428 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
430 # define DEFAULT_TEXTMODE FMODE_TEXTMODE
431 # define TEXTMODE_NEWLINE_DECORATOR_ON_WRITE ECONV_CRLF_NEWLINE_DECORATOR
439 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || (fptr)->encs.ecflags & ~ECONV_CRLF_NEWLINE_DECORATOR)
440 #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)))
441 #define SET_BINARY_MODE(fptr) setmode((fptr)->fd, O_BINARY)
443 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) do {\
444 if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {\
445 if (((fptr)->mode & FMODE_READABLE) &&\
446 !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
447 setmode((fptr)->fd, O_BINARY);\
450 setmode((fptr)->fd, O_TEXT);\
455 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) do {\
456 if ((enc2) && ((ecflags) & ECONV_DEFAULT_NEWLINE_DECORATOR)) {\
457 (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
483 if (r < 0 &&
errno) {
495 if (pos < 0 &&
errno) {
502 extra_max = (long)(pos - fptr->
rbuf.
len);
510 for (i = 0; i < fptr->
rbuf.
len; i++) {
511 if (*p ==
'\n') newlines++;
512 if (extra_max == newlines)
break;
517 while (newlines >= 0) {
519 if (newlines == 0)
break;
524 read_size = _read(fptr->
fd, buf, fptr->
rbuf.
len + newlines);
529 if (read_size == fptr->
rbuf.
len) {
551 set_binary_mode_with_seek_cur(
rb_io_t *fptr)
561 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) set_binary_mode_with_seek_cur(fptr)
565 # define DEFAULT_TEXTMODE 0
566 #define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || NEED_NEWLINE_DECORATOR_ON_READ(fptr))
567 #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)))
568 #define SET_BINARY_MODE(fptr) (void)(fptr)
569 #define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) (void)(fptr)
570 #define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) ((void)(enc2), (void)(ecflags))
571 #define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) (void)(fptr)
574 #if !defined HAVE_SHUTDOWN && !defined shutdown
575 #define shutdown(a,b) 0
579 #define is_socket(fd, path) rb_w32_is_socket(fd)
580 #elif !defined(S_ISSOCK)
581 #define is_socket(fd, path) 0
587 if (
fstat(fd, &sbuf) < 0)
589 return S_ISSOCK(sbuf.st_mode);
643 write_io =
RFILE(io)->fptr->tied_io_for_writing;
661 write_io =
RFILE(io)->fptr->tied_io_for_writing;
662 RFILE(io)->fptr->tied_io_for_writing = w;
663 return write_io ? write_io :
Qnil;
689 #if !(defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32))
700 if (r < 0 &&
errno) {
722 #if SIZEOF_LONG > SIZEOF_INT
756 #define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, (ofs), (whence)))
757 #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
885 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
891 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
897 if (
fstat(fptr->
fd, &buf) == 0 &&
899 #
if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(
O_NONBLOCK)
900 && (r =
fcntl(fptr->
fd, F_GETFL)) != -1 &&
927 return read(iis->
fd, iis->
buf, iis->
capa);
934 return write(iis->
fd, iis->
buf, iis->
capa);
1018 return !result ? (
void*)1 : (
void*)
result;
1041 }
else if (ret == 1) {
1084 #if defined(ERESTART)
1091 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1110 #if defined(ERESTART)
1126 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1141 const char *senc, *denc;
1217 long n, r, offset = 0;
1222 if ((n = len) <= 0)
return n;
1259 arg.
ptr = ptr + offset;
1269 if (r == n)
return len;
1293 # define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \
1294 (fmode & FMODE_TEXTMODE) ? (c) : (a))
1305 #define fmode (fptr->mode)
1309 rb_raise(
rb_eArgError,
"ASCII incompatible string written for text mode IO without encoding conversion: %s",
1321 if (!
NIL_P(common_encoding)) {
1330 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
1331 #define fmode (fptr->mode)
1338 setmode(fptr->
fd, O_TEXT);
1341 rb_raise(
rb_eArgError,
"ASCII incompatible string written for text mode IO without encoding conversion: %s",
1356 if (len > 0)
return len;
1371 return (ssize_t)
io_binwrite(0, buf, (
long)size, fptr, 0);
1457 nogvl_fsync(
void *
ptr)
1549 pos =
io_seek(fptr, pos, whence);
1565 if (vwhence == sym_DATA)
1569 if (vwhence == sym_HOLE)
1599 VALUE offset, ptrname;
1602 if (
rb_scan_args(argc, argv,
"11", &offset, &ptrname) == 2) {
1661 if (io ==
ARGF.current_file) {
1755 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
1853 # define rb_io_fsync rb_f_notimplement
1854 # define rb_io_sync rb_f_notimplement
1863 #ifdef HAVE_FDATASYNC
1865 nogvl_fdatasync(
void *ptr)
1869 return (
VALUE)fdatasync(fptr->
fd);
1901 #define rb_io_fdatasync rb_io_fsync
1972 static const char closed[] =
" (closed)";
1974 fptr =
RFILE(obj)->fptr;
2016 if (n <= 0)
return 0;
2017 if (n > len) n = (int)len;
2042 if ((n -= c) <= 0)
break;
2051 if ((n -= c) <= 0)
break;
2100 return (ssize_t)
io_bufread(buf, (
long)size, fptr);
2103 #define SMALLBUF 100
2113 #
if defined(__BEOS__) || defined(__HAIKU__)
2121 if (st.st_size >= pos && pos >= 0) {
2122 siz += st.st_size - pos;
2148 const char *sname, *dname;
2169 #define MORE_CHAR_SUSPENDED Qtrue
2170 #define MORE_CHAR_FINISHED Qnil
2174 const unsigned char *ss, *sp, *se;
2175 unsigned char *ds, *
dp, *de;
2195 ss = sp = (
const unsigned char *)fptr->
rbuf.
ptr + fptr->
rbuf.
off;
2200 fptr->
rbuf.
off += (int)(sp - ss);
2201 fptr->
rbuf.
len -= (int)(sp - ss);
2202 fptr->
cbuf.
len += (int)(dp - ds);
2207 fptr->
rbuf.
off -= putbackable;
2208 fptr->
rbuf.
len += putbackable;
2215 if (cbuf_len0 != fptr->
cbuf.
len)
2232 fptr->
cbuf.
len += (int)(dp - ds);
2239 if (cbuf_len0 != fptr->
cbuf.
len)
2286 len = (len + 1) & ~1L;
2322 int first = !
NIL_P(str);
2355 if (siz == 0) siz = BUFSIZ;
2359 n =
io_fread(str, bytes, siz - bytes, fptr);
2360 if (n == 0 && bytes == 0) {
2368 if (bytes < siz)
break;
2382 oflags =
fcntl(fptr->
fd, F_GETFL);
2424 if ((len =
NUM2LONG(length)) < 0) {
2597 int no_exception = 0;
2711 int no_exceptions = 0;
2793 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2799 if (
NIL_P(length)) {
2819 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2820 previous_mode = set_binary_mode_with_seek_cur(fptr);
2824 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
2825 if (previous_mode == O_TEXT) {
2826 setmode(fptr->
fd, O_TEXT);
2829 if (n == 0)
return Qnil;
2857 if (0 < limit && limit < searchlen)
2858 searchlen = (int)limit;
2859 e = memchr(p, delim, searchlen);
2861 int len = (int)(e-p+1);
2900 if (limit > 0 && pending > limit) pending = limit;
2901 e = memchr(p, delim, pending);
2902 if (e) pending = e - p + 1;
2915 if (e)
return delim;
2939 if (*p != term)
return TRUE;
2941 while (--i && *++p == term);
2944 const char *e = p +
cnt;
2946 while ((p += i) < e &&
rb_enc_ascget(p, e, &i, enc) == term);
2962 if (cnt >
sizeof buf) cnt =
sizeof buf;
2963 if (*p != term)
return TRUE;
2965 while (--i && *++p == term);
2989 e = memchr(p,
'\n', pending);
2991 pending = (int)(e - p + 1);
3014 if (io ==
ARGF.current_file) {
3041 else if (2 <= argc) {
3052 if (enc_io != enc_rs &&
3080 if (
NIL_P(rs) && limit < 0) {
3084 else if (limit == 0) {
3093 int c, newline = -1;
3094 const char *rsptr = 0;
3097 int extra_limit = 16;
3121 newline = (
unsigned char)rsptr[rslen - 1];
3125 while ((c =
appendline(fptr, newline, &str, &limit)) !=
EOF) {
3126 const char *s, *p, *pp, *e;
3134 if (pp != p)
continue;
3135 if (!rspara)
rscheck(rsptr, rslen, rs);
3136 if (
memcmp(p, rsptr, rslen) == 0)
break;
3156 if (rspara && c !=
EOF)
3162 if (!
NIL_P(str) && !nolimit) {
3164 if (io ==
ARGF.current_file) {
3326 VALUE line, ary, rs;
3391 rb_warn(
"IO#lines is deprecated; use #each_line instead");
3442 rb_warn(
"IO#bytes is deprecated; use #each_byte instead");
3596 rb_warn(
"IO#chars is deprecated; use #each_char instead");
3703 rb_warn(
"IO#codepoints is deprecated; use #each_codepoint instead");
3888 #if SIZEOF_LONG > SIZEOF_INT
3930 if (isatty(fptr->
fd) == 0)
3935 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
3958 if (io != write_io) {
3960 if (fptr && 0 <= (fd = fptr->
fd)) {
3962 if (!(ret & FD_CLOEXEC))
return Qfalse;
3967 if (fptr && 0 <= (fd = fptr->
fd)) {
3969 if (!(ret & FD_CLOEXEC))
return Qfalse;
3974 #define rb_io_close_on_exec_p rb_f_notimplement
3977 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
4001 int flag =
RTEST(arg) ? FD_CLOEXEC : 0;
4007 if (io != write_io) {
4009 if (fptr && 0 <= (fd = fptr->
fd)) {
4011 if ((ret & FD_CLOEXEC) != flag) {
4012 ret = (ret & ~FD_CLOEXEC) | flag;
4013 ret =
fcntl(fd, F_SETFD, ret);
4021 if (fptr && 0 <= (fd = fptr->
fd)) {
4023 if ((ret & FD_CLOEXEC) != flag) {
4024 ret = (ret & ~FD_CLOEXEC) | flag;
4025 ret =
fcntl(fd, F_SETFD, ret);
4032 #define rb_io_set_close_on_exec rb_f_notimplement
4035 #define FMODE_PREP (1<<16)
4036 #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
4037 #define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv))
4042 unsigned char *ds, *
dp, *de;
4046 unsigned char buf[1024];
4052 de = buf +
sizeof(
buf);
4092 fptr->
wbuf.
len += (int)(dp - ds);
4119 return (
void*)(
intptr_t)close(*fd);
4140 return (
void*)(
intptr_t)fclose(file);
4147 return fclose(file);
4188 else if (stdio_file) {
4202 if (!
NIL_P(err) && !noraise) {
4203 switch (
TYPE(err)) {
4259 if (!fptr)
return 0;
4300 if (io != write_io) {
4301 write_fptr =
RFILE(write_io)->fptr;
4302 if (write_fptr && 0 <= write_fptr->
fd) {
4307 fptr =
RFILE(io)->fptr;
4308 if (!fptr)
return Qnil;
4309 if (fptr->
fd < 0)
return Qnil;
4403 if (io != write_io) {
4404 write_fptr =
RFILE(write_io)->fptr;
4405 if (write_fptr && 0 <= write_fptr->
fd) {
4410 fptr =
RFILE(io)->fptr;
4453 if (io != write_io) {
4458 RFILE(io)->fptr = wfptr;
4517 if (io != write_io) {
4542 VALUE offset, ptrname;
4547 if (
rb_scan_args(argc, argv,
"11", &offset, &ptrname) == 2) {
4557 rb_warn(
"sysseek for buffered IO");
4560 pos = lseek(fptr->
fd, pos, whence);
4595 rb_warn(
"syswrite for buffered IO");
4636 if (ilen == 0)
return str;
4670 if (n == 0 && ilen > 0) {
4791 case FMODE_READWRITE:
4802 static const char bom_prefix[] =
"bom|utf-";
4803 enum {bom_prefix_len = (int)
sizeof(bom_prefix) - 1};
4805 const char *p =
strchr(name,
':');
4806 len = p ? (long)(p - name) : (long)
strlen(name);
4808 return len > bom_prefix_len &&
STRNCASECMP(name, bom_prefix, bom_prefix_len) == 0;
4815 const char *m = modestr, *p =
NULL;
4865 switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
4877 if (oflags & O_APPEND) {
4880 if (oflags & O_TRUNC) {
4883 if (oflags & O_CREAT) {
4907 case FMODE_READWRITE:
4940 # define MODE_BINARY(a,b) ((oflags & O_BINARY) ? (b) : (a))
4942 # define MODE_BINARY(a,b) (a)
4944 int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
4945 if (oflags & O_APPEND) {
4946 if (accmode == O_WRONLY) {
4949 if (accmode == O_RDWR) {
4953 switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
4973 int default_ext = 0;
4983 else if (intern ==
NULL) {
4989 *enc = (default_ext && intern != ext) ?
NULL : ext;
5001 rb_warn(
"Unsupported encoding %s ignored", name);
5010 int fmode = fmode_p ? *fmode_p : 0;
5017 long len = (p++) - estr;
5026 memcpy(encname, estr, len);
5027 encname[
len] =
'\0';
5038 memcpy(encname, estr, len);
5039 encname[
len] =
'\0';
5044 if (fmode_p) *fmode_p = fmode;
5056 if (*p ==
'-' && *(p+1) ==
'\0') {
5086 if (v !=
Qnil) encoding = v;
5088 if (v !=
Qnil) extenc = v;
5090 if (v !=
Qundef) intenc = v;
5095 rb_warn(
"Ignoring encoding parameter '%s': %s_encoding is used",
5097 extenc ==
Qundef ?
"internal" :
"external");
5105 if (
NIL_P(intenc)) {
5112 if (*p ==
'-' && *(p+1) ==
'\0') {
5123 if (extencoding == intencoding) {
5127 if (!
NIL_P(encoding)) {
5148 int fmode = *fmode_p;
5156 if (!(fmode & FMODE_BINMODE) &&
5161 #if !DEFAULT_TEXTMODE
5162 else if (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
5172 if (!
NIL_P(opthash)) {
5200 int *oflags_p,
int *fmode_p,
convconfig_t *convconfig_p)
5207 int has_enc = 0, has_vmode = 0;
5245 if (
NIL_P(opthash)) {
5249 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5252 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5267 #if DEFAULT_TEXTMODE
5268 else if (
NIL_P(vmode)) {
5275 if (!
NIL_P(vmode)) {
5286 if (!
NIL_P(*vperm_p)) {
5298 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
5301 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
5319 convconfig_p->enc = enc;
5320 convconfig_p->enc2 = enc2;
5321 convconfig_p->ecflags = ecflags;
5322 convconfig_p->ecopts = ecopts;
5380 file = fdopen(fd, modestr);
5391 file = fdopen(fd, modestr);
5396 #elif defined(__sun)
5405 if (setvbuf(file,
NULL, _IOFBF, 0) != 0)
5406 rb_warn(
"setvbuf() can't be honoured (fd=%d)", fd);
5414 if (isatty(fptr->
fd))
5424 VALUE b1, b2, b3, b4;
5442 return ENCINDEX_UTF_16BE;
5453 return ENCINDEX_UTF_32LE;
5460 return ENCINDEX_UTF_16LE;
5471 return ENCINDEX_UTF_32BE;
5513 convconfig->enc, convconfig->enc2);
5517 fptr->
encs = *convconfig;
5530 const char *p =
strchr(modestr,
':');
5542 convconfig.ecflags = 0;
5543 convconfig.ecopts =
Qnil;
5566 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
5589 if (list->
fptr == fptr) {
5595 while (list->
next) {
5622 #if !defined(HAVE_FORK) && !defined(_WIN32)
5669 #define HAVE_SPAWNV 1
5670 #define spawnv(mode, cmd, args) rb_w32_uaspawn((mode), (cmd), (args))
5671 #define spawn(mode, cmd) rb_w32_uspawn((mode), (cmd), 0)
5674 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5686 popen_redirect(
struct popen_arg *p)
5689 close(p->write_pair[1]);
5690 if (p->write_pair[0] != 0) {
5691 dup2(p->write_pair[0], 0);
5692 close(p->write_pair[0]);
5695 if (p->pair[1] != 1) {
5696 dup2(p->pair[1], 1);
5702 if (p->pair[1] != 1) {
5703 dup2(p->pair[1], 1);
5709 if (p->pair[0] != 0) {
5710 dup2(p->pair[0], 0);
5716 #if defined(__linux__)
5727 linux_get_maxfd(
void)
5730 char buf[4096], *p, *np, *e;
5733 if (fd == -1)
return -1;
5734 ss = read(fd, buf,
sizeof(buf));
5735 if (ss == -1)
goto err;
5738 while ((
int)
sizeof(
"FDSize:\t0\n")-1 <= e-p &&
5739 (np = memchr(p,
'\n', e-p)) !=
NULL) {
5740 if (
memcmp(p,
"FDSize:",
sizeof(
"FDSize:")-1) == 0) {
5742 p +=
sizeof(
"FDSize:")-1;
5766 ret =
fcntl(0, F_MAXFD);
5768 maxhint = max = ret;
5769 #elif defined(__linux__)
5770 ret = linux_get_maxfd();
5777 for (fd = lowfd; fd <=
max; fd++) {
5778 if (!
NIL_P(noclose_fds) &&
5781 ret =
fcntl(fd, F_GETFD);
5782 if (ret != -1 && !(ret & FD_CLOEXEC)) {
5783 fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
5785 #define CONTIGUOUS_CLOSED_FDS 20
5787 if (max < fd + CONTIGUOUS_CLOSED_FDS)
5788 max = fd + CONTIGUOUS_CLOSED_FDS;
5794 popen_exec(
void *pp,
char *errmsg,
size_t errmsg_len)
5796 struct popen_arg *p = (
struct popen_arg*)pp;
5812 #if defined(HAVE_FORK)
5814 char errmsg[80] = {
'\0' };
5816 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5817 struct popen_arg arg;
5820 #if defined(HAVE_SPAWNV)
5821 # if defined(HAVE_SPAWNVE)
5822 # define DO_SPAWN(cmd, args, envp) ((args) ? \
5823 spawnve(P_NOWAIT, (cmd), (args), (envp)) : \
5824 spawne(P_NOWAIT, (cmd), (envp)))
5826 # define DO_SPAWN(cmd, args, envp) ((args) ? \
5827 spawnv(P_NOWAIT, (cmd), (args)) : \
5828 spawn(P_NOWAIT, (cmd)))
5830 # if !defined(HAVE_FORK)
5832 # if defined(HAVE_SPAWNVE)
5837 #if !defined(HAVE_FORK)
5843 #if !defined(HAVE_FORK)
5844 const char *
cmd = 0;
5845 #if !defined(HAVE_SPAWNV)
5854 #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
5855 arg.execarg_obj = execarg_obj;
5858 arg.pair[0] = arg.pair[1] = -1;
5859 arg.write_pair[0] = arg.write_pair[1] = -1;
5860 # if !defined(HAVE_FORK)
5867 if (
rb_pipe(arg.write_pair) < 0)
5871 close(arg.write_pair[0]);
5872 close(arg.write_pair[1]);
5896 if (!
NIL_P(execarg_obj)) {
5898 # if defined(HAVE_FORK)
5902 # if defined(HAVE_SPAWNVE)
5905 while ((pid = DO_SPAWN(cmd, args, envp)) == -1) {
5907 switch (e =
errno) {
5909 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5922 # if defined(HAVE_FORK)
5926 popen_redirect(&arg);
5938 # if defined(HAVE_FORK)
5944 close(arg.write_pair[0]);
5945 close(arg.write_pair[1]);
5948 # if defined(HAVE_FORK)
5957 close(arg.write_pair[0]);
5958 write_fd = arg.write_pair[1];
5960 else if (fmode & FMODE_READABLE) {
5973 if (!
NIL_P(execarg_obj)) {
5977 fp = popen(cmd, modestr);
5990 fptr->
encs = *convconfig;
5991 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
6001 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
6003 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
6009 if (0 <= write_fd) {
6012 write_fptr->
fd = write_fd;
6014 fptr->
mode &= ~FMODE_WRITABLE;
6019 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
6030 #if !defined(HAVE_FORK)
6032 "fork() function is unimplemented on this machine");
6049 return pipe_open(execarg_obj, modestr, fmode, convconfig);
6143 const char *modestr;
6158 int ex = !
NIL_P(opt);
6166 #if SIZEOF_LONG > SIZEOF_INT
6167 if (len > INT_MAX) {
6182 if (!
NIL_P(execarg_obj)) {
6191 port =
pipe_open(execarg_obj, modestr, fmode, &convconfig);
6211 VALUE *fname_p,
int *oflags_p,
int *fmode_p,
6214 VALUE opt, fname, vmode, vperm;
6218 argc =
rb_scan_args(argc, argv,
"12:", &fname, &vmode, &vperm, &opt);
6307 VALUE fname, vmode, vperm;
6312 rb_scan_args(argc, argv,
"12", &fname, &vmode, &vperm);
6323 if (
NIL_P(vperm)) perm = 0666;
6449 int redirect =
FALSE;
6457 VALUE tmp = argv[0];
6498 oflags, fmode, &convconfig, perm);
6523 if (fptr == orig)
return io;
6529 "%s can't change access mode from \"%s\" to \"%s\"",
6556 #
if defined (__CYGWIN__) || !defined(HAVE_FORK)
6617 VALUE fname, nmode, opt;
6621 if (
rb_scan_args(argc, argv,
"11:", &fname, &nmode, &opt) == 1) {
6630 fptr =
RFILE(file)->fptr;
6643 (fptr->
mode & FMODE_READWRITE)) {
6645 "%s can't change access mode from \"%s\" to \"%s\"",
6650 fptr->
encs = convconfig;
6683 else if (fptr->
stdio_file == stdout && isatty(fptr->
fd)) {
6725 #if defined (__CYGWIN__) || !defined(HAVE_FORK)
6740 if (io != write_io) {
6781 if (argc == 0)
return Qnil;
6829 for (i=0; i<
argc; i++) {
6935 if (len == 0)
return 0;
6937 return ptr[len - 1] ==
c;
6939 return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) ==
c;
6993 for (i=0; i<
argc; i++) {
7060 for (i=0; i<
argc; i++) {
7066 else if (argc > 1) {
7146 if (fwrite(mesg,
sizeof(
char), (
size_t)len, stderr) < (
size_t)len) {
7168 if (fwrite(
RSTRING_PTR(mesg),
sizeof(
char), len, stderr) < len) {
7223 if (path && strcmp(path,
"-")) klass =
rb_cFile;
7235 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
7236 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
7407 int fd,
fmode, oflags = O_RDONLY;
7410 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7417 argc =
rb_scan_args(argc, argv,
"11:", &fnum, &vmode, &opt);
7424 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7425 oflags =
fcntl(fd, F_GETFL);
7431 #if defined(HAVE_FCNTL) && defined(F_GETFL)
7447 fp->
encs = convconfig;
7452 else if (
fileno(stdout) == fd)
7454 else if (
fileno(stderr) == fd)
7485 if (
RFILE(io)->fptr) {
7488 if (0 < argc && argc < 3) {
7508 rb_warn(
"%s::new() does not take block; use %s::open() instead",
7569 if (!
RTEST(autoclose))
7579 struct argf *p = ptr;
7589 struct argf *p = ptr;
7597 const struct argf *p = ptr;
7598 size_t size =
sizeof(*p);
7707 #define next_argv() argf_next_argv(argf)
7708 #define ARGF_GENERIC_INPUT_P() \
7709 (ARGF.current_file == rb_stdin && !RB_TYPE_P(ARGF.current_file, T_FILE))
7710 #define ARGF_FORWARD(argc, argv) do {\
7711 if (ARGF_GENERIC_INPUT_P())\
7712 return argf_forward((argc), (argv), argf);\
7714 #define NEXT_ARGF_FORWARD(argc, argv) do {\
7715 if (!next_argv()) return Qnil;\
7716 ARGF_FORWARD((argc), (argv));\
7736 int stdout_binmode = 0;
7745 if (
ARGF.init_p == 0) {
7763 if (
ARGF.next_p == 1) {
7768 if (
strlen(fn) == 1 && fn[0] ==
'-') {
7771 rb_warn(
"Can't do inplace edit for stdio; skipping");
7781 #ifndef NO_SAFE_RENAME
7791 if (*
ARGF.inplace) {
7794 #ifdef NO_SAFE_RENAME
7798 rb_warn(
"Can't rename %s to %s: %s, skipping file",
7805 rb_warn(
"Can't rename %s to %s: %s, skipping file",
7813 #ifdef NO_SAFE_RENAME
7814 rb_fatal(
"Can't do inplace edit without backup");
7816 if (unlink(fn) < 0) {
7817 rb_warn(
"Can't remove %s: %s, skipping file",
7825 #ifndef NO_SAFE_RENAME
7828 fchmod(fw, st.st_mode);
7830 chmod(fn, st.st_mode);
7832 if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
7835 err = fchown(fw, st.st_uid, st.st_gid);
7837 err =
chown(fn, st.st_uid, st.st_gid);
7839 if (err &&
getuid() == 0 && st2.st_uid == 0) {
7841 rb_warn(
"Can't set owner/group of %s to same as %s: %s, skipping file",
7855 if (!
ARGF.binmode) {
7859 if (!
NIL_P(write_io)) {
7865 if (
ARGF.encs.enc) {
7871 if (!
ARGF.binmode) {
7873 #ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
7874 fptr->
encs.
ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
7885 else if (
ARGF.next_p == -1) {
7889 rb_warn(
"Can't do inplace edit for stdio");
7893 if (
ARGF.init_p == -1)
ARGF.init_p = 1;
7901 long lineno =
ARGF.lineno;
7922 ARGF.lineno = ++lineno;
7940 ARGF.last_lineno =
ARGF.lineno = n;
8132 long lineno =
ARGF.lineno;
8170 volatile VALUE port;
8186 #ifdef HAVE_SYS_SELECT_H
8187 #include <sys/select.h>
8210 if (max < fptr->fd) max = fptr->
fd;
8221 if (!
NIL_P(write)) {
8227 if (max < fptr->fd) max = fptr->
fd;
8234 if (!
NIL_P(except)) {
8241 if (max < fptr->fd) max = fptr->
fd;
8242 if (io != write_io) {
8245 if (max < fptr->fd) max = fptr->
fd;
8260 if (!pending && n == 0)
return Qnil;
8303 else if (io != write_io) {
8343 #ifdef HAVE_POSIX_FADVISE
8344 struct io_advise_struct {
8352 io_advise_internal(
void *
arg)
8354 struct io_advise_struct *ptr =
arg;
8355 return posix_fadvise(ptr->fd, ptr->offset, ptr->len, ptr->advice);
8361 #ifdef POSIX_FADV_NORMAL
8362 if (sym == sym_normal)
8363 return INT2NUM(POSIX_FADV_NORMAL);
8366 #ifdef POSIX_FADV_RANDOM
8367 if (sym == sym_random)
8368 return INT2NUM(POSIX_FADV_RANDOM);
8371 #ifdef POSIX_FADV_SEQUENTIAL
8372 if (sym == sym_sequential)
8373 return INT2NUM(POSIX_FADV_SEQUENTIAL);
8376 #ifdef POSIX_FADV_WILLNEED
8377 if (sym == sym_willneed)
8378 return INT2NUM(POSIX_FADV_WILLNEED);
8381 #ifdef POSIX_FADV_DONTNEED
8382 if (sym == sym_dontneed)
8383 return INT2NUM(POSIX_FADV_DONTNEED);
8386 #ifdef POSIX_FADV_NOREUSE
8387 if (sym == sym_noreuse)
8388 return INT2NUM(POSIX_FADV_NOREUSE);
8398 struct io_advise_struct ias;
8401 num_adv = io_advise_sym_to_const(advice);
8411 ias.advice =
NUM2INT(num_adv);
8412 ias.offset = offset;
8433 if (advice != sym_normal &&
8434 advice != sym_sequential &&
8435 advice != sym_random &&
8436 advice != sym_willneed &&
8437 advice != sym_dontneed &&
8438 advice != sym_noreuse) {
8490 VALUE advice, offset, len;
8494 rb_scan_args(argc, argv,
"12", &advice, &offset, &len);
8503 #ifdef HAVE_POSIX_FADVISE
8504 return do_io_advise(fptr, advice, off, l);
8506 ((void)off, (
void)l);
8664 if (
NIL_P(timeout)) {
8678 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
8680 # define NUM2IOCTLREQ(num) NUM2ULONG(num)
8683 # define NUM2IOCTLREQ(num) NUM2INT(num)
8715 #define DEFULT_IOCTL_NARG_LEN (256)
8717 #if defined(__linux__) && defined(_IOC_SIZE)
8719 linux_iocparm_len(ioctl_req_t
cmd)
8723 if ((cmd & 0xFFFF0000) == 0) {
8728 len = _IOC_SIZE(cmd);
8745 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
8749 len = IOCPARM_LEN(cmd);
8750 #elif defined(__linux__) && defined(_IOC_SIZE)
8751 len = linux_iocparm_len(cmd);
8762 typedef long fcntl_arg_t;
8765 typedef int fcntl_arg_t;
8776 len =
sizeof(fcntl_arg_t);
8784 #ifdef F_DUPFD_CLOEXEC
8785 case F_DUPFD_CLOEXEC:
8786 len =
sizeof(fcntl_arg_t);
8796 len =
sizeof(fcntl_arg_t);
8806 len =
sizeof(fcntl_arg_t);
8816 len =
sizeof(fcntl_arg_t);
8821 len =
sizeof(
struct f_owner_ex);
8826 len =
sizeof(
struct f_owner_ex);
8831 len =
sizeof(
struct flock);
8836 len =
sizeof(
struct flock);
8841 len =
sizeof(
struct flock);
8861 len =
sizeof(fcntl_arg_t);
8871 len =
sizeof(fcntl_arg_t);
8876 len =
sizeof(fcntl_arg_t);
8907 else if (arg ==
Qtrue) {
8991 nogvl_fcntl(
void *ptr)
8993 struct fcntl_arg *
arg = ptr;
8995 #if defined(F_DUPFD)
8999 return (
VALUE)
fcntl(arg->fd, arg->cmd, arg->narg);
9003 do_fcntl(
int fd,
int cmd,
long narg)
9006 struct fcntl_arg arg;
9013 #if defined(F_DUPFD)
9014 if (retval != -1 && cmd ==
F_DUPFD) {
9034 retval = do_fcntl(fptr->
fd, cmd, narg);
9074 return rb_fcntl(io, req, arg);
9077 #define rb_io_fcntl rb_f_notimplement
9080 #if defined(HAVE_SYSCALL) || defined(HAVE___SYSCALL)
9122 #if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8
9123 # define SYSCALL __syscall
9124 # define NUM2SYSCALLID(x) NUM2LONG(x)
9125 # define RETVAL2NUM(x) LONG2NUM(x)
9126 # if SIZEOF_LONG == 8
9127 long num, retval = -1;
9128 # elif SIZEOF_LONG_LONG == 8
9129 long long num, retval = -1;
9131 # error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
9133 #elif defined(__linux__)
9134 # define SYSCALL syscall
9135 # define NUM2SYSCALLID(x) NUM2LONG(x)
9136 # define RETVAL2NUM(x) LONG2NUM(x)
9144 long num, retval = -1;
9146 # define SYSCALL syscall
9147 # define NUM2SYSCALLID(x) NUM2INT(x)
9148 # define RETVAL2NUM(x) INT2NUM(x)
9149 int num, retval = -1;
9154 rb_warning(
"We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
9162 num = NUM2SYSCALLID(argv[0]); ++
argv;
9163 for (i = argc - 1; i--; ) {
9178 retval = SYSCALL(num);
9181 retval = SYSCALL(num, arg[0]);
9184 retval = SYSCALL(num, arg[0],arg[1]);
9187 retval = SYSCALL(num, arg[0],arg[1],arg[2]);
9190 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
9193 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
9196 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
9199 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
9203 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9207 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9211 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9212 arg[7], arg[8], arg[9]);
9215 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9216 arg[7], arg[8], arg[9], arg[10]);
9219 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9220 arg[7], arg[8], arg[9], arg[10], arg[11]);
9223 retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
9224 arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
9231 return RETVAL2NUM(retval);
9233 #undef NUM2SYSCALLID
9237 #define rb_f_syscall rb_f_notimplement
9387 int pipes[2], state;
9388 VALUE r, w, args[3], v1, v2;
9394 argc =
rb_scan_args(argc, argv,
"02:", &v1, &v2, &opt);
9421 #if DEFAULT_TEXTMODE
9426 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
9432 fptr->
mode |= fmode;
9433 #if DEFAULT_TEXTMODE
9439 fptr2->
mode |= fmode;
9479 #if SIZEOF_LONG > SIZEOF_INT
9535 int orig_argc =
argc;
9646 if (!
NIL_P(offset)) {
9687 arg.
argc = (argc > 1) ? 1 : 0;
9688 if (!
NIL_P(offset)) {
9703 VALUE string, offset, opt;
9714 int mode = O_WRONLY|O_CREAT;
9718 if (
NIL_P(offset)) mode |= O_TRUNC;
9728 if (!
NIL_P(offset)) {
9845 #if defined(ERESTART)
9906 #ifdef HAVE_SENDFILE
9909 # define USE_SENDFILE
9911 # ifdef HAVE_SYS_SENDFILE_H
9912 # include <sys/sendfile.h>
9918 return sendfile(out_fd, in_fd, offset, (
size_t)count);
9921 # elif 0 || defined(__APPLE__)
9925 # define USE_SENDFILE
9927 # ifdef HAVE_SYS_UIO_H
9928 # include <sys/uio.h>
9932 simple_sendfile(
int out_fd,
int in_fd,
off_t *offset,
off_t count)
9938 r = sendfile(in_fd, out_fd, pos, &count,
NULL, 0);
9941 r = sendfile(in_fd, out_fd, pos, (
size_t)count,
NULL, &sbytes, 0);
9943 if (r != 0 && sbytes == 0)
return -1;
9950 return (ssize_t)sbytes;
9961 struct stat src_stat, dst_stat;
9975 if (!
S_ISREG(src_stat.st_mode))
9984 if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
9988 use_pread = src_offset != (
off_t)-1;
9991 if (copy_length == (
off_t)-1) {
9993 copy_length = src_stat.st_size - src_offset;
9998 if (cur == (
off_t)-1 && errno) {
10003 copy_length = src_stat.st_size - cur;
10008 # if SIZEOF_OFF_T > SIZEOF_SIZE_T
10012 ss = (ssize_t)copy_length;
10015 ss = simple_sendfile(stp->
dst_fd, stp->
src_fd, &src_offset, ss);
10023 if (0 < copy_length) {
10024 goto retry_sendfile;
10029 goto retry_sendfile;
10037 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
10053 goto retry_sendfile;
10055 stp->
syserr =
"sendfile";
10069 return read(fd, buf, count);
10077 if (offset == (
off_t)-1) {
10082 ss = pread(stp->
src_fd, buf, len, offset);
10096 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
10108 stp->
syserr = offset == (
off_t)-1 ?
"read" :
"pread";
10121 ss = write(stp->
dst_fd, buf+off, len);
10154 use_eof = copy_length == (
off_t)-1;
10156 use_pread = src_offset != (
off_t)-1;
10167 src_offset = (
off_t)-1;
10171 while (use_eof || 0 < copy_length) {
10172 if (!use_eof && copy_length < (
off_t)
sizeof(
buf)) {
10173 len = (size_t)copy_length;
10202 #ifdef USE_SENDFILE
10206 #ifdef USE_SENDFILE
10207 ret = nogvl_copy_stream_sendfile(stp);
10214 #ifdef USE_SENDFILE
10224 const int buflen = 16*1024;
10231 if (stp->
src_fd == -1) {
10246 l = buflen < rest ? buflen : (long)rest;
10248 if (stp->
src_fd == -1) {
10263 if (off != (
off_t)-1)
10268 stp->
total += numwrote;
10295 rb_io_t *src_fptr = 0, *dst_fptr = 0;
10297 const int common_oflags = 0
10307 if (src_io ==
argf ||
10318 args[1] =
INT2NUM(O_RDONLY|common_oflags);
10325 src_fd = src_fptr->
fd;
10329 if (dst_io ==
argf ||
10340 args[1] =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC|common_oflags);
10352 dst_fd = dst_fptr->fd;
10364 size_t len = src_fptr->
rbuf.
len;
10383 if (dst_fptr &&
io_fflush(dst_fptr) < 0) {
10390 if (src_fd == -1 || dst_fd == -1) {
10458 rb_scan_args(argc, argv,
"22", &src, &dst, &length, &src_offset);
10468 if (
NIL_P(src_offset))
10551 argc =
rb_scan_args(argc, argv,
"11:", &v1, &v2, &opt);
10779 return ARGF.current_file;
10857 VALUE tmp, str, length;
10861 if (!
NIL_P(length)) {
10880 if (
NIL_P(str)) str = tmp;
10883 if (
ARGF.next_p != -1) {
10889 else if (argc >= 1) {
10958 VALUE tmp, str, length;
10982 if (
ARGF.next_p == -1) {
11152 #define FOREACH_ARGF() while (next_argv())
11157 const VALUE current =
ARGF.current_file;
11159 if (
ARGF.init_p == -1 || current !=
ARGF.current_file) {
11221 rb_warn(
"ARGF#lines is deprecated; use #each_line instead");
11268 rb_warn(
"ARGF#bytes is deprecated; use #each_byte instead");
11307 rb_warn(
"ARGF#chars is deprecated; use #each_char instead");
11346 rb_warn(
"ARGF#codepoints is deprecated; use #each_codepoint instead");
11378 return ARGF.filename;
11409 return ARGF.current_file;
11469 if (
ARGF.init_p &&
ARGF.next_p == 0) {
11499 if (
ARGF.next_p != -1) {
11602 return ARGF.inplace;
11684 #if EAGAIN != EWOULDBLOCK
11701 #if EAGAIN != EWOULDBLOCK
11714 rb_bug(
"invalid read/write type passed to rb_readwrite_sys_fail: %d", writable);
11877 #define rb_intern(str) rb_intern_const(str)
11881 #include <sys/cygwin.h>
11882 static struct __cygwin_perfile pf[] =
11890 cygwin_internal(CW_PERFILE, pf);
11930 #if EAGAIN == EWOULDBLOCK
12194 #if defined (_WIN32) || defined(__CYGWIN__)