| 1 |
/* Selective.c provide access to timeval and select. |
| 2 |
|
| 3 |
Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. */ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "system.h" |
| 29 |
#include "ansidecl.h" |
| 30 |
|
| 31 |
#include "gm2-libs-host.h" |
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
extern "C" { |
| 35 |
#endif |
| 36 |
|
| 37 |
#if defined(HAVE_SELECT) |
| 38 |
# define FDSET_T fd_set |
| 39 |
#else |
| 40 |
# define FDSET_T void |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
#if defined(HAVE_SELECT) |
| 45 |
int Selective_Select (int nooffds, |
| 46 |
fd_set *readfds, |
| 47 |
fd_set *writefds, |
| 48 |
fd_set *exceptfds, |
| 49 |
struct timeval *timeout) |
| 50 |
{ |
| 51 |
return select (nooffds, readfds, writefds, exceptfds, timeout); |
| 52 |
} |
| 53 |
#else |
| 54 |
int Selective_Select (int nooffds, |
| 55 |
void *readfds, |
| 56 |
void *writefds, |
| 57 |
void *exceptfds, |
| 58 |
void *timeout) |
| 59 |
{ |
| 60 |
return 0; |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
/* InitTime initialises a timeval structure and returns a pointer to it. */ |
| 65 |
|
| 66 |
#if defined(HAVE_SELECT) |
| 67 |
struct timeval *Selective_InitTime (unsigned int sec, unsigned int usec) |
| 68 |
{ |
| 69 |
struct timeval *t = (struct timeval *) malloc (sizeof (struct timeval)); |
| 70 |
|
| 71 |
t->tv_sec = (long int) sec; |
| 72 |
t->tv_usec = (long int) usec; |
| 73 |
return t; |
| 74 |
} |
| 75 |
|
| 76 |
void Selective_GetTime (struct timeval *t, |
| 77 |
unsigned int *sec, unsigned int *usec) |
| 78 |
{ |
| 79 |
*sec = (unsigned int) t->tv_sec; |
| 80 |
*usec = (unsigned int) t->tv_usec; |
| 81 |
} |
| 82 |
|
| 83 |
void Selective_SetTime (struct timeval *t, |
| 84 |
unsigned int sec, unsigned int usec) |
| 85 |
{ |
| 86 |
t->tv_sec = sec; |
| 87 |
t->tv_usec = usec; |
| 88 |
} |
| 89 |
|
| 90 |
/* KillTime frees the timeval structure and returns NULL. */ |
| 91 |
|
| 92 |
struct timeval *Selective_KillTime (struct timeval *t) |
| 93 |
{ |
| 94 |
free (t); |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
/* InitSet returns a pointer to a FD_SET. */ |
| 99 |
|
| 100 |
fd_set *Selective_InitSet (void) |
| 101 |
{ |
| 102 |
fd_set *s = (fd_set *) malloc (sizeof (fd_set)); |
| 103 |
|
| 104 |
return s; |
| 105 |
} |
| 106 |
|
| 107 |
/* KillSet frees the FD_SET and returns NULL. */ |
| 108 |
|
| 109 |
fd_set *Selective_KillSet (fd_set *s) |
| 110 |
{ |
| 111 |
free (s); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
/* FdZero generate an empty set. */ |
| 116 |
|
| 117 |
void Selective_FdZero (fd_set *s) |
| 118 |
{ |
| 119 |
FD_ZERO (s); |
| 120 |
} |
| 121 |
|
| 122 |
/* FS_Set include an element, fd, into set, s. */ |
| 123 |
|
| 124 |
void Selective_FdSet (int fd, fd_set *s) |
| 125 |
{ |
| 126 |
FD_SET (fd, s); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/* FdClr exclude an element, fd, from the set, s. */ |
| 131 |
|
| 132 |
void Selective_FdClr (int fd, fd_set *s) |
| 133 |
{ |
| 134 |
FD_CLR (fd, s); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* FdIsSet return TRUE if, fd, is present in set, s. */ |
| 139 |
|
| 140 |
int Selective_FdIsSet (int fd, fd_set *s) |
| 141 |
{ |
| 142 |
return FD_ISSET (fd, s); |
| 143 |
} |
| 144 |
|
| 145 |
/* GetTimeOfDay fills in a record, Timeval, filled in with the |
| 146 |
current system time in seconds and microseconds. |
| 147 |
It returns zero (see man 3p gettimeofday). */ |
| 148 |
|
| 149 |
int Selective_GetTimeOfDay (struct timeval *t) |
| 150 |
{ |
| 151 |
return gettimeofday (t, NULL); |
| 152 |
} |
| 153 |
#else |
| 154 |
|
| 155 |
void *Selective_InitTime (unsigned int sec, unsigned int usec) |
| 156 |
{ |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
void *Selective_KillTime (void *t) |
| 161 |
{ |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
void Selective_GetTime (struct timeval *t, |
| 166 |
unsigned int *sec, unsigned int *usec) |
| 167 |
{ |
| 168 |
} |
| 169 |
|
| 170 |
void Selective_SetTime (struct timeval *t, |
| 171 |
unsigned int sec, unsigned int usec) |
| 172 |
{ |
| 173 |
} |
| 174 |
|
| 175 |
fd_set *Selective_InitSet (void) |
| 176 |
{ |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
void Selective_FdZero (void *s) |
| 181 |
{ |
| 182 |
} |
| 183 |
|
| 184 |
void Selective_FdSet (int fd, void *s) |
| 185 |
{ |
| 186 |
} |
| 187 |
|
| 188 |
void Selective_FdClr (int fd, void *s) |
| 189 |
{ |
| 190 |
} |
| 191 |
|
| 192 |
int Selective_FdIsSet (int fd, void *s) |
| 193 |
{ |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
int Selective_GetTimeOfDay (struct timeval *t) |
| 198 |
{ |
| 199 |
return -1; |
| 200 |
} |
| 201 |
#endif |
| 202 |
|
| 203 |
|
| 204 |
/* MaxFdsPlusOne returns max (a + 1, b + 1). */ |
| 205 |
|
| 206 |
int Selective_MaxFdsPlusOne (int a, int b) |
| 207 |
{ |
| 208 |
if (a > b) |
| 209 |
return a+1; |
| 210 |
else |
| 211 |
return b+1; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/* WriteCharRaw writes a single character to the file descriptor. */ |
| 216 |
|
| 217 |
void Selective_WriteCharRaw (int fd, char ch) |
| 218 |
{ |
| 219 |
write (fd, &ch, 1); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* ReadCharRaw read and return a single char from file descriptor, fd. */ |
| 224 |
|
| 225 |
char Selective_ReadCharRaw (int fd) |
| 226 |
{ |
| 227 |
char ch; |
| 228 |
|
| 229 |
read (fd, &ch, 1); |
| 230 |
return ch; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
void |
| 235 |
_M2_Selective_init () |
| 236 |
{ |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
_M2_Selective_finish () |
| 241 |
{ |
| 242 |
} |
| 243 |
|
| 244 |
#ifdef __cplusplus |
| 245 |
} |
| 246 |
#endif |