[Fortran, please advise] Choosing mkstemp over tempnam when opening SCRATCH files.

Toon Moene toon@moene.indiv.nluug.nl
Sun Nov 12 08:32:00 GMT 2000


L.S.,

About a month and half ago, we received the following bug report:

> For example below, on FreeBSD 4.1R, the linker points out that
> use of tempnam is not good.

> % cat a.f
>       program main 
>       end
> % f77 a.f
> /usr/lib/libg2c.so: warning: tempnam() possibly used unsafely;
> consider using mkstemp()

So I tried to persuade libf2c to use mkstemp instead of tempnam.
No problem, I thought:

*** libI77/configure.in.orig    Mon Nov 23 14:58:47 1998
--- libI77/configure.in Sun Nov 12 13:18:16 2000
*************** else
*** 130,133 ****
--- 130,134 ----
  fi
  
+ AC_CHECK_FUNCS(mkstemp)
  AC_CHECK_FUNCS(tempnam)
  
*** libI77/open.c.orig  Mon May 10 16:40:59 1999
--- libI77/open.c       Sun Nov 12 13:21:42 2000
*************** integer f_open(olist *a)
*** 138,141 ****
--- 138,142 ----
        int ufmt;
        FILE *tf;
+         int fd;
  #ifndef NON_UNIX_STDIO
        int n;
*************** integer f_open(olist *a)
*** 210,213 ****
--- 211,220 ----
         case 'S':
                b->uscrtch=1;
+ #ifdef HAVE_MKSTEMP
+               (void) strcpy(buf,"tmp.FXXXXXX");
+               fd = mkstemp(buf);
+               if (fd == -1)
+                 err (a->oerr, 132, "open");
+ #else
  #ifdef HAVE_TEMPNAM           /* Allow use of TMPDIR preferentially.
*/
                s = tempnam (0, buf);
*************** integer f_open(olist *a)
*** 224,227 ****
--- 231,235 ----
  #endif
  #endif /* ! defined (HAVE_TEMPNAM) */
+ #endif /* ! defined (HAVE_MKSTEMP) */
                goto replace;
        case 'n':

Although it works, this solution has the following drawbacks:

1. The temporary file is created in the current directory, instead of
   in $TMPDIR, which is the preferred solution for our clientele (we
   have had bug reports in the past about this - hence the remark in
   the comment above).

2. It's possible this solution leaks file descriptors.  From the
   mkstemp man page it's not clear whether I'm supposed to close the
   file (further down in libI77/open.c the file is opened using fopen).

3. Richard Henderson pointed me to mkstemp*s* in libiberty.  This
   seems to be a rip-off of a glibc routine.  So the question is:
   Which is more standard: mkstemp or mkstemps ? (obligatory AST quote
   elided).

Example code:

      PROGRAM TEMP
      CHARACTER*20 LINE
      OPEN(UNIT=3,STATUS='SCRATCH')
      WRITE(3,*)'HELLO, WORLD'
      PAUSE
      REWIND(3)
      PAUSE
      READ(3,'(a)')LINE
      CLOSE(3)
      PAUSE
      PRINT*,LINE
      END

Please advise.

<rant mode="rainy sunday afternoon">
Of course, if we would be using a real operating system, like VMS, this
whole discussion would be moot.  In VMS, a scratch file is opened by
specifying a TEMPORARY bit in the FAB (File Access Block).  This has the
following advantages over the Unix approach:

1. No race condition as the file system code is responsible for
   opening the file and no directory entry is needed.

2. The file is invisible (not merely unreadable) to other processes.

3. Automatic (from the user's point of view) deletion on image exit
   or close.
</rant>

-- 
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
GNU Fortran 95: http://g95.sourceforge.net/ (under construction)

"U nix en wij alles"


More information about the Gcc-patches mailing list