Commit 55701c66 authored by Alexandre Julliard's avatar Alexandre Julliard

tools: Add a helper function to create temp files.

parent 1f0bc618
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h> #include <errno.h>
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
...@@ -249,4 +251,33 @@ static inline int strarray_spawn( struct strarray args ) ...@@ -249,4 +251,33 @@ static inline int strarray_spawn( struct strarray args )
#endif #endif
} }
static inline int make_temp_file( const char *prefix, const char *suffix, char **name )
{
static unsigned int value;
int fd, count;
const char *tmpdir = NULL;
if (!prefix) prefix = "tmp";
if (!suffix) suffix = "";
value += time(NULL) + getpid();
for (count = 0; count < 0x8000; count++)
{
if (tmpdir)
*name = strmake( "%s/%s-%08x%s", tmpdir, prefix, value, suffix );
else
*name = strmake( "%s-%08x%s", prefix, value, suffix );
fd = open( *name, O_RDWR | O_CREAT | O_EXCL, 0600 );
if (fd >= 0) return fd;
value += 7777;
if (errno == EACCES && !tmpdir && !strchr( prefix, '/' ))
{
if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
}
free( *name );
}
fprintf( stderr, "failed to create temp file for %s%s\n", prefix, suffix );
exit(1);
}
#endif /* __WINE_TOOLS_H */ #endif /* __WINE_TOOLS_H */
...@@ -564,10 +564,7 @@ int do_import(char *fname) ...@@ -564,10 +564,7 @@ int do_import(char *fname)
input_name = path; input_name = path;
line_number = 1; line_number = 1;
name = xstrdup( "widl.XXXXXX" ); fd = make_temp_file( "widl-pp", NULL, &name );
if((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
temp_name = name; temp_name = name;
if (!(f = fdopen(fd, "wt"))) if (!(f = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name); error("Could not open fd %s for writing\n", name);
...@@ -605,10 +602,7 @@ static void switch_to_acf(void) ...@@ -605,10 +602,7 @@ static void switch_to_acf(void)
acf_name = NULL; acf_name = NULL;
line_number = 1; line_number = 1;
name = xstrdup( "widl.XXXXXX" ); fd = make_temp_file( "widl-acf", NULL, &name );
if((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
temp_name = name; temp_name = name;
if (!(f = fdopen(fd, "wt"))) if (!(f = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name); error("Could not open fd %s for writing\n", name);
......
...@@ -967,14 +967,9 @@ int main(int argc,char *argv[]) ...@@ -967,14 +967,9 @@ int main(int argc,char *argv[])
{ {
FILE *output; FILE *output;
int fd; int fd;
char *name = xmalloc( strlen(header_name) + 8 ); char *name;
strcpy( name, header_name );
strcat( name, ".XXXXXX" );
if ((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
fd = make_temp_file( header_name, NULL, &name );
temp_name = name; temp_name = name;
if (!(output = fdopen(fd, "wt"))) if (!(output = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name); error("Could not open fd %s for writing\n", name);
......
...@@ -441,26 +441,14 @@ char *get_temp_file_name( const char *prefix, const char *suffix ) ...@@ -441,26 +441,14 @@ char *get_temp_file_name( const char *prefix, const char *suffix )
const char *ext, *basename; const char *ext, *basename;
int fd; int fd;
if (!prefix || !prefix[0]) prefix = "winebuild"; if (prefix)
if (!suffix) suffix = "";
if ((basename = strrchr( prefix, '/' ))) basename++;
else basename = prefix;
if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
memcpy( name, prefix, ext - prefix );
strcpy( name + (ext - prefix), ".XXXXXX" );
strcat( name, suffix );
if ((fd = mkstemps( name, strlen(suffix) )) == -1)
{ {
strcpy( name, "/tmp/" ); if ((basename = strrchr( prefix, '/' ))) basename++;
memcpy( name + 5, basename, ext - basename ); else basename = prefix;
strcpy( name + 5 + (ext - basename), ".XXXXXX" ); if ((ext = strchr( basename, '.' ))) prefix = strmake( "%.*s", ext - basename, basename );
strcat( name, suffix ); else prefix = basename;
if ((fd = mkstemps( name, strlen(suffix) )) == -1)
fatal_error( "could not generate a temp file\n" );
} }
fd = make_temp_file( prefix, suffix, &name );
close( fd ); close( fd );
strarray_add( &tmp_files, name ); strarray_add( &tmp_files, name );
return name; return name;
......
...@@ -297,25 +297,14 @@ static void exit_on_signal( int sig ) ...@@ -297,25 +297,14 @@ static void exit_on_signal( int sig )
static char* get_temp_file(const char* prefix, const char* suffix) static char* get_temp_file(const char* prefix, const char* suffix)
{ {
int fd; int fd;
char* tmp = strmake("%s-XXXXXX%s", prefix, suffix); char *tmp;
#ifdef HAVE_SIGPROCMASK #ifdef HAVE_SIGPROCMASK
sigset_t old_set; sigset_t old_set;
/* block signals while manipulating the temp files list */ /* block signals while manipulating the temp files list */
sigprocmask( SIG_BLOCK, &signal_mask, &old_set ); sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
#endif #endif
fd = mkstemps( tmp, strlen(suffix) ); fd = make_temp_file( prefix, suffix, &tmp );
if (fd == -1)
{
/* could not create it in current directory, try in TMPDIR */
const char* tmpdir;
free(tmp);
if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
tmp = strmake("%s/%s-XXXXXX%s", tmpdir, prefix, suffix);
fd = mkstemps( tmp, strlen(suffix) );
if (fd == -1) error( "could not create temp file\n" );
}
close( fd ); close( fd );
strarray_add(&tmp_files, tmp); strarray_add(&tmp_files, tmp);
#ifdef HAVE_SIGPROCMASK #ifdef HAVE_SIGPROCMASK
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "genres.h" #include "genres.h"
#include "utils.h" #include "utils.h"
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "newstruc.h" #include "newstruc.h"
#include "utils.h" #include "utils.h"
......
...@@ -111,6 +111,7 @@ ws [ \f\t\r] ...@@ -111,6 +111,7 @@ ws [ \f\t\r]
#define YY_NO_UNISTD_H #define YY_NO_UNISTD_H
#endif #endif
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "utils.h" #include "utils.h"
#include "parser.h" #include "parser.h"
......
...@@ -130,6 +130,7 @@ ...@@ -130,6 +130,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "utils.h" #include "utils.h"
#include "newstruc.h" #include "newstruc.h"
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <gettext-po.h> #include <gettext-po.h>
#endif #endif
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "genres.h" #include "genres.h"
#include "newstruc.h" #include "newstruc.h"
......
...@@ -173,6 +173,7 @@ ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL] ...@@ -173,6 +173,7 @@ ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
#define YY_NO_UNISTD_H #define YY_NO_UNISTD_H
#endif #endif
#include "../tools.h"
#include "utils.h" #include "utils.h"
#include "wpp_private.h" #include "wpp_private.h"
#include "ppy.tab.h" #include "ppy.tab.h"
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "../tools.h"
#include "utils.h" #include "utils.h"
#include "wpp_private.h" #include "wpp_private.h"
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "../tools.h"
#include "dumpres.h" #include "dumpres.h"
#include "utils.h" #include "utils.h"
#include "wrc.h" #include "wrc.h"
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "utils.h" #include "utils.h"
#include "parser.h" #include "parser.h"
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
# include <unistd.h> # include <unistd.h>
#endif #endif
#include "../tools.h"
#include "utils.h" #include "utils.h"
#include "wpp_private.h" #include "wpp_private.h"
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../tools.h"
#include "wine/list.h" #include "wine/list.h"
extern void wpp_del_define( const char *name ); extern void wpp_del_define( const char *name );
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
# include <getopt.h> # include <getopt.h>
#endif #endif
#include "../tools.h"
#include "wrc.h" #include "wrc.h"
#include "utils.h" #include "utils.h"
#include "dumpres.h" #include "dumpres.h"
...@@ -280,12 +281,7 @@ static int load_file( const char *input_name, const char *output_name ) ...@@ -280,12 +281,7 @@ static int load_file( const char *input_name, const char *output_name )
exit(0); exit(0);
} }
if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name ); fd = make_temp_file( output_name, "", &name );
else name = xstrdup( "wrc.XXXXXX" );
if ((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
temp_name = name; temp_name = name;
if (!(output = fdopen(fd, "wt"))) if (!(output = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name); error("Could not open fd %s for writing\n", name);
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#ifndef __WRC_WRC_H #ifndef __WRC_WRC_H
#define __WRC_WRC_H #define __WRC_WRC_H
#include "../tools.h"
#include "wrctypes.h" #include "wrctypes.h"
/* From wrc.c */ /* From wrc.c */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment