/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *  Portions Copyright (C) 1994 Rafael W. Luebbert
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id: kern_descrip.c,v 1.3 1994/06/19 15:13:31 rluebbert Exp $
 *
 *  $Log: kern_descrip.c,v $
 *  Revision 1.3  1994/06/19  15:13:31	rluebbert
 *  *** empty log message ***
 *
 * Revision 1.1  1992/05/14  19:55:40  mwild
 * Initial revision
 *
 *
 *  Since the code originated from Berkeley, the following copyright
 *  header applies as well. The code has been changed, it's not the
 *  original Berkeley code!
 */

/*
 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution is only permitted until one year after the first shipment
 * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
 * binary forms are permitted provided that: (1) source distributions retain
 * this entire copyright notice and comment, and (2) distributions including
 * binaries display the following acknowledgement:  This product includes
 * software developed by the University of California, Berkeley and its
 * contributors'' in the documentation or other materials provided with the
 * distribution and in all advertising materials mentioning features or use
 * of this software.  Neither the name of the University nor the names of
 * its contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *	@(#)kern_descrip.c      7.16 (Berkeley) 6/28/90
 */

/* modified by Jeff Shepherd to allow use of sockets
 * replaces functions normally found in ixemul.library
 * makes it easier than making a new library
 */
#define KERNEL
#define _INTERNAL_FILE
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <user.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syslog.h>
#include <proto/socket.h>
#include <proto/exec.h>
#include <errno.h>

extern int errno;

#include <unistd.h>

#define u (*((struct user *)((struct Task *)(FindTask(0))->tc_TrapData)))
extern int gcc_fstat (unsigned fdes, struct stat *sb);

/*
 * Descriptor management.
 */

/*
 * Return status information about a file descriptor.
 */
/* ARGSUSED */
int
fstat (unsigned fdes, struct stat *sb)
{
  struct file *f = u.u_ofile[fdes];

  if (fdes >= NOFILE || f == NULL)
    {
      errno = EBADF;
      return -1;
    }

    if (f->f_type == DTYPE_SOCKET) {
       long value;
       int size = sizeof(value);

       bzero(sb, sizeof(*sb));

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

       if (getsockopt(fdes, SOL_SOCKET, SO_SNDBUF, (void *)&value, &size) == 0)
	 sb->st_blksize = value;
       return 0;
    }/* if */

    return gcc_fstat(fdes,sb);
}
