summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fputs/test2/test2.cpp
blob: b8e2f410bbf8adea160dcf395a0ce3ccf983bd4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  test2.c
**
** Purpose:  Check to see that fputs fails and returns EOF when called on
**           a closed file stream and a read-only file stream.
**  

**
**===================================================================*/
                                 
#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    
    FILE* TheFile;
    char* StringOne = "FooBar";
    int ret;

    if (PAL_Initialize(argc, argv))
    {
        return FAIL;
    }

    /* Create a file with read/write access */

    TheFile = fopen("TestFile", "w+");
  
    if(TheFile == NULL)
    {
        Fail("ERROR: fopen failed to open the file 'TestFile' in read/write "
             "mode.\n");
    }

    /* Then close that file we just opened */

    if(fclose(TheFile) != 0)
    {
        Fail("ERROR: fclose failed to close the file.\n");
    }

    /* Check that calling fputs on this closed file stream fails. */

    if((ret = fputs(StringOne, TheFile)) >= 0)
    {
        Fail("ERROR: fputs should have failed to write to a closed "
             "file stream, but it didn't return a negative value.\n");
    }

    if(ret != EOF)
    {
        Fail("ERROR: fputs should have returned EOF on an error, but instead "
              "returned %d.\n",ret);
    }

    /* Open a file as Readonly */

    TheFile = fopen("TestFile", "r");
  
    if(TheFile == NULL)
    {
        Fail("ERROR: fopen failed to open the file 'TestFile' in read/write "
             "mode.\n");
    }

    /* Check that fputs fails when trying to write to a read-only stream */

    if((ret = fputs(StringOne, TheFile)) >= 0)
    {
        Fail("ERROR: fputs should have failed to write to a read-only "
             "file stream, but it didn't return a negative value.\n");
    }
    
    if(ret != EOF)
    {
        Fail("ERROR: fputs should have returned EOF when writing to a "
             "read-only filestream, but instead  "
             "returned %d.\n",ret);
    }
    
    PAL_Terminate();
    return PASS;
}