home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001018a < prev    next >
Text File  |  1991-11-18  |  1KB  |  51 lines

  1. Listing 6 -- the file xfoprep.c
  2.  
  3.  
  4. /* _Foprep function */
  5. #include "xstdio.h"
  6.  
  7. /* open a stream */
  8. FILE *_Foprep(const char *name, const char *mods,
  9.     FILE *str)
  10.     {    /* make str safe for fclose, macros */
  11.     str->_Handle = -1;
  12.     str->_Tmpnam = NULL;
  13.     str->_Buf = NULL;
  14.     str->_Next = &str->_Cbuf;
  15.     str->_Rend = &str->_Cbuf;
  16.     str->_Wend = &str->_Cbuf;
  17.     str->_Nback = 0;
  18.     str->_Mode = (str->_Mode & _MALFIL)
  19.         | (*mods == 'r' ? _MOPENR
  20.         : *mods == 'w' ? _MCREAT|_MOPENW|_MTRUNC
  21.         : *mods == 'a' ? _MCREAT|_MOPENW|_MOPENA
  22.         : 0);
  23.     if ((str->_Mode & (_MOPENR|_MOPENW)) == 0)
  24.         {    /* bad mods */
  25.         fclose(str);
  26.         return (NULL);
  27.         }
  28.     while (*++mods== 'b' || *mods == '+')
  29.         if (*mods == 'b')
  30.             if (str->_Mode & _MBIN)
  31.                 break;
  32.             else
  33.                 str->_Mode |= _MBIN;
  34.         else
  35.             if ((str->_Mode & (_MOPENR|_MOPENW))
  36.                 == (_MOPENR|_MOPENW))
  37.                 break;
  38.             else
  39.                 str->_Mode |= _MOPENR|_MOPENW;
  40.     str->_Handle = _Fopen(name, str->_Mode, mods);
  41.     if (str->_Handle < 0)
  42.         {    /* open failed */
  43.         fclose(str);
  44.         return (NULL);
  45.         }
  46.     return (str);
  47.     }
  48.  
  49.  
  50.  
  51.