From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: Date: Mon, 9 Oct 2006 00:00:39 -0400 From: "Russ Cox" To: "Fans of the OS Plan 9 from Bell Labs" <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: c72e0e04-ead1-11e9-9d60-3106f5b1d025 > 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: Rc just tries the open, and if that fails, tries the create. It doesn't even bother looking at the error code. If the open failed due to permissions, so will the create. Or use create with OEXCL if you really care. > 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)? Nope. This is Plan 9. Everyone has private /tmp directories. > 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? Probably not. The interesting part of sysfatal is the terminating the program, not the printing of the error. Russ