Skip to content

Instantly share code, notes, and snippets.

@udzura
Created May 6, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udzura/ae7b8537054616a9c8475621ed3a6af9 to your computer and use it in GitHub Desktop.
Save udzura/ae7b8537054616a9c8475621ed3a6af9 to your computer and use it in GitHub Desktop.
dup2 を自分で使う場合
require 'fiddle/import'
module Dupper
extend Fiddle::Importer
dlload 'libc.so.6'
extern 'int dup2(int oldfd, int newfd);'
end
require 'pty'
master, tty = PTY.open
pid = fork do
Dupper.dup2(tty.fileno, 0)
Dupper.dup2(tty.fileno, 1)
Dupper.dup2(tty.fileno, 2)
exec 'wc'
end
tty.close
master.puts "hello"
master.gets
master.puts "world"
master.gets
master.puts "\u{04}"
master.gets
begin
while l = master.readline
puts "PTY: " + l
end
rescue Errno::EIO => e
end
p Process.waitpid2(pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment