1--TEST--
2Test ftruncate() function : usage variations - truncate when file pointer at EOF
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) != 'WIN') {
6    die('skip.. only valid for Windows');
7}
8?>
9--FILE--
10<?php
11/* truncate the file when file pointer is positioned at end of the file */
12// include common file related test functions
13include ("file.inc");
14
15echo "*** Testing ftruncate() : usage variations ***\n";
16
17/* test ftruncate with file opened in different modes */
18$file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
19                    "w", "wb", "wt", "w+", "w+b", "w+t",
20                    "x", "xb", "xt", "x+", "x+b", "x+t",
21                    "a", "ab", "at", "a+", "a+b", "a+t");
22
23$file_content_types = array("numeric","text_with_new_line");
24
25foreach($file_content_types as $file_content_type) {
26 echo "\n-- Testing ftruncate() with file having data of type ". $file_content_type ." --\n";
27
28 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
29  echo "-- Testing ftruncate() with file opening using $file_modes[$mode_counter] mode --\n";
30
31   // create 1 file with some contents
32   $filename = __DIR__."/ftruncate_variation7.tmp";
33   if( strstr($file_modes[$mode_counter], "x") || strstr($file_modes[$mode_counter], "w") ) {
34     // fopen the file using the $file_modes
35     $file_handle = fopen($filename, $file_modes[$mode_counter]);
36     fill_file($file_handle, $file_content_type, 1024);
37   } else {
38     create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "ftruncate_variation", 7);
39     // fopen the file using the $file_modes
40     $file_handle = fopen($filename, $file_modes[$mode_counter]);
41   }
42   if (!$file_handle) {
43     echo "Error: failed to open file $filename!\n";
44     exit();
45   }
46
47   rewind($file_handle); // file pointer to 0
48
49   echo "-- Testing ftruncate(): File pointer at the end --\n";
50   /* try to truncate it to while file pointer at the end */
51   fseek($file_handle, 0, SEEK_END);
52   $new_size = 200;
53   var_dump( filesize($filename) );  // current filesize
54   var_dump( ftell($file_handle) );
55   var_dump( ftruncate($file_handle, $new_size) ); // truncate it
56   var_dump( ftell($file_handle) );
57   var_dump( feof($file_handle) );
58   fclose($file_handle);
59   clearstatcache(); // clear previous size value in cache
60   var_dump( filesize($filename) );
61
62   //delete all files created
63   delete_file($filename);
64 }//end of inner for loop
65}//end of outer foreach loop
66echo "Done\n";
67?>
68--EXPECT--
69*** Testing ftruncate() : usage variations ***
70
71-- Testing ftruncate() with file having data of type numeric --
72-- Testing ftruncate() with file opening using r mode --
73-- Testing ftruncate(): File pointer at the end --
74int(1024)
75int(1024)
76bool(false)
77int(1024)
78bool(false)
79int(1024)
80-- Testing ftruncate() with file opening using rb mode --
81-- Testing ftruncate(): File pointer at the end --
82int(1024)
83int(1024)
84bool(false)
85int(1024)
86bool(false)
87int(1024)
88-- Testing ftruncate() with file opening using rt mode --
89-- Testing ftruncate(): File pointer at the end --
90int(1024)
91int(1024)
92bool(false)
93int(1024)
94bool(false)
95int(1024)
96-- Testing ftruncate() with file opening using r+ mode --
97-- Testing ftruncate(): File pointer at the end --
98int(1024)
99int(1024)
100bool(true)
101int(1024)
102bool(false)
103int(200)
104-- Testing ftruncate() with file opening using r+b mode --
105-- Testing ftruncate(): File pointer at the end --
106int(1024)
107int(1024)
108bool(true)
109int(1024)
110bool(false)
111int(200)
112-- Testing ftruncate() with file opening using r+t mode --
113-- Testing ftruncate(): File pointer at the end --
114int(1024)
115int(1024)
116bool(true)
117int(1024)
118bool(false)
119int(200)
120-- Testing ftruncate() with file opening using w mode --
121-- Testing ftruncate(): File pointer at the end --
122int(1024)
123int(1024)
124bool(true)
125int(1024)
126bool(false)
127int(200)
128-- Testing ftruncate() with file opening using wb mode --
129-- Testing ftruncate(): File pointer at the end --
130int(1024)
131int(1024)
132bool(true)
133int(1024)
134bool(false)
135int(200)
136-- Testing ftruncate() with file opening using wt mode --
137-- Testing ftruncate(): File pointer at the end --
138int(1024)
139int(1024)
140bool(true)
141int(1024)
142bool(false)
143int(200)
144-- Testing ftruncate() with file opening using w+ mode --
145-- Testing ftruncate(): File pointer at the end --
146int(1024)
147int(1024)
148bool(true)
149int(1024)
150bool(false)
151int(200)
152-- Testing ftruncate() with file opening using w+b mode --
153-- Testing ftruncate(): File pointer at the end --
154int(1024)
155int(1024)
156bool(true)
157int(1024)
158bool(false)
159int(200)
160-- Testing ftruncate() with file opening using w+t mode --
161-- Testing ftruncate(): File pointer at the end --
162int(1024)
163int(1024)
164bool(true)
165int(1024)
166bool(false)
167int(200)
168-- Testing ftruncate() with file opening using x mode --
169-- Testing ftruncate(): File pointer at the end --
170int(1024)
171int(1024)
172bool(true)
173int(1024)
174bool(false)
175int(200)
176-- Testing ftruncate() with file opening using xb mode --
177-- Testing ftruncate(): File pointer at the end --
178int(1024)
179int(1024)
180bool(true)
181int(1024)
182bool(false)
183int(200)
184-- Testing ftruncate() with file opening using xt mode --
185-- Testing ftruncate(): File pointer at the end --
186int(1024)
187int(1024)
188bool(true)
189int(1024)
190bool(false)
191int(200)
192-- Testing ftruncate() with file opening using x+ mode --
193-- Testing ftruncate(): File pointer at the end --
194int(1024)
195int(1024)
196bool(true)
197int(1024)
198bool(false)
199int(200)
200-- Testing ftruncate() with file opening using x+b mode --
201-- Testing ftruncate(): File pointer at the end --
202int(1024)
203int(1024)
204bool(true)
205int(1024)
206bool(false)
207int(200)
208-- Testing ftruncate() with file opening using x+t mode --
209-- Testing ftruncate(): File pointer at the end --
210int(1024)
211int(1024)
212bool(true)
213int(1024)
214bool(false)
215int(200)
216-- Testing ftruncate() with file opening using a mode --
217-- Testing ftruncate(): File pointer at the end --
218int(1024)
219int(1024)
220bool(true)
221int(1024)
222bool(false)
223int(200)
224-- Testing ftruncate() with file opening using ab mode --
225-- Testing ftruncate(): File pointer at the end --
226int(1024)
227int(1024)
228bool(true)
229int(1024)
230bool(false)
231int(200)
232-- Testing ftruncate() with file opening using at mode --
233-- Testing ftruncate(): File pointer at the end --
234int(1024)
235int(1024)
236bool(true)
237int(1024)
238bool(false)
239int(200)
240-- Testing ftruncate() with file opening using a+ mode --
241-- Testing ftruncate(): File pointer at the end --
242int(1024)
243int(1024)
244bool(true)
245int(1024)
246bool(false)
247int(200)
248-- Testing ftruncate() with file opening using a+b mode --
249-- Testing ftruncate(): File pointer at the end --
250int(1024)
251int(1024)
252bool(true)
253int(1024)
254bool(false)
255int(200)
256-- Testing ftruncate() with file opening using a+t mode --
257-- Testing ftruncate(): File pointer at the end --
258int(1024)
259int(1024)
260bool(true)
261int(1024)
262bool(false)
263int(200)
264
265-- Testing ftruncate() with file having data of type text_with_new_line --
266-- Testing ftruncate() with file opening using r mode --
267-- Testing ftruncate(): File pointer at the end --
268int(1024)
269int(1024)
270bool(false)
271int(1024)
272bool(false)
273int(1024)
274-- Testing ftruncate() with file opening using rb mode --
275-- Testing ftruncate(): File pointer at the end --
276int(1024)
277int(1024)
278bool(false)
279int(1024)
280bool(false)
281int(1024)
282-- Testing ftruncate() with file opening using rt mode --
283-- Testing ftruncate(): File pointer at the end --
284int(1024)
285int(1024)
286bool(false)
287int(1024)
288bool(false)
289int(1024)
290-- Testing ftruncate() with file opening using r+ mode --
291-- Testing ftruncate(): File pointer at the end --
292int(1024)
293int(1024)
294bool(true)
295int(1024)
296bool(false)
297int(200)
298-- Testing ftruncate() with file opening using r+b mode --
299-- Testing ftruncate(): File pointer at the end --
300int(1024)
301int(1024)
302bool(true)
303int(1024)
304bool(false)
305int(200)
306-- Testing ftruncate() with file opening using r+t mode --
307-- Testing ftruncate(): File pointer at the end --
308int(1024)
309int(1024)
310bool(true)
311int(1024)
312bool(false)
313int(200)
314-- Testing ftruncate() with file opening using w mode --
315-- Testing ftruncate(): File pointer at the end --
316int(1024)
317int(1024)
318bool(true)
319int(1024)
320bool(false)
321int(200)
322-- Testing ftruncate() with file opening using wb mode --
323-- Testing ftruncate(): File pointer at the end --
324int(1024)
325int(1024)
326bool(true)
327int(1024)
328bool(false)
329int(200)
330-- Testing ftruncate() with file opening using wt mode --
331-- Testing ftruncate(): File pointer at the end --
332int(1137)
333int(1137)
334bool(true)
335int(1137)
336bool(false)
337int(200)
338-- Testing ftruncate() with file opening using w+ mode --
339-- Testing ftruncate(): File pointer at the end --
340int(1024)
341int(1024)
342bool(true)
343int(1024)
344bool(false)
345int(200)
346-- Testing ftruncate() with file opening using w+b mode --
347-- Testing ftruncate(): File pointer at the end --
348int(1024)
349int(1024)
350bool(true)
351int(1024)
352bool(false)
353int(200)
354-- Testing ftruncate() with file opening using w+t mode --
355-- Testing ftruncate(): File pointer at the end --
356int(1137)
357int(1137)
358bool(true)
359int(1137)
360bool(false)
361int(200)
362-- Testing ftruncate() with file opening using x mode --
363-- Testing ftruncate(): File pointer at the end --
364int(1024)
365int(1024)
366bool(true)
367int(1024)
368bool(false)
369int(200)
370-- Testing ftruncate() with file opening using xb mode --
371-- Testing ftruncate(): File pointer at the end --
372int(1024)
373int(1024)
374bool(true)
375int(1024)
376bool(false)
377int(200)
378-- Testing ftruncate() with file opening using xt mode --
379-- Testing ftruncate(): File pointer at the end --
380int(1137)
381int(1137)
382bool(true)
383int(1137)
384bool(false)
385int(200)
386-- Testing ftruncate() with file opening using x+ mode --
387-- Testing ftruncate(): File pointer at the end --
388int(1024)
389int(1024)
390bool(true)
391int(1024)
392bool(false)
393int(200)
394-- Testing ftruncate() with file opening using x+b mode --
395-- Testing ftruncate(): File pointer at the end --
396int(1024)
397int(1024)
398bool(true)
399int(1024)
400bool(false)
401int(200)
402-- Testing ftruncate() with file opening using x+t mode --
403-- Testing ftruncate(): File pointer at the end --
404int(1137)
405int(1137)
406bool(true)
407int(1137)
408bool(false)
409int(200)
410-- Testing ftruncate() with file opening using a mode --
411-- Testing ftruncate(): File pointer at the end --
412int(1024)
413int(1024)
414bool(true)
415int(1024)
416bool(false)
417int(200)
418-- Testing ftruncate() with file opening using ab mode --
419-- Testing ftruncate(): File pointer at the end --
420int(1024)
421int(1024)
422bool(true)
423int(1024)
424bool(false)
425int(200)
426-- Testing ftruncate() with file opening using at mode --
427-- Testing ftruncate(): File pointer at the end --
428int(1024)
429int(1024)
430bool(true)
431int(1024)
432bool(false)
433int(200)
434-- Testing ftruncate() with file opening using a+ mode --
435-- Testing ftruncate(): File pointer at the end --
436int(1024)
437int(1024)
438bool(true)
439int(1024)
440bool(false)
441int(200)
442-- Testing ftruncate() with file opening using a+b mode --
443-- Testing ftruncate(): File pointer at the end --
444int(1024)
445int(1024)
446bool(true)
447int(1024)
448bool(false)
449int(200)
450-- Testing ftruncate() with file opening using a+t mode --
451-- Testing ftruncate(): File pointer at the end --
452int(1024)
453int(1024)
454bool(true)
455int(1024)
456bool(false)
457int(200)
458Done
459