00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "win32.hh"
00016
00017 #include <errno.h>
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 #include <sys/stat.h>
00023 #include <fcntl.h>
00024 #include <assert.h>
00025
00026
00027
00028 #ifdef OS_WIN32
00029
00030 int mkstemp(char *tmpl) {
00031 int fd=-1;
00032 int len;
00033 char *nf;
00034 int i;
00035
00036 len=strlen(tmpl);
00037 if (len<6) {
00038
00039 errno=EINVAL;
00040 return -1;
00041 }
00042 if (strcasecmp(tmpl+(len-7), "XXXXXX")) {
00043
00044 errno=EINVAL;
00045 return -1;
00046 }
00047
00048 nf=strdup(tmpl);
00049
00050 for (i=0; i<10; i++) {
00051 int rnd;
00052 char numbuf[16];
00053
00054 rnd=rand();
00055 snprintf(numbuf, sizeof(numbuf)-1, "%06x", rnd);
00056 memmove(nf+(len-7), numbuf, 6);
00057 fd=open(nf, O_RDWR | O_BINARY | O_CREAT, 0444);
00058 if (fd>=0) {
00059 memmove(tmpl, nf, len);
00060 free(nf);
00061 return fd;
00062 }
00063 }
00064 free(nf);
00065 errno=EEXIST;
00066 return -1;
00067 }
00068
00069
00070 #endif
00071
00072