/*
 * stdio.c - User side system calls implementation.
 * 
 * Author     Alain Greiner (2016,2017)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <shared_syscalls.h>
#include <hal_user.h>
#include <stdio.h>
#include <stdlib.h>

#define  reg_t     int

///////////////////////
void exit( int status )
{
    hal_user_syscall( SYS_EXIT,
                      (reg_t)status, 0, 0, 0 );
}

// 11 ///////////////////////////
int munmap( void         * addr,
            unsigned int   size )
{
    return hal_user_syscall( SYS_MUNMAP,
                             (reg_t)addr,
                             (reg_t)size, 0, 0 );
}

// 12 //////////////////////////
int open( const char * pathname,
          int          flags,
          int          mode )
{
    return hal_user_syscall( SYS_OPEN,
                             (reg_t)pathname,
                             (reg_t)flags,
                             (reg_t)mode, 0 );
}

// 13 /////////////////////////
void * mmap( void       * addr,
             unsigned int length,
             int          prot,
             int          flags,
             int          fd,
             unsigned int offset )
{
    mmap_attr_t attr;
    attr.addr   = addr;
    attr.length = length;
    attr.prot   = prot;
    attr.flags  = flags;
    attr.fdid   = fd;
    attr.offset = offset;

    if( hal_user_syscall( SYS_MMAP,
                          (reg_t)&attr, 0, 0, 0 ) ) return NULL;
    else                                            return attr.addr;
}

// 14 ////////////////////
int read( int          fd,
          void       * buf,
          unsigned int count )
{
    return hal_user_syscall( SYS_READ,
                             (reg_t)fd,
                             (reg_t)buf,
                             (reg_t)count, 0 );
}

// 15 /////////////////////
int write( int          fd,
           const void * buf,
           unsigned int count )
{
    return hal_user_syscall( SYS_WRITE,
                             (reg_t)fd,
                             (reg_t)buf,
                             (reg_t)count, 0 );
}

// 16 /////////////////////
int lseek( int          fd,
           unsigned int offset,
           int          whence )
{
    return hal_user_syscall( SYS_LSEEK,
                             (reg_t)fd,
                             (reg_t)offset,
                             (reg_t)whence, 0 );
}

// 17 /////////////
int close( int fd )
{
    return hal_user_syscall( SYS_CLOSE,
                             (reg_t)fd, 0, 0, 0 );
}

// 18 /////////////////////////////
int unlink( const char * pathname )
{
    return hal_user_syscall( SYS_UNLINK,
                             (reg_t)pathname, 0, 0, 0 );
}

// 19 ///////////////
int pipe( int fd[2] )
{
    return -1;
}

// 20 ////////////////////////////
int chdir( const char * pathname )
{
    return hal_user_syscall( SYS_CHDIR,
                             (reg_t)pathname, 0, 0, 0 );
}

// 21 ///////////////////////////
int mkdir( const char * pathname,
           int          mode )
{
    return hal_user_syscall( SYS_MKDIR,
                             (reg_t)pathname,
                             (reg_t)mode, 0, 0 );
}

// 22 ////////////////////////////
int mkfifo( const char * pathname,
            int          mode )
{
    return hal_user_syscall( SYS_MKFIFO,
                             (reg_t)pathname,
                             (reg_t)mode, 0, 0 );
}

// 23 ////////////////////////////////
DIR * opendir( const char * pathname )
{
    DIR   * dirp; 
    int     error;
    error = hal_user_syscall( SYS_OPENDIR,
                              (reg_t)pathname,
                              (reg_t)&dirp, 0, 0 );
    if( error ) return NULL;
    else        return dirp;
}

// 24 ///////////////////////////////
struct dirent * readdir( DIR * dirp )
{
    struct dirent * dentp;
    int             error;
    error = hal_user_syscall( SYS_READDIR,
                              (reg_t)dirp,
                              (reg_t)&dentp, 0, 0 );
    if( error ) return NULL;
    else        return dentp;
}

// 25 ////////////////////
int closedir( DIR * dirp )
{
    return hal_user_syscall( SYS_CLOSEDIR,
                             (reg_t)dirp, 0, 0, 0 );
}

// 26 ///////////////////////
int getcwd( char       * buf,
            unsigned int bytes )
{
    return hal_user_syscall( SYS_GETCWD,
                             (reg_t)buf,
                             (reg_t)bytes, 0, 0 );
}

// 29 //////////////////////
int rmdir( char * pathname )
{
    return hal_user_syscall( SYS_RMDIR,
                             (reg_t)pathname, 0, 0, 0 );
} 

// 30 ///////////////////////////
int utls( unsigned int operation,
          unsigned int value )
{
    return hal_user_syscall( SYS_UTLS,
                             (reg_t)operation,
                             (reg_t)value, 0, 0 );
}

// 31 /////////////////////////
int chmod( char     * pathname,
           unsigned int   rights )
{
    return hal_user_syscall( SYS_CHMOD,
                             (reg_t)pathname,
                             (reg_t)rights, 0, 0 );
}

// 32 ///////////////////////////
int signal( unsigned int   sigid,
            void         * handler )
{
    return hal_user_syscall( SYS_SIGNAL,
                             (reg_t)sigid,
                             (reg_t)handler, 0, 0 );
}

// 33 /////////////////////////////////
int gettimeofday( struct timeval  * tv,
                  struct timezone * tz )
{
    return hal_user_syscall( SYS_SIGNAL,
                             (reg_t)tv,
                             (reg_t)tz, 0, 0 );
}

// 34 /////////////////////
int kill( unsigned int pid,
          unsigned int sig_id )
{
    return hal_user_syscall( SYS_KILL,
                             (reg_t)pid,
                             (reg_t)sig_id, 0, 0 );
}

// 35 //////
int getpid()
{
    return hal_user_syscall( SYS_GETPID, 0, 0, 0, 0 );
}

// 36 ////
int fork()
{
    return hal_user_syscall( SYS_FORK, 0, 0, 0, 0 );
}

// 37 /////////////////////
int exec( char  * pathname,
          char ** argv,
          char ** envp )
{
    return hal_user_syscall( SYS_EXEC,
                             (reg_t)pathname,
                             (reg_t)argv,
                             (reg_t)envp, 0 );
}

// 38 ///////////////////////////
int stat( const char  * pathname,
          struct stat * stat )
{
    return hal_user_syscall( SYS_EXEC,
                             (reg_t)pathname,
                             (reg_t)stat, 0, 0 );
}

// 39 ////////////////////////////
int trace( unsigned int operation,
           unsigned int pid, 
           unsigned int trdid )
{
    return hal_user_syscall( SYS_TRACE,
                             (reg_t)pid,
                             (reg_t)trdid, 0, 0 );
}


