popen Subroutine

Purpose

Initiates a pipe to a process.

Library

Standard C Library (libc.a)

Syntax

#include <stdio.h>

FILE *popen ( Command,  Type)
const char *Command, *Type;

Description

The popen subroutine creates a pipe between the calling program and a shell command to be executed.

Note: The popen subroutine runs only sh shell commands. The results are unpredictable if the Command parameter is not a valid sh shell command. If the terminal is in a trusted state, the tsh shell commands are run.

If streams opened by previous calls to the popen subroutine remain open in the parent process, the popen subroutine closes them in the child process.

The popen subroutine returns a pointer to a FILE structure for the stream.

Attention: If the original processes and the process started with the popen subroutine concurrently read or write a common file, neither should use buffered I/O. If they do, the results are unpredictable.

Some problems with an output filter can be prevented by flushing the buffer with the fflush subroutine.

Parameters

Item Description
Command Points to a null-terminated string containing a shell command line.
Type Points to a null-terminated string containing an I/O mode. If the Type parameter is the value r, you can read from the standard output of the command by reading from the file Stream. If the Type parameter is the value w, you can write to the standard input of the command by writing to the file Stream.

Because open files are shared, a type r command can be used as an input filter and a type w command as an output filter.

Return Values

The popen subroutine returns a null pointer if files or processes cannot be created, or if the shell cannot be accessed.

Error Codes

The popen subroutine may set the EINVAL variable if the Type parameter is not valid. The popen subroutine may also set errno global variables as described by the fork or pipe subroutines.