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