pcsc-lite  1.8.20
sys_unix.c
Go to the documentation of this file.
1 /*
2  * This handles abstract system level calls.
3  *
4  * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
5  *
6  * Copyright (C) 1999
7  * David Corcoran <corcoran@musclecard.com>
8  * Copyright (C) 2002-2010
9  * Ludovic Rousseau <ludovic.rousseau@free.fr>
10  *
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14 
15 1. Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 3. The name of the author may not be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
40 #include "config.h"
41 #include <sys/time.h>
42 #include <stdlib.h>
43 #include <time.h>
44 
45 #include "misc.h"
46 #include "sys_generic.h"
47 
53 INTERNAL int SYS_Sleep(int iTimeVal)
54 {
55 #ifdef HAVE_NANOSLEEP
56  struct timespec mrqtp;
57  mrqtp.tv_sec = iTimeVal;
58  mrqtp.tv_nsec = 0;
59 
60  return nanosleep(&mrqtp, NULL);
61 #else
62  return sleep(iTimeVal);
63 #endif
64 }
65 
71 INTERNAL int SYS_USleep(int iTimeVal)
72 {
73 #ifdef HAVE_NANOSLEEP
74  struct timespec mrqtp;
75  mrqtp.tv_sec = iTimeVal/1000000;
76  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
77 
78  return nanosleep(&mrqtp, NULL);
79 #else
80  struct timeval tv;
81  tv.tv_sec = iTimeVal/1000000;
82  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
83  return select(0, NULL, NULL, NULL, &tv);
84 #endif
85 }
86 
95 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
96 {
97  int iRandNum = 0;
98 
99  if (-1 == fEnd)
100  /* full int range */
101  iRandNum = rand();
102  else
103  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
104 
105  return iRandNum;
106 }
107 
111 INTERNAL void SYS_InitRandom(void)
112 {
113  struct timeval tv;
114  struct timezone tz;
115  long myseed = 0;
116 
117  tz.tz_minuteswest = 0;
118  tz.tz_dsttime = 0;
119  if (gettimeofday(&tv, &tz) == 0)
120  {
121  myseed = tv.tv_usec;
122  } else
123  {
124  myseed = (long) time(NULL);
125  }
126 
127  srand(myseed);
128 }
129 
INTERNAL void SYS_InitRandom(void)
Initialize the random generator.
Definition: sys_unix.c:111
INTERNAL int SYS_RandomInt(int fStart, int fEnd)
Generate a pseudo random number.
Definition: sys_unix.c:95
This handles abstract system level calls.
INTERNAL int SYS_Sleep(int iTimeVal)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:53
INTERNAL int SYS_USleep(int iTimeVal)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:71