| 1 |
/* |
| 2 |
** io.c - IO class |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "mruby.h" |
| 6 |
#include "mruby/array.h" |
| 7 |
#include "mruby/class.h" |
| 8 |
#include "mruby/data.h" |
| 9 |
#include "mruby/hash.h" |
| 10 |
#include "mruby/string.h" |
| 11 |
#include "mruby/variable.h" |
| 12 |
#include "mruby/ext/io.h" |
| 13 |
#include "mruby/error.h" |
| 14 |
#include "mruby/internal.h" |
| 15 |
#include "mruby/presym.h" |
| 16 |
|
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/stat.h> |
| 19 |
|
| 20 |
#if defined(_WIN32) || defined(_WIN64) |
| 21 |
#include <winsock.h> |
| 22 |
#include <io.h> |
| 23 |
#include <basetsd.h> |
| 24 |
#define open _open |
| 25 |
#define close _close |
| 26 |
#define dup _dup |
| 27 |
#define dup2 _dup2 |
| 28 |
#define read _read |
| 29 |
#define write _write |
| 30 |
#define lseek _lseek |
| 31 |
#define isatty _isatty |
| 32 |
#define WEXITSTATUS(x) (x) |
| 33 |
typedef int fsize_t; |
| 34 |
typedef long ftime_t; |
| 35 |
typedef long fsuseconds_t; |
| 36 |
typedef int fmode_t; |
| 37 |
typedef int fssize_t; |
| 38 |
|
| 39 |
#ifndef O_TMPFILE |
| 40 |
#define O_TMPFILE O_TEMPORARY |
| 41 |
#endif |
| 42 |
|
| 43 |
#else |
| 44 |
#include <sys/wait.h> |
| 45 |
#include <sys/time.h> |
| 46 |
#include <unistd.h> |
| 47 |
typedef size_t fsize_t; |
| 48 |
typedef time_t ftime_t; |
| 49 |
#ifdef __DJGPP__ |
| 50 |
typedef long fsuseconds_t; |
| 51 |
#else |
| 52 |
typedef suseconds_t fsuseconds_t; |
| 53 |
#endif |
| 54 |
typedef mode_t fmode_t; |
| 55 |
typedef ssize_t fssize_t; |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef _MSC_VER |
| 59 |
typedef mrb_int pid_t; |
| 60 |
#endif |
| 61 |
|
| 62 |
#include <fcntl.h> |
| 63 |
|
| 64 |
#include <errno.h> |
| 65 |
#include <string.h> |
| 66 |
|
| 67 |
#define OPEN_ACCESS_MODE_FLAGS (O_RDONLY | O_WRONLY | O_RDWR) |
| 68 |
#define OPEN_RDONLY_P(f) ((mrb_bool)(((f) & OPEN_ACCESS_MODE_FLAGS) == O_RDONLY)) |
| 69 |
#define OPEN_WRONLY_P(f) ((mrb_bool)(((f) & OPEN_ACCESS_MODE_FLAGS) == O_WRONLY)) |
| 70 |
#define OPEN_RDWR_P(f) ((mrb_bool)(((f) & OPEN_ACCESS_MODE_FLAGS) == O_RDWR)) |
| 71 |
#define OPEN_READABLE_P(f) ((mrb_bool)(OPEN_RDONLY_P(f) || OPEN_RDWR_P(f))) |
| 72 |
#define OPEN_WRITABLE_P(f) ((mrb_bool)(OPEN_WRONLY_P(f) || OPEN_RDWR_P(f))) |
| 73 |
|
| 74 |
static void io_free(mrb_state *mrb, void *ptr); |
| 75 |
struct mrb_data_type mrb_io_type = { "IO", io_free }; |
| 76 |
|
| 77 |
|
| 78 |
static int io_modestr_to_flags(mrb_state *mrb, const char *modestr); |
| 79 |
static int io_mode_to_flags(mrb_state *mrb, mrb_value mode); |
| 80 |
static void fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet); |
| 81 |
|
| 82 |
static struct mrb_io* |
| 83 |
io_get_open_fptr(mrb_state *mrb, mrb_value io) |
| 84 |
{ |
| 85 |
struct mrb_io *fptr; |
| 86 |
|
| 87 |
fptr = (struct mrb_io*)mrb_data_get_ptr(mrb, io, &mrb_io_type); |
| 88 |
if (fptr == NULL) { |
| 89 |
mrb_raise(mrb, E_IO_ERROR, "uninitialized stream"); |
| 90 |
} |
| 91 |
if (fptr->fd < 0) { |
| 92 |
mrb_raise(mrb, E_IO_ERROR, "closed stream"); |
| 93 |
} |
| 94 |
return fptr; |
| 95 |
} |
| 96 |
|
| 97 |
#if !defined(MRB_NO_IO_POPEN) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
| 98 |
# define MRB_NO_IO_POPEN 1 |
| 99 |
#endif |
| 100 |
|
| 101 |
#ifndef MRB_NO_IO_POPEN |
| 102 |
static void |
| 103 |
io_set_process_status(mrb_state *mrb, pid_t pid, int status) |
| 104 |
{ |
| 105 |
struct RClass *c_process, *c_status; |
| 106 |
mrb_value v; |
| 107 |
|
| 108 |
c_status = NULL; |
| 109 |
if (mrb_class_defined_id(mrb, MRB_SYM(Process))) { |
| 110 |
c_process = mrb_module_get_id(mrb, MRB_SYM(Process)); |
| 111 |
if (mrb_const_defined(mrb, mrb_obj_value(c_process), MRB_SYM(Status))) { |
| 112 |
c_status = mrb_class_get_under_id(mrb, c_process, MRB_SYM(Status)); |
| 113 |
} |
| 114 |
} |
| 115 |
if (c_status != NULL) { |
| 116 |
v = mrb_funcall_id(mrb, mrb_obj_value(c_status), MRB_SYM(new), 2, mrb_fixnum_value(pid), mrb_fixnum_value(status)); |
| 117 |
} |
| 118 |
else { |
| 119 |
v = mrb_fixnum_value(WEXITSTATUS(status)); |
| 120 |
} |
| 121 |
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$?"), v); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
static int |
| 126 |
io_modestr_to_flags(mrb_state *mrb, const char *mode) |
| 127 |
{ |
| 128 |
int flags; |
| 129 |
const char *m = mode; |
| 130 |
|
| 131 |
switch (*m++) { |
| 132 |
case 'r': |
| 133 |
flags = O_RDONLY; |
| 134 |
break; |
| 135 |
case 'w': |
| 136 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 137 |
break; |
| 138 |
case 'a': |
| 139 |
flags = O_WRONLY | O_CREAT | O_APPEND; |
| 140 |
break; |
| 141 |
default: |
| 142 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %s", mode); |
| 143 |
flags = 0; /* not reached */ |
| 144 |
} |
| 145 |
|
| 146 |
while (*m) { |
| 147 |
switch (*m++) { |
| 148 |
case 'b': |
| 149 |
#ifdef O_BINARY |
| 150 |
flags |= O_BINARY; |
| 151 |
#endif |
| 152 |
break; |
| 153 |
case '+': |
| 154 |
flags = (flags & ~OPEN_ACCESS_MODE_FLAGS) | O_RDWR; |
| 155 |
break; |
| 156 |
case ':': |
| 157 |
/* XXX: PASSTHROUGH*/ |
| 158 |
default: |
| 159 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %s", mode); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return flags; |
| 164 |
} |
| 165 |
|
| 166 |
static int |
| 167 |
io_mode_to_flags(mrb_state *mrb, mrb_value mode) |
| 168 |
{ |
| 169 |
if (mrb_nil_p(mode)) { |
| 170 |
return O_RDONLY; |
| 171 |
} |
| 172 |
else if (mrb_string_p(mode)) { |
| 173 |
return io_modestr_to_flags(mrb, RSTRING_CSTR(mrb, mode)); |
| 174 |
} |
| 175 |
else { |
| 176 |
int flags = 0; |
| 177 |
mrb_int flags0 = mrb_as_int(mrb, mode); |
| 178 |
|
| 179 |
switch (flags0 & MRB_O_ACCMODE) { |
| 180 |
case MRB_O_RDONLY: |
| 181 |
flags |= O_RDONLY; |
| 182 |
break; |
| 183 |
case MRB_O_WRONLY: |
| 184 |
flags |= O_WRONLY; |
| 185 |
break; |
| 186 |
case MRB_O_RDWR: |
| 187 |
flags |= O_RDWR; |
| 188 |
break; |
| 189 |
default: |
| 190 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %v", mode); |
| 191 |
} |
| 192 |
|
| 193 |
if (flags0 & MRB_O_APPEND) flags |= O_APPEND; |
| 194 |
if (flags0 & MRB_O_CREAT) flags |= O_CREAT; |
| 195 |
if (flags0 & MRB_O_EXCL) flags |= O_EXCL; |
| 196 |
if (flags0 & MRB_O_TRUNC) flags |= O_TRUNC; |
| 197 |
#ifdef O_NONBLOCK |
| 198 |
if (flags0 & MRB_O_NONBLOCK) flags |= O_NONBLOCK; |
| 199 |
#endif |
| 200 |
#ifdef O_NOCTTY |
| 201 |
if (flags0 & MRB_O_NOCTTY) flags |= O_NOCTTY; |
| 202 |
#endif |
| 203 |
#ifdef O_BINARY |
| 204 |
if (flags0 & MRB_O_BINARY) flags |= O_BINARY; |
| 205 |
#endif |
| 206 |
#ifdef O_SHARE_DELETE |
| 207 |
if (flags0 & MRB_O_SHARE_DELETE) flags |= O_SHARE_DELETE; |
| 208 |
#endif |
| 209 |
#ifdef O_SYNC |
| 210 |
if (flags0 & MRB_O_SYNC) flags |= O_SYNC; |
| 211 |
#endif |
| 212 |
#ifdef O_DSYNC |
| 213 |
if (flags0 & MRB_O_DSYNC) flags |= O_DSYNC; |
| 214 |
#endif |
| 215 |
#ifdef O_RSYNC |
| 216 |
if (flags0 & MRB_O_RSYNC) flags |= O_RSYNC; |
| 217 |
#endif |
| 218 |
#ifdef O_NOFOLLOW |
| 219 |
if (flags0 & MRB_O_NOFOLLOW) flags |= O_NOFOLLOW; |
| 220 |
#endif |
| 221 |
#ifdef O_NOATIME |
| 222 |
if (flags0 & MRB_O_NOATIME) flags |= O_NOATIME; |
| 223 |
#endif |
| 224 |
#ifdef O_DIRECT |
| 225 |
if (flags0 & MRB_O_DIRECT) flags |= O_DIRECT; |
| 226 |
#endif |
| 227 |
#ifdef O_TMPFILE |
| 228 |
if (flags0 & MRB_O_TMPFILE) flags |= O_TMPFILE; |
| 229 |
#endif |
| 230 |
|
| 231 |
return flags; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
static void |
| 236 |
io_fd_cloexec(mrb_state *mrb, int fd) |
| 237 |
{ |
| 238 |
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
| 239 |
int flags, flags2; |
| 240 |
|
| 241 |
flags = fcntl(fd, F_GETFD); |
| 242 |
if (flags < 0) { |
| 243 |
mrb_sys_fail(mrb, "cloexec GETFD"); |
| 244 |
} |
| 245 |
if (fd <= 2) { |
| 246 |
flags2 = flags & ~FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */ |
| 247 |
} |
| 248 |
else { |
| 249 |
flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */ |
| 250 |
} |
| 251 |
if (flags != flags2) { |
| 252 |
if (fcntl(fd, F_SETFD, flags2) < 0) { |
| 253 |
mrb_sys_fail(mrb, "cloexec SETFD"); |
| 254 |
} |
| 255 |
} |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
#if !defined(_WIN32) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) |
| 260 |
static int |
| 261 |
io_cloexec_pipe(mrb_state *mrb, int fildes[2]) |
| 262 |
{ |
| 263 |
int ret; |
| 264 |
ret = pipe(fildes); |
| 265 |
if (ret == -1) |
| 266 |
return -1; |
| 267 |
io_fd_cloexec(mrb, fildes[0]); |
| 268 |
io_fd_cloexec(mrb, fildes[1]); |
| 269 |
return ret; |
| 270 |
} |
| 271 |
|
| 272 |
static int |
| 273 |
io_pipe(mrb_state *mrb, int pipes[2]) |
| 274 |
{ |
| 275 |
int ret; |
| 276 |
ret = io_cloexec_pipe(mrb, pipes); |
| 277 |
if (ret == -1) { |
| 278 |
if (errno == EMFILE || errno == ENFILE) { |
| 279 |
mrb_garbage_collect(mrb); |
| 280 |
ret = io_cloexec_pipe(mrb, pipes); |
| 281 |
} |
| 282 |
} |
| 283 |
return ret; |
| 284 |
} |
| 285 |
|
| 286 |
static int |
| 287 |
io_process_exec(const char *pname) |
| 288 |
{ |
| 289 |
const char *s; |
| 290 |
s = pname; |
| 291 |
|
| 292 |
while (*s == ' ' || *s == '\t' || *s == '\n') |
| 293 |
s++; |
| 294 |
|
| 295 |
if (!*s) { |
| 296 |
errno = ENOENT; |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
execl("/bin/sh", "sh", "-c", pname, (char*)NULL); |
| 301 |
return -1; |
| 302 |
} |
| 303 |
#endif |
| 304 |
|
| 305 |
static void |
| 306 |
io_free(mrb_state *mrb, void *ptr) |
| 307 |
{ |
| 308 |
struct mrb_io *io = (struct mrb_io*)ptr; |
| 309 |
if (io != NULL) { |
| 310 |
fptr_finalize(mrb, io, TRUE); |
| 311 |
mrb_free(mrb, io); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static void |
| 316 |
io_buf_init(mrb_state *mrb, struct mrb_io *fptr) |
| 317 |
{ |
| 318 |
if (fptr->readable) { |
| 319 |
fptr->buf = (struct mrb_io_buf*)mrb_malloc(mrb, sizeof(struct mrb_io_buf)); |
| 320 |
fptr->buf->start = 0; |
| 321 |
fptr->buf->len = 0; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static struct mrb_io * |
| 326 |
io_alloc(mrb_state *mrb) |
| 327 |
{ |
| 328 |
struct mrb_io *fptr; |
| 329 |
|
| 330 |
fptr = (struct mrb_io*)mrb_malloc(mrb, sizeof(struct mrb_io)); |
| 331 |
fptr->fd = -1; |
| 332 |
fptr->fd2 = -1; |
| 333 |
fptr->pid = 0; |
| 334 |
fptr->buf = 0; |
| 335 |
fptr->readable = 0; |
| 336 |
fptr->writable = 0; |
| 337 |
fptr->sync = 0; |
| 338 |
fptr->eof = 0; |
| 339 |
fptr->is_socket = 0; |
| 340 |
return fptr; |
| 341 |
} |
| 342 |
|
| 343 |
#ifndef NOFILE |
| 344 |
#define NOFILE 64 |
| 345 |
#endif |
| 346 |
|
| 347 |
#ifdef MRB_NO_IO_POPEN |
| 348 |
# define io_s_popen mrb_notimplement_m |
| 349 |
#else |
| 350 |
static int |
| 351 |
option_to_fd(mrb_state *mrb, mrb_value v) |
| 352 |
{ |
| 353 |
if (mrb_undef_p(v)) return -1; |
| 354 |
if (mrb_nil_p(v)) return -1; |
| 355 |
|
| 356 |
switch (mrb_type(v)) { |
| 357 |
case MRB_TT_CDATA: /* IO */ |
| 358 |
return mrb_io_fileno(mrb, v); |
| 359 |
case MRB_TT_INTEGER: |
| 360 |
return (int)mrb_integer(v); |
| 361 |
default: |
| 362 |
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong exec redirect action"); |
| 363 |
break; |
| 364 |
} |
| 365 |
return -1; /* never reached */ |
| 366 |
} |
| 367 |
|
| 368 |
static mrb_value |
| 369 |
io_s_popen_args(mrb_state *mrb, mrb_value klass, |
| 370 |
const char **cmd, int *flags, int *doexec, |
| 371 |
int *opt_in, int *opt_out, int *opt_err) |
| 372 |
{ |
| 373 |
mrb_value mode = mrb_nil_value(); |
| 374 |
struct { mrb_value opt_in, opt_out, opt_err; } kv; |
| 375 |
mrb_sym knames[3] = {MRB_SYM(in), MRB_SYM(out), MRB_SYM(err)}; |
| 376 |
const mrb_kwargs kw = { |
| 377 |
3, 0, |
| 378 |
knames, |
| 379 |
&kv.opt_in, |
| 380 |
NULL, |
| 381 |
}; |
| 382 |
|
| 383 |
mrb_get_args(mrb, "zo:", cmd, &mode, &kw); |
| 384 |
|
| 385 |
*flags = io_mode_to_flags(mrb, mode); |
| 386 |
*doexec = (strcmp("-", *cmd) != 0); |
| 387 |
*opt_in = option_to_fd(mrb, kv.opt_in); |
| 388 |
*opt_out = option_to_fd(mrb, kv.opt_out); |
| 389 |
*opt_err = option_to_fd(mrb, kv.opt_err); |
| 390 |
|
| 391 |
return mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); |
| 392 |
} |
| 393 |
|
| 394 |
#ifdef _WIN32 |
| 395 |
static mrb_value |
| 396 |
io_s_popen(mrb_state *mrb, mrb_value klass) |
| 397 |
{ |
| 398 |
mrb_value io; |
| 399 |
int doexec; |
| 400 |
int opt_in, opt_out, opt_err; |
| 401 |
const char *cmd; |
| 402 |
|
| 403 |
struct mrb_io *fptr; |
| 404 |
int pid = 0, flags; |
| 405 |
STARTUPINFO si; |
| 406 |
PROCESS_INFORMATION pi; |
| 407 |
SECURITY_ATTRIBUTES saAttr; |
| 408 |
|
| 409 |
HANDLE ifd[2]; |
| 410 |
HANDLE ofd[2]; |
| 411 |
|
| 412 |
ifd[0] = INVALID_HANDLE_VALUE; |
| 413 |
ifd[1] = INVALID_HANDLE_VALUE; |
| 414 |
ofd[0] = INVALID_HANDLE_VALUE; |
| 415 |
ofd[1] = INVALID_HANDLE_VALUE; |
| 416 |
|
| 417 |
mrb->c->ci->mid = 0; |
| 418 |
io = io_s_popen_args(mrb, klass, &cmd, &flags, &doexec, |
| 419 |
&opt_in, &opt_out, &opt_err); |
| 420 |
|
| 421 |
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 422 |
saAttr.bInheritHandle = TRUE; |
| 423 |
saAttr.lpSecurityDescriptor = NULL; |
| 424 |
|
| 425 |
if (OPEN_READABLE_P(flags)) { |
| 426 |
if (!CreatePipe(&ofd[0], &ofd[1], &saAttr, 0) |
| 427 |
|| !SetHandleInformation(ofd[0], HANDLE_FLAG_INHERIT, 0)) { |
| 428 |
mrb_sys_fail(mrb, "pipe"); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
if (OPEN_WRITABLE_P(flags)) { |
| 433 |
if (!CreatePipe(&ifd[0], &ifd[1], &saAttr, 0) |
| 434 |
|| !SetHandleInformation(ifd[1], HANDLE_FLAG_INHERIT, 0)) { |
| 435 |
mrb_sys_fail(mrb, "pipe"); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
if (doexec) { |
| 440 |
ZeroMemory(&pi, sizeof(pi)); |
| 441 |
ZeroMemory(&si, sizeof(si)); |
| 442 |
si.cb = sizeof(si); |
| 443 |
si.dwFlags |= STARTF_USESHOWWINDOW; |
| 444 |
si.wShowWindow = SW_HIDE; |
| 445 |
si.dwFlags |= STARTF_USESTDHANDLES; |
| 446 |
if (OPEN_READABLE_P(flags)) { |
| 447 |
si.hStdOutput = ofd[1]; |
| 448 |
si.hStdError = ofd[1]; |
| 449 |
} |
| 450 |
if (OPEN_WRITABLE_P(flags)) { |
| 451 |
si.hStdInput = ifd[0]; |
| 452 |
} |
| 453 |
if (!CreateProcess( |
| 454 |
NULL, (char*)cmd, NULL, NULL, |
| 455 |
TRUE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) { |
| 456 |
CloseHandle(ifd[0]); |
| 457 |
CloseHandle(ifd[1]); |
| 458 |
CloseHandle(ofd[0]); |
| 459 |
CloseHandle(ofd[1]); |
| 460 |
mrb_raisef(mrb, E_IO_ERROR, "command not found: %s", cmd); |
| 461 |
} |
| 462 |
CloseHandle(pi.hThread); |
| 463 |
CloseHandle(ifd[0]); |
| 464 |
CloseHandle(ofd[1]); |
| 465 |
pid = pi.dwProcessId; |
| 466 |
} |
| 467 |
|
| 468 |
fptr = io_alloc(mrb); |
| 469 |
fptr->fd = _open_osfhandle((intptr_t)ofd[0], 0); |
| 470 |
fptr->fd2 = _open_osfhandle((intptr_t)ifd[1], 0); |
| 471 |
fptr->pid = pid; |
| 472 |
fptr->readable = OPEN_READABLE_P(flags); |
| 473 |
fptr->writable = OPEN_WRITABLE_P(flags); |
| 474 |
io_buf_init(mrb, fptr); |
| 475 |
|
| 476 |
DATA_TYPE(io) = &mrb_io_type; |
| 477 |
DATA_PTR(io) = fptr; |
| 478 |
return io; |
| 479 |
} |
| 480 |
#else |
| 481 |
static mrb_value |
| 482 |
io_s_popen(mrb_state *mrb, mrb_value klass) |
| 483 |
{ |
| 484 |
mrb_value io, result; |
| 485 |
int doexec; |
| 486 |
int opt_in, opt_out, opt_err; |
| 487 |
const char *cmd; |
| 488 |
|
| 489 |
struct mrb_io *fptr; |
| 490 |
int pid, flags, fd, write_fd = -1; |
| 491 |
int pr[2] = { -1, -1 }; |
| 492 |
int pw[2] = { -1, -1 }; |
| 493 |
int saved_errno; |
| 494 |
|
| 495 |
mrb->c->ci->mid = 0; |
| 496 |
io = io_s_popen_args(mrb, klass, &cmd, &flags, &doexec, |
| 497 |
&opt_in, &opt_out, &opt_err); |
| 498 |
|
| 499 |
if (OPEN_READABLE_P(flags)) { |
| 500 |
if (pipe(pr) == -1) { |
| 501 |
mrb_sys_fail(mrb, "pipe"); |
| 502 |
} |
| 503 |
io_fd_cloexec(mrb, pr[0]); |
| 504 |
io_fd_cloexec(mrb, pr[1]); |
| 505 |
} |
| 506 |
|
| 507 |
if (OPEN_WRITABLE_P(flags)) { |
| 508 |
if (pipe(pw) == -1) { |
| 509 |
if (pr[0] != -1) close(pr[0]); |
| 510 |
if (pr[1] != -1) close(pr[1]); |
| 511 |
mrb_sys_fail(mrb, "pipe"); |
| 512 |
} |
| 513 |
io_fd_cloexec(mrb, pw[0]); |
| 514 |
io_fd_cloexec(mrb, pw[1]); |
| 515 |
} |
| 516 |
|
| 517 |
if (!doexec) { |
| 518 |
// XXX |
| 519 |
fflush(stdin); |
| 520 |
fflush(stdout); |
| 521 |
fflush(stderr); |
| 522 |
} |
| 523 |
|
| 524 |
result = mrb_nil_value(); |
| 525 |
switch (pid = fork()) { |
| 526 |
case 0: /* child */ |
| 527 |
if (opt_in != -1) { |
| 528 |
dup2(opt_in, 0); |
| 529 |
} |
| 530 |
if (opt_out != -1) { |
| 531 |
dup2(opt_out, 1); |
| 532 |
} |
| 533 |
if (opt_err != -1) { |
| 534 |
dup2(opt_err, 2); |
| 535 |
} |
| 536 |
if (OPEN_READABLE_P(flags)) { |
| 537 |
close(pr[0]); |
| 538 |
if (pr[1] != 1) { |
| 539 |
dup2(pr[1], 1); |
| 540 |
close(pr[1]); |
| 541 |
} |
| 542 |
} |
| 543 |
if (OPEN_WRITABLE_P(flags)) { |
| 544 |
close(pw[1]); |
| 545 |
if (pw[0] != 0) { |
| 546 |
dup2(pw[0], 0); |
| 547 |
close(pw[0]); |
| 548 |
} |
| 549 |
} |
| 550 |
if (doexec) { |
| 551 |
for (fd = 3; fd < NOFILE; fd++) { |
| 552 |
close(fd); |
| 553 |
} |
| 554 |
io_process_exec(cmd); |
| 555 |
mrb_raisef(mrb, E_IO_ERROR, "command not found: %s", cmd); |
| 556 |
_exit(127); |
| 557 |
} |
| 558 |
result = mrb_nil_value(); |
| 559 |
break; |
| 560 |
|
| 561 |
default: /* parent */ |
| 562 |
if (OPEN_RDWR_P(flags)) { |
| 563 |
close(pr[1]); |
| 564 |
fd = pr[0]; |
| 565 |
close(pw[0]); |
| 566 |
write_fd = pw[1]; |
| 567 |
} |
| 568 |
else if (OPEN_RDONLY_P(flags)) { |
| 569 |
close(pr[1]); |
| 570 |
fd = pr[0]; |
| 571 |
} |
| 572 |
else { |
| 573 |
close(pw[0]); |
| 574 |
fd = pw[1]; |
| 575 |
} |
| 576 |
|
| 577 |
fptr = io_alloc(mrb); |
| 578 |
fptr->fd = fd; |
| 579 |
fptr->fd2 = write_fd; |
| 580 |
fptr->pid = pid; |
| 581 |
fptr->readable = OPEN_READABLE_P(flags); |
| 582 |
fptr->writable = OPEN_WRITABLE_P(flags); |
| 583 |
io_buf_init(mrb, fptr); |
| 584 |
|
| 585 |
DATA_TYPE(io) = &mrb_io_type; |
| 586 |
DATA_PTR(io) = fptr; |
| 587 |
result = io; |
| 588 |
break; |
| 589 |
|
| 590 |
case -1: /* error */ |
| 591 |
saved_errno = errno; |
| 592 |
if (OPEN_READABLE_P(flags)) { |
| 593 |
close(pr[0]); |
| 594 |
close(pr[1]); |
| 595 |
} |
| 596 |
if (OPEN_WRITABLE_P(flags)) { |
| 597 |
close(pw[0]); |
| 598 |
close(pw[1]); |
| 599 |
} |
| 600 |
errno = saved_errno; |
| 601 |
mrb_sys_fail(mrb, "pipe_open failed"); |
| 602 |
break; |
| 603 |
} |
| 604 |
return result; |
| 605 |
} |
| 606 |
#endif /* _WIN32 */ |
| 607 |
#endif /* TARGET_OS_IPHONE */ |
| 608 |
|
| 609 |
static int |
| 610 |
symdup(mrb_state *mrb, int fd, mrb_bool *failed) |
| 611 |
{ |
| 612 |
int new_fd; |
| 613 |
|
| 614 |
*failed = TRUE; |
| 615 |
if (fd < 0) |
| 616 |
return fd; |
| 617 |
|
| 618 |
new_fd = dup(fd); |
| 619 |
if (new_fd > 0) *failed = FALSE; |
| 620 |
return new_fd; |
| 621 |
} |
| 622 |
|
| 623 |
static mrb_value |
| 624 |
io_init_copy(mrb_state *mrb, mrb_value copy) |
| 625 |
{ |
| 626 |
mrb_value orig = mrb_get_arg1(mrb); |
| 627 |
struct mrb_io *fptr_copy; |
| 628 |
struct mrb_io *fptr_orig; |
| 629 |
mrb_bool failed = TRUE; |
| 630 |
|
| 631 |
fptr_orig = io_get_open_fptr(mrb, orig); |
| 632 |
fptr_copy = (struct mrb_io*)DATA_PTR(copy); |
| 633 |
if (fptr_orig == fptr_copy) return copy; |
| 634 |
if (fptr_copy != NULL) { |
| 635 |
fptr_finalize(mrb, fptr_copy, FALSE); |
| 636 |
mrb_free(mrb, fptr_copy); |
| 637 |
} |
| 638 |
fptr_copy = (struct mrb_io*)io_alloc(mrb); |
| 639 |
fptr_copy->pid = fptr_orig->pid; |
| 640 |
fptr_copy->readable = fptr_orig->readable; |
| 641 |
fptr_copy->writable = fptr_orig->writable; |
| 642 |
fptr_copy->sync = fptr_orig->sync; |
| 643 |
fptr_copy->is_socket = fptr_orig->is_socket; |
| 644 |
|
| 645 |
io_buf_init(mrb, fptr_copy); |
| 646 |
|
| 647 |
DATA_TYPE(copy) = &mrb_io_type; |
| 648 |
DATA_PTR(copy) = fptr_copy; |
| 649 |
|
| 650 |
fptr_copy->fd = symdup(mrb, fptr_orig->fd, &failed); |
| 651 |
if (failed) { |
| 652 |
mrb_sys_fail(mrb, 0); |
| 653 |
} |
| 654 |
io_fd_cloexec(mrb, fptr_copy->fd); |
| 655 |
|
| 656 |
if (fptr_orig->fd2 != -1) { |
| 657 |
fptr_copy->fd2 = symdup(mrb, fptr_orig->fd2, &failed); |
| 658 |
if (failed) { |
| 659 |
close(fptr_copy->fd); |
| 660 |
mrb_sys_fail(mrb, 0); |
| 661 |
} |
| 662 |
io_fd_cloexec(mrb, fptr_copy->fd2); |
| 663 |
} |
| 664 |
|
| 665 |
return copy; |
| 666 |
} |
| 667 |
|
| 668 |
static void |
| 669 |
check_file_descriptor(mrb_state *mrb, mrb_int fd) |
| 670 |
{ |
| 671 |
struct stat sb; |
| 672 |
int fdi = (int)fd; |
| 673 |
|
| 674 |
#if MRB_INT_MIN < INT_MIN || MRB_INT_MAX > INT_MAX |
| 675 |
if (fdi != fd) { |
| 676 |
goto badfd; |
| 677 |
} |
| 678 |
#endif |
| 679 |
|
| 680 |
#ifdef _WIN32 |
| 681 |
{ |
| 682 |
DWORD err; |
| 683 |
int len = sizeof(err); |
| 684 |
|
| 685 |
if (getsockopt(fdi, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) { |
| 686 |
return; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
if (fdi < 0 || fdi > _getmaxstdio()) { |
| 691 |
goto badfd; |
| 692 |
} |
| 693 |
#endif /* _WIN32 */ |
| 694 |
|
| 695 |
if (fstat(fdi, &sb) == 0) return; |
| 696 |
if (errno == EBADF) goto badfd; |
| 697 |
return; |
| 698 |
|
| 699 |
badfd: |
| 700 |
mrb_sys_fail(mrb, "bad file descriptor"); |
| 701 |
} |
| 702 |
|
| 703 |
static mrb_value |
| 704 |
io_init(mrb_state *mrb, mrb_value io) |
| 705 |
{ |
| 706 |
struct mrb_io *fptr; |
| 707 |
mrb_int fd; |
| 708 |
mrb_value mode, opt; /* opt (Hash) will be ignored */ |
| 709 |
int flags; |
| 710 |
|
| 711 |
mode = opt = mrb_nil_value(); |
| 712 |
|
| 713 |
if (mrb_block_given_p(mrb)) { |
| 714 |
mrb_warn(mrb, "File.new() does not take block; use File.open() instead"); |
| 715 |
} |
| 716 |
mrb_get_args(mrb, "i|oH", &fd, &mode, &opt); |
| 717 |
switch (fd) { |
| 718 |
case 0: /* STDIN_FILENO */ |
| 719 |
case 1: /* STDOUT_FILENO */ |
| 720 |
case 2: /* STDERR_FILENO */ |
| 721 |
break; |
| 722 |
default: |
| 723 |
check_file_descriptor(mrb, fd); |
| 724 |
break; |
| 725 |
} |
| 726 |
flags = io_mode_to_flags(mrb, mode); |
| 727 |
|
| 728 |
fptr = (struct mrb_io*)DATA_PTR(io); |
| 729 |
if (fptr != NULL) { |
| 730 |
fptr_finalize(mrb, fptr, TRUE); |
| 731 |
mrb_free(mrb, fptr); |
| 732 |
} |
| 733 |
fptr = io_alloc(mrb); |
| 734 |
|
| 735 |
DATA_TYPE(io) = &mrb_io_type; |
| 736 |
DATA_PTR(io) = fptr; |
| 737 |
|
| 738 |
fptr->fd = (int)fd; |
| 739 |
fptr->readable = OPEN_READABLE_P(flags); |
| 740 |
fptr->writable = OPEN_WRITABLE_P(flags); |
| 741 |
io_buf_init(mrb, fptr); |
| 742 |
return io; |
| 743 |
} |
| 744 |
|
| 745 |
static void |
| 746 |
fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet) |
| 747 |
{ |
| 748 |
int saved_errno = 0; |
| 749 |
int limit = quiet ? 3 : 0; |
| 750 |
|
| 751 |
if (fptr == NULL) { |
| 752 |
return; |
| 753 |
} |
| 754 |
|
| 755 |
if (fptr->fd >= limit) { |
| 756 |
#ifdef _WIN32 |
| 757 |
if (fptr->is_socket) { |
| 758 |
if (closesocket(fptr->fd) != 0) { |
| 759 |
saved_errno = WSAGetLastError(); |
| 760 |
} |
| 761 |
fptr->fd = -1; |
| 762 |
} |
| 763 |
#endif |
| 764 |
if (fptr->fd != -1) { |
| 765 |
if (close(fptr->fd) == -1) { |
| 766 |
saved_errno = errno; |
| 767 |
} |
| 768 |
} |
| 769 |
fptr->fd = -1; |
| 770 |
} |
| 771 |
|
| 772 |
if (fptr->fd2 >= limit) { |
| 773 |
if (close(fptr->fd2) == -1) { |
| 774 |
if (saved_errno == 0) { |
| 775 |
saved_errno = errno; |
| 776 |
} |
| 777 |
} |
| 778 |
fptr->fd2 = -1; |
| 779 |
} |
| 780 |
|
| 781 |
#ifndef MRB_NO_IO_POPEN |
| 782 |
if (fptr->pid != 0) { |
| 783 |
#if !defined(_WIN32) && !defined(_WIN64) |
| 784 |
pid_t pid; |
| 785 |
int status; |
| 786 |
do { |
| 787 |
pid = waitpid(fptr->pid, &status, 0); |
| 788 |
} while (pid == -1 && errno == EINTR); |
| 789 |
if (!quiet && pid == fptr->pid) { |
| 790 |
io_set_process_status(mrb, pid, status); |
| 791 |
} |
| 792 |
#else |
| 793 |
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, fptr->pid); |
| 794 |
DWORD status; |
| 795 |
if (WaitForSingleObject(h, INFINITE) && GetExitCodeProcess(h, &status)) |
| 796 |
if (!quiet) |
| 797 |
io_set_process_status(mrb, fptr->pid, (int)status); |
| 798 |
CloseHandle(h); |
| 799 |
#endif |
| 800 |
fptr->pid = 0; |
| 801 |
/* Note: we don't raise an exception when waitpid(3) fails */ |
| 802 |
} |
| 803 |
#endif |
| 804 |
|
| 805 |
if (fptr->buf) { |
| 806 |
mrb_free(mrb, fptr->buf); |
| 807 |
fptr->buf = NULL; |
| 808 |
} |
| 809 |
|
| 810 |
if (!quiet && saved_errno != 0) { |
| 811 |
errno = saved_errno; |
| 812 |
mrb_sys_fail(mrb, "fptr_finalize failed"); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
static struct mrb_io* |
| 817 |
io_get_read_fptr(mrb_state *mrb, mrb_value io) |
| 818 |
{ |
| 819 |
struct mrb_io *fptr = io_get_open_fptr(mrb, io); |
| 820 |
if (!fptr->readable) { |
| 821 |
mrb_raise(mrb, E_IO_ERROR, "not opened for reading"); |
| 822 |
} |
| 823 |
return fptr; |
| 824 |
} |
| 825 |
|
| 826 |
static struct mrb_io* |
| 827 |
io_get_write_fptr(mrb_state *mrb, mrb_value io) |
| 828 |
{ |
| 829 |
struct mrb_io *fptr = io_get_open_fptr(mrb, io); |
| 830 |
if (!fptr->writable) { |
| 831 |
mrb_raise(mrb, E_IO_ERROR, "not opened for writing"); |
| 832 |
} |
| 833 |
return fptr; |
| 834 |
} |
| 835 |
|
| 836 |
static int |
| 837 |
io_get_write_fd(struct mrb_io *fptr) |
| 838 |
{ |
| 839 |
if (fptr->fd2 == -1) { |
| 840 |
return fptr->fd; |
| 841 |
} |
| 842 |
else { |
| 843 |
return fptr->fd2; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
static mrb_value |
| 848 |
io_isatty(mrb_state *mrb, mrb_value io) |
| 849 |
{ |
| 850 |
struct mrb_io *fptr; |
| 851 |
|
| 852 |
fptr = io_get_open_fptr(mrb, io); |
| 853 |
if (isatty(fptr->fd) == 0) |
| 854 |
return mrb_false_value(); |
| 855 |
return mrb_true_value(); |
| 856 |
} |
| 857 |
|
| 858 |
static mrb_value |
| 859 |
io_s_for_fd(mrb_state *mrb, mrb_value klass) |
| 860 |
{ |
| 861 |
struct RClass *c = mrb_class_ptr(klass); |
| 862 |
enum mrb_vtype ttype = MRB_INSTANCE_TT(c); |
| 863 |
mrb_value obj; |
| 864 |
|
| 865 |
/* copied from mrb_instance_alloc() */ |
| 866 |
if (ttype == 0) ttype = MRB_TT_OBJECT; |
| 867 |
obj = mrb_obj_value((struct RObject*)mrb_obj_alloc(mrb, ttype, c)); |
| 868 |
return io_init(mrb, obj); |
| 869 |
} |
| 870 |
|
| 871 |
static mrb_value |
| 872 |
io_s_sysclose(mrb_state *mrb, mrb_value klass) |
| 873 |
{ |
| 874 |
mrb_int fd; |
| 875 |
mrb->c->ci->mid = 0; |
| 876 |
mrb_get_args(mrb, "i", &fd); |
| 877 |
if (close((int)fd) == -1) { |
| 878 |
mrb_sys_fail(mrb, "close"); |
| 879 |
} |
| 880 |
return mrb_fixnum_value(0); |
| 881 |
} |
| 882 |
|
| 883 |
static int |
| 884 |
io_cloexec_open(mrb_state *mrb, const char *pathname, int flags, fmode_t mode) |
| 885 |
{ |
| 886 |
int fd, retry = FALSE; |
| 887 |
char* fname = mrb_locale_from_utf8(pathname, -1); |
| 888 |
|
| 889 |
#ifdef O_CLOEXEC |
| 890 |
/* O_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */ |
| 891 |
flags |= O_CLOEXEC; |
| 892 |
#elif defined O_NOINHERIT |
| 893 |
flags |= O_NOINHERIT; |
| 894 |
#endif |
| 895 |
reopen: |
| 896 |
fd = open(fname, flags, mode); |
| 897 |
if (fd == -1) { |
| 898 |
if (!retry) { |
| 899 |
switch (errno) { |
| 900 |
case ENFILE: |
| 901 |
case EMFILE: |
| 902 |
mrb_garbage_collect(mrb); |
| 903 |
retry = TRUE; |
| 904 |
goto reopen; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
mrb_sys_fail(mrb, RSTRING_CSTR(mrb, mrb_format(mrb, "open %s", pathname))); |
| 909 |
} |
| 910 |
mrb_locale_free(fname); |
| 911 |
|
| 912 |
if (fd <= 2) { |
| 913 |
io_fd_cloexec(mrb, fd); |
| 914 |
} |
| 915 |
return fd; |
| 916 |
} |
| 917 |
|
| 918 |
static mrb_value |
| 919 |
io_s_sysopen(mrb_state *mrb, mrb_value klass) |
| 920 |
{ |
| 921 |
mrb_value path = mrb_nil_value(); |
| 922 |
mrb_value mode = mrb_nil_value(); |
| 923 |
mrb_int fd, perm = -1; |
| 924 |
const char *pat; |
| 925 |
int flags; |
| 926 |
|
| 927 |
mrb_get_args(mrb, "S|oi", &path, &mode, &perm); |
| 928 |
if (perm < 0) { |
| 929 |
perm = 0666; |
| 930 |
} |
| 931 |
|
| 932 |
pat = RSTRING_CSTR(mrb, path); |
| 933 |
flags = io_mode_to_flags(mrb, mode); |
| 934 |
fd = io_cloexec_open(mrb, pat, flags, (fmode_t)perm); |
| 935 |
return mrb_fixnum_value(fd); |
| 936 |
} |
| 937 |
|
| 938 |
static void |
| 939 |
eof_error(mrb_state *mrb) |
| 940 |
{ |
| 941 |
mrb_raise(mrb, E_EOF_ERROR, "end of file reached"); |
| 942 |
} |
| 943 |
|
| 944 |
static mrb_value |
| 945 |
io_read_common(mrb_state *mrb, |
| 946 |
fssize_t (*readfunc)(int, void*, fsize_t, off_t), |
| 947 |
mrb_value io, mrb_value buf, mrb_int maxlen, off_t offset) |
| 948 |
{ |
| 949 |
int ret; |
| 950 |
|
| 951 |
if (maxlen < 0) { |
| 952 |
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative expanding string size"); |
| 953 |
} |
| 954 |
else if (maxlen == 0) { |
| 955 |
return mrb_str_new(mrb, NULL, maxlen); |
| 956 |
} |
| 957 |
|
| 958 |
if (mrb_nil_p(buf)) { |
| 959 |
buf = mrb_str_new(mrb, NULL, maxlen); |
| 960 |
} |
| 961 |
|
| 962 |
if (RSTRING_LEN(buf) != maxlen) { |
| 963 |
buf = mrb_str_resize(mrb, buf, maxlen); |
| 964 |
} |
| 965 |
else { |
| 966 |
mrb_str_modify(mrb, RSTRING(buf)); |
| 967 |
} |
| 968 |
|
| 969 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 970 |
ret = readfunc(fptr->fd, RSTRING_PTR(buf), (fsize_t)maxlen, offset); |
| 971 |
if (ret < 0) { |
| 972 |
mrb_sys_fail(mrb, "sysread failed"); |
| 973 |
} |
| 974 |
if (RSTRING_LEN(buf) != ret) { |
| 975 |
buf = mrb_str_resize(mrb, buf, ret); |
| 976 |
} |
| 977 |
if (ret == 0 && maxlen > 0) { |
| 978 |
fptr->eof = 1; |
| 979 |
eof_error(mrb); |
| 980 |
} |
| 981 |
return buf; |
| 982 |
} |
| 983 |
|
| 984 |
static fssize_t |
| 985 |
sysread(int fd, void *buf, fsize_t nbytes, off_t offset) |
| 986 |
{ |
| 987 |
return (fssize_t)read(fd, buf, nbytes); |
| 988 |
} |
| 989 |
|
| 990 |
static mrb_value |
| 991 |
io_sysread(mrb_state *mrb, mrb_value io) |
| 992 |
{ |
| 993 |
mrb_value buf = mrb_nil_value(); |
| 994 |
mrb_int maxlen; |
| 995 |
|
| 996 |
mrb_get_args(mrb, "i|S", &maxlen, &buf); |
| 997 |
|
| 998 |
return io_read_common(mrb, sysread, io, buf, maxlen, 0); |
| 999 |
} |
| 1000 |
|
| 1001 |
static mrb_value |
| 1002 |
io_sysseek(mrb_state *mrb, mrb_value io) |
| 1003 |
{ |
| 1004 |
struct mrb_io *fptr; |
| 1005 |
off_t pos; |
| 1006 |
mrb_int offset, whence = -1; |
| 1007 |
|
| 1008 |
mrb_get_args(mrb, "i|i", &offset, &whence); |
| 1009 |
if (whence < 0) { |
| 1010 |
whence = 0; |
| 1011 |
} |
| 1012 |
|
| 1013 |
fptr = io_get_open_fptr(mrb, io); |
| 1014 |
pos = lseek(fptr->fd, (off_t)offset, (int)whence); |
| 1015 |
if (pos == -1) { |
| 1016 |
mrb_sys_fail(mrb, "sysseek"); |
| 1017 |
} |
| 1018 |
fptr->eof = 0; |
| 1019 |
if (sizeof(off_t) > sizeof(mrb_int) && pos > (off_t)MRB_INT_MAX) { |
| 1020 |
mrb_raise(mrb, E_IO_ERROR, "sysseek reached too far for mrb_int"); |
| 1021 |
} |
| 1022 |
return mrb_int_value(mrb, (mrb_int)pos); |
| 1023 |
} |
| 1024 |
|
| 1025 |
static mrb_value |
| 1026 |
io_seek(mrb_state *mrb, mrb_value io) |
| 1027 |
{ |
| 1028 |
mrb_value pos = io_sysseek(mrb, io); |
| 1029 |
struct mrb_io *fptr = io_get_open_fptr(mrb, io); |
| 1030 |
if (fptr->buf) { |
| 1031 |
fptr->buf->start = 0; |
| 1032 |
fptr->buf->len = 0; |
| 1033 |
} |
| 1034 |
return pos; |
| 1035 |
} |
| 1036 |
|
| 1037 |
static mrb_value |
| 1038 |
io_write_common(mrb_state *mrb, |
| 1039 |
fssize_t (*writefunc)(int, const void*, fsize_t, off_t), |
| 1040 |
struct mrb_io *fptr, const void *buf, fsize_t blen, off_t offset) |
| 1041 |
{ |
| 1042 |
int fd; |
| 1043 |
fssize_t length; |
| 1044 |
|
| 1045 |
fd = io_get_write_fd(fptr); |
| 1046 |
length = writefunc(fd, buf, blen, offset); |
| 1047 |
if (length == -1) { |
| 1048 |
mrb_sys_fail(mrb, "syswrite"); |
| 1049 |
} |
| 1050 |
return mrb_int_value(mrb, (mrb_int)length); |
| 1051 |
} |
| 1052 |
|
| 1053 |
static fssize_t |
| 1054 |
syswrite(int fd, const void *buf, fsize_t nbytes, off_t offset) |
| 1055 |
{ |
| 1056 |
return (fssize_t)write(fd, buf, nbytes); |
| 1057 |
} |
| 1058 |
|
| 1059 |
static mrb_value |
| 1060 |
io_syswrite(mrb_state *mrb, mrb_value io) |
| 1061 |
{ |
| 1062 |
mrb_value buf; |
| 1063 |
|
| 1064 |
mrb_get_args(mrb, "S", &buf); |
| 1065 |
|
| 1066 |
return io_write_common(mrb, syswrite, io_get_write_fptr(mrb, io), RSTRING_PTR(buf), RSTRING_LEN(buf), 0); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* def write(string) */ |
| 1070 |
/* str = string.is_a?(String) ? string : string.to_s */ |
| 1071 |
/* return 0 if str.empty? */ |
| 1072 |
/* len = syswrite(str) */ |
| 1073 |
/* len */ |
| 1074 |
/* end */ |
| 1075 |
|
| 1076 |
static mrb_int |
| 1077 |
fd_write(mrb_state *mrb, int fd, mrb_value str) |
| 1078 |
{ |
| 1079 |
fssize_t len, sum, n; |
| 1080 |
|
| 1081 |
str = mrb_obj_as_string(mrb, str); |
| 1082 |
len = (fssize_t)RSTRING_LEN(str); |
| 1083 |
if (len == 0)return 0; |
| 1084 |
|
| 1085 |
for (sum=0; sum<len; sum+=n) { |
| 1086 |
n = write(fd, RSTRING_PTR(str), (fsize_t)len); |
| 1087 |
if (n == -1) { |
| 1088 |
mrb_sys_fail(mrb, "syswrite"); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
return len; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static mrb_value |
| 1095 |
io_write(mrb_state *mrb, mrb_value io) |
| 1096 |
{ |
| 1097 |
struct mrb_io *fptr = io_get_write_fptr(mrb, io); |
| 1098 |
int fd = io_get_write_fd(fptr); |
| 1099 |
mrb_int len = 0; |
| 1100 |
|
| 1101 |
if (fptr->buf && fptr->buf->len > 0) { |
| 1102 |
off_t n; |
| 1103 |
|
| 1104 |
/* get current position */ |
| 1105 |
n = lseek(fd, 0, SEEK_CUR); |
| 1106 |
if (n == -1) mrb_sys_fail(mrb, "lseek"); |
| 1107 |
/* move cursor */ |
| 1108 |
n = lseek(fd, n - fptr->buf->len, SEEK_SET); |
| 1109 |
if (n == -1) mrb_sys_fail(mrb, "lseek(2)"); |
| 1110 |
fptr->buf->start = fptr->buf->len = 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (mrb_get_argc(mrb) == 1) { |
| 1114 |
len = fd_write(mrb, fd, mrb_get_arg1(mrb)); |
| 1115 |
} |
| 1116 |
else { |
| 1117 |
mrb_value *argv; |
| 1118 |
mrb_int argc; |
| 1119 |
|
| 1120 |
mrb_get_args(mrb, "*", &argv, &argc); |
| 1121 |
while (argc--) { |
| 1122 |
len += fd_write(mrb, fd, *argv++); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
return mrb_int_value(mrb, len); |
| 1126 |
} |
| 1127 |
|
| 1128 |
static mrb_value |
| 1129 |
io_close(mrb_state *mrb, mrb_value io) |
| 1130 |
{ |
| 1131 |
struct mrb_io *fptr; |
| 1132 |
fptr = io_get_open_fptr(mrb, io); |
| 1133 |
fptr_finalize(mrb, fptr, FALSE); |
| 1134 |
return mrb_nil_value(); |
| 1135 |
} |
| 1136 |
|
| 1137 |
static mrb_value |
| 1138 |
io_close_write(mrb_state *mrb, mrb_value io) |
| 1139 |
{ |
| 1140 |
struct mrb_io *fptr; |
| 1141 |
fptr = io_get_open_fptr(mrb, io); |
| 1142 |
if (close((int)fptr->fd2) == -1) { |
| 1143 |
mrb_sys_fail(mrb, "close"); |
| 1144 |
} |
| 1145 |
return mrb_nil_value(); |
| 1146 |
} |
| 1147 |
|
| 1148 |
static mrb_value |
| 1149 |
io_closed(mrb_state *mrb, mrb_value io) |
| 1150 |
{ |
| 1151 |
struct mrb_io *fptr; |
| 1152 |
fptr = (struct mrb_io*)mrb_data_get_ptr(mrb, io, &mrb_io_type); |
| 1153 |
if (fptr == NULL || fptr->fd >= 0) { |
| 1154 |
return mrb_false_value(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
return mrb_true_value(); |
| 1158 |
} |
| 1159 |
|
| 1160 |
static mrb_value |
| 1161 |
io_pos(mrb_state *mrb, mrb_value io) |
| 1162 |
{ |
| 1163 |
struct mrb_io *fptr = io_get_open_fptr(mrb, io); |
| 1164 |
off_t pos = lseek(fptr->fd, 0, SEEK_CUR); |
| 1165 |
if (pos == -1) mrb_sys_fail(mrb, 0); |
| 1166 |
|
| 1167 |
if (fptr->buf) { |
| 1168 |
return mrb_int_value(mrb, pos - fptr->buf->len); |
| 1169 |
} |
| 1170 |
else { |
| 1171 |
return mrb_int_value(mrb, pos); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
static mrb_value |
| 1176 |
io_pid(mrb_state *mrb, mrb_value io) |
| 1177 |
{ |
| 1178 |
struct mrb_io *fptr; |
| 1179 |
fptr = io_get_open_fptr(mrb, io); |
| 1180 |
|
| 1181 |
if (fptr->pid > 0) { |
| 1182 |
return mrb_fixnum_value(fptr->pid); |
| 1183 |
} |
| 1184 |
|
| 1185 |
return mrb_nil_value(); |
| 1186 |
} |
| 1187 |
|
| 1188 |
static struct timeval |
| 1189 |
time2timeval(mrb_state *mrb, mrb_value time) |
| 1190 |
{ |
| 1191 |
struct timeval t = { 0, 0 }; |
| 1192 |
|
| 1193 |
switch (mrb_type(time)) { |
| 1194 |
case MRB_TT_INTEGER: |
| 1195 |
t.tv_sec = (ftime_t)mrb_integer(time); |
| 1196 |
t.tv_usec = 0; |
| 1197 |
break; |
| 1198 |
|
| 1199 |
#ifndef MRB_NO_FLOAT |
| 1200 |
case MRB_TT_FLOAT: |
| 1201 |
t.tv_sec = (ftime_t)mrb_float(time); |
| 1202 |
t.tv_usec = (fsuseconds_t)((mrb_float(time) - t.tv_sec) * 1000000.0); |
| 1203 |
break; |
| 1204 |
#endif |
| 1205 |
|
| 1206 |
default: |
| 1207 |
mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); |
| 1208 |
} |
| 1209 |
|
| 1210 |
return t; |
| 1211 |
} |
| 1212 |
|
| 1213 |
#if !defined(_WIN32) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) |
| 1214 |
static mrb_value |
| 1215 |
io_s_pipe(mrb_state *mrb, mrb_value klass) |
| 1216 |
{ |
| 1217 |
mrb_value r = mrb_nil_value(); |
| 1218 |
mrb_value w = mrb_nil_value(); |
| 1219 |
struct mrb_io *fptr_r; |
| 1220 |
struct mrb_io *fptr_w; |
| 1221 |
int pipes[2]; |
| 1222 |
|
| 1223 |
if (io_pipe(mrb, pipes) == -1) { |
| 1224 |
mrb_sys_fail(mrb, "pipe"); |
| 1225 |
} |
| 1226 |
|
| 1227 |
r = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); |
| 1228 |
fptr_r = io_alloc(mrb); |
| 1229 |
fptr_r->fd = pipes[0]; |
| 1230 |
fptr_r->readable = 1; |
| 1231 |
DATA_TYPE(r) = &mrb_io_type; |
| 1232 |
DATA_PTR(r) = fptr_r; |
| 1233 |
io_buf_init(mrb, fptr_r); |
| 1234 |
|
| 1235 |
w = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); |
| 1236 |
fptr_w = io_alloc(mrb); |
| 1237 |
fptr_w->fd = pipes[1]; |
| 1238 |
fptr_w->writable = 1; |
| 1239 |
fptr_w->sync = 1; |
| 1240 |
DATA_TYPE(w) = &mrb_io_type; |
| 1241 |
DATA_PTR(w) = fptr_w; |
| 1242 |
|
| 1243 |
return mrb_assoc_new(mrb, r, w); |
| 1244 |
} |
| 1245 |
#endif |
| 1246 |
|
| 1247 |
static int |
| 1248 |
mrb_io_read_data_pending(mrb_state *mrb, struct mrb_io *fptr) |
| 1249 |
{ |
| 1250 |
if (fptr->buf && fptr->buf->len > 0) return 1; |
| 1251 |
return 0; |
| 1252 |
} |
| 1253 |
|
| 1254 |
static mrb_value |
| 1255 |
io_s_select(mrb_state *mrb, mrb_value klass) |
| 1256 |
{ |
| 1257 |
const mrb_value *argv; |
| 1258 |
mrb_int argc; |
| 1259 |
mrb_value read, read_io, write, except, timeout, list; |
| 1260 |
struct timeval *tp, timerec; |
| 1261 |
fd_set pset, rset, wset, eset; |
| 1262 |
fd_set *rp, *wp, *ep; |
| 1263 |
struct mrb_io *fptr; |
| 1264 |
int pending = 0; |
| 1265 |
mrb_value result; |
| 1266 |
int max = 0; |
| 1267 |
int interrupt_flag = 0; |
| 1268 |
int i, n; |
| 1269 |
|
| 1270 |
mrb_get_args(mrb, "*", &argv, &argc); |
| 1271 |
|
| 1272 |
if (argc < 1 || argc > 4) { |
| 1273 |
mrb_argnum_error(mrb, argc, 1, 4); |
| 1274 |
} |
| 1275 |
|
| 1276 |
timeout = mrb_nil_value(); |
| 1277 |
except = mrb_nil_value(); |
| 1278 |
write = mrb_nil_value(); |
| 1279 |
if (argc > 3) |
| 1280 |
timeout = argv[3]; |
| 1281 |
if (argc > 2) |
| 1282 |
except = argv[2]; |
| 1283 |
if (argc > 1) |
| 1284 |
write = argv[1]; |
| 1285 |
read = argv[0]; |
| 1286 |
|
| 1287 |
if (mrb_nil_p(timeout)) { |
| 1288 |
tp = NULL; |
| 1289 |
} |
| 1290 |
else { |
| 1291 |
timerec = time2timeval(mrb, timeout); |
| 1292 |
tp = &timerec; |
| 1293 |
} |
| 1294 |
|
| 1295 |
FD_ZERO(&pset); |
| 1296 |
if (!mrb_nil_p(read)) { |
| 1297 |
mrb_check_type(mrb, read, MRB_TT_ARRAY); |
| 1298 |
rp = &rset; |
| 1299 |
FD_ZERO(rp); |
| 1300 |
for (i = 0; i < RARRAY_LEN(read); i++) { |
| 1301 |
read_io = RARRAY_PTR(read)[i]; |
| 1302 |
fptr = io_get_open_fptr(mrb, read_io); |
| 1303 |
if (fptr->fd >= FD_SETSIZE) continue; |
| 1304 |
FD_SET(fptr->fd, rp); |
| 1305 |
if (mrb_io_read_data_pending(mrb, fptr)) { |
| 1306 |
pending++; |
| 1307 |
FD_SET(fptr->fd, &pset); |
| 1308 |
} |
| 1309 |
if (max < fptr->fd) |
| 1310 |
max = fptr->fd; |
| 1311 |
} |
| 1312 |
if (pending) { |
| 1313 |
timerec.tv_sec = timerec.tv_usec = 0; |
| 1314 |
tp = &timerec; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
else { |
| 1318 |
rp = NULL; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (!mrb_nil_p(write)) { |
| 1322 |
mrb_check_type(mrb, write, MRB_TT_ARRAY); |
| 1323 |
wp = &wset; |
| 1324 |
FD_ZERO(wp); |
| 1325 |
for (i = 0; i < RARRAY_LEN(write); i++) { |
| 1326 |
fptr = io_get_open_fptr(mrb, RARRAY_PTR(write)[i]); |
| 1327 |
if (fptr->fd >= FD_SETSIZE) continue; |
| 1328 |
FD_SET(fptr->fd, wp); |
| 1329 |
if (max < fptr->fd) |
| 1330 |
max = fptr->fd; |
| 1331 |
if (fptr->fd2 >= 0) { |
| 1332 |
FD_SET(fptr->fd2, wp); |
| 1333 |
if (max < fptr->fd2) |
| 1334 |
max = fptr->fd2; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
} |
| 1338 |
else { |
| 1339 |
wp = NULL; |
| 1340 |
} |
| 1341 |
|
| 1342 |
if (!mrb_nil_p(except)) { |
| 1343 |
mrb_check_type(mrb, except, MRB_TT_ARRAY); |
| 1344 |
ep = &eset; |
| 1345 |
FD_ZERO(ep); |
| 1346 |
for (i = 0; i < RARRAY_LEN(except); i++) { |
| 1347 |
fptr = io_get_open_fptr(mrb, RARRAY_PTR(except)[i]); |
| 1348 |
if (fptr->fd >= FD_SETSIZE) continue; |
| 1349 |
FD_SET(fptr->fd, ep); |
| 1350 |
if (max < fptr->fd) |
| 1351 |
max = fptr->fd; |
| 1352 |
if (fptr->fd2 >= 0) { |
| 1353 |
FD_SET(fptr->fd2, ep); |
| 1354 |
if (max < fptr->fd2) |
| 1355 |
max = fptr->fd2; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
else { |
| 1360 |
ep = NULL; |
| 1361 |
} |
| 1362 |
|
| 1363 |
max++; |
| 1364 |
|
| 1365 |
retry: |
| 1366 |
n = select(max, rp, wp, ep, tp); |
| 1367 |
if (n < 0) { |
| 1368 |
#ifdef _WIN32 |
| 1369 |
errno = WSAGetLastError(); |
| 1370 |
if (errno != WSAEINTR) |
| 1371 |
mrb_sys_fail(mrb, "select failed"); |
| 1372 |
#else |
| 1373 |
if (errno != EINTR) |
| 1374 |
mrb_sys_fail(mrb, "select failed"); |
| 1375 |
#endif |
| 1376 |
if (tp == NULL) |
| 1377 |
goto retry; |
| 1378 |
interrupt_flag = 1; |
| 1379 |
} |
| 1380 |
|
| 1381 |
if (!pending && n == 0) |
| 1382 |
return mrb_nil_value(); |
| 1383 |
|
| 1384 |
result = mrb_ary_new_capa(mrb, 3); |
| 1385 |
mrb_ary_push(mrb, result, rp? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); |
| 1386 |
mrb_ary_push(mrb, result, wp? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); |
| 1387 |
mrb_ary_push(mrb, result, ep? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); |
| 1388 |
|
| 1389 |
if (interrupt_flag == 0) { |
| 1390 |
if (rp) { |
| 1391 |
list = RARRAY_PTR(result)[0]; |
| 1392 |
for (i = 0; i < RARRAY_LEN(read); i++) { |
| 1393 |
fptr = io_get_open_fptr(mrb, RARRAY_PTR(read)[i]); |
| 1394 |
if (FD_ISSET(fptr->fd, rp) || |
| 1395 |
FD_ISSET(fptr->fd, &pset)) { |
| 1396 |
mrb_ary_push(mrb, list, RARRAY_PTR(read)[i]); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
if (wp) { |
| 1402 |
list = RARRAY_PTR(result)[1]; |
| 1403 |
for (i = 0; i < RARRAY_LEN(write); i++) { |
| 1404 |
fptr = io_get_open_fptr(mrb, RARRAY_PTR(write)[i]); |
| 1405 |
if (FD_ISSET(fptr->fd, wp)) { |
| 1406 |
mrb_ary_push(mrb, list, RARRAY_PTR(write)[i]); |
| 1407 |
} |
| 1408 |
else if (fptr->fd2 >= 0 && FD_ISSET(fptr->fd2, wp)) { |
| 1409 |
mrb_ary_push(mrb, list, RARRAY_PTR(write)[i]); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
if (ep) { |
| 1415 |
list = RARRAY_PTR(result)[2]; |
| 1416 |
for (i = 0; i < RARRAY_LEN(except); i++) { |
| 1417 |
fptr = io_get_open_fptr(mrb, RARRAY_PTR(except)[i]); |
| 1418 |
if (FD_ISSET(fptr->fd, ep)) { |
| 1419 |
mrb_ary_push(mrb, list, RARRAY_PTR(except)[i]); |
| 1420 |
} |
| 1421 |
else if (fptr->fd2 >= 0 && FD_ISSET(fptr->fd2, ep)) { |
| 1422 |
mrb_ary_push(mrb, list, RARRAY_PTR(except)[i]); |
| 1423 |
} |
| 1424 |
} |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
return result; |
| 1429 |
} |
| 1430 |
|
| 1431 |
int |
| 1432 |
mrb_io_fileno(mrb_state *mrb, mrb_value io) |
| 1433 |
{ |
| 1434 |
struct mrb_io *fptr; |
| 1435 |
fptr = io_get_open_fptr(mrb, io); |
| 1436 |
return fptr->fd; |
| 1437 |
} |
| 1438 |
|
| 1439 |
static mrb_value |
| 1440 |
io_fileno(mrb_state *mrb, mrb_value io) |
| 1441 |
{ |
| 1442 |
int fd = mrb_io_fileno(mrb, io); |
| 1443 |
return mrb_fixnum_value(fd); |
| 1444 |
} |
| 1445 |
|
| 1446 |
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
| 1447 |
static mrb_value |
| 1448 |
io_close_on_exec_p(mrb_state *mrb, mrb_value io) |
| 1449 |
{ |
| 1450 |
struct mrb_io *fptr; |
| 1451 |
int ret; |
| 1452 |
|
| 1453 |
fptr = io_get_open_fptr(mrb, io); |
| 1454 |
|
| 1455 |
if (fptr->fd2 >= 0) { |
| 1456 |
if ((ret = fcntl(fptr->fd2, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); |
| 1457 |
if (!(ret & FD_CLOEXEC)) return mrb_false_value(); |
| 1458 |
} |
| 1459 |
|
| 1460 |
if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); |
| 1461 |
if (!(ret & FD_CLOEXEC)) return mrb_false_value(); |
| 1462 |
return mrb_true_value(); |
| 1463 |
} |
| 1464 |
#else |
| 1465 |
# define io_close_on_exec_p mrb_notimplement_m |
| 1466 |
#endif |
| 1467 |
|
| 1468 |
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
| 1469 |
static mrb_value |
| 1470 |
io_set_close_on_exec(mrb_state *mrb, mrb_value io) |
| 1471 |
{ |
| 1472 |
struct mrb_io *fptr; |
| 1473 |
int flag, ret; |
| 1474 |
mrb_bool b; |
| 1475 |
|
| 1476 |
fptr = io_get_open_fptr(mrb, io); |
| 1477 |
mrb_get_args(mrb, "b", &b); |
| 1478 |
flag = b ? FD_CLOEXEC : 0; |
| 1479 |
|
| 1480 |
if (fptr->fd2 >= 0) { |
| 1481 |
if ((ret = fcntl(fptr->fd2, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); |
| 1482 |
if ((ret & FD_CLOEXEC) != flag) { |
| 1483 |
ret = (ret & ~FD_CLOEXEC) | flag; |
| 1484 |
ret = fcntl(fptr->fd2, F_SETFD, ret); |
| 1485 |
|
| 1486 |
if (ret == -1) mrb_sys_fail(mrb, "F_SETFD failed"); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); |
| 1491 |
if ((ret & FD_CLOEXEC) != flag) { |
| 1492 |
ret = (ret & ~FD_CLOEXEC) | flag; |
| 1493 |
ret = fcntl(fptr->fd, F_SETFD, ret); |
| 1494 |
if (ret == -1) mrb_sys_fail(mrb, "F_SETFD failed"); |
| 1495 |
} |
| 1496 |
|
| 1497 |
return mrb_bool_value(b); |
| 1498 |
} |
| 1499 |
#else |
| 1500 |
# define io_set_close_on_exec mrb_notimplement_m |
| 1501 |
#endif |
| 1502 |
|
| 1503 |
static mrb_value |
| 1504 |
io_set_sync(mrb_state *mrb, mrb_value io) |
| 1505 |
{ |
| 1506 |
struct mrb_io *fptr; |
| 1507 |
mrb_bool b; |
| 1508 |
|
| 1509 |
fptr = io_get_open_fptr(mrb, io); |
| 1510 |
mrb_get_args(mrb, "b", &b); |
| 1511 |
fptr->sync = b; |
| 1512 |
return mrb_bool_value(b); |
| 1513 |
} |
| 1514 |
|
| 1515 |
static mrb_value |
| 1516 |
io_sync(mrb_state *mrb, mrb_value io) |
| 1517 |
{ |
| 1518 |
struct mrb_io *fptr; |
| 1519 |
fptr = io_get_open_fptr(mrb, io); |
| 1520 |
return mrb_bool_value(fptr->sync); |
| 1521 |
} |
| 1522 |
|
| 1523 |
#ifndef MRB_WITH_IO_PREAD_PWRITE |
| 1524 |
# define io_pread mrb_notimplement_m |
| 1525 |
# define io_pwrite mrb_notimplement_m |
| 1526 |
#else |
| 1527 |
static off_t |
| 1528 |
value2off(mrb_state *mrb, mrb_value offv) |
| 1529 |
{ |
| 1530 |
return (off_t)mrb_as_int(mrb, offv); |
| 1531 |
} |
| 1532 |
|
| 1533 |
/* |
| 1534 |
* call-seq: |
| 1535 |
* pread(maxlen, offset, outbuf = "") -> outbuf |
| 1536 |
*/ |
| 1537 |
static mrb_value |
| 1538 |
io_pread(mrb_state *mrb, mrb_value io) |
| 1539 |
{ |
| 1540 |
mrb_value buf = mrb_nil_value(); |
| 1541 |
mrb_value off; |
| 1542 |
mrb_int maxlen; |
| 1543 |
|
| 1544 |
mrb_get_args(mrb, "io|S!", &maxlen, &off, &buf); |
| 1545 |
|
| 1546 |
return io_read_common(mrb, pread, io, buf, maxlen, value2off(mrb, off)); |
| 1547 |
} |
| 1548 |
|
| 1549 |
/* |
| 1550 |
* call-seq: |
| 1551 |
* pwrite(buffer, offset) -> wrote_bytes |
| 1552 |
*/ |
| 1553 |
static mrb_value |
| 1554 |
io_pwrite(mrb_state *mrb, mrb_value io) |
| 1555 |
{ |
| 1556 |
mrb_value buf, off; |
| 1557 |
|
| 1558 |
mrb_get_args(mrb, "So", &buf, &off); |
| 1559 |
|
| 1560 |
return io_write_common(mrb, pwrite, io_get_write_fptr(mrb, io), RSTRING_PTR(buf), RSTRING_LEN(buf), value2off(mrb, off)); |
| 1561 |
} |
| 1562 |
#endif /* MRB_WITH_IO_PREAD_PWRITE */ |
| 1563 |
|
| 1564 |
static mrb_value |
| 1565 |
io_ungetc(mrb_state *mrb, mrb_value io) |
| 1566 |
{ |
| 1567 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1568 |
struct mrb_io_buf *buf = fptr->buf; |
| 1569 |
mrb_value str; |
| 1570 |
mrb_int len; |
| 1571 |
|
| 1572 |
mrb_get_args(mrb, "S", &str); |
| 1573 |
len = RSTRING_LEN(str); |
| 1574 |
if (len > MRB_IO_BUF_SIZE - buf->len) { |
| 1575 |
fptr->buf = (struct mrb_io_buf*)mrb_realloc(mrb, buf, sizeof(struct mrb_io_buf)+buf->len+len-MRB_IO_BUF_SIZE); |
| 1576 |
buf = fptr->buf; |
| 1577 |
} |
| 1578 |
memmove(buf->mem+len, buf->mem+buf->start, buf->len); |
| 1579 |
memcpy(buf->mem, RSTRING_PTR(str), len); |
| 1580 |
buf->start = 0; |
| 1581 |
buf->len += len; |
| 1582 |
return mrb_nil_value(); |
| 1583 |
} |
| 1584 |
|
| 1585 |
static void |
| 1586 |
io_buf_reset(struct mrb_io *fptr) |
| 1587 |
{ |
| 1588 |
struct mrb_io_buf *buf = fptr->buf; |
| 1589 |
|
| 1590 |
buf->start = 0; |
| 1591 |
buf->len = 0; |
| 1592 |
} |
| 1593 |
|
| 1594 |
static void |
| 1595 |
io_buf_shift(struct mrb_io *fptr, mrb_int n) |
| 1596 |
{ |
| 1597 |
struct mrb_io_buf *buf = fptr->buf; |
| 1598 |
|
| 1599 |
buf->start += n; |
| 1600 |
buf->len -= n; |
| 1601 |
} |
| 1602 |
|
| 1603 |
#ifdef MRB_UTF8_STRING |
| 1604 |
static void |
| 1605 |
io_buf_fill_comp(mrb_state *mrb, struct mrb_io *fptr) |
| 1606 |
{ |
| 1607 |
struct mrb_io_buf *buf = fptr->buf; |
| 1608 |
int keep = buf->len; |
| 1609 |
|
| 1610 |
memmove(buf->mem, buf->mem+buf->start, keep); |
| 1611 |
int n = read(fptr->fd, buf->mem+keep, MRB_IO_BUF_SIZE-keep); |
| 1612 |
if (n < 0) mrb_sys_fail(mrb, 0); |
| 1613 |
if (n == 0) fptr->eof = 1; |
| 1614 |
buf->start = 0; |
| 1615 |
buf->len += (short)n; |
| 1616 |
} |
| 1617 |
#endif |
| 1618 |
|
| 1619 |
static void |
| 1620 |
io_buf_fill(mrb_state *mrb, struct mrb_io *fptr) |
| 1621 |
{ |
| 1622 |
struct mrb_io_buf *buf = fptr->buf; |
| 1623 |
|
| 1624 |
if (buf->len > 0) return; |
| 1625 |
|
| 1626 |
int n = read(fptr->fd, buf->mem, MRB_IO_BUF_SIZE); |
| 1627 |
if (n < 0) mrb_sys_fail(mrb, 0); |
| 1628 |
if (n == 0) fptr->eof = 1; |
| 1629 |
buf->start = 0; |
| 1630 |
buf->len = (short)n; |
| 1631 |
} |
| 1632 |
|
| 1633 |
static mrb_value |
| 1634 |
io_eof(mrb_state *mrb, mrb_value io) |
| 1635 |
{ |
| 1636 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1637 |
|
| 1638 |
io_buf_fill(mrb, fptr); |
| 1639 |
if (fptr->buf->len > 0) return mrb_false_value(); |
| 1640 |
return mrb_bool_value(fptr->eof); |
| 1641 |
} |
| 1642 |
|
| 1643 |
static void |
| 1644 |
io_buf_cat(mrb_state *mrb, mrb_value outbuf, struct mrb_io *fptr, mrb_int n) |
| 1645 |
{ |
| 1646 |
struct mrb_io_buf *buf = fptr->buf; |
| 1647 |
|
| 1648 |
mrb_assert(n <= buf->len); |
| 1649 |
mrb_str_cat(mrb, outbuf, buf->mem+buf->start, n); |
| 1650 |
io_buf_shift(fptr, n); |
| 1651 |
} |
| 1652 |
|
| 1653 |
static void |
| 1654 |
io_buf_cat_all(mrb_state *mrb, mrb_value outbuf, struct mrb_io *fptr) |
| 1655 |
{ |
| 1656 |
struct mrb_io_buf *buf = fptr->buf; |
| 1657 |
|
| 1658 |
mrb_str_cat(mrb, outbuf, buf->mem+buf->start, buf->len); |
| 1659 |
io_buf_reset(fptr); |
| 1660 |
} |
| 1661 |
|
| 1662 |
static mrb_value |
| 1663 |
io_read_all(mrb_state *mrb, struct mrb_io *fptr, mrb_value outbuf) |
| 1664 |
{ |
| 1665 |
for (;;) { |
| 1666 |
io_buf_fill(mrb, fptr); |
| 1667 |
if (fptr->eof) { |
| 1668 |
return outbuf; |
| 1669 |
} |
| 1670 |
io_buf_cat_all(mrb, outbuf, fptr); |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
static mrb_value |
| 1675 |
io_read(mrb_state *mrb, mrb_value io) |
| 1676 |
{ |
| 1677 |
mrb_value outbuf = mrb_nil_value(); |
| 1678 |
mrb_value len; |
| 1679 |
mrb_int length = 0; |
| 1680 |
mrb_bool length_given; |
| 1681 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1682 |
|
| 1683 |
mrb_get_args(mrb, "|o?S", &len, &length_given, &outbuf); |
| 1684 |
if (length_given) { |
| 1685 |
if (mrb_nil_p(len)) { |
| 1686 |
length_given = FALSE; |
| 1687 |
} |
| 1688 |
else { |
| 1689 |
length = mrb_as_int(mrb, len); |
| 1690 |
if (length < 0) { |
| 1691 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative length %d given", length); |
| 1692 |
} |
| 1693 |
if (length == 0) { |
| 1694 |
return mrb_str_new(mrb, NULL, 0); |
| 1695 |
} |
| 1696 |
} |
| 1697 |
} |
| 1698 |
|
| 1699 |
if (mrb_nil_p(outbuf)) { |
| 1700 |
outbuf = mrb_str_new_capa(mrb, MRB_IO_BUF_SIZE); |
| 1701 |
} |
| 1702 |
if (!length_given) { /* read as much as possible */ |
| 1703 |
return io_read_all(mrb, fptr, outbuf); |
| 1704 |
} |
| 1705 |
for (;;) { |
| 1706 |
io_buf_fill(mrb, fptr); |
| 1707 |
if (fptr->eof || length == 0) { |
| 1708 |
if (RSTRING_LEN(outbuf) == 0) |
| 1709 |
return mrb_nil_value(); |
| 1710 |
return outbuf; |
| 1711 |
} |
| 1712 |
if (fptr->buf->len < length) { |
| 1713 |
length -= fptr->buf->len; |
| 1714 |
io_buf_cat_all(mrb, outbuf, fptr); |
| 1715 |
} |
| 1716 |
else { |
| 1717 |
io_buf_cat(mrb, outbuf, fptr, length); |
| 1718 |
return outbuf; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
static mrb_int |
| 1724 |
io_find_index(struct mrb_io *fptr, const char *rs, mrb_int rslen) |
| 1725 |
{ |
| 1726 |
struct mrb_io_buf *buf = fptr->buf; |
| 1727 |
|
| 1728 |
mrb_assert(rslen > 0); |
| 1729 |
const char c = rs[0]; |
| 1730 |
const mrb_int limit = buf->len - rslen + 1; |
| 1731 |
const char *p = buf->mem+buf->start; |
| 1732 |
for (mrb_int i=0; i<limit; i++) { |
| 1733 |
if (p[i] == c && (rslen == 1 || memcmp(p+i, rs, rslen) == 0)) { |
| 1734 |
return i; |
| 1735 |
} |
| 1736 |
} |
| 1737 |
return -1; |
| 1738 |
} |
| 1739 |
|
| 1740 |
static mrb_value |
| 1741 |
io_gets(mrb_state *mrb, mrb_value io) |
| 1742 |
{ |
| 1743 |
mrb_value rs = mrb_nil_value(); |
| 1744 |
mrb_int limit; |
| 1745 |
mrb_bool rs_given = FALSE; /* newline break */ |
| 1746 |
mrb_bool limit_given = FALSE; /* no limit */ |
| 1747 |
mrb_value outbuf; |
| 1748 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1749 |
struct mrb_io_buf *buf = fptr->buf; |
| 1750 |
|
| 1751 |
mrb_get_args(mrb, "|o?i?", &rs, &rs_given, &limit, &limit_given); |
| 1752 |
|
| 1753 |
if (limit_given == FALSE) { |
| 1754 |
if (rs_given) { |
| 1755 |
if (mrb_nil_p(rs)) { |
| 1756 |
rs_given = FALSE; |
| 1757 |
} |
| 1758 |
else if (mrb_integer_p(rs)) { |
| 1759 |
limit = mrb_integer(rs); |
| 1760 |
limit_given = TRUE; |
| 1761 |
rs = mrb_nil_value(); |
| 1762 |
} |
| 1763 |
else if (!mrb_string_p(rs)) { |
| 1764 |
mrb_ensure_int_type(mrb, rs); |
| 1765 |
} |
| 1766 |
} |
| 1767 |
} |
| 1768 |
if (rs_given) { |
| 1769 |
if (mrb_nil_p(rs)) { |
| 1770 |
rs_given = FALSE; |
| 1771 |
} |
| 1772 |
else { |
| 1773 |
mrb_ensure_string_type(mrb, rs); |
| 1774 |
if (RSTRING_LEN(rs) == 0) { /* paragraph mode */ |
| 1775 |
rs = mrb_str_new_lit(mrb, "\n\n"); |
| 1776 |
} |
| 1777 |
} |
| 1778 |
} |
| 1779 |
else { |
| 1780 |
rs = mrb_str_new_lit(mrb, "\n"); |
| 1781 |
rs_given = TRUE; |
| 1782 |
} |
| 1783 |
|
| 1784 |
/* from now on rs_given==FALSE means no RS */ |
| 1785 |
if (mrb_nil_p(rs) && !limit_given) { |
| 1786 |
return io_read_all(mrb, fptr, mrb_str_new_capa(mrb, MRB_IO_BUF_SIZE)); |
| 1787 |
} |
| 1788 |
|
| 1789 |
io_buf_fill(mrb, fptr); |
| 1790 |
if (fptr->eof) return mrb_nil_value(); |
| 1791 |
|
| 1792 |
if (limit_given) { |
| 1793 |
if (limit == 0) return mrb_str_new(mrb, NULL, 0); |
| 1794 |
outbuf = mrb_str_new_capa(mrb, limit); |
| 1795 |
} |
| 1796 |
else { |
| 1797 |
outbuf = mrb_str_new(mrb, NULL, 0); |
| 1798 |
} |
| 1799 |
|
| 1800 |
for (;;) { |
| 1801 |
if (rs_given) { /* with RS */ |
| 1802 |
int rslen = RSTRING_LEN(rs); |
| 1803 |
mrb_int idx = io_find_index(fptr, RSTRING_PTR(rs), rslen); |
| 1804 |
if (idx >= 0) { /* found */ |
| 1805 |
mrb_int n = idx+rslen; |
| 1806 |
if (limit_given && limit < n) { |
| 1807 |
n = limit; |
| 1808 |
} |
| 1809 |
io_buf_cat(mrb, outbuf, fptr, n); |
| 1810 |
return outbuf; |
| 1811 |
} |
| 1812 |
} |
| 1813 |
if (limit_given) { |
| 1814 |
if (limit <= buf->len) { |
| 1815 |
io_buf_cat(mrb, outbuf, fptr, limit); |
| 1816 |
return outbuf; |
| 1817 |
} |
| 1818 |
limit -= buf->len; |
| 1819 |
} |
| 1820 |
io_buf_cat_all(mrb, outbuf, fptr); |
| 1821 |
io_buf_fill(mrb, fptr); |
| 1822 |
if (fptr->eof) { |
| 1823 |
if (RSTRING_LEN(outbuf) == 0) return mrb_nil_value(); |
| 1824 |
return outbuf; |
| 1825 |
} |
| 1826 |
} |
| 1827 |
} |
| 1828 |
|
| 1829 |
static mrb_value |
| 1830 |
io_readline(mrb_state *mrb, mrb_value io) |
| 1831 |
{ |
| 1832 |
mrb_value result = io_gets(mrb, io); |
| 1833 |
if (mrb_nil_p(result)) { |
| 1834 |
eof_error(mrb); |
| 1835 |
} |
| 1836 |
return result; |
| 1837 |
} |
| 1838 |
|
| 1839 |
static mrb_value |
| 1840 |
io_readlines(mrb_state *mrb, mrb_value io) |
| 1841 |
{ |
| 1842 |
mrb_value ary = mrb_ary_new(mrb); |
| 1843 |
for (;;) { |
| 1844 |
mrb_value line = io_gets(mrb, io); |
| 1845 |
|
| 1846 |
if (mrb_nil_p(line)) return ary; |
| 1847 |
mrb_ary_push(mrb, ary, line); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
static mrb_value |
| 1852 |
io_getc(mrb_state *mrb, mrb_value io) |
| 1853 |
{ |
| 1854 |
mrb_int len = 1; |
| 1855 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1856 |
|
| 1857 |
io_buf_fill(mrb, fptr); |
| 1858 |
if (fptr->eof) return mrb_nil_value(); |
| 1859 |
#ifdef MRB_UTF8_STRING |
| 1860 |
struct mrb_io_buf *buf = fptr->buf; |
| 1861 |
const char *p = &buf->mem[buf->start]; |
| 1862 |
if ((*p) & 0x80) { |
| 1863 |
len = mrb_utf8len(p, p+buf->len); |
| 1864 |
if (len == 1 && buf->len < 4) { /* partial UTF-8 */ |
| 1865 |
io_buf_fill_comp(mrb, fptr); |
| 1866 |
p = &buf->mem[buf->start]; |
| 1867 |
len = mrb_utf8len(p, p+buf->len); |
| 1868 |
} |
| 1869 |
} |
| 1870 |
#endif |
| 1871 |
mrb_value str = mrb_str_new(mrb, fptr->buf->mem+fptr->buf->start, len); |
| 1872 |
io_buf_shift(fptr, len); |
| 1873 |
return str; |
| 1874 |
} |
| 1875 |
|
| 1876 |
static mrb_value |
| 1877 |
io_readchar(mrb_state *mrb, mrb_value io) |
| 1878 |
{ |
| 1879 |
mrb_value result = io_getc(mrb, io); |
| 1880 |
if (mrb_nil_p(result)) { |
| 1881 |
eof_error(mrb); |
| 1882 |
} |
| 1883 |
return result; |
| 1884 |
} |
| 1885 |
|
| 1886 |
static mrb_value |
| 1887 |
io_getbyte(mrb_state *mrb, mrb_value io) |
| 1888 |
{ |
| 1889 |
struct mrb_io *fptr = io_get_read_fptr(mrb, io); |
| 1890 |
|
| 1891 |
io_buf_fill(mrb, fptr); |
| 1892 |
if (fptr->eof) return mrb_nil_value(); |
| 1893 |
|
| 1894 |
unsigned char c = fptr->buf->mem[fptr->buf->start]; |
| 1895 |
io_buf_shift(fptr, 1); |
| 1896 |
return mrb_int_value(mrb, (mrb_int)c); |
| 1897 |
} |
| 1898 |
|
| 1899 |
static mrb_value |
| 1900 |
io_readbyte(mrb_state *mrb, mrb_value io) |
| 1901 |
{ |
| 1902 |
mrb_value result = io_getbyte(mrb, io); |
| 1903 |
if (mrb_nil_p(result)) { |
| 1904 |
eof_error(mrb); |
| 1905 |
} |
| 1906 |
return result; |
| 1907 |
} |
| 1908 |
|
| 1909 |
static mrb_value |
| 1910 |
io_flush(mrb_state *mrb, mrb_value io) |
| 1911 |
{ |
| 1912 |
io_get_open_fptr(mrb, io); |
| 1913 |
return io; |
| 1914 |
} |
| 1915 |
|
| 1916 |
void |
| 1917 |
mrb_init_io(mrb_state *mrb) |
| 1918 |
{ |
| 1919 |
struct RClass *io; |
| 1920 |
|
| 1921 |
io = mrb_define_class(mrb, "IO", mrb->object_class); |
| 1922 |
MRB_SET_INSTANCE_TT(io, MRB_TT_CDATA); |
| 1923 |
|
| 1924 |
mrb_include_module(mrb, io, mrb_module_get(mrb, "Enumerable")); /* 15.2.20.3 */ |
| 1925 |
mrb_define_class_method(mrb, io, "_popen", io_s_popen, MRB_ARGS_ARG(1,2)); |
| 1926 |
mrb_define_class_method(mrb, io, "_sysclose", io_s_sysclose, MRB_ARGS_REQ(1)); |
| 1927 |
mrb_define_class_method(mrb, io, "for_fd", io_s_for_fd, MRB_ARGS_ARG(1,2)); |
| 1928 |
mrb_define_class_method(mrb, io, "select", io_s_select, MRB_ARGS_ARG(1,3)); |
| 1929 |
mrb_define_class_method(mrb, io, "sysopen", io_s_sysopen, MRB_ARGS_ARG(1,2)); |
| 1930 |
#if !defined(_WIN32) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) |
| 1931 |
mrb_define_class_method(mrb, io, "_pipe", io_s_pipe, MRB_ARGS_NONE()); |
| 1932 |
#endif |
| 1933 |
|
| 1934 |
mrb_define_method(mrb, io, "initialize", io_init, MRB_ARGS_ARG(1,2)); |
| 1935 |
mrb_define_method(mrb, io, "initialize_copy", io_init_copy, MRB_ARGS_REQ(1)); |
| 1936 |
mrb_define_method(mrb, io, "isatty", io_isatty, MRB_ARGS_NONE()); |
| 1937 |
mrb_define_method(mrb, io, "eof?", io_eof, MRB_ARGS_NONE()); /* 15.2.20.5.6 */ |
| 1938 |
mrb_define_method(mrb, io, "getc", io_getc, MRB_ARGS_NONE()); /* 15.2.20.5.8 */ |
| 1939 |
mrb_define_method(mrb, io, "gets", io_gets, MRB_ARGS_OPT(2)); /* 15.2.20.5.9 */ |
| 1940 |
mrb_define_method(mrb, io, "read", io_read, MRB_ARGS_OPT(2)); /* 15.2.20.5.14 */ |
| 1941 |
mrb_define_method(mrb, io, "readchar", io_readchar, MRB_ARGS_NONE()); /* 15.2.20.5.15 */ |
| 1942 |
mrb_define_method(mrb, io, "readline", io_readline, MRB_ARGS_OPT(2)); /* 15.2.20.5.16 */ |
| 1943 |
mrb_define_method(mrb, io, "readlines", io_readlines, MRB_ARGS_OPT(2)); /* 15.2.20.5.17 */ |
| 1944 |
mrb_define_method(mrb, io, "sync", io_sync, MRB_ARGS_NONE()); /* 15.2.20.5.18 */ |
| 1945 |
mrb_define_method(mrb, io, "sync=", io_set_sync, MRB_ARGS_REQ(1)); /* 15.2.20.5.19 */ |
| 1946 |
mrb_define_method(mrb, io, "sysread", io_sysread, MRB_ARGS_ARG(1,1)); |
| 1947 |
mrb_define_method(mrb, io, "sysseek", io_sysseek, MRB_ARGS_ARG(1,1)); |
| 1948 |
mrb_define_method(mrb, io, "syswrite", io_syswrite, MRB_ARGS_REQ(1)); |
| 1949 |
mrb_define_method(mrb, io, "seek", io_seek, MRB_ARGS_ARG(1,1)); |
| 1950 |
mrb_define_method(mrb, io, "close", io_close, MRB_ARGS_NONE()); /* 15.2.20.5.1 */ |
| 1951 |
mrb_define_method(mrb, io, "close_write", io_close_write, MRB_ARGS_NONE()); |
| 1952 |
mrb_define_method(mrb, io, "close_on_exec=", io_set_close_on_exec, MRB_ARGS_REQ(1)); |
| 1953 |
mrb_define_method(mrb, io, "close_on_exec?", io_close_on_exec_p, MRB_ARGS_NONE()); |
| 1954 |
mrb_define_method(mrb, io, "closed?", io_closed, MRB_ARGS_NONE()); /* 15.2.20.5.2 */ |
| 1955 |
mrb_define_method(mrb, io, "flush", io_flush, MRB_ARGS_NONE()); /* 15.2.20.5.7 */ |
| 1956 |
mrb_define_method(mrb, io, "ungetc", io_ungetc, MRB_ARGS_REQ(1)); |
| 1957 |
mrb_define_method(mrb, io, "pos", io_pos, MRB_ARGS_NONE()); |
| 1958 |
mrb_define_method(mrb, io, "pid", io_pid, MRB_ARGS_NONE()); |
| 1959 |
mrb_define_method(mrb, io, "fileno", io_fileno, MRB_ARGS_NONE()); |
| 1960 |
mrb_define_method(mrb, io, "write", io_write, MRB_ARGS_ANY()); /* 15.2.20.5.20 */ |
| 1961 |
mrb_define_method(mrb, io, "pread", io_pread, MRB_ARGS_ANY()); /* ruby 2.5 feature */ |
| 1962 |
mrb_define_method(mrb, io, "pwrite", io_pwrite, MRB_ARGS_ANY()); /* ruby 2.5 feature */ |
| 1963 |
mrb_define_method(mrb, io, "getbyte", io_getbyte, MRB_ARGS_NONE()); |
| 1964 |
mrb_define_method(mrb, io, "readbyte", io_readbyte, MRB_ARGS_NONE()); |
| 1965 |
|
| 1966 |
mrb_define_const_id(mrb, io, MRB_SYM(SEEK_SET), mrb_fixnum_value(SEEK_SET)); |
| 1967 |
mrb_define_const_id(mrb, io, MRB_SYM(SEEK_CUR), mrb_fixnum_value(SEEK_CUR)); |
| 1968 |
mrb_define_const_id(mrb, io, MRB_SYM(SEEK_END), mrb_fixnum_value(SEEK_END)); |
| 1969 |
} |