// gnoStdin.c — stdin verification, now with a printf BEFORE the read
// (mirroring gnoCat) to test whether prior stdout output corrupts fgets.
//   0xC0DE = exact "FILE-STDIN-OK"; 0xBAD1 = other; 0xBAD0 = EOF.
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
    char line[80];
    line[0] = 0;
    printf("Type a line:\n");                 // <-- prior stdout write, like gnoCat
    char *r = fgets(line, sizeof(line), stdin);
    size_t n = strlen(line);
    while (n && (line[n-1] == '\n' || line[n-1] == '\r')) line[--n] = 0;
    uint16_t mark;
    if (r && strcmp(line, "FILE-STDIN-OK") == 0) mark = 0xC0DE;
    else if (r && n > 0)                          mark = 0xBAD1;
    else                                          mark = 0xBAD0;
    *(volatile uint16_t *)0x025000UL = mark;
    *(volatile uint16_t *)0x025002UL = (uint16_t)n;
    *(volatile uint16_t *)0x025004UL = (uint16_t)(uint8_t)line[0];
    for (volatile unsigned long i = 0; i < 300000UL; i++) {}
    return 0;
}
