pcsc-lite  1.8.11
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 Changes to this license can be made only by the copyright author with
24 explicit written consent.
25 
26 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: sys_unix.c 6851 2014-02-14 15:43:32Z rousseau $
38  */
39 
45 #include "config.h"
46 #include <sys/types.h>
47 #include <sys/mman.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 #include <sys/time.h>
51 #include <sys/file.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <signal.h>
59 #include <time.h>
60 
61 #include "misc.h"
62 #include "sys_generic.h"
63 #include "debuglog.h"
64 
70 INTERNAL int SYS_Sleep(int iTimeVal)
71 {
72 #ifdef HAVE_NANOSLEEP
73  struct timespec mrqtp;
74  mrqtp.tv_sec = iTimeVal;
75  mrqtp.tv_nsec = 0;
76 
77  return nanosleep(&mrqtp, NULL);
78 #else
79  return sleep(iTimeVal);
80 #endif
81 }
82 
88 INTERNAL int SYS_USleep(int iTimeVal)
89 {
90 #ifdef HAVE_NANOSLEEP
91  struct timespec mrqtp;
92  mrqtp.tv_sec = iTimeVal/1000000;
93  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
94 
95  return nanosleep(&mrqtp, NULL);
96 #else
97  struct timeval tv;
98  tv.tv_sec = iTimeVal/1000000;
99  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
100  return select(0, NULL, NULL, NULL, &tv);
101 #endif
102 }
103 
104 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
105 {
106  static int iInitialized = 0;
107  int iRandNum = 0;
108 
109  if (0 == iInitialized)
110  {
111  srand(SYS_GetSeed());
112  iInitialized = 1;
113  }
114 
115  if (-1 == fEnd)
116  /* full int range */
117  iRandNum = rand();
118  else
119  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
120 
121  return iRandNum;
122 }
123 
124 INTERNAL int SYS_GetSeed(void)
125 {
126  struct timeval tv;
127  struct timezone tz;
128  long myseed = 0;
129 
130  tz.tz_minuteswest = 0;
131  tz.tz_dsttime = 0;
132  if (gettimeofday(&tv, &tz) == 0)
133  {
134  myseed = tv.tv_usec;
135  } else
136  {
137  myseed = (long) time(NULL);
138  }
139  return myseed;
140 }
141 
This handles abstract system level calls.
INTERNAL int SYS_Sleep(int iTimeVal)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:70
INTERNAL int SYS_USleep(int iTimeVal)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:88
This handles debugging.