r6831@lvps87-230-33-50: verhaegs | 2008-02-03 14:08:57 +0100
[tangerine.git] / compiler / clib / mktemp.c
blobbe0659c27c43132a96f61835891d9cabf2fa6b61
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 #include <stdio.h> /* FIXME: remove */
16 /*****************************************************************************
18 NAME */
19 #include <stdlib.h>
21 char *mktemp (
23 /* SYNOPSIS */
24 char *template)
26 /* FUNCTION
27 Make a unique temporary file name.
29 INPUTS
30 template- Name of the environment variable.
32 RESULT
33 Returns template.
35 NOTES
36 Template must end in "XXXXXX" (i.e at least 6 X's).
38 Prior to this paragraph being created, mktemp() sometimes produced filenames
39 with '/' in them. AROS doesn't like that at all. Fortunately, the bug in this
40 function which produced it has been fixed. -- blippy
42 For clarity, define the HEAD of the template to be the part before the tail,
43 and the TAIL to be the succession of X's. So in, T:temp.XXXXXX , the head is
44 T:temp. and the tail is XXXXXX .
46 EXAMPLE
48 BUGS
49 Cannot create more than 26 filenames for the same process id. This is because
50 the "bumping" is only done to the first tail character - it should be
51 generalised to bump more characters if necessary.
53 SEE ALSO
55 INTERNALS
56 Based on libnix mktemp
58 ******************************************************************************/
60 ULONG pid = (ULONG)FindTask(0L);
61 char *c = template + strlen(template);
62 BPTR lock;
63 int remainder;
65 while (*--c == 'X')
67 remainder = (int)(pid % 10L);
68 assert(remainder>=0);
69 assert(remainder<10);
70 *c = remainder + '0';
71 pid /= 10L;
74 c++; /* ... c now points to the 1st char of the template tail */
76 if (*c) /* FIXME: why would you get *c == 0 legitimately? */
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)))
83 return template;
85 UnLock(lock);
87 *c = 0; /* FIXME: looks wrong. Why would you want to chop the tail? */
90 return template;
92 } /* mktemp */