scratch(3f) - [M_io:QUERY] Return the name of a scratch file (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
function scratch(prefix) result(tname)
character(len=:),allocatable :: tname character(len=*),intent(in),optional :: prefix
Fortran supports non-retainable automatically deleted scratch files via OPEN(STATUS=SCRATCH,...). However, there are circumstances where a file with a unique name is required instead.
Specifying the pathname of a file can be required for performance reasons, file space limitations, or to support the ability for other processes or subprocesses to access the file.
By default SCRATCH(3f) Returns a unique filename for a scratch file in the directory pointed to by the most common environment variables used to designate a scratch directory.
The environment variables queried are $TMPDIR (the canonical environment variable in Unix and POSIX used to specify a temporary directory for scratch space) . If $TMPDIR is not set, $TEMP, $TEMPDIR, and $TMP are examined in that order. If nothing is set "/tmp/" is used.
prefix an optional prefix for the leaf of the filename. The prefix is used as-is if it contains the character "/" or "\". A suffix created by genuuid(3) is used to make the name unique. Otherwise, the prefix is prefixed by the first value that is not blank from the set {$TMPDIR, $TEMP, $TEMPDIR, $TMP, /tmp}. The default prefix is the basename of the program that called the procedure (the name trimmed of directories and anything from the right-most period in the name to the end of the name).
o If a prefix is not supplied: a filename whose basename is the current program followed by a UUID ( Universal Unique IDentifier) with the prefix ".scr" in the current temporary directory.
o if the prefix contains a slash or backslash: The same as if a prefix is not supplied accept the prefix is assumed to be a directory in which to create a file
o A prefix not containing a slash or backslash: The same as if a prefix is not supplied accept the basename begins with the given string instead of the current program name.
Sample:
program demo_scratch use M_io, only : scratch implicit none write(*,*)find good scratch file name candidates; one should test if writable call printit(JUNK:) call printit(./) call printit(/var/tmp/) call printit() call printit() contains subroutine printit(NAME) character(len=*),intent(in),optional :: NAME if(present(NAME))then write(*,(a,t20,a))NAME,scratch(NAME) else write(*,(a,t20,a))*OT PRESENT*,scratch() endif end subroutine printit end program demo_scratchResults:
> find good scratch file name candidates; one should test if writable > JUNK: /tmp/JUNK:405d766e-1320-4405-50e1-5d88fffbee9a.scr > ./ ./xx-901606b1-6ad2-4e96-6b17-e8bffedf2452.scr > /var/tmp/ /var/tmp/xx-3f5c55fa-17ca-4020-4a05-a9d9cfad8dbe.scr > /tmp/f10e0491-a2ff-4455-5ff6-55d7dfe7fa8c.scr > *NOT PRESENT* /tmp/xx-f4fed5f7-3694-4609-5af4-8902ffa75839.scr
John S. Urban
Public Domain
Nemo Release 3.1 | scratch (3) | February 23, 2025 |