LibOFX
win32.cpp
1 /***************************************************************************
2  $RCSfile: win32.cpp,v $
3  -------------------
4  cvs : $Id: win32.cpp,v 1.3 2007-10-27 12:15:58 aquamaniac Exp $
5  begin : Sat Oct 27 2007
6  copyright : (C) 2007 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * This file is part of the project "LibOfx". *
11  * Please see toplevel file COPYING of that project for license details. *
12  ***************************************************************************/
13 
14 
15 #include "win32.hh"
16 
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <assert.h>
25 
26 
27 
28 #ifdef __WIN32__
29 
30 int mkstemp_win32(char *tmpl)
31 {
32  int fd = -1;
33  int len;
34  char *nf;
35  int i;
36 
37  len = strlen(tmpl);
38  if (len < 6)
39  {
40  /* bad template */
41  errno = EINVAL;
42  return -1;
43  }
44  if (strcasecmp(tmpl + (len - 7), "XXXXXX"))
45  {
46  /* bad template, last 6 chars must be "X" */
47  errno = EINVAL;
48  return -1;
49  }
50 
51  nf = strdup(tmpl);
52 
53  for (i = 0; i < 10; i++)
54  {
55  int rnd;
56  char numbuf[16];
57 
58  rnd = rand();
59  snprintf(numbuf, sizeof(numbuf) - 1, "%06x", rnd);
60  memmove(nf + (len - 7), numbuf, 6);
61  fd = open(nf, O_RDWR | O_BINARY | O_CREAT, 0444);
62  if (fd >= 0)
63  {
64  memmove(tmpl, nf, len);
65  free(nf);
66  return fd;
67  }
68  }
69  free(nf);
70  errno = EEXIST;
71  return -1;
72 }
73 
74 
75 #endif
76 
77