mkstemp() is a bit more complex than it looks; because ego wants to use the
authorDavid Given <dg@cowlark.com>
Sun, 6 Aug 2017 12:25:12 +0000 (14:25 +0200)
committerDavid Given <dg@cowlark.com>
Sun, 6 Aug 2017 12:25:12 +0000 (14:25 +0200)
same base name and generate multiple files based on it, we can't really use
mkstemp() for every temporary file. Instead, use mkstemp() once on a
placeholder, then generate temporary names based on this. (And delete the
placeholder once we've finished.)

util/ego/em_ego/em_ego.c

index 543f0b5..36d6456 100644 (file)
@@ -58,6 +58,7 @@ static const struct
 #define MAXARGS 1024 /* mar # of args */
 #define NTEMPS 4 /* # of temporary files; not tunable */
 
+static char tmpbase[] = TMP_DIR "/ego.XXXXXX";
 static char ddump[128] = TMP_DIR; /* data label dump file */
 static char pdump[128] = TMP_DIR; /* procedure name dump file */
 static char tmpbufs[NTEMPS * 2][128] = {
@@ -115,6 +116,8 @@ cleanup()
                if (pdump[0] != '\0')
                        (void)unlink(pdump);
        }
+
+       (void)unlink(tmpbase);
 }
 
 /*VARARGS1*/
@@ -200,9 +203,8 @@ new_outfiles()
        char** dst = &phargs[NTEMPS + 1];
 
        if (!Bindex)
-       {
-               Bindex = strrchr(tmpbufs[0], 'B') - tmpbufs[0];
-       }
+               Bindex = strlen(tmpbufs[0]) - 2;
+
        for (i = 1; i <= NTEMPS; i++)
        {
                *dst = tmpbufs[tmpindex];
@@ -403,25 +405,25 @@ int main(int argc, char* argv[])
                fatal("no correct -P flag given");
        }
 
+       close(mkstemp(tmpbase));
+       strcpy(ddump, tmpbase);
+       strcpy(pdump, tmpbase);
+       strcpy(tmpbufs[0], tmpbase);
+
        if (keeptemps)
        {
                (void)strcpy(ddump, ".");
                (void)strcpy(pdump, ".");
                (void)strcpy(tmpbufs[0], ".");
        }
-       (void)strcat(ddump, "/ego.dd.XXXXXX");
-       close(mkstemp(ddump));
-       (void)strcat(pdump, "/ego.pd.XXXXXX");
-       close(mkstemp(pdump));
-
-       (void)strcat(tmpbufs[0], "/ego.XXXXXX");
-       close(mkstemp(tmpbufs[0]));
-       (void)strcat(tmpbufs[0], ".A.BB");
-       for (i = 2 * NTEMPS - 1; i >= 1; i--)
-       {
+       (void)strcat(ddump, "dd");
+       (void)strcat(pdump, "pd");
+
+       (void)strcat(tmpbufs[0], "A.BB");
+       for (i=1; i<(2 * NTEMPS); i++)
                (void)strcpy(tmpbufs[i], tmpbufs[0]);
-       }
-       i = strrchr(tmpbufs[0], 'A') - tmpbufs[0];
+
+       i = strlen(tmpbufs[0]) - 4;
        tmpbufs[0][i] = 'p';
        tmpbufs[NTEMPS + 0][i] = 'p';
        tmpbufs[1][i] = 'd';
@@ -430,6 +432,7 @@ int main(int argc, char* argv[])
        tmpbufs[NTEMPS + 2][i] = 'l';
        tmpbufs[3][i] = 'b';
        tmpbufs[NTEMPS + 3][i] = 'b';
+
        run_phase(IC);
        run_phase(CF);
        while (*Ophase)