Rev | Author | # | Line |
---|---|---|---|
1 | perry | 1 | !!NAME |
2 | popen, pclose - process I/O | ||
3 | |||
4 | CraigBox | 4 | !!SYNOPSIS |
1 | perry | 5 | |
3 | SamJansen | 6 | __#include <stdio.h>__ |
1 | perry | 7 | |
3 | SamJansen | 8 | __FILE *popen(const char *__''command''__, const char*__''type''__);__ |
9 | __int pclose(FILE *__''stream''__);__ | ||
1 | perry | 10 | |
11 | !!DESCRIPTION | ||
12 | |||
13 | The __popen()__ function opens a process by creating a | ||
14 | pipe, forking, and invoking the shell. Since a pipe is by | ||
15 | definition unidirectional, the ''type'' argument may | ||
16 | specify only reading or writing, not both; the resulting | ||
17 | stream is correspondingly read-only or | ||
18 | write-only. | ||
19 | |||
20 | |||
21 | The ''command'' argument is a pointer to a | ||
22 | null-terminated string containing a shell command line. This | ||
23 | command is passed to ''/bin/sh'' using the __-c__ | ||
24 | flag; interpretation, if any, is performed by the shell. The | ||
25 | ''mode'' argument is a pointer to a null-terminated | ||
26 | string which must be either `r' for reading or `w' for | ||
27 | writing. | ||
28 | |||
29 | |||
30 | The return value from __popen()__ is a normal standard | ||
31 | I/O stream in all respects save that it must be closed with | ||
32 | __pclose()__ rather than __fclose()__. Writing to such | ||
33 | a stream writes to the standard input of the command; the | ||
34 | command's standard output is the same as that of the process | ||
35 | that called __popen()__, unless this is altered by the | ||
36 | command itself. Conversely, reading from a ``popened'' | ||
37 | stream reads the command's standard output, and the | ||
38 | command's standard input is the same as that of the process | ||
39 | that called __popen__. | ||
40 | |||
41 | |||
42 | Note that output __popen__ streams are fully buffered by | ||
43 | default. | ||
44 | |||
45 | |||
46 | The __pclose__ function waits for the associated process | ||
47 | to terminate and returns the exit status of the command as | ||
48 | returned by __wait4__. | ||
4 | CraigBox | 49 | |
1 | perry | 50 | !!RETURN VALUE |
51 | |||
52 | The __popen__ function returns __NULL__ if the | ||
53 | fork(2) or pipe(2) calls fail, or if it cannot | ||
54 | allocate memory. | ||
55 | |||
56 | The __pclose__ function returns -1 if __wait4__ | ||
57 | returns an error, or some other error is | ||
58 | detected. | ||
4 | CraigBox | 59 | |
1 | perry | 60 | !!ERRORS |
61 | |||
62 | The __popen__ function does not set ''errno'' if | ||
63 | memory allocation fails. If the underlying __fork()__ or | ||
64 | __pipe()__ fails, ''errno'' is set appropriately. If | ||
65 | the ''mode'' argument is invalid, and this condition is | ||
66 | detected, ''errno'' is set to [EINVAL]. | ||
67 | |||
4 | CraigBox | 68 | If __pclose()__ cannot obtain the child status, ''errno'' is set to [ECHILD]. |
1 | perry | 69 | |
70 | !!CONFORMING TO | ||
71 | |||
4 | CraigBox | 72 | POSIX.2 |
1 | perry | 73 | |
74 | !!BUGS | ||
75 | |||
4 | CraigBox | 76 | Since the standard input of a command opened for reading shares its seek offset with the process that called __popen()__, if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before __popen__. |
1 | perry | 77 | |
4 | CraigBox | 78 | Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. |
1 | perry | 79 | |
80 | !!HISTORY | ||
81 | |||
4 | CraigBox | 82 | A __popen()__ and a __pclose()__ function appeared in Version 7 AT&T Unix. |
1 | perry | 83 | |
84 | !!SEE ALSO | ||
85 | |||
4 | CraigBox | 86 | fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3) |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 12 times)