From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: Date: Mon, 9 Oct 2006 01:01:57 -0300 From: "Felipe Bichued" To: 9fans <9fans@cse.psu.edu> Subject: Re: [9fans] non-truncating create In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: Topicbox-Message-UUID: c735a614-ead1-11e9-9d60-3106f5b1d025 Hello, You might want to read this: http://lsub.org/who/nemo/9.intro.pdf Regards. On 10/9/06, Joel "chesky" Salomon wrote: > Still for the Brain-Dead SHell homework assignment... > > I'm trying to implement output redirection, > and >>. Truncating > redirect (>) is easy; just use create(2) whether the file exists or > no. The non-truncing option is puzzling me, though. If the file > exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the > file does not exist I need to create it. > > How do I check for the existence of a file? I'm thinking about the > following code snippet: > o = open(oname, OWRITE) > rerrstr(errbuf, ERRMAX); > if((strstr(errbuf, "file does not exist") == 0){ > complain("cannot open %s: %r", oname); > return; > }else o = create(oname, OWRITE, ~0); > if(o < 0) > ... > Is there a cleaner way to accomplish this? Some form of stat(2) that has an explicit "file does not exist" code outside parsing errstr > > Of secondary interest: Not that this is production code, but should I > be concerned about race conditions where the file is created by > somebody else between the failed open(2) and the create(2)? > > Of tertiary interest: Is there a general use for a library function > complain() that behaves identically to sysfatal(2) except for not > terminating the program? > > > --Joel > > -- Felipe