/* 
 * file writen for bsp by 
 * Michal Moskal <malekith@pld.org.pl>
 * $Id: syscall.c,v 1.10 2002/12/08 15:53:18 malekith Exp $
 */

#include <sys/types.h>
#include <linux/types.h>
#include <syscall.h>
#include <signal.h>

int errno;

struct rusage;
struct utsname;
struct stat;

_syscall4(pid_t,wait4,pid_t,pid,int *,wait_stat,int,options,struct rusage *,rusage)
_syscall3(int,execve,const char *,file,char **,argv,char **,envp)
_syscall0(pid_t,fork)
#ifdef __sparc__
_syscall0(pid_t,getppid)
#endif
_syscall3(int,write,int,fd,const void *,buf,int,count)
_syscall3(int,read,int,fd,void *,buf,int,count)
_syscall1(void*,brk,void*,end)
_syscall2(int,kill,pid_t,pid,int,sig)
_syscall1(int,chdir,char *,dir)
_syscall1(int,close,int,fd)
_syscall3(int,open,const char *,name,int,xx,int,mode)
_syscall3(off_t,lseek,int, fildes, off_t, offset, int, whence)
_syscall2(caddr_t,create_module,const char *,name, size_t, size)		     
_syscall1(int, delete_module,const char *,name)
_syscall5(int,query_module,const char *,name, int, which, void *,buf, size_t, bufsize, size_t *,ret)
_syscall1(int, uname,struct utsname *,buf)
_syscall2(int,stat,const char *,file_name, struct stat *,buf)
_syscall2(int,nanosleep,struct timespec *,a, struct timespec *,b)
_syscall3(int,ioctl,int,d,int,r,char *,p)
_syscall2(int,fstat,int,d,struct stat *,buf)
_syscall1(int,umount,char *,p)
_syscall5(int,mount,char *,dev,char *,dir,char *,fs,int,flag,void*,opt)
#ifndef __NR_pivot_root
# warning "Guessing pivot_root() syscall number"
# ifdef __i386__
#  define __NR_pivot_root 217
# elif defined(__alpha__)
#  define __NR_pivot_root 374
# elif defined(__sparc__) || defined(__sparc64__)
#  define __NR_pivot_root 146
# elif defined(__powerpc__)
#  define __NR_pivot_root 203
# else
#  error "Get kernel-2.4 headers!"
# endif
#endif
_syscall2(int,pivot_root,char *,new_root,char *,old_root)

_syscall3(int,mknod,char*,patchname,int,mode,int,dev)
_syscall2(int,mkdir,char*,patchname,int,mode)
_syscall1(int,unlink,char*,patchname)
_syscall1(int,rmdir,char*,patchname)

_syscall3(int,init_module,void*,mod,size_t,sz,char*,opt);

void usleep(int a)
{
	struct timespec t;
	t.tv_sec = a / 1000000;
	t.tv_nsec = a%1000000 * 1000;
	nanosleep(&t,NULL);
}

/* 
 * avoid braindemaged sunOS fork() return convetion of fork
 * don't-know-why followed by Linux ... w/o using any asm
 */
pid_t xfork()
{
	pid_t p;
	
	p = fork();

#ifdef __sparc__
	if (p == getppid())
		p = 0;
#endif

	return p;
}

int norm_stat(const char *file_name, struct stat *buf)
{
	return stat(file_name, buf);
}

/* avoid stupid warning about noreturn func, that returns */
#define __NR_trabka 1
static _syscall1(int,trabka,int,s)

void exit(int x)
{
	trabka(x);
	for(;;){}
}

void abort()
{
	kill(0,SIGABRT);
	for(;;){}
}

int *__errno_location(void)
{
	return &errno;
}
