STDIN inheritance during exec and difference between getc and sysread

Hi all!

I've accidentally found that "getc" and "sysread" behave differently
after "exec"
getc does not get remaining data from the STDIN but sysread does it

bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.getc; exec 'ruby',
'-e', 'p STDIN.getc'"
"a"
nil
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.sysread(1); exec
'ruby', '-e', 'p STDIN.sysread(1)'"
"a"
"b"
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $

why so? where is "b" in the first case?

···

--
Posted via http://www.ruby-forum.com/.

getc uses Ruby I/O buffering in user space, so the process read
more bytes than it needed the first time getc was called.

Buffered I/O leads to surprises like this behavior across fork/exec.

···

Dmitry Bolshakov <lists@ruby-forum.com> wrote:

Hi all!

I've accidentally found that "getc" and "sysread" behave differently
after "exec"
getc does not get remaining data from the STDIN but sysread does it

bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.getc; exec 'ruby',
'-e', 'p STDIN.getc'"
"a"
nil
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.sysread(1); exec
'ruby', '-e', 'p STDIN.sysread(1)'"
"a"
"b"
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $

why so? where is "b" in the first case?