pcsc-lite  1.8.2
sys_unix.c
Go to the documentation of this file.
00001 /*
00002  * This handles abstract system level calls.
00003  *
00004  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00005  *
00006  * Copyright (C) 1999
00007  *  David Corcoran <corcoran@linuxnet.com>
00008  * Copyright (C) 2002-2010
00009  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00010  *
00011  * $Id: sys_unix.c 5047 2010-06-29 14:39:24Z rousseau $
00012  */
00013 
00019 #include "config.h"
00020 #include <sys/types.h>
00021 #include <sys/mman.h>
00022 #include <sys/stat.h>
00023 #include <sys/wait.h>
00024 #include <sys/time.h>
00025 #include <sys/file.h>
00026 #include <fcntl.h>
00027 #include <errno.h>
00028 #include <unistd.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <signal.h>
00033 #include <time.h>
00034 
00035 #include "misc.h"
00036 #include "sys_generic.h"
00037 #include "debuglog.h"
00038 
00044 INTERNAL int SYS_Sleep(int iTimeVal)
00045 {
00046 #ifdef HAVE_NANOSLEEP
00047     struct timespec mrqtp;
00048     mrqtp.tv_sec = iTimeVal;
00049     mrqtp.tv_nsec = 0;
00050 
00051     return nanosleep(&mrqtp, NULL);
00052 #else
00053     return sleep(iTimeVal);
00054 #endif
00055 }
00056 
00062 INTERNAL int SYS_USleep(int iTimeVal)
00063 {
00064 #ifdef HAVE_NANOSLEEP
00065     struct timespec mrqtp;
00066     mrqtp.tv_sec = iTimeVal/1000000;
00067     mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
00068 
00069     return nanosleep(&mrqtp, NULL);
00070 #else
00071     struct timeval tv;
00072     tv.tv_sec  = iTimeVal/1000000;
00073     tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
00074     return select(0, NULL, NULL, NULL, &tv);
00075 #endif
00076 }
00077 
00078 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
00079 {
00080     static int iInitialized = 0;
00081     int iRandNum = 0;
00082 
00083     if (0 == iInitialized)
00084     {
00085         srand(SYS_GetSeed());
00086         iInitialized = 1;
00087     }
00088 
00089     iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
00090 
00091     return iRandNum;
00092 }
00093 
00094 INTERNAL int SYS_GetSeed(void)
00095 {
00096     struct timeval tv;
00097     struct timezone tz;
00098     long myseed = 0;
00099 
00100     tz.tz_minuteswest = 0;
00101     tz.tz_dsttime = 0;
00102     if (gettimeofday(&tv, &tz) == 0)
00103     {
00104         myseed = tv.tv_usec;
00105     } else
00106     {
00107         myseed = (long) time(NULL);
00108     }
00109     return myseed;
00110 }
00111