/* $Id: c,v.RO-fopen 7.2 1998/09/01 19:52:11 stoklund Exp $ */
/* RO-fopen.c: Opening files after name translation

   Copyright (C) 1998 Jakob Stoklund Olesen

   This library is free software; you can redistribute it and / or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not ,write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 -
   1307, USA. */

#include "config.h"
#include "riscos.h"
#include "OS:osfile.h"

FILE *
riscos_fopen_input (const_string name)
{
  string tname;
  FILE *ret;

  tname = riscos_translate_input (name);
  if (tname == NULL)
    {
      if (errno)
	FATAL_PERROR (name);
      else
	FATAL1 ("%s: no readable file", name);
    }

  ret = fopen (tname, "r");
  if (ret == NULL)
    FATAL1 ("%s: cannot open for input\n", tname);

  free (tname);
  return ret;
}

FILE *
riscos_fopen_output (const_string name, int type)
{
  string tname;
  FILE *ret;
  os_error *erp;

  tname = riscos_translate_output (name);
  /* translate_output always sets errno when NULL is returned */
  if (tname == NULL)
    FATAL_PERROR (name);

  /* Since we're going to do fopen("w") we might as well create the file */
  erp = xosfile_create_stamped (tname, type, 0);
  if (erp)
    {
      errno_swi (erp);
      FATAL_PERROR (tname);
    }

  ret = fopen (tname, "w");
  if (ret == NULL)
    FATAL1 ("%s: cannot open for output\n", tname);

  free (tname);
  return ret;
}
