Re: Running a command-line program and reading/writing its stdin/stdout/stderr

From: Luca T. (lucat_at_despammed.com)
Date: 03/21/04


Date: Sun, 21 Mar 2004 08:40:07 +0100

Paul Pluzhnikov wrote:
> "Luca T." <lucat@despammed.com> writes:
>
>
>>What's wrong here?
>
>
> Add some error checking, and you'll discover that
> fdopen(fdin[1],"w") succeeds, but fdopen(fdout[1],"r") fails.
>
> The reason it fails is that the 2 ends of the pipe are not
> equivalent: one is only good for writing, the other only for reading.
>
> There are too many other problems with your code to list.
>
> If you are serious about developing in C under UNIX, get Stevens'
> APUE and study it carefully. If you are not, ask someone else to
> write this program for you.

The reason why i begun writing this program (that is only at its early
stages of development) is _exactly_ to learn linux-programming with a
"real-world example".
I understand the need of a book to study and the book suggested by you
and by Basile is certainly one book that i'll buy in a recent future.
For now i have "Beginning Linux Programming [Neil Matthew & Richard
Stones]" and i also have "Advanced Linux Programming [M. Mitchell, J.
Oldham and A. Samuel]" from where the scheleton of this program comes
out. Reading better the chapter about pipes of ALP they clearly state
that one side of the pipe is for reading and the other side is for
writing, unfortunately i didn't see this (or better... in some way i
didn't think it was important) the first time i read it.
Anyways, i would be very glad if you (or someone else) would point out
all the possible problems of this program so that i'll have a chance to
better understand them and recognize them the next time.

This is the list of the problems that i can see here:

1) One side of the pipe is for writing and the other is for reading, so
i should invert 0s and 1s when i want to read from them;
2) Lack of error checking, i should add error-checking to every
system-call such as fdopen, pipe, dup2, execlp, etc;
3) I should have written fscanf in this way
"fscanf(streamout,"%255[^]",s);", in this way the output of fscanf can't
go to write data past the boundaries of "s";
4) The "execlp" line is wrong, the first argument is, by convention, the
name of the file being executed (even though i cannot understand why i
have to do this and execlp can't do this for me... oh well);

Do you (or someone else) see other problems in this source code?

Thank you,
   Luca



Relevant Pages

  • Re: C/C++ library to access a processs stdin and stdout?
    ... close the reading end of one pipe and the writing ... The remaining two file descriptors are your access to ...
    (comp.programming)
  • Re: a thread about something else
    ... Essence lays before the ... We can still value and recognise great writing in that sense, ... it becomes great reading as well as great writing. ... Do you have some reason for your agression aside from excessive ...
    (misc.writing)
  • Re: How to open a pipe for reading and writing?
    ... > I would like to know if there is a way to open a pipie for both reading ... > and writing? ... How can I open a pipe both to and from a command? ... See "Bidirectional Communication with Another Process" in per- ...
    (comp.lang.perl.misc)
  • Re: pipes - at least, I think thats what they are
    ... pipe exists as a file, so writing to it is just like writing to a file. ... A process trying to write to a named pipe with nothing reading it will ...
    (uk.comp.os.linux)
  • Re: Generating random signatures?
    ... >> reader on $SIGFILE. ... opening a FIFO for writing when no other process ... > has it open for reading will block until another process opens it for ... to the pipe again, and the reading process getting SIGPIPE. ...
    (comp.unix.shell)