COHERENT manpages

This page displays the COHERENT manpage for popen() [Open a pipe].

List of available manpages
Index


popen() -- STDIO Function (libc)

Open a pipe
#include <stdio.h>
FILE *popen(command, how)
char *command, *how;

popen() opens  a pipe.  It resembles the function  fopen(), except that the
opened object is a command line to the shell sh rather than a file.

The caller can read the standard  output of command when how is r, or write
to the standard input of command  when how is w.  popen() returns a pointer
to a FILE structure that may be read or written.

Example

This example is equivalent to the command

ls -l | mail me
where me is your login identifier.

#include <stdio.h>
main()
{
    FILE *ifp, *ofp;
    int c;

    if ((NULL == (ofp = popen("lmail me", "w"))) ||
        (NULL == (ifp = popen("ls -l",     "r")))) {
            fprintf(stderr, "cannot popen\n");
        exit(1);
    }

    while (EOF != (c = fgetc(ifp)))
        fputc(c, ofp);

    pclose(ifp);
    pclose(ofp);
}

Files

<stdio.h>

See Also

fclose(),
fopen(),
libc,
pclose(),
pipe(),
sh,
system(),
wait()

Diagnostics

popen() returns NULL if the link to command could not be established.