muimaster.library: support Listview_List in List
[AROS.git] / compiler / posixc / mkstemp.c
blob6bfcfc9451c342585768db479361b64de14f11a7
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function mkstemp().
6 */
7 #include <string.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <proto/dos.h>
12 #include "__upath.h"
14 /*****************************************************************************
16 NAME */
17 #include <stdlib.h>
19 int mkstemp(
21 /* SYNOPSIS */
22 char *template)
24 /* FUNCTION
26 INPUTS
27 A template that must end with 'XXXXXX'
29 RESULT
30 A file descriptor of opened temporary file or -1 on error.
32 NOTES
34 EXAMPLE
36 BUGS
38 SEE ALSO
40 INTERNALS
42 ******************************************************************************/
44 char *c = template + strlen(template);
45 char *c_start;
46 BPTR lock= BNULL;
47 int ctr = 0;
48 const char filename_letters[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZFILLTO64";
49 const char *atemplate;
51 while (c > template && *--c == 'X') {
52 ctr++;
55 if (ctr < 6) {
56 errno = EINVAL;
57 return -1;
60 c++;
61 c_start = c;
63 while (1) {
64 while (*c) {
65 *c = filename_letters[rand() & 0x3F];
66 c++;
69 atemplate = __path_u2a(template);
70 if(!atemplate)
71 return -1;
72 if (!(lock = Lock(atemplate, ACCESS_READ))) {
73 int fd = open(template, O_WRITE|O_CREAT|O_EXCL);
74 if (fd > 0)
75 return fd;
77 UnLock(lock);
78 c = c_start;
80 * Try around 1000 filenames and then give up.
82 if (++ctr > 1000)
83 break;
86 errno = EEXIST;
87 return -1;
89 } /* mkstemp() */