Debian Codegraph @

libgm2/libm2iso/wraptime.cc

raw
1 /* wraptime.c provides access to time related system calls.
2
3 Copyright (C) 2009-2022 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 <m2rts.h>
29
30 #define EXPORT(FUNC) m2iso ## _wraptime_ ## FUNC
31 #define M2EXPORT(FUNC) m2iso ## _M2_wraptime_ ## FUNC
32 #define M2LIBNAME "m2iso"
33
34 #if defined(HAVE_SYS_TYPES_H)
35 #include "sys/types.h"
36 #endif
37
38 #if defined(HAVE_SYS_TIME_H)
39 #include "sys/time.h"
40 #endif
41
42 #if defined(HAVE_TIME_H)
43 #include "time.h"
44 #endif
45
46 #if defined(HAVE_MALLOC_H)
47 #include "malloc.h"
48 #endif
49
50 #if defined(HAVE_LIMITS_H)
51 #include "limits.h"
52 #endif
53
54 #if !defined(NULL)
55 #define NULL (void *)0
56 #endif
57
58
59 /* InitTimeval returns a newly created opaque type. */
60
61 #if defined(WE_HAVE_STRUCT_TIMEVAL) && defined(HAVE_MALLOC_H)
62 extern "C" struct timeval *
63 EXPORT(InitTimeval) (void)
64 {
65 return (struct timeval *)malloc (sizeof (struct timeval));
66 }
67 #else
68 extern "C" void *
69 EXPORT(InitTimeval) (void)
70 {
71 return NULL;
72 }
73 #endif
74
75 /* KillTimeval deallocates the memory associated with an opaque type. */
76
77 extern "C" struct timeval *
78 EXPORT(KillTimeval) (void *tv)
79 {
80 #if defined(HAVE_MALLOC_H)
81 free (tv);
82 #endif
83 return NULL;
84 }
85
86 /* InitTimezone returns a newly created opaque type. */
87
88 #if defined(HAVE_STRUCT_TM_TM_ZONE) && defined(HAVE_MALLOC_H)
89 extern "C" struct timezone *
90 EXPORT(InitTimezone) (void)
91 {
92 return (struct timezone *)malloc (sizeof (struct timezone));
93 }
94 #else
95 extern "C" void *
96 EXPORT(InitTimezone) (void)
97 {
98 return NULL;
99 }
100 #endif
101
102 /* KillTimezone - deallocates the memory associated with an opaque
103 type. */
104
105 #if defined(HAVE_STRUCT_TM_TM_ZONE) && defined(HAVE_MALLOC_H)
106 extern "C" struct timezone *
107 EXPORT(KillTimezone) (struct timezone *tv)
108 {
109 free (tv);
110 return NULL;
111 }
112 #else
113 extern "C" void *
114 EXPORT(KillTimezone) (void *tv)
115 {
116 return NULL;
117 }
118 #endif
119
120 /* InitTM - returns a newly created opaque type. */
121
122 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_MALLOC_H)
123 extern "C" struct tm *
124 EXPORT(InitTM) (void)
125 {
126 return (struct tm *)malloc (sizeof (struct tm));
127 }
128 #else
129 extern "C" void *
130 EXPORT(InitTM) (void)
131 {
132 return NULL;
133 }
134 #endif
135
136 /* KillTM - deallocates the memory associated with an opaque type. */
137
138 extern "C" struct tm *
139 EXPORT(KillTM) (struct tm *tv)
140 {
141 #if defined(HAVE_MALLOC_H)
142 free (tv);
143 #endif
144 return NULL;
145 }
146
147 /* gettimeofday - calls gettimeofday(2) with the same parameters, tv,
148 and, tz. It returns 0 on success. */
149
150 #if defined(HAVE_STRUCT_TM_TM_ZONE) && defined(HAVE_GETTIMEOFDAY)
151 extern "C" int
152 EXPORT(gettimeofday) (void *tv, struct timezone *tz)
153 {
154 return gettimeofday ((struct timeval *) tv, tz);
155 }
156 #else
157 extern "C" int
158 EXPORT(gettimeofday) (void *tv, void *tz)
159 {
160 return -1;
161 }
162 #endif
163
164 /* settimeofday - calls settimeofday(2) with the same parameters, tv,
165 and, tz. It returns 0 on success. */
166
167 #if defined(HAVE_STRUCT_TM_TM_ZONE) && defined(HAVE_SETTIMEOFDAY)
168 extern "C" int
169 EXPORT(settimeofday) (void *tv, struct timezone *tz)
170 {
171 return settimeofday ((struct timeval *) tv, tz);
172 }
173 #else
174 extern "C" int
175 EXPORT(settimeofday) (void *tv, void *tz)
176 {
177 return -1;
178 }
179 #endif
180
181 /* wraptime_GetFractions - returns the tv_usec field inside the
182 timeval structure. */
183
184 #if defined(WE_HAVE_STRUCT_TIMEVAL_TV_USEC)
185 extern "C" unsigned int
186 EXPORT(GetFractions) (struct timeval *tv)
187 {
188 return (unsigned int)tv->tv_usec;
189 }
190 #else
191 extern "C" unsigned int
192 EXPORT(GetFractions) (void *tv)
193 {
194 return (unsigned int)-1;
195 }
196 #endif
197
198 /* localtime_r - returns the tm parameter, m, after it has been
199 assigned with appropriate contents determined by, tv. Notice that
200 this procedure function expects, timeval, as its first parameter
201 and not a time_t (as expected by the posix equivalent). */
202
203 #if defined(WE_HAVE_STRUCT_TIMEVAL_TV_SEC)
204 extern "C" struct tm *
205 EXPORT(localtime_r) (struct timeval *tv, struct tm *m)
206 {
207 return localtime_r (&tv->tv_sec, m);
208 }
209 #else
210 extern "C" struct tm *
211 EXPORT(localtime_r) (void *tv, struct tm *m)
212 {
213 return m;
214 }
215 #endif
216
217 /* wraptime_GetYear - returns the year from the structure, m. */
218
219 #if defined(WE_HAVE_STRUCT_TM_YEAR)
220 extern "C" unsigned int
221 EXPORT(GetYear) (struct tm *m)
222 {
223 return m->tm_year;
224 }
225 #else
226 extern "C" unsigned int
227 EXPORT(GetYear) (void *m)
228 {
229 return (unsigned int)-1;
230 }
231 #endif
232
233 /* wraptime_GetMonth - returns the month from the structure, m. */
234
235 #if defined(WE_HAVE_STRUCT_TM_MON)
236 extern "C" unsigned int
237 EXPORT(GetMonth) (struct tm *m)
238 {
239 return m->tm_mon;
240 }
241 #else
242 extern "C" unsigned int
243 EXPORT(GetMonth) (void *m)
244 {
245 return (unsigned int)-1;
246 }
247 #endif
248
249 /* wraptime_GetDay - returns the day of the month from the structure,
250 m. */
251
252 #if defined(WE_HAVE_STRUCT_TM_MDAY)
253 extern "C" unsigned int
254 EXPORT(GetDay) (struct tm *m)
255 {
256 return m->tm_mday;
257 }
258 #else
259 extern "C" unsigned int
260 EXPORT(GetDay) (void *m)
261 {
262 return (unsigned int)-1;
263 }
264 #endif
265
266 /* wraptime_GetHour - returns the hour of the day from the structure,
267 m. */
268
269 #if defined(WE_HAVE_STRUCT_TM_HOUR)
270 extern "C" unsigned int
271 EXPORT(GetHour) (struct tm *m)
272 {
273 return m->tm_hour;
274 }
275 #else
276 extern "C" unsigned int
277 EXPORT(GetHour) (void *m)
278 {
279 return (unsigned int)-1;
280 }
281 #endif
282
283 /* wraptime_GetMinute - returns the minute within the hour from the
284 structure, m. */
285
286 #if defined(WE_HAVE_STRUCT_TM_MIN)
287 extern "C" unsigned int
288 EXPORT(GetMinute) (struct tm *m)
289 {
290 return m->tm_min;
291 }
292 #else
293 extern "C" unsigned int
294 EXPORT(GetMinute) (void *m)
295 {
296 return (unsigned int)-1;
297 }
298 #endif
299
300 /* wraptime_GetSecond - returns the seconds in the minute from the
301 structure, m. The return value will always be in the range 0..59.
302 A leap minute of value 60 will be truncated to 59. */
303
304 #if defined(WE_HAVE_STRUCT_TM_SEC)
305 extern "C" unsigned int
306 EXPORT(GetSecond) (struct tm *m)
307 {
308 if (m->tm_sec == 60)
309 return 59;
310 else
311 return m->tm_sec;
312 }
313 #else
314 extern "C" unsigned int
315 EXPORT(GetSecond) (void *m)
316 {
317 return (unsigned int)-1;
318 }
319 #endif
320
321 /* wraptime_GetSummerTime - returns true if summer time is in effect. */
322
323 #if defined(HAVE_STRUCT_TM_TM_ZONE)
324 extern "C" bool
325 EXPORT(GetSummerTime) (struct timezone *tz)
326 {
327 return tz->tz_dsttime != 0;
328 }
329 #else
330 extern "C" bool
331 EXPORT(GetSummerTime) (void *tz)
332 {
333 return false;
334 }
335 #endif
336
337 /* wraptime_GetDST - returns the number of minutes west of GMT. */
338
339 #if defined(HAVE_STRUCT_TM_TM_ZONE)
340 extern "C" int
341 EXPORT(GetDST) (struct timezone *tz)
342 {
343 return tz->tz_minuteswest;
344 }
345 #else
346 extern "C" int
347 EXPORT(GetDST) (void *tz)
348 {
349 #if defined(INT_MIN)
350 return INT_MIN;
351 #else
352 return (int)((unsigned int)-1);
353 #endif
354 }
355 #endif
356
357 /* SetTimezone - set the timezone field inside timeval, tv. */
358
359 #if defined(HAVE_STRUCT_TM_TM_ZONE)
360 extern "C" void
361 EXPORT(SetTimezone) (struct timezone *tz, int zone, int minuteswest)
362 {
363 tz->tz_dsttime = zone;
364 tz->tz_minuteswest = minuteswest;
365 }
366 #else
367 extern "C" void
368 EXPORT(SetTimezone) (void *tz, int zone, int minuteswest)
369 {
370 }
371 #endif
372
373 /* SetTimeval - sets the fields in tm, t, with: second, minute, hour,
374 day, month, year, fractions. */
375
376 #if defined(WE_HAVE_STRUCT_TM)
377 extern "C" void
378 EXPORT(SetTimeval) (struct tm *t, unsigned int second, unsigned int minute,
379 unsigned int hour, unsigned int day, unsigned int month,
380 unsigned int year, unsigned int yday, unsigned int wday,
381 unsigned int isdst)
382 {
383 #if defined(WE_HAVE_STRUCT_TM_SEC)
384 t->tm_sec = second;
385 #endif
386 #if defined(WE_HAVE_STRUCT_TM_MIN)
387 t->tm_min = minute;
388 #endif
389 #if defined(WE_HAVE_STRUCT_TM_HOUR)
390 t->tm_hour = hour;
391 #endif
392 #if defined(WE_HAVE_STRUCT_TM_MDAY)
393 t->tm_mday = day;
394 #endif
395 #if defined(WE_HAVE_STRUCT_TM_MON)
396 t->tm_mon = month;
397 #endif
398 #if defined(WE_HAVE_STRUCT_TM_YEAR)
399 t->tm_year = year;
400 #endif
401 #if defined(WE_HAVE_STRUCT_TM_YDAY)
402 t->tm_yday = yday;
403 #endif
404 #if defined(WE_HAVE_STRUCT_TM_WDAY)
405 t->tm_wday = wday;
406 #endif
407 #if defined(WE_HAVE_STRUCT_TM_ISDST)
408 t->tm_isdst = isdst;
409 #endif
410 }
411 #else
412 extern "C" void
413 EXPORT(SetTimeval) (void *t, unsigned int second, unsigned int minute,
414 unsigned int hour, unsigned int day, unsigned int month,
415 unsigned int year, unsigned int yday, unsigned int wday,
416 unsigned int isdst)
417 {
418 }
419 #endif
420
421 /* init - init/finish functions for the module */
422
423 /* GNU Modula-2 linking hooks. */
424
425 extern "C" void
426 M2EXPORT(init) (int, char **, char **)
427 {
428 }
429
430 extern "C" void
431 M2EXPORT(fini) (int, char **, char **)
432 {
433 }
434
435 extern "C" void
436 M2EXPORT(dep) (void)
437 {
438 }
439
440 extern "C" void __attribute__((__constructor__))
441 M2EXPORT(ctor) (void)
442 {
443 m2iso_M2RTS_RegisterModule ("wraptime", M2LIBNAME,
444 M2EXPORT(init), M2EXPORT(fini),
445 M2EXPORT(dep));
446 }