Fix use of hard-coded temporary filename
authorMark Abraham <mark.j.abraham@gmail.com>
Tue, 26 May 2015 00:53:42 +0000 (02:53 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Tue, 26 May 2015 12:11:22 +0000 (14:11 +0200)
Using temp.top can run into a user file of the same name, which is
unfriendly. Instead, use the function we have for doing the job of
making a temporary filename.

Fixes #1747

Change-Id: Ia6266e5605dd14032743e11f5dc68a520c47ce06

src/gromacs/gmxana/gmx_genion.c
src/gromacs/gmxpreprocess/solvate.cpp
src/programs/mdrun/membed.c

index 6abf410904ad2a55ba02072435b4cea8e27c86a6..6b03ea738b616fea6f89d93ebadff13593efc0f2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -238,15 +238,17 @@ void sort_ions(int nsa, int nw, int repl[], atom_id index[],
 static void update_topol(const char *topinout, int p_num, int n_num,
                          const char *p_name, const char *n_name, char *grpname)
 {
-#define TEMP_FILENM "temp.top"
     FILE    *fpin, *fpout;
     char     buf[STRLEN], buf2[STRLEN], *temp, **mol_line = NULL;
     int      line, i, nsol, nmol_line, sol_line, nsol_last;
     gmx_bool bMolecules;
+    char     temporary_filename[STRLEN];
 
     printf("\nProcessing topology\n");
     fpin  = gmx_ffopen(topinout, "r");
-    fpout = gmx_ffopen(TEMP_FILENM, "w");
+    strncpy(temporary_filename, "temp.topXXXXXX", STRLEN);
+    gmx_tmpnam(temporary_filename);
+    fpout = gmx_ffopen(temporary_filename, "w");
 
     line       = 0;
     bMolecules = FALSE;
@@ -342,8 +344,7 @@ static void update_topol(const char *topinout, int p_num, int n_num,
     /* use gmx_ffopen to generate backup of topinout */
     fpout = gmx_ffopen(topinout, "w");
     gmx_ffclose(fpout);
-    rename(TEMP_FILENM, topinout);
-#undef TEMP_FILENM
+    rename(temporary_filename, topinout);
 }
 
 int gmx_genion(int argc, char *argv[])
index 27623f165ebfa059dab3cd68761a0c8177b1dd9a..6cfe8ef86d82b7042104c52a2a09a7d7fc551326 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -485,7 +485,6 @@ static void add_solv(const char *fn, t_atoms *atoms, rvec **x, rvec **v, real **
 static void update_top(t_atoms *atoms, matrix box, int NFILE, t_filenm fnm[],
                        gmx_atomprop_t aps)
 {
-#define TEMP_FILENM "temp.top"
     FILE       *fpin, *fpout;
     char        buf[STRLEN], buf2[STRLEN], *temp;
     const char *topinout;
@@ -525,9 +524,13 @@ static void update_top(t_atoms *atoms, matrix box, int NFILE, t_filenm fnm[],
     topinout  = ftp2fn(efTOP, NFILE, fnm);
     if (ftp2bSet(efTOP, NFILE, fnm) )
     {
+        char temporary_filename[STRLEN];
+        strncpy(temporary_filename, "temp.topXXXXXX", STRLEN);
+
         fprintf(stderr, "Processing topology\n");
         fpin    = gmx_ffopen(topinout, "r");
-        fpout   = gmx_ffopen(TEMP_FILENM, "w");
+        gmx_tmpnam(temporary_filename);
+        fpout   = gmx_ffopen(temporary_filename, "w");
         line    = 0;
         bSystem = bMolecules = FALSE;
         while (fgets(buf, STRLEN, fpin))
@@ -607,9 +610,8 @@ static void update_top(t_atoms *atoms, matrix box, int NFILE, t_filenm fnm[],
         /* use gmx_ffopen to generate backup of topinout */
         fpout = gmx_ffopen(topinout, "w");
         gmx_ffclose(fpout);
-        rename(TEMP_FILENM, topinout);
+        rename(temporary_filename, topinout);
     }
-#undef TEMP_FILENM
 }
 
 int gmx_solvate(int argc, char *argv[])
index cf83ce57b58c02cf477638d3a33036295337b1a4..326de5f9696aeb1010389bd1543067e9c2f8fc7f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -890,14 +890,16 @@ int rm_bonded(t_block *ins_at, gmx_mtop_t *mtop)
 /* Write a topology where the number of molecules is correct for the system after embedding */
 static void top_update(const char *topfile, rm_t *rm_p, gmx_mtop_t *mtop)
 {
-#define TEMP_FILENM "temp.top"
-    int        bMolecules = 0;
+    int        bMolecules         = 0;
     FILE      *fpin, *fpout;
     char       buf[STRLEN], buf2[STRLEN], *temp;
     int        i, *nmol_rm, nmol, line;
+    char       temporary_filename[STRLEN];
 
     fpin  = gmx_ffopen(topfile, "r");
-    fpout = gmx_ffopen(TEMP_FILENM, "w");
+    strncpy(temporary_filename, "temp.topXXXXXX", STRLEN);
+    gmx_tmpnam(temporary_filename);
+    fpout = gmx_ffopen(temporary_filename, "w");
 
     snew(nmol_rm, mtop->nmoltype);
     for (i = 0; i < rm_p->nr; i++)
@@ -966,8 +968,7 @@ static void top_update(const char *topfile, rm_t *rm_p, gmx_mtop_t *mtop)
     /* use gmx_ffopen to generate backup of topinout */
     fpout = gmx_ffopen(topfile, "w");
     gmx_ffclose(fpout);
-    rename(TEMP_FILENM, topfile);
-#undef TEMP_FILENM
+    rename(temporary_filename, topfile);
 }
 
 void rescale_membed(int step_rel, gmx_membed_t membed, rvec *x)