home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / freopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1.0 KB  |  46 lines

  1.  
  2.  
  3. /* freopen */
  4.  
  5. /* another kludge.  sigh */
  6.  
  7. #include <file.h>
  8. #include "std-guts.h"
  9.  
  10. struct file * freopen(pathname, mode, f)
  11. char * pathname;
  12. char * mode;
  13. struct file * f;
  14. {
  15. /* don't know quite how this should work.  For now, close the handle, if
  16.    it's a real file, and then do an fopen equivalent on the file struct.
  17.    From looking at code that uses this, it looks like it's supposed
  18.    to return the file struct it was passed if it wins, NULL else.  What
  19.    a crock!
  20. */
  21.  
  22.   int new_modes, new_handle;
  23.   
  24.   if (f->open_p && (((f->mode & 0x03) == O_WRONLY) || 
  25.             ((f->mode & 0x03) == O_RDWR)))
  26.     fflush(f);
  27.  
  28.   if (!isatty(f->handle))
  29.     close(f->handle);
  30.   f->open_p = 0;
  31.  
  32.   new_modes = _parse_open_options(mode);
  33.   new_handle = open(pathname, new_modes, 0);
  34.   if (new_handle < 0)
  35.     return(0);
  36.  
  37. /* ok the new file is there.  go ahead and do the rest of fopen */
  38.   f->mode = new_modes;
  39.   f->handle = new_handle;
  40.   f->open_p = 1;
  41.   f->last_file_error = 0;
  42.  
  43. /* more??? */
  44.   return(f);
  45. }
  46.