From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <88ee35789aeb1fdcfb4491b2b83a95b4@plan9.jp> To: 9fans@cse.psu.edu From: Joel “chesky” Salomon Date: Sun, 8 Oct 2006 23:13:28 -0400 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Subject: [9fans] non-truncating create Content-Transfer-Encoding: quoted-printable Topicbox-Message-UUID: c729bb24-ead1-11e9-9d60-3106f5b1d025 Still for the Brain-Dead SHell homework assignment... I=E2=80=99m 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 =3D 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=E2=80=99m thinking about t= he following code snippet: o =3D open(oname, OWRITE) rerrstr(errbuf, ERRMAX); if((strstr(errbuf, "file does not exist") =3D=3D 0){ complain("cannot open %s: %r", oname); return; }else o =3D create(oname, OWRITE, ~0); if(o < 0) ... Is there a cleaner way to accomplish this? Some form of stat(2) that has an explicit =E2=80=9Cfile does not exist=E2=80=9D code outside parsin= g 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