Removed double NAME entry.
[AROS.git] / compiler / clib / mktemp.c
blobe527f7f2f0162e39e354d0f4cbedce88efdd7659
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <string.h>
7 #include <proto/exec.h>
8 #include <proto/dos.h>
9 #include <dos/dos.h>
11 #include <exec/types.h>
12 #include <assert.h>
14 /*****************************************************************************
16 NAME */
17 #include <stdlib.h>
19 char *mktemp (
21 /* SYNOPSIS */
22 char *template)
24 /* FUNCTION
25 Make a unique temporary file name.
27 INPUTS
28 template - template to change into unique filename
30 RESULT
31 Returns template.
33 NOTES
34 Template must end in "XXXXXX" (i.e at least 6 X's).
36 Prior to this paragraph being created, mktemp() sometimes produced filenames
37 with '/' in them. AROS doesn't like that at all. Fortunately, the bug in this
38 function which produced it has been fixed. -- blippy
40 For clarity, define the HEAD of the template to be the part before the tail,
41 and the TAIL to be the succession of X's. So in, T:temp.XXXXXX , the head is
42 T:temp. and the tail is XXXXXX .
44 EXAMPLE
46 BUGS
47 Cannot create more than 26 filenames for the same process id. This is because
48 the "bumping" is only done to the first tail character - it should be
49 generalised to bump more characters if necessary.
51 SEE ALSO
53 INTERNALS
54 Based on libnix mktemp
56 ******************************************************************************/
58 IPTR pid = (IPTR)FindTask(0L);
59 char *c = template + strlen(template);
60 BPTR lock;
61 IPTR remainder;
63 while (*--c == 'X')
65 remainder = pid % 10;
66 assert(remainder>=0 && remainder<10);
67 *c = remainder + '0';
68 pid /= 10L;
71 c++; /* ... c now points to the 1st char of the template tail */
73 /* If template errornously does not end in X c will point to '\0';
74 exit gracefully
76 if (*c)
78 /* Loop over the first position of the tail, bumping it up as necessary */
79 for(*c = 'A'; *c <= 'Z'; (*c)++)
81 if (!(lock = Lock(template, ACCESS_READ)))
82 return template;
83 UnLock(lock);
87 return template;
88 } /* mktemp */