Skip to content

Strange output of CR characters with fprintf #147

@pietvo

Description

@pietvo

I found a strange problem with fprintf. If fprintf is used with arguments, then a LF (^J, 0a) in the format string will be translated to CR NL (^M ^J, 0d 0a), whether the LF is at the end or in the middle of the format string.
It doesn't matter if the arguments are actually used, even with unused arguments it works like that.
fprintf with only a format string, no arguments, only emits the NL.

This is with the downloaded library libcmini-0.54.tar.gz, which is apparently compiled without
-DSTDIO_MAP_NEWLINE.

When I recompile the library from the Github souce (Commit 970b7b7), with -DSTDIO_MAP_NEWLINE, all newlines are translated as CR NL, as it should be, and there are no spurious CR's.

Here is the test program:

#include <stdio.h>
#include <unistd.h>

int main()
{
    FILE* outfile;

    outfile = fopen("hello.txt", "w");

    fprintf(outfile, "Begin\n");
    fprintf(outfile, "WithParams %s %d\n", "string", 137);
    fprintf(outfile, "Unused Params string 137\n", "string", 137);
    fprintf(outfile, "End\n");
    fprintf(outfile, "Multiple\nLines\n", 200);
    fputs("fputs\n", outfile);

    fclose(outfile);
    return 0;
}

I compiled it with
/opt/cross-mint/bin/m68k-atari-mint-gcc -I$MINI/include -o output1.ttp -nostdlib $MINI/lib/crt0.o -s output1.c -L$MINI/lib -lcmini -lgcc -lcmini
where $MINI/ is the location of libcmini.

The output lines

WithParams string 137
Unused Params string 137
Multiple
Lines

end with CR NL. The other lines have just a NL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions