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