This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: GNU GCC's f77 [tempnam -> mkstemp; proposed patch]


Zack Weinberg wrote:

> On Mon, Nov 13, 2000 at 10:00:19PM +0100, Toon Moene wrote:

> > Do you know why the FreeBSD developers consider tempnam a problem ?
> >
> > Is it a generic problem with tempnam's specification, or is the specific
> > implementation on FreeBSD problematic ?
> 
> It is a generic problem with tempnam's specification.  tempnam does
> not create a scratch file, it just finds a name which is not in use.

Thanks; that explains the need for the warning message sufficiently.

I reworked your example code a little to force it into the framework of
the libf2c/libI77 open.c code.  It works in the sense that puts the
scratch file in the correct directory and removes it at CLOSE time.  I
didn't test the race condition itself.

If noone shoots a hole in this update before tomorrow 18 UTC, I'll apply
the patch:

*** configure.in.orig   Mon Nov 13 18:55:06 2000
--- configure.in        Tue Nov 14 20:49:05 2000
*************** else
*** 130,133 ****
--- 130,134 ----
  fi
  
+ AC_CHECK_FUNCS(mkstemp)
  AC_CHECK_FUNCS(tempnam)
  
*** open.c.orig Mon Nov 13 18:55:09 2000
--- open.c      Tue Nov 14 22:25:54 2000
*************** integer f_open(olist *a)
*** 134,141 ****
  {     unit *b;
        integer rv;
!       char buf[256], *s;
        cllist x;
        int ufmt;
        FILE *tf;
  #ifndef NON_UNIX_STDIO
        int n;
--- 134,142 ----
  {     unit *b;
        integer rv;
!       char buf[256], *s, *env;
        cllist x;
        int ufmt;
        FILE *tf;
+       int fd, len;
  #ifndef NON_UNIX_STDIO
        int n;
*************** integer f_open(olist *a)
*** 210,213 ****
--- 211,227 ----
         case 'S':
                b->uscrtch=1;
+ #ifdef HAVE_MKSTEMP             /* Allow use of TMPDIR preferentially.
*/
+               env = getenv("TMPDIR");
+               if (!env) env = getenv("TEMP");
+               if (!env) env = "/tmp";
+               len = strlen(env);
+               if (len > 256 - sizeof "/tmp.FXXXXXX")
+                 err (a->oerr, 132, "open");
+               strcpy(buf, env);
+               strcat(buf, "/tmp.FXXXXXX");
+               fd = mkstemp(buf);
+               if (fd == -1 || close(fd))
+                 err (a->oerr, 132, "open");
+ #else /* ! defined (HAVE_MKSTEMP) */
  #ifdef HAVE_TEMPNAM           /* Allow use of TMPDIR preferentially.
*/
                s = tempnam (0, buf);
*************** integer f_open(olist *a)
*** 224,227 ****
--- 238,242 ----
  #endif
  #endif /* ! defined (HAVE_TEMPNAM) */
+ #endif /* ! defined (HAVE_MKSTEMP) */
                goto replace;
        case 'n':

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

[ Bootstrapped and tested on i686-pc-linux-gnu ]

-- 
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)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]