| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- initiate a pipe to or from a process
#include <stdio.h>FILE *popen(const char *command,const char *mode);
intpclose(FILE *stream);
Thepopen() function creates a pipe between the calling program and thecommand to be executed. The arguments topopen() are pointers tonull-terminated strings. Thecommand argument consists of a shell command line. Themode argument is an I/O mode, eitherr for readingorw for writing. The value returned is a stream pointer suchthat one can write to the standard input of the command, ifthe I/O mode isw, by writing to the filestream (seeIntro(3)); and one can read from the standard output of the command,if the I/O mode isr, by reading from the filestream.Because open files are shared, a typer command may be usedas an input filter and a typew as an output filter.A trailingF character can also be included in themode argumentas described infopen(3C) to enable extended FILE facility.
The environment of the executed command will be as if a childprocess were created within thepopen() call usingfork(2). If the application isstandard-conforming (seestandards(5)), the child is created as if invoked with thecall:
execl("/usr/xpg4/bin/sh", "sh", "-c",command,(char *)0);
otherwise, the child is created as if invoked with the call:
execl("/usr/bin/sh", "sh", "-c",command,(char *)0);
Thepclose() function closes a stream opened bypopen() by closing thepipe. It waits for the associated process to terminate and returns thetermination status of the process running the command language interpreter. This isthe value returned bywaitpid(3C). Seewait.h(3HEAD) for more information on termination status.If, however, a call towaitpid() with apid argument equal tothe process ID of the command line interpreter causes the termination statusto be unavailable topclose(), thenpclose() returns -1 witherrno set toECHILD to report this condition.
Upon successful completion,popen() returns a pointer to an open stream thatcan be used to read or write to the pipe. Otherwise, itreturns a null pointer and may seterrno to indicate the error.
Upon successful completion,pclose() returns the termination status of the command languageinterpreter as returned bywaitpid(). Otherwise, it returns-1 and setserrno to indicate the error.
Thepclose() function will fail if:
The status of the child process could not be obtained, as described in the DESCRIPTION.
Thepopen() function may fail if:
There are currentlyFOPEN_MAX orSTREAM_MAX streams open in the calling process.
Themode argument is invalid.
Thepopen() function may also seterrno values as described byfork(2)orpipe(2).
If the original andpopen() processes concurrently read or write a commonfile, neither should use buffered I/O. Problems with an output filter maybe forestalled by careful buffer flushing, for example, withfflush() (seefclose(3C)).A security hole exists through theIFS andPATH environment variables. Full pathnames should be used (orPATH reset) andIFS should beset to space and tab (" \t").
Even if the process has established a signal handler forSIGCHLD, itwill be called when the command terminates. Even if another threadin the same process issues await(3C) call, it will interfere withthe return value ofpclose(). Even if the process's signal handler forSIGCHLD has been set to ignore the signal, there will be noeffect onpclose().
Example 1popen() example
The following program will print on the standard output (seestdio(3C)) thenames of files in the current directory with a.c suffix.
#include <stdio.h>#include <stdlib.h>main( ){ char *cmd = "/usr/bin/ls *.c"; char buf[BUFSIZ]; FILE *ptr; if ((ptr = popen(cmd, "r")) != NULL) { while (fgets(buf, BUFSIZ, ptr) != NULL) (void) printf("%s", buf); (void) pclose(ptr); } return 0;}Example 2system() replacement
The following function can be used in a multithreaded process in placeof the most common usage of the Unsafesystem(3C) function:
int my_system(const char *cmd) { FILE *p; if ((p = popen(cmd, "w")) == NULL) return (-1); return (pclose(p)); }Seeattributes(5) for descriptions of the following attributes:
|
Forpclose() and all aspects ofpopen() except theF character inthemode argument, seestandards(5).
ksh(1),pipe(2),fclose(3C),fopen(3C),posix_spawn(3C),stdio(3C),system(3C),wait(3C),waitpid(3C),wait.h(3HEAD),attributes(5),standards(5)
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |