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: test4.c
8**
9** Purpose: Tests the PAL implementation of the fopen function.
10** Test to ensure that you can't write to a 'r' mode file.
11** And that you can read from a 'r' mode file.
12**
13** Depends:
14** fprintf
15** fclose
16** fgets
17**
18
19**
20**===================================================================*/
21
22#include <palsuite.h>
23
24int __cdecl main(int argc, char **argv)
25{
26
27 FILE *fp;
28 char buffer[128];
29
30 if (PAL_Initialize(argc, argv))
31 {
32 return FAIL;
33 }
34
35
36 /* Open a file with 'w' mode */
37 if( (fp = fopen( "testfile", "w" )) == NULL )
38 {
39 Fail( "ERROR: The file failed to open with 'w' mode.\n" );
40 }
41
42 /* Write some text to the file */
43 if(fprintf(fp,"%s","some text") <= 0)
44 {
45 Fail("ERROR: Attempted to WRITE to a file opened with 'w' mode "
46 "but fprintf failed. Either fopen or fprintf have problems.");
47 }
48
49 if(fclose(fp))
50 {
51 Fail("ERROR: Attempted to close a file, but fclose failed. "
52 "This test depends upon it.");
53 }
54
55 /* Open a file with 'r' mode */
56 if( (fp = fopen( "testfile", "r" )) == NULL )
57 {
58 Fail( "ERROR: The file failed to open with 'r' mode.\n" );
59 }
60
61 /* Attempt to read from the 'r' only file, should pass */
62 if(fgets(buffer,10,fp) == NULL)
63 {
64 Fail("ERROR: Tried to READ from a file with 'r' mode set. "
65 "This should succeed, but fgets returned NULL. Either fgets "
66 "or fopen is broken.");
67 }
68
69 /* Write some text to the file */
70 if(fprintf(fp,"%s","some text") > 0)
71 {
72 Fail("ERROR: Attempted to WRITE to a file opened with 'r' mode "
73 "but fprintf succeeded It should have failed. "
74 "Either fopen or fprintf have problems.");
75 }
76
77
78 PAL_Terminate();
79 return PASS;
80}
81
82
83