Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


popen(3p) — Linux manual page

PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT

POPEN(3P)               POSIX Programmer's ManualPOPEN(3P)

PROLOG        top

       This manual page is part of the POSIX Programmer's Manual.  The       Linux implementation of this interface may differ (consult the       corresponding Linux manual page for details of Linux behavior), or       the interface may not be implemented on Linux.

NAME        top

       popen — initiate pipe streams to or from a process

SYNOPSIS        top

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

DESCRIPTION        top

       Thepopen() function shall execute the command specified by the       stringcommand.  It shall create a pipe between the calling       program and the executed command, and shall return a pointer to a       stream that can be used to either read from or write to the pipe.       The environment of the executed command shall be as if a child       process were created within thepopen() call using thefork()       function, and the child invoked thesh utility using the call:           execl(shell path, "sh", "-c",command, (char *)0);       whereshell path is an unspecified pathname for thesh utility.       Thepopen() function shall ensure that any streams from previouspopen() calls that remain open in the parent process are closed in       the new child process.       Themode argument topopen() is a string that specifies I/O mode:        1. Ifmode isr, when the child process is started, its file           descriptor STDOUT_FILENO shall be the writable end of the           pipe, and the file descriptorfileno(stream) in the calling           process, wherestream is the stream pointer returned bypopen(), shall be the readable end of the pipe.        2. Ifmode isw, when the child process is started its file           descriptor STDIN_FILENO shall be the readable end of the pipe,           and the file descriptorfileno(stream) in the calling process,           wherestream is the stream pointer returned bypopen(), shall           be the writable end of the pipe.        3. Ifmode is any other value, the result is unspecified.       Afterpopen(), both the parent and the child process shall be       capable of executing independently before either terminates.       Pipe streams are byte-oriented.

RETURN VALUE        top

       Upon successful completion,popen() shall return a pointer to an       open stream that can be used to read or write to the pipe.       Otherwise, it shall return a null pointer and may seterrno to       indicate the error.

ERRORS        top

       Thepopen() function shall fail if:EMFILE{STREAM_MAX} streams are currently open in the calling              process.       Thepopen() function may fail if:EMFILE{FOPEN_MAX} streams are currently open in the calling              process.EINVALThemode argument is invalid.       Thepopen() function may also seterrno values as described byfork(3p) orpipe(3p).The following sections are informative.

EXAMPLES        top

Using popen() to Obtain a List of Files from the ls Utility       The following example demonstrates the use ofpopen() andpclose()       to execute the commandls* in order to obtain a list of files in       the current directory:           #include <stdio.h>           ...           FILE *fp;           int status;           char path[PATH_MAX];           fp = popen("ls *", "r");           if (fp == NULL)               /* Handle error */;           while (fgets(path, PATH_MAX, fp) != NULL)               printf("%s", path);           status = pclose(fp);           if (status == -1) {               /* Error reported by pclose() */               ...           } else {               /* Use macros described under wait() to inspect `status' in order                  to determine success/failure of command executed by popen() */               ...           }

APPLICATION USAGE        top

       Since open files are shared, a moder command can be used as an       input filter and a modew command as an output filter.       Buffered reading before opening an input filter may leave the       standard input of that filter mispositioned. Similar problems with       an output filter may be prevented by careful buffer flushing; for       example, withfflush(3p).       A stream opened bypopen() should be closed bypclose().       The behavior ofpopen() is specified for values ofmode ofr andw.  Other modes such asrb andwb might be supported by specific       implementations, but these would not be portable features. Note       that historical implementations ofpopen() only check to see if       the first character ofmode isr.  Thus, amode ofrobert therobot would be treated asmode r, and amode ofanything else       would be treated asmode w.       If the application callswaitpid() orwaitid() with apid argument       greater than 0, and it still has a stream that was called withpopen() open, it must ensure thatpid does not refer to the       process started bypopen().       To determine whether or not the environment specified in the Shell       and Utilities volume of POSIX.1‐2017 is present, use the function       call:           sysconf(_SC_2_VERSION)       (Seesysconf(3p)).

RATIONALE        top

       Thepopen() function should not be used by programs that have set       user (or group) ID privileges. Thefork() andexec family of       functions (exceptexeclp() andexecvp()), should be used instead.       This prevents any unforeseen manipulation of the environment of       the user that could cause execution of commands not anticipated by       the calling program.       If the original andpopen()ed processes both intend to read or       write or read and write a common file, and either will be using       FILE-type C functions (fread(),fwrite(), and so on), the rules       for sharing file handles must be observed (seeSection 2.5.1,Interaction of File Descriptors and Standard I/O Streams).

FUTURE DIRECTIONS        top

       None.

SEE ALSO        top

Section 2.5,Standard I/O Streams,fork(3p),pclose(3p),pipe(3p),sysconf(3p),system(3p),wait(3p),waitid(3p)       The Base Definitions volume of POSIX.1‐2017,stdio.h(0p)       The Shell and Utilities volume of POSIX.1‐2017,sh(1p)

COPYRIGHT        top

       Portions of this text are reprinted and reproduced in electronic       form from IEEE Std 1003.1-2017, Standard for Information       Technology -- Portable Operating System Interface (POSIX), The       Open Group Base Specifications Issue 7, 2018 Edition, Copyright       (C) 2018 by the Institute of Electrical and Electronics Engineers,       Inc and The Open Group.  In the event of any discrepancy between       this version and the original IEEE and The Open Group Standard,       the original IEEE and The Open Group Standard is the referee       document. The original Standard can be obtained online athttp://www.opengroup.org/unix/online.html .       Any typographical or formatting errors that appear in this page       are most likely to have been introduced during the conversion of       the source files to man page format. To report such errors, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group                2017POPEN(3P)

Pages that refer to this page:stdio.h(0p)awk(1p)pclose(3p)stdin(3p)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp