
/* stdio.c */

#include <exec/types.h>
#include <exec/alerts.h>
#include <libraries/dos.h>
#include <functions.h>

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

#define CONSOLE_NAME "*"

short _numdev;
extern struct _device *_devtab;

FILE *stdin, *stdout, *stderr;

void
__open_stdio (void)
{
  /* Size and allocate the device table. */
  _numdev = OPEN_MAX;
  _devtab = (struct _device *) calloc (OPEN_MAX, sizeof (struct _device));
  if (!_devtab)
    {
      Alert (AG_NoMemory, NULL);
      exit (100);
    }

  /* Open the standard input stream, file descriptor 0.  O_STDIO inhibits
     close() from Close()'ing the DOS file handles we inherited. */
  _devtab[0]._mode = O_RDONLY | O_STDIO;
  _devtab[0]._fileHandle = Input ();
  stdin = fdopen (0, "r");

  /* Open standard output stream, file descriptor 1. */
  _devtab[1]._mode = O_WRONLY | O_STDIO;
  _devtab[1]._fileHandle = Output ();
  stdout = fdopen (1, "w");

  /* Open standard error stream, file descriptor 2. */
  if (_devtab[1]._fileHandle)
    {
      _devtab[2]._fileHandle = Open (CONSOLE_NAME, MODE_OLDFILE);
      _devtab[2]._mode |= O_WRONLY;
      stderr = fdopen (2, "w");
    }
}
