home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / _SOPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  2.9 KB  |  109 lines

  1. /* _sopen.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <io.h>
  5. #include <stdarg.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. /* Bugs: O_TRUNC|O_RDONLY not implemented */
  12. /*       O_TEXT|O_WRONLY  does/can not overwrite Ctrl-Z */
  13.  
  14. int _fmode_bin;                 /* Set non-zero to make binary mode default */
  15.  
  16. #define SH_MASK           0x70
  17.  
  18. int _sopen (const char *name, int oflag, int shflag, va_list va)
  19. {
  20.   int access, attr, handle, pmode, flags, saved_errno;
  21.   int bits;
  22.   char dummy, new;
  23.   long last;
  24.  
  25.   access = (oflag & O_ACCMODE) | (shflag & SH_MASK);
  26.   new = FALSE;
  27.   flags = access;
  28.   saved_errno = errno;
  29.   handle = __open (name, flags);
  30.   if (handle < 0)
  31.     {
  32.       if (errno != ENOENT)    /* file/path not found */
  33.         return (-1);
  34.       if (!(oflag & O_CREAT))
  35.         {
  36.           errno = ENOENT;
  37.           return (-1);
  38.         }
  39.       /* create file */
  40.       errno = saved_errno;
  41.       new = TRUE;
  42.       attr = 0;
  43.       pmode = va_arg (va, int);
  44.       if (!(pmode & S_IWRITE))
  45.         attr |= _A_RDONLY;
  46.       flags = access | (attr << 8) | 0x10000;
  47.       handle = __open (name, flags);
  48.       if (handle < 0)
  49.         return (-1);
  50.     }
  51.   if (handle >= _nfiles)
  52.     {
  53.       __close (handle);
  54.       errno = EMFILE;
  55.       return (-1);
  56.     }
  57.   if (!new && (oflag & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  58.     {
  59.       __close (handle);
  60.       errno = EEXIST;
  61.       return (-1);     /* File exists... error */
  62.     }
  63.   bits = 0;
  64.   if (oflag & O_BINARY)
  65.     /* do nothing */;
  66.   else if (oflag & O_TEXT)
  67.     bits |= O_TEXT;
  68.   else if (_fmode_bin == 0)          /* neither O_TEXT nor O_BINARY given */
  69.     bits |= O_TEXT;
  70.   if (__ioctl1 (handle, 0) & 0x80)
  71.     {
  72.       bits |= F_DEV;
  73.       oflag &= ~O_APPEND;
  74.     }
  75.   bits |= oflag & (O_ACCMODE|O_NDELAY|O_APPEND);
  76.   if (!(bits & F_DEV))
  77.     {
  78.       if (oflag & O_TRUNC)
  79.         {
  80.           if ((oflag & O_ACCMODE) == O_RDWR || (oflag & O_ACCMODE) == O_WRONLY)
  81.             {
  82.               if (__ftruncate (handle, 0) != 0)     /* Truncate file */
  83.                 {
  84.                   saved_errno = errno;
  85.                   __close (handle);
  86.                   errno = saved_errno;
  87.                   return (-1);
  88.                 }
  89.             }
  90.           else
  91.             {
  92.               __close (handle);
  93.               errno = ENOENT;
  94.               return (-1);                  /* Not implemented */
  95.             }
  96.         }
  97.       else if ((oflag & O_RDWR) && (oflag & O_TEXT))
  98.         {
  99.           last = __lseek (handle, -1L, SEEK_END);
  100.           if (__read (handle, &dummy, 1) == 1 && dummy == 0x1a)
  101.             __ftruncate (handle, last);      /* Remove Ctrl-Z) */
  102.           __lseek (handle, 0L, SEEK_SET);
  103.         }
  104.     }
  105.   _files[handle] = bits;
  106.   _lookahead[handle] = -1;
  107.   return (handle);
  108. }
  109.