| 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: test2.c |
| 8 | ** |
| 9 | ** Purpose: Calls fgets to read a full line from a file. A maximum length |
| 10 | ** parameter greater than the length of the line is passed. |
| 11 | ** |
| 12 | ** |
| 13 | **==========================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | int __cdecl main(int argc, char **argv) |
| 18 | { |
| 19 | const char outBuf1[] = "This is a test.\n" ; |
| 20 | const char outBuf2[] = "This is too." ; |
| 21 | |
| 22 | char inBuf[sizeof(outBuf1) + sizeof(outBuf2)]; |
| 23 | const char filename[] = "testfile.tmp" ; |
| 24 | const int offset = 5; /*value chosen arbitrarily*/ |
| 25 | int expectedLen; |
| 26 | int actualLen; |
| 27 | |
| 28 | FILE * fp; |
| 29 | |
| 30 | if (PAL_Initialize(argc, argv)) |
| 31 | { |
| 32 | return FAIL; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /*write the file that we will use to test */ |
| 37 | fp = fopen(filename, "w" ); |
| 38 | if (fp == NULL) |
| 39 | { |
| 40 | Fail("Unable to open file for write.\n" ); |
| 41 | } |
| 42 | |
| 43 | fwrite(outBuf1, sizeof(outBuf1[0]), sizeof(outBuf1), fp); |
| 44 | fwrite(outBuf2, sizeof(outBuf2[0]), sizeof(outBuf2), fp); |
| 45 | |
| 46 | if (fclose(fp) != 0) |
| 47 | { |
| 48 | Fail("error closing stream opened for write.\n" ); |
| 49 | } |
| 50 | |
| 51 | /*Read until the first linebreak*/ |
| 52 | fp = fopen(filename, "r" ); |
| 53 | if (fp == NULL) |
| 54 | { |
| 55 | Fail("Unable to open file for read.\n" ); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | if (fgets(inBuf, sizeof(outBuf1) + offset , fp) != inBuf) |
| 60 | { |
| 61 | Fail("Error reading from file using fgets.\n" ); |
| 62 | } |
| 63 | |
| 64 | /*note: -1 because strlen returns the length of a string _not_ |
| 65 | including the NULL, while fgets returns a string of specified |
| 66 | maximum length _including_ the NULL.*/ |
| 67 | expectedLen = strlen(outBuf1); |
| 68 | actualLen = strlen(inBuf); |
| 69 | if (actualLen > expectedLen) |
| 70 | { |
| 71 | Fail("fgets() was asked to read the first line of a file, but did " |
| 72 | "not stop at the end of the line.\n" ); |
| 73 | } |
| 74 | else if (actualLen < expectedLen) |
| 75 | { |
| 76 | Fail("fgets() was asked to read the first line of a file, but did " |
| 77 | "not read the entire line.\n" ); |
| 78 | } |
| 79 | else if (memcmp(inBuf, outBuf1, actualLen) != 0) |
| 80 | { |
| 81 | /*We didn't read back exactly outBuf1*/ |
| 82 | Fail("fgets() was asked to read the first line of a file. It " |
| 83 | "has read back a string of the correct length, but the" |
| 84 | " contents are not correct.\n" ); |
| 85 | } |
| 86 | |
| 87 | if (fclose(fp) != 0) |
| 88 | { |
| 89 | Fail("Error closing file after using fgets().\n" ); |
| 90 | } |
| 91 | |
| 92 | PAL_Terminate(); |
| 93 | return PASS; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |