home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / mntlib16.lzh / MNTLIB16 / FDOPEN.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  1KB  |  69 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6.  
  7. extern    FILE    _iob[];
  8.  
  9. FILE *fdopen(h, mode)
  10.     register int h;
  11.     const register char *mode;
  12. {
  13.     extern int __default_mode__;    /* see binmode.c */
  14.     register int i, iomode = 0, f = __default_mode__;
  15.     register FILE *fp = NULL;
  16.     void _getbuf(FILE *);
  17.  
  18.     for(i=0; (!fp && (i < _NFILE)); ++i)
  19.         if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT)))
  20.             fp = &_iob[i];    /* empty slot */
  21.     if(!fp)
  22.         return(NULL);
  23.  
  24.     while(*mode) {
  25.         switch(*mode++) {
  26.             case 'r':
  27.                 f |= _IOREAD;
  28.                 break;
  29.             case 'w':
  30.                 f |= _IOWRT;
  31.                 iomode |= (O_CREAT | O_TRUNC);
  32.                 break;
  33.             case 'a':
  34.                 f |= _IOWRT;
  35.                 iomode |= (O_CREAT | O_APPEND);
  36.                 break;
  37.             case '+':
  38.                 f &= ~(_IOREAD | _IOWRT);
  39.                 f |= _IORW;
  40.                 break;
  41.             case 'b':
  42.                 f |= _IOBIN;
  43.                 break;
  44.             case 't':
  45.                 f &= ~_IOBIN;
  46.                 break;
  47.             default:        /* illegal file mode */
  48.                 return(NULL);
  49.             }
  50.     }
  51.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  52.         return(NULL);
  53.     else if(i == _IOREAD)
  54.         iomode |= O_RDONLY;
  55.     else if(i == _IOWRT)
  56.         iomode |= O_WRONLY;
  57.     else
  58.         iomode |= O_RDWR;
  59.  
  60.     if(isatty(h))
  61.         f |= (_IODEV | _IONBF);
  62.     else
  63.         f |= _IOFBF;
  64.     fp->_file = h;            /* file handle */
  65.     fp->_flag = f;            /* file status flags */
  66.     _getbuf(fp);
  67.     return(fp);
  68. }
  69.