bebbo / libnix

libnix (v4): a C link library for AmigaOS/m68k
15 stars 9 forks source link

Redirection of output in CLI doesn't work #18

Closed Samuel-DEVULDER closed 5 years ago

Samuel-DEVULDER commented 5 years ago

Consider that piece of code:

#include <stdio.h>

int main(int ac, char **av)
{
    printf("Hello world\n");
    return 0;
}

Compile with: m68k-amigaos-gcc tst.c -o tst -noixemul

And launch it in an amiga CLI like this:

CLI> tst >ram:output
Hello world
CLI> list ram:output
Directory "ram:" on Saturday 13-Jul-19
output                     empty ----rwed Today     22:51:35
1 file - 2 blocks used
CLI>

Obviously the redirection to ram:output didn't work. The text is printed onto the console and though the redirection file is created, it remains empty.

bebbo commented 5 years ago

for stdout Output() from the dos.library is used. No idea what should be used instead.

Samuel-DEVULDER commented 5 years ago

With the version from amiga-gcc I build ~1yr ago (possibly gcc 6.4.1b or 6.4.0b... I don't remember) redirection was working.

I wonder if startup-code does, by chance, now reopen stdio: hence close the redirection and redirect everything onto the console since gcc 6.5.0.

Samuel-DEVULDER commented 5 years ago

Browsing https://github.com/bebbo/libnix/blob/master/sources/nix20/stdio/__initstdio.c, I've spotted stomething strange in function

static StdFileDes * stdfiledes(BPTR fh) {
    StdFileDes *sfd = (StdFileDes *) malloc(sizeof(StdFileDes));
    if (sfd) {
        __stdfiledes[__stdfilesize] = sfd;
        sfd->lx_pos = __stdfilesize++;
        sfd->lx_fh = Input(); /// <=== HERE
        sfd->lx_oflags = O_WRONLY;
        _setup_file(sfd);
        sfd->lx_flags |= LX_SYS;
    }
    return sfd;
}

The passed argument is not used, and it is Input() which is used instead. Does this explain the issue ?

bebbo commented 5 years ago

Browsing https://github.com/bebbo/libnix/blob/master/sources/nix20/stdio/__initstdio.c, I've spotted stomething strange in function

static StdFileDes * stdfiledes(BPTR fh) {
  StdFileDes *sfd = (StdFileDes *) malloc(sizeof(StdFileDes));
  if (sfd) {
      __stdfiledes[__stdfilesize] = sfd;
      sfd->lx_pos = __stdfilesize++;
      sfd->lx_fh = Input(); /// <=== HERE
      sfd->lx_oflags = O_WRONLY;
      _setup_file(sfd);
      sfd->lx_flags |= LX_SYS;
  }
  return sfd;
}

The passed argument is not used, and it is Input() which is used instead. Does this explain the issue ?

good one - thanks :-)

bebbo commented 5 years ago

see fe27f83af2038c69718295d49314e657a9532d70