OSDN Git Service

Lots and lots of cleanups.
[uclinux-h8/uClibc.git] / libc / stdlib / mkstemp.c
1 #include <string.h>
2 #include <features.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5
6 int mkstemp(template)
7 char * template;
8 {
9         int i;
10         int num __attribute__ ((unused)); /* UNINITIALIZED */
11         int n2;
12         int l = strlen(template);
13         
14         if (l<6) {
15                 errno = EINVAL;
16                 return -1;
17         }
18         
19         for(i=l-6;i<l;i++)
20                 if (template[i] != 'X') {
21                         errno = EINVAL;
22                         return -1;
23                 }
24         
25 again:  
26         n2 = num;
27         for(i=l-1;i>=l-6;i--) {
28                 template[i] = '0' + n2 % 10;
29                 n2 /= 10;
30         }
31         
32         i = open(template, O_RDWR|O_EXCL|O_CREAT, 0666);
33         
34         if (i==-1) {
35                 if (errno == EEXIST) {
36                         num++;
37                         goto again;
38                 } else
39                         return -1;
40         }
41         
42         return i;
43 }