diff --git a/src/main.c b/src/main.c index f7ac6ad5c..80a064030 100644 --- a/src/main.c +++ b/src/main.c @@ -45,7 +45,11 @@ #include "misc/misc_time.h" #if defined(HAS_WINDOWS) || defined(_MSC_VER) || defined(__MINGW32__) +# include # 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