Alexandre Julliard : tools: Add a helper function to create temp files.
Alexandre Julliard
julliard at winehq.org
Wed Sep 29 15:54:09 CDT 2021
Module: wine
Branch: master
Commit: 55701c66728f6f2589e8fb5c72506aa8ba9ace66
URL: https://source.winehq.org/git/wine.git/?a=commit;h=55701c66728f6f2589e8fb5c72506aa8ba9ace66
Author: Alexandre Julliard <julliard at winehq.org>
Date: Wed Sep 29 09:32:01 2021 +0200
tools: Add a helper function to create temp files.
Signed-off-by: Alexandre Julliard <julliard at winehq.org>
---
tools/tools.h | 31 +++++++++++++++++++++++++++++++
tools/widl/parser.l | 10 ++--------
tools/widl/widl.c | 9 ++-------
tools/winebuild/utils.c | 24 ++++++------------------
tools/winegcc/winegcc.c | 15 ++-------------
tools/wrc/genres.c | 1 +
tools/wrc/newstruc.c | 1 +
tools/wrc/parser.l | 1 +
tools/wrc/parser.y | 1 +
tools/wrc/po.c | 1 +
tools/wrc/ppl.l | 1 +
tools/wrc/ppy.y | 1 +
tools/wrc/translation.c | 1 +
tools/wrc/utils.c | 1 +
tools/wrc/wpp.c | 1 +
tools/wrc/wpp_private.h | 1 -
tools/wrc/wrc.c | 8 ++------
tools/wrc/wrc.h | 1 -
18 files changed, 55 insertions(+), 54 deletions(-)
diff --git a/tools/tools.h b/tools/tools.h
index 7c0f4c20a42..09b4fa14b28 100644
--- a/tools/tools.h
+++ b/tools/tools.h
@@ -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 */
diff --git a/tools/widl/parser.l b/tools/widl/parser.l
index 72a7a432bbc..d01a9ece3bb 100644
--- a/tools/widl/parser.l
+++ b/tools/widl/parser.l
@@ -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);
diff --git a/tools/widl/widl.c b/tools/widl/widl.c
index 5b1ce6ca689..d168a01529a 100644
--- a/tools/widl/widl.c
+++ b/tools/widl/widl.c
@@ -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);
diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c
index de0d26ddba6..e375b696e59 100644
--- a/tools/winebuild/utils.c
+++ b/tools/winebuild/utils.c
@@ -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;
diff --git a/tools/winegcc/winegcc.c b/tools/winegcc/winegcc.c
index 2bf13570bba..0a6481ff0db 100644
--- a/tools/winegcc/winegcc.c
+++ b/tools/winegcc/winegcc.c
@@ -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
diff --git a/tools/wrc/genres.c b/tools/wrc/genres.c
index bf2d3b5b9bc..002d1a46bfb 100644
--- a/tools/wrc/genres.c
+++ b/tools/wrc/genres.c
@@ -34,6 +34,7 @@
#include <assert.h>
#include <ctype.h>
+#include "../tools.h"
#include "wrc.h"
#include "genres.h"
#include "utils.h"
diff --git a/tools/wrc/newstruc.c b/tools/wrc/newstruc.c
index e93545f04f6..1b6cd429609 100644
--- a/tools/wrc/newstruc.c
+++ b/tools/wrc/newstruc.c
@@ -27,6 +27,7 @@
#include <assert.h>
#include <ctype.h>
+#include "../tools.h"
#include "wrc.h"
#include "newstruc.h"
#include "utils.h"
diff --git a/tools/wrc/parser.l b/tools/wrc/parser.l
index 0c71396de73..c5d9db1488f 100644
--- a/tools/wrc/parser.l
+++ b/tools/wrc/parser.l
@@ -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"
diff --git a/tools/wrc/parser.y b/tools/wrc/parser.y
index bde02b736f8..7daa4581f23 100644
--- a/tools/wrc/parser.y
+++ b/tools/wrc/parser.y
@@ -130,6 +130,7 @@
#include <ctype.h>
#include <string.h>
+#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "newstruc.h"
diff --git a/tools/wrc/po.c b/tools/wrc/po.c
index 622d2f7a3ad..423d7142145 100644
--- a/tools/wrc/po.c
+++ b/tools/wrc/po.c
@@ -31,6 +31,7 @@
#include <gettext-po.h>
#endif
+#include "../tools.h"
#include "wrc.h"
#include "genres.h"
#include "newstruc.h"
diff --git a/tools/wrc/ppl.l b/tools/wrc/ppl.l
index bd29dd274a7..d4df76f7950 100644
--- a/tools/wrc/ppl.l
+++ b/tools/wrc/ppl.l
@@ -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"
diff --git a/tools/wrc/ppy.y b/tools/wrc/ppy.y
index 7dc75ac1965..50561cbe1aa 100644
--- a/tools/wrc/ppy.y
+++ b/tools/wrc/ppy.y
@@ -30,6 +30,7 @@
#include <ctype.h>
#include <string.h>
+#include "../tools.h"
#include "utils.h"
#include "wpp_private.h"
diff --git a/tools/wrc/translation.c b/tools/wrc/translation.c
index bdbcb4342bd..eb0047c10b5 100644
--- a/tools/wrc/translation.c
+++ b/tools/wrc/translation.c
@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <assert.h>
+#include "../tools.h"
#include "dumpres.h"
#include "utils.h"
#include "wrc.h"
diff --git a/tools/wrc/utils.c b/tools/wrc/utils.c
index b37463ee6bf..c335e9eaa10 100644
--- a/tools/wrc/utils.c
+++ b/tools/wrc/utils.c
@@ -28,6 +28,7 @@
#include <string.h>
#include <ctype.h>
+#include "../tools.h"
#include "wrc.h"
#include "utils.h"
#include "parser.h"
diff --git a/tools/wrc/wpp.c b/tools/wrc/wpp.c
index 200b21f8a2a..392ef189754 100644
--- a/tools/wrc/wpp.c
+++ b/tools/wrc/wpp.c
@@ -34,6 +34,7 @@
# include <unistd.h>
#endif
+#include "../tools.h"
#include "utils.h"
#include "wpp_private.h"
diff --git a/tools/wrc/wpp_private.h b/tools/wrc/wpp_private.h
index be118b020bd..435dbcc005a 100644
--- a/tools/wrc/wpp_private.h
+++ b/tools/wrc/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 );
diff --git a/tools/wrc/wrc.c b/tools/wrc/wrc.c
index 56c4c2f656b..1ffde86f0b7 100644
--- a/tools/wrc/wrc.c
+++ b/tools/wrc/wrc.c
@@ -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);
diff --git a/tools/wrc/wrc.h b/tools/wrc/wrc.h
index 4c572074e5e..d086c5c60f9 100644
--- a/tools/wrc/wrc.h
+++ b/tools/wrc/wrc.h
@@ -21,7 +21,6 @@
#ifndef __WRC_WRC_H
#define __WRC_WRC_H
-#include "../tools.h"
#include "wrctypes.h"
/* From wrc.c */
More information about the wine-cvs
mailing list