4f531706de84b30513543e648f69c9af5a29af2d
2 * COPYRIGHT: See COPYING in the top level directory
3 * Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/crtdll/io/mktemp.c
6 * PURPOSE: Makes a temp file based on a template
7 * PROGRAMER: DJ Delorie
10 * 28/12/98: Appropriated for the Reactos Kernel
13 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
14 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
16 #include <msvcrt/stdio.h>
17 #include <msvcrt/string.h>
18 #include <msvcrt/io.h>
21 char* _mktemp (char *_template
)
25 int i
, len
, xcount
, loopcnt
;
27 len
= strlen (_template
);
31 while (xcount
< 6 && cp
> _template
&& cp
[-1] == 'X')
36 while (dp
> _template
&& dp
[-1] != '/' && dp
[-1] != '\\' && dp
[-1] != ':')
39 /* Keep the first characters of the template, but turn the rest into
41 while (cp
> dp
+ 8 - xcount
) {
43 xcount
= (xcount
>= 6) ? 6 : 1 + xcount
;
46 /* If dots occur too early -- squash them. */
48 if (*dp
== '.') *dp
= 'a';
52 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
53 if (cp
+ xcount
+ 3 < _template
+ len
)
54 strcpy (cp
+ xcount
, ".tmp");
58 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
59 for (loopcnt
= 0; loopcnt
< (1 << (5 * xcount
)); loopcnt
++) {
61 for (i
= 0; i
< xcount
; i
++, c
>>= 5)
62 cp
[i
] = "abcdefghijklmnopqrstuvwxyz012345"[c
& 0x1f];
63 if (_access(_template
,0) == -1)
68 /* Failure: truncate the template and return NULL. */
73 wchar_t* _wmktemp (wchar_t *_template
)
77 int i
, len
, xcount
, loopcnt
;
79 len
= wcslen (_template
);
83 while (xcount
< 6 && cp
> _template
&& cp
[-1] == L
'X')
88 while (dp
> _template
&& dp
[-1] != L
'/' && dp
[-1] != L
'\\' && dp
[-1] != L
':')
91 /* Keep the first characters of the template, but turn the rest into
93 while (cp
> dp
+ 8 - xcount
) {
95 xcount
= (xcount
>= 6) ? 6 : 1 + xcount
;
98 /* If dots occur too early -- squash them. */
100 if (*dp
== L
'.') *dp
= L
'a';
104 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
105 if (cp
+ xcount
+ 3 < _template
+ len
)
106 wcscpy (cp
+ xcount
, L
".tmp");
110 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
111 for (loopcnt
= 0; loopcnt
< (1 << (5 * xcount
)); loopcnt
++) {
113 for (i
= 0; i
< xcount
; i
++, c
>>= 5)
114 cp
[i
] = L
"abcdefghijklmnopqrstuvwxyz012345"[c
& 0x1f];
115 if (_waccess(_template
,0) == -1)
120 /* Failure: truncate the template and return NULL. */