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/*
11 *  Prototype: resource popen ( string command, string mode )
12 *  Description: Opens process file pointer.
13 *
14 *  Prototype: int pclose ( resource handle );
15 *  Description: Closes process file pointer.
16 */
17
18$file_path = dirname(__FILE__);
19require($file_path."/file.inc");
20
21echo "*** Testing popen() and pclose() with different processes ***\n";
22
23echo "-- Testing popen(): reading from the pipe --\n";
24$dirpath = $file_path."/popen_basic";
25mkdir($dirpath);
26touch($dirpath."/popen_basic.tmp");
27define('CMD', "ls $dirpath");
28$file_handle = popen(CMD, 'r');
29fpassthru($file_handle);
30pclose($file_handle);
31
32echo "-- Testing popen(): reading from a file using 'cat' command --\n";
33create_files($dirpath, 1, "text_with_new_line", 0755, 100, "w", "popen_basic", 1, "bytes");
34$filename = $dirpath."/popen_basic1.tmp";
35$command = "cat $filename";
36$file_handle = popen($command, "r");
37$return_value =  fpassthru($file_handle);
38echo "\n";
39var_dump($return_value);
40pclose($file_handle);
41delete_files($dirpath, 1);
42
43echo "*** Testing popen(): writing to the pipe ***\n";
44$arr = array("ggg", "ddd", "aaa", "sss");
45$file_handle = popen("sort", "w");
46$counter = 0;
47$newline = "\n";
48foreach($arr as $str) {
49  fwrite($file_handle, $str);
50  fwrite($file_handle, $newline);
51}
52pclose($file_handle);
53
54
55echo "*** Testing for return type of popen() and pclose() functions ***\n";
56$string = "Test String";
57$return_value_popen = popen("echo $string", "r");
58var_dump( is_resource($return_value_popen) );
59fpassthru($return_value_popen);
60$return_value_pclose = pclose($return_value_popen);
61var_dump( is_int($return_value_pclose) );
62
63echo "\n--- Done ---";
64?>
65--CLEAN--
66<?php
67$file_path = dirname(__FILE__);
68$dirpath = $file_path."/popen_basic";
69unlink($dirpath."/popen_basic.tmp");
70unlink($dirpath."/popen_basic1.tmp");
71rmdir($dirpath);
72?>
73--EXPECTF--
74*** Testing popen() and pclose() with different processes ***
75-- Testing popen(): reading from the pipe --
76popen_basic.tmp
77-- Testing popen(): reading from a file using 'cat' command --
78line
79line of text
80line
81line of text
82line
83line of text
84line
85line of text
86line
87line of text
88line
89line
90int(100)
91*** Testing popen(): writing to the pipe ***
92aaa
93ddd
94ggg
95sss
96*** Testing for return type of popen() and pclose() functions ***
97bool(true)
98Test String
99bool(true)
100
101--- Done ---
102