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   int fd=-1;
00032   int len;
00033   char *nf;
00034   int i;
00035 
00036   len=strlen(tmpl);
00037   if (len<6) {
00038     /* bad template */
00039     errno=EINVAL;
00040     return -1;
00041   }
00042   if (strcasecmp(tmpl+(len-7), "XXXXXX")) {
00043     /* bad template, last 6 chars must be "X" */
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 

Generated on Mon Feb 9 21:22:00 2009 for LibOFX by  doxygen 1.5.0