RCS_ID_C="$Id: fhopen.c,v 1.4 1994/04/04 01:29:06 jraja Exp $";
/*
 * fhopen.c --- open level 1 file from an AmigaDOS file handle
 *
 * Author: jraja <Jarno.Rajahalme@hut.fi>
 *
 * This file is part of the AmiTCP/IP Network Support Library.
 *
 * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
 *                  Helsinki University of Technology, Finland.
 *                  All rights reserved.
 *
 * Created      : Thu Mar 17 23:57:24 1994 jraja
 * Last modified: Wed Mar 30 10:35:26 1994 jraja
 *
 */

#include <ios1.h>
#include <fcntl.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <errno.h>
#include <dos/dos.h>
#include <proto/dos.h>

#include <bsdsocket.h>

extern int (*__closefunc)(int);

int
fhopen(long file, int mode)
{
  struct UFB *ufb;
  int         fd;
  int         flags;

  /*
   * Set up __closefunc (which is used at cleanup)
   */
  __closefunc = __close;
  /*
   * Check for the break signals
   */
  __chkabort();
  /*
   * find first free ufb
   */
  ufb = __allocufb(&fd);
  if (ufb == NULL)
    return -1; /* errno is set by the __allocufb() */

  /*
   * Translate mode to ufb flags
   */
  switch (mode & (O_WRONLY | O_RDWR)) {
  case O_RDONLY:
    if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
      errno = EINVAL;
      return -1;
    }
    flags = UFB_RA;
    break;
  case O_WRONLY:
    flags = UFB_WA;
    break;
  case O_RDWR:
    flags = UFB_RA | UFB_WA;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  if (mode & O_APPEND)
    flags |= UFB_APP;
  if (mode & O_XLATE)
    flags |= UFB_XLAT;
  /*
   * All done!
   */
  ufb->ufbflg = flags;
  ufb->ufbfh = (long)file;
  ufb->ufbfn = NULL;

#if 0
  if (Dup2Socket(-1, fd) < 0)	/* mark the fd as used in the AmiTCP's dTable */
    perror("Dup2Socket failed on " __FILE__);
#endif

  return fd;
}
