LibOFX
win32.cpp
00001 /***************************************************************************
00002  $RCSfile: win32.cpp,v $
00003  -------------------
00004  cvs         : $Id: win32.cpp,v 1.3 2007-10-27 12:15:58 aquamaniac Exp $
00005  begin       : Sat Oct 27 2007
00006  copyright   : (C) 2007 by Martin Preuss
00007  email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  * This file is part of the project "LibOfx".                              *
00011  * Please see toplevel file COPYING of that project for license details.   *
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 {
00032   int fd = -1;
00033   int len;
00034   char *nf;
00035   int i;
00036 
00037   len = strlen(tmpl);
00038   if (len < 6)
00039   {
00040     /* bad template */
00041     errno = EINVAL;
00042     return -1;
00043   }
00044   if (strcasecmp(tmpl + (len - 7), "XXXXXX"))
00045   {
00046     /* bad template, last 6 chars must be "X" */
00047     errno = EINVAL;
00048     return -1;
00049   }
00050 
00051   nf = strdup(tmpl);
00052 
00053   for (i = 0; i < 10; i++)
00054   {
00055     int rnd;
00056     char numbuf[16];
00057 
00058     rnd = rand();
00059     snprintf(numbuf, sizeof(numbuf) - 1, "%06x", rnd);
00060     memmove(nf + (len - 7), numbuf, 6);
00061     fd = open(nf, O_RDWR | O_BINARY | O_CREAT, 0444);
00062     if (fd >= 0)
00063     {
00064       memmove(tmpl, nf, len);
00065       free(nf);
00066       return fd;
00067     }
00068   }
00069   free(nf);
00070   errno = EEXIST;
00071   return -1;
00072 }
00073 
00074 
00075 #endif
00076 
00077