StdIn/Stdout

StdIn/Stdout

Post by Kok Fon » Wed, 19 Feb 2003 13:00:08



Hi,

I'm writting a testing framework and need to spawn new Unix process.
However I need to redirect the StdOut of the Unix Process to a flat file for
storage.  For instance for the code below:

    if ((pid = fork ()) < 0) {
      err_sys("fork error");
    }
    if (pid == 0) {

Quote:>>>      execlp (buf, buf, (char *) 0);

      err_ret ("Couldn't execute : %s", buf);
      exit (127);
    }

I want to beable to redirect the StdIn and StdOut for the line

execlp (buf, buf, (char *) 0);

so I can output char to the process and receive the output from the process
and store it.

Does anybody have any suggestions?  So far I have been looking around on the
net and couldn't find anything at all.

Thanks,

The Poor  Programmer

 
 
 

StdIn/Stdout

Post by those who know me have no need of my nam » Wed, 19 Feb 2003 16:23:27


in comp.unix.shell i read:

Quote:>I'm writting a testing framework and need to spawn new Unix process.
>However I need to redirect the StdOut of the Unix Process to a flat file for
>storage.  For instance for the code below:
>Does anybody have any suggestions?  So far I have been looking around on the
>net and couldn't find anything at all.

yes, locate the unix programming faq.

also, this group is about using the various shells, comp.unix.programmer is
better for c programming on a unix/posix/sus platform.

--
bringing you boring signatures for 17 years

 
 
 

StdIn/Stdout

Post by Heiner Steve » Thu, 20 Feb 2003 07:36:47


[follow-ups redirected to comp.unix.programmer]

 > I'm writting a testing framework and need to spawn new Unix process.
 > However I need to redirect the StdOut of the Unix Process to a flat file for
 > storage.  For instance for the code below:
 >
 >     if ((pid = fork ()) < 0) {
 >       err_sys("fork error");
 >     }
 >     if (pid == 0) {
 >
 >>>>     execlp (buf, buf, (char *) 0);
 >
 >       err_ret ("Couldn't execute : %s", buf);
 >       exit (127);
 >     }
 >
 > I want to beable to redirect the StdIn and StdOut for the line
 >
 > execlp (buf, buf, (char *) 0);
 >
 > so I can output char to the process and receive the output from the process
 > and store it.

Before the execlp() you could set up the file descriptors in any
way you like. Example:

    in = open ("somepath", O_RDONLY);
    out = open ("someotherpath", O_WRONLY);

    if ( (pid = fork ()) < 0 )
        /* error */
    else if ( pid == 0 ) {    /* child process */
        if ( close (0) == -1 || dup (in) != 0 )
            perror ("cannot redirect stdin");
        if ( close (1) == -1 || dup (out) != 1 )
            perror ("cannot redirect stdout");
        execlp (...);
    }

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/

 
 
 

1. Controlling forked processes stdin/stdout

Is popen() the only way to interface with a forked processes stdin and stdout?
I'd like to be able for fork/exec a process, feed its stdin and capture its
stdout.  It looks to me like popen() will only let you do one or the other.
Thanks for the advice.

Mike Warren
HRB Systems
State College, PA

2. porting MAGIC to PC running Linux

3. Using separate window child process for stdin/stdout

4. is there a top??

5. Set stdin/stdout in binary on UNIX

6. Question on How to get Fwvm95, installed!!

7. redirect terminal stdin/stdout

8. PIPE Question

9. Temporary stdin/stdout redirection

10. Strange behaviour on stdin/stdout filter

11. Network communications thru stdin/stdout

12. reopen stdin, stdout

13. Establishing new streams for file descriptors stdin, stdout, stderr