/*
   Jrd's revamped startup fun.  Init stdio stuff, and call main.
   Supply a hook function to be called by crt0, so as to get around
   broken assumptions of C++.
*/

#include "stdio.h"
#include <osbind.h>

FILE * stdin;
FILE * stdout;
FILE * stderr;

static int stdio_setup = 0;

static void setup_stdio()
{
  if (!stdio_setup)		/* only do this once */
	{
	stdin = fdopen(0, "r");
	stdout = fdopen(1, "a");
	stderr = fdopen(-1, "w");
	stdio_setup = 1;
	}
}

typedef void (* hook)();

hook __setup_stdio_hook = setup_stdio;

_main(argc, argv, env)
int argc;
char ** argv;
char ** env;
{
  int result;

  setup_stdio();
  main(argc, argv, env);
  exit(0);
}

exit(i)
int i;
{
  fflush(stdout);
  __close_all_streamoids();
  Pterm(i);
}

