Created
          May 6, 2021 09:53 
        
      - 
      
- 
        Save udzura/ae7b8537054616a9c8475621ed3a6af9 to your computer and use it in GitHub Desktop. 
    dup2 を自分で使う場合
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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