a5499ecf5d8784a0f1c47de0088dbff8cd908cfe
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 <crtdll/stdio.h>
17 #include <crtdll/string.h>
18 #include <crtdll/io.h>
21 char* _mktemp (char *_template
)
25 int i
, len
, xcount
, loopcnt
;
29 len
= strlen (_template
);
33 while (xcount
< 6 && cp
> _template
&& cp
[-1] == 'X')
38 while (dp
> _template
&& dp
[-1] != '/' && dp
[-1] != '\\' && dp
[-1] != ':')
41 /* Keep the first characters of the template, but turn the rest into
43 while (cp
> dp
+ 8 - xcount
) {
45 xcount
= (xcount
>= 6) ? 6 : 1 + xcount
;
48 /* If dots occur too early -- squash them. */
50 if (*dp
== '.') *dp
= 'a';
54 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
55 if (cp
+ xcount
+ 3 < _template
+ len
)
56 strcpy (cp
+ xcount
, ".tmp");
60 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
61 for (loopcnt
= 0; loopcnt
< (1 << (5 * xcount
)); loopcnt
++) {
63 for (i
= 0; i
< xcount
; i
++, c
>>= 5)
64 cp
[i
] = "abcdefghijklmnopqrstuvwxyz012345"[c
& 0x1f];
65 if (_access(_template
,0) == -1)
70 /* Failure: truncate the template and return NULL. */