1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5 and 7                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2004 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.0 of the PHP license,       |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_0.txt.                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Michael Spector <michael@php.net>                            |
16   +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 #include "php_expect.h"
23 #include "php_streams.h"
24 #include <sys/wait.h>
25 
26 #if PHP_MAJOR_VERSION >= 8
27 #define TSRMLS_DC
28 #endif
29 
30 #if PHP_MAJOR_VERSION >= 7
php_expect_stream_open(php_stream_wrapper * wrapper,const char * command,const char * mode,int options,zend_string ** opened_command,php_stream_context * context STREAMS_DC TSRMLS_DC)31 php_stream *php_expect_stream_open (php_stream_wrapper *wrapper, const char *command, const char *mode, int options,
32                            zend_string **opened_command, php_stream_context *context STREAMS_DC TSRMLS_DC)
33 #else
34 php_stream *php_expect_stream_open (php_stream_wrapper *wrapper, char *command, char *mode, int options,
35 							  char **opened_command, php_stream_context *context STREAMS_DC TSRMLS_DC)
36 #endif
37 {
38 	FILE *fp;
39 	if (strncasecmp("expect://", command, sizeof("expect://")-1) == 0) {
40 		command += sizeof("expect://")-1;
41 	}
42 
43 #if PHP_MAJOR_VERSION >= 7
44     if ((fp = exp_popen((char*)command)) != NULL) {
45 #else
46 	if ((fp = exp_popen(command)) != NULL) {
47 #endif
48 		php_stream* stream = php_stream_fopen_from_pipe (fp, mode);
49 #if PHP_MAJOR_VERSION >= 7
50         zval z_pid;
51         ZVAL_LONG (&z_pid, exp_pid);
52 #else
53 		zval *z_pid;
54 		MAKE_STD_ZVAL (z_pid);
55 		ZVAL_LONG (z_pid, exp_pid);
56 #endif
57 		stream->wrapperdata = z_pid;
58 		return stream;
59 	}
60 
61 	return NULL;
62 }
63 
64 static int php_expect_stream_close (php_stream_wrapper *wrapper, php_stream *stream TSRMLS_DC)
65 {
66 #if PHP_MAJOR_VERSION >= 7
67     zval* z_pid = &(stream->wrapperdata);
68 #else
69 	zval* z_pid = stream->wrapperdata;
70 #endif
71 	int s = 0;
72 	waitpid (Z_LVAL_P(z_pid), &s, 0);
73 #if PHP_MAJOR_VERSION >= 7
74     zval_ptr_dtor (z_pid);
75     php_stream_free(stream, PHP_STREAM_FREE_CLOSE);
76 #else
77 	zval_ptr_dtor (&z_pid);
78 	stream->wrapperdata = NULL;
79 #endif
80 	return s;
81 }
82 /* }}} */
83 
84 
85 static php_stream_wrapper_ops php_expect_wrapper_ops = {
86 	php_expect_stream_open,
87 	php_expect_stream_close, /* close */
88 	NULL, /* stat */
89 	NULL, /* stat_url */
90 	NULL, /* opendir */
91 	"expect",
92 	NULL, /* unlink */
93 	NULL, /* rename */
94 	NULL, /* mkdir */
95 	NULL  /* rmdir */
96 };
97 
98 php_stream_wrapper php_expect_wrapper = {
99 	&php_expect_wrapper_ops,
100 	NULL,
101 	0, /* is_url */
102 };
103 
104 /*
105  * Local variables:
106  * tab-width: 4
107  * c-basic-offset: 4
108  * End:
109  * vim600: noet sw=4 ts=4 fdm=marker
110  * vim<600: noet sw=4 ts=4
111  */
112