phoenix-rtos / phoenix-rtos-project

Sample project using Phoenix-RTOS
https://phoenix-rtos.com
BSD 3-Clause "New" or "Revised" License
45 stars 32 forks source link

Ftell() shows wrong position on files opened in "a+" and "ab+" mode #924

Closed KArkadiusz closed 11 months ago

KArkadiusz commented 11 months ago

Checked on Phoenix-rtos-project commit 4e2ac6c.

Upon opening not empty file in "a+" and "ab+" modes, ftell() function shows that the pointer in the file lies at its end, like in "a" mode. Originally, the file pointer should be positioned at the beginning of the file.

ia32 result: image

Host result: image

Code used for checking:

#include <stdio.h>

int main()
{
    FILE *f = fopen("test", "w+");
    fputs("1234wertgedr", f);
    fclose(f);

    f = fopen("test", "a+");
    printf("%ld\n", ftell(f));
    fclose(f);

    f = fopen("test", "a");
    printf("%ld\n", ftell(f));
    fclose(f);

    f = fopen("test", "ab+");
    printf("%ld\n", ftell(f));
    fclose(f);

    f = fopen("test", "ab");
    printf("%ld\n", ftell(f));
    fclose(f);

    remove("test");
    return 0;
}

I suspect it might be the fopen() problem, in test cases ftell() returned expected values in all modes except "a+" and "ab+" so it probably works fine.

adamdebek commented 11 months ago

Open Group does not directly specify position of file when opened in append mode, documentation only require subsequent write() calls to that file to happen at the end-of-file. See: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html