Discussion:
[hercules-os380] dup and dup2
kerravon86@yahoo.com.au [hercules-os380]
2018-09-10 06:32:13 UTC
Permalink
Peppe, some time ago you mentioned that
dup() and dup2() were used to do command
redirection.

Can you elaborate on what is required?

Here's my guess:

When the command processor has received
a command like:

fred > fred.txt

it should call dup() to save a handle (e.g. file
number 17) to normal stdout (file number 1),
then open fred.txt, e.g. file number 25, then
call dup2() to make file number 1 point to file
number 25, then close file number 25, then
run the command, then call dup2() to make
file number 1 a copy of file number 17, then
close file number 17.

Is that the right procedure?

Thanks. Paul.
Giuseppe Vitillaro giuseppe@vitillaro.org [hercules-os380]
2018-09-21 10:17:26 UTC
Permalink
Post by ***@yahoo.com.au [hercules-os380]
Peppe, some time ago you mentioned that
dup() and dup2() were used to do command
redirection.

Can you elaborate on what is required?

Here's my guess:

When the command processor has received
a command like:

fred > fred.txt

it should call dup() to save a handle (e.g. file
number 17) to normal stdout (file number 1),
then open fred.txt, e.g. file number 25, then
call dup2() to make file number 1 point to file
number 25, then close file number 25, then
run the command, then call dup2() to make
file number 1 a copy of file number 17, then
close file number 17.

Is that the right procedure?

Thanks. Paul.

---

Apologies for the delay, Paul, but the
really opaque Yahoo algorithm sent me
the messages of last weeks just now ;-(

I think this discussion of stackoverflow
should clear the topic, Paul:

https://stackoverflow.com/questions/1720535/practical-examples-use-dup-or-dup2

giving enough examples for checking the
correct sequences (I always forgot) of
dup/dup2 used in redirection.

Those sequences are also used, on the UNIX
platform, for daemons and logging purpouses,
beside shell command redirection.

There are many variants which may be useful
for different goals. all founded on the dup,
dup2, dup3 calls.

A reference to the Linux manual page may also
be useful:

http://man7.org/linux/man-pages/man2/dup.2.html

Peppe.

Loading...