[comp.os.minix] new and improved freopen.c

meulenbr@cst.UUCP (Frans Meulenbroeks) (11/12/88)

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 <stdio.h>
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; i++)
X		if (fp == _io_table[i]) {
X			break;
X		}
X	if (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)

katzung@laidbak.UUCP (Brian Katzung) (11/15/88)

In article <260@cst.UUCP> meulenbr@cst.UUCP (Frans Meulenbroeks) writes:
>X#define  PMODE    0644

Shouldn't this be 0666 so that umask can be used to get the desired mode?

>X	flags = flags & !WRITEMODE & !READMODE;

That sets all bits to zero (perhaps those are network-mangled tildas??).
I believe you meant

	flags &= ~(WRITEMODE | READMODE);

>X	case 'a': 
>X		flags |= WRITEMODE;
>X		if (( fd = open(name,1)) < 0 )
>X			return((FILE *)NULL);
>X		lseek(fd,0L,2);
>X		break;         

Append is really two attempts:

	if ((fd = open(name, 1)) < 0 && (fd = creat(name, PMODE)) < 0)
		return ((FILE *) NULL);

Does Minix support O_APPEND mode?  If not, perhaps there needs to be
an APPENDMODE bit (see flame below) to achieve nearly the same effect.
The only reference I have handy right now (SVID) guarantees writes at
the end.

Do the original routines support r+, w+, and a+ modes?  These are nice
to have.  I'm not near a Minix machine at the moment.  Does Minix support
open with 3 arguments?  This would make an implementation a little
easier/cleaner.

>Frans Meulenbroeks        (meulenbr@cst.prl.philips.nl)
>	Centre for Software Technology
>	( or try: ...!mcvax!philmds!prle!cst!meulenbr)

VERY small flame/spark (but not aimed at Frans) on a related topic:
Aren't READMODE and WRITEMODE rather "central" in the name-space
for "semi-invisible" definitions?  It seems to me that something
like _SIO_READ and _SIO_WRITE might have been a bit less likely to
collide with application definitions.

-- Brian Katzung  ...!spl1!laidbak!katzung

meulenbr@cstw01.UUCP (Frans Meulenbroeks) (11/18/88)

In article <1782@laidbak.UUCP> katzung@laidbak.UUCP (Brian Katzung) writes:
>In article <260@cst.UUCP> meulenbr@cst.UUCP (Frans Meulenbroeks) writes:
>>X#define  PMODE    0644
>
>Shouldn't this be 0666 so that umask can be used to get the desired mode?

I've not the slightest idea! I just picked this from the original fopen.c
Can one of the Minix wizards (Johan? Andy?) comment?
>
>>X	flags = flags & !WRITEMODE & !READMODE;
>
>That sets all bits to zero (perhaps those are network-mangled tildas??).
>I believe you meant
>
>	flags &= ~(WRITEMODE | READMODE);
>
Oops: I meant:
	flags = flags & ~WRITEMODE & ~READMODE;
The error was brain mangled, not network mangled.

Note that this line of code is due to the fact that I assumed that
buffering etc. stays the same during a reopen. Could not find anything to
assert or deny this assumption. Anyone an idea?

>>X	case 'a': 
>>X		flags |= WRITEMODE;
>>X		if (( fd = open(name,1)) < 0 )
>>X			return((FILE *)NULL);
>>X		lseek(fd,0L,2);
>>X		break;         
>
>Append is really two attempts:
>
>	if ((fd = open(name, 1)) < 0 && (fd = creat(name, PMODE)) < 0)
>		return ((FILE *) NULL);
>
[ some stuff deleted]
The code I sent is again copied from fopen.c
>
>Do the original routines support r+, w+, and a+ modes?  These are nice

No! I'd love to see them, but haven't had time to make them.

>to have.  I'm not near a Minix machine at the moment.  Does Minix support
>open with 3 arguments?  This would make an implementation a little
>easier/cleaner.

Again. No.
>
[ small flame deleted]
>
>-- Brian Katzung  ...!spl1!laidbak!katzung



Newsgroups: comp.os.minix
Subject: Re: new and improved freopen.c (I hope)
Summary: 
Expires: 
References: <260@cst.UUCP> <1782@laidbak.UUCP>
Sender: 
Reply-To: meulenbr@cstw01.UUCP (Frans Meulenbroeks)
Followup-To: 
Distribution: 
Organization: Centre for Software Technology, Philips Eindhoven
Keywords: 

In article <1782@laidbak.UUCP> katzung@laidbak.UUCP (Brian Katzung) writes:
>In article <260@cst.UUCP> meulenbr@cst.UUCP (Frans Meulenbroeks) writes:
>>X#define  PMODE    0644
 
>Shouldn't this be 0666 so that umask can be used to get the desired mode?

I've not the slightest idea! I just picked this from the original fopen.c
Can one of the Minix wizards (Johan? Andy?) comment?
 
>>X	flags = flags & !WRITEMODE & !READMODE;
 
>That sets all bits to zero (perhaps those are network-mangled tildas??).
>I believe you meant
 
>	flags &= ~(WRITEMODE | READMODE);
 
Oops: I meant:
	flags = flags & ~WRITEMODE & ~READMODE;
The error was brain mangled, not network mangled.

Note that this line of code is due to the fact that I assumed that
buffering etc. stays the same during a reopen. Could not find anything to
assert or deny this assumption. Anyone an idea?

>>X	case 'a': 
>>X		flags |= WRITEMODE;
>>X		if (( fd = open(name,1)) < 0 )
>>X			return((FILE *)NULL);
>>X		lseek(fd,0L,2);
>>X		break;         
 
>Append is really two attempts:
 
>	if ((fd = open(name, 1)) < 0 && (fd = creat(name, PMODE)) < 0)
>		return ((FILE *) NULL);
 
[ some stuff deleted]
The code I sent is again copied from fopen.c
 
>Do the original routines support r+, w+, and a+ modes?  These are nice

No! I'd love to see them, but haven't had time to make them.

>to have.  I'm not near a Minix machine at the moment.  Does Minix support
>open with 3 arguments?  This would make an implementation a little
>easier/cleaner.

Again. No.
 
[ small flame deleted]
 
>-- Brian Katzung  ...!spl1!laidbak!katzung


-- 
Frans Meulenbroeks        (meulenbr@cst.prl.philips.nl)
	Centre for Software Technology
	( or try: ...!mcvax!philmds!prle!cst!meulenbr)