home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / libNS / freopen.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  923b  |  52 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #include    <stdio.h>
  3.  
  4. struct _iobuf *freopen(file, mode, iop)
  5. register char *mode;
  6. register struct _iobuf *iop;
  7. {
  8.     register f;
  9.     int unixmode, a_flag;
  10.  
  11.     fclose(iop);
  12.     a_flag = 0;
  13.     for(;*mode;mode++) {
  14.         switch(*mode) {
  15.  
  16.         case 'w':
  17.             iop->_flag |= _IOWRT;
  18.             break;
  19.  
  20.         case 'r':
  21.             iop->_flag |= _IOREAD;
  22.             break;
  23.  
  24.         case 'a':
  25.             a_flag = 1;
  26.             iop->_flag |= _IOWRT ;
  27.         }
  28.     }
  29.     if((unixmode = (iop->_flag & 3) - 1) < 0) {
  30.         unixmode = 0;
  31.         iop->_flag = 1;
  32.     }
  33.     if ((iop->_flag & _IOWRT) && a_flag==0 ) {
  34.         f = creat(file, 0644);
  35.         if((iop->_flag & _IOREAD) && (f > 0)) {
  36.             close(f);
  37.             f = open(file,2);
  38.         }
  39.     } else if ((0>(f = open(file, unixmode))) && (a_flag || !(iop->_flag&_IOREAD))){
  40.             f = creat(file, 0644);
  41.             close(f);
  42.             f = open(file,unixmode);
  43.         }
  44.  
  45.     if (f < 0)
  46.         return(NULL);
  47.     if (a_flag)
  48.         lseek(f, (long) 0, 2);
  49.     iop->_file = f;
  50.     return(iop);
  51. }
  52.