query: Add a stub implementation for LocateCatalogs.
[wine] / dlls / msvcrt / tests / file.c
1 /*
2  * Unit test suite for file functions
3  *
4  * Copyright 2002 Bill Currie
5  * Copyright 2005 Paul Rupe
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "wine/test.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <share.h>
28 #include <sys/stat.h>
29 #include <io.h>
30 #include <windef.h>
31 #include <winbase.h>
32 #include <winnls.h>
33 #include <process.h>
34 #include <errno.h>
35
36 static void test_fdopen( void )
37 {
38     static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
39     char ibuf[10];
40     int fd;
41     FILE *file;
42
43     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
44     write (fd, buffer, sizeof (buffer));
45     close (fd);
46
47     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
48     lseek (fd, 5, SEEK_SET);
49     file = fdopen (fd, "rb");
50     ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
51     ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
52     fclose (file);
53     unlink ("fdopen.tst");
54 }
55
56 static void test_fileops( void )
57 {
58     static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
59     char buffer[256];
60     WCHAR wbuffer[256];
61     int fd;
62     FILE *file;
63     fpos_t pos;
64     int i, c;
65
66     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
67     write (fd, outbuffer, sizeof (outbuffer));
68     close (fd);
69
70     fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
71     file = fdopen (fd, "rb");
72     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
73     ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
74     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
75     ok(feof(file) !=0,"feof doesn't signal EOF\n");
76     rewind(file);
77     ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
78     ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
79     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
80     ok(strlen(buffer) == 1,"fgets dropped chars\n");
81     ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
82
83     rewind(file);
84     for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
85     {
86         ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
87     }
88     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
89     ok(feof(file), "feof did not return EOF\n");
90     ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n");
91     ok(feof(file), "feof after ungetc(EOF) did not return EOF\n");
92     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
93     c = outbuffer[sizeof(outbuffer) - 1];
94     ok(ungetc(c, file) == c, "ungetc did not return its input\n");
95     ok(!feof(file), "feof after ungetc returned EOF\n");
96     ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n");
97     ok(c == outbuffer[sizeof(outbuffer) - 1],
98        "getc did not return ungetc'd data\n");
99     ok(!feof(file), "feof after getc returned EOF prematurely\n");
100     ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
101     ok(feof(file), "feof after getc did not return EOF\n");
102
103     rewind(file);
104     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
105     ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
106     pos = (ULONGLONG)sizeof (outbuffer);
107     ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
108     ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
109     ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
110
111     fclose (file);
112     fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
113     file = fdopen (fd, "rt"); /* open in TEXT mode */
114     ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
115     ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
116     ok(feof(file) !=0,"feof doesn't signal EOF\n");
117     rewind(file);
118     ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
119     ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
120     ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
121     ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
122     fclose (file);
123
124     file = fopen("fdopen.tst", "rb");
125     ok( file != NULL, "fopen failed\n");
126     /* sizeof(buffer) > content of file */
127     ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
128     /* feof should be set now */
129     ok(feof(file), "feof after fread failed\n");
130     fclose (file);
131
132     unlink ("fdopen.tst");
133 }
134
135 static void test_asciifileops( void )
136 {
137     static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9\r\n\r\nA,B,C,D,E\r\nX,Y,Z";
138     char buffer[256];
139     int fd;
140     FILE *file;
141     int i;
142     long l;
143
144     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
145     write (fd, outbuffer, sizeof (outbuffer));
146     close (fd);
147     
148     /* Open file in ascii mode */
149     fd = open ("fdopen.tst", O_RDONLY);
150     file = fdopen (fd, "rb");
151     ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
152     ok(ftell(file) == 0,"Did not start at beginning of file\n");
153     ok(fgets(buffer,sizeof(buffer),file) !=0,"line 1 fgets failed unexpected\n");
154     ok(fgets(buffer,sizeof(buffer),file) !=0,"line 2 fgets failed unexpected\n");
155     ok(fgets(buffer,sizeof(buffer),file) !=0,"line 3 fgets failed unexpected\n");
156     ok(fgets(buffer,sizeof(buffer),file) !=0,"line 4 fgets failed unexpected\n");
157     ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
158     ok(feof(file) !=0,"feof doesn't signal EOF\n");
159     rewind(file);
160     ok(ftell(file) == 0,"Did not rewind to beginning of file\n");
161     for (i=0; i<strlen(outbuffer); i++)
162         if (outbuffer[i] == '\n') break;
163     i++;
164     ok(fgets(buffer,strlen(outbuffer),file) !=0,"line 1 fgets failed unexpected\n");
165     l = ftell(file);
166     todo_wine ok(l == i,"line 1 ftell got %ld should be %d\n", l, i);
167     ok(lstrlenA(buffer) == i-1,"line 1 fgets got size %d should be %d\n", lstrlenA(buffer), i-1);
168     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"line 2 fgets failed unexpected\n");
169     i += 2;
170     l = ftell(file);
171     todo_wine ok(l == i,"line 2 ftell got %ld should be %d\n", l, i);
172     ok(lstrlenA(buffer) == 1,"line 2 fgets got size %d should be %d\n", lstrlenA(buffer), 1);
173     ok(fgets(buffer,sizeof(outbuffer),file) !=0,"line 3 fgets failed unexpected\n");
174     for (; i<strlen(outbuffer); i++)
175         if (outbuffer[i] == '\n') break;
176     i++;
177     l = ftell(file);
178     ok(l == i,"line 3 ftell got %ld should be %d\n", l, i);
179     
180     fclose (file);
181
182     unlink ("fdopen.tst");
183 }
184
185
186 static WCHAR* AtoW( char* p )
187 {
188     WCHAR* buffer;
189     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
190     buffer = malloc( len * sizeof(WCHAR) );
191     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
192     return buffer;
193 }
194
195 static void test_fgetwc( void )
196 {
197 #define LLEN 512
198
199   char* tempf;
200   FILE *tempfh;
201   static const char mytext[]= "This is test_fgetwc\n";
202   WCHAR wtextW[LLEN+1];
203   WCHAR *mytextW = NULL, *aptr, *wptr;
204   BOOL diff_found = FALSE;
205   unsigned int i;
206
207   tempf=_tempnam(".","wne");
208   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
209   fputs(mytext,tempfh);
210   fclose(tempfh);
211   tempfh = fopen(tempf,"rt");
212   fgetws(wtextW,LLEN,tempfh);
213   mytextW = AtoW ((char*)mytext);
214   aptr = mytextW;
215   wptr = wtextW;
216
217   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
218     {
219       diff_found |= (*aptr != *wptr);
220     }
221   ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
222   if(mytextW) free (mytextW);
223   fclose(tempfh);
224   unlink(tempf);
225 }
226
227 static void test_file_put_get( void )
228 {
229   char* tempf;
230   FILE *tempfh;
231   static const char mytext[]=  "This is a test_file_put_get\n";
232   static const char dostext[]= "This is a test_file_put_get\r\n";
233   char btext[LLEN];
234   WCHAR wtextW[LLEN+1];
235   WCHAR *mytextW = NULL, *aptr, *wptr;
236   BOOL diff_found = FALSE;
237   unsigned int i;
238
239   tempf=_tempnam(".","wne");
240   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
241   fputs(mytext,tempfh);
242   fclose(tempfh);
243   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
244   fgets(btext,LLEN,tempfh);
245   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
246   ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
247   fclose(tempfh);
248   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
249   fputs(dostext,tempfh);
250   fclose(tempfh);
251   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
252   fgets(btext,LLEN,tempfh);
253   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
254   fclose(tempfh);
255   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
256   fgets(btext,LLEN,tempfh);
257   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
258
259   fclose(tempfh);
260   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
261   fgetws(wtextW,LLEN,tempfh);
262   mytextW = AtoW ((char*)mytext);
263   aptr = mytextW;
264   wptr = wtextW;
265
266   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
267     {
268       diff_found |= (*aptr != *wptr);
269     }
270   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
271   if(mytextW) free (mytextW);
272   fclose(tempfh);
273   unlink(tempf);
274 }
275
276 static void test_file_write_read( void )
277 {
278   char* tempf;
279   int tempfd;
280   static const char mytext[]=  "This is test_file_write_read\nsecond line\n";
281   static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
282   char btext[LLEN];
283   int ret;
284
285   tempf=_tempnam(".","wne");
286   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
287                      _S_IREAD | _S_IWRITE);
288   ok( tempfd != -1,
289      "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
290   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
291      "_write _O_TEXT bad return value\n");
292   _close(tempfd);
293   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
294   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
295      "_read _O_BINARY got bad length\n");
296   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
297       "problems with _O_TEXT _write / _O_BINARY _read\n");
298   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
299   _close(tempfd);
300   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
301   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
302      "_read _O_TEXT got bad length\n");
303   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
304       "problems with _O_TEXT _write / _read\n");
305   _close(tempfd);
306
307   memset(btext, 0, LLEN);
308   tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
309   ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
310   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
311   ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
312   _close(tempfd);
313
314   /* Test reading only \n or \r */
315   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
316   _lseek(tempfd, -1, FILE_END);
317   ret = _read(tempfd,btext,LLEN);
318   ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
319   _lseek(tempfd, -2, FILE_END);
320   ret = _read(tempfd,btext,LLEN);
321   ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
322   _lseek(tempfd, -3, FILE_END);
323   ret = _read(tempfd,btext,2);
324   todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
325   todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
326   _close(tempfd);
327
328   ret = unlink(tempf);
329   ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
330
331   tempf=_tempnam(".","wne");
332   tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
333   ok( tempfd != -1,
334      "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
335   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
336      "_write _O_BINARY bad return value\n");
337   _close(tempfd);
338   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
339   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
340      "_read _O_BINARY got bad length\n");
341   ok( memcmp(dostext,btext,strlen(dostext)) == 0,
342       "problems with _O_BINARY _write / _read\n");
343   ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
344   _close(tempfd);
345   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
346   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
347      "_read _O_TEXT got bad length\n");
348   ok( memcmp(mytext,btext,strlen(mytext)) == 0,
349       "problems with _O_BINARY _write / _O_TEXT _read\n");
350   _close(tempfd);
351
352    ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
353   ok( ret == 0,
354      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
355   ret = unlink(tempf);
356   ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
357 }
358
359 static void test_file_inherit_child(const char* fd_s)
360 {
361     int fd = atoi(fd_s);
362     char buffer[32];
363     int ret;
364
365     ret =write(fd, "Success", 8);
366     ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
367     lseek(fd, 0, SEEK_SET);
368     ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
369     ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
370 }
371
372 static void test_file_inherit_child_no(const char* fd_s)
373 {
374     int fd = atoi(fd_s);
375     int ret;
376
377     ret = write(fd, "Success", 8);
378     ok( ret == -1 && errno == EBADF, 
379        "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
380 }
381  
382 static void test_file_inherit( const char* selfname )
383 {
384     int                 fd;
385     const char*         arg_v[5];
386     char                buffer[16];
387
388     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
389     ok(fd != -1, "Couldn't create test file\n");
390     arg_v[0] = selfname;
391     arg_v[1] = "tests/file.c";
392     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
393     arg_v[3] = 0;
394     _spawnvp(_P_WAIT, selfname, arg_v);
395     ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
396     lseek(fd, 0, SEEK_SET);
397     ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
398     close (fd);
399     ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
400     
401     fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
402     ok(fd != -1, "Couldn't create test file\n");
403     arg_v[0] = selfname;
404     arg_v[1] = "tests/file.c";
405     arg_v[2] = buffer; sprintf(buffer, "%d", fd);
406     arg_v[3] = buffer;
407     arg_v[4] = 0;
408     _spawnvp(_P_WAIT, selfname, arg_v);
409     ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
410     ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
411     close (fd);
412     ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
413 }
414
415 static void test_tmpnam( void )
416 {
417   char name[MAX_PATH] = "abc";
418   char *res;
419
420   res = tmpnam(NULL);
421   ok(res != NULL, "tmpnam returned NULL\n");
422   ok(res[0] == '\\', "first character is not a backslash\n");
423   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
424   ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
425
426   res = tmpnam(name);
427   ok(res != NULL, "tmpnam returned NULL\n");
428   ok(res == name, "supplied buffer was not used\n");
429   ok(res[0] == '\\', "first character is not a backslash\n");
430   ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
431   ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
432 }
433
434 static void test_chsize( void )
435 {
436     int fd;
437     long cur, pos, count;
438     char temptext[] = "012345678";
439     char *tempfile = _tempnam( ".", "tst" );
440     
441     ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
442
443     fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
444     ok( fd > 0, "Couldn't open test file\n" );
445
446     count = _write( fd, temptext, sizeof(temptext) );
447     ok( count > 0, "Couldn't write to test file\n" );
448
449     /* get current file pointer */
450     cur = _lseek( fd, 0, SEEK_CUR );
451
452     /* make the file smaller */
453     ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
454
455     pos = _lseek( fd, 0, SEEK_CUR );
456     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
457     ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
458
459     /* enlarge the file */
460     ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); 
461
462     pos = _lseek( fd, 0, SEEK_CUR );
463     ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
464     ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
465
466     _close( fd );
467     _unlink( tempfile );
468 }
469
470 static void test_fopen_fclose_fcloseall( void )
471 {
472     char fname1[] = "empty1";
473     char fname2[] = "empty2";
474     char fname3[] = "empty3";
475     FILE *stream1, *stream2, *stream3, *stream4;
476     int ret, numclosed;
477
478     /* testing fopen() */
479     stream1 = fopen(fname1, "w+");
480     ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
481     stream2 = fopen(fname2, "w ");
482     ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
483     _unlink(fname3);
484     stream3 = fopen(fname3, "r");
485     ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
486     stream3 = fopen(fname3, "w+");
487     ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
488     errno = 0xfaceabad;
489     stream4 = fopen("", "w+");
490     ok(stream4 == NULL && errno == ENOENT, 
491        "filename is empty, errno = %d (expected 2)\n", errno);
492     errno = 0xfaceabad;
493     stream4 = fopen(NULL, "w+");
494     ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT), 
495        "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
496
497     /* testing fclose() */
498     ret = fclose(stream2);
499     ok(ret == 0, "The file '%s' was not closed\n", fname2);
500     ret = fclose(stream3);
501     ok(ret == 0, "The file '%s' was not closed\n", fname3);
502     ret = fclose(stream2);
503     ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
504     ret = fclose(stream3);
505     ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
506
507     /* testing fcloseall() */
508     numclosed = _fcloseall();
509     /* fname1 should be closed here */
510     ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
511     numclosed = _fcloseall();
512     ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
513
514     ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
515     ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
516     ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
517 }
518
519 static void test_get_osfhandle(void)
520 {
521     int fd;
522     char fname[] = "t_get_osfhanle";
523     DWORD bytes_written;
524     HANDLE handle;
525
526     fd = _sopen(fname, _O_CREAT|_O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
527     handle = (HANDLE)_get_osfhandle(fd);
528     WriteFile(handle, "bar", 3, &bytes_written, NULL);
529     _close(fd);
530     fd = _open(fname, _O_RDONLY, 0);
531     ok(fd != -1, "Coudn't open '%s' after _get_osfhanle()\n", fname);
532
533     _close(fd);
534     _unlink(fname);
535 }
536
537 START_TEST(file)
538 {
539     int arg_c;
540     char** arg_v;
541
542     arg_c = winetest_get_mainargs( &arg_v );
543
544     /* testing low-level I/O */
545     if (arg_c >= 3)
546     {
547         if (arg_c == 3) test_file_inherit_child(arg_v[2]); 
548         else test_file_inherit_child_no(arg_v[2]);
549         return;
550     }
551     test_file_inherit(arg_v[0]);
552     test_file_write_read();
553     test_chsize();
554
555     /* testing stream I/O */
556     test_fdopen();
557     test_fopen_fclose_fcloseall();
558     test_fileops();
559     test_asciifileops();
560     test_fgetwc();
561     test_file_put_get();
562     test_tmpnam();
563     test_get_osfhandle();
564 }