OSDN Git Service

08b356710af67a69280b49e7bcf0d3c9d01521a6
[uclinux-h8/uClibc.git] / libc / stdlib / mktemp.c
1
2 #include <features.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6
7 char * mktemp(template)
8 char * template;
9 {
10         int i;
11         int num; /* UNINITIALIZED */
12         int n2;
13         int l = strlen(template);
14         struct stat stbuf;
15         
16         if (l<6) {
17                 errno = EINVAL;
18                 return 0;
19         }
20         
21         for(i=l-6;i<l;i++)
22                 if (template[i] != 'X') {
23                         errno = EINVAL;
24                         return 0;
25                 }
26         
27 again:  
28         n2 = num;
29         for(i=l-1;i>=l-6;i--) {
30                 template[i] = '0' + n2 % 10;
31                 n2 /= 10;
32         }
33         
34         if (stat(template, &stbuf) == 0) {
35                 num++;
36                 goto again;
37         }
38         
39         return template;
40 }