/* * This handles abstract system level calls. * * MUSCLE SmartCard Development ( http://www.linuxnet.com ) * * Copyright (C) 1999 * David Corcoran * Copyright (C) 2002-2010 * Ludovic Rousseau * * $Id: sys_unix.c 5047 2010-06-29 14:39:24Z rousseau $ */ /** * @file * @brief This handles abstract system level calls. */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "misc.h" #include "sys_generic.h" #include "debuglog.h" /** * @brief Makes the current process sleep for some seconds. * * @param[in] iTimeVal Number of seconds to sleep. */ INTERNAL int SYS_Sleep(int iTimeVal) { #ifdef HAVE_NANOSLEEP struct timespec mrqtp; mrqtp.tv_sec = iTimeVal; mrqtp.tv_nsec = 0; return nanosleep(&mrqtp, NULL); #else return sleep(iTimeVal); #endif } /** * @brief Makes the current process sleep for some microseconds. * * @param[in] iTimeVal Number of microseconds to sleep. */ INTERNAL int SYS_USleep(int iTimeVal) { #ifdef HAVE_NANOSLEEP struct timespec mrqtp; mrqtp.tv_sec = iTimeVal/1000000; mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000; return nanosleep(&mrqtp, NULL); #else struct timeval tv; tv.tv_sec = iTimeVal/1000000; tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000); return select(0, NULL, NULL, NULL, &tv); #endif } INTERNAL int SYS_RandomInt(int fStart, int fEnd) { static int iInitialized = 0; int iRandNum = 0; if (0 == iInitialized) { srand(SYS_GetSeed()); iInitialized = 1; } iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart; return iRandNum; } INTERNAL int SYS_GetSeed(void) { struct timeval tv; struct timezone tz; long myseed = 0; tz.tz_minuteswest = 0; tz.tz_dsttime = 0; if (gettimeofday(&tv, &tz) == 0) { myseed = tv.tv_usec; } else { myseed = (long) time(NULL); } return myseed; }