Re: [PATCH v2 1/7] common: add scoped_fd

Yao Qi
2018-02-20T10:12:33Z

On Mon, Feb 19, 2018 at 3:28 PM, Metzger, Markus T
<markus.t.metzger@intel.com> wrote:
> Hello Yao,
>
> Looks like they added mkstemp() in 2015 after a few complaints:
> https://sourceforge.net/u/jeisele123/mingw-w64/ci/f713f639f6f017371c5176f4deab07d1a92473b4/
>

Thanks for looking into this...

> How about checking for mkstemp and, if not available, falling back to tmpnam.

Can we unconditionally use tmpnam+open which are more portable? I know
it is racy, but mkstemp is only used in a test case. It is overkill to me to do
the configure check or import gnulib module.

If we use mkstemp in gdb source (not test case) one day in the future, I prefer
importing gnulib module. What do you think?

> Something like this:
>
> #ifndef HAVE_MKSTEMP
>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <stdio.h>
>
> /* Define a racy version of mkstemp based on tmpnam for systems that do not
> support mkstemp. */
> static int
> mkstemp (char *name)
> {
> if (strlen (name) < L_tmpnam)
> return -1;
>
> name = tmpnam (name);
> if (name == nullptr)
> return -1;
>
> return open (name);
> }
>
> #endif /* HAVE_MKSTEMP */
>

--
Yao (齐尧)