pcsc-lite  1.8.8
sys_unix.c
Go to the documentation of this file.
1 /*
2  * This handles abstract system level calls.
3  *
4  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
5  *
6  * Copyright (C) 1999
7  * David Corcoran <corcoran@linuxnet.com>
8  * Copyright (C) 2002-2010
9  * Ludovic Rousseau <ludovic.rousseau@free.fr>
10  *
11  * $Id: sys_unix.c 6319 2012-06-05 08:59:08Z rousseau $
12  */
13 
19 #include "config.h"
20 #include <sys/types.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <sys/time.h>
25 #include <sys/file.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <time.h>
34 
35 #include "misc.h"
36 #include "sys_generic.h"
37 #include "debuglog.h"
38 
44 INTERNAL int SYS_Sleep(int iTimeVal)
45 {
46 #ifdef HAVE_NANOSLEEP
47  struct timespec mrqtp;
48  mrqtp.tv_sec = iTimeVal;
49  mrqtp.tv_nsec = 0;
50 
51  return nanosleep(&mrqtp, NULL);
52 #else
53  return sleep(iTimeVal);
54 #endif
55 }
56 
62 INTERNAL int SYS_USleep(int iTimeVal)
63 {
64 #ifdef HAVE_NANOSLEEP
65  struct timespec mrqtp;
66  mrqtp.tv_sec = iTimeVal/1000000;
67  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
68 
69  return nanosleep(&mrqtp, NULL);
70 #else
71  struct timeval tv;
72  tv.tv_sec = iTimeVal/1000000;
73  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
74  return select(0, NULL, NULL, NULL, &tv);
75 #endif
76 }
77 
78 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
79 {
80  static int iInitialized = 0;
81  int iRandNum = 0;
82 
83  if (0 == iInitialized)
84  {
85  srand(SYS_GetSeed());
86  iInitialized = 1;
87  }
88 
89  if (-1 == fEnd)
90  /* full int range */
91  iRandNum = rand();
92  else
93  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
94 
95  return iRandNum;
96 }
97 
98 INTERNAL int SYS_GetSeed(void)
99 {
100  struct timeval tv;
101  struct timezone tz;
102  long myseed = 0;
103 
104  tz.tz_minuteswest = 0;
105  tz.tz_dsttime = 0;
106  if (gettimeofday(&tv, &tz) == 0)
107  {
108  myseed = tv.tv_usec;
109  } else
110  {
111  myseed = (long) time(NULL);
112  }
113  return myseed;
114 }
115 
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:44
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:62
This handles debugging.