1111#include <sys/stat.h>
1212#include <unistd.h>
1313
14- int main ()
15- {
16- // Reading
17-
14+ // Reading
15+ void test_reading () {
1816 FILE * file = fopen ("somefile.binary" , "rb" );
1917 assert (file );
2018
@@ -38,28 +36,29 @@ int main()
3836 free (buffer );
3937
4038 // Do it again, with a loop on feof
41-
4239 printf ("loop: " );
4340 file = fopen ("somefile.binary" , "rb" );
4441 assert (file );
4542 while (!feof (file )) {
4643 char c = fgetc (file );
4744 if (c != EOF ) printf ("%d " , c );
4845 }
49- fclose (file);
46+ fclose (file );
5047 printf ("\n" );
48+ }
5149
52- // Standard streams
53-
50+ // Standard streams
51+ void test_stdstreams () {
5452 char gets_buffer [1024 ];
55- printf (" input:%s\n " , gets (gets_buffer));
53+ printf ("input:%s\n" , fgets (gets_buffer , 1024 , stdin ));
5654 fwrite ("texto\n" , 1 , 6 , stdout );
5755 fwrite ("texte\n" , 1 , 6 , stderr );
5856 putchar ('$' );
5957 putc ('\n' , stdout );
58+ }
6059
61- // Writing
62-
60+ // Writing
61+ void test_writing () {
6362 char data [5 ] = { 10 , 30 , 20 , 11 , 88 };
6463 FILE * outf = fopen ("go.out" , "wb" );
6564 fwrite (data , 1 , 5 , outf );
@@ -73,14 +72,16 @@ int main()
7372 int num = fread (data2 , 1 , 10 , inf );
7473 fclose (inf );
7574 printf ("%d : %d,%d,%d,%d,%d\n" , num , data2 [0 ], data2 [1 ], data2 [2 ], data2 [3 ], data2 [4 ]);
75+ }
7676
77+ void test_seeking () {
7778 // Test reading a file that has not been cached
78-
79+
7980 FILE * other = fopen ("test.file" , "r" );
8081 assert (other );
8182
8283 char otherData [1000 ];
83- num = fread (otherData, 1 , 9 , other);
84+ int num = fread (otherData , 1 , 9 , other );
8485 otherData [num ] = 0 ;
8586 printf ("other=%s.\n" , otherData );
8687
@@ -102,21 +103,24 @@ int main()
102103 printf ("seeked=%s.\n" , otherData );
103104
104105 fclose (other );
106+ }
105107
106- // fscanf
107-
108- outf = fopen (" fscan.f" , " w" );
108+ // fscanf
109+ void test_fscanf () {
110+ FILE * outf = fopen ("fscan.f" , "w" );
109111 fprintf (outf , "10 hello" );
110112 fclose (outf );
111113
112114 int number ;
113115 char text [100 ];
114- inf = fopen (" fscan.f" , " r" );
115- num = fscanf (inf, " %d %s" , &number, text);
116+ FILE * inf = fopen ("fscan.f" , "r" );
117+ int num = fscanf (inf , "%d %s" , & number , text );
116118 fclose (inf );
117119 printf ("fscanfed: %d - %s\n" , number , text );
120+ }
118121
119- // temp files
122+ // temp files
123+ void test_tempfiles () {
120124 const char * tname = "file_XXXXXX" ;
121125 char tname1 [100 ];
122126 char tname2 [100 ];
@@ -142,20 +146,28 @@ int main()
142146 assert (strncmp ("/tmp/" , str , 5 ) == 0 );
143147 }
144148
149+ char data [5 ] = { 10 , 30 , 20 , 11 , 88 };
145150 FILE * n = fopen ("/dev/null" , "w" );
146151 printf ("5 bytes to dev/null: %zu\n" , fwrite (data , 1 , 5 , n ));
147152 fclose (n );
148-
153+
149154 // Test file creation with O_TRUNC (regression test for #16784)
150155 const char * file_name = "test.out" ;
151156 int fd = open (file_name , O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR );
152157 assert (fd >= 0 );
153158 int status = write (fd , "blablabla\n" , 10 );
154159 assert (status == 10 );
155160 close (fd );
161+ }
156162
163+ int main () {
164+ test_reading ();
165+ test_stdstreams ();
166+ test_writing ();
167+ test_seeking ();
168+ test_fscanf ();
169+ test_tempfiles ();
157170 printf ("ok.\n" );
158-
159171 return 0 ;
160172}
161173
0 commit comments