Use dos.library/CreateNewProc() instead of alib/CreateNewProcTags()
[tangerine.git] / compiler / clib / mkstemp.c
blob0e31d15fd25a5b8ae084cf2a1d8a3e5c47da2eb7
1 /*
2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function mkstemp().
6 */
7 #include <string.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <proto/dos.h>
12 /*****************************************************************************
14 NAME */
15 #include <stdlib.h>
17 int mkstemp(
19 /* SYNOPSIS */
20 char *template)
22 /* FUNCTION
24 INPUTS
25 A template that must end with 'XXXXXX'
27 RESULT
28 A file descriptor of opened temporary file or -1 on error.
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= NULL;
45 int ctr = 0;
46 static char filename_letters[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZFILLTO64";
48 while (c > template && *--c == 'X') {
49 ctr++;
52 if (ctr < 6) {
53 errno = EINVAL;
54 return -1;
57 c++;
58 c_start = c;
60 while (1) {
61 while (*c) {
62 *c = filename_letters[rand() & 0x3F];
63 c++;
66 if (!(lock = Lock(template, ACCESS_READ))) {
67 int fd = open(template, O_WRITE|O_CREAT|O_EXCL);
68 if (fd > 0)
69 return fd;
71 UnLock(lock);
72 c = c_start;
74 * Try around 1000 filenames and then give up.
76 if (++ctr > 1000)
77 break;
80 errno = EEXIST;
81 return -1;
83 } /* mkstemp() */