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

  1. /* _fopen.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8.  
  9. FILE *_fopen (FILE *dst, const char *fname, const char *mode, int shflag)
  10. {
  11.   char ok, bt;
  12.   int omode;
  13.  
  14.   switch (*mode)
  15.     {
  16.     case 'r':
  17.       dst->flags = _IOREAD;
  18.       omode = O_RDONLY;
  19.       break;
  20.     case 'w':
  21.       dst->flags = _IOWRT;
  22.       omode = O_WRONLY|O_CREAT|O_TRUNC;
  23.       break;
  24.     case 'a':
  25.       dst->flags = _IOWRT;
  26.       omode = O_WRONLY|O_CREAT|O_APPEND;
  27.       break;
  28.     default:
  29.       errno = EINVAL;
  30.       return (NULL);
  31.     }
  32.   ++mode; ok = TRUE; bt = FALSE;
  33.   while (*mode != 0 && ok)
  34.     {
  35.       switch (*mode)
  36.         {
  37.         case 't':
  38.           if (bt)
  39.             ok = FALSE;
  40.           else
  41.             {
  42.               bt = TRUE;
  43.               omode |= O_TEXT;
  44.             }
  45.           break;
  46.         case 'b':
  47.           if (bt)
  48.             ok = FALSE;
  49.           else
  50.             {
  51.               bt = TRUE;
  52.               omode |= O_BINARY;
  53.             }
  54.           break;
  55.         case '+':
  56.           if (dst->flags & _IORW)
  57.             ok = FALSE;
  58.           else
  59.             {
  60.               omode &= ~(O_RDONLY|O_WRONLY);
  61.               omode |= O_RDWR;
  62.               dst->flags &= ~(_IOREAD|_IOWRT);
  63.               dst->flags |= _IORW;
  64.             }
  65.           break;
  66.         default:
  67.           ok = FALSE; break;
  68.         }
  69.       if (ok) ++mode;
  70.     }
  71.   dst->handle = sopen (fname, omode, shflag, 0644);
  72.   if (dst->handle < 0)
  73.     return (NULL);
  74.   dst->ptr = NULL;
  75.   dst->buffer = NULL;
  76.   dst->rcount = 0;
  77.   dst->wcount = 0;
  78.   dst->tmpidx = 0;
  79.   dst->flags |= _IOOPEN | _IOBUFNONE;
  80.   dst->flush = _flushstream;
  81.   return (dst);
  82. }
  83.