From 786d24903b1a397cf2e4b549d8a0b3fa34bc9589 Mon Sep 17 00:00:00 2001 From: Marcus Meissner Date: Mon, 29 Jul 2002 23:55:39 +0000 Subject: [PATCH] Check for mkstemp, added a port implementation if it is not present. Use mkstemp() in various places needing tmp files. --- configure | 2 ++ configure.ac | 1 + debugger/gdbproxy.c | 6 +++++- dlls/shell32/shelllink.c | 12 ++++++++++-- include/config.h.in | 3 +++ include/wine/port.h | 4 ++++ library/port.c | 29 +++++++++++++++++++++++++++++ server/file.c | 15 ++++----------- tools/wpp/wpp.c | 17 +++++++++++------ 9 files changed, 69 insertions(+), 20 deletions(-) diff --git a/configure b/configure index b96b12e6ba8..eb19ffc543f 100755 --- a/configure +++ b/configure @@ -11197,6 +11197,7 @@ fi + for ac_func in \ @@ -11226,6 +11227,7 @@ for ac_func in \ lseek64 \ lstat \ memmove \ + mkstemp \ mmap \ pclose \ popen \ diff --git a/configure.ac b/configure.ac index 8c2a857bf1f..f0617d7deb6 100644 --- a/configure.ac +++ b/configure.ac @@ -879,6 +879,7 @@ AC_CHECK_FUNCS(\ lseek64 \ lstat \ memmove \ + mkstemp \ mmap \ pclose \ popen \ diff --git a/debugger/gdbproxy.c b/debugger/gdbproxy.c index 860d4ef4b0d..a78702f66bd 100644 --- a/debugger/gdbproxy.c +++ b/debugger/gdbproxy.c @@ -1812,11 +1812,15 @@ static BOOL gdb_startup(struct gdb_context* gdbctx, DEBUG_EVENT* de, unsigned fl case 0: /* in child... and alive */ { char buf[MAX_PATH]; + int fd; char* gdb_path; FILE* f; if (!(gdb_path = getenv("WINE_GDB"))) gdb_path = "gdb"; - if (!tmpnam(buf) || (f = fopen(buf, "w+")) == NULL) return FALSE; + strcpy(buf,"/tmp/winegdb.XXXXXX"); + fd = mkstemp(buf); + if (fd == -1) return FALSE; + if ((f = fdopen(fd, "w+")) == NULL) return FALSE; fprintf(f, "file %s\n", wine_path); fprintf(f, "target remote localhost:%d\n", ntohs(s_addr.sin_port)); fprintf(f, "monitor trace=0\n"); diff --git a/dlls/shell32/shelllink.c b/dlls/shell32/shelllink.c index 32a40e621d6..03210933855 100644 --- a/dlls/shell32/shelllink.c +++ b/dlls/shell32/shelllink.c @@ -28,6 +28,7 @@ # include #endif #include "wine/debug.h" +#include "wine/port.h" #include "winerror.h" #include "winbase.h" #include "winnls.h" @@ -551,8 +552,15 @@ static BOOL create_default_icon( const char *filename ) /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */ static char *extract_icon( const char *path, int index) { - int nodefault = 1; - char *filename = heap_strdup( tmpnam(NULL) ); + int fd, nodefault = 1; + char *filename, tmpfn[25]; + + strcpy(tmpfn,"/tmp/icon.XXXXXX"); + fd = mkstemp( tmpfn ); + if (fd == -1) + return NULL; + filename = heap_strdup( tmpfn ); + close(fd); /* not needed */ /* If icon path begins with a '*' then this is a deferred call */ if (path[0] == '*') diff --git a/include/config.h.in b/include/config.h.in index 38b78c58c66..1a3e26b3770 100644 --- a/include/config.h.in +++ b/include/config.h.in @@ -290,6 +290,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `mkstemp' function. */ +#undef HAVE_MKSTEMP + /* Define to 1 if you have the `mmap' function. */ #undef HAVE_MMAP diff --git a/include/wine/port.h b/include/wine/port.h index 3d95259d357..ca4728b00d1 100644 --- a/include/wine/port.h +++ b/include/wine/port.h @@ -222,6 +222,10 @@ unsigned long inet_network(const char *cp); int lstat(const char *file_name, struct stat *buf); #endif /* HAVE_LSTAT */ +#ifndef HAVE_MKSTEMP +int mkstemp(char *tmpfn); +#endif /* HAVE_MKSTEMP */ + #ifndef HAVE_MEMMOVE void *memmove(void *dest, const void *src, unsigned int len); #endif /* !defined(HAVE_MEMMOVE) */ diff --git a/library/port.c b/library/port.c index def94694d34..eb93423efea 100644 --- a/library/port.c +++ b/library/port.c @@ -340,6 +340,35 @@ int lstat(const char *file_name, struct stat *buf) } #endif /* HAVE_LSTAT */ +/*********************************************************************** + * mkstemp + */ +#ifndef HAVE_MKSTEMP +int mkstemp(char *tmpfn) +{ + int tries; + char *xstart; + + xstart = tmpfn+strlen(tmpfn)-1; + while ((xstart > tmpfn) && (*xstart == 'X')) + xstart--; + tries = 10; + while (tries--) { + char *newfn = mktemp(tmpfn); + int fd; + if (!newfn) /* something else broke horribly */ + return -1; + fd = open(newfn,O_CREAT|O_RDWR|O_EXCL,0600); + if (fd!=-1) + return fd; + newfn = xstart; + /* fill up with X and try again ... */ + while (*newfn) *newfn++ = 'X'; + } + return -1; +} +#endif /* HAVE_MKSTEMP */ + /*********************************************************************** * pread diff --git a/server/file.c b/server/file.c index d8da51724d3..b302fe67f1b 100644 --- a/server/file.c +++ b/server/file.c @@ -230,24 +230,17 @@ int get_file_drive_type( struct file *file ) /* Create an anonymous Unix file */ int create_anonymous_file(void) { - char *name; + char tmpfn[21]; int fd; - do - { - if (!(name = tmpnam(NULL))) - { - set_error( STATUS_TOO_MANY_OPENED_FILES ); - return -1; - } - fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 ); - } while ((fd == -1) && (errno == EEXIST)); + sprintf(tmpfn,"/tmp/anonmap.XXXXXX"); + fd = mkstemp(tmpfn); if (fd == -1) { file_set_error(); return -1; } - unlink( name ); + unlink( tmpfn ); return fd; } diff --git a/tools/wpp/wpp.c b/tools/wpp/wpp.c index 7ceee3f3463..6d9ef5a678d 100644 --- a/tools/wpp/wpp.c +++ b/tools/wpp/wpp.c @@ -19,6 +19,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "config.h" +#include "wine/port.h" + #include #include @@ -109,20 +112,22 @@ int wpp_parse( const char *input, FILE *output ) /* parse into a temporary file */ int wpp_parse_temp( const char *input, char **output_name ) { - char *temp_name; FILE *output; - int ret; + int ret, fd; + char tmpfn[20], *temp_name; - if(!(temp_name = tmpnam(NULL))) + strcpy(tmpfn,"/tmp/wpp.XXXXXX"); + + if((fd = mkstemp(tmpfn)) == -1) { fprintf(stderr, "Could not generate a temp-name\n"); exit(2); } - temp_name = pp_xstrdup(temp_name); + temp_name = pp_xstrdup(tmpfn); - if (!(output = fopen(temp_name, "wt"))) + if (!(output = fdopen(fd, "wt"))) { - fprintf(stderr,"Could not open %s for writing\n", temp_name); + fprintf(stderr,"Could not open fd %s for writing\n", temp_name); exit(2); }