Writing to A NON-Existing File in "C"

Stephen J. Friedl friedl at vsi.UUCP
Sat Apr 16 12:28:01 AEST 1988


In article <9654 at jplgodo.UUCP> deutsch at jplgodo.UUCP (Michael Deutsch ) writes:
> I have a "C" program that records the program results into a file,
> provided that file already exists.  In case the file DOES NOT exist
> I want the program to function identically but the results should
> be flushed down the tube, i.e. nowhere, i.e.  written to a nong
> file?

Then (William E. Davidsen Jr) replies:
>  [little C fragment to do the above]

In article <147 at obie.UUCP>, wes at obie.UUCP (Barnacle Wes) writes:
> I'd do it slightly different.  Unless you need speed, try using
> access(2) to determine if the file is available to you, and if not
> open /dev/null for writing:
>
> [little C fragment to do the above]

AAAAAAAAAARGH!  Please do not use access(2) as a handy-dandy
"does the file exist?" function.  It is designed to be used
by set-{user,group}-id programs to see if the underlying user
has permission to access the file in the requested manner.
I hate to sound like a flamer, but programmers who do not
understand the troubles with access(2) should probably not
use it; you will make life difficult for somebody down the
road.  Stat(2) can be used to do the same thing with just a
little extra code.

I wrote an "accfile(3)" function that does what you *think*
access(2) is doing; send me a note to get a copy.

Steve Friedl, resident access(2)-basher

-- 
Steve Friedl   V-Systems, Inc.   "Yes, I'm jeff at unh's brother"
friedl at vsi.com  {backbones}!vsi.com!friedl  attmail!vsi!friedl



More information about the Comp.unix.questions mailing list