Next: , Previous: , Up: File Names   [Contents][Index]


35.8.5 Generating Unique File Names

Some programs need to write temporary files. Here is the usual way to construct a name for such a file:

(make-temp-name (expand-file-name name-of-application (temp-directory)))

Here we use (temp-directory) to specify a directory for temporary files—under Unix, it will normally evaluate to "/tmp/". The job of make-temp-name is to prevent two different users or two different processes from trying to use the same name.

Function: temp-directory

This function returns the name of the directory to use for temporary files. Under Unix, this will be the value of TMPDIR, defaulting to /tmp.

Note: The temp-directory function does not exist under FSF Emacs.

Function: make-temp-name prefix

This function generates a temporary file name starting with prefix. The Emacs process number forms part of the result, so there is no danger of generating a name being used by another process.

(make-temp-name "/tmp/foo")
     ⇒ "/tmp/fooGaAQjC"

In addition, this function makes an attempt to choose a name that does not specify an existing file. To make this work, prefix should be an absolute file name.

To avoid confusion, each Lisp application should preferably use a unique prefix to make-temp-name.