home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / open.c < prev    next >
C/C++ Source or Header  |  1992-08-13  |  2KB  |  110 lines

  1. #define INCL_DOSFILEMGR
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4. #include <stdarg.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9.  
  10. int __textio[20] = { 0,0,0,};
  11. char __textbuf[8096];
  12.  
  13. ULONG Dos32Open() asm ("Dos32Open");
  14.  
  15. int open (const char *path, int flags, ...)
  16. {
  17.    ULONG rc;
  18.    HFILE handle;
  19.    ULONG ActionTaken;
  20.    ULONG FileAttributes = FILE_NORMAL;
  21.    ULONG OpenFlag;
  22.    ULONG RWaccess;
  23.    va_list list;
  24.    int mode;
  25.  
  26.    if (path == 0)
  27.    {
  28.       errno = EINVAL;
  29.       return (-1);
  30.    }
  31.  
  32.    if ((flags & 3) == O_CREAT)
  33.    {
  34.       va_start(list, flags);
  35.       mode = va_arg(list, int);
  36.       if (mode == S_IREAD)
  37.          FileAttributes = FILE_READONLY;
  38.       else
  39.          FileAttributes = FILE_NORMAL;
  40.    }
  41.  
  42.  
  43.    if ((flags & 3) == O_RDONLY)
  44.       OpenFlag = OPEN_ACTION_OPEN_IF_EXISTS;
  45.    else
  46.       OpenFlag = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
  47.  
  48.  
  49.    if ((flags & 3) == O_RDONLY)
  50.       RWaccess = OPEN_ACCESS_READONLY;
  51.    else
  52.       if ((flags & 3) == O_WRONLY)
  53.          RWaccess = OPEN_ACCESS_WRITEONLY;
  54.       else
  55.          RWaccess = OPEN_ACCESS_READWRITE;
  56.  
  57.    rc = Dos32Open ((PSZ)path,
  58.                    &handle,
  59.                    &ActionTaken,
  60.                    0,
  61.                    FileAttributes,
  62.                    OpenFlag,
  63.                    OPEN_SHARE_DENYNONE | RWaccess,
  64.                    0);
  65.  
  66.    if (rc)
  67.    {
  68.       if (rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND
  69.           || rc == ERROR_OPEN_FAILED)
  70.       {
  71.          errno = ENOENT;
  72.          return (-1);
  73.       }
  74.  
  75.       if (rc == ERROR_TOO_MANY_OPEN_FILES)
  76.       {
  77.          errno = EMFILE;
  78.          return (-1);
  79.       }
  80.  
  81.       if (rc == ERROR_ACCESS_DENIED ||
  82.           rc == ERROR_INVALID_ACCESS ||
  83.           rc == ERROR_SHARING_VIOLATION)
  84.       {
  85.          errno = EACCES;
  86.          return (-1);
  87.       }
  88.  
  89.       if (rc == ERROR_DISK_FULL)
  90.       {
  91.          errno = ENOSPC;
  92.          return (-1);
  93.       }
  94.  
  95.       if (rc == ERROR_FILENAME_EXCED_RANGE)
  96.       {
  97.          errno = ENAMETOOLONG;
  98.          return (-1);
  99.       }
  100.  
  101.       errno = EIO;
  102.       return (-1);
  103.    }
  104.  
  105.    __textio[handle] = flags;
  106.  
  107.    return (handle);
  108. }
  109.  
  110.