Added missing _wmktemp().
[reactos.git] / reactos / lib / msvcrt / io / mktemp.c
1 /*
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
8 Boudewijn Dekker
9 * UPDATE HISTORY:
10 * 28/12/98: Appropriated for the Reactos Kernel
11 */
12
13 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
14 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
15
16 #include <msvcrt/stdio.h>
17 #include <msvcrt/string.h>
18 #include <msvcrt/io.h>
19
20
21 char* _mktemp (char *_template)
22 {
23 static int count = 0;
24 char *cp, *dp;
25 int i, len, xcount, loopcnt;
26
27 len = strlen (_template);
28 cp = _template + len;
29
30 xcount = 0;
31 while (xcount < 6 && cp > _template && cp[-1] == 'X')
32 xcount++, cp--;
33
34 if (xcount) {
35 dp = cp;
36 while (dp > _template && dp[-1] != '/' && dp[-1] != '\\' && dp[-1] != ':')
37 dp--;
38
39 /* Keep the first characters of the template, but turn the rest into
40 Xs. */
41 while (cp > dp + 8 - xcount) {
42 *--cp = 'X';
43 xcount = (xcount >= 6) ? 6 : 1 + xcount;
44 }
45
46 /* If dots occur too early -- squash them. */
47 while (dp < cp) {
48 if (*dp == '.') *dp = 'a';
49 dp++;
50 }
51
52 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
53 if (cp + xcount + 3 < _template + len)
54 strcpy (cp + xcount, ".tmp");
55 else
56 cp[xcount] = 0;
57
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++) {
60 int c = count++;
61 for (i = 0; i < xcount; i++, c >>= 5)
62 cp[i] = "abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f];
63 if (_access(_template,0) == -1)
64 return _template;
65 }
66 }
67
68 /* Failure: truncate the template and return NULL. */
69 *_template = 0;
70 return 0;
71 }
72
73 wchar_t* _wmktemp (wchar_t *_template)
74 {
75 static int count = 0;
76 wchar_t *cp, *dp;
77 int i, len, xcount, loopcnt;
78
79 len = wcslen (_template);
80 cp = _template + len;
81
82 xcount = 0;
83 while (xcount < 6 && cp > _template && cp[-1] == L'X')
84 xcount++, cp--;
85
86 if (xcount) {
87 dp = cp;
88 while (dp > _template && dp[-1] != L'/' && dp[-1] != L'\\' && dp[-1] != L':')
89 dp--;
90
91 /* Keep the first characters of the template, but turn the rest into
92 Xs. */
93 while (cp > dp + 8 - xcount) {
94 *--cp = L'X';
95 xcount = (xcount >= 6) ? 6 : 1 + xcount;
96 }
97
98 /* If dots occur too early -- squash them. */
99 while (dp < cp) {
100 if (*dp == L'.') *dp = L'a';
101 dp++;
102 }
103
104 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
105 if (cp + xcount + 3 < _template + len)
106 wcscpy (cp + xcount, L".tmp");
107 else
108 cp[xcount] = 0;
109
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++) {
112 int c = count++;
113 for (i = 0; i < xcount; i++, c >>= 5)
114 cp[i] = L"abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f];
115 if (_waccess(_template,0) == -1)
116 return _template;
117 }
118 }
119
120 /* Failure: truncate the template and return NULL. */
121 *_template = 0;
122 return 0;
123 }