printf("\nProcessing topology\n");
fpin = gmx_ffopen(topinout, "r");
std::strncpy(temporary_filename, "temp.topXXXXXX", STRLEN);
- gmx_tmpnam(temporary_filename);
- fpout = gmx_ffopen(temporary_filename, "w");
+ fpout = gmx_fopen_temporary(temporary_filename);
line = 0;
bMolecules = FALSE;
}
}
gmx_ffclose(fpout);
- /* use gmx_ffopen to generate backup of topinout */
- fpout = gmx_ffopen(topinout, "w");
- gmx_ffclose(fpout);
- rename(temporary_filename, topinout);
+ make_backup(topinout);
+ gmx_file_rename(temporary_filename, topinout);
}
int gmx_genion(int argc, char *argv[])
fprintf(stderr, "Processing topology\n");
fpin = gmx_ffopen(topinout, "r");
- gmx_tmpnam(temporary_filename);
- fpout = gmx_ffopen(temporary_filename, "w");
+ fpout = gmx_fopen_temporary(temporary_filename);
line = 0;
bSystem = bMolecules = FALSE;
while (fgets(buf, STRLEN, fpin))
fprintf(fpout, "%-15s %5d\n", "SOL", nsol);
}
gmx_ffclose(fpout);
- /* use gmx_ffopen to generate backup of topinout */
- fpout = gmx_ffopen(topinout, "w");
- gmx_ffclose(fpout);
- rename(temporary_filename, topinout);
+ make_backup(topinout);
+ gmx_file_rename(temporary_filename, topinout);
}
}
*/
#ifdef GMX_NATIVE_WINDOWS
_mktemp(buf);
+ if (buf == NULL)
+ {
+ gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
+ strerror(errno));
+ }
#else
int fd = mkstemp(buf);
if (fd < 0)
{
- gmx_fatal(FARGS, "Creating temporary file %s: %s", buf,
+ gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
strerror(errno));
}
close(fd);
+
+#endif
+
+ /* name in Buf should now be OK and file is CLOSED */
+
+ return;
+}
+
+FILE *gmx_fopen_temporary(char *buf)
+{
+ int i, len;
+ FILE *fpout = NULL;
+
+ if ((len = strlen(buf)) < 7)
+ {
+ gmx_fatal(FARGS, "Buf passed to gmx_fopentmp must be at least 7 bytes long");
+ }
+ for (i = len-6; (i < len); i++)
+ {
+ buf[i] = 'X';
+ }
+ /* mktemp is dangerous and we should use mkstemp instead, but
+ * since windows doesnt support it we have to separate the cases.
+ * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
+ */
+#ifdef GMX_NATIVE_WINDOWS
+ _mktemp(buf);
+ if (buf == NULL)
+ {
+ gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
+ strerror(errno));
+ }
+ if ((fpout = fopen(buf, "w")) == NULL)
+ {
+ gmx_fatal(FARGS, "Cannot open temporary file %s", buf);
+ }
+#else
+ int fd = mkstemp(buf);
+ if (fd < 0)
+ {
+ gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
+ strerror(errno));
+ }
+ if ((fpout = fdopen(fd, "w")) == NULL)
+ {
+ gmx_fatal(FARGS, "Cannot open temporary file %s", buf);
+ }
#endif
- /* name in Buf should now be OK */
+ /* name in Buf should now be OK and file is open */
+
+ return fpout;
}
int gmx_file_rename(const char *oldname, const char *newname)
FILE *low_libopen(const char *file, gmx_bool bFatal);
+/*! \brief
+ * Creates unique name for temp file (wrapper around mkstemp) and opens it.
+ *
+ * \p buf should be at least 7 bytes long
+ */
+FILE *gmx_fopen_temporary(char *buf);
+
/*! \brief
* Creates unique name for temp file (wrapper around mkstemp).
*
char tmp[32];
std::strcpy(tmp, "dialogXXXXXX");
- gmx_tmpnam(tmp);
+ tfil = gmx_fopen_temporary(tmp);
- if ((tfil = std::fopen(tmp, "w")) == NULL)
- {
- std::sprintf(tmp, "%ctmp%cdialogXXXXXX", DIR_SEPARATOR, DIR_SEPARATOR);
- gmx_tmpnam(tmp);
- }
- else
- {
- std::fclose(tfil);
- }
- if ((tfil = std::fopen(tmp, "w")) == NULL)
- {
- gmx_fatal(FARGS, "Can not open tmp file %s", tmp);
- }
-
- std::fprintf(tfil, "%s\n", script);
- std::fprintf(tfil, "sleep %d\n", nsleep);
- std::fclose(tfil);
+ fprintf(tfil, "%s\n", script);
+ fprintf(tfil, "sleep %d\n", nsleep);
+ gmx_ffclose(tfil);
std::sprintf(command, "xterm -title %s -e sh %s", title, tmp);
#ifdef DEBUG
ht = 1+(gmx->filter->grps->nr+1)*2+3;
}
std::strcpy(tmpfile, "filterXXXXXX");
- gmx_tmpnam(tmpfile);
+ tmp = gmx_fopen_temporary(tmpfile);
#ifdef DEBUG
std::fprintf(stderr, "file: %s\n", tmpfile);
#endif
- if ((tmp = std::fopen(tmpfile, "w")) == NULL)
- {
- std::sprintf(tmpfile, "%ctmp%cfilterXXXXXX", DIR_SEPARATOR, DIR_SEPARATOR);
- gmx_tmpnam(tmpfile);
- if ((tmp = std::fopen(tmpfile, "w")) == NULL)
- {
- gmx_fatal(FARGS, "Can not open tmp file %s", tmpfile);
- }
- }
tlen = 1+ncol*(1+len);
std::fprintf(tmp, "grid %d %d {\n\n", tlen, ht);
std::fprintf(tmp, "simple 1 %d %d 2 {\n", ht-3, tlen-2);
std::fprintf(tmp, "defbutton %s %s %s %s %s\n", ok, ok, dummy, dummy, dummy);
std::fprintf(tmp, "}\n\n}\n");
- std::fclose(tmp);
+ gmx_ffclose(tmp);
dlg = ReadDlg(x11, gmx->wd->self, title, tmpfile,
0, 0, true, false, FilterCB, gmx);