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