Debian Codegraph @

extension/time.c

raw
1 /*
2 * time.c - Builtin functions that provide time-related functions.
3 */
4
5 /*
6 * Copyright (C) 2012, 2013, 2014, 2018, 2022, 2023, 2024,
7 * the Free Software Foundation, Inc.
8 *
9 * This file is part of GAWK, the GNU implementation of the
10 * AWK Programming Language.
11 *
12 * GAWK is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * GAWK is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdbool.h>
37 #include <unistd.h>
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #ifdef __VMS
43 #ifndef HAVE_NANOSLEEP
44 #define HAVE_NANOSLEEP
45 #endif
46 #ifdef gettimeofday
47 #undef gettimeofday
48 #endif
49 #ifdef __ia64__
50 /* nanosleep not working correctly on IA64 */
51 static int
52 vms_fake_nanosleep(struct timespec *rqdly, struct timespec *rmdly)
53 {
54 int result;
55 struct timespec mtime1, mtime2;
56
57 result = nanosleep(rqdly, &mtime1);
58 if (result == 0)
59 return 0;
60
61 /* On IA64 it returns 100 nanoseconds early with an error */
62 if ((mtime1.tv_sec == 0) && (mtime1.tv_nsec <= 100)) {
63 mtime1.tv_nsec += 100;
64 result = nanosleep(&mtime1, &mtime2);
65 if (result == 0)
66 return 0;
67 if ((mtime2.tv_sec == 0) && (mtime2.tv_nsec <= 100)) {
68 return 0;
69 }
70 }
71 return result;
72 }
73 #define nanosleep(x,y) vms_fake_nanosleep(x, y)
74 #endif
75 #endif
76
77 #include "gawkapi.h"
78
79 #include "gettext.h"
80 #define _(msgid) gettext(msgid)
81 #define N_(msgid) msgid
82
83 static const gawk_api_t *api; /* for convenience macros to work */
84 static awk_ext_id_t ext_id;
85 static const char *ext_version = "time extension: version 1.2";
86 static awk_bool_t (*init_func)(void) = NULL;
87
88 int plugin_is_GPL_compatible;
89
90 #include <time.h>
91 #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
92 #include <sys/time.h>
93 #endif
94 #if defined(HAVE_SELECT) && defined(HAVE_SYS_SELECT_H)
95 #include <sys/select.h>
96 #endif
97 #if defined(HAVE_GETSYSTEMTIMEASFILETIME)
98 #define WIN32_LEAN_AND_MEAN
99 #include <windows.h>
100 #endif
101
102 /*
103 * Returns time since 1/1/1970 UTC as a floating point value; should
104 * have sub-second precision, but the actual precision will vary based
105 * on the platform
106 */
107 static awk_value_t *
108 do_gettimeofday(int nargs, awk_value_t *result, struct awk_ext_func *unused)
109 {
110 double curtime;
111
112 assert(result != NULL);
113
114 #if defined(HAVE_CLOCK_GETTIME)
115 {
116 struct timespec tv;
117 clock_gettime(CLOCK_REALTIME, & tv);
118 curtime = tv.tv_sec+(tv.tv_nsec/1000000000.0);
119 }
120 #elif defined(HAVE_GETTIMEOFDAY)
121 {
122 struct timeval tv;
123 gettimeofday(&tv,NULL);
124 curtime = tv.tv_sec+(tv.tv_usec/1000000.0);
125 }
126 #elif defined(HAVE_GETSYSTEMTIMEASFILETIME)
127 /* based on perl win32/win32.c:win32_gettimeofday() implementation */
128 {
129 union {
130 unsigned __int64 ft_i64;
131 FILETIME ft_val;
132 } ft;
133
134 /* # of 100-nanosecond intervals since January 1, 1601 (UTC) */
135 GetSystemTimeAsFileTime(&ft.ft_val);
136 #ifdef __GNUC__
137 #define Const64(x) x##LL
138 #else
139 #define Const64(x) x##i64
140 #endif
141 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
142 #define EPOCH_BIAS Const64(116444736000000000)
143 curtime = (ft.ft_i64 - EPOCH_BIAS)/10000000.0;
144 #undef Const64
145 }
146 #else
147 /* no way to retrieve system time on this platform */
148 curtime = -1;
149 update_ERRNO_string(_("gettimeofday: not supported on this platform"));
150 #endif
151
152 return make_number(curtime, result);
153 }
154
155 /*
156 * Returns 0 if successful in sleeping the requested time;
157 * returns -1 if there is no platform support, or if the sleep request
158 * did not complete successfully (perhaps interrupted)
159 */
160 static awk_value_t *
161 do_sleep(int nargs, awk_value_t *result, struct awk_ext_func *unused)
162 {
163 awk_value_t num;
164 double secs;
165 int rc;
166
167 assert(result != NULL);
168
169 if (! get_argument(0, AWK_NUMBER, &num)) {
170 update_ERRNO_string(_("sleep: missing required numeric argument"));
171 return make_number(-1, result);
172 }
173 secs = num.num_value;
174
175 if (secs < 0) {
176 update_ERRNO_string(_("sleep: argument is negative"));
177 return make_number(-1, result);
178 }
179
180 #if defined(HAVE_NANOSLEEP)
181 {
182 struct timespec req;
183
184 req.tv_sec = secs;
185 req.tv_nsec = (secs-(double)req.tv_sec)*1000000000.0;
186 if ((rc = nanosleep(&req,NULL)) < 0)
187 /* probably interrupted */
188 update_ERRNO_int(errno);
189 }
190 #elif defined(HAVE_SELECT)
191 {
192 struct timeval timeout;
193
194 timeout.tv_sec = secs;
195 timeout.tv_usec = (secs-(double)timeout.tv_sec)*1000000.0;
196 if ((rc = select(0,NULL,NULL,NULL,&timeout)) < 0)
197 /* probably interrupted */
198 update_ERRNO_int(errno);
199 }
200 #elif defined(HAVE_GETSYSTEMTIMEASFILETIME)
201 {
202 DWORD milliseconds = secs * 1000;
203
204 Sleep (milliseconds);
205 rc = 0;
206 }
207 #else
208 /* no way to sleep on this platform */
209 rc = -1;
210 update_ERRNO_string(_("sleep: not supported on this platform"));
211 #endif
212
213 return make_number(rc, result);
214 }
215
216 #ifndef HAVE_STRPTIME
217 #include "../missing_d/strptime.c"
218 #endif
219
220 /* do_strptime --- call strptime */
221
222 static awk_value_t *
223 do_strptime(int nargs, awk_value_t *result, struct awk_ext_func *unused)
224 {
225 awk_value_t string, format;
226
227 assert(result != NULL);
228 make_number(0.0, result);
229
230 if (do_lint) {
231 if (nargs == 0) {
232 lintwarn(ext_id, _("strptime: called with no arguments"));
233 make_number(-1.0, result);
234 goto done0;
235 }
236 }
237
238 /* string is first arg, format is second arg */
239 if (! get_argument(0, AWK_STRING, & string)) {
240 fprintf(stderr, _("do_strptime: argument 1 is not a string\n"));
241 errno = EINVAL;
242 goto done1;
243 }
244 if (! get_argument(1, AWK_STRING, & format)) {
245 fprintf(stderr, _("do_strptime: argument 2 is not a string\n"));
246 errno = EINVAL;
247 goto done1;
248 }
249
250 struct tm broken_time;
251 memset(& broken_time, 0, sizeof(broken_time));
252 broken_time.tm_isdst = -1;
253 if (strptime(string.str_value.str, format.str_value.str, & broken_time) == NULL) {
254 make_number(-1.0, result);
255 goto done0;
256 }
257
258 time_t epoch_time = mktime(& broken_time);
259 make_number((double) epoch_time, result);
260 goto done0;
261
262 done1:
263 update_ERRNO_int(errno);
264
265 done0:
266 return result;
267 }
268
269 static awk_ext_func_t func_table[] = {
270 { "gettimeofday", do_gettimeofday, 0, 0, awk_false, NULL },
271 { "sleep", do_sleep, 1, 1, awk_false, NULL },
272 { "strptime", do_strptime, 2, 2, awk_false, NULL },
273 };
274
275 /* define the dl_load function using the boilerplate macro */
276
277 dl_load_func(func_table, time, "")