9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Joel “chesky” Salomon <JoelCSalomon@Gmail.com>
To: 9fans@cse.psu.edu
Subject: [9fans] non-truncating create
Date: Sun,  8 Oct 2006 23:13:07 -0400	[thread overview]
Message-ID: <f1dec573a0466a32a59881314c144e9f@plan9.jp> (raw)

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



             reply	other threads:[~2006-10-09  3:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-09  3:13 Joel “chesky” Salomon [this message]
2006-10-09  4:00 ` Russ Cox
2006-10-09  6:09   ` Joel “chesky” Salomon
2006-10-09 15:57     ` Russ Cox
2006-10-09  4:01 ` Felipe Bichued
2006-10-09  5:39   ` Joel Salomon
2006-10-09  3:13 Joel “chesky” Salomon
2006-10-09  3:13 Joel “chesky” Salomon
2006-10-09  6:46 Joel “chesky” Salomon
2006-10-09  7:30 ` Steve Simon
2006-10-09 22:38 G. David Butler
2006-10-09 23:36 ` Joel Salomon
2006-10-10  0:03 ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f1dec573a0466a32a59881314c144e9f@plan9.jp \
    --to=joelcsalomon@gmail.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).