ReactOS 0.4.16-dev-1311-g81a4d83
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#ifdef __REACTOS__
1806 if (is_reactos())
1807 {
1808 skip("Skipping test_file_inherit pipe test, because it hangs on ReactOS\n");
1809 }
1810 else
1811 {
1812#endif
1813 ret = _spawnvp(_P_WAIT, selfname, arg_v);
1814 ok(ret == 0, "_spawnvp returned %Id, errno %d\n", ret, errno);
1815 ret = tell(fd);
1816 ok(ret == 8, "bad position %Iu expecting 8\n", ret);
1817 lseek(fd, 0, SEEK_SET);
1818 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
1819 close (fd);
1820 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
1821 _write(pipefds[1], "a", 1);
1822 WaitForSingleObject(thread_handle, INFINITE);
1823 CloseHandle(thread_handle);
1824 close(pipefds[0]);
1825 close(pipefds[1]);
1826#ifdef __REACTOS__
1827 }
1828#endif
1829 /* make file handle inheritable */
1830 sa.nLength = sizeof(sa);
1831 sa.lpSecurityDescriptor = NULL;
1832 sa.bInheritHandle = TRUE;
1833 sprintf(cmdline, "%s file inherit 1", selfname);
1834
1835 /* init an empty Reserved2, which should not be recognized as inherit-block */
1836 ZeroMemory(&startup, sizeof(startup));
1837 startup.cb = sizeof(startup);
1839 test_stdout_handle( &startup, cmdline, 0, FALSE, "empty block" );
1840
1841 /* test with valid inheritblock */
1843 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1847 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "valid block" );
1848 CloseHandle( handles[1] );
1849 DeleteFileA("fdopen.tst");
1850
1851 /* test inherit block starting with unsigned zero */
1852 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1855 *(unsigned int *)startup.lpReserved2 = 0;
1856 test_stdout_handle( &startup, cmdline, handles[1], FALSE, "zero count block" );
1857 CloseHandle( handles[1] );
1858 DeleteFileA("fdopen.tst");
1859
1860 /* test inherit block with smaller size */
1861 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1864 startup.cbReserved2 -= 3;
1865 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "small size block" );
1866 CloseHandle( handles[1] );
1867 DeleteFileA("fdopen.tst");
1868
1869 /* test inherit block with even smaller size */
1870 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1873 startup.cbReserved2 = sizeof(unsigned int) + sizeof(HANDLE) + sizeof(char);
1874 test_stdout_handle( &startup, cmdline, handles[1], FALSE, "smaller size block" );
1875 CloseHandle( handles[1] );
1876 DeleteFileA("fdopen.tst");
1877
1878 /* test inherit block with larger size */
1879 handles[1] = CreateFileA( "fdopen.tst", GENERIC_READ|GENERIC_WRITE,
1882 startup.cbReserved2 += 7;
1883 test_stdout_handle( &startup, cmdline, handles[1], TRUE, "large size block" );
1884 CloseHandle( handles[1] );
1885 DeleteFileA("fdopen.tst");
1886
1887 /* test inherit block with invalid handle */
1890 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1891 test_stdout_handle( &startup, cmdline, NULL, FALSE, "INVALID_HANDLE_VALUE stdout handle" );
1892
1893 handles[1] = NULL;
1895 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1896 test_stdout_handle( &startup, cmdline, NULL, FALSE, "NULL stdout handle" );
1897
1898 handles[1] = (void *)0xdeadbeef;
1900 sprintf(cmdline, "%s file inherit 1 %p", selfname, handles[1]);
1901 test_stdout_handle( &startup, cmdline, NULL, FALSE, "invalid stdout handle" );
1902}
1903
1904static void test_invalid_stdin_child( void )
1905{
1906 HANDLE handle;
1907 ioinfo *info;
1908 int ret;
1909 char c;
1910
1911 errno = 0xdeadbeef;
1913 ok(handle == (HANDLE)-2, "handle = %p\n", handle);
1914 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
1916 ok((LONG_PTR)handle > 0, "Expecting passed handle to be untouched\n");
1917
1919 ok(info->handle == (HANDLE)-2, "info->handle = %p\n", info->handle);
1920 ok(info->wxflag == 0xc1, "info->wxflag = %x\n", info->wxflag);
1921
1922 ok(stdin->_file == -2, "stdin->_file = %d\n", stdin->_file);
1923
1924 errno = 0xdeadbeef;
1925 ret = fread(&c, 1, 1, stdin);
1926 ok(!ret, "fread(stdin) returned %d\n", ret);
1927 ok(errno == EBADF, "errno = %d\n", errno);
1928
1929 errno = 0xdeadbeef;
1930 ret = read(-2, &c, 1);
1931 ok(ret == -1, "read(-2) returned %d\n", ret);
1932 ok(errno == EBADF, "errno = %d\n", errno);
1933
1934 errno = 0xdeadbeef;
1935 ret = read(STDIN_FILENO, &c, 1);
1936 ok(ret == -1, "read(STDIN_FILENO) returned %d\n", ret);
1937 ok(errno == EBADF, "errno = %d\n", errno);
1938
1939 errno = 0xdeadbeef;
1940 ret = _flsbuf('a', stdin);
1941 ok(ret == EOF, "_flsbuf(stdin) returned %d\n", ret);
1942 ok(errno == EBADF, "errno = %d\n", errno);
1943
1944 errno = 0xdeadbeef;
1945 ret = fwrite(&c, 1, 1, stdin);
1946 ok(!ret, "fwrite(stdin) returned %d\n", ret);
1947 ok(errno == EBADF, "errno = %d\n", errno);
1948
1949 errno = 0xdeadbeef;
1950 ret = write(-2, &c, 1);
1951 ok(ret == -1, "write(-2) returned %d\n", ret);
1952 ok(errno == EBADF, "errno = %d\n", errno);
1953
1954 errno = 0xdeadbeef;
1955 ret = write(STDIN_FILENO, &c, 1);
1956 ok(ret == -1, "write(STDIN_FILENO) returned %d\n", ret);
1957 ok(errno == EBADF, "errno = %d\n", errno);
1958
1959 errno = 0xdeadbeef;
1960 ret = fclose(stdin);
1961 ok(ret == -1, "fclose(stdin) returned %d\n", ret);
1962 ok(errno == EBADF, "errno = %d\n", errno);
1963
1964 errno = 0xdeadbeef;
1965 ret = close(-2);
1966 ok(ret == -1, "close(-2) returned %d\n", ret);
1967 ok(errno == EBADF, "errno = %d\n", errno);
1968
1969 errno = 0xdeadbeef;
1971 ok(ret==-1 || !ret, "close(STDIN_FILENO) returned %d\n", ret);
1972 ok((ret==-1 && errno==EBADF) || (!ret && errno==0xdeadbeef), "errno = %d\n", errno);
1973}
1974
1975static void test_invalid_stdin( const char* selfname )
1976{
1977 char cmdline[MAX_PATH];
1981 HKEY key;
1982 LONG ret;
1983
1984 if(!p_fopen_s) {
1985 /* Behaviour of the dll has changed in newer version */
1986 win_skip("skipping invalid stdin tests\n");
1987 return;
1988 }
1989
1991 ok(!ret, "RegOpenCurrentUser failed: %lx\n", ret);
1992
1995 ok(ret, "DuplicateHandle failed: %lx\n", GetLastError());
1996
1997 sa.nLength = sizeof(sa);
1998 sa.lpSecurityDescriptor = NULL;
1999 sa.bInheritHandle = TRUE;
2000
2001 memset(&startup, 0, sizeof(startup));
2002 startup.cb = sizeof(startup);
2003 startup.dwFlags = STARTF_USESTDHANDLES;
2004 startup.hStdInput = key;
2007
2008 sprintf(cmdline, "%s file stdin", selfname);
2011 wait_child_process(proc.hProcess);
2012
2013 ret = RegCloseKey(key);
2014 ok(!ret, "RegCloseKey failed: %lx\n", ret);
2015}
2016
2017static void test_tmpnam( void )
2018{
2019 char name[MAX_PATH] = "abc";
2020 char *res;
2021
2022 res = tmpnam(NULL);
2023 ok(res != NULL, "tmpnam returned NULL\n");
2024 ok(res[0] == '\\', "first character is not a backslash\n");
2025 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
2026 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
2027
2028 res = tmpnam(name);
2029 ok(res != NULL, "tmpnam returned NULL\n");
2030 ok(res == name, "supplied buffer was not used\n");
2031 ok(res[0] == '\\', "first character is not a backslash\n");
2032 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
2033 ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
2034}
2035
2036static void test_chsize( void )
2037{
2038 int fd;
2039 LONG cur, pos, count;
2040 char temptext[] = "012345678";
2041 char *tempfile = _tempnam( ".", "tst" );
2042
2043 ok( tempfile != NULL, "Couldn't create test file\n" );
2044
2046 ok( fd > 0, "Couldn't open test file\n" );
2047
2048 count = _write( fd, temptext, sizeof(temptext) );
2049 ok( count > 0, "Couldn't write to test file\n" );
2050
2051 /* get current file pointer */
2052 cur = _lseek( fd, 0, SEEK_CUR );
2053
2054 /* make the file smaller */
2055 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
2056
2057 pos = _lseek( fd, 0, SEEK_CUR );
2058 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
2059 ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
2060
2061 /* enlarge the file */
2062 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
2063
2064 pos = _lseek( fd, 0, SEEK_CUR );
2065 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
2066 ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
2067
2068 _close( fd );
2069 _unlink( tempfile );
2070 free( tempfile );
2071}
2072
2074{
2075 char fname1[] = "empty1";
2076 char fname2[] = "empty2";
2077 char fname3[] = "empty3";
2078 FILE *stream1, *stream2, *stream3, *stream4;
2079 int ret, numclosed;
2080
2081 /* testing fopen() */
2082 stream1 = fopen(fname1, "w+");
2083 ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
2084 stream2 = fopen(fname2, "w ");
2085 ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
2086 _unlink(fname3);
2087 stream3 = fopen(fname3, "r");
2088 ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
2089 stream3 = fopen(fname3, "w+");
2090 ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
2091 errno = 0xfaceabad;
2092 stream4 = fopen("", "w+");
2093 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
2094 "filename is empty, errno = %d (expected 2 or 22)\n", errno);
2095 errno = 0xfaceabad;
2096 stream4 = fopen(NULL, "w+");
2097 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
2098 "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
2099
2100 /* testing fclose() */
2101 ret = fclose(stream2);
2102 ok(ret == 0, "The file '%s' was not closed\n", fname2);
2103 ret = fclose(stream3);
2104 ok(ret == 0, "The file '%s' was not closed\n", fname3);
2105 errno = 0xdeadbeef;
2106 ret = fclose(stream2);
2107 ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
2108 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2109 ret = fclose(stream3);
2110 ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
2111 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2113 ret = fclose(NULL);
2114 ok(ret == EOF, "Closing NULL file returned %d\n", ret);
2115 ok(errno == EINVAL, "errno = %d\n", errno);
2116 }
2117
2118 /* testing fcloseall() */
2119 numclosed = _fcloseall();
2120 /* fname1 should be closed here */
2121 ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
2122 numclosed = _fcloseall();
2123 ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
2124
2125 ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
2126 ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
2127 ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
2128}
2129
2130static void test_fopen_s( void )
2131{
2132 const char name[] = "empty1";
2133 char buff[16];
2134 unsigned char *ubuff = (unsigned char*)buff;
2135 FILE *file, *file2;
2136 int ret;
2137 int len;
2138
2139 if (!p_fopen_s)
2140 {
2141 win_skip("Skipping fopen_s test\n");
2142 return;
2143 }
2144 /* testing fopen_s */
2145 ret = p_fopen_s(&file, name, "w");
2146 ok(ret == 0, "fopen_s failed with %d\n", ret);
2147 ok(file != 0, "fopen_s failed to return value\n");
2148 fwrite(name, sizeof(name), 1, file);
2149
2150 ret = fclose(file);
2151 ok(ret != EOF, "File failed to close\n");
2152
2153 file = fopen(name, "r");
2154 ok(file != 0, "fopen failed\n");
2155 len = fread(buff, 1, sizeof(name), file);
2156 ok(len == sizeof(name), "File length is %d\n", len);
2157 buff[sizeof(name)] = '\0';
2158 ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
2159
2160 ret = fclose(file);
2161 ok(ret != EOF, "File failed to close\n");
2162
2163 ret = p_fopen_s(&file, name, "w, ccs=UNIcode");
2164 ok(ret == 0, "fopen_s failed with %d\n", ret);
2165 ret = fwrite("a", 1, 2, file);
2166 ok(ret == 2, "fwrite returned %d\n", ret);
2167 fclose(file);
2168
2169 ret = p_fopen_s(&file, name, "r");
2170 ok(ret == 0, "fopen_s failed with %d\n", ret);
2171 len = fread(buff, 1, 2, file);
2172 ok(len == 2, "len = %d\n", len);
2173 ok(ubuff[0]==0xff && ubuff[1]==0xfe, "buff[0]=%02x, buff[1]=%02x\n",
2174 ubuff[0], ubuff[1]);
2175 fclose(file);
2176
2177 ret = p_fopen_s(&file, name, "r,ccs=unicode");
2178 ok(ret == 0, "fopen_s failed with %d\n", ret);
2179 len = fread(buff, 1, 2, file);
2180 ok(len == 2, "len = %d\n", len);
2181 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2182 ubuff[0], ubuff[1]);
2183 fclose(file);
2184
2185 ret = p_fopen_s(&file, name, "r,ccs=utf-16le");
2186 ok(ret == 0, "fopen_s failed with %d\n", ret);
2187 len = fread(buff, 1, 2, file);
2188 ok(len == 2, "len = %d\n", len);
2189 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2190 ubuff[0], ubuff[1]);
2191 fclose(file);
2192
2193 ret = p_fopen_s(&file, name, "r,ccs=utf-8");
2194 ok(ret == 0, "fopen_s failed with %d\n", ret);
2195 len = fread(buff, 1, 2, file);
2196 ok(len == 2, "len = %d\n", len);
2197 ok(ubuff[0]=='a' && ubuff[1]==0, "buff[0]=%02x, buff[1]=%02x\n",
2198 ubuff[0], ubuff[1]);
2199 fclose(file);
2200
2201 ret = p_fopen_s(&file, name, "w,ccs=utf-16le");
2202 ok(ret == 0, "fopen_s failed with %d\n", ret);
2203 fclose(file);
2204
2205 ret = p_fopen_s(&file, name, "r");
2206 ok(ret == 0, "fopen_s failed with %d\n", ret);
2207 len = fread(buff, 1, 3, file);
2208 ok(len == 2, "len = %d\n", len);
2209 ok(ubuff[0]==0xff && ubuff[1]==0xfe, "buff[0]=%02x, buff[1]=%02x\n",
2210 ubuff[0], ubuff[1]);
2211 fclose(file);
2212
2213 ret = p_fopen_s(&file, name, "w,ccs=utf-8");
2214 ok(ret == 0, "fopen_s failed with %d\n", ret);
2215 fclose(file);
2216
2217 ret = p_fopen_s(&file, name, "r");
2218 ok(ret == 0, "fopen_s failed with %d\n", ret);
2219 len = fread(buff, 1, 4, file);
2220 ok(len == 3, "len = %d\n", len);
2221 ok(ubuff[0]==0xef && ubuff[1]==0xbb && ubuff[2]==0xbf,
2222 "buff[0]=%02x, buff[1]=%02x, buff[2]=%02x\n",
2223 ubuff[0], ubuff[1], ubuff[2]);
2224 fclose(file);
2225
2226 /* test initial FILE values */
2227 memset(file, 0xfe, sizeof(*file));
2228 file->_flag = 0;
2229 ret = p_fopen_s(&file2, name, "r");
2230 ok(!ret, "fopen_s failed with %d\n", ret);
2231 ok(file == file2, "file != file2 %p %p\n", file, file2);
2232 ok(!file->_ptr, "file->_ptr != NULL\n");
2233 ok(!file->_cnt, "file->_cnt != 0\n");
2234 ok(!file->_base, "file->_base != NULL\n");
2235 ok(file->_flag == 1, "file->_flag = %x\n", file->_flag);
2236 ok(file->_file, "file->_file == 0\n");
2237 ok(file->_charbuf == 0xfefefefe, "file->_charbuf = %x\n", file->_charbuf);
2238 ok(file->_bufsiz == 0xfefefefe, "file->_bufsiz = %x\n", file->_bufsiz);
2239 ok(!file->_tmpfname, "file->_tmpfname != NULL\n");
2240 fclose(file2);
2241
2242 ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
2243}
2244
2245static void test__wfopen_s( void )
2246{
2247 const char name[] = "empty1";
2248 char buff[16];
2249 FILE *file;
2250 int ret;
2251 int len;
2252
2253 if (!p__wfopen_s)
2254 {
2255 win_skip("Skipping _wfopen_s test\n");
2256 return;
2257 }
2258 /* testing _wfopen_s */
2259 ret = p__wfopen_s(&file, L"empty1", L"w");
2260 ok(ret == 0, "_wfopen_s failed with %d\n", ret);
2261 ok(file != 0, "_wfopen_s failed to return value\n");
2262 fwrite(name, sizeof(name), 1, file);
2263
2264 ret = fclose(file);
2265 ok(ret != EOF, "File failed to close\n");
2266
2267 file = fopen(name, "r");
2268 ok(file != 0, "fopen failed\n");
2269 len = fread(buff, 1, sizeof(name), file);
2270 ok(len == sizeof(name), "File length is %d\n", len);
2271 buff[sizeof(name)] = '\0';
2272 ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
2273
2274 ret = fclose(file);
2275 ok(ret != EOF, "File failed to close\n");
2276
2277 ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
2278}
2279
2280static void test_setmode(void)
2281{
2282 const char name[] = "empty1";
2283 int fd, ret;
2284
2285 if(!p_fopen_s) {
2286 win_skip("unicode file modes are not available, skipping setmode tests\n");
2287 return;
2288 }
2289
2290 errno = 0xdeadbeef;
2291 ret = _setmode(-2, 0);
2292 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2293 ok(errno == EINVAL, "errno = %d\n", errno);
2294
2295 errno = 0xdeadbeef;
2296 ret = _setmode(-2, _O_TEXT);
2297 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2298 ok(errno == EBADF, "errno = %d\n", errno);
2299
2301 ok(fd != -1, "failed to open file\n");
2302
2303 errno = 0xdeadbeef;
2304 ret = _setmode(fd, 0xffffffff);
2305 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2306 ok(errno == EINVAL, "errno = %d\n", errno);
2307
2308 errno = 0xdeadbeef;
2309 ret = _setmode(fd, 0);
2310 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2311 ok(errno == EINVAL, "errno = %d\n", errno);
2312
2313 errno = 0xdeadbeef;
2315 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2316 ok(errno == EINVAL, "errno = %d\n", errno);
2317
2318 errno = 0xdeadbeef;
2320 ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
2321 ok(errno == EINVAL, "errno = %d\n", errno);
2322
2324 ok(ret == _O_TEXT, "_setmode returned %x, expected _O_TEXT\n", ret);
2325
2326 ret = _setmode(fd, _O_WTEXT);
2327 ok(ret == _O_BINARY, "_setmode returned %x, expected _O_BINARY\n", ret);
2328
2329 ret = _setmode(fd, _O_TEXT);
2330 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2331
2333 ok(ret == _O_TEXT, "_setmode returned %x, expected _O_TEXT\n", ret);
2334
2336 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2337
2338 ret = _setmode(fd, _O_TEXT);
2339 ok(ret == _O_WTEXT, "_setmode returned %x, expected _O_WTEXT\n", ret);
2340
2341 _close(fd);
2342 _unlink(name);
2343}
2344
2345static void test_get_osfhandle(void)
2346{
2347 int fd;
2348 char fname[] = "t_get_osfhanle";
2349 DWORD bytes_written;
2350 HANDLE handle;
2351
2354 WriteFile(handle, "bar", 3, &bytes_written, NULL);
2355 _close(fd);
2356 fd = _open(fname, _O_RDONLY, 0);
2357 ok(fd != -1, "Couldn't open '%s' after _get_osfhandle()\n", fname);
2358
2359 _close(fd);
2360 _unlink(fname);
2361
2362 errno = 0xdeadbeef;
2364 ok(handle == INVALID_HANDLE_VALUE, "_get_osfhandle returned %p\n", handle);
2365 ok(errno == EBADF, "errno = %d\n", errno);
2366}
2367
2368static void test_setmaxstdio(void)
2369{
2370 if (p__setmaxstdio)
2371 {
2372 ok(2048 == p__setmaxstdio(2048),"_setmaxstdio returned %d instead of 2048\n",p__setmaxstdio(2048));
2373 ok(-1 == p__setmaxstdio(2049),"_setmaxstdio returned %d instead of -1\n",p__setmaxstdio(2049));
2374 }
2375 else win_skip( "_setmaxstdio not supported\n" );
2376}
2377
2378static void test_stat(void)
2379{
2380 int fd;
2381 int pipes[2];
2382 int ret;
2383 struct stat buf;
2384
2385 /* Tests for a file */
2386 fd = open("stat.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
2387 if (fd >= 0)
2388 {
2389 ret = fstat(fd, &buf);
2390 ok(!ret, "fstat failed: errno=%d\n", errno);
2391 ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode);
2392 ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode);
2393 ok(buf.st_dev == 0, "st_dev is %d, expected 0\n", buf.st_dev);
2394 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2395 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2396 ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size);
2397
2398 ret = stat("stat.tst", &buf);
2399 ok(!ret, "stat failed: errno=%d\n", errno);
2400 ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode);
2401 ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode);
2402 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2403 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2404 ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size);
2405
2406 errno = 0xdeadbeef;
2407 ret = stat("stat.tst\\", &buf);
2408 ok(ret == -1, "stat returned %d\n", ret);
2409 ok(errno == ENOENT, "errno = %d\n", errno);
2410
2411 close(fd);
2412 remove("stat.tst");
2413 }
2414 else
2415 skip("open failed with errno %d\n", errno);
2416
2417 /* Tests for a char device */
2418 if (_dup2(0, 10) == 0)
2419 {
2420 ret = fstat(10, &buf);
2421 ok(!ret, "fstat(stdin) failed: errno=%d\n", errno);
2422 if ((buf.st_mode & _S_IFMT) == _S_IFCHR)
2423 {
2424 ok(buf.st_mode == _S_IFCHR, "bad st_mode=%06o\n", buf.st_mode);
2425 ok(buf.st_dev == 10, "st_dev is %d, expected 10\n", buf.st_dev);
2426 ok(buf.st_rdev == 10, "st_rdev is %d, expected 10\n", buf.st_rdev);
2427 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2428 }
2429 else
2430 skip("stdin is not a char device? st_mode=%06o\n", buf.st_mode);
2431 close(10);
2432 }
2433 else
2434 skip("_dup2 failed with errno %d\n", errno);
2435
2436 /* Tests for pipes */
2437 if (_pipe(pipes, 1024, O_BINARY) == 0)
2438 {
2439 ret = fstat(pipes[0], &buf);
2440 ok(!ret, "fstat(pipe) failed: errno=%d\n", errno);
2441 ok(buf.st_mode == _S_IFIFO, "bad st_mode=%06o\n", buf.st_mode);
2442 ok(buf.st_dev == pipes[0], "st_dev is %d, expected %d\n", buf.st_dev, pipes[0]);
2443 ok(buf.st_rdev == pipes[0], "st_rdev is %d, expected %d\n", buf.st_rdev, pipes[0]);
2444 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2445 close(pipes[0]);
2446 close(pipes[1]);
2447 }
2448 else
2449 skip("pipe failed with errno %d\n", errno);
2450
2451 /* Tests for directory */
2452 if(mkdir("stat.tst") == 0)
2453 {
2454 ret = stat("stat.tst ", &buf);
2455 ok(!ret, "stat(directory) failed: errno=%d\n", errno);
2456 ok((buf.st_mode & _S_IFMT) == _S_IFDIR, "bad format = %06o\n", buf.st_mode);
2457 ok((buf.st_mode & 0777) == 0777, "bad st_mode = %06o\n", buf.st_mode);
2458 ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev);
2459 ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink);
2460
2461 errno = 0xdeadbeef;
2462 ret = stat("stat.tst\\ ", &buf);
2463 ok(ret == -1, "stat returned %d\n", ret);
2464 ok(errno == ENOENT, "errno = %d\n", errno);
2465 rmdir( "stat.tst" );
2466 }
2467 else
2468 skip("mkdir failed with errno %d\n", errno);
2469
2470 errno = 0xdeadbeef;
2471 ret = stat("c:", &buf);
2472 ok(ret == -1, "stat returned %d\n", ret);
2473 ok(errno == ENOENT, "errno = %d\n", errno);
2474
2475 ret = stat("c:/", &buf);
2476 ok(!ret, "stat returned %d\n", ret);
2477 ok(buf.st_dev == 2, "st_dev = %d\n", buf.st_dev);
2478 ok(buf.st_rdev == 2, "st_rdev = %d\n", buf.st_rdev);
2479}
2480
2481static const char* pipe_string="Hello world";
2482
2483/* How many messages to transfer over the pipe */
2484#define N_TEST_MESSAGES 3
2485
2486static void test_pipes_child(int argc, char** args)
2487{
2488 int fd;
2489 int nwritten;
2490 int i;
2491
2492 if (argc < 5)
2493 {
2494 ok(0, "not enough parameters: %d\n", argc);
2495 return;
2496 }
2497
2498 fd=atoi(args[3]);
2499 i=close(fd);
2500 ok(!i, "unable to close %d: %d\n", fd, errno);
2501
2502 fd=atoi(args[4]);
2503
2504 for (i=0; i<N_TEST_MESSAGES; i++) {
2505 nwritten=write(fd, pipe_string, strlen(pipe_string));
2506 ok(nwritten == strlen(pipe_string), "i %d, expected to write '%s' wrote %d\n", i, pipe_string, nwritten);
2507 /* let other process wake up so they can show off their "keep reading until EOF" behavior */
2508 if (i < N_TEST_MESSAGES-1)
2509 Sleep(100);
2510 }
2511
2512 i=close(fd);
2513 ok(!i, "unable to close %d: %d\n", fd, errno);
2514}
2515
2516static void test_pipes(const char* selfname)
2517{
2518 int pipes[2];
2519 char str_fdr[12], str_fdw[12];
2520 FILE* file;
2521 const char* arg_v[6];
2522 char buf[4096];
2523 char expected[4096];
2524 int r;
2525 int i;
2526#ifdef __REACTOS__
2527 if (is_reactos())
2528 {
2529 win_skip("Skipping test_pipes, because it hangs on ReactOS\n");
2530 return;
2531 }
2532#endif
2533 /* Test reading from a pipe with read() */
2534 if (_pipe(pipes, 1024, O_BINARY) < 0)
2535 {
2536 ok(0, "pipe failed with errno %d\n", errno);
2537 return;
2538 }
2539
2540 arg_v[0] = get_base_name(selfname);
2541 arg_v[1] = "file";
2542 arg_v[2] = "pipes";
2543 arg_v[3] = str_fdr; sprintf(str_fdr, "%d", pipes[0]);
2544 arg_v[4] = str_fdw; sprintf(str_fdw, "%d", pipes[1]);
2545 arg_v[5] = NULL;
2547 i=close(pipes[1]);
2548 ok(!i, "unable to close %d: %d\n", pipes[1], errno);
2549
2550 for (i=0; i<N_TEST_MESSAGES; i++) {
2551 r=read(pipes[0], buf, sizeof(buf)-1);
2552 ok(r == strlen(pipe_string), "i %d, got %d\n", i, r);
2553 if (r > 0)
2554 buf[r]='\0';
2555 ok(strcmp(buf, pipe_string) == 0, "expected to read '%s', got '%s'\n", pipe_string, buf);
2556 }
2557
2558 r=read(pipes[0], buf, sizeof(buf)-1);
2559 ok(r == 0, "expected to read 0 bytes, got %d\n", r);
2560 i=close(pipes[0]);
2561 ok(!i, "unable to close %d: %d\n", pipes[0], errno);
2562
2563 /* Test reading from a pipe with fread() */
2564 if (_pipe(pipes, 1024, O_BINARY) < 0)
2565 {
2566 ok(0, "pipe failed with errno %d\n", errno);
2567 return;
2568 }
2569
2570 arg_v[1] = "file";
2571 arg_v[2] = "pipes";
2572 arg_v[3] = str_fdr; sprintf(str_fdr, "%d", pipes[0]);
2573 arg_v[4] = str_fdw; sprintf(str_fdw, "%d", pipes[1]);
2574 arg_v[5] = NULL;
2576 i=close(pipes[1]);
2577 ok(!i, "unable to close %d: %d\n", pipes[1], errno);
2578 file=fdopen(pipes[0], "r");
2579
2580 /* In blocking mode, fread will keep calling read() until it gets
2581 * enough bytes, or EOF, even on Unix. (If this were a Unix terminal
2582 * in cooked mode instead of a pipe, it would also stop on EOL.)
2583 */
2584 expected[0] = 0;
2585 for (i=0; i<N_TEST_MESSAGES; i++)
2587 r=fread(buf, 1, sizeof(buf)-1, file);
2588 ok(r == strlen(expected), "fread() returned %d: ferror=%d\n", r, ferror(file));
2589 if (r > 0)
2590 buf[r]='\0';
2591 ok(strcmp(buf, expected) == 0, "got '%s' expected '%s'\n", buf, expected);
2592
2593 /* Let child close the file before we read, so we can sense EOF reliably */
2594 Sleep(100);
2595 r=fread(buf, 1, sizeof(buf)-1, file);
2596 ok(r == 0, "fread() returned %d instead of 0\n", r);
2597 ok(ferror(file) == 0, "got ferror() = %d\n", ferror(file));
2598 ok(feof(file), "feof() is false!\n");
2599
2600 i=fclose(file);
2601 ok(!i, "unable to close the pipe: %d\n", errno);
2602
2603 /* test \r handling when it's the last character read */
2604 if (_pipe(pipes, 1024, O_BINARY) < 0)
2605 {
2606 ok(0, "pipe failed with errno %d\n", errno);
2607 return;
2608 }
2609 r = write(pipes[1], "\r\n\rab\r\n", 7);
2610 ok(r == 7, "write returned %d, errno = %d\n", r, errno);
2611 setmode(pipes[0], O_TEXT);
2612 r = read(pipes[0], buf, 1);
2613 ok(r == 1, "read returned %d, expected 1\n", r);
2614 ok(buf[0] == '\n', "buf[0] = %x, expected '\\n'\n", buf[0]);
2615 r = read(pipes[0], buf, 1);
2616 ok(r == 1, "read returned %d, expected 1\n", r);
2617 ok(buf[0] == '\r', "buf[0] = %x, expected '\\r'\n", buf[0]);
2618 r = read(pipes[0], buf, 1);
2619 ok(r == 1, "read returned %d, expected 1\n", r);
2620 ok(buf[0] == 'a', "buf[0] = %x, expected 'a'\n", buf[0]);
2621 r = read(pipes[0], buf, 2);
2622 ok(r == 2, "read returned %d, expected 1\n", r);
2623 ok(buf[0] == 'b', "buf[0] = %x, expected 'b'\n", buf[0]);
2624 ok(buf[1] == '\n', "buf[1] = %x, expected '\\n'\n", buf[1]);
2625
2626 if (p_fopen_s)
2627 {
2628 /* test utf16 read with insufficient data */
2629 r = write(pipes[1], "a\0b", 3);
2630 ok(r == 3, "write returned %d, errno = %d\n", r, errno);
2631 buf[2] = 'z';
2632 buf[3] = 'z';
2633 setmode(pipes[0], _O_WTEXT);
2634 r = read(pipes[0], buf, 4);
2635 ok(r == 2, "read returned %d, expected 2\n", r);
2636 ok(!memcmp(buf, "a\0bz", 4), "read returned incorrect data\n");
2637 r = write(pipes[1], "\0", 1);
2638 ok(r == 1, "write returned %d, errno = %d\n", r, errno);
2639 buf[0] = 'z';
2640 buf[1] = 'z';
2641 r = read(pipes[0], buf, 2);
2642 ok(r == 0, "read returned %d, expected 0\n", r);
2643 ok(!memcmp(buf, "\0z", 2), "read returned incorrect data\n");
2644 }
2645 else
2646 {
2647 win_skip("unicode mode tests on pipe\n");
2648 }
2649
2650 close(pipes[1]);
2651 close(pipes[0]);
2652}
2653
2654static void test_unlink(void)
2655{
2656 FILE* file;
2657 ok(mkdir("test_unlink") == 0, "unable to create test dir\n");
2658 file = fopen("test_unlink\\empty", "w");
2659 ok(file != NULL, "unable to create test file\n");
2660 if(file)
2661 fclose(file);
2662 ok(_unlink("test_unlink") != 0, "unlinking a non-empty directory must fail\n");
2663 unlink("test_unlink\\empty");
2664 rmdir("test_unlink");
2665}
2666
2667static void test_dup2(void)
2668{
2669 ok(-1 == _dup2(0, -1), "expected _dup2 to fail when second arg is negative\n" );
2670}
2671
2672static void test_stdin(void)
2673{
2675 int stdin_dup, fd;
2676 HANDLE h;
2677 DWORD r;
2678
2679 stdin_dup = _dup(STDIN_FILENO);
2680 ok(stdin_dup != -1, "_dup(STDIN_FILENO) failed\n");
2681
2683 "GetStdHandle(STD_INPUT_HANDLE) != _get_osfhandle(STDIN_FILENO)\n");
2684
2686 ok(r == TRUE, "SetStdHandle returned %lx, expected TRUE\n", r);
2688 ok(h == INVALID_HANDLE_VALUE, "h = %p\n", h);
2689
2692 ok(h == NULL, "h != NULL\n");
2693
2694 fd = open("stdin.tst", O_WRONLY | O_CREAT, _S_IREAD |_S_IWRITE);
2695 ok(fd != -1, "open failed\n");
2696 ok(fd == STDIN_FILENO, "fd = %d, expected STDIN_FILENO\n", fd);
2698 ok(h != NULL, "h == NULL\n");
2699 close(fd);
2700 unlink("stdin.tst");
2701
2702 r = _dup2(stdin_dup, STDIN_FILENO);
2703 ok(r != -1, "_dup2 failed\n");
2705 ok(h != NULL, "h == NULL\n");
2706}
2707
2708static void test_mktemp(void)
2709{
2710 char buf[16];
2711
2712 strcpy(buf, "a");
2713 ok(!_mktemp(buf), "_mktemp(\"a\") != NULL\n");
2714
2715 strcpy(buf, "testXXXXX");
2716 ok(!_mktemp(buf), "_mktemp(\"testXXXXX\") != NULL\n");
2717
2718 strcpy(buf, "testXXXXXX");
2719 ok(_mktemp(buf) != NULL, "_mktemp(\"testXXXXXX\") == NULL\n");
2720
2721 strcpy(buf, "testXXXXXXa");
2722 ok(!_mktemp(buf), "_mktemp(\"testXXXXXXa\") != NULL\n");
2723
2724 strcpy(buf, "**XXXXXX");
2725 ok(_mktemp(buf) != NULL, "_mktemp(\"**XXXXXX\") == NULL\n");
2726}
2727
2728static void test__open_osfhandle(void)
2729{
2730 ioinfo *info;
2731 HANDLE h, tmp;
2732 int fd;
2733
2734 errno = 0xdeadbeef;
2736 ok(fd == -1, "_open_osfhandle returned %d\n", fd);
2737 ok(errno == EBADF, "errno = %d\n", errno);
2738
2739 h = CreateFileA("open_osfhandle.tst", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
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 == 1, "info->wxflag = %x, expected 1\n", info->wxflag);
2745 close(fd);
2746 ok(info->handle == INVALID_HANDLE_VALUE, "info->handle = %p, expected INVALID_HANDLE_VALUE\n", info->handle);
2747 ok(info->wxflag == 0, "info->wxflag = %x, expected 0\n", info->wxflag);
2748 DeleteFileA("open_osfhandle.tst");
2749
2750 errno = 0xdeadbeef;
2752 ok(fd == -1, "_open_osfhandle returned %d\n", fd);
2753 ok(errno == EBADF, "errno = %d\n", errno);
2754
2755 ok(CreatePipe(&h, &tmp, NULL, 0), "CreatePipe failed\n");
2757 ok(fd > 0, "_open_osfhandle returned %d (%d)\n", fd, errno);
2759 ok(info->handle == h, "info->handle = %p, expected %p\n", info->handle, h);
2760 ok(info->wxflag == 9, "info->wxflag = %x, expected 9\n", info->wxflag);
2761 close(fd);
2762 CloseHandle(tmp);
2763}
2764
2766{
2767 char *inbuffer;
2768 char *outbuffer;
2769 int size, fd;
2770 fpos_t pos, pos2;
2771
2772 fd = fileno(file);
2773 inbuffer = calloc(1, bufsize + 1);
2774 outbuffer = calloc(1, bufsize + 1);
2775 _snprintf(outbuffer, bufsize + 1, "0,1,2,3,4,5,6,7,8,9");
2776
2777 for (size = bufsize + 1; size >= bufsize - 1; size--) {
2778 rewind(file);
2779 ok(file->_cnt == 0, "_cnt should be 0 after rewind, but is %d\n", file->_cnt);
2780 fwrite(outbuffer, 1, size, file);
2781 /* lseek() below intentionally redirects the write in fflush() to detect
2782 * if fwrite() has already flushed the whole buffer or not.
2783 */
2784 lseek(fd, 1, SEEK_SET);
2785 fflush(file);
2786 ok(file->_cnt == 0, "_cnt should be 0 after fflush, but is %d\n", file->_cnt);
2787 fseek(file, 0, SEEK_SET);
2788 ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
2789 if (size == bufsize)
2790 ok(memcmp(outbuffer, inbuffer, bufsize) == 0, "missing flush by %d byte write\n", size);
2791 else
2792 ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d byte write\n", size);
2793 }
2794 rewind(file);
2795 fwrite(outbuffer, 1, bufsize / 2, file);
2796 fwrite(outbuffer + bufsize / 2, 1, bufsize / 2, file);
2797 lseek(fd, 1, SEEK_SET);
2798 fflush(file);
2799 fseek(file, 0, SEEK_SET);
2800 ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
2801 ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize);
2802
2803 ok(!fseek(file, -1, SEEK_END), "fseek failed\n");
2804 ok(!fgetpos(file, &pos), "fgetpos failed\n");
2805 ok(fread(inbuffer, 1, 1, file) == 1, "fread failed\n");
2806 ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
2807 ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
2808 ok(file->_ptr != file->_base, "file->_ptr == file->_base\n");
2809 ok(fwrite(outbuffer, 1, bufsize, file), "fwrite failed\n");
2810 ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
2811 ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
2812 ok(file->_ptr == file->_base, "file->_ptr == file->_base\n");
2813 ok(!fgetpos(file, &pos2), "fgetpos failed\n");
2814 ok(pos+bufsize+1 == pos2, "pos = %d (%d)\n", (int)pos, (int)pos2);
2815 free(inbuffer);
2816 free(outbuffer);
2817}
2818
2819static void test_write_flush(void)
2820{
2821 char iobuf[1024];
2822 char *tempf;
2823 FILE *file;
2824
2825 tempf = _tempnam(".","wne");
2826 file = fopen(tempf, "wb+");
2827 ok(file != NULL, "unable to create test file\n");
2828 iobuf[0] = 0;
2829 ok(file->_bufsiz == 4096, "incorrect default buffer size: %d\n", file->_bufsiz);
2830 test_write_flush_size(file, file->_bufsiz);
2831 setvbuf(file, iobuf, _IOFBF, sizeof(iobuf));
2832 test_write_flush_size(file, sizeof(iobuf));
2833 fclose(file);
2834 unlink(tempf);
2835 free(tempf);
2836}
2837
2838static void test_close(void)
2839{
2840 ioinfo *stdout_info, stdout_copy, *stderr_info, stderr_copy;
2841 int fd1, fd2, ret1, ret2, ret3, ret4;
2842 DWORD flags;
2843 HANDLE h;
2844
2845 /* test close on fds that use the same handle */
2846 h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
2848 ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
2849
2850 fd1 = _open_osfhandle((intptr_t)h, 0);
2851 ok(fd1 != -1, "_open_osfhandle failed (%d)\n", errno);
2852 fd2 = _open_osfhandle((intptr_t)h, 0);
2853 ok(fd2 != -1, "_open_osfhandle failed (%d)\n", errno);
2854 ok(fd1 != fd2, "fd1 == fd2\n");
2855
2856 ok((HANDLE)_get_osfhandle(fd1) == h, "handles mismatch (%p != %p)\n",
2857 (HANDLE)_get_osfhandle(fd1), h);
2858 ok((HANDLE)_get_osfhandle(fd2) == h, "handles mismatch (%p != %p)\n",
2859 (HANDLE)_get_osfhandle(fd2), h);
2860 ret1 = close(fd1);
2861 ok(!ret1, "close(fd1) failed (%d)\n", errno);
2862 ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n");
2863 ok(close(fd2), "close(fd2) succeeded\n");
2864
2865 /* test close on already closed fd */
2866 errno = 0xdeadbeef;
2867 ret1 = close(fd1);
2868 ok(ret1 == -1, "close(fd1) succeeded\n");
2869 ok(errno == 9, "errno = %d\n", errno);
2870
2871 /* test close on stdout and stderr that use the same handle */
2872 h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
2874 ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
2875
2876 /* tests output will not be visible from now on */
2879 stdout_copy = *stdout_info;
2880 stderr_copy = *stderr_info;
2881 stdout_info->handle = h;
2882 stderr_info->handle = h;
2883
2884 ret1 = close(STDOUT_FILENO);
2885 ret2 = GetHandleInformation(h, &flags);
2886 ret3 = close(STDERR_FILENO);
2887 ret4 = GetHandleInformation(h, &flags);
2888
2889 *stdout_info = stdout_copy;
2890 *stderr_info = stderr_copy;
2891 SetStdHandle(STD_OUTPUT_HANDLE, stdout_info->handle);
2892 SetStdHandle(STD_ERROR_HANDLE, stderr_info->handle);
2893 /* stdout and stderr restored */
2894
2895 ok(!ret1, "close(STDOUT_FILENO) failed\n");
2896 ok(ret2, "GetHandleInformation failed\n");
2897 ok(!ret3, "close(STDERR_FILENO) failed\n");
2898 ok(!ret4, "GetHandleInformation succeeded\n");
2899
2900 DeleteFileA( "fdopen.tst" );
2901}
2902
2903static void test__creat(void)
2904{
2905 int fd, pos, count, readonly, old_fmode = 0, have_fmode;
2906 char buf[6], testdata[4] = {'a', '\n', 'b', '\n'};
2907
2908 have_fmode = p__get_fmode && p__set_fmode && !p__get_fmode(&old_fmode);
2909 if (!have_fmode)
2910 win_skip("_fmode can't be set, skipping mode tests\n");
2911
2912 if (have_fmode)
2913 p__set_fmode(_O_TEXT);
2914 fd = _creat("_creat.tst", 0);
2915 ok(fd > 0, "_creat failed\n");
2916 _write(fd, testdata, 4);
2917 if (have_fmode) {
2918 pos = _tell(fd);
2919 ok(pos == 6, "expected pos 6 (text mode), got %d\n", pos);
2920 }
2921 ok(_lseek(fd, 0, SEEK_SET) == 0, "_lseek failed\n");
2922 count = _read(fd, buf, 6);
2923 ok(count == 4, "_read returned %d, expected 4\n", count);
2924 count = count > 0 ? count > 4 ? 4 : count : 0;
2925 ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
2926 _close(fd);
2927 readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
2928 ok(readonly, "expected read-only file\n");
2930 DeleteFileA("_creat.tst");
2931
2932 if (have_fmode)
2933 p__set_fmode(_O_BINARY);
2934 fd = _creat("_creat.tst", _S_IREAD | _S_IWRITE);
2935 ok(fd > 0, "_creat failed\n");
2936 _write(fd, testdata, 4);
2937 if (have_fmode) {
2938 pos = _tell(fd);
2939 ok(pos == 4, "expected pos 4 (binary mode), got %d\n", pos);
2940 }
2941 ok(_lseek(fd, 0, SEEK_SET) == 0, "_lseek failed\n");
2942 count = _read(fd, buf, 6);
2943 ok(count == 4, "_read returned %d, expected 4\n", count);
2944 count = count > 0 ? count > 4 ? 4 : count : 0;
2945 ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
2946 _close(fd);
2947 readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
2948 ok(!readonly, "expected rw file\n");
2950 DeleteFileA("_creat.tst");
2951
2952 if (have_fmode)
2953 p__set_fmode(old_fmode);
2954}
2955
2956static void test_lseek(void)
2957{
2958 int fd;
2959 char testdata[4] = {'a', '\n', 'b', '\n'};
2960
2961 errno = 0xdeadbeef;
2962 ok(_lseek(-42, 0, SEEK_SET) == -1, "expected failure\n");
2963 ok(errno == EBADF, "errno = %d\n", errno);
2964
2965 fd = _creat("_creat.tst", _S_IWRITE);
2966 ok(fd > 0, "_creat failed\n");
2967 _write(fd, testdata, 4);
2968
2969 errno = 0xdeadbeef;
2970 ok(_lseek(fd, 0, 42) == -1, "expected failure\n");
2971 ok(errno == EINVAL, "errno = %d\n", errno);
2972
2973 errno = 0xdeadbeef;
2974 ok(_lseek(fd, -42, SEEK_SET) == -1, "expected failure\n");
2975 ok(errno == EINVAL, "errno = %d\n", errno);
2976
2977 _close(fd);
2978 DeleteFileA("_creat.tst");
2979}
2980
2982{
2983 HANDLE handle;
2984 FILE_MODE_INFORMATION mode_info;
2987
2989 status = NtQueryInformationFile(handle, &io, &mode_info, sizeof(mode_info),
2991 ok(!status, "NtQueryInformationFile failed\n");
2992 return (mode_info.Mode & FILE_SEQUENTIAL_ONLY) != 0;
2993}
2994
2995static void test_fopen_hints(void)
2996{
2997 static const struct {
2998 const char *mode;
2999 BOOL seq;
3000 } tests[] = {
3001 { "rb", FALSE },
3002 { "rbS", TRUE },
3003 { "rbR", FALSE },
3004 { "rbSR", TRUE },
3005 { "rbRS", FALSE }
3006 };
3007
3008 char temppath[MAX_PATH], tempfile[MAX_PATH];
3009 FILE *fp;
3010 int i;
3011
3012 GetTempPathA(MAX_PATH, temppath);
3013 GetTempFileNameA(temppath, "", 0, tempfile);
3014
3015 for (i = 0; i < ARRAY_SIZE(tests); ++i)
3016 {
3017 fp = fopen(tempfile, tests[i].mode);
3018 ok(fp != NULL, "unable to fopen test file with mode \"%s\"\n", tests[i].mode);
3019 ok(has_sequential_hint(_fileno(fp)) == tests[i].seq,
3020 "unexpected sequential hint for fopen mode \"%s\"\n", tests[i].mode);
3021 fclose(fp);
3022 }
3023 unlink(tempfile);
3024}
3025
3026static void test_open_hints(void)
3027{
3028 static const struct {
3029 int mode;
3030 BOOL seq;
3031 } tests[] = {
3032 { _O_RDONLY | _O_BINARY, FALSE },
3036 };
3037
3038 char temppath[MAX_PATH], tempfile[MAX_PATH];
3039 int fd;
3040 int i;
3041
3042 GetTempPathA(MAX_PATH, temppath);
3043 GetTempFileNameA(temppath, "", 0, tempfile);
3044
3045 for (i = 0; i < ARRAY_SIZE(tests); ++i)
3046 {
3047 fd = open(tempfile, tests[i].mode);
3048 ok(fd != -1, "unable to _open test file with flags %x\n", tests[i].mode);
3049 ok(has_sequential_hint(fd) == tests[i].seq,
3050 "unexpected sequential hint for _open flags %x\n", tests[i].mode);
3051 close(fd);
3052 }
3053 unlink(tempfile);
3054}
3055
3056static void test_ioinfo_flags(void)
3057{
3058 HANDLE handle;
3059 ioinfo *info;
3060 char *tempf;
3061 int tempfd;
3062
3063 tempf = _tempnam(".","wne");
3064
3066 ok(tempfd != -1, "_open failed with error: %d\n", errno);
3067
3068 handle = (HANDLE)_get_osfhandle(tempfd);
3070 ok(!!info, "NULL info.\n");
3071 ok(info->handle == handle, "Unexpected handle %p, expected %p.\n", info->handle, handle);
3072 skip_2k3_fail ok(info->exflag == (EF_UTF16 | EF_CRIT_INIT | EF_UNK_UNICODE), "Unexpected exflag %#x.\n", info->exflag);
3073 ok(info->wxflag == (WX_TEXT | WX_OPEN), "Unexpected wxflag %#x.\n", info->wxflag);
3074
3075 close(tempfd);
3076
3077 ok(info->handle == INVALID_HANDLE_VALUE, "Unexpected handle %p.\n", info->handle);
3078 skip_2k3_fail ok(info->exflag == (EF_UTF16 | EF_CRIT_INIT | EF_UNK_UNICODE), "Unexpected exflag %#x.\n", info->exflag);
3079 ok(!info->wxflag, "Unexpected wxflag %#x.\n", info->wxflag);
3080
3081 info = &__pioinfo[(tempfd + 4) / MSVCRT_FD_BLOCK_SIZE][(tempfd + 4) % MSVCRT_FD_BLOCK_SIZE];
3082 ok(!!info, "NULL info.\n");
3083 ok(info->handle == INVALID_HANDLE_VALUE, "Unexpected handle %p.\n", info->handle);
3084 ok(!info->exflag, "Unexpected exflag %#x.\n", info->exflag);
3085
3086 unlink(tempf);
3087 free(tempf);
3088}
3089
3091{
3092 int dup_fd, ret, pos;
3093 FILE *file;
3094 char ch;
3095
3096 dup_fd = _dup(STDOUT_FILENO);
3097 ok(dup_fd != -1, "_dup failed\n");
3098
3099 file = freopen("std_stream_test.tmp", "w", stdout);
3100 ok(file != NULL, "freopen failed\n");
3101
3102 ret = fprintf(stdout, "test");
3104
3105 fflush(stdout);
3106 _dup2(dup_fd, STDOUT_FILENO);
3107 close(dup_fd);
3108 setvbuf(stdout, NULL, _IONBF, 0);
3109
3110 ok(ret == 4, "fprintf(stdout) returned %d\n", ret);
3111 ok(!pos, "expected stdout to be buffered\n");
3112
3113 dup_fd = _dup(STDERR_FILENO);
3114 ok(dup_fd != -1, "_dup failed\n");
3115
3116 file = freopen("std_stream_test.tmp", "w", stderr);
3117 ok(file != NULL, "freopen failed\n");
3118
3119 ret = fprintf(stderr, "test");
3120 ok(ret == 4, "fprintf(stderr) returned %d\n", ret);
3122 ok(!pos, "expected stderr to be buffered\n");
3123
3124 fflush(stderr);
3125 _dup2(dup_fd, STDERR_FILENO);
3126 close(dup_fd);
3127
3128 dup_fd = _dup(STDIN_FILENO);
3129 ok(dup_fd != -1, "_dup failed\n");
3130
3131 file = freopen("std_stream_test.tmp", "r", stdin);
3132 ok(file != NULL, "freopen failed\n");
3133
3134 ch = 0;
3135 ret = fscanf(stdin, "%c", &ch);
3136 ok(ret == 1, "fscanf returned %d\n", ret);
3137 ok(ch == 't', "ch = 0x%x\n", (unsigned char)ch);
3139 ok(pos == 4, "pos = %d\n", pos);
3140
3141 fflush(stdin);
3142 _dup2(dup_fd, STDIN_FILENO);
3143 close(dup_fd);
3144
3145 ok(DeleteFileA("std_stream_test.tmp"), "DeleteFile failed\n");
3146}
3147
3148static void test_std_stream_open(void)
3149{
3150 FILE *f;
3151 int fd;
3152
3153 fd = _dup(STDIN_FILENO);
3154 ok(fd != -1, "_dup failed\n");
3155
3156 ok(!fclose(stdin), "fclose failed\n");
3157 f = fopen("nul", "r");
3158 ok(f == stdin, "f = %p, expected %p\n", f, stdin);
3159 ok(_fileno(f) == STDIN_FILENO, "_fileno(f) = %d\n", _fileno(f));
3160
3162 close(fd);
3163}
3164
3166{
3167 int arg_c;
3168 char** arg_v;
3169
3170 init();
3171
3172 arg_c = winetest_get_mainargs( &arg_v );
3173
3174 /* testing low-level I/O */
3175 if (arg_c >= 3)
3176 {
3177 if (strcmp(arg_v[2], "inherit") == 0)
3178 test_file_inherit_child(arg_v[3], arg_c > 4 ? arg_v[4] : NULL);
3179 else if (strcmp(arg_v[2], "inherit_no") == 0)
3181 else if (strcmp(arg_v[2], "pipes") == 0)
3182 test_pipes_child(arg_c, arg_v);
3183 else if (strcmp(arg_v[2], "stdin") == 0)
3185 else
3186 ok(0, "invalid argument '%s'\n", arg_v[2]);
3187 return;
3188 }
3189 test_dup2();
3190 test_file_inherit(arg_v[0]);
3191 test_invalid_stdin(arg_v[0]);
3193 test_chsize();
3194 test_stat();
3195 test_unlink();
3196
3197 /* testing stream I/O */
3198 test_filbuf();
3199 test_fdopen();
3201 test_fopen_s();
3203 test_setmode();
3204 test_fileops();
3208 test_readmode(FALSE); /* binary mode */
3209 test_readmode(TRUE); /* ascii mode */
3211 test_fgetc();
3212 test_fputc();
3213 test_flsbuf();
3214 test_fflush();
3215 test_fgetwc();
3216 /* \x83\xa9 is double byte character, \xe0\x7f is not (undefined). */
3217 test_fgetwc_locale("AB\x83\xa9\xe0\x7f", "Japanese_Japan.932", 932);
3218 /* \x83 is U+0192 */
3219 test_fgetwc_locale("AB\x83\xa9", "English", 1252);
3220 /* \x83 is U+0083 */
3221 test_fgetwc_locale("AB\x83\xa9", "C", 0);
3223 test_fputwc();
3224 test_freopen();
3225 test_ctrlz();
3227 test_tmpnam();
3230 test_pipes(arg_v[0]);
3231 test_stdin();
3232 test_mktemp();
3235 test_close();
3236 test__creat();
3237 test_lseek();
3243
3244 /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
3245 * file contains lines in the correct order
3246 */
3248}
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:1215
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
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
DWORD WINAPI DECLSPEC_HOTPATCH GetTempPathA(DWORD count, LPSTR path)
Definition: file.c:2420
BOOL WINAPI DECLSPEC_HOTPATCH WriteFile(HANDLE file, LPCVOID buffer, DWORD count, LPDWORD result, LPOVERLAPPED overlapped)
Definition: file.c:3896
BOOL WINAPI DECLSPEC_HOTPATCH SetFileAttributesA(LPCSTR name, DWORD attributes)
Definition: file.c:2906
BOOL WINAPI DECLSPEC_HOTPATCH DeleteFileA(LPCSTR path)
Definition: file.c:991
UINT WINAPI DECLSPEC_HOTPATCH GetTempFileNameA(LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
Definition: file.c:2335
DWORD WINAPI DECLSPEC_HOTPATCH GetFileAttributesA(LPCSTR name)
Definition: file.c:1659
HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstFileA(const char *filename, WIN32_FIND_DATAA *data)
Definition: file.c:1363
BOOL WINAPI DECLSPEC_HOTPATCH FindClose(HANDLE handle)
Definition: file.c:1525
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
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:1904
static void test_fputc(void)
Definition: file.c:675
static void test_tmpnam(void)
Definition: file.c:2017
static void test_fopen_hints(void)
Definition: file.c:2995
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:3056
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:2667
static const char * pipe_string
Definition: file.c:2481
static void test_get_osfhandle(void)
Definition: file.c:2345
static void test_lseek(void)
Definition: file.c:2956
#define LLEN
static void test_freopen(void)
Definition: file.c:1219
static void test_pipes_child(int argc, char **args)
Definition: file.c:2486
static BOOL has_sequential_hint(int fd)
Definition: file.c:2981
static void test_setmode(void)
Definition: file.c:2280
static void test__creat(void)
Definition: file.c:2903
static void test_chsize(void)
Definition: file.c:2036
#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:2838
static void test_std_stream_buffering(void)
Definition: file.c:3090
static void test_unlink(void)
Definition: file.c:2654
static void test_write_flush(void)
Definition: file.c:2819
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:2378
static void test_open_hints(void)
Definition: file.c:3026
#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:3148
static void test_write_flush_size(FILE *file, int bufsize)
Definition: file.c:2765
static void test_file_inherit_child_no(const char *fd_s)
Definition: file.c:1659
static void test__open_osfhandle(void)
Definition: file.c:2728
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:2484
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:2073
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:1975
#define EF_UNK_UNICODE
Definition: file.c:56
static void test_stdin(void)
Definition: file.c:2672
static void test_file_put_get(void)
Definition: file.c:1359
static void test_mktemp(void)
Definition: file.c:2708
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:2516
static void test_fopen_s(void)
Definition: file.c:2130
static void test__wfopen_s(void)
Definition: file.c:2245
static void test_fgetwc_unicode(void)
Definition: file.c:1022
static void test_setmaxstdio(void)
Definition: file.c:2368
#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 is_reactos()
Definition: test.h:1004
#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:880
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:191
#define ZeroMemory
Definition: winbase.h:1753
#define STD_OUTPUT_HANDLE
Definition: winbase.h:301
#define FILE_END
Definition: winbase.h:122
#define STD_INPUT_HANDLE
Definition: winbase.h:300
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define STD_ERROR_HANDLE
Definition: winbase.h:302
#define CREATE_DEFAULT_ERROR_MODE
Definition: winbase.h:221
#define STARTF_USESTDHANDLES
Definition: winbase.h:532
#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