Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 882a084

Browse files
committed
Add rb_io_path and rb_io_open_descriptor.
1 parent 612ad58 commit 882a084

File tree

5 files changed

+72
-33
lines changed

5 files changed

+72
-33
lines changed

ext/pty/pty.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -499,28 +499,21 @@ pty_open(VALUE klass)
499499
{
500500
int master_fd, slave_fd;
501501
char slavename[DEVICELEN];
502-
VALUE master_io, slave_file;
503-
rb_io_t *master_fptr, *slave_fptr;
504-
VALUE assoc;
505502

506503
getDevice(&master_fd, &slave_fd, slavename, 1);
507504

508-
master_io = rb_obj_alloc(rb_cIO);
509-
MakeOpenFile(master_io, master_fptr);
510-
master_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX;
511-
master_fptr->fd = master_fd;
512-
master_fptr->pathv = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename));
505+
VALUE master_path = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename));
506+
VALUE master_io = rb_io_open_descriptor(rb_cIO, master_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX, master_path, RUBY_IO_TIMEOUT_DEFAULT);
513507

514-
slave_file = rb_obj_alloc(rb_cFile);
515-
MakeOpenFile(slave_file, slave_fptr);
516-
slave_fptr->mode = FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY;
517-
slave_fptr->fd = slave_fd;
518-
slave_fptr->pathv = rb_obj_freeze(rb_str_new_cstr(slavename));
508+
VALUE slave_path = rb_obj_freeze(rb_sprintf("slavepty:%s", slavename));
509+
VALUE slave_file = rb_io_open_descriptor(rb_cFile, slave_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY, rb_str_new_cstr(slavename), RUBY_IO_TIMEOUT_DEFAULT);
510+
511+
VALUE assoc = rb_assoc_new(master_io, slave_file);
519512

520-
assoc = rb_assoc_new(master_io, slave_file);
521513
if (rb_block_given_p()) {
522514
return rb_ensure(rb_yield, assoc, pty_close_pty, assoc);
523515
}
516+
524517
return assoc;
525518
}
526519

ext/stringio/stringio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
276276
{
277277
VALUE string, vmode, opt;
278278
int oflags;
279-
struct rb_io_enc_t convconfig;
279+
struct rb_io_encoding convconfig;
280280

281281
argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
282282
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
@@ -1743,7 +1743,7 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
17431743
else {
17441744
enc = rb_find_encoding(ext_enc);
17451745
if (!enc) {
1746-
struct rb_io_enc_t convconfig;
1746+
struct rb_io_encoding convconfig;
17471747
int oflags, fmode;
17481748
VALUE vmode = rb_str_append(rb_str_new_cstr("r:"), ext_enc);
17491749
rb_io_extract_modeenc(&vmode, 0, Qnil, &oflags, &fmode, &convconfig);

include/ruby/io.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ typedef enum {
9191
* e1 NULL force_encoding(e1) convert str.encoding to e1
9292
* e1 e2 convert from e2 to e1 convert str.encoding to e2
9393
*/
94-
struct rb_io_enc_t {
94+
struct rb_io_encoding {
9595
/** Internal encoding. */
9696
rb_encoding *enc;
9797
/** External encoding. */
@@ -115,7 +115,7 @@ struct rb_io_enc_t {
115115
struct rb_io;
116116
typedef struct rb_io rb_io_t;
117117

118-
typedef struct rb_io_enc_t rb_io_enc_t;
118+
typedef struct rb_io_encoding rb_io_enc_t;
119119

120120
/**
121121
* @private
@@ -229,6 +229,11 @@ typedef struct rb_io_enc_t rb_io_enc_t;
229229

230230
/** @} */
231231

232+
/**
233+
* Allocate a new IO object, with the given file descriptor.
234+
*/
235+
VALUE rb_io_open_descriptor(VALUE klass, int descriptor, int mode, VALUE path, VALUE timeout, struct rb_io_encoding *encoding);
236+
232237
/**
233238
* Queries the underlying IO pointer.
234239
*
@@ -587,6 +592,12 @@ VALUE rb_io_set_write_io(VALUE io, VALUE w);
587592
*/
588593
void rb_io_set_nonblock(rb_io_t *fptr);
589594

595+
/**
596+
* Returns the path for the given IO.
597+
*
598+
*/
599+
VALUE rb_io_path(VALUE io);
600+
590601
/**
591602
* Returns an integer representing the numeric file descriptor for
592603
* <em>io</em>.

internal/io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct rb_io {
7676
*/
7777
struct rb_io_internal_buffer rbuf;
7878

79-
struct rb_io_enc_t encs; /**< Decomposed encoding flags. */
79+
struct rb_io_encoding encs; /**< Decomposed encoding flags. */
8080

8181
/** Encoding converter used when reading from this IO. */
8282
rb_econv_t *readconv;

io.c

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct argf {
216216
long lineno;
217217
VALUE argv;
218218
VALUE inplace;
219-
struct rb_io_enc_t encs;
219+
struct rb_io_encoding encs;
220220
int8_t init_p, next_p, binmode;
221221
};
222222

@@ -3375,6 +3375,14 @@ rb_io_set_nonblock(rb_io_t *fptr)
33753375
}
33763376
}
33773377

3378+
VALUE
3379+
rb_io_path(VALUE io)
3380+
{
3381+
rb_io_t *fptr;
3382+
GetOpenFile(io, fptr);
3383+
return fptr->pathv;
3384+
}
3385+
33783386
static VALUE
33793387
io_read_memory_call(VALUE arg)
33803388
{
@@ -6719,7 +6727,7 @@ rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2
67196727
return extracted;
67206728
}
67216729

6722-
typedef struct rb_io_enc_t convconfig_t;
6730+
typedef struct rb_io_encoding convconfig_t;
67236731

67246732
static void
67256733
validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *enc2)
@@ -9181,27 +9189,54 @@ stderr_getter(ID id, VALUE *ptr)
91819189
return rb_ractor_stderr();
91829190
}
91839191

9192+
VALUE
9193+
rb_io_open_descriptor(VALUE klass, int descriptor, int mode, VALUE path, VALUE timeout, struct rb_io_encoding *encoding)
9194+
{
9195+
VALUE self = io_alloc(klass);
9196+
struct rb_io *io = rb_io_make_open_file(self);
9197+
9198+
io->self = self;
9199+
io->fd = descriptor;
9200+
io->mode = mode;
9201+
9202+
if (NIL_P(path)) {
9203+
io->pathv = Qnil;
9204+
}
9205+
else {
9206+
StringValue(path);
9207+
io->pathv = rb_str_new_frozen(path);
9208+
}
9209+
9210+
io->timeout = timeout;
9211+
9212+
if (encoding) {
9213+
*io->encs = *encoding;
9214+
}
9215+
9216+
rb_update_max_fd(descriptor);
9217+
9218+
return self;
9219+
}
9220+
91849221
static VALUE
91859222
prep_io(int fd, int fmode, VALUE klass, const char *path)
91869223
{
9187-
rb_io_t *fp;
9188-
VALUE io = io_alloc(klass);
9224+
VALUE path_value = Qnil;
9225+
if (path) {
9226+
path_value = rb_obj_freeze(rb_str_new_cstr(path));
9227+
}
91899228

9190-
MakeOpenFile(io, fp);
9191-
fp->self = io;
9192-
fp->fd = fd;
9193-
fp->mode = fmode;
9194-
fp->timeout = Qnil;
9195-
if (!io_check_tty(fp)) {
9229+
VALUE self = rb_io_open_descriptor(klass, fd, fmode, path_value, Qnil);
9230+
struct rb_io *io = RFILE(self)->fptr;
9231+
9232+
if (!io_check_tty(io)) {
91969233
#ifdef __CYGWIN__
9197-
fp->mode |= FMODE_BINMODE;
9234+
io->mode |= FMODE_BINMODE;
91989235
setmode(fd, O_BINARY);
91999236
#endif
92009237
}
9201-
if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path));
9202-
rb_update_max_fd(fd);
92039238

9204-
return io;
9239+
return self;
92059240
}
92069241

92079242
VALUE

0 commit comments

Comments
 (0)