Browse Source

tmpfile() on MS Windows

create temporary files with Microsoft specific fopen() flags "TD"
and (propably redundant) remove them with an atexit() handler

see bug report #3535348
  "sp*.tmp file created, but not deleted when Windows UAC is en"
  https://sourceforge.net/tracker/?func=detail&atid=423915&aid=3535348&group_id=38962
h_vogt 14 years ago
parent
commit
cc44d34f70
  1. 42
      src/main.c

42
src/main.c

@ -45,7 +45,11 @@
#include "misc/misc_time.h"
#if defined(HAS_WINDOWS) || defined(_MSC_VER) || defined(__MINGW32__)
# include <stdio.h>
# include "misc/mktemp.h"
static char *tpf; /* temporary file by name */
static FILE *tempfile; /* temporary file */
static void delete_tpf(void);
#endif
#if defined(HAVE_GETOPT_LONG) && defined(HAVE_GETOPT_H)
@ -1167,23 +1171,28 @@ main(int argc, char **argv)
current algorithm is uniform at the expense of a little
startup time. */
FILE *tempfile = tmpfile();
#if defined(HAS_WINDOWS) || defined(_MSC_VER) || defined(__MINGW32__)
char *tpf = NULL; /* temporary file */
char *dname = NULL; /* input file*/
tpf = NULL; /* temporary file */
tempfile = tmpfile();
/* tmpfile() returns NULL, if in MS Windows as non admin user
then we add a tempfile in the local directory */
in directory C:\something (no write permission to root C:).
Then we add a tempfile in the local directory.
File will be removed again at exit() using atexit() */
if (tempfile == NULL) {
atexit(delete_tpf);
tpf = smktemp("sp");
tempfile = fopen(tpf, "w+b");
tempfile = fopen(tpf, "w+bTD");
if (tempfile == NULL) {
fprintf(stderr, "Could not open a temporary file to save and use optional arguments.");
sp_shutdown(EXIT_BAD);
}
}
#else
FILE *tempfile = tmpfile();
#endif
if(!tempfile) {
@ -1245,11 +1254,6 @@ main(int argc, char **argv)
gotone = TRUE;
}
#if defined(HAS_WINDOWS) || defined(_MSC_VER) || defined(__MINGW32__)
if (tpf && (unlink(tpf)==-1))
perror("Could not delete temp file");
#endif
if (ft_batchmode && err)
sp_shutdown(EXIT_BAD);
@ -1345,3 +1349,23 @@ main(int argc, char **argv)
sp_shutdown(EXIT_NORMAL);
return 0;
}
#if defined(HAS_WINDOWS) || defined(_MSC_VER) || defined(__MINGW32__)
/* For using with atexit */
void delete_tpf(void)
{
if(tempfile) {
fclose(tempfile);
tempfile = NULL;
}
if (tpf) {
if (unlink(tpf) == -1)
perror("Could not delete temp file\n");
tpf = NULL;
}
}
#endif
Loading…
Cancel
Save