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#ifdef HAVE_UNISTD_H
22#include <unistd.h>
23#endif
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <assert.h>
27
28
29
30#ifdef _WIN32
31
32#ifdef _MSC_VER
33#include <Windows.h>
34#include <io.h>
35#define strcasecmp strcmpi
36#define snprintf _snprintf
37#define open _open
38#endif
39
40int mkstemp_win32(char *tmpl)
41{
42 int fd = -1;
43 int len;
44 char *nf;
45 int i;
46
47 len = strlen(tmpl);
48 if (len < 6)
49 {
50 /* bad template */
51 errno = EINVAL;
52 return -1;
53 }
54 if (strcasecmp(tmpl + (len - 7), "XXXXXX"))
55 {
56 /* bad template, last 6 chars must be "X" */
57 errno = EINVAL;
58 return -1;
59 }
60
61 nf = strdup(tmpl);
62
63 for (i = 0; i < 10; i++)
64 {
65 int rnd;
66 char numbuf[16];
67
68 rnd = rand();
69 snprintf(numbuf, sizeof(numbuf) - 1, "%06x", rnd);
70 memmove(nf + (len - 7), numbuf, 6);
71 fd = open(nf, O_RDWR | O_BINARY | O_CREAT, 0444);
72 if (fd >= 0)
73 {
74 memmove(tmpl, nf, len);
75 free(nf);
76 return fd;
77 }
78 }
79 free(nf);
80 errno = EEXIST;
81 return -1;
82}
83
84
85#endif
86
87