Re: Running a command-line program and reading/writing its stdin/stdout/stderr
From: Luca T. (lucat_at_despammed.com)
Date: 03/21/04
- Next message: Kenji Chan: "Unicode/widechar version of linux API"
- Previous message: Kasper Dupont: "Re: Getting -1 as the value for the rlim_cur and rlim_max from the rlimit structure"
- In reply to: Paul Pluzhnikov: "Re: Running a command-line program and reading/writing its stdin/stdout/stderr"
- Next in thread: Kasper Dupont: "Re: Running a command-line program and reading/writing its stdin/stdout/stderr"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Kenji Chan: "Unicode/widechar version of linux API"
- Previous message: Kasper Dupont: "Re: Getting -1 as the value for the rlim_cur and rlim_max from the rlimit structure"
- In reply to: Paul Pluzhnikov: "Re: Running a command-line program and reading/writing its stdin/stdout/stderr"
- Next in thread: Kasper Dupont: "Re: Running a command-line program and reading/writing its stdin/stdout/stderr"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|