]> Zhao Yanbai Git Server - minix.git/commitdiff
lib: don't give back the same temporary filenames even if removed.
authorBen Gras <ben@minix3.org>
Wed, 14 Jul 2010 15:18:50 +0000 (15:18 +0000)
committerBen Gras <ben@minix3.org>
Wed, 14 Jul 2010 15:18:50 +0000 (15:18 +0000)
workaround for what seems to be a clang/llvm bug/assumption.

lib/libc/stdio/mktemp.c

index 4cd712163137129ef06439c11dcc909a23760830..e63a47b30be84e90e38a116b3088c8899a38f55e 100644 (file)
@@ -71,6 +71,8 @@ _gettemp(path, doopen)
        register char *start, *trv;
        struct stat sbuf;
        u_int pid;
+       static int lastnames = 0;
+       int names = 0;
 
        pid = getpid();
        for (trv = path; *trv; ++trv);          /* extra X's get set to 0's */
@@ -100,16 +102,6 @@ _gettemp(path, doopen)
        }
 
        for (;;) {
-               if (doopen) {
-                       if ((*doopen =
-                           open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
-                               return(1);
-                       if (errno != EEXIST)
-                               return(0);
-               }
-               else if (stat(path, &sbuf))
-                       return(errno == ENOENT ? 1 : 0);
-
                /* tricky little algorithm for backward compatibility */
                for (trv = start;;) {
                        if (!*trv)
@@ -124,6 +116,27 @@ _gettemp(path, doopen)
                                break;
                        }
                }
+
+               names++;
+
+               if(names <= lastnames)
+                       continue;
+
+               lastnames = names;
+
+               if (doopen) {
+                       if ((*doopen =
+                           open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) {
+                               return(1);
+                           }
+                       if (errno != EEXIST) {
+                               return(0);
+                       }
+               }
+               else if (stat(path, &sbuf)) {
+                       return(errno == ENOENT ? 1 : 0);
+               }
+
        }
        /*NOTREACHED*/
 }