| 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: test3.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the fopen function. |
| 10 | ** Test to ensure that you can write to a 'w+' mode file. |
| 11 | ** And that you can read from a 'w+' mode file. |
| 12 | ** |
| 13 | ** Depends: |
| 14 | ** fprintf |
| 15 | ** fseek |
| 16 | ** fgets |
| 17 | ** |
| 18 | |
| 19 | ** |
| 20 | **===================================================================*/ |
| 21 | |
| 22 | #include <palsuite.h> |
| 23 | |
| 24 | int __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(fseek(fp, 0, SEEK_SET)) |
| 50 | { |
| 51 | Fail("ERROR: fseek failed, and this test depends on it." ); |
| 52 | } |
| 53 | |
| 54 | /* Attempt to read from the 'w+' only file, should pass */ |
| 55 | if(fgets(buffer,10,fp) == NULL) |
| 56 | { |
| 57 | Fail("ERROR: Tried to READ from a file with 'w+' mode set. " |
| 58 | "This should succeed, but fgets returned NULL. Either fgets " |
| 59 | "or fopen is broken." ); |
| 60 | } |
| 61 | |
| 62 | PAL_Terminate(); |
| 63 | return PASS; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |