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