Fix to fnamebuf array size and invoke getenv only once.
Index: src/file.cpp
===================================================================
--- src/file.cpp (revision 702657)
+++ src/file.cpp (working copy)
@@ -42,6 +42,7 @@
#include <stdio.h> // for P_tmpdir, std{err,in,out}, tmpnam()
#include <stdlib.h> // for mkstemp(), strtoul(), size_t
#include <ctype.h> // for isalpha(), isspace(), toupper()
+#include <string.h> // for memcpy()
#if defined (_WIN32) && !defined (__CYGWIN__)
@@ -58,6 +59,9 @@
# define _BINARY 0
#endif
+#ifndef PATH_MAX
+# define PATH_MAX 1024
+#endif
#include <rw/_file.h>
#include <rw/_defs.h>
@@ -257,8 +261,18 @@
# define P_tmpdir "/tmp"
# endif // P_tmpdir
- char fnamebuf[] = P_tmpdir "/.rwtmpXXXXXX";
+ const char *tmpdir = getenv ("TMPDIR");
+ if (tmpdir == NULL) {
+ tmpdir = P_tmpdir;
+ }
+ char fnamebuf [PATH_MAX];
+
+ size_t len = strlen (tmpdir) - 1;
+
+ memcpy (fnamebuf, tmpdir, len);
+ memcpy (fnamebuf+len, "/.rwtmpXXXXXX", sizeof ("/.rwtmpXXXXXX"));
+
fd = mkstemp (fnamebuf);
if (fd >= 0)
@@ -294,7 +308,7 @@
// names that have no extension. tempnam uses malloc to allocate
// space for the filename; the program is responsible for freeing
// this space when it is no longer needed.
- char* const fname = tempnam (P_tmpdir, ".rwtmp");
+ char* const fname = tempnam (tmpdir, ".rwtmp");
if (!fname)
return -1;
> -----Original Message-----
> From: Scott Zhong [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 08, 2008 11:03 AM
> To: [email protected]
> Subject: RE: [PATCH] STDCXX-1019 __rw_mkstemp in file.cpp should honor
> TMPDIR environment variable
>
> Posted wrong diff. here is the correct diff
>
> Index: src/file.cpp
> ===================================================================
> --- src/file.cpp (revision 702657)
> +++ src/file.cpp (working copy)
> @@ -42,6 +42,7 @@
> #include <stdio.h> // for P_tmpdir, std{err,in,out}, tmpnam()
> #include <stdlib.h> // for mkstemp(), strtoul(), size_t
> #include <ctype.h> // for isalpha(), isspace(), toupper()
> +#include <string.h> // for memcpy()
>
>
> #if defined (_WIN32) && !defined (__CYGWIN__)
> @@ -257,8 +258,15 @@
> # define P_tmpdir "/tmp"
> # endif // P_tmpdir
>
> - char fnamebuf[] = P_tmpdir "/.rwtmpXXXXXX";
> + char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv
> ("TMPDIR");
>
> + char fnamebuf [sizeof (tmpdir) + sizeof ("/.rwtmpXXXXXX")];
> +
> + size_t len = sizeof (tmpdir) - 1;
> +
> + memcpy (fnamebuf, tmpdir, len);
> + memcpy (fnamebuf+len, "/.rwtmpXXXXXX", sizeof ("/.rwtmpXXXXXX"));
> +
> fd = mkstemp (fnamebuf);
>
> if (fd >= 0)
> @@ -294,7 +302,7 @@
> // names that have no extension. tempnam uses malloc to allocate
> // space for the filename; the program is responsible for freeing
> // this space when it is no longer needed.
> - char* const fname = tempnam (P_tmpdir, ".rwtmp");
> + char* const fname = tempnam (tmpdir, ".rwtmp");
>
> if (!fname)
> return -1;
>
> > -----Original Message-----
> > From: Scott Zhong [mailto:[EMAIL PROTECTED]
> > Sent: Wednesday, October 08, 2008 10:15 AM
> > To: [email protected]
> > Subject: [PATCH] STDCXX-1019 __rw_mkstemp in file.cpp should honor
> TMPDIR
> > environment variable
> >
> > https://issues.apache.org/jira/browse/STDCXX-1019
> >
> > file affected: <stdcxx>/src/file.cpp
> >
> > Index: src/file.cpp
> > ===================================================================
> > --- src/file.cpp (revision 702657)
> > +++ src/file.cpp (working copy)
> > @@ -257,7 +257,9 @@
> > # define P_tmpdir "/tmp"
> > # endif // P_tmpdir
> >
> > - char fnamebuf[] = P_tmpdir "/.rwtmpXXXXXX";
> > + char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv
> > ("TMPDIR");
> > +
> > + char fnamebuf[] = tmpdir "/.rwtmpXXXXXX";
> >
> > fd = mkstemp (fnamebuf);
> >
> > @@ -294,7 +296,7 @@
> > // names that have no extension. tempnam uses malloc to
allocate
> > // space for the filename; the program is responsible for
freeing
> > // this space when it is no longer needed.
> > - char* const fname = tempnam (P_tmpdir, ".rwtmp");
> > + char* const fname = tempnam (tmpdir, ".rwtmp");
> >
> > if (!fname)
> > return -1;