dongsheng.s...@gmail.com 2015-01-07 10:29:
> From: 宋冬生 <songdongsh...@live.cn>

Hi,

> 
> ---
>  mingw-w64-crt/Makefile.am      |  2 +-
>  mingw-w64-crt/misc/mkstemp.c   | 91 
> ++++++++++++++++++++++++++++++++++++++++++
>  mingw-w64-headers/crt/stdlib.h |  1 +
>  3 files changed, 93 insertions(+), 1 deletion(-)
>  create mode 100644 mingw-w64-crt/misc/mkstemp.c
> 
> diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
> index b0ac2f2..3348ea2 100644
> --- a/mingw-w64-crt/Makefile.am
> +++ b/mingw-w64-crt/Makefile.am
> @@ -263,7 +263,7 @@ src_libmingwex=\
>    misc/gettimeofday.c    misc/imaxabs.c             misc/imaxdiv.c
>       misc/isblank.c               misc/iswblank.c        \
>    misc/mbrtowc.c         misc/mbsinit.c             misc/mempcpy.c
>       misc/mingw-aligned-malloc.c  misc/mingw-fseek.c     \
>    misc/mingw_matherr.c   misc/mingw_mbwc_convert.c
> misc/mingw_usleep.c     misc/mingw_wcstod.c
> misc/mingw_wcstof.c    \
> -  misc/mingw_wcstold.c   misc/seterrno.c            misc/sleep.c
>       misc/spawnv.c          \
> +  misc/mingw_wcstold.c   misc/mkstemp.c             misc/seterrno.c
>       misc/sleep.c                 misc/spawnv.c          \
>    misc/spawnve.c         misc/spawnvp.c             misc/spawnvpe.c
>       misc/strnlen.c               misc/strsafe.c         \
>    misc/strtoimax.c       misc/strtold.c             misc/strtoumax.c
>       misc/tdelete.c               misc/tfind.c           \
>    misc/tsearch.c         misc/twalk.c
> misc/uchar_c16rtomb.c   misc/uchar_c32rtomb.c
> misc/uchar_mbrtoc16.c  \
> diff --git a/mingw-w64-crt/misc/mkstemp.c 
> b/mingw-w64-crt/misc/mkstemp.c
> new file mode 100644
> index 0000000..081b512
> --- /dev/null
> +++ b/mingw-w64-crt/misc/mkstemp.c
> @@ -0,0 +1,91 @@
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <io.h>
> +#include <errno.h>
> +#include <share.h>
> +#include <fcntl.h>
> +#include <sys/stat.h>
> +
> +/*
> +    The mkstemp() function generates a unique temporary filename from 
> template,
> +    creates and opens the file, and returns an open file descriptor 
> for the
> +    file.
> +
> +    The template may be any file name with at least six trailing Xs,
> for example
> +    /tmp/temp.XXXXXXXX. The trailing Xs are replaced with a unique 
> digit and
> +    letter combination that makes the file name unique. Since it will 
> be
> +    modified, template must not be a string constant, but should be 
> declared as
> +    a character array.
> +
> +    The file is created with permissions 0600, that is, read plus 
> write for
> +    owner only. The returned file descriptor provides both read and
> write access
> +    to the file.
> + */
> +int __cdecl mkstemp (char *template_name)
> +{
> +    errno_t ec;
> +    int i, j, fd, len, index;
> +
> +    /* These are the (62) characters used in temporary filenames. */
> +    static const char letters[] =
> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
> +
> +    /* The last six characters of template must be "XXXXXX" */
> +    if (template_name == NULL || (len = strlen (template_name)) < 6
> +            || memcmp (template_name + (len - 6), "XXXXXX", 6)) {
> +        errno = EINVAL;
> +        return -1;
> +    }

It will be better if "XXXXXX" and '6' will be variables:
static const char x_template[] = "XXXXXX";
static const int x_template_len = sizeof(x_template)-1;

and the test for 'template_name == NULL' I propose to move to the top of 
the function.

in this case, the code look like this:
if (template_name == NULL) {
     errno = EINVAL;
     return -1;
}

static const char letters[] 
="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const char x_template[] = "XXXXXX";
static const int x_template_len = sizeof(x_template)-1;
const int len = strlen(template_name);

/* The last six characters of template must be "XXXXXX" */
if (len < x_template_len
        || memcmp (template_name + (len - x_template_len), x_template, 
x_template_len)) {
     errno = EINVAL;
     return -1;
}

...

> +
> +    /* User may supply more than six trailing Xs */
> +    for (index = len - 6; index > 0 && template_name[index - 1] ==
> 'X'; index--);
> +
> +    /*
> +        Like OpenBSD, mkstemp() will try at least 2 ** 31 combinations 
> before
> +        giving up.
> +     */
> +    for (i = 0; i >= 0; i++) {
> +        for(j = index; j < len; j++) {
> +            template_name[j] = letters[rand () % 62];
> +        }
> +
> +        ec = _sopen_s (&fd, template_name,
> +                _O_RDWR | _O_CREAT | _O_EXCL | _O_TEMPORARY | 
> _O_BINARY,
> +                SH_DENYRW, _S_IREAD | _S_IWRITE);
> +        if (ec == 0) return fd;
> +        if (ec != EEXIST) return -1;
> +    }
> +
> +    return -1;
> +}
> +
> +#if 0
> +int main (int argc, char *argv[])
> +{
> +    int i, fd;
> +
> +    for (i = 0; i < 10; i++) {
> +        char template_name[] = { "temp_XXXXXX" };
> +        fd = mkstemp (template_name);
> +        if (fd >= 0) {
> +            fprintf (stderr, "fd=%d, name=%s\n", fd, template_name);
> +            _close (fd);
> +        } else {
> +            fprintf (stderr, "errno=%d\n", errno);
> +        }
> +    }
> +
> +    for (i = 0; i < 10; i++) {
> +        char template_name[] = { "temp_XXXXXXXX" };
> +        fd = mkstemp (template_name);
> +        if (fd >= 0) {
> +            fprintf (stderr, "fd=%d, name=%s\n", fd, template_name);
> +            _close (fd);
> +        } else {
> +            fprintf (stderr, "errno=%d\n", errno);
> +        }
> +    }
> +
> +    return 0;
> +}
> +#endif
> diff --git a/mingw-w64-headers/crt/stdlib.h 
> b/mingw-w64-headers/crt/stdlib.h
> index 7743452..fe44195 100644
> --- a/mingw-w64-headers/crt/stdlib.h
> +++ b/mingw-w64-headers/crt/stdlib.h
> @@ -378,6 +378,7 @@ extern "C" {
>    _CRTIMP int __cdecl _mbtowc_l(wchar_t * __restrict__ _DstCh,const
> char * __restrict__ _SrcCh,size_t _SrcSizeInBytes,_locale_t _Locale);
>    size_t __cdecl mbstowcs(wchar_t * __restrict__ _Dest,const char *
> __restrict__ _Source,size_t _MaxCount);
>    _CRTIMP size_t __cdecl _mbstowcs_l(wchar_t * __restrict__
> _Dest,const char * __restrict__ _Source,size_t _MaxCount,_locale_t
> _Locale);
> +  int __cdecl mkstemp(char *template);
>    int __cdecl rand(void);
>    _CRTIMP int __cdecl _set_error_mode(int _Mode);
>    void __cdecl srand(unsigned int _Seed);

-- 
Regards, niXman
___________________________________________________
Dual-target(32 & 64-bit) MinGW-W64 compilers for 32 and 64-bit Windows:
http://sourceforge.net/projects/mingw-w64/
___________________________________________________
Another online IDE: http://liveworkspace.org/

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to