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 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
......@@ -249,4 +251,33 @@ static inline int strarray_spawn( struct strarray args )
#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 */
......@@ -564,10 +564,7 @@ int do_import(char *fname)
input_name = path;
line_number = 1;
name = xstrdup( "widl.XXXXXX" );
if((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
fd = make_temp_file( "widl-pp", NULL, &name );
temp_name = name;
if (!(f = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name);
......@@ -605,10 +602,7 @@ static void switch_to_acf(void)
acf_name = NULL;
line_number = 1;
name = xstrdup( "widl.XXXXXX" );
if((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
fd = make_temp_file( "widl-acf", NULL, &name );
temp_name = name;
if (!(f = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name);
......
......@@ -967,14 +967,9 @@ int main(int argc,char *argv[])
{
FILE *output;
int fd;
char *name = xmalloc( strlen(header_name) + 8 );
strcpy( name, header_name );
strcat( name, ".XXXXXX" );
if ((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
char *name;
fd = make_temp_file( header_name, NULL, &name );
temp_name = name;
if (!(output = fdopen(fd, "wt")))
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 )
const char *ext, *basename;
int fd;
if (!prefix || !prefix[0]) prefix = "winebuild";
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)
if (prefix)
{
strcpy( name, "/tmp/" );
memcpy( name + 5, basename, ext - basename );
strcpy( name + 5 + (ext - basename), ".XXXXXX" );
strcat( name, suffix );
if ((fd = mkstemps( name, strlen(suffix) )) == -1)
fatal_error( "could not generate a temp file\n" );
if ((basename = strrchr( prefix, '/' ))) basename++;
else basename = prefix;
if ((ext = strchr( basename, '.' ))) prefix = strmake( "%.*s", ext - basename, basename );
else prefix = basename;
}
fd = make_temp_file( prefix, suffix, &name );
close( fd );
strarray_add( &tmp_files, name );
return name;
......
......@@ -297,25 +297,14 @@ static void exit_on_signal( int sig )
static char* get_temp_file(const char* prefix, const char* suffix)
{
int fd;
char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
char *tmp;
#ifdef HAVE_SIGPROCMASK
sigset_t old_set;
/* block signals while manipulating the temp files list */
sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
#endif
fd = mkstemps( tmp, strlen(suffix) );
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" );
}
fd = make_temp_file( prefix, suffix, &tmp );
close( fd );
strarray_add(&tmp_files, tmp);
#ifdef HAVE_SIGPROCMASK
......
......@@ -34,6 +34,7 @@
#include <assert.h>
#include <ctype.h>
#include "../tools.h"
#include "wrc.h"
#include "genres.h"
#include "utils.h"
......
......@@ -27,6 +27,7 @@
#include <assert.h>
#include <ctype.h>
#include "../tools.h"
#include "wrc.h"
#include "newstruc.h"
#include "utils.h"
......
......@@ -111,6 +111,7 @@ ws [ \f\t\r]
#define YY_NO_UNISTD_H
#endif
#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "parser.h"
......
......@@ -130,6 +130,7 @@
#include <ctype.h>
#include <string.h>
#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "newstruc.h"
......
......@@ -31,6 +31,7 @@
#include <gettext-po.h>
#endif
#include "../tools.h"
#include "wrc.h"
#include "genres.h"
#include "newstruc.h"
......
......@@ -173,6 +173,7 @@ ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
#define YY_NO_UNISTD_H
#endif
#include "../tools.h"
#include "utils.h"
#include "wpp_private.h"
#include "ppy.tab.h"
......
......@@ -30,6 +30,7 @@
#include <ctype.h>
#include <string.h>
#include "../tools.h"
#include "utils.h"
#include "wpp_private.h"
......
......@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <assert.h>
#include "../tools.h"
#include "dumpres.h"
#include "utils.h"
#include "wrc.h"
......
......@@ -28,6 +28,7 @@
#include <string.h>
#include <ctype.h>
#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "parser.h"
......
......@@ -34,6 +34,7 @@
# include <unistd.h>
#endif
#include "../tools.h"
#include "utils.h"
#include "wpp_private.h"
......
......@@ -22,7 +22,6 @@
#include <stdio.h>
#include <string.h>
#include "../tools.h"
#include "wine/list.h"
extern void wpp_del_define( const char *name );
......
......@@ -35,6 +35,7 @@
# include <getopt.h>
#endif
#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "dumpres.h"
......@@ -280,12 +281,7 @@ static int load_file( const char *input_name, const char *output_name )
exit(0);
}
if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
else name = xstrdup( "wrc.XXXXXX" );
if ((fd = mkstemps( name, 0 )) == -1)
error("Could not generate a temp name from %s\n", name);
fd = make_temp_file( output_name, "", &name );
temp_name = name;
if (!(output = fdopen(fd, "wt")))
error("Could not open fd %s for writing\n", name);
......
......@@ -21,7 +21,6 @@
#ifndef __WRC_WRC_H
#define __WRC_WRC_H
#include "../tools.h"
#include "wrctypes.h"
/* 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