From: Jarkko Hietaniemi Date: Tue, 3 Jun 2003 09:51:59 +0000 (+0000) Subject: Salvage parts of #19667. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2941a2e1ed23bc0518b5a6191536ac96286a18c1;p=p5sagit%2Fp5-mst-13.2.git Salvage parts of #19667. p4raw-id: //depot/perl@19673 --- diff --git a/perlio.c b/perlio.c index feee1b0..dcd2f66 100644 --- a/perlio.c +++ b/perlio.c @@ -4814,35 +4814,43 @@ PerlIO_stdoutf(const char *fmt, ...) PerlIO * PerlIO_tmpfile(void) { - /* - * I have no idea how portable mkstemp() is ... - */ -#if defined(WIN32) || !defined(HAVE_MKSTEMP) - dTHX; - PerlIO *f = NULL; - FILE *stdio = PerlSIO_tmpfile(); - if (stdio) { - if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), &PerlIO_stdio, "w+", Nullsv))) { - PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); - s->stdio = stdio; - } - } - return f; -#else - dTHX; - SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0); - int fd = mkstemp(SvPVX(sv)); - PerlIO *f = NULL; - if (fd >= 0) { - f = PerlIO_fdopen(fd, "w+"); - if (f) { - PerlIOBase(f)->flags |= PERLIO_F_TEMP; - } - PerlLIO_unlink(SvPVX(sv)); - SvREFCNT_dec(sv); - } - return f; -#endif + dTHX; + PerlIO *f = NULL; + int fd = -1; +#ifdef WIN32 + fd = win32_tmpfd(); + if (fd >= 0) + f = PerlIO_fdopen(fd, "w+b"); +#else /* WIN32 */ +# ifdef HAS_MKSTEMP + SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0); + + /* + * I have no idea how portable mkstemp() is ... NI-S + */ + fd = mkstemp(SvPVX(sv)); + if (fd >= 0) { + f = PerlIO_fdopen(fd, "w+"); + if (f) + PerlIOBase(f)->flags |= PERLIO_F_TEMP; + PerlLIO_unlink(SvPVX(sv)); + SvREFCNT_dec(sv); + } +# else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */ + FILE *stdio = PerlSIO_tmpfile(); + + if (stdio) { + if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), + &PerlIO_stdio, "w+", Nullsv))) { + PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); + + if (s) + s->stdio = stdio; + } + } +# endif /* else HAS_MKSTEMP */ +#endif /* else WIN32 */ + return f; } #undef HAS_FSETPOS diff --git a/win32/win32.c b/win32/win32.c index b022e0c..083e98e 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -2615,8 +2615,8 @@ win32_rewind(FILE *pf) return; } -DllExport FILE* -win32_tmpfile(void) +DllExport int +win32_tmpfd(void) { dTHX; char prefix[MAX_PATH+1]; @@ -2640,11 +2640,20 @@ win32_tmpfile(void) #endif DEBUG_p(PerlIO_printf(Perl_debug_log, "Created tmpfile=%s\n",filename)); - return fdopen(fd, "w+b"); + return fd; } } } } + return -1; +} + +DllExport FILE* +win32_tmpfile(void) +{ + int fd = win32_tmpfd(); + if (fd >= 0) + return win32_fdopen(fd, "w+b"); return NULL; } diff --git a/win32/win32iop.h b/win32/win32iop.h index e835b2e..1683e97 100644 --- a/win32/win32iop.h +++ b/win32/win32iop.h @@ -67,6 +67,7 @@ DllExport int win32_fseek(FILE *pf,Off_t offset,int origin); DllExport int win32_fgetpos(FILE *pf,fpos_t *p); DllExport int win32_fsetpos(FILE *pf,const fpos_t *p); DllExport void win32_rewind(FILE *pf); +DllExport int win32_tmpfd(void); DllExport FILE* win32_tmpfile(void); DllExport void win32_abort(void); DllExport int win32_fstat(int fd,Stat_t *sbufptr);