Subject: Re: touch(1) enchancement, mkstemp(1)
To: J.T. Conklin <jtc@NetBSD.ORG>
From: Darren Reed <>
List: current-users
Date: 02/11/1998 17:22:16
In some mail I received from J.T. Conklin, sie wrote
> Darren Reed <> writes:
> > I'd like to propose an extension to touch(1) that returns information
> > about whether the file being touch'd actually existed prior to touch
> > being executed.  Something like an option causing it to return -1
> > if EEXIST when opening with O_EXCL|O_CREAT.
> What would this give you that ``test -e foo'' does not?  I'd hate to
> have us start adding various new knobs to shell utilities --- people
> might use them.  I can allready hear the whining ``but it works on 
> NetBSD...''

if [ ! -e foo ] ; then
	touch foo

- race condition between test and touch running.  I'm not even sure
that the change to touch is needed, if mktemp(1) was imported from
OpenBSD.  What I was thinking of fo touch would be like:

touch -E foo

if foo already exited, touch would return non-zero, otherwise it would
create it and return 0.  No race condition.

> > I'd also like to propose that a command line interface to mkstemp(3)
> > be implemented.  It would work something like this:
> > 
> > prog=`basename $0`
> > tmpfile=`mkstemp /tmp/${prog}.XXXXXX`
> > 
> > where tmpfile is the resulting filename (i.e. X's are changed to
> > something else).  mkstemp(1) would return 0 on success and 1 if
> > it failed to create a file.
> For temporary files in shell scripts, does it matter what name is
> given to the file?  Perhaps there is a way to create a unlinked 
> temp file and refer to it by file descriptor.
> Perhaps something like:
> 	exec tmpfile 4<>
> 	echo "foo" 1>&4 
> 	echo "bar" 1>&4
> 	cat 0<&4
> Where tmpfile creates a file with file descriptor 4 and then unlinks
> the file (I haven't tried this myself, but I have used bits and pieces
> like this in shell scripts in the past.

To me, that's "complex" shell script programming, plus you still need
some way to generate tmpfile and have that way generally accessible
from a shell script.

Then what about occasions when I want shell scripts to leave the tmpfile
around so I can look at it or pass it to other programs, etc.

btw, 'exec tmpfile 4<>' gives me:
Syntax error: redirection unexpected