Coverage Report

Created: 2026-01-17 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/loslib.c
Line
Count
Source
1
/*
2
** $Id: loslib.c $
3
** Standard Operating System library
4
** See Copyright Notice in lua.h
5
*/
6
7
#define loslib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <errno.h>
14
#include <locale.h>
15
#include <stdlib.h>
16
#include <string.h>
17
#include <time.h>
18
19
#include "lua.h"
20
21
#include "lauxlib.h"
22
#include "lualib.h"
23
#include "llimits.h"
24
25
26
/*
27
** {==================================================================
28
** List of valid conversion specifiers for the 'strftime' function;
29
** options are grouped by length; group of length 2 start with '||'.
30
** ===================================================================
31
*/
32
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
33
34
#if defined(LUA_USE_WINDOWS)
35
#define LUA_STRFTIMEOPTIONS  "aAbBcdHIjmMpSUwWxXyYzZ%" \
36
    "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y"  /* two-char options */
37
#elif defined(LUA_USE_C89)  /* C89 (only 1-char options) */
38
#define LUA_STRFTIMEOPTIONS  "aAbBcdHIjmMpSUwWxXyYZ%"
39
#else  /* C99 specification */
40
95.4k
#define LUA_STRFTIMEOPTIONS  "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
41
95.4k
    "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy"  /* two-char options */
42
#endif
43
44
#endif          /* } */
45
/* }================================================================== */
46
47
48
/*
49
** {==================================================================
50
** Configuration for time-related stuff
51
** ===================================================================
52
*/
53
54
/*
55
** type to represent time_t in Lua
56
*/
57
#if !defined(LUA_NUMTIME) /* { */
58
59
5.57k
#define l_timet     lua_Integer
60
3.37k
#define l_pushtime(L,t)   lua_pushinteger(L,(lua_Integer)(t))
61
5.57k
#define l_gettime(L,arg)  luaL_checkinteger(L, arg)
62
63
#else       /* }{ */
64
65
#define l_timet     lua_Number
66
#define l_pushtime(L,t)   lua_pushnumber(L,(lua_Number)(t))
67
#define l_gettime(L,arg)  luaL_checknumber(L, arg)
68
69
#endif        /* } */
70
71
72
#if !defined(l_gmtime)    /* { */
73
/*
74
** By default, Lua uses gmtime/localtime, except when POSIX is available,
75
** where it uses gmtime_r/localtime_r
76
*/
77
78
#if defined(LUA_USE_POSIX)  /* { */
79
80
#define l_gmtime(t,r)   gmtime_r(t,r)
81
#define l_localtime(t,r)  localtime_r(t,r)
82
83
#else       /* }{ */
84
85
/* ISO C definitions */
86
690
#define l_gmtime(t,r)   ((void)(r)->tm_sec, gmtime(t))
87
4.75k
#define l_localtime(t,r)  ((void)(r)->tm_sec, localtime(t))
88
89
#endif        /* } */
90
91
#endif        /* } */
92
93
/* }================================================================== */
94
95
96
/*
97
** {==================================================================
98
** Configuration for 'tmpnam':
99
** By default, Lua uses tmpnam except when POSIX is available, where
100
** it uses mkstemp.
101
** ===================================================================
102
*/
103
#if !defined(lua_tmpnam)  /* { */
104
105
#if defined(LUA_USE_POSIX)  /* { */
106
107
#include <unistd.h>
108
109
#define LUA_TMPNAMBUFSIZE 32
110
111
#if !defined(LUA_TMPNAMTEMPLATE)
112
#define LUA_TMPNAMTEMPLATE  "/tmp/lua_XXXXXX"
113
#endif
114
115
#define lua_tmpnam(b,e) { \
116
        strcpy(b, LUA_TMPNAMTEMPLATE); \
117
        e = mkstemp(b); \
118
        if (e != -1) close(e); \
119
        e = (e == -1); }
120
121
#else       /* }{ */
122
123
/* ISO C definitions */
124
#define LUA_TMPNAMBUFSIZE L_tmpnam
125
15.1k
#define lua_tmpnam(b,e)   { e = (tmpnam(b) == NULL); }
126
127
#endif        /* } */
128
129
#endif        /* } */
130
/* }================================================================== */
131
132
133
#if !defined(l_system)
134
#if defined(LUA_USE_IOS)
135
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
136
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
137
#else
138
0
#define l_system(cmd) system(cmd)  /* default definition */
139
#endif
140
#endif
141
142
143
0
static int os_execute (lua_State *L) {
144
0
  const char *cmd = luaL_optstring(L, 1, NULL);
145
0
  int stat;
146
0
  errno = 0;
147
0
  stat = l_system(cmd);
148
0
  if (cmd != NULL)
149
0
    return luaL_execresult(L, stat);
150
0
  else {
151
0
    lua_pushboolean(L, stat);  /* true if there is a shell */
152
0
    return 1;
153
0
  }
154
0
}
155
156
157
2.41k
static int os_remove (lua_State *L) {
158
2.41k
  const char *filename = luaL_checkstring(L, 1);
159
2.41k
  errno = 0;
160
2.41k
  return luaL_fileresult(L, remove(filename) == 0, filename);
161
2.41k
}
162
163
164
1.05k
static int os_rename (lua_State *L) {
165
1.05k
  const char *fromname = luaL_checkstring(L, 1);
166
1.05k
  const char *toname = luaL_checkstring(L, 2);
167
1.05k
  errno = 0;
168
1.05k
  return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
169
1.05k
}
170
171
172
15.1k
static int os_tmpname (lua_State *L) {
173
15.1k
  char buff[LUA_TMPNAMBUFSIZE];
174
15.1k
  int err;
175
15.1k
  lua_tmpnam(buff, err);
176
15.1k
  if (l_unlikely(err))
177
0
    return luaL_error(L, "unable to generate a unique filename");
178
15.1k
  lua_pushstring(L, buff);
179
15.1k
  return 1;
180
15.1k
}
181
182
183
2.23k
static int os_getenv (lua_State *L) {
184
2.23k
  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
185
2.23k
  return 1;
186
2.23k
}
187
188
189
19.1k
static int os_clock (lua_State *L) {
190
19.1k
  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
191
19.1k
  return 1;
192
19.1k
}
193
194
195
/*
196
** {======================================================
197
** Time/Date operations
198
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
199
**   wday=%w+1, yday=%j, isdst=? }
200
** =======================================================
201
*/
202
203
/*
204
** About the overflow check: an overflow cannot occur when time
205
** is represented by a lua_Integer, because either lua_Integer is
206
** large enough to represent all int fields or it is not large enough
207
** to represent a time that cause a field to overflow.  However, if
208
** times are represented as doubles and lua_Integer is int, then the
209
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
210
** to compute the year.
211
*/
212
2.90k
static void setfield (lua_State *L, const char *key, int value, int delta) {
213
  #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
214
    if (l_unlikely(value > LUA_MAXINTEGER - delta))
215
      luaL_error(L, "field '%s' is out-of-bound", key);
216
  #endif
217
2.90k
  lua_pushinteger(L, (lua_Integer)value + delta);
218
2.90k
  lua_setfield(L, -2, key);
219
2.90k
}
220
221
222
363
static void setboolfield (lua_State *L, const char *key, int value) {
223
363
  if (value < 0)  /* undefined? */
224
0
    return;  /* does not set field */
225
363
  lua_pushboolean(L, value);
226
363
  lua_setfield(L, -2, key);
227
363
}
228
229
230
/*
231
** Set all fields from structure 'tm' in the table on top of the stack
232
*/
233
363
static void setallfields (lua_State *L, struct tm *stm) {
234
363
  setfield(L, "year", stm->tm_year, 1900);
235
363
  setfield(L, "month", stm->tm_mon, 1);
236
363
  setfield(L, "day", stm->tm_mday, 0);
237
363
  setfield(L, "hour", stm->tm_hour, 0);
238
363
  setfield(L, "min", stm->tm_min, 0);
239
363
  setfield(L, "sec", stm->tm_sec, 0);
240
363
  setfield(L, "yday", stm->tm_yday, 1);
241
363
  setfield(L, "wday", stm->tm_wday, 1);
242
363
  setboolfield(L, "isdst", stm->tm_isdst);
243
363
}
244
245
246
333
static int getboolfield (lua_State *L, const char *key) {
247
333
  int res;
248
333
  res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
249
333
  lua_pop(L, 1);
250
333
  return res;
251
333
}
252
253
254
5.90k
static int getfield (lua_State *L, const char *key, int d, int delta) {
255
5.90k
  int isnum;
256
5.90k
  int t = lua_getfield(L, -1, key);  /* get field and its type */
257
5.90k
  lua_Integer res = lua_tointegerx(L, -1, &isnum);
258
5.90k
  if (!isnum) {  /* field is not an integer? */
259
1.87k
    if (l_unlikely(t != LUA_TNIL))  /* some other value? */
260
0
      return luaL_error(L, "field '%s' is not an integer", key);
261
1.87k
    else if (l_unlikely(d < 0))  /* absent field; no default? */
262
888
      return luaL_error(L, "field '%s' missing in date table", key);
263
990
    res = d;
264
990
  }
265
4.02k
  else {
266
4.02k
    if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
267
2.13k
      return luaL_error(L, "field '%s' is out-of-bound", key);
268
1.89k
    res -= delta;
269
1.89k
  }
270
2.88k
  lua_pop(L, 1);
271
2.88k
  return (int)res;
272
5.90k
}
273
274
275
static const char *checkoption (lua_State *L, const char *conv,
276
95.4k
                                size_t convlen, char *buff) {
277
95.4k
  const char *option = LUA_STRFTIMEOPTIONS;
278
95.4k
  unsigned oplen = 1;  /* length of options being checked */
279
3.42M
  for (; *option != '\0' && oplen <= convlen; option += oplen) {
280
3.42M
    if (*option == '|')  /* next block? */
281
2.18k
      oplen++;  /* will check options with next length (+1) */
282
3.42M
    else if (memcmp(conv, option, oplen) == 0) {  /* match? */
283
94.2k
      memcpy(buff, conv, oplen);  /* copy valid option to buffer */
284
94.2k
      buff[oplen] = '\0';
285
94.2k
      return conv + oplen;  /* return next item */
286
94.2k
    }
287
3.42M
  }
288
1.18k
  luaL_argerror(L, 1,
289
1.18k
    lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
290
1.18k
  return conv;  /* to avoid warnings */
291
95.4k
}
292
293
294
5.57k
static time_t l_checktime (lua_State *L, int arg) {
295
5.57k
  l_timet t = l_gettime(L, arg);
296
5.57k
  luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
297
5.57k
  return (time_t)t;
298
5.57k
}
299
300
301
/* maximum size for an individual 'strftime' item */
302
190k
#define SIZETIMEFMT 250
303
304
305
5.44k
static int os_date (lua_State *L) {
306
5.44k
  size_t slen;
307
5.44k
  const char *s = luaL_optlstring(L, 1, "%c", &slen);
308
5.44k
  time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
309
5.44k
  const char *se = s + slen;  /* 's' end */
310
5.44k
  struct tm tmr, *stm;
311
5.44k
  if (*s == '!') {  /* UTC? */
312
690
    stm = l_gmtime(&t, &tmr);
313
690
    s++;  /* skip '!' */
314
690
  }
315
4.75k
  else
316
4.75k
    stm = l_localtime(&t, &tmr);
317
5.44k
  if (stm == NULL)  /* invalid date? */
318
0
    return luaL_error(L,
319
0
                 "date result cannot be represented in this installation");
320
5.44k
  if (strcmp(s, "*t") == 0) {
321
30
    lua_createtable(L, 0, 9);  /* 9 = number of fields */
322
30
    setallfields(L, stm);
323
30
  }
324
5.41k
  else {
325
5.41k
    char cc[4];  /* buffer for individual conversion specifiers */
326
5.41k
    luaL_Buffer b;
327
5.41k
    cc[0] = '%';
328
5.41k
    luaL_buffinit(L, &b);
329
6.86M
    while (s < se) {
330
6.86M
      if (*s != '%')  /* not a conversion specifier? */
331
6.76M
        luaL_addchar(&b, *s++);
332
95.4k
      else {
333
95.4k
        size_t reslen;
334
95.4k
        char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
335
95.4k
        s++;  /* skip '%' */
336
        /* copy specifier to 'cc' */
337
95.4k
        s = checkoption(L, s, ct_diff2sz(se - s), cc + 1);
338
95.4k
        reslen = strftime(buff, SIZETIMEFMT, cc, stm);
339
95.4k
        luaL_addsize(&b, reslen);
340
95.4k
      }
341
6.86M
    }
342
5.41k
    luaL_pushresult(&b);
343
5.41k
  }
344
5.44k
  return 1;
345
5.44k
}
346
347
348
3.37k
static int os_time (lua_State *L) {
349
3.37k
  time_t t;
350
3.37k
  if (lua_isnoneornil(L, 1))  /* called without args? */
351
21
    t = time(NULL);  /* get current time */
352
3.35k
  else {
353
3.35k
    struct tm ts;
354
3.35k
    luaL_checktype(L, 1, LUA_TTABLE);
355
3.35k
    lua_settop(L, 1);  /* make sure table is at the top */
356
3.35k
    ts.tm_year = getfield(L, "year", -1, 1900);
357
3.35k
    ts.tm_mon = getfield(L, "month", -1, 1);
358
3.35k
    ts.tm_mday = getfield(L, "day", -1, 0);
359
3.35k
    ts.tm_hour = getfield(L, "hour", 12, 0);
360
3.35k
    ts.tm_min = getfield(L, "min", 0, 0);
361
3.35k
    ts.tm_sec = getfield(L, "sec", 0, 0);
362
3.35k
    ts.tm_isdst = getboolfield(L, "isdst");
363
3.35k
    t = mktime(&ts);
364
3.35k
    setallfields(L, &ts);  /* update fields with normalized values */
365
3.35k
  }
366
3.37k
  if (t != (time_t)(l_timet)t || t == (time_t)(-1))
367
0
    return luaL_error(L,
368
0
                  "time result cannot be represented in this installation");
369
3.37k
  l_pushtime(L, t);
370
3.37k
  return 1;
371
3.37k
}
372
373
374
852
static int os_difftime (lua_State *L) {
375
852
  time_t t1 = l_checktime(L, 1);
376
852
  time_t t2 = l_checktime(L, 2);
377
852
  lua_pushnumber(L, (lua_Number)difftime(t1, t2));
378
852
  return 1;
379
852
}
380
381
/* }====================================================== */
382
383
384
6.70k
static int os_setlocale (lua_State *L) {
385
6.70k
  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
386
6.70k
                      LC_NUMERIC, LC_TIME};
387
6.70k
  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
388
6.70k
     "numeric", "time", NULL};
389
6.70k
  const char *l = luaL_optstring(L, 1, NULL);
390
6.70k
  int op = luaL_checkoption(L, 2, "all", catnames);
391
6.70k
  lua_pushstring(L, setlocale(cat[op], l));
392
6.70k
  return 1;
393
6.70k
}
394
395
396
0
static int os_exit (lua_State *L) {
397
0
  int status;
398
0
  if (lua_isboolean(L, 1))
399
0
    status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
400
0
  else
401
0
    status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
402
0
  if (lua_toboolean(L, 2))
403
0
    lua_close(L);
404
0
  if (L) exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
405
0
  return 0;
406
0
}
407
408
409
static const luaL_Reg syslib[] = {
410
  {"clock",     os_clock},
411
  {"date",      os_date},
412
  {"difftime",  os_difftime},
413
  {"execute",   os_execute},
414
  {"exit",      os_exit},
415
  {"getenv",    os_getenv},
416
  {"remove",    os_remove},
417
  {"rename",    os_rename},
418
  {"setlocale", os_setlocale},
419
  {"time",      os_time},
420
  {"tmpname",   os_tmpname},
421
  {NULL, NULL}
422
};
423
424
/* }====================================================== */
425
426
427
428
8.95k
LUAMOD_API int luaopen_os (lua_State *L) {
429
8.95k
  luaL_newlib(L, syslib);
430
8.95k
  return 1;
431
8.95k
}
432