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 " . escapeshellarg($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$filename_escaped = escapeshellarg($filename);
28$command = "cat $filename_escaped";
29$file_handle = popen($command, "r");
30$return_value =  fpassthru($file_handle);
31echo "\n";
32var_dump($return_value);
33pclose($file_handle);
34delete_files($dirpath, 1);
35
36echo "*** Testing popen(): writing to the pipe ***\n";
37$arr = array("ggg", "ddd", "aaa", "sss");
38$file_handle = popen("sort", "w");
39$counter = 0;
40$newline = "\n";
41foreach($arr as $str) {
42  fwrite($file_handle, $str);
43  fwrite($file_handle, $newline);
44}
45pclose($file_handle);
46
47
48echo "*** Testing for return type of popen() and pclose() functions ***\n";
49$string = "Test String";
50$return_value_popen = popen("echo $string", "r");
51var_dump( is_resource($return_value_popen) );
52fpassthru($return_value_popen);
53$return_value_pclose = pclose($return_value_popen);
54var_dump( is_int($return_value_pclose) );
55
56echo "\n--- Done ---";
57?>
58--CLEAN--
59<?php
60$file_path = __DIR__;
61$dirpath = $file_path."/popen_basic";
62unlink($dirpath."/popen_basic.tmp");
63unlink($dirpath."/popen_basic1.tmp");
64rmdir($dirpath);
65?>
66--EXPECT--
67*** Testing popen() and pclose() with different processes ***
68-- Testing popen(): reading from the pipe --
69popen_basic.tmp
70-- Testing popen(): reading from a file using 'cat' command --
71line
72line of text
73line
74line of text
75line
76line of text
77line
78line of text
79line
80line of text
81line
82line
83int(100)
84*** Testing popen(): writing to the pipe ***
85aaa
86ddd
87ggg
88sss
89*** Testing for return type of popen() and pclose() functions ***
90bool(true)
91Test String
92bool(true)
93
94--- Done ---
95