1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test1.c (fclose)
8**
9** Purpose: Tests the PAL implementation of the fclose function.
10** This test will use fdopen to create a file stream,
11** that will be used to test fclose. fclose will also
12** be passed a closed file handle to make sure it handle
13** it accordingly.
14**
15**
16**===================================================================*/
17
18#include <palsuite.h>
19
20int __cdecl main(int argc, char **argv)
21{
22 HANDLE hReadPipe = NULL;
23 HANDLE hWritePipe = NULL;
24 BOOL bRetVal = FALSE;
25 int iFiledes = 0;
26 FILE *fp;
27
28 SECURITY_ATTRIBUTES lpPipeAttributes;
29
30 /*Initialize the PAL*/
31 if ((PAL_Initialize(argc, argv)) != 0)
32 {
33 return (FAIL);
34 }
35
36 /*Setup SECURITY_ATTRIBUTES structure for CreatePipe*/
37 lpPipeAttributes.nLength = sizeof(lpPipeAttributes);
38 lpPipeAttributes.lpSecurityDescriptor = NULL;
39 lpPipeAttributes.bInheritHandle = TRUE;
40
41 /*Create a Pipe*/
42 bRetVal = CreatePipe(&hReadPipe, // read handle
43 &hWritePipe, // write handle
44 &lpPipeAttributes,// security attributes
45 0); // pipe size
46
47 if (bRetVal == FALSE)
48 {
49 Fail("ERROR: Unable to create pipe; returned error code %ld"
50 , GetLastError());
51 }
52
53 /*Get a file descriptor for the read pipe handle*/
54 iFiledes = _open_osfhandle((long)hReadPipe,_O_RDONLY);
55
56 if (iFiledes == -1)
57 {
58 Fail("ERROR: _open_osfhandle failed to open "
59 " hReadPipe=0x%lx", hReadPipe);
60 }
61
62 /*Open read pipe handle in read mode*/
63 fp = _fdopen(iFiledes, "r");
64
65 if (fp == NULL)
66 {
67 Fail("ERROR: unable to fdopen file descriptor"
68 " iFiledes=%d", iFiledes);
69 }
70
71 /*Attempt to close the file stream*/
72 if (fclose(fp) != 0)
73 {
74 Fail("ERROR: Unable to fclose file stream fp=0x%lx\n",fp);
75 }
76
77 PAL_Terminate();
78 return (PASS);
79}
80