/*
**    Name: open.c
**
**    Date: Sat Jan  1 16:10:33 2005
**
**    Purpose:
**
*/

#include "csfcntl.h"
#include "kernel.h"
#include "osfind.h"
#include "cserrno.h"
#include <stddef.h>

int csopen(const char *filename,int mode)
    {
    int mymode;

    _kernel_swi_regs regs;

    mymode = osfind_NO_PATH;

    if (filename == NULL || (mode & ~(CSO_RDONLY | CSO_WRONLY | CSO_RDWR | CSO_BINARY)))
        {
        errno = EINVAL;
        return -1;
        }

    switch (mode & (CSO_RDONLY | CSO_WRONLY | CSO_RDWR))
        {
        case CSO_RDONLY: mymode |= OSFind_Openin; break;
        case CSO_WRONLY: mymode |= OSFind_Openout; break;
        case CSO_RDWR: mymode |= OSFind_Openup; break;
        default: errno = EINVAL; return -1;
        }

    regs.r[0] = mymode;
    regs.r[1] = (int)filename;

    if (_kernel_swi(XOS_Find,&regs,&regs) != NULL)
        {
        errno = EIO;
        return -1;
        }

    if (regs.r[0] == 0) {
        errno = ENOENT;
        return -1;
    }

    return regs.r[0];
    }
