pcsc-lite  1.8.14
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  * $Id$
35  */
36 
42 #include "config.h"
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <sys/time.h>
48 #include <sys/file.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <signal.h>
56 #include <time.h>
57 
58 #include "misc.h"
59 #include "sys_generic.h"
60 #include "debuglog.h"
61 
67 INTERNAL int SYS_Sleep(int iTimeVal)
68 {
69 #ifdef HAVE_NANOSLEEP
70  struct timespec mrqtp;
71  mrqtp.tv_sec = iTimeVal;
72  mrqtp.tv_nsec = 0;
73 
74  return nanosleep(&mrqtp, NULL);
75 #else
76  return sleep(iTimeVal);
77 #endif
78 }
79 
85 INTERNAL int SYS_USleep(int iTimeVal)
86 {
87 #ifdef HAVE_NANOSLEEP
88  struct timespec mrqtp;
89  mrqtp.tv_sec = iTimeVal/1000000;
90  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
91 
92  return nanosleep(&mrqtp, NULL);
93 #else
94  struct timeval tv;
95  tv.tv_sec = iTimeVal/1000000;
96  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
97  return select(0, NULL, NULL, NULL, &tv);
98 #endif
99 }
100 
101 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
102 {
103  static int iInitialized = 0;
104  int iRandNum = 0;
105 
106  if (0 == iInitialized)
107  {
108  srand(SYS_GetSeed());
109  iInitialized = 1;
110  }
111 
112  if (-1 == fEnd)
113  /* full int range */
114  iRandNum = rand();
115  else
116  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
117 
118  return iRandNum;
119 }
120 
121 INTERNAL int SYS_GetSeed(void)
122 {
123  struct timeval tv;
124  struct timezone tz;
125  long myseed = 0;
126 
127  tz.tz_minuteswest = 0;
128  tz.tz_dsttime = 0;
129  if (gettimeofday(&tv, &tz) == 0)
130  {
131  myseed = tv.tv_usec;
132  } else
133  {
134  myseed = (long) time(NULL);
135  }
136  return myseed;
137 }
138 
This handles abstract system level calls.
INTERNAL int SYS_Sleep(int iTimeVal)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:67
INTERNAL int SYS_USleep(int iTimeVal)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:85
This handles debugging.