ReactOS 0.4.16-dev-979-g79f281e
mktemp.cpp File Reference
#include <corecrt_internal_lowio.h>
#include <corecrt_internal_securecrt.h>
#include <mbctype.h>
#include <process.h>
#include <stddef.h>
#include <stdio.h>
Include dependency graph for mktemp.cpp:

Go to the source code of this file.

Functions

static bool common_mktemp_s_continue (char const *const string, char const *const current) throw ()
 
static bool common_mktemp_s_continue (wchar_t const *const, wchar_t const *const) throw ()
 
template<typename Character >
 _Success_ (return==0) static errno_t __cdecl common_mktemp_s(_Inout_updates_z_(buffer_size_in_chars) Character *const template_string
 
size_t const buffer_size_in_chars throw ()
 
errno_t __cdecl _mktemp_s (char *const template_string, size_t const buffer_size_in_chars)
 
errno_t __cdecl _wmktemp_s (wchar_t *const template_string, size_t const buffer_size_in_chars)
 
template<typename Character >
static Character *__cdecl common_mktemp (Character *const template_string) throw ()
 
char *__cdecl _mktemp (char *const template_string)
 
wchar_t *__cdecl _wmktemp (wchar_t *const template_string)
 

Function Documentation

◆ _mktemp()

char *__cdecl _mktemp ( char *const  template_string)

Definition at line 160 of file mktemp.cpp.

161{
162 return common_mktemp(template_string);
163}
static Character *__cdecl common_mktemp(Character *const template_string)
Definition: mktemp.cpp:145

◆ _mktemp_s()

errno_t __cdecl _mktemp_s ( char *const  template_string,
size_t const  buffer_size_in_chars 
)

Definition at line 119 of file mktemp.cpp.

123{
124 return common_mktemp_s(template_string, buffer_size_in_chars);
125}

Referenced by test_utf8().

◆ _Success_()

template<typename Character >
_Success_ ( return  = = 0) const

◆ _wmktemp()

wchar_t *__cdecl _wmktemp ( wchar_t *const  template_string)

Definition at line 165 of file mktemp.cpp.

166{
167 return common_mktemp(template_string);
168}

◆ _wmktemp_s()

errno_t __cdecl _wmktemp_s ( wchar_t *const  template_string,
size_t const  buffer_size_in_chars 
)

Definition at line 127 of file mktemp.cpp.

131{
132 return common_mktemp_s(template_string, buffer_size_in_chars);
133}

◆ common_mktemp()

template<typename Character >
static Character *__cdecl common_mktemp ( Character *const  template_string)
throw (
)
static

Definition at line 145 of file mktemp.cpp.

148{
149 typedef __crt_char_traits<Character> traits;
150
151 _VALIDATE_RETURN(template_string != nullptr, EINVAL, nullptr);
152
153 errno_t const result = common_mktemp_s(
154 template_string,
155 static_cast<size_t>(traits::tcslen(template_string) + 1));
156
157 return result == 0 ? template_string : nullptr;
158}
#define EINVAL
Definition: acclib.h:90
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
GLuint64EXT * result
Definition: glext.h:11304
int errno_t
Definition: corecrt.h:615

Referenced by _mktemp(), and _wmktemp().

◆ common_mktemp_s_continue() [1/2]

static bool common_mktemp_s_continue ( char const *const  string,
char const *const  current 
)
throw (
)
static

Definition at line 18 of file mktemp.cpp.

19{
20 return !_ismbstrail(
21 reinterpret_cast<unsigned char const*>(string),
22 reinterpret_cast<unsigned char const*>(current));
23}
struct task_struct * current
Definition: linux.c:32
int __cdecl _ismbstrail(const unsigned char *start, const unsigned char *str)
Definition: ismbtrl.c:32
#define const
Definition: zconf.h:233

Referenced by throw().

◆ common_mktemp_s_continue() [2/2]

static bool common_mktemp_s_continue ( wchar_t const * const  ,
wchar_t const * const   
)
throw (
)
static

Definition at line 25 of file mktemp.cpp.

26{
27 return true;
28}

◆ throw()

size_t const buffer_size_in_chars throw ( )

Definition at line 43 of file mktemp.cpp.

44{
45 typedef __crt_char_traits<Character> traits;
46
47 _VALIDATE_RETURN_ERRCODE(template_string != nullptr && buffer_size_in_chars > 0, EINVAL);
48
49 size_t const template_string_length = traits::tcsnlen(template_string, buffer_size_in_chars);
50 if (template_string_length >= buffer_size_in_chars)
51 {
52 _RESET_STRING(template_string, buffer_size_in_chars);
53 _RETURN_DEST_NOT_NULL_TERMINATED(template_string, buffer_size_in_chars);
54 }
55 _FILL_STRING(template_string, buffer_size_in_chars, template_string_length + 1);
56
57 if(template_string_length < 6 || buffer_size_in_chars <= template_string_length)
58 {
59 _RESET_STRING(template_string, buffer_size_in_chars);
60 _VALIDATE_RETURN_ERRCODE(("Incorrect Input for mktemp", 0), EINVAL);
61 }
62
63 // The Win32 thread identifier is unique across all threads in all processes.
64 // Note, however, that unlike *NIX process identifiers, which are not reused
65 // until all values up to 32K have been used, Win32 thread identifiers are
66 // frequenty reused and usually have small numbers.
67 unsigned number = GetCurrentThreadId();
68
69 // 'string' points to the null-terminator:
70 Character* string = template_string + template_string_length;
71
72 size_t template_length = 0;
73
74 // Replace the last five 'X' characters with the number:
75 while (--string >= template_string
76 && common_mktemp_s_continue(template_string, string)
77 && *string == 'X'
78 && template_length < 5)
79 {
80 ++template_length;
81 *string = static_cast<Character>((number % 10) + '0');
82 number /= 10;
83 }
84
85 // Too few X's?
86 if (*string != 'X' || template_length < 5)
87 {
88 _RESET_STRING(template_string, buffer_size_in_chars);
89 _VALIDATE_RETURN_ERRCODE(("Incorrect Input for mktemp", 0), EINVAL);
90 }
91
92 // Finally, add the letter:
93 Character letter = 'a';
94
95 *string = letter++;
96
97 errno_t const saved_errno = errno;
98 errno = 0;
99
100 // Test each letter, from a-z, until we find a name that is not yet used:
101 while (traits::taccess_s(template_string, 0) == 0)
102 {
103 if (letter == 'z' + 1)
104 {
105 _RESET_STRING(template_string, buffer_size_in_chars);
106 errno = EEXIST;
107 return errno;
108 }
109
110 *string = letter++;
111 errno = 0;
112 }
113
114 // Restore the old value of errno and return success:
115 errno = saved_errno;
116 return 0;
117}
#define EEXIST
Definition: acclib.h:88
#define _RETURN_DEST_NOT_NULL_TERMINATED(_String, _Size)
#define _FILL_STRING
#define _RESET_STRING(_String, _Size)
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
static bool common_mktemp_s_continue(char const *const string, char const *const current)
Definition: mktemp.cpp:18
static unsigned int number
Definition: dsound.c:1479
#define errno
Definition: errno.h:18
DWORD WINAPI GetCurrentThreadId(void)
Definition: thread.c:459