1--TEST--
2Test ftruncate() function : usage variations - truncate file to negative size
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// 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_variation4.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", 4);
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(): try truncating file to a negative size --\n";
54   /* try to truncate it to a negative size, size should not change*/
55
56   $new_size = -1000;
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 = actual size, no change
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(): try truncating file to a negative size --
78int(1024)
79int(0)
80
81Warning: ftruncate(): Negative size is not supported in %s on line %d
82bool(false)
83int(0)
84bool(false)
85int(1024)
86-- Testing ftruncate() with file opening using rb mode --
87-- Testing ftruncate(): try truncating file to a negative size --
88int(1024)
89int(0)
90
91Warning: ftruncate(): Negative size is not supported in %s on line %d
92bool(false)
93int(0)
94bool(false)
95int(1024)
96-- Testing ftruncate() with file opening using rt mode --
97-- Testing ftruncate(): try truncating file to a negative size --
98int(1024)
99int(0)
100
101Warning: ftruncate(): Negative size is not supported in %s on line %d
102bool(false)
103int(0)
104bool(false)
105int(1024)
106-- Testing ftruncate() with file opening using r+ mode --
107-- Testing ftruncate(): try truncating file to a negative size --
108int(1024)
109int(0)
110
111Warning: ftruncate(): Negative size is not supported in %s on line %d
112bool(false)
113int(0)
114bool(false)
115int(1024)
116-- Testing ftruncate() with file opening using r+b mode --
117-- Testing ftruncate(): try truncating file to a negative size --
118int(1024)
119int(0)
120
121Warning: ftruncate(): Negative size is not supported in %s on line %d
122bool(false)
123int(0)
124bool(false)
125int(1024)
126-- Testing ftruncate() with file opening using r+t mode --
127-- Testing ftruncate(): try truncating file to a negative size --
128int(1024)
129int(0)
130
131Warning: ftruncate(): Negative size is not supported in %s on line %d
132bool(false)
133int(0)
134bool(false)
135int(1024)
136-- Testing ftruncate() with file opening using w mode --
137-- Testing ftruncate(): try truncating file to a negative size --
138int(1024)
139int(0)
140
141Warning: ftruncate(): Negative size is not supported in %s on line %d
142bool(false)
143int(0)
144bool(false)
145int(1024)
146-- Testing ftruncate() with file opening using wb mode --
147-- Testing ftruncate(): try truncating file to a negative size --
148int(1024)
149int(0)
150
151Warning: ftruncate(): Negative size is not supported in %s on line %d
152bool(false)
153int(0)
154bool(false)
155int(1024)
156-- Testing ftruncate() with file opening using wt mode --
157-- Testing ftruncate(): try truncating file to a negative size --
158int(1024)
159int(0)
160
161Warning: ftruncate(): Negative size is not supported in %s on line %d
162bool(false)
163int(0)
164bool(false)
165int(1024)
166-- Testing ftruncate() with file opening using w+ mode --
167-- Testing ftruncate(): try truncating file to a negative size --
168int(1024)
169int(0)
170
171Warning: ftruncate(): Negative size is not supported in %s on line %d
172bool(false)
173int(0)
174bool(false)
175int(1024)
176-- Testing ftruncate() with file opening using w+b mode --
177-- Testing ftruncate(): try truncating file to a negative size --
178int(1024)
179int(0)
180
181Warning: ftruncate(): Negative size is not supported in %s on line %d
182bool(false)
183int(0)
184bool(false)
185int(1024)
186-- Testing ftruncate() with file opening using w+t mode --
187-- Testing ftruncate(): try truncating file to a negative size --
188int(1024)
189int(0)
190
191Warning: ftruncate(): Negative size is not supported in %s on line %d
192bool(false)
193int(0)
194bool(false)
195int(1024)
196-- Testing ftruncate() with file opening using x mode --
197-- Testing ftruncate(): try truncating file to a negative size --
198int(1024)
199int(0)
200
201Warning: ftruncate(): Negative size is not supported in %s on line %d
202bool(false)
203int(0)
204bool(false)
205int(1024)
206-- Testing ftruncate() with file opening using xb mode --
207-- Testing ftruncate(): try truncating file to a negative size --
208int(1024)
209int(0)
210
211Warning: ftruncate(): Negative size is not supported in %s on line %d
212bool(false)
213int(0)
214bool(false)
215int(1024)
216-- Testing ftruncate() with file opening using xt mode --
217-- Testing ftruncate(): try truncating file to a negative size --
218int(1024)
219int(0)
220
221Warning: ftruncate(): Negative size is not supported in %s on line %d
222bool(false)
223int(0)
224bool(false)
225int(1024)
226-- Testing ftruncate() with file opening using x+ mode --
227-- Testing ftruncate(): try truncating file to a negative size --
228int(1024)
229int(0)
230
231Warning: ftruncate(): Negative size is not supported in %s on line %d
232bool(false)
233int(0)
234bool(false)
235int(1024)
236-- Testing ftruncate() with file opening using x+b mode --
237-- Testing ftruncate(): try truncating file to a negative size --
238int(1024)
239int(0)
240
241Warning: ftruncate(): Negative size is not supported in %s on line %d
242bool(false)
243int(0)
244bool(false)
245int(1024)
246-- Testing ftruncate() with file opening using x+t mode --
247-- Testing ftruncate(): try truncating file to a negative size --
248int(1024)
249int(0)
250
251Warning: ftruncate(): Negative size is not supported in %s on line %d
252bool(false)
253int(0)
254bool(false)
255int(1024)
256-- Testing ftruncate() with file opening using a mode --
257-- Testing ftruncate(): try truncating file to a negative size --
258int(1024)
259int(0)
260
261Warning: ftruncate(): Negative size is not supported in %s on line %d
262bool(false)
263int(0)
264bool(false)
265int(1024)
266-- Testing ftruncate() with file opening using ab mode --
267-- Testing ftruncate(): try truncating file to a negative size --
268int(1024)
269int(0)
270
271Warning: ftruncate(): Negative size is not supported in %s on line %d
272bool(false)
273int(0)
274bool(false)
275int(1024)
276-- Testing ftruncate() with file opening using at mode --
277-- Testing ftruncate(): try truncating file to a negative size --
278int(1024)
279int(0)
280
281Warning: ftruncate(): Negative size is not supported in %s on line %d
282bool(false)
283int(0)
284bool(false)
285int(1024)
286-- Testing ftruncate() with file opening using a+ mode --
287-- Testing ftruncate(): try truncating file to a negative size --
288int(1024)
289int(0)
290
291Warning: ftruncate(): Negative size is not supported in %s on line %d
292bool(false)
293int(0)
294bool(false)
295int(1024)
296-- Testing ftruncate() with file opening using a+b mode --
297-- Testing ftruncate(): try truncating file to a negative size --
298int(1024)
299int(0)
300
301Warning: ftruncate(): Negative size is not supported in %s on line %d
302bool(false)
303int(0)
304bool(false)
305int(1024)
306-- Testing ftruncate() with file opening using a+t mode --
307-- Testing ftruncate(): try truncating file to a negative size --
308int(1024)
309int(0)
310
311Warning: ftruncate(): Negative size is not supported in %s on line %d
312bool(false)
313int(0)
314bool(false)
315int(1024)
316
317-- Testing ftruncate() with file having data of type text_with_new_line --
318-- Testing ftruncate() with file opening using r mode --
319-- Testing ftruncate(): try truncating file to a negative size --
320int(1024)
321int(0)
322
323Warning: ftruncate(): Negative size is not supported in %s on line %d
324bool(false)
325int(0)
326bool(false)
327int(1024)
328-- Testing ftruncate() with file opening using rb mode --
329-- Testing ftruncate(): try truncating file to a negative size --
330int(1024)
331int(0)
332
333Warning: ftruncate(): Negative size is not supported in %s on line %d
334bool(false)
335int(0)
336bool(false)
337int(1024)
338-- Testing ftruncate() with file opening using rt mode --
339-- Testing ftruncate(): try truncating file to a negative size --
340int(1024)
341int(0)
342
343Warning: ftruncate(): Negative size is not supported in %s on line %d
344bool(false)
345int(0)
346bool(false)
347int(1024)
348-- Testing ftruncate() with file opening using r+ mode --
349-- Testing ftruncate(): try truncating file to a negative size --
350int(1024)
351int(0)
352
353Warning: ftruncate(): Negative size is not supported in %s on line %d
354bool(false)
355int(0)
356bool(false)
357int(1024)
358-- Testing ftruncate() with file opening using r+b mode --
359-- Testing ftruncate(): try truncating file to a negative size --
360int(1024)
361int(0)
362
363Warning: ftruncate(): Negative size is not supported in %s on line %d
364bool(false)
365int(0)
366bool(false)
367int(1024)
368-- Testing ftruncate() with file opening using r+t mode --
369-- Testing ftruncate(): try truncating file to a negative size --
370int(1024)
371int(0)
372
373Warning: ftruncate(): Negative size is not supported in %s on line %d
374bool(false)
375int(0)
376bool(false)
377int(1024)
378-- Testing ftruncate() with file opening using w mode --
379-- Testing ftruncate(): try truncating file to a negative size --
380int(1024)
381int(0)
382
383Warning: ftruncate(): Negative size is not supported in %s on line %d
384bool(false)
385int(0)
386bool(false)
387int(1024)
388-- Testing ftruncate() with file opening using wb mode --
389-- Testing ftruncate(): try truncating file to a negative size --
390int(1024)
391int(0)
392
393Warning: ftruncate(): Negative size is not supported in %s on line %d
394bool(false)
395int(0)
396bool(false)
397int(1024)
398-- Testing ftruncate() with file opening using wt mode --
399-- Testing ftruncate(): try truncating file to a negative size --
400int(1137)
401int(0)
402
403Warning: ftruncate(): Negative size is not supported in %s on line %d
404bool(false)
405int(0)
406bool(false)
407int(1137)
408-- Testing ftruncate() with file opening using w+ mode --
409-- Testing ftruncate(): try truncating file to a negative size --
410int(1024)
411int(0)
412
413Warning: ftruncate(): Negative size is not supported in %s on line %d
414bool(false)
415int(0)
416bool(false)
417int(1024)
418-- Testing ftruncate() with file opening using w+b mode --
419-- Testing ftruncate(): try truncating file to a negative size --
420int(1024)
421int(0)
422
423Warning: ftruncate(): Negative size is not supported in %s on line %d
424bool(false)
425int(0)
426bool(false)
427int(1024)
428-- Testing ftruncate() with file opening using w+t mode --
429-- Testing ftruncate(): try truncating file to a negative size --
430int(1137)
431int(0)
432
433Warning: ftruncate(): Negative size is not supported in %s on line %d
434bool(false)
435int(0)
436bool(false)
437int(1137)
438-- Testing ftruncate() with file opening using x mode --
439-- Testing ftruncate(): try truncating file to a negative size --
440int(1024)
441int(0)
442
443Warning: ftruncate(): Negative size is not supported in %s on line %d
444bool(false)
445int(0)
446bool(false)
447int(1024)
448-- Testing ftruncate() with file opening using xb mode --
449-- Testing ftruncate(): try truncating file to a negative size --
450int(1024)
451int(0)
452
453Warning: ftruncate(): Negative size is not supported in %s on line %d
454bool(false)
455int(0)
456bool(false)
457int(1024)
458-- Testing ftruncate() with file opening using xt mode --
459-- Testing ftruncate(): try truncating file to a negative size --
460int(1137)
461int(0)
462
463Warning: ftruncate(): Negative size is not supported in %s on line %d
464bool(false)
465int(0)
466bool(false)
467int(1137)
468-- Testing ftruncate() with file opening using x+ mode --
469-- Testing ftruncate(): try truncating file to a negative size --
470int(1024)
471int(0)
472
473Warning: ftruncate(): Negative size is not supported in %s on line %d
474bool(false)
475int(0)
476bool(false)
477int(1024)
478-- Testing ftruncate() with file opening using x+b mode --
479-- Testing ftruncate(): try truncating file to a negative size --
480int(1024)
481int(0)
482
483Warning: ftruncate(): Negative size is not supported in %s on line %d
484bool(false)
485int(0)
486bool(false)
487int(1024)
488-- Testing ftruncate() with file opening using x+t mode --
489-- Testing ftruncate(): try truncating file to a negative size --
490int(1137)
491int(0)
492
493Warning: ftruncate(): Negative size is not supported in %s on line %d
494bool(false)
495int(0)
496bool(false)
497int(1137)
498-- Testing ftruncate() with file opening using a mode --
499-- Testing ftruncate(): try truncating file to a negative size --
500int(1024)
501int(0)
502
503Warning: ftruncate(): Negative size is not supported in %s on line %d
504bool(false)
505int(0)
506bool(false)
507int(1024)
508-- Testing ftruncate() with file opening using ab mode --
509-- Testing ftruncate(): try truncating file to a negative size --
510int(1024)
511int(0)
512
513Warning: ftruncate(): Negative size is not supported in %s on line %d
514bool(false)
515int(0)
516bool(false)
517int(1024)
518-- Testing ftruncate() with file opening using at mode --
519-- Testing ftruncate(): try truncating file to a negative size --
520int(1024)
521int(0)
522
523Warning: ftruncate(): Negative size is not supported in %s on line %d
524bool(false)
525int(0)
526bool(false)
527int(1024)
528-- Testing ftruncate() with file opening using a+ mode --
529-- Testing ftruncate(): try truncating file to a negative size --
530int(1024)
531int(0)
532
533Warning: ftruncate(): Negative size is not supported in %s on line %d
534bool(false)
535int(0)
536bool(false)
537int(1024)
538-- Testing ftruncate() with file opening using a+b mode --
539-- Testing ftruncate(): try truncating file to a negative size --
540int(1024)
541int(0)
542
543Warning: ftruncate(): Negative size is not supported in %s on line %d
544bool(false)
545int(0)
546bool(false)
547int(1024)
548-- Testing ftruncate() with file opening using a+t mode --
549-- Testing ftruncate(): try truncating file to a negative size --
550int(1024)
551int(0)
552
553Warning: ftruncate(): Negative size is not supported in %s on line %d
554bool(false)
555int(0)
556bool(false)
557int(1024)
558Done
559