RCS_ID_C="$Id: _fstat.c,v 3.2 1994/04/04 01:36:59 jraja Exp $";
/*
 * _fstat.c --- fstat() for Network Support Library
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * This file is part of the AmiTCP/IP Network Support Library.
 *
 * Copyright © 1993,1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
 *             Helsinki University of Technology, Finland.
 *             All rights reserved.
 *
 * Created      : Sun Mar 27 19:58:21 1994 ppessi
 * Last modified: Sun Mar 27 20:34:49 1994 ppessi
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>

#include <string.h>
#include <stdlib.h>

/* DOS 3.0 and MuFS extensions to file info block */
#include "fibex.h"
#include <proto/dos.h>
#include <proto/utility.h>

#include <ios1.h>

int fstat(int fd, struct stat *st)
{
  struct UFB *ufb = chkufb(fd);

  if (st == NULL || ((1 & (long)st) == 1)) {
    errno = EFAULT;
    return -1;
  }

  if (ufb == NULL || ufb->ufbflg == 0) {
    errno = EBADF;
    return -1;
  }

  if (ufb->ufbflg & UFB_SOCK) { /* a socket */
    long value;
    long size = sizeof(value);
    bzero(st, sizeof(*st));

    /* st->st_dev = ??? */
    st->st_mode = S_IFSOCK | S_IRUSR | S_IWUSR;
    st->st_uid = geteuid();
    st->st_gid = getegid();

    if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &size) == 0)
      st->st_blksize = value;

    return 0;
  } else { /* ordinal file */
    if (ExamineFH(ufb->ufbfh, __dostat_fib)) {
      __dostat(__dostat_fib, st);
      st->st_dev = (dev_t)((struct FileHandle *)BADDR(ufb->ufbfh))->fh_Type;
      return 0;
    } else {
      errno = EIO;
      return -1;
    }
  }
}
