pcsc-lite  1.8.3
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 5047 2010-06-29 14:39:24Z 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  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
90 
91  return iRandNum;
92 }
93 
94 INTERNAL int SYS_GetSeed(void)
95 {
96  struct timeval tv;
97  struct timezone tz;
98  long myseed = 0;
99 
100  tz.tz_minuteswest = 0;
101  tz.tz_dsttime = 0;
102  if (gettimeofday(&tv, &tz) == 0)
103  {
104  myseed = tv.tv_usec;
105  } else
106  {
107  myseed = (long) time(NULL);
108  }
109  return myseed;
110 }
111