/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *  Portions Copyright (C) 1994 Rafael W. Luebbert
 *  Portions Copyright (C) 1996 Jeff Shepherd
 *
 *  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.
 */

/* Miscellaneous functions */

#define _KERNEL
#include "ixemul.h"
#include "kprintf.h"
#include <stdlib.h>

int
getpid(void)
{
  return (int)FindTask (0);
}

int
getppid(void)
{
  /* all processes have been started by init :-)) */
  return 1;
}

int
setpgid(int pid, int pgrp)
{
  return pid==pgrp ? 0 : -1;
}

int
setpgrp(int pid, int pgrp)
{
  return setpgid(pid, pgrp);
}

pid_t
getpgrp(void)
{
  if (u.u_ixnetbase)
    return netcall(NET_getpgrp);
  return (int) FindTask(0);
}

int
getrusage(int who, struct rusage *rusage)
{
  if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
    {
      errno = EINVAL;
      KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
      return -1;
    }

  *rusage = (who == RUSAGE_SELF) ? u.u_ru : u.u_cru;
  return 0;
}

char hostname[64] = "localhost";

int
gethostname(char *name, int namelen)
{
  if (u.u_ixnetbase)
    return netcall(NET_gethostname, name, namelen);
  strncpy (name, hostname, namelen);
  return 0;
}

int
sethostname(char *name, int namelen)
{
  int len;

  if (u.u_ixnetbase)
    return netcall(NET_sethostname, name, namelen);

  len = namelen < sizeof (hostname) - 1 ? namelen : sizeof (hostname) - 1;
  strncpy (hostname, name, len);
  hostname[len] = 0;
  return 0;
}

int
__chown_func (struct StandardPacket *sp, struct MsgPort *handler,
	      BPTR parent_lock,
	      BSTR name,
	      int ugid, int *no_error)
{
  sp->sp_Pkt.dp_Type = ACTION_SET_OWNER;
  sp->sp_Pkt.dp_Arg1 = 0;
  sp->sp_Pkt.dp_Arg2 = parent_lock;
  sp->sp_Pkt.dp_Arg3 = name;
  sp->sp_Pkt.dp_Arg4 = ugid;

  PutPacket (handler, sp);
  __wait_sync_packet (sp);

  *no_error = sp->sp_Pkt.dp_Res1 == -1;
  return 1;
}

int

chown(const char *name, uid_t uid, gid_t gid)
{
  if (muBase)
    {
      int user = (uid << 16) | gid;
      int result;
      result = __plock(name,__chown_func, (void *)user);
      if (result == 0)
	{
	  errno = __ioerr_to_errno (IoErr ());
	  KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
	}

      return result == -1 ? 0 : -1;
   }
  return 0;
}

int
fchown(int fd, int uid, int mode)
{
  return 0;
}

/* not really useful.. but it's there ;-)) */
int
getpagesize(void)
{
  return 2048;
}

extern char _ctype_[];
extern int sys_nerr;

void
ix_get_vars(char **ctype, int *_sys_nerr)
{
  if (ctype)
    *ctype = _ctype_;
  if (_sys_nerr)
    *_sys_nerr = sys_nerr;
}

void sync (void)
{
  /* could probably walk the entire file table and fsync each, but
     this is too much of a nuisance ;-)) */
  errno = ENOSYS;
  KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
}

int
fork (void)
{
  errno = ENOSYS;
  KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  return -1;
}

int
mkfifo (const char *path, mode_t mode)
{
  errno = ENOSYS;
  KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  return -1;
}

int
mknod (const char *path, mode_t mode, dev_t dev)
{
  errno = ENOSYS;
  KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  return -1;
}

