1--TEST--
2Test fgetc() function : usage variations - write only modes (Bug #42036)
3--FILE--
4<?php
5/*
6 Prototype: string fgetc ( resource $handle );
7 Description: Gets character from file pointer
8*/
9
10/* try fgetc on files which are opened in non readable modes
11    w, wb, wt,
12    a, ab, at,
13    x, xb, xt
14*/
15// include the header for common test function
16include ("file.inc");
17
18echo "*** Testing fgetc() with file opened in write only mode ***\n";
19
20$file_modes = array("w", "wb", "wt", "a", "ab", "at", "x", "xb", "xt");
21$filename = __DIR__."/fgetc_variation3.tmp";
22foreach ($file_modes as $file_mode ) {
23  echo "-- File opened in mode : $file_mode --\n";
24
25  $file_handle = fopen($filename, $file_mode);
26  if(!$file_handle) {
27    echo "Error: failed to open file $filename!\n";
28    exit();
29  }
30  $data = "fgetc_variation test";
31  fwrite($file_handle, $data);
32
33  // rewind the file pointer to beginning of the file
34  var_dump( rewind($file_handle) );
35  var_dump( ftell($file_handle) );
36  var_dump( feof($file_handle) );
37
38  // read from file
39  var_dump( fgetc($file_handle) ); // expected : no chars should be read
40  var_dump( ftell($file_handle) ); // ensure that file pointer position is not changed
41  var_dump( feof($file_handle) ); // check if end of file pointer is set
42
43  // close the file
44  fclose($file_handle);
45
46  // delete the file
47  unlink($filename);
48}
49
50echo "Done\n";
51?>
52--EXPECTF--
53*** Testing fgetc() with file opened in write only mode ***
54-- File opened in mode : w --
55bool(true)
56int(0)
57bool(false)
58
59Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
60bool(false)
61int(0)
62bool(false)
63-- File opened in mode : wb --
64bool(true)
65int(0)
66bool(false)
67
68Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
69bool(false)
70int(0)
71bool(false)
72-- File opened in mode : wt --
73bool(true)
74int(0)
75bool(false)
76
77Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
78bool(false)
79int(0)
80bool(false)
81-- File opened in mode : a --
82bool(true)
83int(0)
84bool(false)
85
86Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
87bool(false)
88int(0)
89bool(false)
90-- File opened in mode : ab --
91bool(true)
92int(0)
93bool(false)
94
95Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
96bool(false)
97int(0)
98bool(false)
99-- File opened in mode : at --
100bool(true)
101int(0)
102bool(false)
103
104Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
105bool(false)
106int(0)
107bool(false)
108-- File opened in mode : x --
109bool(true)
110int(0)
111bool(false)
112
113Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
114bool(false)
115int(0)
116bool(false)
117-- File opened in mode : xb --
118bool(true)
119int(0)
120bool(false)
121
122Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
123bool(false)
124int(0)
125bool(false)
126-- File opened in mode : xt --
127bool(true)
128int(0)
129bool(false)
130
131Notice: fgetc(): read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
132bool(false)
133int(0)
134bool(false)
135Done
136