Usermode/libaxwin4 - Fix bad message ID for resize, bad return in window create
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / mktemp.c
1 /*
2  * Acess2 POSIX Emulation Layer
3  * - By John Hodge
4  * 
5  * mkstemp.c
6  * - mkstemp/mktemp
7  */
8 #include <unistd.h>     // mktemp
9 #include <stdlib.h>     // mkstemp
10 #include <string.h>     // str*
11 #include <errno.h>
12
13 // === CODE ===
14 int mkstemp(char *template)
15 {
16         size_t  tpl_len = strlen(template);
17         if( tpl_len < 6 ) {
18                 errno = EINVAL;
19                 return -1;
20         }
21         if( strcmp(template+tpl_len-6, "XXXXXX") != 0 ) {
22                 errno = EINVAL;
23                 return -1;
24         }
25         
26         for( int i = 0; i < 1000000; i ++ )
27         {
28                 sprintf(template+tpl_len-6, "%06d", i);
29                 int fd = open(template, O_EXCL|O_CREAT, 0600);
30                 if(fd == -1)    continue ;
31         
32                 return fd;
33         }
34         
35         errno = EEXIST;
36         template[0] = '\0';
37         return -1;
38 }
39
40 char *mktemp(char *template)
41 {
42          int fd = mkstemp(template);
43         if( fd == -1 )
44                 return NULL;
45         close(fd);
46         return template;
47 }
48
49

UCC git Repository :: git.ucc.asn.au