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: ungetc.c (test 1)
8**
9** Purpose: Tests the PAL implementation of the ungetc function by calling
10** the function on a write-only file.
11**
12** Dependencies:
13** fopen
14** fclose
15** fseek
16**
17**
18**===================================================================*/
19
20#include <palsuite.h>
21
22
23int __cdecl main(int argc, char *argv[])
24{
25 char szFileName[] = {"test1.tmp"};
26 const char text[] =
27 {"The quick brown fox jumped over the lazy dog's back."};
28 FILE* pFile = NULL;
29 int nChar = 65; /* 'A' */
30 int nRc = 0;
31 int itemsExpected;
32 int itemsWritten;
33
34 if (0 != PAL_Initialize(argc,argv))
35 {
36 return FAIL;
37 }
38
39 /* create the file */
40 pFile = fopen(szFileName, "w");
41 if (pFile == NULL)
42 {
43 Fail("ungetc: ERROR -> fopen failed to create the file \"%s\""
44 " as write-only.\n",
45 szFileName);
46 }
47
48 /* write to the file */
49 itemsExpected = sizeof(text);
50 itemsWritten = fwrite(text, sizeof(text[0]), sizeof(text), pFile);
51 if (itemsWritten == 0)
52 {
53 Trace("ungetc: ERROR -> fwrite failed to write to the file \"%s\"\n",
54 szFileName);
55
56 if (fclose(pFile) != 0)
57 {
58 Fail("ungetc: ERROR -> fclose failed to close the file.\n");
59 }
60 Fail("");
61
62 }
63 else if (itemsWritten != itemsExpected)
64 {
65 Trace("ungetc: ERROR -> fwrite failed to write the correct number "
66 "of characters to the file \"%s\"\n",
67 szFileName);
68
69 if (fclose(pFile) != 0)
70 {
71 Fail("ungetc: ERROR -> fclose failed to close the file.\n");
72 }
73 Fail("");
74 }
75
76 /* Close the file */
77 if (fclose(pFile) != 0)
78 {
79 Fail("ungetc: ERROR -> fclose failed to close the file.\n");
80 }
81
82 /*
83 ** open the file in write only mode and
84 ** attempt to push an unread character back on the stream
85 */
86
87
88 /* open the file write-only */
89 pFile = fopen(szFileName, "a");
90 if (pFile == NULL)
91 {
92 Fail("ungetc: ERROR -> fopen failed to open the file \"%s\""
93 " as write-only.\n",
94 szFileName);
95 }
96
97 /* move the file pointer back to the beginning of the file */
98 if (fseek(pFile, 1, SEEK_SET) != 0)
99 {
100
101 Trace("ungetc: ERROR -> fseek failed to move the file pointer to the "
102 "beginning of the file.\n");
103 if (fclose(pFile) != 0)
104 {
105 Trace("ungetc: ERROR -> fclose failed to close the file.\n");
106 }
107 Fail("");
108 }
109
110 /* call ungetc on a write-only file which should fail */
111 if ((nRc = ungetc(nChar, pFile)) != EOF)
112 {
113 Trace("ungetc: ERROR -> ungetc returned \"%c\" when run on "
114 "an write-only file.\n", nChar);
115 if (fclose(pFile) != 0)
116 {
117 Trace("ungetc: ERROR -> fclose failed to close the file.\n");
118 }
119 Fail("");
120 }
121
122 if (fclose(pFile) != 0)
123 {
124 Fail("ungetc: ERROR -> fclose failed to close the file.\n");
125 }
126
127
128 PAL_Terminate();
129 return PASS;
130}
131