From 4b1e5a1868b49cea906b9822df0f21c132088573 Mon Sep 17 00:00:00 2001 From: Mark Abraham Date: Tue, 26 May 2015 02:53:42 +0200 Subject: [PATCH] Fix use of hard-coded temporary filename 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 | 11 ++++++----- src/gromacs/gmxpreprocess/solvate.cpp | 12 +++++++----- src/programs/mdrun/membed.c | 13 +++++++------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/gromacs/gmxana/gmx_genion.c b/src/gromacs/gmxana/gmx_genion.c index 6abf410904..6b03ea738b 100644 --- a/src/gromacs/gmxana/gmx_genion.c +++ b/src/gromacs/gmxana/gmx_genion.c @@ -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[]) diff --git a/src/gromacs/gmxpreprocess/solvate.cpp b/src/gromacs/gmxpreprocess/solvate.cpp index 27623f165e..6cfe8ef86d 100644 --- a/src/gromacs/gmxpreprocess/solvate.cpp +++ b/src/gromacs/gmxpreprocess/solvate.cpp @@ -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[]) diff --git a/src/programs/mdrun/membed.c b/src/programs/mdrun/membed.c index cf83ce57b5..326de5f969 100644 --- a/src/programs/mdrun/membed.c +++ b/src/programs/mdrun/membed.c @@ -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) -- 2.11.4.GIT