popen

Opens a pipe stream and executes a command.

Library:LibC
Classification:UNIX
Service:File and Directory I/O

Syntax

  #include <stdio.h> 
   
  FILE *popen (
     const char   *command,
     const char   *mode);
  

Parameters

command

(IN) Points to a null-terminated string specifying a command line as if the command were issued at the NetWare® System Console.

mode

(IN) Points to the pipe open mode.

  • If the mode is "r" when the child process is started, the parent's stdout is the writable end of the pipe and the file descriptor of the returned stream is the readable end of the pipe.

  • If the mode is "w" when the child process is started, the parent's stdin is the readable end of the pipe and the file descriptor of the returned stream is the writable end of the pipe.

To the "r" or the "w" mode, you can append one or more of the following modes (they must follow either the r or the w):

0

The zero specifies that the child process should be launched in the same address space. This in largely used by NLMs running in ring 0.

If the NLM is a POSIX NLM, this flag has no effect because only one process can live in the address space of a POSIX NLM.

s

This flag specifies that child process should load silently.

Return Values

If successful, returns a pointer to the stream that can be used for either reading or writing, depending upon the open mode.

If the open operation fails, popen returns a NULL pointer and sets errno to one of the following:

Decimal

Constant

Description

9

EINVAL

The mode argument is invalid.

11

EMFILE

The maximum number of file descriptors are currently open in the calling process.

Remarks

The popen function creates a pipe between the calling program and the child process, which can be used for both reading or writing. Both the parent and the child are capable of executing independently before either terminates.

The popen function ensures that any pipes previously opened by the parent remain open for the parent process but are closed in the new child process.

The popen function, in versions of LibC prior to October 2003, accepts only a path argument to an executable in the command parameter. It does not support command line options (for example, "sys:/mysql/bin/mysqldump --help" where help is an argument for mysqldump). Later versions of popen (LibC version dated October 2003 or later) support the following features:

  • Command line options in the command parameter

  • An 0 option for the mode parameter: "r0" and "w0". The 0 option causes the child NLM to be loaded into the same address space as the parent.

See Also