| 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: test7.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 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 | |
| 26 | int __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.\n" ); |
| 49 | } |
| 50 | |
| 51 | if(fseek(fp, 0, SEEK_SET)) |
| 52 | { |
| 53 | Fail("ERROR: fseek failed, and this test depends on it.\n" ); |
| 54 | } |
| 55 | |
| 56 | /* Attempt to read from the 'a+' only file, should succeed */ |
| 57 | if(fgets(buffer,10,fp) == NULL) |
| 58 | { |
| 59 | Fail("ERROR: Tried to READ from a file with 'a+' mode set. " |
| 60 | "This should pass, but fgets returned failure. Either fgets " |
| 61 | "or fopen is broken.\n" ); |
| 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 | /* Attempt to read from the 'a+' only file, should succeed */ |
| 99 | if(fgets(buffer,10,fp) == NULL) |
| 100 | { |
| 101 | Fail("ERROR: Tried to READ from a file with 'a+' mode set. " |
| 102 | "This should pass, but fgets returned failure. Either fgets " |
| 103 | "or fopen is broken.\n" ); |
| 104 | } |
| 105 | |
| 106 | /* Compare what was read and what should have been in the file */ |
| 107 | if(memcmp(buffer,"abcdefgh" ,8)) |
| 108 | { |
| 109 | Fail("ERROR: The string read should have equaled 'abcdefgh' " |
| 110 | "but instead it is %s\n" , buffer); |
| 111 | } |
| 112 | |
| 113 | PAL_Terminate(); |
| 114 | return PASS; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |