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/msvcrt/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>
27 char* _mktemp(char* _template)
31 int i, len, xcount, loopcnt;
33 DPRINT("_mktemp('%s')\n", _template);
34 len = strlen (_template);
38 while (xcount < 6 && cp > _template && cp[-1] == 'X')
43 while (dp > _template && dp[-1] != '/' && dp[-1] != '\\' && dp[-1] != ':')
46 /* Keep the first characters of the template, but turn the rest into
48 while (cp > dp + 8 - xcount) {
50 xcount = (xcount >= 6) ? 6 : 1 + xcount;
53 /* If dots occur too early -- squash them. */
55 if (*dp == '.') *dp = 'a';
59 /* Try to add ".tmp" to the filename. Truncate unused Xs. */
60 if (cp + xcount + 3 < _template + len)
61 strcpy (cp + xcount, ".tmp");
65 /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */
66 for (loopcnt = 0; loopcnt < (1 << (5 * xcount)); loopcnt++) {
68 for (i = 0; i < xcount; i++, c >>= 5)
69 cp[i] = "abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f];
70 if (_access(_template,0) == -1)
75 /* Failure: truncate the template and return NULL. */