dtsession/SmSave: get rid of use of tempnam (CID 89359)
authorJon Trulson <jon@radscan.com>
Sun, 28 Dec 2014 02:33:03 +0000 (19:33 -0700)
committerJon Trulson <jon@radscan.com>
Sun, 28 Dec 2014 02:33:03 +0000 (19:33 -0700)
cde/programs/dtsession/SmSave.c

index 3df9e15541e8a11de5cf08d95d5e8d1ac6907635..f86c291ee6372750f695683175b2733264501287 100644 (file)
@@ -504,29 +504,36 @@ PruneSessionDirectory ()
                 */ 
 
                char            * tmpName;
-               char            * tmp;
+                int len, tfd;
 
-               tmpName = (char *) XtMalloc (strlen (smGD.restoreSession) + 2);
-               sprintf (tmpName, "%s.", smGD.restoreSession);
-               if (strlen (tmpName) > 5) {
-                       tmpName[4] = '.';
-                       tmpName[5] = '\000';
-               }
-               tmp = (char *) tempnam (smGD.savePath, tmpName);
+                len = strlen(smGD.savePath) + strlen(smGD.restoreSession) 
+                  + strlen("XXXXXX") + 3;
+               tmpName = XtCalloc (1, len);
+               sprintf(tmpName, "%s/%s.XXXXXX", smGD.savePath, 
+                        smGD.restoreSession);
 
-               sprintf (saveDir, "%s/%s", smGD.savePath, smGD.restoreSession);
+                if ((tfd = mkstemp(tmpName)) == -1)
+                  {
+                    PrintErrnoError(DtError, smNLS.cantCreateDirsString);
+                  }
+                else
+                  {
+                    close(tfd);
+                    unlink(tmpName);
 
-               MoveDirectory (saveDir, tmp, False);
-               MoveDirectory (oldestDir, saveDir, False);
+                    sprintf (saveDir, "%s/%s", 
+                             smGD.savePath, smGD.restoreSession);
 
-               sprintf (clientDB, "%s/%s/%s", smGD.savePath, 
-                               smGD.restoreSession, SM_CLIENT_FILE2);
+                    MoveDirectory (saveDir, tmpName, False);
+                    MoveDirectory (oldestDir, saveDir, False);
 
-               ExecuteDiscardCommands (clientDB);
+                    sprintf (clientDB, "%s/%s/%s", smGD.savePath, 
+                             smGD.restoreSession, SM_CLIENT_FILE2);
 
-               MoveDirectory (tmp, saveDir, True);
+                    ExecuteDiscardCommands (clientDB);
 
-               free (tmp);
+                    MoveDirectory (tmpName, saveDir, True);
+                  }
                XtFree (tmpName);
        }