home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
LIBSRC.ZOO
/
libsrc
/
local
/
open.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-13
|
2KB
|
110 lines
#define INCL_DOSFILEMGR
#define INCL_DOSERRORS
#include <os2.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int __textio[20] = { 0,0,0,};
char __textbuf[8096];
ULONG Dos32Open() asm ("Dos32Open");
int open (const char *path, int flags, ...)
{
ULONG rc;
HFILE handle;
ULONG ActionTaken;
ULONG FileAttributes = FILE_NORMAL;
ULONG OpenFlag;
ULONG RWaccess;
va_list list;
int mode;
if (path == 0)
{
errno = EINVAL;
return (-1);
}
if ((flags & 3) == O_CREAT)
{
va_start(list, flags);
mode = va_arg(list, int);
if (mode == S_IREAD)
FileAttributes = FILE_READONLY;
else
FileAttributes = FILE_NORMAL;
}
if ((flags & 3) == O_RDONLY)
OpenFlag = OPEN_ACTION_OPEN_IF_EXISTS;
else
OpenFlag = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
if ((flags & 3) == O_RDONLY)
RWaccess = OPEN_ACCESS_READONLY;
else
if ((flags & 3) == O_WRONLY)
RWaccess = OPEN_ACCESS_WRITEONLY;
else
RWaccess = OPEN_ACCESS_READWRITE;
rc = Dos32Open ((PSZ)path,
&handle,
&ActionTaken,
0,
FileAttributes,
OpenFlag,
OPEN_SHARE_DENYNONE | RWaccess,
0);
if (rc)
{
if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND
|| rc == ERROR_OPEN_FAILED)
{
errno = ENOENT;
return (-1);
}
if (rc == ERROR_TOO_MANY_OPEN_FILES)
{
errno = EMFILE;
return (-1);
}
if (rc == ERROR_ACCESS_DENIED ||
rc == ERROR_INVALID_ACCESS ||
rc == ERROR_SHARING_VIOLATION)
{
errno = EACCES;
return (-1);
}
if (rc == ERROR_DISK_FULL)
{
errno = ENOSPC;
return (-1);
}
if (rc == ERROR_FILENAME_EXCED_RANGE)
{
errno = ENAMETOOLONG;
return (-1);
}
errno = EIO;
return (-1);
}
__textio[handle] = flags;
return (handle);
}