1--TEST--
2Test popen() and pclose function: basic functionality
3--SKIPIF--
4<?php
5if(substr(PHP_OS, 0, 3) == 'WIN' )
6  die("skip Not Valid for Windows");
7?>
8--FILE--
9<?php
10$file_path = __DIR__;
11require($file_path."/file.inc");
12
13echo "*** Testing popen() and pclose() with different processes ***\n";
14
15echo "-- Testing popen(): reading from the pipe --\n";
16$dirpath = $file_path."/popen_basic";
17mkdir($dirpath);
18touch($dirpath."/popen_basic.tmp");
19define('CMD', "ls $dirpath");
20$file_handle = popen(CMD, 'r');
21fpassthru($file_handle);
22pclose($file_handle);
23
24echo "-- Testing popen(): reading from a file using 'cat' command --\n";
25create_files($dirpath, 1, "text_with_new_line", 0755, 100, "w", "popen_basic", 1, "bytes");
26$filename = $dirpath."/popen_basic1.tmp";
27$command = "cat $filename";
28$file_handle = popen($command, "r");
29$return_value =  fpassthru($file_handle);
30echo "\n";
31var_dump($return_value);
32pclose($file_handle);
33delete_files($dirpath, 1);
34
35echo "*** Testing popen(): writing to the pipe ***\n";
36$arr = array("ggg", "ddd", "aaa", "sss");
37$file_handle = popen("sort", "w");
38$counter = 0;
39$newline = "\n";
40foreach($arr as $str) {
41  fwrite($file_handle, $str);
42  fwrite($file_handle, $newline);
43}
44pclose($file_handle);
45
46
47echo "*** Testing for return type of popen() and pclose() functions ***\n";
48$string = "Test String";
49$return_value_popen = popen("echo $string", "r");
50var_dump( is_resource($return_value_popen) );
51fpassthru($return_value_popen);
52$return_value_pclose = pclose($return_value_popen);
53var_dump( is_int($return_value_pclose) );
54
55echo "\n--- Done ---";
56?>
57--CLEAN--
58<?php
59$file_path = __DIR__;
60$dirpath = $file_path."/popen_basic";
61unlink($dirpath."/popen_basic.tmp");
62unlink($dirpath."/popen_basic1.tmp");
63rmdir($dirpath);
64?>
65--EXPECT--
66*** Testing popen() and pclose() with different processes ***
67-- Testing popen(): reading from the pipe --
68popen_basic.tmp
69-- Testing popen(): reading from a file using 'cat' command --
70line
71line of text
72line
73line of text
74line
75line of text
76line
77line of text
78line
79line of text
80line
81line
82int(100)
83*** Testing popen(): writing to the pipe ***
84aaa
85ddd
86ggg
87sss
88*** Testing for return type of popen() and pclose() functions ***
89bool(true)
90Test String
91bool(true)
92
93--- Done ---
94