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: test6.c
8**
9** Purpose: Tests the PAL implementation of the fopen function.
10** Test to ensure that you can write to an 'a' mode file.
11** And that you can't read from a 'a' mode file. Also ensure
12** that you can use fseek and still write to the end of a file.
13**
14** Depends:
15** fprintf
16** fgets
17** fseek
18** fclose
19**
20
21**
22**===================================================================*/
23
24#include <palsuite.h>
25
26int __cdecl main(int argc, char **argv)
27{
28
29 FILE *fp;
30 char buffer[128];
31
32 if (PAL_Initialize(argc, argv))
33 {
34 return FAIL;
35 }
36
37
38 /* Open a file with 'a+' mode */
39 if( (fp = fopen( "testfile", "a" )) == NULL )
40 {
41 Fail( "ERROR: The file failed to open with 'a' mode.\n" );
42 }
43
44 /* Write some text to the file */
45 if(fprintf(fp,"%s","some text") <= 0)
46 {
47 Fail("ERROR: Attempted to WRITE to a file opened with 'a' mode "
48 "but fprintf failed. Either fopen or fprintf have problems.");
49 }
50
51 if(fseek(fp, 0, SEEK_SET))
52 {
53 Fail("ERROR: fseek failed, and this test depends on it.");
54 }
55
56 /* Attempt to read from the 'a' only file, should fail */
57 if(fgets(buffer,10,fp) != NULL)
58 {
59 Fail("ERROR: Tried to READ from a file with 'a' mode set. "
60 "This should fail, but fgets returned success. Either fgets "
61 "or fopen is broken.");
62 }
63
64
65 /* Attempt to write to a file after using 'a' and fseek */
66 fp = fopen("testfile2", "a");
67 if(fp == NULL)
68 {
69 Fail("ERROR: The file failed to be created with 'a' mode.\n");
70 }
71
72 /* write text to the file initially */
73 if(fprintf(fp,"%s","abcd") <= 0)
74 {
75 Fail("ERROR: Attempted to WRITE to a file opened with 'a' mode "
76 "but fprintf failed. Either fopen or fprintf have problems.\n");
77 }
78
79 /* set the pointer to the front of the file */
80 if(fseek(fp, 0, SEEK_SET))
81 {
82 Fail("ERROR: fseek failed, and this test depends on it.\n");
83 }
84
85 /* using 'a' should still write to the end of the file, not the front */
86 if(fputs("efgh",fp) < 0)
87 {
88 Fail("ERROR: Attempted to WRITE to a file opened with 'a' mode "
89 "but fputs failed.\n");
90 }
91
92 /* set the pointer to the front of the file */
93 if(fseek(fp, 0, SEEK_SET))
94 {
95 Fail("ERROR: fseek failed, and this test depends on it.\n");
96 }
97
98 /* a file with 'a' mode can only write, so close the file before reading */
99 if(fclose(fp))
100 {
101 Fail("ERROR: fclose failed when it should have succeeded.\n");
102 }
103
104 /* open the file again to read */
105 fp = fopen("testfile2","r");
106 if(fp == NULL)
107 {
108 Fail("ERROR: fopen failed to open the file using 'r' mode");
109 }
110
111 /* Attempt to read from the 'a' only file, should succeed */
112 if(fgets(buffer,10,fp) == NULL)
113 {
114 Fail("ERROR: Tried to READ from a file with 'a' mode set. "
115 "This should pass, but fgets returned failure. Either fgets "
116 "or fopen is broken.\n");
117 }
118
119 /* Compare what was read and what should have been in the file */
120 if(memcmp(buffer,"abcdefgh",8))
121 {
122 Fail("ERROR: The string read should have equaled 'abcdefgh' "
123 "but instead it is %s\n", buffer);
124 }
125
126
127 PAL_Terminate();
128 return PASS;
129}
130
131
132