Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84 SMI; site sun.uucp
Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!hao!hplabs!pesnta!greipa!decwrl!sun!guy
From: guy@sun.uucp (Guy Harris)
Newsgroups: net.unix-wizards
Subject: Re: Semantics of test
Message-ID: <2973@sun.uucp>
Date: Sat, 9-Nov-85 04:38:54 EST
Article-I.D.: sun.2973
Posted: Sat Nov  9 04:38:54 1985
Date-Received: Tue, 12-Nov-85 05:12:24 EST
References: <874@mcvax.UUCP>
Organization: Sun Microsystems, Inc.
Lines: 53

> Odd as it seems at first hand, it can be interpret as correct. Even if you
> own the directory, you cannot copy an arbitrary file "on top of it". (Your
> file system would be seriously be corrupted). Let us call this the purist
> view.

Let's not, and say we did.  Sun's "/bin/test" uses "access", rather than
"open", to test writability, so it will say that a directory is writable iff
you have the appropriate write permission bit set.  If "-w" tries to open
for writing, as it does in V7 and 4.xBSD, it is impossible to use "test" to
see if you could create a file in a directory, move a file to that
directory, or remove a file from that directory.  If, however, it is just a
shell-level front end for "access" (which is *my* definition of a "purist"
view"), you can do that very test.  If you want to test whether you can open
something for writing, try using the shell's front end for opening for
writing, namely ">>".  Try

	if [ -f "$file" ] && (>> "$file") 2>/dev/null
	then
		echo writable
	else
		echo not writable
	fi

The "2>/dev/null" is to suppress complaints if it can't open it for
writing; ">>" is used instead of ">" for obvious reasons.  This works fine
in S3/S5 systems, where "-f" returns "true" if it exists *and* it isn't a
directory; it's not perfect in V7/4.x systems, where it returns true if it
exists and it *is* a plain file (you can try throwing in tests for character
special and block special in these cases, which tests will never be
performed on an S3/S5 system because if it's character or block special the
"-f" test succeeds).

> Another interpretation is that if you can create a file in the directory,
> the directory can be considered writable. Let us call this the sloppy view.

Again, let's not.  This interpretation can be useful, as mentioned above;
it's hardly "sloppy", considering either kind of test is a straightforward
interface to a particular system call ("open(, )" or
"access(, )").

> These different interpretations of test -w give a problem in porting shell
> programs.
> 
> Question: What to do. Change "test", so it will be sloppy or should the
> shells with build in "test" be reformed to take the purist view.

Change "test" so it will be *useful*, not "sloppy"; shells with built-in
"test" can already test whether something can be opened.  Changing the shel
isn't a "reform", since it renders it inconvenient to test whether something
is a writable directory (you can do it, but you have to write your own
program).

	Guy Harris