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 |
8 | ** |
9 | ** Purpose: Check to see that fputs fails and returns EOF when called on |
10 | ** a closed file stream and a read-only file stream. |
11 | ** |
12 | |
13 | ** |
14 | **===================================================================*/ |
15 | |
16 | #include <palsuite.h> |
17 | |
18 | int __cdecl main(int argc, char **argv) |
19 | { |
20 | |
21 | FILE* TheFile; |
22 | char* StringOne = "FooBar" ; |
23 | int ret; |
24 | |
25 | if (PAL_Initialize(argc, argv)) |
26 | { |
27 | return FAIL; |
28 | } |
29 | |
30 | /* Create a file with read/write access */ |
31 | |
32 | TheFile = fopen("TestFile" , "w+" ); |
33 | |
34 | if(TheFile == NULL) |
35 | { |
36 | Fail("ERROR: fopen failed to open the file 'TestFile' in read/write " |
37 | "mode.\n" ); |
38 | } |
39 | |
40 | /* Then close that file we just opened */ |
41 | |
42 | if(fclose(TheFile) != 0) |
43 | { |
44 | Fail("ERROR: fclose failed to close the file.\n" ); |
45 | } |
46 | |
47 | /* Check that calling fputs on this closed file stream fails. */ |
48 | |
49 | if((ret = fputs(StringOne, TheFile)) >= 0) |
50 | { |
51 | Fail("ERROR: fputs should have failed to write to a closed " |
52 | "file stream, but it didn't return a negative value.\n" ); |
53 | } |
54 | |
55 | if(ret != EOF) |
56 | { |
57 | Fail("ERROR: fputs should have returned EOF on an error, but instead " |
58 | "returned %d.\n" ,ret); |
59 | } |
60 | |
61 | /* Open a file as Readonly */ |
62 | |
63 | TheFile = fopen("TestFile" , "r" ); |
64 | |
65 | if(TheFile == NULL) |
66 | { |
67 | Fail("ERROR: fopen failed to open the file 'TestFile' in read/write " |
68 | "mode.\n" ); |
69 | } |
70 | |
71 | /* Check that fputs fails when trying to write to a read-only stream */ |
72 | |
73 | if((ret = fputs(StringOne, TheFile)) >= 0) |
74 | { |
75 | Fail("ERROR: fputs should have failed to write to a read-only " |
76 | "file stream, but it didn't return a negative value.\n" ); |
77 | } |
78 | |
79 | if(ret != EOF) |
80 | { |
81 | Fail("ERROR: fputs should have returned EOF when writing to a " |
82 | "read-only filestream, but instead " |
83 | "returned %d.\n" ,ret); |
84 | } |
85 | |
86 | PAL_Terminate(); |
87 | return PASS; |
88 | } |
89 | |