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: test3.c
8**
9** Purpose: Tries to read from an empty file using fgets(), to verify
10** handling of EOF condition.
11**
12**
13**==========================================================================*/
14
15#include <palsuite.h>
16
17int __cdecl main(int argc, char **argv)
18{
19 char inBuf[10];
20 const char filename[] = "testfile.tmp";
21
22 FILE * fp;
23 if (PAL_Initialize(argc, argv))
24 {
25 return FAIL;
26 }
27
28
29 /*write the empty file that we will use to test */
30 fp = fopen(filename, "w");
31 if (fp == NULL)
32 {
33 Fail("Unable to open file for write.\n");
34 }
35
36 /*Don't write anything*/
37
38 if (fclose(fp) != 0)
39 {
40 Fail("Error closing stream opened for write.\n");
41 }
42
43
44 /*Open the file and try to read.*/
45 fp = fopen(filename, "r");
46 if (fp == NULL)
47 {
48 Fail("Unable to open file for read.\n");
49 }
50
51
52 if (fgets(inBuf, sizeof(inBuf) , fp) != NULL)
53 {
54 /*NULL could also mean an error condition, but since the PAL
55 doesn't supply feof or ferror, we can't distinguish between
56 the two.*/
57 Fail("fgets doesn't handle EOF properly. When asked to read from "
58 "an empty file, it didn't return NULL as it should have.\n");
59 }
60
61 if (fclose(fp) != 0)
62 {
63 Fail("Error closing an empty file after trying to use fgets().\n");
64 }
65 PAL_Terminate();
66 return PASS;
67
68}
69
70
71
72
73
74