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 #include <msvcrt/msvcrtdbg.h>
24 char* _mktemp (char *_template)
28 int i, len, xcount, loopcnt;
30 DPRINT("_mktemp('%s')\n", _template);
31 len = strlen (_template);
35 while (xcount < 6 && cp > _template && cp[-1] == 'X')
40 while (dp > _template && dp[-1] != '/' && dp[-1] != '\\' && dp[-1] != ':')
43 /* Keep the first characters of the template, but turn the rest into
45 while (cp > dp + 8 - xcount) {
47 xcount = (xcount >= 6) ? 6 : 1 + xcount;
50 /* If dots occur too early -- squash them. */
52 if (*dp == '.') *dp = 'a';
56 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
57 if (cp + xcount + 3 < _template + len)
58 strcpy (cp + xcount, ".tmp");
62 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
63 for (loopcnt = 0; loopcnt < (1 << (5 * xcount)); loopcnt++) {
65 for (i = 0; i < xcount; i++, c >>= 5)
66 cp[i] = "abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f];
67 if (_access(_template,0) == -1)
72 /* Failure: truncate the template and return NULL. */
77 wchar_t* _wmktemp (wchar_t *_template)
81 int i, len, xcount, loopcnt;
83 DPRINT("_wmktemp('%S')\n", _template);
84 len = wcslen (_template);
88 while (xcount < 6 && cp > _template && cp[-1] == L'X')
93 while (dp > _template && dp[-1] != L'/' && dp[-1] != L'\\' && dp[-1] != L':')
96 /* Keep the first characters of the template, but turn the rest into
98 while (cp > dp + 8 - xcount) {
100 xcount = (xcount >= 6) ? 6 : 1 + xcount;
103 /* If dots occur too early -- squash them. */
105 if (*dp == L'.') *dp = L'a';
109 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
110 if (cp + xcount + 3 < _template + len)
111 wcscpy (cp + xcount, L".tmp");
115 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
116 for (loopcnt = 0; loopcnt < (1 << (5 * xcount)); loopcnt++) {
118 for (i = 0; i < xcount; i++, c >>= 5)
119 cp[i] = L"abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f];
120 if (_waccess(_template,0) == -1)
125 /* Failure: truncate the template and return NULL. */