purge remaining gpl code from clib, and make clib build again
[tangerine.git] / compiler / clib / mkstemp.c
blob528d939a555fa358e27ca22f4fe3991b51ee4220
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function mkstemp().
6 */
8 #include <string.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <proto/dos.h>
13 /*****************************************************************************
15 NAME */
16 #include <stdlib.h>
18 int mkstemp(
20 /* SYNOPSIS */
21 char *template)
23 /* FUNCTION
25 INPUTS
26 A template that must end with 'XXXXXX'
28 RESULT
30 NOTES
32 EXAMPLE
34 BUGS
36 SEE ALSO
38 INTERNALS
40 ******************************************************************************/
42 char *c = template + strlen(template);
43 char *c_start;
44 BPTR lock;
45 int ctr = 0;
46 static char filename_letters[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZFILLTO64";
48 while (c > template && *--c == 'X') {
49 ctr++;
52 if (ctr < 6) {
53 return EINVAL;
56 c++;
57 c_start = c;
59 while (1) {
60 while (*c) {
61 *c = filename_letters[rand() & 0x3F];
62 c++;
64 if (!(lock = Lock(template, ACCESS_READ))) {
65 int fd = open(template, O_CREAT|O_EXCL);
66 if (fd > 0)
67 return fd;
69 UnLock(lock);
70 c = c_start;
72 * Try around 1000 filenames and then give up.
74 if (++ctr > 1000)
75 break;
78 return EEXIST;
80 } /* mkstemp() */