ReactOS 0.4.16-dev-889-g9563c07
file.c
Go to the documentation of this file.
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 <direct.h>
31#include <windef.h>
32#include <winbase.h>
33#include <winnls.h>
34#include <winreg.h>
35#include <process.h>
36#include <errno.h>
37#include <locale.h>
38#ifdef __REACTOS__
39#include <ntndk.h>
40#else
41#include <winternl.h>
42#endif
43
44#define WX_OPEN 0x01
45#define WX_ATEOF 0x02
46#define WX_READNL 0x04
47#define WX_PIPE 0x08
48#define WX_DONTINHERIT 0x10
49#define WX_APPEND 0x20
50#define WX_TTY 0x40
51#define WX_TEXT 0x80
52
53#define EF_UTF8 0x01
54#define EF_UTF16 0x02
55#define EF_CRIT_INIT 0x04
56#define EF_UNK_UNICODE 0x08
57
58#define MSVCRT_FD_BLOCK_SIZE 32
59typedef struct {
61 unsigned char wxflag;
62 char lookahead[3];
63 int exflag;
65} ioinfo;
67
69
70static int (__cdecl *p_fopen_s)(FILE**, const char*, const char*);
71static int (__cdecl *p__wfopen_s)(FILE**, const wchar_t*, const wchar_t*);
72static errno_t (__cdecl *p__get_fmode)(int*);
73static errno_t (__cdecl *p__set_fmode)(int);
74static int (__cdecl *p__setmaxstdio)(int);
75
76static const char* get_base_name(const char *path)
77{
78 const char *ret = path+strlen(path)-1;
79
80 while(ret >= path) {
81 if(*ret=='\\' || *ret=='/')
82 break;
83 ret--;
84 }
85 return ret+1;
86}
87
88static void init(void)
89{
90 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
91
92 setlocale(LC_CTYPE, "C");
93
94 p_fopen_s = (void*)GetProcAddress(hmod, "fopen_s");
95 p__wfopen_s = (void*)GetProcAddress(hmod, "_wfopen_s");
96 __pioinfo = (void*)GetProcAddress(hmod, "__pioinfo");
97 p__get_fmode = (void*)GetProcAddress(hmod, "_get_fmode");
98 p__set_fmode = (void*)GetProcAddress(hmod, "_set_fmode");
99 p__setmaxstdio = (void*)GetProcAddress(hmod, "_setmaxstdio");
100}
101
102static void test_filbuf( void )
103{
104 FILE *fp;
105 int c;
106 fpos_t pos;
107
108 fp = fopen("filbuf.tst", "wb");
109 fwrite("\n\n\n\n", 1, 4, fp);
110 fclose(fp);
111
112 fp = fopen("filbuf.tst", "rt");
113 c = _filbuf(fp);
114 ok(c == '\n', "read wrong byte\n");
115 /* See bug 16970 for why we care about _filbuf.
116 * ftell returns screwy values on files with lots
117 * of bare LFs in ascii mode because it assumes
118 * that ascii files contain only CRLFs, removes
119 * the CR's early in _filbuf, and adjusts the return
120 * value of ftell to compensate.
121 * native _filbuf will read the whole file, then consume and return
122 * the first one. That leaves fp->_fd at offset 4, and fp->_ptr
123 * pointing to a buffer of three bare LFs, so
124 * ftell will return 4 - 3 - 3 = -2.
125 */
126 ok(ftell(fp) == -2, "ascii crlf removal does not match native\n");
127 ok(fgetpos(fp, &pos) == 0, "fgetpos fail\n");
128 ok(pos == -2, "ftell does not match fgetpos\n");
129 fclose(fp);
130 unlink("filbuf.tst");
131}
132
133static void test_fdopen( void )
134{
135 static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
136 char ibuf[10];
137 int fd;
138 FILE *file;
139
140 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
141 write (fd, buffer, sizeof (buffer));
142 close (fd);
143
144 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
145 lseek (fd, 5, SEEK_SET);
146 file = fdopen (fd, "rb");
147 ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
148 ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
149 fclose (file);
150 unlink ("fdopen.tst");
151}
152
153static void test_fileops( void )
154{
155 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
156 char buffer[256];
157 WCHAR wbuffer[256];
158 int fd;
159 FILE *file;
160 fpos_t pos;
161 int i, c, bufmode;
162 static const int bufmodes[] = {_IOFBF,_IONBF};
163
164 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
165 write (fd, outbuffer, sizeof (outbuffer));
166 close (fd);
167
168 for (bufmode=0; bufmode < ARRAY_SIZE(bufmodes); bufmode++)
169 {
170 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
171 file = fdopen (fd, "rb");
172 setvbuf(file,NULL,bufmodes[bufmode],2048);
173 if(bufmodes[bufmode] == _IOFBF)
174 ok(file->_bufsiz == 2048, "file->_bufsiz = %d\n", file->_bufsiz);
175 ok(file->_base != NULL, "file->_base = NULL\n");
176 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error for bufmode=%x\n", bufmodes[bufmode]);
177 ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
178 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF for bufmode=%x\n", bufmodes[bufmode]);
179 ok(feof(file) !=0,"feof doesn't signal EOF for bufmode=%x\n", bufmodes[bufmode]);
180 rewind(file);
181 ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
182 ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size for bufmode=%x\n", bufmodes[bufmode]);
183 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
184 ok(strlen(buffer) == 1,"fgets dropped chars for bufmode=%x\n", bufmodes[bufmode]);
185 ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars for bufmode=%x\n", bufmodes[bufmode]);
186
187 rewind(file);
188 for (i = 0; i < sizeof(outbuffer); i++)
189 {
190 ok(fgetc(file) == outbuffer[i], "fgetc returned wrong data for bufmode=%x\n", bufmodes[bufmode]);
191 }
192 ok((c = fgetc(file)) == EOF, "getc did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
193 ok(feof(file), "feof did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
194 ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
195 ok(feof(file), "feof after ungetc(EOF) did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
196 ok(fgetc(file) == EOF, "getc did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
197 c = outbuffer[sizeof(outbuffer) - 1];
198 ok(ungetc(c, file) == c, "ungetc did not return its input for bufmode=%x\n", bufmodes[bufmode]);
199 ok(!feof(file), "feof after ungetc returned EOF for bufmode=%x\n", bufmodes[bufmode]);
200 ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF for bufmode=%x\n", bufmodes[bufmode]);
201 ok(c == outbuffer[sizeof(outbuffer) - 1],
202 "getc did not return ungetc'd data for bufmode=%x\n", bufmodes[bufmode]);
203 ok(!feof(file), "feof after getc returned EOF prematurely for bufmode=%x\n", bufmodes[bufmode]);
204 ok(fgetc(file) == EOF, "getc did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
205 ok(feof(file), "feof after getc did not return EOF for bufmode=%x\n", bufmodes[bufmode]);
206
207 rewind(file);
208 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
209 ok(pos == 0, "Unexpected result of fgetpos %s for bufmode=%x\n", wine_dbgstr_longlong(pos), bufmodes[bufmode]);
210 pos = sizeof (outbuffer);
211 ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
212 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
213 ok(pos == sizeof (outbuffer), "Unexpected result of fgetpos %s for bufmode=%x\n", wine_dbgstr_longlong(pos), bufmodes[bufmode]);
214
215 fclose (file);
216 }
217 fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
218 file = fdopen (fd, "rt"); /* open in TEXT mode */
219 ok(fgetws(wbuffer,ARRAY_SIZE(wbuffer),file) !=0,"fgetws failed unexpected\n");
220 ok(fgetws(wbuffer,ARRAY_SIZE(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
221 ok(feof(file) !=0,"feof doesn't signal EOF\n");
222 rewind(file);
223 ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
224 ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
225 ok(fgetws(wbuffer,ARRAY_SIZE(outbuffer),file) !=0,"fgets failed unexpected\n");
226 ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
227 fclose (file);
228
229 file = fopen("fdopen.tst", "rb");
230 ok( file != NULL, "fopen failed\n");
231 /* sizeof(buffer) > content of file */
232 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
233 /* feof should be set now */
234 ok(feof(file), "feof after fread failed\n");
235 clearerr(file);
236 ok(!feof(file), "feof after clearerr failed\n");
237 fclose(file);
238
239 file = fopen("fdopen.tst", "rb");
240 ok( file != NULL, "fopen failed\n");
241 /* sizeof(buffer) > content of file */
242 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
243 /* feof should be set now */
244 ok(feof(file), "feof after fread failed\n");
245 rewind(file);
246 ok(!feof(file), "feof after rewind failed\n");
247 fclose(file);
248
249 file = fopen("fdopen.tst", "rb");
250 ok( file != NULL, "fopen failed\n");
251 /* sizeof(buffer) > content of file */
252 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
253 /* feof should be set now */
254 ok(feof(file), "feof after fread failed\n");
255 fseek(file, 0, SEEK_SET);
256 ok(!feof(file), "feof after fseek failed\n");
257 fclose(file);
258
259 file = fopen("fdopen.tst", "rb");
260 ok( file != NULL, "fopen failed\n");
261 /* sizeof(buffer) > content of file */
262 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
263 /* feof should be set now */
264 ok(feof(file), "feof after fread failed\n");
265 fgetpos(file, &pos);
266 fsetpos(file, &pos);
267 ok(!feof(file), "feof after fsetpos failed\n");
268 fclose(file);
269
270 file = fopen("fdopen.tst", "rb");
271 ok( file != NULL, "fopen failed\n");
272 /* sizeof(buffer) > content of file */
273 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
274 /* feof should be set now */
275 ok(feof(file), "feof after fread failed\n");
276 fsetpos(file, &pos);
277 ok(!feof(file), "feof after fsetpos failed\n");
278 fclose(file);
279
280 unlink ("fdopen.tst");
281}
282
283#define IOMODE (ao?"ascii mode":"binary mode")
284static void test_readmode( BOOL ascii_mode )
285{
286 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";
287 static const char padbuffer[] = "ghjghjghjghj";
288 static const char nlbuffer[] = "\r\n";
289 static char buffer[8192];
290 const char *optr;
291 int fd;
292 FILE *file;
293 const int *ip;
294 int i, j, m, ao, pl;
295 unsigned int fp;
296 LONG l;
297
298 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
299 /* an internal buffer of BUFSIZ is maintained, so make a file big
300 * enough to test operations that cross the buffer boundary
301 */
302 j = (2*BUFSIZ-4)/strlen(padbuffer);
303 for (i=0; i<j; i++)
304 write (fd, padbuffer, strlen(padbuffer));
305 j = (2*BUFSIZ-4)%strlen(padbuffer);
306 for (i=0; i<j; i++)
307 write (fd, &padbuffer[i], 1);
308 write (fd, nlbuffer, strlen(nlbuffer));
309 write (fd, outbuffer, sizeof (outbuffer));
310
311 errno = 0xdeadbeef;
312 _doserrno = 0xdeadbeef;
313 ok(read(fd, buffer, 1) == -1, "read succeeded on write-only file\n");
314 ok(errno == EBADF, "errno = %d\n", errno);
315 ok(_doserrno == ERROR_ACCESS_DENIED, "doserrno = %ld\n", _doserrno);
316
317 close (fd);
318
319 fd = open ("fdopen.tst", O_RDONLY, _S_IREAD |_S_IWRITE);
320 errno = 0xdeadbeef;
321 ok(dup2(fd, -1) == -1, "dup2(fd, -1) succeeded\n");
322 ok(errno == EBADF, "errno = %d\n", errno);
323 close (fd);
324
325 if (ascii_mode) {
326 /* Open file in ascii mode */
327 fd = open ("fdopen.tst", O_RDONLY);
328 file = fdopen (fd, "r");
329 ao = -1; /* on offset to account for carriage returns */
330 }
331 else {
332 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
333 file = fdopen (fd, "rb");
334 ao = 0;
335 }
336
337 /* first is a test of fgets, ftell, fseek */
338 ok(ftell(file) == 0,"Did not start at beginning of file in %s\n", IOMODE);
339 ok(fgets(buffer,2*BUFSIZ+256,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
340 l = ftell(file);
341 pl = 2*BUFSIZ-2;
342 ok(l == pl,"padding line ftell got %ld should be %d in %s\n", l, pl, IOMODE);
343 ok(lstrlenA(buffer) == pl+ao,"padding line fgets got size %d should be %d in %s\n",
344 lstrlenA(buffer), pl+ao, IOMODE);
345 for (fp=0; fp<strlen(outbuffer); fp++)
346 if (outbuffer[fp] == '\n') break;
347 fp++;
348 ok(fgets(buffer,256,file) !=0,"line 1 fgets failed unexpected in %s\n", IOMODE);
349 l = ftell(file);
350 ok(l == pl+fp,"line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
351 ok(lstrlenA(buffer) == fp+ao,"line 1 fgets got size %d should be %d in %s\n",
352 lstrlenA(buffer), fp+ao, IOMODE);
353 /* test a seek back across the buffer boundary */
354 l = pl;
355 ok(fseek(file,l,SEEK_SET)==0,"seek failure in %s\n", IOMODE);
356 l = ftell(file);
357 ok(l == pl,"ftell after seek got %ld should be %d in %s\n", l, pl, IOMODE);
358 ok(fgets(buffer,256,file) !=0,"second read of line 1 fgets failed unexpected in %s\n", IOMODE);
359 l = ftell(file);
360 ok(l == pl+fp,"second read of line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
361 ok(lstrlenA(buffer) == fp+ao,"second read of line 1 fgets got size %d should be %d in %s\n",
362 lstrlenA(buffer), fp+ao, IOMODE);
363 ok(fgets(buffer,256,file) !=0,"line 2 fgets failed unexpected in %s\n", IOMODE);
364 fp += 2;
365 l = ftell(file);
366 ok(l == pl+fp,"line 2 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
367 ok(lstrlenA(buffer) == 2+ao,"line 2 fgets got size %d should be %d in %s\n",
368 lstrlenA(buffer), 2+ao, IOMODE);
369
370 errno = 0xdeadbeef;
371 _doserrno = 0xdeadbeef;
372 ok(write(fd, buffer, 1) == -1, "read succeeded on write-only file\n");
373 ok(errno == EBADF, "errno = %d\n", errno);
374 ok(_doserrno == ERROR_ACCESS_DENIED, "doserrno = %ld\n", _doserrno);
375
376 /* test fread across buffer boundary */
377 rewind(file);
378 ok(ftell(file) == 0,"Did not start at beginning of file in %s\n", IOMODE);
379 ok(fgets(buffer,BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
382 ok(i==BUFSIZ+j,"fread failed, expected %d got %d in %s\n", BUFSIZ+j, i, IOMODE);
383 l = ftell(file);
384 ok(l == pl+j-(ao*4)-5,"ftell after fread got %ld should be %d in %s\n", l, pl+j-(ao*4)-5, IOMODE);
385 for (m=0; m<3; m++)
386 ok(buffer[m]==padbuffer[m+(BUFSIZ-4)%strlen(padbuffer)],"expected %c got %c\n", padbuffer[m], buffer[m]);
387 m+=BUFSIZ+2+ao;
388 optr = outbuffer;
389 for (; m<i; m++) {
390 ok(buffer[m]==*optr,"char %d expected %c got %c in %s\n", m, *optr, buffer[m], IOMODE);
391 optr++;
392 if (ao && (*optr == '\r'))
393 optr++;
394 }
395 /* fread should return the requested number of bytes if available */
396 rewind(file);
397 ok(ftell(file) == 0,"Did not start at beginning of file in %s\n", IOMODE);
398 ok(fgets(buffer,BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
399 j = fp+10;
400 i=fread(buffer,1,j,file);
401 ok(i==j,"fread failed, expected %d got %d in %s\n", j, i, IOMODE);
402 /* test fread eof */
403 ok(fseek(file,0,SEEK_END)==0,"seek failure in %s\n", IOMODE);
404 ok(feof(file)==0,"feof failure in %s\n", IOMODE);
405 ok(fread(buffer,1,1,file)==0,"fread failure in %s\n", IOMODE);
406 ok(feof(file)!=0,"feof failure in %s\n", IOMODE);
407 ok(fseek(file,-3,SEEK_CUR)==0,"seek failure in %s\n", IOMODE);
408 ok(feof(file)==0,"feof failure in %s\n", IOMODE);
409 ok(fread(buffer,2,1,file)==1,"fread failed in %s\n", IOMODE);
410 ok(feof(file)==0,"feof failure in %s\n", IOMODE);
411 ok(fread(buffer,2,1,file)==0,"fread failure in %s\n",IOMODE);
412 ok(feof(file)!=0,"feof failure in %s\n", IOMODE);
413
414 /* test some additional functions */
415 rewind(file);
416 ok(ftell(file) == 0,"Did not start at beginning of file in %s\n", IOMODE);
417 ok(fgets(buffer,2*BUFSIZ+256,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
418 i = _getw(file);
419 ip = (const int *)outbuffer;
420 ok(i == *ip,"_getw failed, expected %08x got %08x in %s\n", *ip, i, IOMODE);
421 for (fp=0; fp<strlen(outbuffer); fp++)
422 if (outbuffer[fp] == '\n') break;
423 fp++;
424 /* this will cause the next _getw to cross carriage return characters */
425 ok(fgets(buffer,fp-6,file) !=0,"line 1 fgets failed unexpected in %s\n", IOMODE);
426 for (i=0, j=0; i<6; i++) {
427 if (ao==0 || outbuffer[fp-3+i] != '\r')
428 buffer[j++] = outbuffer[fp-3+i];
429 }
430 i = _getw(file);
431 ip = (int *)buffer;
432 ok(i == *ip,"_getw failed, expected %08x got %08x in %s\n", *ip, i, IOMODE);
433
434 fclose (file);
435 unlink ("fdopen.tst");
436
437 /* test INTERNAL_BUFSIZ read containing 0x1a character (^Z) */
438 fd = open("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
439 ok(fd != -1, "open failed\n");
440 memset(buffer, 'a', sizeof(buffer));
441 buffer[1] = 0x1a;
442 ok(write(fd, buffer, sizeof(buffer)) == sizeof(buffer), "write failed\n");
443 ok(close(fd) != -1, "close failed\n");
444
445 fd = open("fdopen.tst", O_RDONLY);
446 ok(fd != -1, "open failed\n");
447 file = fdopen(fd, ascii_mode ? "r" : "rb");
448 ok(file != NULL, "fdopen failed\n");
449
450 memset(buffer, 0, sizeof(buffer));
451 i = fread(buffer, 4096, 1, file);
452 ok(!i, "fread succeeded\n");
453 ok(file->_bufsiz == 4096, "file->_bufsiz = %d\n", file->_bufsiz);
454 for(i=0; i<4096; i++)
455 if(buffer[i] != (i==1 ? 0x1a : 'a')) break;
456 ok(i==4096, "buffer[%d] = %d\n", i, buffer[i]);
457
458 fclose(file);
459 unlink("fdopen.tst");
460}
461
462static void test_asciimode(void)
463{
464 FILE *fp;
465 char buf[64];
466 int c, i, j;
467
468 /* Simple test of CR CR LF handling. Test both fgets and fread code paths, they're different! */
469 fp = fopen("ascii.tst", "wb");
470 fputs("\r\r\n", fp);
471 fclose(fp);
472 fp = fopen("ascii.tst", "rt");
473 ok(fgets(buf, sizeof(buf), fp) != NULL, "fgets\n");
474 ok(0 == strcmp(buf, "\r\n"), "CR CR LF not read as CR LF\n");
475 rewind(fp);
476 ok((fread(buf, 1, sizeof(buf), fp) == 2) && (0 == strcmp(buf, "\r\n")), "CR CR LF not read as CR LF\n");
477 fclose(fp);
478 unlink("ascii.tst");
479
480 /* Simple test of foo ^Z [more than one block] bar handling */
481 fp = fopen("ascii.tst", "wb");
482 fputs("foo\032", fp); /* foo, logical EOF, ... */
483 fseek(fp, 65536L, SEEK_SET); /* ... more than MSVCRT_BUFSIZ, ... */
484 fputs("bar", fp); /* ... bar */
485 fclose(fp);
486 fp = fopen("ascii.tst", "rt");
487 ok(fgets(buf, sizeof(buf), fp) != NULL, "fgets foo\n");
488 ok(0 == strcmp(buf, "foo"), "foo ^Z not read as foo by fgets\n");
489 ok(fgets(buf, sizeof(buf), fp) == NULL, "fgets after logical EOF\n");
490 rewind(fp);
491 ok((fread(buf, 1, sizeof(buf), fp) == 3) && (0 == strcmp(buf, "foo")), "foo ^Z not read as foo by fread\n");
492 ok((fread(buf, 1, sizeof(buf), fp) == 0), "fread after logical EOF\n");
493 fclose(fp);
494
495 /* Show ASCII mode handling*/
496 fp= fopen("ascii.tst","wb");
497 fputs("0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n", fp);
498 fclose(fp);
499
500 fp = fopen("ascii.tst", "r");
501 c= fgetc(fp);
502 ok(c == '0', "fgetc failed, expected '0', got '%c'\n", c);
503 c= fgetc(fp);
504 ok(c == '\n', "fgetc failed, expected '\\n', got '%c'\n", c);
505 fseek(fp,0,SEEK_CUR);
506 for(i=1; i<10; i++) {
507 ok((j = ftell(fp)) == i*3, "ftell fails in TEXT mode\n");
508 fseek(fp,0,SEEK_CUR);
509 ok((c = fgetc(fp)) == '0'+ i, "fgetc after fseek failed in line %d\n", i);
510 c= fgetc(fp);
511 ok(c == '\n', "fgetc failed, expected '\\n', got '%c'\n", c);
512 }
513 /* Show that fseek doesn't skip \\r !*/
514 rewind(fp);
515 c= fgetc(fp);
516 ok(c == '0', "fgetc failed, expected '0', got '%c'\n", c);
517 fseek(fp, 2 ,SEEK_CUR);
518 for(i=1; i<10; i++) {
519 ok((c = fgetc(fp)) == '0'+ i, "fgetc after fseek with pos Offset failed in line %d\n", i);
520 fseek(fp, 2 ,SEEK_CUR);
521 }
522 fseek(fp, 9*3 ,SEEK_SET);
523 c = fgetc(fp);
524 ok(c == '9', "fgetc failed, expected '9', got '%c'\n", c);
525 fseek(fp, -4 ,SEEK_CUR);
526 for(i= 8; i>=0; i--) {
527 ok((c = fgetc(fp)) == '0'+ i, "fgetc after fseek with neg Offset failed in line %d\n", i);
528 fseek(fp, -4 ,SEEK_CUR);
529 }
530 /* Show what happens if fseek positions filepointer on \\r */
531 fclose(fp);
532 fp = fopen("ascii.tst", "r");
533 fseek(fp, 3 ,SEEK_SET);
534 ok((c = fgetc(fp)) == '1', "fgetc fails to read next char when positioned on \\r\n");
535 fclose(fp);
536
537 unlink("ascii.tst");
538}
539
540static void test_asciimode2(void)
541{
542 /* Error sequence from one app was getchar followed by small fread
543 * with one \r removed had last byte of buffer filled with
544 * next byte of *unbuffered* data rather than next byte from buffer
545 * Test case is a short string of one byte followed by a newline
546 * followed by filler to fill out the sector, then a sector of
547 * some different byte.
548 */
549
550 FILE *fp;
551 char ibuf[4];
552 int i;
553 static const char obuf[] =
554"00\n"
555"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
556"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
557"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
558"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
559"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
560"000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
561"000000000000000000\n"
562"1111111111111111111";
563
564 fp = fopen("ascii2.tst", "wt");
565 fwrite(obuf, 1, sizeof(obuf), fp);
566 fclose(fp);
567
568 fp = fopen("ascii2.tst", "rt");
569 ok(getc(fp) == '0', "first char not 0\n");
570 memset(ibuf, 0, sizeof(ibuf));
571 i = fread(ibuf, 1, sizeof(ibuf), fp);
572 ok(i == sizeof(ibuf), "fread i %d != sizeof(ibuf)\n", i);
573 ok(0 == strncmp(ibuf, obuf+1, sizeof(ibuf)), "ibuf != obuf\n");
574 fclose(fp);
575 unlink("ascii2.tst");
576}
577
578static void test_filemodeT(void)
579{
580 char DATA [] = {26, 't', 'e', 's' ,'t'};
581 char DATA2 [100];
582 char temppath[MAX_PATH];
583 char tempfile[MAX_PATH];
584 FILE* f;
585 size_t bytesWritten;
586 size_t bytesRead;
587 WIN32_FIND_DATAA findData;
588 HANDLE h;
589
590 GetTempPathA(MAX_PATH, temppath);
591 GetTempFileNameA(temppath, "", 0, tempfile);
592
593 f = fopen(tempfile, "w+bDT");
594 bytesWritten = fwrite(DATA, 1, sizeof(DATA), f);
595 rewind(f);
596 bytesRead = fread(DATA2, 1, sizeof(DATA2), f);
597 fclose(f);
598
599 ok (bytesRead == bytesWritten && bytesRead == sizeof(DATA),
600 "fopen file mode 'T' wrongly interpreted as 't'\n" );
601
602 h = FindFirstFileA(tempfile, &findData);
603
604 ok (h == INVALID_HANDLE_VALUE, "file wasn't deleted when closed.\n" );
605
607}
608
609static WCHAR* AtoW( const char* p )
610{
611 WCHAR* buffer;
612 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
613 buffer = malloc( len * sizeof(WCHAR) );
615 return buffer;
616}
617
618/* Test reading in text mode when the 512'th character read is \r*/
619static void test_readboundary(void)
620{
621 FILE *fp;
622 char buf[513], rbuf[513];
623 int i, j;
624 for (i = 0; i < 511; i++)
625 {
626 j = (i%('~' - ' ')+ ' ');
627 buf[i] = j;
628 }
629 buf[511] = '\n';
630 buf[512] =0;
631 fp = fopen("boundary.tst", "wt");
632 fwrite(buf, 512,1,fp);
633 fclose(fp);
634 fp = fopen("boundary.tst", "rt");
635 for(i=0; i<512; i++)
636 {
637 fseek(fp,0 , SEEK_CUR);
638 rbuf[i] = fgetc(fp);
639 }
640 rbuf[512] =0;
641 fclose(fp);
642 unlink("boundary.tst");
643
644 ok(strcmp(buf, rbuf) == 0,"CRLF on buffer boundary failure\n");
645 }
646
647static void test_fgetc( void )
648{
649 char* tempf;
650 FILE *tempfh;
651 int ich=0xe0, ret;
652
653 tempf=_tempnam(".","wne");
654 tempfh = fopen(tempf,"w+");
655 fputc(ich, tempfh);
656 fputc(ich, tempfh);
657 rewind(tempfh);
658 ret = fgetc(tempfh);
659 ok(ich == ret, "First fgetc expected %x got %x\n", ich, ret);
660 ret = fgetc(tempfh);
661 ok(ich == ret, "Second fgetc expected %x got %x\n", ich, ret);
662 fclose(tempfh);
663 tempfh = fopen(tempf,"wt");
664 fputc('\n', tempfh);
665 fclose(tempfh);
666 tempfh = fopen(tempf,"wt");
667 setbuf(tempfh, NULL);
668 ret = fgetc(tempfh);
669 ok(ret == -1, "Unbuffered fgetc in text mode must failed on \\r\\n\n");
670 fclose(tempfh);
671 unlink(tempf);
672 free(tempf);
673}
674
675static void test_fputc( void )
676{
677 char* tempf;
678 FILE *tempfh;
679 int ret;
680
681 tempf=_tempnam(".","wne");
682 tempfh = fopen(tempf,"wb");
683 ret = fputc(0,tempfh);
684 ok(0 == ret, "fputc(0,tempfh) expected %x got %x\n", 0, ret);
685 ret = fputc(0xff,tempfh);
686 ok(0xff == ret, "fputc(0xff,tempfh) expected %x got %x\n", 0xff, ret);
687 ret = fputc(0xffffffff,tempfh);
688 ok(0xff == ret, "fputc(0xffffffff,tempfh) expected %x got %x\n", 0xff, ret);
689 fclose(tempfh);
690
691 tempfh = fopen(tempf,"rb");
692 ret = fputc(0,tempfh);
693 ok(EOF == ret, "fputc(0,tempfh) on r/o file expected %x got %x\n", EOF, ret);
694 fclose(tempfh);
695
696 unlink(tempf);
697 free(tempf);
698}
699
700static void test_flsbuf( void )
701{
702 char* tempf;
703 FILE *tempfh;
704 int c;
705 int ret;
706 int bufmode;
707 static const int bufmodes[] = {_IOFBF,_IONBF};
708
709 tempf=_tempnam(".","wne");
710 for (bufmode=0; bufmode < ARRAY_SIZE(bufmodes); bufmode++)
711 {
712 tempfh = fopen(tempf,"wb");
713 setvbuf(tempfh,NULL,bufmodes[bufmode],2048);
714 ret = _flsbuf(0,tempfh);
715 ok(0 == ret, "_flsbuf(0,tempfh) with bufmode %x expected %x got %x\n",
716 bufmodes[bufmode], 0, ret);
717 ret = _flsbuf(0xff,tempfh);
718 ok(0xff == ret, "_flsbuf(0xff,tempfh) with bufmode %x expected %x got %x\n",
719 bufmodes[bufmode], 0xff, ret);
720 ret = _flsbuf(0xffffffff,tempfh);
721 ok(0xff == ret, "_flsbuf(0xffffffff,tempfh) with bufmode %x expected %x got %x\n",
722 bufmodes[bufmode], 0xff, ret);
723 if(tempfh->_base) {
724 fputc('x', tempfh);
725 tempfh->_cnt = -1;
726 tempfh->_base[1] = 'a';
727 ret = _flsbuf(0xab,tempfh);
728 ok(ret == 0xab, "_flsbuf(0xab,tempfh) with bufmode %x expected 0xab got %x\n",
729 bufmodes[bufmode], ret);
730 ok(tempfh->_base[1] == 'a', "tempfh->_base[1] should not be changed (%d)\n",
731 tempfh->_base[1]);
732 }
733
734 fclose(tempfh);
735 }
736
737 tempfh = fopen(tempf,"rb");
738 ret = _flsbuf(0,tempfh);
739 ok(EOF == ret, "_flsbuf(0,tempfh) on r/o file expected %x got %x\n", EOF, ret);
740 fclose(tempfh);
741
742 /* See bug 17123, exposed by WinAVR's make */
743 tempfh = fopen(tempf,"w");
744 ok(tempfh->_cnt == 0, "_cnt on freshly opened file was %d\n", tempfh->_cnt);
745 setbuf(tempfh, NULL);
746 ok(tempfh->_cnt == 0, "_cnt on unbuffered file was %d\n", tempfh->_cnt);
747 ok(tempfh->_bufsiz == 2, "_bufsiz = %d\n", tempfh->_bufsiz);
748 /* Inlined putchar sets _cnt to -1. Native seems to ignore the value... */
749 tempfh->_cnt = 1234;
750 ret = _flsbuf('Q',tempfh);
751 ok('Q' == ret, "_flsbuf('Q',tempfh) expected %x got %x\n", 'Q', ret);
752 /* ... and reset it to zero */
753 ok(tempfh->_cnt == 0, "after unbuf _flsbuf, _cnt was %d\n", tempfh->_cnt);
754 fclose(tempfh);
755 /* And just for grins, make sure the file is correct */
756 tempfh = fopen(tempf,"r");
757 c = fgetc(tempfh);
758 ok(c == 'Q', "first byte should be 'Q'\n");
759 c = fgetc(tempfh);
760 ok(c == EOF, "there should only be one byte\n");
761 fclose(tempfh);
762
763 unlink(tempf);
764 free(tempf);
765}
766
767static void test_fflush( void )
768{
769 static const char obuf[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
770 char buf1[16], buf2[24];
771 char *tempf;
772 FILE *tempfh;
773 int ret, fd;
774
775 tempf=_tempnam(".","wne");
776
777 /* Prepare the file. */
778 tempfh = fopen(tempf,"wb");
779 ok(tempfh != NULL, "Can't open test file.\n");
780 fwrite(obuf, 1, sizeof(obuf), tempfh);
781 fclose(tempfh);
782
783 /* Open the file for input. */
784 tempfh = fopen(tempf,"rb");
785 ok(tempfh != NULL, "Can't open test file.\n");
786 fread(buf1, 1, sizeof(buf1), tempfh);
787
788 /* Using fflush() on input stream is undefined in ANSI.
789 * But MSDN says that it clears input buffer. */
790 _lseek(_fileno(tempfh), 0, SEEK_SET);
791 ret = fflush(tempfh);
792 ok(ret == 0, "expected 0, got %d\n", ret);
793 memset(buf2, '?', sizeof(buf2));
794 fread(buf2, 1, sizeof(buf2), tempfh);
795 ok(memcmp(buf1, buf2, sizeof(buf1)) == 0, "Got unexpected data (%c)\n", buf2[0]);
796
797 /* fflush(NULL) doesn't clear input buffer. */
798 _lseek(_fileno(tempfh), 0, SEEK_SET);
799 ret = fflush(NULL);
800 ok(ret == 0, "expected 0, got %d\n", ret);
801 memset(buf2, '?', sizeof(buf2));
802 fread(buf2, 1, sizeof(buf2), tempfh);
803 ok(memcmp(buf1, buf2, sizeof(buf1)) != 0, "Got unexpected data (%c)\n", buf2[0]);
804
805 /* _flushall() clears input buffer. */
806 _lseek(_fileno(tempfh), 0, SEEK_SET);
807 ret = _flushall();
808 ok(ret >= 0, "unexpected ret %d\n", ret);
809 memset(buf2, '?', sizeof(buf2));
810 fread(buf2, 1, sizeof(buf2), tempfh);
811 ok(memcmp(buf1, buf2, sizeof(buf1)) == 0, "Got unexpected data (%c)\n", buf2[0]);
812
813 fclose(tempfh);
814 unlink(tempf);
815
816 /* test flush failure */
817 tempfh = fopen(tempf,"wb");
818 ok(tempfh != NULL, "Can't open test file.\n");
819 fwrite(obuf, 1, sizeof(obuf), tempfh);
820 fd = tempfh->_file;
821 tempfh->_file = -1;
822
823 ok(tempfh->_ptr - tempfh->_base, "buffer is empty\n");
824 ret = fflush(tempfh);
825 ok(ret == EOF, "expected EOF, got %d\n", ret);
826 ok(!(tempfh->_ptr - tempfh->_base), "buffer should be empty\n");
827 ok(!tempfh->_cnt, "tempfh->_cnt = %d\n", tempfh->_cnt);
828
829 tempfh->_file = fd;
830 fclose(tempfh);
831 unlink(tempf);
832 free(tempf);
833}
834
835static void test_fgetwc( void )
836{
837#define LLEN 512
838
839 char* tempf;
840 FILE *tempfh;
841 static const char mytext[]= "This is test_fgetwc\r\n";
842 WCHAR wtextW[BUFSIZ+LLEN+1];
843 WCHAR *mytextW = NULL, *aptr, *wptr;
844 BOOL diff_found = FALSE;
845 int j;
846 unsigned int i;
847 LONG l;
848
849 tempf=_tempnam(".","wne");
850 tempfh = fopen(tempf,"wb");
851 j = 'a';
852 /* pad to almost the length of the internal buffer */
853 for (i=0; i<BUFSIZ-4; i++)
854 fputc(j,tempfh);
855 j = '\r';
856 fputc(j,tempfh);
857 j = '\n';
858 fputc(j,tempfh);
859 fputs(mytext,tempfh);
860 fclose(tempfh);
861 /* in text mode, getws/c expects multibyte characters */
862 /*currently Wine only supports plain ascii, and that is all that is tested here */
863 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
864 fgetws(wtextW,LLEN,tempfh);
865 l=ftell(tempfh);
866 ok(l==BUFSIZ-2, "ftell expected %d got %ld\n", BUFSIZ-2, l);
867 fgetws(wtextW,LLEN,tempfh);
868 l=ftell(tempfh);
869 ok(l==BUFSIZ-2+strlen(mytext), "ftell expected %d got %ld\n", BUFSIZ-2+lstrlenA(mytext), l);
870 mytextW = AtoW (mytext);
871 aptr = mytextW;
872 wptr = wtextW;
873 for (i=0; i<strlen(mytext)-2; i++, aptr++, wptr++)
874 {
875 diff_found |= (*aptr != *wptr);
876 }
877 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
878 ok(*wptr == '\n', "Carriage return was not skipped\n");
879 fclose(tempfh);
880 unlink(tempf);
881
882 tempfh = fopen(tempf,"wb");
883 j = 'a';
884 /* pad to almost the length of the internal buffer. Use an odd number of bytes
885 to test that we can read wchars that are split across the internal buffer
886 boundary */
887 for (i=0; i<BUFSIZ-3-strlen(mytext)*sizeof(WCHAR); i++)
888 fputc(j,tempfh);
889 j = '\r';
890 fputwc(j,tempfh);
891 j = '\n';
892 fputwc(j,tempfh);
893 fputws(wtextW,tempfh);
894 fputws(wtextW,tempfh);
895 fclose(tempfh);
896 /* in binary mode, getws/c expects wide characters */
897 tempfh = fopen(tempf,"rb"); /* open in BINARY mode */
898 j=(BUFSIZ-2)/sizeof(WCHAR)-strlen(mytext);
899 fgetws(wtextW,j,tempfh);
900 l=ftell(tempfh);
901 j=(j-1)*sizeof(WCHAR);
902 ok(l==j, "ftell expected %d got %ld\n", j, l);
903 i=fgetc(tempfh);
904 ok(i=='a', "fgetc expected %d got %d\n", 0x61, i);
905 l=ftell(tempfh);
906 j++;
907 ok(l==j, "ftell expected %d got %ld\n", j, l);
908 fgetws(wtextW,3,tempfh);
909 ok(wtextW[0]=='\r',"expected carriage return got %04hx\n", wtextW[0]);
910 ok(wtextW[1]=='\n',"expected newline got %04hx\n", wtextW[1]);
911 l=ftell(tempfh);
912 j += 4;
913 ok(l==j, "ftell expected %d got %ld\n", j, l);
914 for(i=0; i<strlen(mytext); i++)
915 wtextW[i] = 0;
916 /* the first time we get the string, it should be entirely within the local buffer */
917 fgetws(wtextW,LLEN,tempfh);
918 l=ftell(tempfh);
919 j += (strlen(mytext)-1)*sizeof(WCHAR);
920 ok(l==j, "ftell expected %d got %ld\n", j, l);
921 diff_found = FALSE;
922 aptr = mytextW;
923 wptr = wtextW;
924 for (i=0; i<strlen(mytext)-2; i++, aptr++, wptr++)
925 {
926 ok(*aptr == *wptr, "Char %d expected %04hx got %04hx\n", i, *aptr, *wptr);
927 diff_found |= (*aptr != *wptr);
928 }
929 ok(!(diff_found), "fgetwc difference found in BINARY mode\n");
930 ok(*wptr == '\n', "Should get newline\n");
931 for(i=0; i<strlen(mytext); i++)
932 wtextW[i] = 0;
933 /* the second time we get the string, it should cross the local buffer boundary.
934 One of the wchars should be split across the boundary */
935 fgetws(wtextW,LLEN,tempfh);
936 diff_found = FALSE;
937 aptr = mytextW;
938 wptr = wtextW;
939 for (i=0; i<strlen(mytext)-2; i++, aptr++, wptr++)
940 {
941 ok(*aptr == *wptr, "Char %d expected %04hx got %04hx\n", i, *aptr, *wptr);
942 diff_found |= (*aptr != *wptr);
943 }
944 ok(!(diff_found), "fgetwc difference found in BINARY mode\n");
945 ok(*wptr == '\n', "Should get newline\n");
946
947 free(mytextW);
948 fclose(tempfh);
949 unlink(tempf);
950 free(tempf);
951}
952
953static void test_fgetwc_locale(const char* text, const char* locale, int codepage)
954{
955 char temppath[MAX_PATH], tempfile[MAX_PATH];
956 FILE *tempfh;
957 static const WCHAR wchar_text[] = { 0xfeff, 0xff1f, '!' };
958 WCHAR wtextW[BUFSIZ];
959 int ret = 0, i;
960 wint_t ch;
961
963 {
964 win_skip("%s locale not available\n", locale);
965 return;
966 }
967
968 GetTempPathA(MAX_PATH, temppath);
969 GetTempFileNameA(temppath, "", 0, tempfile);
970
971 tempfh = fopen(tempfile, "wb");
972 ok(tempfh != NULL, "can't open tempfile\n");
973 fwrite(text, 1, strlen(text), tempfh);
974 fclose(tempfh);
975
976 if (codepage != 0)
977 {
978 /* mbstowcs rejects invalid multibyte sequence,
979 so we use MultiByteToWideChar here. */
980 ret = MultiByteToWideChar(codepage, 0, text, -1, wtextW, ARRAY_SIZE(wtextW));
981 ok(ret > 0, "MultiByteToWideChar failed\n");
982 }
983 else
984 {
985 /* C locale */
986 const char *p;
987 for (p = text; *p != '\0'; p++)
988 wtextW[ret++] = (unsigned char)*p;
989 wtextW[ret++] = 0;
990 }
991
992 tempfh = fopen(tempfile, "rt");
993 ok(tempfh != NULL, "can't open tempfile\n");
994
995 for (i = 0; i < ret-1; i++)
996 {
997 ch = fgetwc(tempfh);
998 ok(ch == wtextW[i], "got %04hx, expected %04hx (cp%d[%d])\n", ch, wtextW[i], codepage, i);
999 }
1000 ch = fgetwc(tempfh);
1001 ok(ch == WEOF, "got %04hx, expected WEOF (cp%d)\n", ch, codepage);
1002 fclose(tempfh);
1003
1004 tempfh = fopen(tempfile, "wb");
1005 ok(tempfh != NULL, "can't open tempfile\n");
1006 fwrite(wchar_text, 1, sizeof(wchar_text), tempfh);
1007 fclose(tempfh);
1008
1009 tempfh = fopen(tempfile, "rb");
1010 ok(tempfh != NULL, "can't open tempfile\n");
1011 for (i = 0; i < ARRAY_SIZE(wchar_text); i++)
1012 {
1013 ch = fgetwc(tempfh);
1014 ok(ch == wchar_text[i], "got %04hx, expected %04x (cp%d[%d])\n", ch, wchar_text[i], codepage, i);
1015 }
1016 ch = fgetwc(tempfh);
1017 ok(ch == WEOF, "got %04hx, expected WEOF (cp%d)\n", ch, codepage);
1018 fclose(tempfh);
1019 unlink(tempfile);
1020}
1021
1022static void test_fgetwc_unicode(void)
1023{
1024 char temppath[MAX_PATH], tempfile[MAX_PATH];
1025 FILE *tempfh;
1026 static const WCHAR wchar_text[] = { 0xfeff, 0xff1f, '!' };
1027 char utf8_text[BUFSIZ];
1028 int ret, i;
1029 wint_t ch;
1030
1031 GetTempPathA(MAX_PATH, temppath);
1032 GetTempFileNameA(temppath, "", 0, tempfile);
1033
1034 if (!p_fopen_s)
1035 {
1036 win_skip("fopen_s not available\n");
1037 return;
1038 }
1039
1040 tempfh = fopen(tempfile, "wb");
1041 ok(tempfh != NULL, "can't open tempfile\n");
1042 fwrite(wchar_text, 1, sizeof(wchar_text), tempfh);
1043 fclose(tempfh);
1044
1045 tempfh = fopen(tempfile, "rt,ccs=unicode");
1046 ok(tempfh != NULL, "can't open tempfile\n");
1047 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1048 {
1049 ch = fgetwc(tempfh);
1050 ok(ch == wchar_text[i],
1051 "got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
1052 }
1053 ch = fgetwc(tempfh);
1054 ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
1055 fclose(tempfh);
1056
1057 tempfh = fopen(tempfile, "r,ccs=utf-8");
1058 ok(tempfh != NULL, "can't open tempfile\n");
1059 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1060 {
1061 ch = fgetwc(tempfh);
1062 ok(ch == wchar_text[i],
1063 "got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
1064 }
1065 ch = fgetwc(tempfh);
1066 ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
1067 fclose(tempfh);
1068
1069 tempfh = fopen(tempfile, "a,ccs=utf-16le");
1070 ok(tempfh != NULL, "can't open tempfile\n");
1071 ch = fputwc('a', tempfh);
1072 ok(ch == 'a', "fputwc returned %x\n", ch);
1073 fclose(tempfh);
1074
1075 tempfh = fopen(tempfile, "a+,ccs=utf-8");
1076 ok(tempfh != NULL, "can't open tempfile\n");
1077 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1078 {
1079 ch = fgetwc(tempfh);
1080 ok(ch == wchar_text[i],
1081 "got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
1082 }
1083 ch = fgetwc(tempfh);
1084 ok(ch == 'a', "got %04x, expected 'a'\n", ch);
1085 ch = fgetwc(tempfh);
1086 ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
1087 fclose(tempfh);
1088
1089 tempfh = fopen(tempfile, "wb");
1090 ok(tempfh != NULL, "can't open tempfile\n");
1091 ret = WideCharToMultiByte(CP_UTF8, 0, wchar_text, ARRAY_SIZE(wchar_text),
1092 utf8_text, sizeof(utf8_text), NULL, NULL);
1093 ok(ret > 0, "utf-8 conversion failed\n");
1094 fwrite(utf8_text, sizeof(char), ret, tempfh);
1095 fclose(tempfh);
1096
1097 tempfh = fopen(tempfile, "rt, ccs=UTF-8");
1098 ok(tempfh != NULL, "can't open tempfile\n");
1099 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1100 {
1101 ch = fgetwc(tempfh);
1102 ok(ch == wchar_text[i],
1103 "got %04hx, expected %04x (utf8[%d])\n", ch, wchar_text[i], i-1);
1104 }
1105 ch = fgetwc(tempfh);
1106 ok(ch == WEOF, "got %04hx, expected WEOF (utf8)\n", ch);
1107 fclose(tempfh);
1108
1109 tempfh = fopen(tempfile, "wb");
1110 ok(tempfh != NULL, "can't open tempfile\n");
1111 fwrite(wchar_text+1, 1, sizeof(wchar_text)-1, tempfh);
1112 fclose(tempfh);
1113
1114 tempfh = fopen(tempfile, "rt,ccs=utf-16le");
1115 ok(tempfh != NULL, "can't open tempfile\n");
1116 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1117 {
1118 ch = fgetwc(tempfh);
1119 ok(ch == wchar_text[i],
1120 "got %04hx, expected %04x (unicode[%d])\n", ch, wchar_text[i], i-1);
1121 }
1122 ch = fgetwc(tempfh);
1123 ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
1124 fclose(tempfh);
1125
1126 tempfh = fopen(tempfile, "wb");
1127 ok(tempfh != NULL, "can't open tempfile\n");
1128 ret = WideCharToMultiByte(CP_UTF8, 0, wchar_text + 1, ARRAY_SIZE(wchar_text) - 1,
1129 utf8_text, sizeof(utf8_text), NULL, NULL);
1130 ok(ret > 0, "utf-8 conversion failed\n");
1131 utf8_text[ret] = 0;
1132 fwrite(utf8_text, sizeof(char), ret, tempfh);
1133 fclose(tempfh);
1134
1135 tempfh = fopen(tempfile, "rt, ccs=UTF-8");
1136 ok(tempfh != NULL, "can't open tempfile\n");
1137 for (i = 1; i < ARRAY_SIZE(wchar_text); i++)
1138 {
1139 ch = fgetwc(tempfh);
1140 ok(ch == wchar_text[i],
1141 "got %04hx, expected %04x (utf8[%d])\n", ch, wchar_text[i], i-1);
1142 }
1143 ch = fgetwc(tempfh);
1144 ok(ch == WEOF, "got %04hx, expected WEOF (utf8)\n", ch);
1145 fclose(tempfh);
1146
1147 tempfh = fopen(tempfile, "rt, ccs=unicode");
1148 ok(tempfh != NULL, "can't open tempfile\n");
1149 for (i = 0; utf8_text[i]; i++)
1150 {
1151 ch = fgetwc(tempfh);
1152 ok(ch == (unsigned char) utf8_text[i],
1153 "got %04hx, expected %04x (unicode[%d])\n", ch, (unsigned char)utf8_text[i], i);
1154 }
1155 ch = fgetwc(tempfh);
1156 ok(ch == WEOF, "got %04hx, expected WEOF (unicode)\n", ch);
1157 fclose(tempfh);
1158
1159 unlink(temppath);
1160}
1161
1162static void test_fputwc(void)
1163{
1164 char temppath[MAX_PATH];
1165 char tempfile[MAX_PATH];
1166 FILE *f;
1167 char buf[1024];
1168 int ret;
1169
1170 GetTempPathA(MAX_PATH, temppath);
1171 GetTempFileNameA(temppath, "", 0, tempfile);
1172
1173 f = fopen(tempfile, "w");
1174 ret = fputwc('a', f);
1175 ok(ret == 'a', "fputwc returned %x, expected 'a'\n", ret);
1176 ret = fputwc('\n', f);
1177 ok(ret == '\n', "fputwc returned %x, expected '\\n'\n", ret);
1178 fclose(f);
1179
1180 f = fopen(tempfile, "rb");
1181 ret = fread(buf, 1, sizeof(buf), f);
1182 ok(ret == 3, "fread returned %d, expected 3\n", ret);
1183 ok(!memcmp(buf, "a\r\n", 3), "incorrect file data\n");
1184 fclose(f);
1185
1186 if(p_fopen_s) {
1187 f = fopen(tempfile, "w,ccs=unicode");
1188 ret = fputwc('a', f);
1189 ok(ret == 'a', "fputwc returned %x, expected 'a'\n", ret);
1190 ret = fputwc('\n', f);
1191 ok(ret == '\n', "fputwc returned %x, expected '\\n'\n", ret);
1192 fclose(f);
1193
1194 f = fopen(tempfile, "rb");
1195 ret = fread(buf, 1, sizeof(buf), f);
1196 ok(ret == 8, "fread returned %d, expected 8\n", ret);
1197 ok(!memcmp(buf, "\xff\xfe\x61\x00\r\x00\n\x00", 8), "incorrect file data\n");
1198 fclose(f);
1199
1200 f = fopen(tempfile, "w,ccs=utf-8");
1201 ret = fputwc('a', f);
1202 ok(ret == 'a', "fputwc returned %x, expected 'a'\n", ret);
1203 ret = fputwc('\n', f);
1204 ok(ret == '\n', "fputwc returned %x, expected '\\n'\n", ret);
1205 fclose(f);
1206
1207 f = fopen(tempfile, "rb");
1208 ret = fread(buf, 1, sizeof(buf), f);
1209 ok(ret == 6, "fread returned %d, expected 6\n", ret);
1210 ok(!memcmp(buf, "\xef\xbb\xbf\x61\r\n", 6), "incorrect file data\n");
1211 fclose(f);
1212 }else {
1213 win_skip("fputwc tests on unicode files\n");
1214 }
1215
1216 _unlink(tempfile);
1217}
1218
1219static void test_freopen( void )
1220{
1221 char filename1[8] = "AXXXXXX";
1222 char filename2[8] = "BXXXXXX";
1223 FILE *file;
1224 FILE *new;
1225 int ret;
1226 int fd;
1227 char ch;
1228 long pos;
1229
1230 mktemp(filename1);
1232
1233 file = fopen(filename1, "wt");
1234 ok(file != NULL, "fopen(filename1) returned NULL\n");
1235 ret = fwrite("1", 1, 1, file);
1236 ok(ret == 1, "fwrite() returned %d (%d)\n", ret, errno);
1237 ret = fclose(file);
1238 ok(ret == 0, "fclose() returned %d\n", ret);
1239
1240 file = fopen(filename2, "wt");
1241 ok(file != NULL, "fopen(filename1) returned NULL\n");
1242 ret = fwrite("2", 1, 1, file);
1243 ok(ret == 1, "fwrite() returned %d (%d)\n", ret, errno);
1244 ret = fclose(file);
1245 ok(ret == 0, "fclose() returned %d\n", ret);
1246
1247 file = fopen(filename1, "rt");
1248 ok(file != NULL, "fopen(filename1) returned NULL\n");
1249 file = freopen(filename2, "rt", file);
1250 ok(file != NULL, "fopen(filename2) returned NULL\n");
1251 ch = '#';
1252 ret = fread(&ch, 1, 1, file);
1253 ok(ret == 1, "fread() returned %d\n", ret);
1254 ok(ch == '2', "fread() read %c\n", ch);
1255 ret = fclose(file);
1256 ok(ret == 0, "fclose() returned %d\n", ret);
1257
1258 file = fopen(filename1, "at");
1259 ok(file != NULL, "fopen(filename1) returned NULL\n");
1260 file = freopen(filename1, "rt", file);
1261 ok(file != NULL, "fopen(filename1) returned NULL\n");
1262 pos = ftell(file);
1263 ok(pos == 0, "ftell() returned %ld\n", pos);
1264 ch = '#';
1265 ret = fread(&ch, 1, 1, file);
1266 ok(ret == 1, "fread() returned %d\n", ret);
1267 ok(ch == '1', "fread() read %c\n", ch);
1268 ret = fclose(file);
1269 ok(ret == 0, "fclose() returned %d\n", ret);
1270
1271 file = fopen(filename1, "rt");
1272 ok(file != NULL, "fopen(filename1) returned NULL\n");
1273 fd = fileno(file);
1274 ok(fd > 0, "fileno() returned %d\n", fd);
1275 /* invalid filename */
1276 new = freopen("_:", "rt", file);
1277 ok(new == NULL, "fopen(_:) returned non NULL\n");
1278 errno = 0xdeadbeef;
1279 ch = '#';
1280 ret = read(fd, &ch, 1);
1281 ok(ret == -1, "read() returned %d\n", ret);
1282 ok(errno == EBADF, "errno is %d\n", errno);
1283 errno = 0xdeadbeef;
1284 ret = fclose(file);
1285 ok(ret == EOF, "fclose(file) succeeded\n");
1286 ok(errno == 0xdeadbeef, "errno is %d\n", errno);
1287
1288 file = fopen(filename1, "rb");
1289 ok(file != NULL, "couldn't open %s\n", filename1);
1290 close(file->_file);
1291 file->_file = -1;
1292
1293 new = freopen(filename2, "rb", file);
1294 ok(new == file, "freopen() didn't return same FILE*\n");
1295
1296 fd = fileno(file);
1297 ok(fd > 0, "fileno() returned %d\n", fd);
1298
1299 ch = '#';
1300 ret = fread(&ch, 1, 1, file);
1301 ok(ret == 1, "fread() returned %d\n", ret);
1302 ok(ch == '2', "Unexpected char\n");
1303
1304 ret = fclose(file);
1305 ok(ret == 0, "fclose(file) returned %d\n", ret);
1306
1307 unlink(filename1);
1309}
1310
1311static void test_ctrlz( void )
1312{
1313 char* tempf;
1314 FILE *tempfh;
1315 static const char mytext[]= "This is test_ctrlz";
1316 char buffer[256];
1317 int i, j;
1318 LONG l;
1319
1320 tempf=_tempnam(".","wne");
1321 tempfh = fopen(tempf,"wb");
1322 fputs(mytext,tempfh);
1323 j = 0x1a; /* a ctrl-z character signals EOF in text mode */
1324 fputc(j,tempfh);
1325 j = '\r';
1326 fputc(j,tempfh);
1327 j = '\n';
1328 fputc(j,tempfh);
1329 j = 'a';
1330 fputc(j,tempfh);
1331 fclose(tempfh);
1332 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
1333 ok(fgets(buffer,256,tempfh) != 0,"fgets failed unexpected\n");
1334 i=strlen(buffer);
1335 j=strlen(mytext);
1336 ok(i==j, "returned string length expected %d got %d\n", j, i);
1337 j+=4; /* ftell should indicate the true end of file */
1338 l=ftell(tempfh);
1339 ok(l==j, "ftell expected %d got %ld\n", j, l);
1340 ok(feof(tempfh), "did not get EOF\n");
1341 fclose(tempfh);
1342
1343 tempfh = fopen(tempf,"rb"); /* open in BINARY mode */
1344 ok(fgets(buffer,256,tempfh) != 0,"fgets failed unexpected\n");
1345 i=strlen(buffer);
1346 j=strlen(mytext)+3; /* should get through newline */
1347 ok(i==j, "returned string length expected %d got %d\n", j, i);
1348 l=ftell(tempfh);
1349 ok(l==j, "ftell expected %d got %ld\n", j, l);
1350 ok(fgets(buffer,256,tempfh) != 0,"fgets failed unexpected\n");
1351 i=strlen(buffer);
1352 ok(i==1, "returned string length expected %d got %d\n", 1, i);
1353 ok(feof(tempfh), "did not get EOF\n");
1354 fclose(tempfh);
1355 unlink(tempf);
1356 free(tempf);
1357}
1358
1359static void test_file_put_get( void )
1360{
1361 char* tempf;
1362 FILE *tempfh;
1363 static const char mytext[]= "This is a test_file_put_get\n";
1364 static const char dostext[]= "This is a test_file_put_get\r\n";
1365 char btext[LLEN];
1366 WCHAR wtextW[LLEN+1];
1367 WCHAR *mytextW = NULL, *aptr, *wptr;
1368 BOOL diff_found = FALSE;
1369 unsigned int i;
1370
1371 tempf=_tempnam(".","wne");
1372 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
1373 fputs(mytext,tempfh);
1374 fclose(tempfh);
1375 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
1376 fgets(btext,LLEN,tempfh);
1377 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
1378 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
1379 fclose(tempfh);
1380 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
1381 fputs(dostext,tempfh);
1382 fclose(tempfh);
1383 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
1384 fgets(btext,LLEN,tempfh);
1385 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
1386 fclose(tempfh);
1387 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
1388 fgets(btext,LLEN,tempfh);
1389 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
1390
1391 fclose(tempfh);
1392 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
1393 fgetws(wtextW,LLEN,tempfh);
1394 mytextW = AtoW (mytext);
1395 aptr = mytextW;
1396 wptr = wtextW;
1397
1398 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
1399 {
1400 diff_found |= (*aptr != *wptr);
1401 }
1402 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
1403 free(mytextW);
1404 fclose(tempfh);
1405 unlink(tempf);
1406 free(tempf);
1407}
1408
1409static void test_file_write_read( void )
1410{
1411 char* tempf;
1412 int tempfd;
1413 static const char mytext[]= "This is test_file_write_read\nsecond line\n";
1414 static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
1415 char btext[LLEN];
1416 int ret, i;
1417
1418 tempf=_tempnam(".","wne");
1419 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,
1421 ok( tempfd != -1,
1422 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
1423 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
1424 "_write _O_BINARY bad return value\n");
1425 _close(tempfd);
1426 i = lstrlenA(mytext);
1427 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
1428 ok(_read(tempfd,btext,i) == i,
1429 "_read _O_BINARY got bad length\n");
1430 ok( memcmp(dostext,btext,i) == 0,
1431 "problems with _O_BINARY _write / _read\n");
1432 _close(tempfd);
1433 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1434 ok(_read(tempfd,btext,i) == i-1,
1435 "_read _O_TEXT got bad length\n");
1436 ok( memcmp(mytext,btext,i-1) == 0,
1437 "problems with _O_BINARY _write / _O_TEXT _read\n");
1438 _close(tempfd);
1439 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
1441 ok( tempfd != -1,
1442 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
1443 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
1444 "_write _O_TEXT bad return value\n");
1445 _close(tempfd);
1446 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
1447 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
1448 "_read _O_BINARY got bad length\n");
1449 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
1450 "problems with _O_TEXT _write / _O_BINARY _read\n");
1451 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
1452 _close(tempfd);
1453 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1454 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
1455 "_read _O_TEXT got bad length\n");
1456 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
1457 "problems with _O_TEXT _write / _read\n");
1458 _close(tempfd);
1459
1460 memset(btext, 0, LLEN);
1461 tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
1462 ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
1463 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
1464 ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
1465 _close(tempfd);
1466
1467 /* Test reading only \n or \r */
1468 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1469 _lseek(tempfd, -1, FILE_END);
1470 ret = _read(tempfd,btext,LLEN);
1471 ok(ret == 1 && *btext == '\n', "_read expected 1 got bad length: %d\n", ret);
1472 _lseek(tempfd, -2, FILE_END);
1473 ret = _read(tempfd,btext,LLEN);
1474 ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
1475 _lseek(tempfd, -2, FILE_END);
1476 ret = _read(tempfd,btext,1);
1477 ok(ret == 1 && *btext == '\n', "_read returned %d, buf: %d\n", ret, *btext);
1478 ret = read(tempfd,btext,1);
1479 ok(ret == 0, "_read returned %d, expected 0\n", ret);
1480 _lseek(tempfd, -3, FILE_END);
1481 ret = _read(tempfd,btext,1);
1482 ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
1483 ok(tell(tempfd) == 41, "bad position %lu expecting 41\n", tell(tempfd));
1484 _lseek(tempfd, -3, FILE_END);
1485 ret = _read(tempfd,btext,2);
1486 ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
1487 ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
1488 _lseek(tempfd, -3, FILE_END);
1489 ret = _read(tempfd,btext,3);
1490 ok(ret == 2 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
1491 ok(tell(tempfd) == 43, "bad position %lu expecting 43\n", tell(tempfd));
1492 _close(tempfd);
1493
1494 ret = unlink(tempf);
1495 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
1496 free(tempf);
1497
1498 tempf=_tempnam(".","wne");
1500 ok( tempfd != -1,
1501 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
1502 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
1503 "_write _O_BINARY bad return value\n");
1504 _close(tempfd);
1505 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
1506 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
1507 "_read _O_BINARY got bad length\n");
1508 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
1509 "problems with _O_BINARY _write / _read\n");
1510 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
1511 _close(tempfd);
1512 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1513 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
1514 "_read _O_TEXT got bad length\n");
1515 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
1516 "problems with _O_BINARY _write / _O_TEXT _read\n");
1517 _close(tempfd);
1518
1519 /* test _read with single bytes. CR should be skipped and LF pulled in */
1520 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1521 for (i=0; i<strlen(mytext); i++) /* */
1522 {
1523 _read(tempfd,btext, 1);
1524 ok(btext[0] == mytext[i],"_read failed at pos %d 0x%02x vs 0x%02x\n", i, btext[0], mytext[i]);
1525 }
1526 while (_read(tempfd,btext, 1));
1527 _close(tempfd);
1528
1529 /* test _read in buffered mode. Last CR should be skipped but LF not pulled in */
1530 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
1531 i = _read(tempfd,btext, strlen(mytext));
1532 ok(i == strlen(mytext)-1, "_read_i %d\n", i);
1533 _close(tempfd);
1534
1535 /* test read/write in unicode mode */
1536 if(p_fopen_s)
1537 {
1539 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1540 ret = _write(tempfd, "a", 1);
1541 ok(ret == -1, "_write returned %d, expected -1\n", ret);
1542 ret = _write(tempfd, "a\x00\n\x00\xff\xff", 6);
1543 ok(ret == 6, "_write returned %d, expected 6\n", ret);
1544 _close(tempfd);
1545
1546 tempfd = _open(tempf, _O_RDONLY|_O_BINARY, 0);
1547 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1548 ret = _read(tempfd, btext, sizeof(btext));
1549 ok(ret == 10, "_read returned %d, expected 10\n", ret);
1550 ok(!memcmp(btext, "\xff\xfe\x61\x00\r\x00\n\x00\xff\xff", 10), "btext is incorrect\n");
1551 _close(tempfd);
1552
1553 tempfd = _open(tempf, _O_RDONLY|_O_WTEXT, 0);
1554 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1555 errno = 0xdeadbeef;
1556 ret = _read(tempfd, btext, 3);
1557 ok(ret == -1, "_read returned %d, expected -1\n", ret);
1558 ok(errno == 22, "errno = %d\n", errno);
1559 ret = _read(tempfd, btext, sizeof(btext));
1560 ok(ret == 6, "_read returned %d, expected 6\n", ret);
1561 ok(!memcmp(btext, "\x61\x00\n\x00\xff\xff", 6), "btext is incorrect\n");
1562 _close(tempfd);
1563
1565 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1566 errno = 0xdeadbeef;
1567 ret = _write(tempfd, "a", 1);
1568 ok(ret == -1, "_write returned %d, expected -1\n", ret);
1569 ok(errno == 22, "errno = %d\n", errno);
1570 ret = _write(tempfd, "a\x00\n\x00\x62\x00", 6);
1571 ok(ret == 6, "_write returned %d, expected 6\n", ret);
1572 _close(tempfd);
1573
1574 tempfd = _open(tempf, _O_RDONLY|_O_BINARY, 0);
1575 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1576 ret = _read(tempfd, btext, sizeof(btext));
1577 ok(ret == 7, "_read returned %d, expected 7\n", ret);
1578 ok(!memcmp(btext, "\xef\xbb\xbf\x61\r\n\x62", 7), "btext is incorrect\n");
1579 _close(tempfd);
1580
1581 tempfd = _open(tempf, _O_RDONLY|_O_WTEXT, 0);
1582 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1583 ret = _read(tempfd, btext, sizeof(btext));
1584 ok(ret == 6, "_read returned %d, expected 6\n", ret);
1585 ok(!memcmp(btext, "\x61\x00\n\x00\x62\x00", 6), "btext is incorrect\n");
1586
1587 /* when buffer is small read sometimes fails in native implementation */
1588 lseek(tempfd, 3 /* skip bom */, SEEK_SET);
1589 ret = _read(tempfd, btext, 4);
1590 todo_wine ok(ret == -1, "_read returned %d, expected -1\n", ret);
1591
1592 lseek(tempfd, 6, SEEK_SET);
1593 ret = _read(tempfd, btext, 2);
1594 ok(ret == 2, "_read returned %d, expected 2\n", ret);
1595 ok(!memcmp(btext, "\x62\x00", 2), "btext is incorrect\n");
1596 _close(tempfd);
1597
1599 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1600 ret = _write(tempfd, "\xef\xbb\xbf\x61\xc4\x85\x62\xc5\xbc\r\r\n", 12);
1601 ok(ret == 12, "_write returned %d, expected 9\n", ret);
1602 _close(tempfd);
1603
1604 tempfd = _open(tempf, _O_RDONLY|_O_WTEXT, 0);
1605 ok(tempfd != -1, "_open failed with error: %d\n", errno);
1606 ret = _read(tempfd, btext, sizeof(btext));
1607 ok(ret == 12, "_read returned %d, expected 12\n", ret);
1608 ok(!memcmp(btext, "\x61\x00\x05\x01\x62\x00\x7c\x01\x0d\x00\x0a\x00", 12), "btext is incorrect\n");
1609
1610 /* test invalid utf8 sequence */
1611 lseek(tempfd, 5, SEEK_SET);
1612 ret = _read(tempfd, btext, sizeof(btext));
1613 ok(ret == 10, "_read returned %d, expected 10\n", ret);
1614 /* invalid char should be replaced by U+FFFD in MultiByteToWideChar */
1615 ok(!memcmp(btext, "\xfd\xff", 2), "invalid UTF8 character was not replaced by U+FFFD\n");
1616 ok(!memcmp(btext+ret-8, "\x62\x00\x7c\x01\x0d\x00\x0a\x00", 8), "btext is incorrect\n");
1617 _close(tempfd);
1618 }
1619 else
1620 {
1621 win_skip("unicode mode tests on file\n");
1622 }
1623
1624 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
1625 ok( ret == 0,
1626 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
1627 ret = unlink(tempf);
1628 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
1629 free(tempf);
1630}
1631
1632static void test_file_inherit_child(const char* fd_s, const char *handle_str)
1633{
1634 HANDLE handle_value;
1635 int fd = atoi(fd_s);
1636 HANDLE *handle_ptr;
1637 unsigned int count;
1638 char buffer[32];
1639 STARTUPINFOA si;
1640 int ret;
1641
1642 GetStartupInfoA(&si);
1643 count = *(unsigned *)si.lpReserved2;
1644 if (handle_str)
1645 {
1646 ok(count == 3, "Got unexpected count %u.\n", count);
1647 sscanf(handle_str, "%p", &handle_value);
1648 handle_ptr = (HANDLE *)(si.lpReserved2 + sizeof(unsigned) + count);
1649 ok(handle_value == handle_ptr[1], "Got unexpected handle %p.\n", handle_ptr[1]);
1650 }
1651
1652 ret = write(fd, "Success", 8);
1653 ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
1654 lseek(fd, 0, SEEK_SET);
1655 ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
1656 ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
1657}
1658
1659static void test_file_inherit_child_no(const char* fd_s)
1660{
1661 int fd = atoi(fd_s);
1662 int ret;
1663
1664 ret = write(fd, "Success", 8);
1665 ok( ret == -1 && errno == EBADF,
1666 "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
1667}
1668
1670{
1671 static BYTE block[1024];
1672 BYTE *wxflag_ptr;
1673 HANDLE *handle_ptr;
1674 unsigned int i;
1675
1676 startup->lpReserved2 = block;
1677 startup->cbReserved2 = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * count;
1678 wxflag_ptr = block + sizeof(unsigned);
1679 handle_ptr = (HANDLE *)(wxflag_ptr + count);
1680
1681 *(unsigned*)block = count;
1682 for (i = 0; i < count; i++)
1683 {
1684 wxflag_ptr[i] = 0x81;
1685 handle_ptr[i] = handles[i];
1686 }
1687}
1688
1689static const char *read_file( HANDLE file )
1690{
1691 static char buffer[128];
1692 DWORD ret;
1694 if (!ReadFile( file, buffer, sizeof(buffer) - 1, &ret, NULL)) ret = 0;
1695 buffer[ret] = 0;
1696 return buffer;
1697}
1698
1699static void test_stdout_handle( STARTUPINFOA *startup, char *cmdline, HANDLE hstdout, BOOL expect_stdout,
1700 const char *descr )
1701{
1702 const char *data;
1703 HANDLE hErrorFile;
1706
1707 /* make file handle inheritable */
1708 sa.nLength = sizeof(sa);
1709 sa.lpSecurityDescriptor = NULL;
1710 sa.bInheritHandle = TRUE;
1711
1712 hErrorFile = CreateFileA( "fdopen.err", GENERIC_READ|GENERIC_WRITE,
1714 startup->dwFlags = STARTF_USESTDHANDLES;
1715 startup->hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1716 startup->hStdOutput = hErrorFile;
1717 startup->hStdError = GetStdHandle( STD_ERROR_HANDLE );
1718
1721 wait_child_process( proc.hProcess );
1722
1723 data = read_file( hErrorFile );
1724 if (expect_stdout)
1725 ok( strcmp( data, "Success" ), "%s: Error file shouldn't contain data\n", descr );
1726 else
1727 ok( !strcmp( data, "Success" ), "%s: Wrong error data (%s)\n", descr, data );
1728
1729 if (hstdout)
1730 {
1731 data = read_file( hstdout );
1732 if (expect_stdout)
1733 ok( !strcmp( data, "Success" ), "%s: Wrong stdout data (%s)\n", descr, data );
1734 else
1735 ok( strcmp( data, "Success" ), "%s: Stdout file shouldn't contain data\n", descr );
1736 }
1737
1738 CloseHandle( hErrorFile );
1739 DeleteFileA( "fdopen.err" );
1740}
1741
1742static unsigned WINAPI read_pipe_thread(void *argument)
1743{
1744 unsigned char buffer[2];
1745 int ret;
1746 int *pipefds = argument;
1747
1748 ret = _read(pipefds[0], buffer, sizeof(buffer));
1749 ok(ret == 1, "ret = %d\n", ret);
1750 ok(buffer[0] == 'a', "%x\n", buffer[0]);
1751 return 0;
1752}
1753
1754static void test_file_inherit( const char* selfname )
1755{
1756 int fd;
1757 const char* arg_v[5];
1758 char buffer[16];
1759 char cmdline[MAX_PATH];
1762 HANDLE handles[3];
1763 HANDLE thread_handle;
1764 int pipefds[2];
1765 intptr_t ret;
1766
1767 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
1768 ok(fd != -1, "Couldn't create test file\n");
1769 arg_v[0] = get_base_name(selfname);
1770 arg_v[1] = "file";
1771 arg_v[2] = "inherit";
1772 arg_v[3] = buffer; sprintf(buffer, "%d", fd);
1773 arg_v[4] = 0;
1774 ret = _spawnvp(_P_WAIT, selfname, arg_v);
1775 ok(ret == 0, "_spawnvp returned %Id, errno %d\n", ret, errno);
1776 ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
1777 lseek(fd, 0, SEEK_SET);
1778 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
1779 close (fd);
1780 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
1781
1782 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
1783 ok(fd != -1, "Couldn't create test file\n");
1784 arg_v[1] = "file";
1785 arg_v[2] = "inherit_no";
1786 arg_v[3] = buffer; sprintf(buffer, "%d", fd);
1787 arg_v[4] = 0;
1788 ret = _spawnvp(_P_WAIT, selfname, arg_v);
1789 ok(ret == 0, "_spawnvp returned %Id, errno %d\n", ret, errno);
1790 ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
1791 ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
1792 close (fd);
1793 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
1794
1795 /* Show that spawn works while a read is active */
1796 ok(_pipe(pipefds, 1, O_BINARY) == 0, "_pipe() failed\n");
1797 thread_handle = (HANDLE)_beginthreadex(NULL, 0, read_pipe_thread, pipefds, 0, NULL);
1798 Sleep(100); /* try to make sure the thread is reading */
1799 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
1800 ok(fd != -1, "Couldn't create test file\n");
1801 arg_v[1] = "tests/file.c";
1802 arg_v[2] = "inherit";
1803 arg_v[3] = buffer; sprintf(buffer, "%d", fd);
1804 arg_v[4] = 0;
1805 ret = _spawnvp(_P_WAIT, selfname, arg_v);
1806 ok(ret == 0, "_spawnvp returned %Id, errno %d\n", ret, errno);
1807 ret = tell(fd);
1808 ok(ret == 8, "bad position %Iu expecting 8\n", ret);
1809 lseek(fd, 0, SEEK_SET);
1810 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
1811 close (fd);
1812 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
1813 _write(pipefds[1], "a", 1);
1814 WaitForSingleObject(thread_handle, INFINITE);
1815 CloseHandle(thread_handle);
1816 close(pipefds[0]);
1817 close(pipefds[1]);
1818
1819 /* make file handle inheritable */
1820 sa.nLength = sizeof(sa);
1821 sa.lpSecurityDescriptor = NULL;
1822 sa.bInheritHandle = TRUE;
1823 sprintf(cmdline, "%s file inherit 1", selfname);
1824
1825 /* init an empty Reserved2, which should not be recognized as inherit-block */
1826 ZeroMemory(&startup, sizeof(startup));
1827 startup.cb = sizeof(startup);
1829 test_stdout_handle( &startup, cmdline, 0, FALSE, "empty block" );
1830
1831 /* test with valid inheritblock */
1833 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1837 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "valid block" );
1838 CloseHandle( handles[1] );
1839 DeleteFileA("fdopen.tst");
1840
1841 /* test inherit block starting with unsigned zero */
1842 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1845 *(unsigned int *)startup.lpReserved2 = 0;
1846 test_stdout_handle( &startup, cmdline, handles[1], FALSE, "zero count block" );
1847 CloseHandle( handles[1] );
1848 DeleteFileA("fdopen.tst");
1849
1850 /* test inherit block with smaller size */
1851 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1854 startup.cbReserved2 -= 3;
1855 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "small size block" );
1856 CloseHandle( handles[1] );
1857 DeleteFileA("fdopen.tst");
1858
1859 /* test inherit block with even smaller size */
1860 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1863 startup.cbReserved2 = sizeof(unsigned int) + sizeof(HANDLE) + sizeof(char);
1864 test_stdout_handle( &startup, cmdline, handles[1], FALSE, "smaller size block" );
1865 CloseHandle( handles[1] );
1866 DeleteFileA("fdopen.tst");
1867
1868 /* test inherit block with larger size */
1869 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1872 startup.cbReserved2 += 7;
1873 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "large size block" );
1874 CloseHandle( handles[1] );
1875 DeleteFileA("fdopen.tst");
1876
1877 /* test inherit block with invalid handle */
1880 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1881 test_stdout_handle( &startup, cmdline, NULL, FALSE, "INVALID_HANDLE_VALUE stdout handle" );
1882
1883 handles[1] = NULL;
1885 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1886 test_stdout_handle( &startup, cmdline, NULL, FALSE, "NULL stdout handle" );
1887
1888 handles[1] = (void *)0xdeadbeef;
1890 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1891 test_stdout_handle( &startup, cmdline, NULL, FALSE, "invalid stdout handle" );
1892}
1893
1894static void test_invalid_stdin_child( void )
1895{
1896 HANDLE handle;
1897 ioinfo *info;
1898 int ret;
1899 char c;
1900
1901 errno = 0xdeadbeef;
1903 ok(handle == (HANDLE)-2, "handle = %p\n", handle);
1904 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
1906 ok((LONG_PTR)handle > 0, "Expecting passed handle to be untouched\n");
1907
1909 ok(info->handle == (HANDLE)-2, "info->handle = %p\n", info->handle);
1910 ok(info->wxflag == 0xc1, "info->wxflag = %x\n", info->wxflag);
1911
1912 ok(stdin->_file == -2, "stdin->_file = %d\n", stdin->_file);
1913
1914 errno = 0xdeadbeef;
1915 ret = fread(&c, 1, 1, stdin);
1916 ok(!ret, "fread(stdin) returned %d\n", ret);
1917 ok(errno == EBADF, "errno = %d\n", errno);
1918
1919 errno = 0xdeadbeef;
1920 ret = read(-2, &c, 1);
1921 ok(ret == -1, "read(-2) returned %d\n", ret);
1922 ok(errno == EBADF, "errno = %d\n", errno);
1923
1924 errno = 0xdeadbeef;
1925 ret = read(STDIN_FILENO, &c, 1);
1926 ok(ret == -1, "read(STDIN_FILENO) returned %d\n", ret);
1927 ok(errno == EBADF, "errno = %d\n", errno);
1928
1929 errno = 0xdeadbeef;
1930 ret = _flsbuf('a', stdin);
1931 ok(ret == EOF, "_flsbuf(stdin) returned %d\n", ret);
1932 ok(errno == EBADF, "errno = %d\n", errno);
1933
1934 errno = 0xdeadbeef;
1935 ret = fwrite(&c, 1, 1, stdin);
1936 ok(!ret, "fwrite(stdin) returned %d\n", ret);
1937 ok(errno == EBADF, "errno = %d\n", errno);
1938
1939 errno = 0xdeadbeef;
1940 ret = write(-2, &c, 1);
1941 ok(ret == -1, "write(-2) returned %d\n", ret);
1942 ok(errno == EBADF, "errno = %d\n", errno);
1943
1944 errno = 0xdeadbeef;
1945 ret = write(STDIN_FILENO, &c, 1);
1946 ok(ret == -1, "write(STDIN_FILENO) returned %d\n", ret);
1947 ok(errno == EBADF, "errno = %d\n", errno);
1948
1949 errno = 0xdeadbeef;
1950 ret = fclose(stdin);
1951 ok(ret == -1, "fclose(stdin) returned %d\n", ret);
1952 ok(errno == EBADF, "errno = %d\n", errno);
1953
1954 errno = 0xdeadbeef;
1955 ret = close(-2);
1956 ok(ret == -1, "close(-2) returned %d\n", ret);
1957 ok(errno == EBADF, "errno = %d\n", errno);
1958
1959 errno = 0xdeadbeef;
1961 ok(ret==-1 || !ret, "close(STDIN_FILENO) returned %d\n", ret);
1962 ok((ret==-1 && errno==EBADF) || (!ret && errno==0xdeadbeef), "errno = %d\n", errno);
1963}
1964
1965static void test_invalid_stdin( const char* selfname )
1966{
1967 char cmdline[MAX_PATH];
1971 HKEY key;
1972 LONG ret;
1973
1974 if(!p_fopen_s) {
1975 /* Behaviour of the dll has changed in newer version */
1976 win_skip("skipping invalid stdin tests\n");
1977 return;
1978 }
1979
1981 ok(!ret, "RegOpenCurrentUser failed: %lx\n", ret);
1982
1985 ok(ret, "DuplicateHandle failed: %lx\n", GetLastError());
1986
1987 sa.nLength = sizeof(sa);
1988 sa.lpSecurityDescriptor = NULL;
1989 sa.bInheritHandle = TRUE;
1990
1991 memset(&startup, 0, sizeof(startup));
1992 startup.cb = sizeof(startup);
1993 startup.dwFlags = STARTF_USESTDHANDLES;
1994 startup.hStdInput = key;
1997
1998 sprintf(cmdline, "%s file stdin", selfname);
2001 wait_child_process(proc.hProcess);
2002
2003 ret = RegCloseKey(key);
2004 ok(!ret, "RegCloseKey failed: %lx\n", ret);
2005}
2006
2007static void test_tmpnam( void )
2008{
2009 char name[MAX_PATH] = "abc";
2010 char *res;
2011
2012 res = tmpnam(NULL);
2013 ok(res != NULL, "tmpnam returned NULL\n");
2014 ok(res[0] == '\\', "first character is not a backslash\n");
2015 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
2016 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
2017
2018 res = tmpnam(name);
2019 ok(res != NULL, "tmpnam returned NULL\n");
2020 ok(res == name, "supplied buffer was not used\n");
2021 ok(res[0] == '\\', "first character is not a backslash\n");
2022 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
2023 ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
2024}
2025
2026static void test_chsize( void )
2027{
2028 int fd;
2029 LONG cur, pos, count;
2030 char temptext[] = "012345678";
2031 char *tempfile = _tempnam( ".", "tst" );
2032
2033 ok( tempfile != NULL, "Couldn't create test file\n" );
2034
2036 ok( fd > 0, "Couldn't open test file\n" );
2037
2038 count = _write( fd, temptext, sizeof(temptext) );
2039 ok( count > 0, "Couldn't write to test file\n" );
2040
2041 /* get current file pointer */
2042 cur = _lseek( fd, 0, SEEK_CUR );
2043
2044 /* make the file smaller */
2045 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
2046
2047 pos = _lseek( fd, 0, SEEK_CUR );
2048 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
2049 ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
2050
2051 /* enlarge the file */
2052 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
2053
2054 pos = _lseek( fd, 0, SEEK_CUR );
2055 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
2056 ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
2057
2058 _close( fd );
2059 _unlink( tempfile );
2060 free( tempfile );
2061}
2062
2064{
2065 char fname1[] = "empty1";
2066 char fname2[] = "empty2";
2067 char fname3[] = "empty3";
2068 FILE *stream1, *stream2, *stream3, *stream4;
2069 int ret, numclosed;
2070
2071 /* testing fopen() */
2072 stream1 = fopen(fname1, "w+");
2073 ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
2074 stream2 = fopen(fname2, "w ");
2075 ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
2076 _unlink(fname3);
2077 stream3 = fopen(fname3, "r");
2078 ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
2079 stream3 = fopen(fname3, "w+");
2080 ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
2081 errno = 0xfaceabad;
2082 stream4 = fopen("", "w+");
2083 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
2084 "filename is empty, errno = %d (expected 2 or 22)\n", errno);
2085 errno = 0xfaceabad;
2086 stream4 = fopen(NULL, "w+");
2087 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
2088 "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
2089
2090 /* testing fclose() */
2091 ret = fclose(stream2);
2092 ok(ret == 0, "The file '%s' was not closed\n", fname2);
2093 ret = fclose(stream3);
2094 ok(ret == 0, "The file '%s' was not closed\n", fname3);
2095 errno = 0xdeadbeef;
2096 ret = fclose(stream2);
2097 ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
2098 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2099 ret = fclose(stream3);
2100 ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
2101 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2103 ret = fclose(NULL);
2104 ok(ret == EOF, "Closing NULL file returned %d\n", ret);
2105 ok(errno == EINVAL, "errno = %d\n", errno);
2106 }
2107
2108 /* testing fcloseall() */
2109 numclosed = _fcloseall();
2110 /* fname1 should be closed here */
2111 ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
2112 numclosed = _fcloseall();
2113 ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
2114
2115 ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
2116 ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
2117 ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
2118}
2119
2120static void test_fopen_s( void )
2121{
2122 const char name[] = "empty1";
2123 char buff[16];
2124 unsigned char *ubuff = (unsigned char*)buff;
2125 FILE *file, *file2;
2126 int ret;
2127 int len;
2128
2129 if (!p_fopen_s)
2130 {
2131 win_skip("Skipping fopen_s test\n");
2132 return;
2133 }
2134 /* testing fopen_s */
2135 ret = p_fopen_s(&file, name, "w");
2136 ok(ret == 0, "fopen_s failed with %d\n", ret);
2137 ok(file != 0, "fopen_s failed to return value\n");
2138 fwrite(name, sizeof(name), 1, file);
2139
2140 ret = fclose(file);
2141 ok(ret != EOF, "File failed to close\n");
2142
2143 file = fopen(name, "r");
2144 ok(file != 0, "fopen failed\n");
2145 len = fread(buff, 1, sizeof(name), file);
2146 ok(len == sizeof(name), "File length is %d\n", len);
2147 buff[sizeof(name)] = '\0';
2148 ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
2149
2150 ret = fclose(file);
2151 ok(ret != EOF, "File failed to close\n");
2152
2153 ret = p_fopen_s(&file, name, "w, ccs=UNIcode");
2154 ok(ret == 0, "fopen_s failed with %d\n", ret);
2155 ret = fwrite("a", 1, 2, file);
2156 ok(ret == 2, "fwrite returned %d\n", ret);
2157 fclose(file);
2158
2159 ret = p_fopen_s(&file, name, "r");
2160 ok(ret == 0, "fopen_s failed with %d\n", ret);
2161 len = fread(buff, 1, 2, file);
2162 ok(len == 2, "len = %d\n", len);
2163 ok(ubuff[0]==0xff && ubuff[1]==0xfe, "buff[0]=%02x, buff[1]=%02x\n",
2164 ubuff[0], ubuff[1]);
2165 fclose(file);
2166
2167 ret = p_fopen_s(&file, name, "r,ccs=unicode");
2168 ok(ret == 0, "fopen_s failed with %d\n", ret);
2169 len = fread(buff, 1, 2, file);
2170 ok(len == 2, "len = %d\n", len);
2171 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2172 ubuff[0], ubuff[1]);
2173 fclose(file);
2174
2175 ret = p_fopen_s(&file, name, "r,ccs=utf-16le");
2176 ok(ret == 0, "fopen_s failed with %d\n", ret);
2177 len = fread(buff, 1, 2, file);
2178 ok(len == 2, "len = %d\n", len);
2179 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2180 ubuff[0], ubuff[1]);
2181 fclose(file);
2182
2183 ret = p_fopen_s(&file, name, "r,ccs=utf-8");
2184 ok(ret == 0, "fopen_s failed with %d\n", ret);
2185 len = fread(buff, 1, 2, file);
2186 ok(len == 2, "len = %d\n", len);
2187 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2188 ubuff[0], ubuff[1]);
2189 fclose(file);
2190
2191 ret = p_fopen_s(&file, name, "w,ccs=utf-16le");
2192 ok(ret == 0, "fopen_s failed with %d\n", ret);
2193 fclose(file);
2194
2195 ret = p_fopen_s(&file, name, "r");
2196 ok(ret == 0, "fopen_s failed with %d\n", ret);
2197 len = fread(buff, 1, 3, file);
2198 ok(len == 2, "len = %d\n", len);
2199 ok(ubuff[0]==0xff && ubuff[1]==0xfe, "buff[0]=%02x, buff[1]=%02x\n",
2200 ubuff[0], ubuff[1]);
2201 fclose(file);
2202
2203 ret = p_fopen_s(&file, name, "w,ccs=utf-8");
2204 ok(ret == 0, "fopen_s failed with %d\n", ret);
2205 fclose(file);
2206
2207 ret = p_fopen_s(&file, name, "r");
2208 ok(ret == 0, "fopen_s failed with %d\n", ret);
2209 len = fread(buff, 1, 4, file);
2210 ok(len == 3, "len = %d\n", len);
2211 ok(ubuff[0]==0xef && ubuff[1]==0xbb && ubuff[2]==0xbf,
2212 "buff[0]=%02x, buff[1]=%02x, buff[2]=%02x\n",
2213 ubuff[0], ubuff[1], ubuff[2]);
2214 fclose(file);
2215
2216 /* test initial FILE values */
2217 memset(file, 0xfe, sizeof(*file));
2218 file->_flag = 0;
2219 ret = p_fopen_s(&file2, name, "r");
2220 ok(!ret, "fopen_s failed with %d\n", ret);
2221 ok(file == file2, "file != file2 %p %p\n", file, file2);
2222 ok(!file->_ptr, "file->_ptr != NULL\n");
2223 ok(!file->_cnt, "file->_cnt != 0\n");
2224 ok(!file->_base, "file->_base != NULL\n");
2225 ok(file->_flag == 1, "file->_flag = %x\n", file->_flag);
2226 ok(file->_file, "file->_file == 0\n");
2227 ok(file->_charbuf == 0xfefefefe, "file->_charbuf = %x\n", file->_charbuf);
2228 ok(file->_bufsiz == 0xfefefefe, "file->_bufsiz = %x\n", file->_bufsiz);
2229 ok(!file->_tmpfname, "file->_tmpfname != NULL\n");
2230 fclose(file2);
2231
2232 ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
2233}
2234
2235static void test__wfopen_s( void )
2236{
2237 const char name[] = "empty1";
2238 char buff[16];
2239 FILE *file;
2240 int ret;
2241 int len;
2242
2243 if (!p__wfopen_s)
2244 {
2245 win_skip("Skipping _wfopen_s test\n");
2246 return;
2247 }
2248 /* testing _wfopen_s */
2249 ret = p__wfopen_s(&file, L"empty1", L"w");
2250 ok(ret == 0, "_wfopen_s failed with %d\n", ret);
2251 ok(file != 0, "_wfopen_s failed to return value\n");
2252 fwrite(name, sizeof(name), 1, file);
2253
2254 ret = fclose(file);
2255 ok(ret != EOF, "File failed to close\n");
2256
2257 file = fopen(name, "r");
2258 ok(file != 0, "fopen failed\n");
2259 len = fread(buff, 1, sizeof(name), file);
2260 ok(len == sizeof(name), "File length is %d\n", len);
2261 buff[sizeof(name)] = '\0';
2262 ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
2263
2264 ret = fclose(file);
2265 ok(ret != EOF, "File failed to close\n");
2266
2267 ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
2268}
2269
2270static void test_setmode(void)
2271{
2272 const char name[] = "empty1";
2273 int fd, ret;
2274
2275 if(!p_fopen_s) {
2276 win_skip("unicode file modes are not available, skipping setmode tests\n");
2277 return;
2278 }
2279
2280 errno = 0xdeadbeef;
2281 ret = _setmode(-2, 0);
2282 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2283 ok(errno == EINVAL, "errno = %d\n", errno);
2284
2285 errno = 0xdeadbeef;
2286 ret = _setmode(-2, _O_TEXT);
2287 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2288 ok(errno == EBADF, "errno = %d\n", errno);
2289
2291 ok(fd != -1, "failed to open file\n");
2292
2293 errno = 0xdeadbeef;
2294 ret = _setmode(fd, 0xffffffff);
2295 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2296 ok(errno == EINVAL, "errno = %d\n", errno);
2297
2298 errno = 0xdeadbeef;
2299 ret = _setmode(fd, 0);
2300 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2301 ok(errno == EINVAL, "errno = %d\n", errno);
2302
2303 errno = 0xdeadbeef;
2305 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2306 ok(errno == EINVAL, "errno = %d\n", errno);
2307
2308 errno = 0xdeadbeef;
2310 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2311 ok(errno == EINVAL, "errno = %d\n", errno);
2312
2314 ok(ret == _O_TEXT, "_setmode returned %x, expected _O_TEXT\n", ret);
2315
2316 ret = _setmode(fd, _O_WTEXT);
2317 ok(ret == _O_BINARY, "_setmode returned %x, expected _O_BINARY\n", ret);
2318
2319 ret = _setmode(fd, _O_TEXT);
2320 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2321
2323 ok(ret == _O_TEXT, "_setmode returned %x, expected _O_TEXT\n", ret);
2324
2326 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2327
2328 ret = _setmode(fd, _O_TEXT);
2329 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2330
2331 _close(fd);
2332 _unlink(name);
2333}
2334
2335static void test_get_osfhandle(void)
2336{
2337 int fd;
2338 char fname[] = "t_get_osfhanle";
2339 DWORD bytes_written;
2340 HANDLE handle;
2341
2344 WriteFile(handle, "bar", 3, &bytes_written, NULL);
2345 _close(fd);
2346 fd = _open(fname, _O_RDONLY, 0);
2347 ok(fd != -1, "Couldn't open '%s' after _get_osfhandle()\n", fname);
2348
2349 _close(fd);
2350 _unlink(fname);
2351
2352 errno = 0xdeadbeef;
2354 ok(handle == INVALID_HANDLE_VALUE, "_get_osfhandle returned %p\n", handle);
2355 ok(errno == EBADF, "errno = %d\n", errno);
2356}
2357
2358static void test_setmaxstdio(void)
2359{
2360 if (p__setmaxstdio)
2361 {
2362 ok(2048 == p__setmaxstdio(2048),"_setmaxstdio returned %d instead of 2048\n",p__setmaxstdio(2048));
2363 ok(-1 == p__setmaxstdio(2049),"_setmaxstdio returned %d instead of -1\n",p__setmaxstdio(2049));
2364 }
2365 else win_skip( "_setmaxstdio not supported\n" );
2366}
2367
2368static void test_stat(void)
2369{
2370 int fd;
2371 int pipes[2];
2372 int ret;
2373 struct stat buf;
2374
2375 /* Tests for a file */
2376 fd = open("stat.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
2377 if (fd >= 0)
2378 {
2379 ret = fstat(fd, &buf);
2380 ok(!ret, "fstat failed: errno=%d\n", errno);
2381 ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode);
2382 ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode);
2383 ok(buf.st_dev == 0, "st_dev is %d, expected 0\n", buf.st_dev);
2384 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2385 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2386 ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size);
2387
2388 ret = stat("stat.tst", &buf);
2389 ok(!ret, "stat failed: errno=%d\n", errno);
2390 ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode);
2391 ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode);
2392 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2393 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2394 ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size);
2395
2396 errno = 0xdeadbeef;
2397 ret = stat("stat.tst\\", &buf);
2398 ok(ret == -1, "stat returned %d\n", ret);
2399 ok(errno == ENOENT, "errno = %d\n", errno);
2400
2401 close(fd);
2402 remove("stat.tst");
2403 }
2404 else
2405 skip("open failed with errno %d\n", errno);
2406
2407 /* Tests for a char device */
2408 if (_dup2(0, 10) == 0)
2409 {
2410 ret = fstat(10, &buf);
2411 ok(!ret, "fstat(stdin) failed: errno=%d\n", errno);
2412 if ((buf.st_mode & _S_IFMT) == _S_IFCHR)
2413 {
2414 ok(buf.st_mode == _S_IFCHR, "bad st_mode=%06o\n", buf.st_mode);
2415 ok(buf.st_dev == 10, "st_dev is %d, expected 10\n", buf.st_dev);
2416 ok(buf.st_rdev == 10, "st_rdev is %d, expected 10\n", buf.st_rdev);
2417 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2418 }
2419 else
2420 skip("stdin is not a char device? st_mode=%06o\n", buf.st_mode);
2421 close(10);
2422 }
2423 else
2424 skip("_dup2 failed with errno %d\n", errno);
2425
2426 /* Tests for pipes */
2427 if (_pipe(pipes, 1024, O_BINARY) == 0)
2428 {
2429 ret = fstat(pipes[0], &buf);
2430 ok(!ret, "fstat(pipe) failed: errno=%d\n", errno);
2431 ok(buf.st_mode == _S_IFIFO, "bad st_mode=%06o\n", buf.st_mode);
2432 ok(buf.st_dev == pipes[0], "st_dev is %d, expected %d\n", buf.st_dev, pipes[0]);
2433 ok(buf.st_rdev == pipes[0], "st_rdev is %d, expected %d\n", buf.st_rdev, pipes[0]);
2434 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2435 close(pipes[0]);
2436 close(pipes[1]);
2437 }
2438 else
2439 skip("pipe failed with errno %d\n", errno);
2440
2441 /* Tests for directory */
2442 if(mkdir("stat.tst") == 0)
2443 {
2444 ret = stat("stat.tst ", &buf);
2445 ok(!ret, "stat(directory) failed: errno=%d\n", errno);
2446 ok((buf.st_mode & _S_IFMT) == _S_IFDIR, "bad format = %06o\n", buf.st_mode);
2447 ok((buf.st_mode & 0777) == 0777, "bad st_mode = %06o\n", buf.st_mode);
2448 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2449 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2450
2451 errno = 0xdeadbeef;
2452 ret = stat("stat.tst\\ ", &buf);
2453 ok(ret == -1, "stat returned %d\n", ret);
2454 ok(errno == ENOENT, "errno = %d\n", errno);
2455 rmdir( "stat.tst" );
2456 }
2457 else
2458 skip("mkdir failed with errno %d\n", errno);
2459
2460 errno = 0xdeadbeef;
2461 ret = stat("c:", &buf);
2462 ok(ret == -1, "stat returned %d\n", ret);
2463 ok(errno == ENOENT, "errno = %d\n", errno);
2464
2465 ret = stat("c:/", &buf);
2466 ok(!ret, "stat returned %d\n", ret);
2467 ok(buf.st_dev == 2, "st_dev = %d\n", buf.st_dev);
2468 ok(buf.st_rdev == 2, "st_rdev = %d\n", buf.st_rdev);
2469}
2470
2471static const char* pipe_string="Hello world";
2472
2473/* How many messages to transfer over the pipe */
2474#define N_TEST_MESSAGES 3
2475
2476static void test_pipes_child(int argc, char** args)
2477{
2478 int fd;
2479 int nwritten;
2480 int i;
2481
2482 if (argc < 5)
2483 {
2484 ok(0, "not enough parameters: %d\n", argc);
2485 return;
2486 }
2487
2488 fd=atoi(args[3]);
2489 i=close(fd);
2490 ok(!i, "unable to close %d: %d\n", fd, errno);
2491
2492 fd=atoi(args[4]);
2493
2494 for (i=0; i<N_TEST_MESSAGES; i++) {
2495 nwritten=write(fd, pipe_string, strlen(pipe_string));
2496 ok(nwritten == strlen(pipe_string), "i %d, expected to write '%s' wrote %d\n", i, pipe_string, nwritten);
2497 /* let other process wake up so they can show off their "keep reading until EOF" behavior */
2498 if (i < N_TEST_MESSAGES-1)
2499 Sleep(100);
2500 }
2501
2502 i=close(fd);
2503 ok(!i, "unable to close %d: %d\n", fd, errno);
2504}
2505
2506static void test_pipes(const char* selfname)
2507{
2508 int pipes[2];
2509 char str_fdr[12], str_fdw[12];
2510 FILE* file;
2511 const char* arg_v[6];
2512 char buf[4096];
2513 char expected[4096];
2514 int r;
2515 int i;
2516
2517 /* Test reading from a pipe with read() */
2518 if (_pipe(pipes, 1024, O_BINARY) < 0)
2519 {
2520 ok(0, "pipe failed with errno %d\n", errno);
2521 return;
2522 }
2523
2524 arg_v[0] = get_base_name(selfname);
2525 arg_v[1] = "file";
2526 arg_v[2] = "pipes";
2527 arg_v[3] = str_fdr; sprintf(str_fdr, "%d", pipes[0]);
2528 arg_v[4] = str_fdw; sprintf(str_fdw, "%d", pipes[1]);
2529 arg_v[5] = NULL;
2531 i=close(pipes[1]);
2532 ok(!i, "unable to close %d: %d\n", pipes[1], errno);
2533
2534 for (i=0; i<N_TEST_MESSAGES; i++) {
2535 r=read(pipes[0], buf, sizeof(buf)-1);
2536 ok(r == strlen(pipe_string), "i %d, got %d\n", i, r);
2537 if (r > 0)
2538 buf[r]='\0';
2539 ok(strcmp(buf, pipe_string) == 0, "expected to read '%s', got '%s'\n", pipe_string, buf);
2540 }
2541
2542 r=read(pipes[0], buf, sizeof(buf)-1);
2543 ok(r == 0, "expected to read 0 bytes, got %d\n", r);
2544 i=close(pipes[0]);
2545 ok(!i, "unable to close %d: %d\n", pipes[0], errno);
2546
2547 /* Test reading from a pipe with fread() */
2548 if (_pipe(pipes, 1024, O_BINARY) < 0)
2549 {
2550 ok(0, "pipe failed with errno %d\n", errno);
2551 return;
2552 }
2553
2554 arg_v[1] = "file";
2555 arg_v[2] = "pipes";
2556 arg_v[3] = str_fdr; sprintf(str_fdr, "%d", pipes[0]);
2557 arg_v[4] = str_fdw; sprintf(str_fdw, "%d", pipes[1]);
2558 arg_v[5] = NULL;
2560 i=close(pipes[1]);
2561 ok(!i, "unable to close %d: %d\n", pipes[1], errno);
2562 file=fdopen(pipes[0], "r");
2563
2564 /* In blocking mode, fread will keep calling read() until it gets
2565 * enough bytes, or EOF, even on Unix. (If this were a Unix terminal
2566 * in cooked mode instead of a pipe, it would also stop on EOL.)
2567 */
2568 expected[0] = 0;
2569 for (i=0; i<N_TEST_MESSAGES; i++)
2571 r=fread(buf, 1, sizeof(buf)-1, file);
2572 ok(r == strlen(expected), "fread() returned %d: ferror=%d\n", r, ferror(file));
2573 if (r > 0)
2574 buf[r]='\0';
2575 ok(strcmp(buf, expected) == 0, "got '%s' expected '%s'\n", buf, expected);
2576
2577 /* Let child close the file before we read, so we can sense EOF reliably */
2578 Sleep(100);
2579 r=fread(buf, 1, sizeof(buf)-1, file);
2580 ok(r == 0, "fread() returned %d instead of 0\n", r);
2581 ok(ferror(file) == 0, "got ferror() = %d\n", ferror(file));
2582 ok(feof(file), "feof() is false!\n");
2583
2584 i=fclose(file);
2585 ok(!i, "unable to close the pipe: %d\n", errno);
2586
2587 /* test \r handling when it's the last character read */
2588 if (_pipe(pipes, 1024, O_BINARY) < 0)
2589 {
2590 ok(0, "pipe failed with errno %d\n", errno);
2591 return;
2592 }
2593 r = write(pipes[1], "\r\n\rab\r\n", 7);
2594 ok(r == 7, "write returned %d, errno = %d\n", r, errno);
2595 setmode(pipes[0], O_TEXT);
2596 r = read(pipes[0], buf, 1);
2597 ok(r == 1, "read returned %d, expected 1\n", r);
2598 ok(buf[0] == '\n', "buf[0] = %x, expected '\\n'\n", buf[0]);
2599 r = read(pipes[0], buf, 1);
2600 ok(r == 1, "read returned %d, expected 1\n", r);
2601 ok(buf[0] == '\r', "buf[0] = %x, expected '\\r'\n", buf[0]);
2602 r = read(pipes[0], buf, 1);
2603 ok(r == 1, "read returned %d, expected 1\n", r);
2604 ok(buf[0] == 'a', "buf[0] = %x, expected 'a'\n", buf[0]);
2605 r = read(pipes[0], buf, 2);
2606 ok(r == 2, "read returned %d, expected 1\n", r);
2607 ok(buf[0] == 'b', "buf[0] = %x, expected 'b'\n", buf[0]);
2608 ok(buf[1] == '\n', "buf[1] = %x, expected '\\n'\n", buf[1]);
2609
2610 if (p_fopen_s)
2611 {
2612 /* test utf16 read with insufficient data */
2613 r = write(pipes[1], "a\0b", 3);
2614 ok(r == 3, "write returned %d, errno = %d\n", r, errno);
2615 buf[2] = 'z';
2616 buf[3] = 'z';
2617 setmode(pipes[0], _O_WTEXT);
2618 r = read(pipes[0], buf, 4);
2619 ok(r == 2, "read returned %d, expected 2\n", r);
2620 ok(!memcmp(buf, "a\0bz", 4), "read returned incorrect data\n");
2621 r = write(pipes[1], "\0", 1);
2622 ok(r == 1, "write returned %d, errno = %d\n", r, errno);
2623 buf[0] = 'z';
2624 buf[1] = 'z';
2625 r = read(pipes[0], buf, 2);
2626 ok(r == 0, "read returned %d, expected 0\n", r);
2627 ok(!memcmp(buf, "\0z", 2), "read returned incorrect data\n");
2628 }
2629 else
2630 {
2631 win_skip("unicode mode tests on pipe\n");
2632 }
2633
2634 close(pipes[1]);
2635 close(pipes[0]);
2636}
2637
2638static void test_unlink(void)
2639{
2640 FILE* file;
2641 ok(mkdir("test_unlink") == 0, "unable to create test dir\n");
2642 file = fopen("test_unlink\\empty", "w");
2643 ok(file != NULL, "unable to create test file\n");
2644 if(file)
2645 fclose(file);
2646 ok(_unlink("test_unlink") != 0, "unlinking a non-empty directory must fail\n");
2647 unlink("test_unlink\\empty");
2648 rmdir("test_unlink");
2649}
2650
2651static void test_dup2(void)
2652{
2653 ok(-1 == _dup2(0, -1), "expected _dup2 to fail when second arg is negative\n" );
2654}
2655
2656static void test_stdin(void)
2657{
2659 int stdin_dup, fd;
2660 HANDLE h;
2661 DWORD r;
2662
2663 stdin_dup = _dup(STDIN_FILENO);
2664 ok(stdin_dup != -1, "_dup(STDIN_FILENO) failed\n");
2665
2667 "GetStdHandle(STD_INPUT_HANDLE) != _get_osfhandle(STDIN_FILENO)\n");
2668
2670 ok(r == TRUE, "SetStdHandle returned %lx, expected TRUE\n", r);
2672 ok(h == INVALID_HANDLE_VALUE, "h = %p\n", h);
2673
2676 ok(h == NULL, "h != NULL\n");
2677
2678 fd = open("stdin.tst", O_WRONLY | O_CREAT, _S_IREAD |_S_IWRITE);
2679 ok(fd != -1, "open failed\n");
2680 ok(fd == STDIN_FILENO, "fd = %d, expected STDIN_FILENO\n", fd);
2682 ok(h != NULL, "h == NULL\n");
2683 close(fd);
2684 unlink("stdin.tst");
2685
2686 r = _dup2(stdin_dup, STDIN_FILENO);
2687 ok(r != -1, "_dup2 failed\n");
2689 ok(h != NULL, "h == NULL\n");
2690}
2691
2692static void test_mktemp(void)
2693{
2694 char buf[16];
2695
2696 strcpy(buf, "a");
2697 ok(!_mktemp(buf), "_mktemp(\"a\") != NULL\n");
2698
2699 strcpy(buf, "testXXXXX");
2700 ok(!_mktemp(buf), "_mktemp(\"testXXXXX\") != NULL\n");
2701
2702 strcpy(buf, "testXXXXXX");
2703 ok(_mktemp(buf) != NULL, "_mktemp(\"testXXXXXX\") == NULL\n");
2704
2705 strcpy(buf, "testXXXXXXa");
2706 ok(!_mktemp(buf), "_mktemp(\"testXXXXXXa\") != NULL\n");
2707
2708 strcpy(buf, "**XXXXXX");
2709 ok(_mktemp(buf) != NULL, "_mktemp(\"**XXXXXX\") == NULL\n");
2710}
2711
2712static void test__open_osfhandle(void)
2713{
2714 ioinfo *info;
2715 HANDLE h, tmp;
2716 int fd;
2717
2718 errno = 0xdeadbeef;
2720 ok(fd == -1, "_open_osfhandle returned %d\n", fd);
2721 ok(errno == EBADF, "errno = %d\n", errno);
2722
2723 h = CreateFileA("open_osfhandle.tst", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
2725 ok(fd > 0, "_open_osfhandle returned %d (%d)\n", fd, errno);
2727 ok(info->handle == h, "info->handle = %p, expected %p\n", info->handle, h);
2728 ok(info->wxflag == 1, "info->wxflag = %x, expected 1\n", info->wxflag);
2729 close(fd);
2730 ok(info->handle == INVALID_HANDLE_VALUE, "info->handle = %p, expected INVALID_HANDLE_VALUE\n", info->handle);
2731 ok(info->wxflag == 0, "info->wxflag = %x, expected 0\n", info->wxflag);
2732 DeleteFileA("open_osfhandle.tst");
2733
2734 errno = 0xdeadbeef;
2736 ok(fd == -1, "_open_osfhandle returned %d\n", fd);
2737 ok(errno == EBADF, "errno = %d\n", errno);
2738
2739 ok(CreatePipe(&h, &tmp, NULL, 0), "CreatePipe failed\n");
2741 ok(fd > 0, "_open_osfhandle returned %d (%d)\n", fd, errno);
2743 ok(info->handle == h, "info->handle = %p, expected %p\n", info->handle, h);
2744 ok(info->wxflag == 9, "info->wxflag = %x, expected 9\n", info->wxflag);
2745 close(fd);
2746 CloseHandle(tmp);
2747}
2748
2750{
2751 char *inbuffer;
2752 char *outbuffer;
2753 int size, fd;
2754 fpos_t pos, pos2;
2755
2756 fd = fileno(file);
2757 inbuffer = calloc(1, bufsize + 1);
2758 outbuffer = calloc(1, bufsize + 1);
2759 _snprintf(outbuffer, bufsize + 1, "0,1,2,3,4,5,6,7,8,9");
2760
2761 for (size = bufsize + 1; size >= bufsize - 1; size--) {
2762 rewind(file);
2763 ok(file->_cnt == 0, "_cnt should be 0 after rewind, but is %d\n", file->_cnt);
2764 fwrite(outbuffer, 1, size, file);
2765 /* lseek() below intentionally redirects the write in fflush() to detect
2766 * if fwrite() has already flushed the whole buffer or not.
2767 */
2768 lseek(fd, 1, SEEK_SET);
2769 fflush(file);
2770 ok(file->_cnt == 0, "_cnt should be 0 after fflush, but is %d\n", file->_cnt);
2771 fseek(file, 0, SEEK_SET);
2772 ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
2773 if (size == bufsize)
2774 ok(memcmp(outbuffer, inbuffer, bufsize) == 0, "missing flush by %d byte write\n", size);
2775 else
2776 ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d byte write\n", size);
2777 }
2778 rewind(file);
2779 fwrite(outbuffer, 1, bufsize / 2, file);
2780 fwrite(outbuffer + bufsize / 2, 1, bufsize / 2, file);
2781 lseek(fd, 1, SEEK_SET);
2782 fflush(file);
2783 fseek(file, 0, SEEK_SET);
2784 ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
2785 ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize);
2786
2787 ok(!fseek(file, -1, SEEK_END), "fseek failed\n");
2788 ok(!fgetpos(file, &pos), "fgetpos failed\n");
2789 ok(fread(inbuffer, 1, 1, file) == 1, "fread failed\n");
2790 ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
2791 ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
2792 ok(file->_ptr != file->_base, "file->_ptr == file->_base\n");
2793 ok(fwrite(outbuffer, 1, bufsize, file), "fwrite failed\n");
2794 ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
2795 ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
2796 ok(file->_ptr == file->_base, "file->_ptr == file->_base\n");
2797 ok(!fgetpos(file, &pos2), "fgetpos failed\n");
2798 ok(pos+bufsize+1 == pos2, "pos = %d (%d)\n", (int)pos, (int)pos2);
2799 free(inbuffer);
2800 free(outbuffer);
2801}
2802
2803static void test_write_flush(void)
2804{
2805 char iobuf[1024];
2806 char *tempf;
2807 FILE *file;
2808
2809 tempf = _tempnam(".","wne");
2810 file = fopen(tempf, "wb+");
2811 ok(file != NULL, "unable to create test file\n");
2812 iobuf[0] = 0;
2813 ok(file->_bufsiz == 4096, "incorrect default buffer size: %d\n", file->_bufsiz);
2814 test_write_flush_size(file, file->_bufsiz);
2815 setvbuf(file, iobuf, _IOFBF, sizeof(iobuf));
2816 test_write_flush_size(file, sizeof(iobuf));
2817 fclose(file);
2818 unlink(tempf);
2819 free(tempf);
2820}
2821
2822static void test_close(void)
2823{
2824 ioinfo *stdout_info, stdout_copy, *stderr_info, stderr_copy;
2825 int fd1, fd2, ret1, ret2, ret3, ret4;
2826 DWORD flags;
2827 HANDLE h;
2828
2829 /* test close on fds that use the same handle */
2830 h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
2832 ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
2833
2834 fd1 = _open_osfhandle((intptr_t)h, 0);
2835 ok(fd1 != -1, "_open_osfhandle failed (%d)\n", errno);
2836 fd2 = _open_osfhandle((intptr_t)h, 0);
2837 ok(fd2 != -1, "_open_osfhandle failed (%d)\n", errno);
2838 ok(fd1 != fd2, "fd1 == fd2\n");
2839
2840 ok((HANDLE)_get_osfhandle(fd1) == h, "handles mismatch (%p != %p)\n",
2841 (HANDLE)_get_osfhandle(fd1), h);
2842 ok((HANDLE)_get_osfhandle(fd2) == h, "handles mismatch (%p != %p)\n",
2843 (HANDLE)_get_osfhandle(fd2), h);
2844 ret1 = close(fd1);
2845 ok(!ret1, "close(fd1) failed (%d)\n", errno);
2846 ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n");
2847 ok(close(fd2), "close(fd2) succeeded\n");
2848
2849 /* test close on already closed fd */
2850 errno = 0xdeadbeef;
2851 ret1 = close(fd1);
2852 ok(ret1 == -1, "close(fd1) succeeded\n");
2853 ok(errno == 9, "errno = %d\n", errno);
2854
2855 /* test close on stdout and stderr that use the same handle */
2856 h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
2858 ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
2859
2860 /* tests output will not be visible from now on */
2863 stdout_copy = *stdout_info;
2864 stderr_copy = *stderr_info;
2865 stdout_info->handle = h;
2866 stderr_info->handle = h;
2867
2868 ret1 = close(STDOUT_FILENO);
2869 ret2 = GetHandleInformation(h, &flags);
2870 ret3 = close(STDERR_FILENO);
2871 ret4 = GetHandleInformation(h, &flags);
2872
2873 *stdout_info = stdout_copy;
2874 *stderr_info = stderr_copy;
2875 SetStdHandle(STD_OUTPUT_HANDLE, stdout_info->handle);
2876 SetStdHandle(STD_ERROR_HANDLE, stderr_info->handle);
2877 /* stdout and stderr restored */
2878
2879 ok(!ret1, "close(STDOUT_FILENO) failed\n");
2880 ok(ret2, "GetHandleInformation failed\n");
2881 ok(!ret3, "close(STDERR_FILENO) failed\n");
2882 ok(!ret4, "GetHandleInformation succeeded\n");
2883
2884 DeleteFileA( "fdopen.tst" );
2885}
2886
2887static void test__creat(void)
2888{
2889 int fd, pos, count, readonly, old_fmode = 0, have_fmode;
2890 char buf[6], testdata[4] = {'a', '\n', 'b', '\n'};
2891
2892 have_fmode = p__get_fmode && p__set_fmode && !p__get_fmode(&old_fmode);
2893 if (!have_fmode)
2894 win_skip("_fmode can't be set, skipping mode tests\n");
2895
2896 if (have_fmode)
2897 p__set_fmode(_O_TEXT);
2898 fd = _creat("_creat.tst", 0);
2899 ok(fd > 0, "_creat failed\n");
2900 _write(fd, testdata, 4);
2901 if (have_fmode) {
2902 pos = _tell(fd);
2903 ok(pos == 6, "expected pos 6 (text mode), got %d\n", pos);
2904 }
2905 ok(_lseek(fd, 0, SEEK_SET) == 0, "_lseek failed\n");
2906 count = _read(fd, buf, 6);
2907 ok(count == 4, "_read returned %d, expected 4\n", count);
2908 count = count > 0 ? count > 4 ? 4 : count : 0;
2909 ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
2910 _close(fd);
2911 readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
2912 ok(readonly, "expected read-only file\n");
2914 DeleteFileA("_creat.tst");
2915
2916 if (have_fmode)
2917 p__set_fmode(_O_BINARY);
2918 fd = _creat("_creat.tst", _S_IREAD | _S_IWRITE);
2919 ok(fd > 0, "_creat failed\n");
2920 _write(fd, testdata, 4);
2921 if (have_fmode) {
2922 pos = _tell(fd);
2923 ok(pos == 4, "expected pos 4 (binary mode), got %d\n", pos);
2924 }
2925 ok(_lseek(fd, 0, SEEK_SET) == 0, "_lseek failed\n");
2926 count = _read(fd, buf, 6);
2927 ok(count == 4, "_read returned %d, expected 4\n", count);
2928 count = count > 0 ? count > 4 ? 4 : count : 0;
2929 ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
2930 _close(fd);
2931 readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
2932 ok(!readonly, "expected rw file\n");
2934 DeleteFileA("_creat.tst");
2935
2936 if (have_fmode)
2937 p__set_fmode(old_fmode);
2938}
2939
2940static void test_lseek(void)
2941{
2942 int fd;
2943 char testdata[4] = {'a', '\n', 'b', '\n'};
2944
2945 errno = 0xdeadbeef;
2946 ok(_lseek(-42, 0, SEEK_SET) == -1, "expected failure\n");
2947 ok(errno == EBADF, "errno = %d\n", errno);
2948
2949 fd = _creat("_creat.tst", _S_IWRITE);
2950 ok(fd > 0, "_creat failed\n");
2951 _write(fd, testdata, 4);
2952
2953 errno = 0xdeadbeef;
2954 ok(_lseek(fd, 0, 42) == -1, "expected failure\n");
2955 ok(errno == EINVAL, "errno = %d\n", errno);
2956
2957 errno = 0xdeadbeef;
2958 ok(_lseek(fd, -42, SEEK_SET) == -1, "expected failure\n");
2959 ok(errno == EINVAL, "errno = %d\n", errno);
2960
2961 _close(fd);
2962 DeleteFileA("_creat.tst");
2963}
2964
2966{
2967 HANDLE handle;
2968 FILE_MODE_INFORMATION mode_info;
2971
2973 status = NtQueryInformationFile(handle, &io, &mode_info, sizeof(mode_info),
2975 ok(!status, "NtQueryInformationFile failed\n");
2976 return (mode_info.Mode & FILE_SEQUENTIAL_ONLY) != 0;
2977}
2978
2979static void test_fopen_hints(void)
2980{
2981 static const struct {
2982 const char *mode;
2983 BOOL seq;
2984 } tests[] = {
2985 { "rb", FALSE },
2986 { "rbS", TRUE },
2987 { "rbR", FALSE },
2988 { "rbSR", TRUE },
2989 { "rbRS", FALSE }
2990 };
2991
2992 char temppath[MAX_PATH], tempfile[MAX_PATH];
2993 FILE *fp;
2994 int i;
2995
2996 GetTempPathA(MAX_PATH, temppath);
2997 GetTempFileNameA(temppath, "", 0, tempfile);
2998
2999 for (i = 0; i < ARRAY_SIZE(tests); ++i)
3000 {
3001 fp = fopen(tempfile, tests[i].mode);
3002 ok(fp != NULL, "unable to fopen test file with mode \"%s\"\n", tests[i].mode);
3003 ok(has_sequential_hint(_fileno(fp)) == tests[i].seq,
3004 "unexpected sequential hint for fopen mode \"%s\"\n", tests[i].mode);
3005 fclose(fp);
3006 }
3007 unlink(tempfile);
3008}
3009
3010static void test_open_hints(void)
3011{
3012 static const struct {
3013 int mode;
3014 BOOL seq;
3015 } tests[] = {
3016 { _O_RDONLY | _O_BINARY, FALSE },
3020 };
3021
3022 char temppath[MAX_PATH], tempfile[MAX_PATH];
3023 int fd;
3024 int i;
3025
3026 GetTempPathA(MAX_PATH, temppath);
3027 GetTempFileNameA(temppath, "", 0, tempfile);
3028
3029 for (i = 0; i < ARRAY_SIZE(tests); ++i)
3030 {
3031 fd = open(tempfile, tests[i].mode);
3032 ok(fd != -1, "unable to _open test file with flags %x\n", tests[i].mode);
3033 ok(has_sequential_hint(fd) == tests[i].seq,
3034 "unexpected sequential hint for _open flags %x\n", tests[i].mode);
3035 close(fd);
3036 }
3037 unlink(tempfile);
3038}
3039
3040static void test_ioinfo_flags(void)
3041{
3042 HANDLE handle;
3043 ioinfo *info;
3044 char *tempf;
3045 int tempfd;
3046
3047 tempf = _tempnam(".","wne");
3048
3050 ok(tempfd != -1, "_open failed with error: %d\n", errno);
3051
3052 handle = (HANDLE)_get_osfhandle(tempfd);
3054 ok(!!info, "NULL info.\n");
3055 ok(info->handle == handle, "Unexpected handle %p, expected %p.\n", info->handle, handle);
3056 skip_2k3_fail ok(info->exflag == (EF_UTF16 | EF_CRIT_INIT | EF_UNK_UNICODE), "Unexpected exflag %#x.\n", info->exflag);
3057 ok(info->wxflag == (WX_TEXT | WX_OPEN), "Unexpected wxflag %#x.\n", info->wxflag);
3058
3059 close(tempfd);
3060
3061 ok(info->handle == INVALID_HANDLE_VALUE, "Unexpected handle %p.\n", info->handle);
3062 skip_2k3_fail ok(info->exflag == (EF_UTF16 | EF_CRIT_INIT | EF_UNK_UNICODE), "Unexpected exflag %#x.\n", info->exflag);
3063 ok(!info->wxflag, "Unexpected wxflag %#x.\n", info->wxflag);
3064
3065 info = &__pioinfo[(tempfd + 4) / MSVCRT_FD_BLOCK_SIZE][(tempfd + 4) % MSVCRT_FD_BLOCK_SIZE];
3066 ok(!!info, "NULL info.\n");
3067 ok(info->handle == INVALID_HANDLE_VALUE, "Unexpected handle %p.\n", info->handle);
3068 ok(!info->exflag, "Unexpected exflag %#x.\n", info->exflag);
3069
3070 unlink(tempf);
3071 free(tempf);
3072}
3073
3075{
3076 int dup_fd, ret, pos;
3077 FILE *file;
3078 char ch;
3079
3080 dup_fd = _dup(STDOUT_FILENO);
3081 ok(dup_fd != -1, "_dup failed\n");
3082
3083 file = freopen("std_stream_test.tmp", "w", stdout);
3084 ok(file != NULL, "freopen failed\n");
3085
3086 ret = fprintf(stdout, "test");
3088
3089 fflush(stdout);
3090 _dup2(dup_fd, STDOUT_FILENO);
3091 close(dup_fd);
3092 setvbuf(stdout, NULL, _IONBF, 0);
3093
3094 ok(ret == 4, "fprintf(stdout) returned %d\n", ret);
3095 ok(!pos, "expected stdout to be buffered\n");
3096
3097 dup_fd = _dup(STDERR_FILENO);
3098 ok(dup_fd != -1, "_dup failed\n");
3099
3100 file = freopen("std_stream_test.tmp", "w", stderr);
3101 ok(file != NULL, "freopen failed\n");
3102
3103 ret = fprintf(stderr, "test");
3104 ok(ret == 4, "fprintf(stderr) returned %d\n", ret);
3106 ok(!pos, "expected stderr to be buffered\n");
3107
3108 fflush(stderr);
3109 _dup2(dup_fd, STDERR_FILENO);
3110 close(dup_fd);
3111
3112 dup_fd = _dup(STDIN_FILENO);
3113 ok(dup_fd != -1, "_dup failed\n");
3114
3115 file = freopen("std_stream_test.tmp", "r", stdin);
3116 ok(file != NULL, "freopen failed\n");
3117
3118 ch = 0;
3119 ret = fscanf(stdin, "%c", &ch);
3120 ok(ret == 1, "fscanf returned %d\n", ret);
3121 ok(ch == 't', "ch = 0x%x\n", (unsigned char)ch);
3123 ok(pos == 4, "pos = %d\n", pos);
3124
3125 fflush(stdin);
3126 _dup2(dup_fd, STDIN_FILENO);
3127 close(dup_fd);
3128
3129 ok(DeleteFileA("std_stream_test.tmp"), "DeleteFile failed\n");
3130}
3131
3132static void test_std_stream_open(void)
3133{
3134 FILE *f;
3135 int fd;
3136
3137 fd = _dup(STDIN_FILENO);
3138 ok(fd != -1, "_dup failed\n");
3139
3140 ok(!fclose(stdin), "fclose failed\n");
3141 f = fopen("nul", "r");
3142 ok(f == stdin, "f = %p, expected %p\n", f, stdin);
3143 ok(_fileno(f) == STDIN_FILENO, "_fileno(f) = %d\n", _fileno(f));
3144
3146 close(fd);
3147}
3148
3150{
3151 int arg_c;
3152 char** arg_v;
3153
3154 init();
3155
3156 arg_c = winetest_get_mainargs( &arg_v );
3157
3158 /* testing low-level I/O */
3159 if (arg_c >= 3)
3160 {
3161 if (strcmp(arg_v[2], "inherit") == 0)
3162 test_file_inherit_child(arg_v[3], arg_c > 4 ? arg_v[4] : NULL);
3163 else if (strcmp(arg_v[2], "inherit_no") == 0)
3165 else if (strcmp(arg_v[2], "pipes") == 0)
3166 test_pipes_child(arg_c, arg_v);
3167 else if (strcmp(arg_v[2], "stdin") == 0)
3169 else
3170 ok(0, "invalid argument '%s'\n", arg_v[2]);
3171 return;
3172 }
3173 test_dup2();
3174 test_file_inherit(arg_v[0]);
3175 test_invalid_stdin(arg_v[0]);
3177 test_chsize();
3178 test_stat();
3179 test_unlink();
3180
3181 /* testing stream I/O */
3182 test_filbuf();
3183 test_fdopen();
3185 test_fopen_s();
3187 test_setmode();
3188 test_fileops();
3192 test_readmode(FALSE); /* binary mode */
3193 test_readmode(TRUE); /* ascii mode */
3195 test_fgetc();
3196 test_fputc();
3197 test_flsbuf();
3198 test_fflush();
3199 test_fgetwc();
3200 /* \x83\xa9 is double byte character, \xe0\x7f is not (undefined). */
3201 test_fgetwc_locale("AB\x83\xa9\xe0\x7f", "Japanese_Japan.932", 932);
3202 /* \x83 is U+0192 */
3203 test_fgetwc_locale("AB\x83\xa9", "English", 1252);
3204 /* \x83 is U+0083 */
3205 test_fgetwc_locale("AB\x83\xa9", "C", 0);
3207 test_fputwc();
3208 test_freopen();
3209 test_ctrlz();
3211 test_tmpnam();
3214 test_pipes(arg_v[0]);
3215 test_stdin();
3216 test_mktemp();
3219 test_close();
3220 test__creat();
3221 test_lseek();
3227
3228 /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
3229 * file contains lines in the correct order
3230 */
3232}
static int argc
Definition: ServiceArgs.c:12
int wint_t
Definition: _apple.h:38
#define _flsbuf
Definition: _flswbuf.c:10
#define ENOENT
Definition: acclib.h:79
#define EINVAL
Definition: acclib.h:90
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
#define EBADF
Definition: acclib.h:82
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define __cdecl
Definition: accygwin.h:79
#define O_WRONLY
Definition: acwin.h:111
#define stat
Definition: acwin.h:99
#define O_CREAT
Definition: acwin.h:110
#define fileno
Definition: acwin.h:102
#define mkdir
Definition: acwin.h:101
#define O_BINARY
Definition: acwin.h:109
#define read
Definition: acwin.h:96
#define O_RDONLY
Definition: acwin.h:108
#define open
Definition: acwin.h:95
#define close
Definition: acwin.h:98
#define fstat
Definition: acwin.h:100
#define write
Definition: acwin.h:97
static void startup(void)
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define ARRAY_SIZE(A)
Definition: main.h:20
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define RegCloseKey(hKey)
Definition: registry.h:49
r l[0]
Definition: byte_order.h:168
#define SEEK_END
Definition: cabinet.c:29
#define _setmode(fd, mode)
Definition: cat.c:21
#define O_TEXT
Definition: cat.c:19
Definition: _locale.h:75
#define _SH_DENYRW
Definition: share.h:14
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegOpenCurrentUser(IN REGSAM samDesired, OUT PHKEY phkResult)
Definition: reg.c:3209
#define _O_SEQUENTIAL
Definition: cabinet.h:43
#define _O_RDWR
Definition: cabinet.h:39
#define _O_BINARY
Definition: cabinet.h:51
#define _O_APPEND
Definition: cabinet.h:41
#define _O_RDONLY
Definition: cabinet.h:37
#define _O_TEXT
Definition: cabinet.h:50
#define _O_TRUNC
Definition: cabinet.h:47
#define _O_CREAT
Definition: cabinet.h:46
#define _O_RANDOM
Definition: cabinet.h:42
#define _O_WRONLY
Definition: cabinet.h:38
#define _S_IWRITE
Definition: cabinet.h:33
#define _S_IREAD
Definition: cabinet.h:34
#define CloseHandle
Definition: compat.h:739
#define FILE_BEGIN
Definition: compat.h:761
#define CP_ACP
Definition: compat.h:109
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define GetProcAddress(x, y)
Definition: compat.h:753
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:740
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
#define GetCurrentProcess()
Definition: compat.h:759
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
#define FILE_SHARE_READ
Definition: compat.h:136
#define lstrlenW
Definition: compat.h:750
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
BOOL WINAPI SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
Definition: fileinfo.c:776
DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
Definition: fileinfo.c:636
HANDLE WINAPI FindFirstFileA(IN LPCSTR lpFileName, OUT LPWIN32_FIND_DATAA lpFindFileData)
Definition: find.c:263
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL WINAPI GetHandleInformation(IN HANDLE hObject, OUT LPDWORD lpdwFlags)
Definition: handle.c:40
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
DWORD WINAPI GetTempPathA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2054
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
Definition: proc.c:4747
VOID WINAPI GetStartupInfoA(IN LPSTARTUPINFOA lpStartupInfo)
Definition: proc.c:1320
const WCHAR * text
Definition: package.c:1794
unsigned char
Definition: typeof.h:29
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define INFINITE
Definition: serial.h:102
static unsigned char buff[32768]
Definition: fatten.c:17
UINT WINAPI GetTempFileNameA(IN LPCSTR lpPathName, IN LPCSTR lpPrefixString, IN UINT uUnique, OUT LPSTR lpTempFileName)
Definition: filename.c:26
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
@ FileModeInformation
Definition: from_kernel.h:77
#define FILE_SEQUENTIAL_ONLY
Definition: from_kernel.h:27
FxCollectionEntry * cur
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLuint res
Definition: glext.h:9613
GLuint buffer
Definition: glext.h:5915
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
GLsizeiptr size
Definition: glext.h:5919
const GLubyte * c
Definition: glext.h:8905
GLfloat f
Definition: glext.h:7540
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLbitfield flags
Definition: glext.h:7161
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
GLenum GLsizei len
Definition: glext.h:6722
const GLfloat * m
Definition: glext.h:10848
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
static char obuf[100]
Definition: i386-dis.c:1281
#define _O_WTEXT
Definition: fcntl.h:20
#define _O_U8TEXT
Definition: fcntl.h:22
#define _O_U16TEXT
Definition: fcntl.h:21
#define O_RDWR
Definition: fcntl.h:36
#define LC_CTYPE
Definition: locale.h:19
_Check_return_opt_ _CRTIMP int __cdecl fsetpos(_Inout_ FILE *_File, _In_ const fpos_t *_Pos)
_CRTIMP char *__cdecl tmpnam(_Pre_maybenull_ _Post_z_ char *_Buffer)
_Check_return_ _CRTIMP FILE *__cdecl freopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode, _Inout_ FILE *_File)
#define stdout
Definition: stdio.h:99
_Check_return_opt_ _CRTIMP int __cdecl fgetc(_Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl _filbuf(_Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl _getw(_Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl ferror(_In_ FILE *_File)
_Check_return_opt_ _CRTIMP wint_t __cdecl fputwc(_In_ wchar_t _Ch, _Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl getc(_Inout_ FILE *_File)
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
_Check_return_ _CRTIMP char *__cdecl _tempnam(_In_opt_z_ const char *_DirName, _In_opt_z_ const char *_FilePrefix)
_Check_return_opt_ _CRTIMP int __cdecl fgetpos(_Inout_ FILE *_File, _Out_ fpos_t *_Pos)
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl _fcloseall(void)
Definition: file.c:1081
#define EOF
Definition: stdio.h:24
#define stderr
Definition: stdio.h:100
_Check_return_ _CRTIMP int __cdecl feof(_In_ FILE *_File)
_Check_return_opt_ _CRTIMP_ALT int __cdecl ungetc(_In_ int _Ch, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl fscanf(_Inout_ FILE *_File, _In_z_ _Scanf_format_string_ const char *_Format,...)
#define _IOREAD
Definition: stdio.h:124
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl _flushall(void)
Definition: file.c:893
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
#define _IONBF
Definition: stdio.h:129
_Check_return_ _CRTIMP FILE *__cdecl fdopen(_In_ int _FileHandle, _In_z_ const char *_Format)
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fputc(_In_ int _Ch, _Inout_ FILE *_File)
#define stdin
Definition: stdio.h:98
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
_CRTIMP void __cdecl setbuf(_Inout_ FILE *_File, _Inout_updates_opt_(BUFSIZ) _Post_readable_size_(0) char *_Buffer)
_Check_return_opt_ _CRTIMP wint_t __cdecl fgetwc(_Inout_ FILE *_File)
_Check_return_ _CRTIMP int __cdecl sscanf(_In_z_ const char *_Src, _In_z_ _Scanf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl fputws(_In_z_ const wchar_t *_Str, _Inout_ FILE *_File)
#define _IOFBF
Definition: stdio.h:127
_Check_return_opt_ _CRTIMP int __cdecl setvbuf(_Inout_ FILE *_File, _Inout_updates_opt_z_(_Size) char *_Buf, _In_ int _Mode, _In_ size_t _Size)
_CRTIMP void __cdecl clearerr(_Inout_ FILE *_File)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define _doserrno
Definition: stdlib.h:131
#define _S_IFCHR
Definition: stat.h:135
#define _S_IFREG
Definition: stat.h:137
#define _S_IFDIR
Definition: stat.h:134
#define _S_IFMT
Definition: stat.h:133
#define _S_IFIFO
Definition: stat.h:136
__int64 fpos_t
Definition: stdio.h:73
#define SEEK_SET
Definition: jmemansi.c:26
const unsigned char * inbuffer
Definition: jpeglib.h:983
#define f
Definition: ke_i.h:83
#define c
Definition: ke_i.h:80
#define lseek
Definition: syshdrs.h:47
#define unlink
Definition: syshdrs.h:54
if(dx< 0)
Definition: linetemp.h:194
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
static char filename2[]
Definition: lzexpand_main.c:51
#define O_NOINHERIT
Definition: mkdosfs.c:294
#define SEEK_CUR
Definition: util.h:63
#define CREATE_ALWAYS
Definition: disk.h:72
static struct test_info tests[]
#define sprintf(buf, format,...)
Definition: sprintf.c:55
static PEXPLICIT_ACCESSW *static HMODULE hmod
Definition: security.c:143
static CHAR selfname[MAX_PATH]
Definition: service.c:36
BOOL expected
Definition: file.c:2511
#define todo_wine
Definition: custom.c:89
static const char * read_file(HANDLE file)
Definition: file.c:1689
static void test_invalid_stdin_child(void)
Definition: file.c:1894
static void test_fputc(void)
Definition: file.c:675
static void test_tmpnam(void)
Definition: file.c:2007
static void test_fopen_hints(void)
Definition: file.c:2979
static void test_file_write_read(void)
Definition: file.c:1409
static void init(void)
Definition: file.c:88
static const char * get_base_name(const char *path)
Definition: file.c:76
static WCHAR * AtoW(const char *p)
Definition: file.c:609
static void test_fileops(void)
Definition: file.c:153
static void test_ioinfo_flags(void)
Definition: file.c:3040
static HANDLE proc_handles[2]
Definition: file.c:68
#define WX_OPEN
Definition: file.c:44
static void test_flsbuf(void)
Definition: file.c:700
static void test_fputwc(void)
Definition: file.c:1162
static void test_fgetc(void)
Definition: file.c:647
static void test_dup2(void)
Definition: file.c:2651
static const char * pipe_string
Definition: file.c:2471
static void test_get_osfhandle(void)
Definition: file.c:2335
static void test_lseek(void)
Definition: file.c:2940
#define LLEN
static void test_freopen(void)
Definition: file.c:1219
static void test_pipes_child(int argc, char **args)
Definition: file.c:2476
static BOOL has_sequential_hint(int fd)
Definition: file.c:2965
static void test_setmode(void)
Definition: file.c:2270
static void test__creat(void)
Definition: file.c:2887
static void test_chsize(void)
Definition: file.c:2026
#define EF_UTF16
Definition: file.c:54
static unsigned WINAPI read_pipe_thread(void *argument)
Definition: file.c:1742
static void test_readmode(BOOL ascii_mode)
Definition: file.c:284
static void test_close(void)
Definition: file.c:2822
static void test_std_stream_buffering(void)
Definition: file.c:3074
static void test_unlink(void)
Definition: file.c:2638
static void test_write_flush(void)
Definition: file.c:2803
static void test_fgetwc_locale(const char *text, const char *locale, int codepage)
Definition: file.c:953
static void test_stat(void)
Definition: file.c:2368
static void test_open_hints(void)
Definition: file.c:3010
#define EF_CRIT_INIT
Definition: file.c:55
static void test_stdout_handle(STARTUPINFOA *startup, char *cmdline, HANDLE hstdout, BOOL expect_stdout, const char *descr)
Definition: file.c:1699
static void test_std_stream_open(void)
Definition: file.c:3132
static void test_write_flush_size(FILE *file, int bufsize)
Definition: file.c:2749
static void test_file_inherit_child_no(const char *fd_s)
Definition: file.c:1659
static void test__open_osfhandle(void)
Definition: file.c:2712
static void test_filemodeT(void)
Definition: file.c:578
static void test_asciimode2(void)
Definition: file.c:540
#define N_TEST_MESSAGES
Definition: file.c:2474
static void test_readboundary(void)
Definition: file.c:619
static void test_ctrlz(void)
Definition: file.c:1311
static void test_fdopen(void)
Definition: file.c:133
static void test_fgetwc(void)
Definition: file.c:835
static void test_fopen_fclose_fcloseall(void)
Definition: file.c:2063
static void test_asciimode(void)
Definition: file.c:462
#define WX_TEXT
Definition: file.c:51
static void test_invalid_stdin(const char *selfname)
Definition: file.c:1965
#define EF_UNK_UNICODE
Definition: file.c:56
static void test_stdin(void)
Definition: file.c:2656
static void test_file_put_get(void)
Definition: file.c:1359
static void test_mktemp(void)
Definition: file.c:2692
static void test_fflush(void)
Definition: file.c:767
static ioinfo ** __pioinfo
Definition: file.c:66
static void test_pipes(const char *selfname)
Definition: file.c:2506
static void test_fopen_s(void)
Definition: file.c:2120
static void test__wfopen_s(void)
Definition: file.c:2235
static void test_fgetwc_unicode(void)
Definition: file.c:1022
static void test_setmaxstdio(void)
Definition: file.c:2358
#define MSVCRT_FD_BLOCK_SIZE
Definition: file.c:58
static void create_io_inherit_block(STARTUPINFOA *startup, unsigned int count, const HANDLE *handles)
Definition: file.c:1669
#define IOMODE
Definition: file.c:283
static void test_filbuf(void)
Definition: file.c:102
static void test_file_inherit_child(const char *fd_s, const char *handle_str)
Definition: file.c:1632
static void test_file_inherit(const char *selfname)
Definition: file.c:1754
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK io
Definition: file.c:100
static unsigned(__cdecl *hash_bstr)(bstr_t s)
#define _P_NOWAIT
Definition: port.h:371
const char * strerror(int err)
Definition: compat_str.c:23
#define _P_WAIT
Definition: port.h:370
int remove
Definition: msacm.c:1366
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define rmdir
Definition: syshdrs.h:70
#define STDOUT_FILENO
Definition: syshdrs.h:89
#define STDERR_FILENO
Definition: syshdrs.h:90
#define STDIN_FILENO
Definition: syshdrs.h:88
BOOL WINAPI CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
Definition: npipe.c:117
#define BUFSIZ
Definition: nsplookup.c:25
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI NTSTATUS NTAPI NtQueryInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, OUT PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
#define GENERIC_WRITE
Definition: nt_native.h:90
#define L(x)
Definition: ntvdm.h:50
static HANDLE proc()
Definition: pdb.c:34
long LONG
Definition: pedump.c:60
#define calloc
Definition: rosglue.h:14
#define WEOF
Definition: conio.h:185
#define errno
Definition: errno.h:18
_CRTIMP intptr_t __cdecl _get_osfhandle(_In_ int _FileHandle)
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)
_Check_return_opt_ _CRTIMP long __cdecl _lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
_Check_return_ _CRTIMP long __cdecl _filelength(_In_ int _FileHandle)
_Check_return_ _CRTIMP int __cdecl _chmod(_In_z_ const char *_Filename, _In_ int _Mode)
_CRTIMP int __cdecl _sopen(const char *_Filename, int _OpenFlag, int _ShareFlag,...)
Definition: file.c:1978
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
_Check_return_ _CRTIMP int __cdecl _pipe(_Inout_updates_(2) int *_PtHandles, _In_ unsigned int _PipeSize, _In_ int _TextMode)
_Check_return_ _CRTIMP long __cdecl _tell(_In_ int _FileHandle)
_Check_return_ _CRTIMP char *__cdecl _mktemp(_Inout_z_ char *_TemplateName)
_Check_return_ _CRTIMP int __cdecl _creat(_In_z_ const char *_Filename, _In_ int _PermissionMode)
_Check_return_ _CRTIMP int __cdecl _read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
_Check_return_ _CRTIMP int __cdecl _dup2(_In_ int _FileHandleSrc, _In_ int _FileHandleDst)
_Check_return_ _CRTIMP int __cdecl _unlink(_In_z_ const char *_Filename)
_CRTIMP char *__cdecl mktemp(_Inout_z_ char *_TemplateName)
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001
_Check_return_ _CRTIMP int __cdecl _chsize(_In_ int _FileHandle, _In_ long _Size)
_Check_return_ _CRTIMP int __cdecl _dup(_In_ int _FileHandle)
_Check_return_ _CRTIMP int __cdecl dup2(_In_ int _FileHandleSrc, _In_ int _FileHandleDst)
_CRTIMP int __cdecl _open_osfhandle(_In_ intptr_t _OSFileHandle, _In_ int _Flags)
_CRTIMP uintptr_t __cdecl _beginthreadex(_In_opt_ void *_Security, _In_ unsigned _StackSize, _In_ unsigned(__stdcall *_StartAddress)(void *), _In_opt_ void *_ArgList, _In_ unsigned _InitFlag, _Out_opt_ unsigned *_ThrdAddr)
_CRTIMP intptr_t __cdecl _spawnvp(_In_ int _Mode, _In_z_ const char *_Filename, _In_z_ const char *const *_ArgList)
#define CP_UTF8
Definition: nls.h:20
#define skip_2k3_crash
Definition: test.h:190
#define win_skip
Definition: test.h:164
int winetest_get_mainargs(char ***pargv)
#define skip_2k3_fail
Definition: test.h:191
#define wait_child_process
Definition: test.h:166
strcat
Definition: string.h:92
strcpy
Definition: string.h:131
#define tell(s)
Definition: getopt.c:28
__int64 CDECL _telli64(int fd)
Definition: file.c:2527
const char * descr
Definition: boot.c:45
static int fd
Definition: io.c:51
#define memset(x, y, z)
Definition: compat.h:39
#define setmode(f, m)
Definition: io.h:42
TCHAR * cmdline
Definition: stretchblt.cpp:32
PBYTE lpReserved2
Definition: winbase.h:870
char * _ptr
Definition: mbstring.h:20
char * _base
Definition: mbstring.h:22
int _cnt
Definition: mbstring.h:21
int _file
Definition: mbstring.h:24
int _bufsiz
Definition: mbstring.h:26
Definition: match.c:390
Definition: fci.c:127
Definition: file.c:59
int exflag
Definition: file.c:63
unsigned char wxflag
Definition: file.c:61
HANDLE handle
Definition: file.c:60
CRITICAL_SECTION crit
Definition: file.c:64
Definition: dhcpd.h:62
Definition: copy.c:22
Definition: name.c:39
Definition: stat.h:55
Definition: ps.c:97
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define setlocale(n, s)
Definition: locale.h:46
PVOID HANDLE
Definition: typedefs.h:73
int errno_t
Definition: corecrt.h:615
static EFI_HANDLE * handles
Definition: uefidisk.c:62
int intptr_t
Definition: vcruntime.h:134
int ret
int codepage
Definition: win_iconv.c:156
#define NORMAL_PRIORITY_CLASS
Definition: winbase.h:184
#define ZeroMemory
Definition: winbase.h:1737
#define STD_OUTPUT_HANDLE
Definition: winbase.h:294
#define FILE_END
Definition: winbase.h:115
#define STD_INPUT_HANDLE
Definition: winbase.h:293
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define STD_ERROR_HANDLE
Definition: winbase.h:295
#define CREATE_DEFAULT_ERROR_MODE
Definition: winbase.h:214
#define STARTF_USESTDHANDLES
Definition: winbase.h:525
#define WINAPI
Definition: msvc.h:6
wchar_t * fgetws(wchar_t *buf, int bufsize, FILE *file)
Definition: wmain.c:22
#define DUPLICATE_CLOSE_SOURCE
static unsigned int block
Definition: xmlmemory.c:101
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define _snprintf
Definition: xmlstorage.h:200
unsigned char BYTE
Definition: xxhash.c:193