Path: utzoo!attcan!uunet!mcvax!hp4nl!philmds!prle!cst!meulenbr From: meulenbr@cst.UUCP (Frans Meulenbroeks) Newsgroups: comp.os.minix Subject: new and improved freopen.c (I hope) Message-ID: <260@cst.UUCP> Date: 11 Nov 88 18:41:04 GMT Reply-To: meulenbr@cst.UUCP (Frans Meulenbroeks) Organization: Centre for Software Technology, Philips Eindhoven Lines: 101 Hi! The current Minix/ST freopen does not do the job properly. I've hacked my own which is better (I hope). Feel free to share or improve it. Flames to /dev/null. Use at own risk. The archive is followed by exit and my signature. Frans Meulenbroeks. # This is a shell archive. # Remove everything above and including the cut line. # Then run the rest of the file through sh. -----cut here-----cut here-----cut here-----cut here----- #!/bin/sh # shar: Shell Archiver # Run the following text with /bin/sh to create: # freopen.c # This archive created: Fri Nov 11 19:40:10 1988 sed 's/^X//' << \SHAR_EOF > freopen.c X/* new freopen.c for minix/st X hacked together: 11/11/88 by F. Meulenbroeks. X Disclaimer: almost all of this code is extracted from fopen.c and X fclose.c. Use at own risk. X X The problem with the original freopen was that it did not X use the same FILE struct to store its values. X This results in problems when using code like X FILE *yyin = stdin; X which occurs in flex. X X Also freopen should return the original value of stream. X This is not the case in the minix/st freopen. X X The original freopen does not neccesarily return the same entry in _io_table X if another file is closed before. This causes problems when reopening X stderr after closing stdin or stdout (I think). X X I decided the when doing an freopen, one uses the same buffer and X buffering method. ANSI C does not explicitly specify this. X*/ X X#include X X#define PMODE 0644 X XFILE *freopen(name, mode,fp) Xchar *name , *mode; XFILE *fp; X{ X register int i; X int fd, flags; X X for (i=0; i= NFILES) X return((FILE *)EOF); X flags = fp->_flags; X flags = flags & !WRITEMODE & !READMODE; X fflush(fp); X close(fp->_fd); X X switch(*mode){ X X case 'w': X flags |= WRITEMODE; X if (( fd = creat (name,PMODE)) < 0) X return((FILE *)NULL); X break; X X case 'a': X flags |= WRITEMODE; X if (( fd = open(name,1)) < 0 ) X return((FILE *)NULL); X lseek(fd,0L,2); X break; X X case 'r': X flags |= READMODE; X if (( fd = open (name,0)) < 0 ) X return((FILE *)NULL); X break; X X default: X return((FILE *)NULL); X } X X fp->_count = 0; X fp->_fd = fd; X fp->_flags = flags; X fp->_ptr = fp->_buf; X return(fp); X} SHAR_EOF # End of shell archive exit 0 -- Frans Meulenbroeks (meulenbr@cst.prl.philips.nl) Centre for Software Technology ( or try: ...!mcvax!philmds!prle!cst!meulenbr)