home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / fopen.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  3KB  |  150 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8.  
  9. __EXTERN void _getbuf __PROTO((FILE *));
  10. static FILE *_fopen __PROTO((const char *, const char *, FILE *));
  11.  
  12. /* lowest character device handle # */
  13. #define    LAST_DEVICE    __SMALLEST_VALID_HANDLE
  14.  
  15. extern int __default_mode__;
  16.  
  17. static FILE *_fopen(filename, mode, fp)
  18.     const char *filename;
  19.     const char *mode;
  20.     FILE *fp;
  21. /*
  22.  *    INTERNAL FUNCTION.  Attempt to open <filename> in the given
  23.  *    <mode> and attach it to the stream <fp>
  24.  */
  25.     {
  26.     register int h, i, iomode = 0, f = __default_mode__;
  27.  
  28.     while(*mode)
  29.         {
  30.         switch(*mode++)
  31.             {
  32.             case 'r':
  33.                 f |= _IOREAD;
  34.                 break;
  35.             case 'w':
  36.                 f |= _IOWRT;
  37.                 iomode |= (O_CREAT | O_TRUNC);
  38.                 break;
  39.             case 'a':
  40.                 f |= _IOWRT;
  41.                 iomode |= (O_CREAT | O_APPEND);
  42.                 break;
  43.             case '+':
  44.                 f &= ~(_IOREAD | _IOWRT);
  45.                 f |= _IORW;
  46.                 break;
  47.             case 'b':
  48.                 f |= _IOBIN;
  49.                 break;
  50.             case 't':
  51.                 f &= ~_IOBIN;
  52.                 break;
  53.             default:
  54.                 return(NULL);
  55.             }
  56.         }
  57.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  58.         return(NULL);
  59.     else if(i == _IOREAD)
  60.         iomode |= O_RDONLY;
  61.     else if(i == _IOWRT)
  62.         iomode |= O_WRONLY;
  63.     else
  64.         iomode |= O_RDWR;
  65.     h = open(filename, iomode, 0644);
  66.     if(h < __SMALLEST_VALID_HANDLE)
  67.         {
  68.         return(NULL);        /* file open/create error */
  69.         }
  70.     if(isatty(h))
  71.         f |= (_IODEV | _IOLBF);
  72.     else
  73.         f |= _IOFBF;
  74.     fp->_file = h;            /* file handle */
  75.     fp->_flag = f;            /* file status flags */
  76.  
  77.     return(fp);
  78.     }
  79.  
  80. FILE *fopen(filename, mode)
  81.     const char *filename, *mode;
  82.     {
  83.     register int i;
  84.     register FILE *fp = NULL;
  85.  
  86.     for(i=0; (!fp && (i < _NFILE)); ++i)
  87.         if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT))) 
  88.             fp = &_iob[i]; /* empty slot */
  89.     if(fp != NULL)
  90.     {
  91.         if(_fopen(filename, mode, fp) == NULL)
  92.             return NULL;
  93.         _getbuf(fp);
  94.         return fp;
  95.     }
  96.     else
  97.       {
  98.         errno = EMFILE;
  99.         return NULL;
  100.       }
  101.     }
  102.  
  103. /*
  104.  * re-coded,  foobared code deleted
  105.  *
  106.  *    ++jrb
  107.  */
  108. FILE *freopen(filename, mode, fp)
  109.     const char *filename, *mode;
  110.     FILE *fp;
  111. {
  112.     unsigned int f;
  113.     
  114.     if(fp == NULL) return NULL;
  115.     
  116.     f = fp->_flag;
  117.     if((f & (_IORW | _IOREAD | _IOWRT)) != 0)
  118.     { /* file not closed, close it */
  119. #if 0
  120.         if(fflush(fp) != 0) return NULL;            /* flush err */
  121.         if(close(fp->_file) != 0) return NULL;        /* close err */
  122. #else
  123.     fflush(fp); close(fp->_file);        /* ANSI says ignore errors */
  124. #endif
  125.     }
  126.     /* save buffer discipline and setbuf settings, and _IOWRT just for
  127.        determinining line buffering after the next _fopen */
  128.     f &= (_IOFBF | _IOLBF | _IONBF | _IOMYBUF | _IOWRT);
  129.  
  130.     /* open the new file according to mode */
  131.     if((fp = _fopen(filename, mode, fp)) == NULL)
  132.     return NULL;
  133.     if(fp->_flag & _IODEV)
  134.     { /* we are re-opening to a tty */
  135.     if((f & _IOFBF) && (f & _IOWRT) && (f & _IOMYBUF))
  136.     {   /* was a FBF & WRT & !setvbuff'ed  */
  137.         /* reset to line buffering */
  138.         f &= ~_IOFBF;
  139.         f |=  _IOLBF;
  140.     }
  141.     }
  142.     f &= ~_IOWRT; /* get rid of saved _IOWRT flag */
  143.     
  144.     /* put buffering and discipline flags in new fp->_flag */
  145.     fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF | _IOMYBUF);
  146.     fp->_flag |= f;
  147.     
  148.     return fp;
  149.     }
  150.