🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.bugs.sys5
Article #148

Re: AT&T system V fdopen mode checking

#148
From: Conor P. Cahill
Date: Sat, 02 Dec 1989 15:43
64 lines
2831 bytes
In article <11723@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn) writes:
> In article <713@mbf.UUCP> hae@mbf.UUCP (Hae Hirdler) writes:
> -The man page on the fdopen states that "the type of the stream must 
> -agree with the mode of the open file."
> 
> I believe by "mode" they mean "file access permissions".

Why would it have to do with the file permissions?  The only thing fdopen
does is associate a file pointer with a file descriptor.

Besides, if you open a file with O_RDONLY and then fdopen it with "w", 
the buffer flushes will fail because the "status flags" for the file 
descriptor do not allow write.  This is even the case when the access
permissions do permit write.

I think the problem is caused by the fact that older unixs did not
have an ability to obtain the current status flags for a file descriptor.
Therefore the fdopen() had no way to determine whether or not the 
fd mode and the fdopen mode matched.

Both System V and BSD (at least 4.3, not sure about prior) provide the 
F_GETFL functionality in fcntl() which can be used to determine the 
flags used to open the file (like O_RDONLY...).  fdopen() should be 
modified to check this and return NULL if appropriate.

> -For example, fd= open(filename, O_RDONLY); fptr= fdopen(filename, "w")
> -will return a NON_NULL fptr value instead of NULL.  
> 
> O_RDONLY has nothing to do with the file.  It is just a parameter to open().

It has everything to do with accessing data in the file via the file
descriptor.  Since fdopen uses the file descriptor (not the file name as
is displayed above) it will also have an effect on the correct usage
of the file pointer returned by fdopen().

As an example, the following code will return an error at either the
fprintf or the fclose() (usually the fclose since that is where the buffer
is flushed).

#include <stdio.h>
#include <fcntl.h>

main()
{
	int fd,status;
	FILE * fp;

	if( (fd=open("test",O_RDONLY)) == -1)
		exit(1);

	if( (fp=fdopen(fd,"w")) == NULL)
		 exit(2);

	printf("cnt = %d\n",             fprintf(fp,"this is a test\n"));

	printf("fclose returned %d\n",   fclose(fp));
	
	exit(0);
}
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+
Technical Details
Message-ID: <1989Dec2.154336.23613@virtech.uucp>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!virtech!cpcahil
References: <713@mbf.UUCP> <11723@smoke.BRL.MIL>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!virtech!cpcahil
From: cpcahil@virtech.uucp (Conor P. Cahill)
Newsgroups: comp.bugs.sys5
Subject: Re: AT&T system V fdopen mode checking
Message-ID: <1989Dec2.154336.23613@virtech.uucp>
Date: 2 Dec 89 15:43:36 GMT
References: <713@mbf.UUCP> <11723@smoke.BRL.MIL>
Organization: Virtual Technologies Inc.
Lines: 64