Coverage Report

Created: 2025-07-11 06:33

/src/testdir/build/lua-master/source/loslib.c
Line
Count
Source (jump to first uncovered line)
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)  /* ANSI C 89 (only 1-char options) */
38
#define LUA_STRFTIMEOPTIONS  "aAbBcdHIjmMpSUwWxXyYZ%"
39
#else  /* C99 specification */
40
130k
#define LUA_STRFTIMEOPTIONS  "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
41
130k
    "||" "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
928
#define l_timet     lua_Integer
60
83.2k
#define l_pushtime(L,t)   lua_pushinteger(L,(lua_Integer)(t))
61
928
#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
324
#define l_gmtime(t,r)   gmtime_r(t,r)
81
3.05k
#define l_localtime(t,r)  localtime_r(t,r)
82
83
#else       /* }{ */
84
85
/* ISO C definitions */
86
#define l_gmtime(t,r)   ((void)(r)->tm_sec, gmtime(t))
87
#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
294k
#define LUA_TMPNAMTEMPLATE  "/tmp/lua_XXXXXX"
113
#endif
114
115
294k
#define lua_tmpnam(b,e) { \
116
294k
        strcpy(b, LUA_TMPNAMTEMPLATE); \
117
294k
        e = mkstemp(b); \
118
294k
        if (e != -1) close(e); \
119
294k
        e = (e == -1); }
120
121
#else       /* }{ */
122
123
/* ISO C definitions */
124
#define LUA_TMPNAMBUFSIZE L_tmpnam
125
#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
152
#define l_system(cmd) system(cmd)  /* default definition */
139
#endif
140
#endif
141
142
143
152
static int os_execute (lua_State *L) {
144
152
  const char *cmd = luaL_optstring(L, 1, NULL);
145
152
  int stat;
146
152
  errno = 0;
147
152
  stat = l_system(cmd);
148
152
  if (cmd != NULL)
149
151
    return luaL_execresult(L, stat);
150
1
  else {
151
1
    lua_pushboolean(L, stat);  /* true if there is a shell */
152
1
    return 1;
153
1
  }
154
152
}
155
156
157
3.83k
static int os_remove (lua_State *L) {
158
3.83k
  const char *filename = luaL_checkstring(L, 1);
159
3.83k
  errno = 0;
160
3.83k
  return luaL_fileresult(L, remove(filename) == 0, filename);
161
3.83k
}
162
163
164
3.45k
static int os_rename (lua_State *L) {
165
3.45k
  const char *fromname = luaL_checkstring(L, 1);
166
3.45k
  const char *toname = luaL_checkstring(L, 2);
167
3.45k
  errno = 0;
168
3.45k
  return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
169
3.45k
}
170
171
172
294k
static int os_tmpname (lua_State *L) {
173
294k
  char buff[LUA_TMPNAMBUFSIZE];
174
294k
  int err;
175
294k
  lua_tmpnam(buff, err);
176
294k
  if (l_unlikely(err))
177
0
    return luaL_error(L, "unable to generate a unique filename");
178
294k
  lua_pushstring(L, buff);
179
294k
  return 1;
180
294k
}
181
182
183
4.70k
static int os_getenv (lua_State *L) {
184
4.70k
  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
185
4.70k
  return 1;
186
4.70k
}
187
188
189
217
static int os_clock (lua_State *L) {
190
217
  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
191
217
  return 1;
192
217
}
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
665k
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
665k
  lua_pushinteger(L, (lua_Integer)value + delta);
218
665k
  lua_setfield(L, -2, key);
219
665k
}
220
221
222
83.2k
static void setboolfield (lua_State *L, const char *key, int value) {
223
83.2k
  if (value < 0)  /* undefined? */
224
0
    return;  /* does not set field */
225
83.2k
  lua_pushboolean(L, value);
226
83.2k
  lua_setfield(L, -2, key);
227
83.2k
}
228
229
230
/*
231
** Set all fields from structure 'tm' in the table on top of the stack
232
*/
233
83.2k
static void setallfields (lua_State *L, struct tm *stm) {
234
83.2k
  setfield(L, "year", stm->tm_year, 1900);
235
83.2k
  setfield(L, "month", stm->tm_mon, 1);
236
83.2k
  setfield(L, "day", stm->tm_mday, 0);
237
83.2k
  setfield(L, "hour", stm->tm_hour, 0);
238
83.2k
  setfield(L, "min", stm->tm_min, 0);
239
83.2k
  setfield(L, "sec", stm->tm_sec, 0);
240
83.2k
  setfield(L, "yday", stm->tm_yday, 1);
241
83.2k
  setfield(L, "wday", stm->tm_wday, 1);
242
83.2k
  setboolfield(L, "isdst", stm->tm_isdst);
243
83.2k
}
244
245
246
83.1k
static int getboolfield (lua_State *L, const char *key) {
247
83.1k
  int res;
248
83.1k
  res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
249
83.1k
  lua_pop(L, 1);
250
83.1k
  return res;
251
83.1k
}
252
253
254
498k
static int getfield (lua_State *L, const char *key, int d, int delta) {
255
498k
  int isnum;
256
498k
  int t = lua_getfield(L, -1, key);  /* get field and its type */
257
498k
  lua_Integer res = lua_tointegerx(L, -1, &isnum);
258
498k
  if (!isnum) {  /* field is not an integer? */
259
22.8k
    if (l_unlikely(t != LUA_TNIL))  /* some other value? */
260
0
      return luaL_error(L, "field '%s' is not an integer", key);
261
22.8k
    else if (l_unlikely(d < 0))  /* absent field; no default? */
262
1
      return luaL_error(L, "field '%s' missing in date table", key);
263
22.8k
    res = d;
264
22.8k
  }
265
475k
  else {
266
475k
    if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
267
0
      return luaL_error(L, "field '%s' is out-of-bound", key);
268
475k
    res -= delta;
269
475k
  }
270
498k
  lua_pop(L, 1);
271
498k
  return (int)res;
272
498k
}
273
274
275
static const char *checkoption (lua_State *L, const char *conv,
276
130k
                                size_t convlen, char *buff) {
277
130k
  const char *option = LUA_STRFTIMEOPTIONS;
278
130k
  unsigned oplen = 1;  /* length of options being checked */
279
4.78M
  for (; *option != '\0' && oplen <= convlen; option += oplen) {
280
4.78M
    if (*option == '|')  /* next block? */
281
290
      oplen++;  /* will check options with next length (+1) */
282
4.78M
    else if (memcmp(conv, option, oplen) == 0) {  /* match? */
283
129k
      memcpy(buff, conv, oplen);  /* copy valid option to buffer */
284
129k
      buff[oplen] = '\0';
285
129k
      return conv + oplen;  /* return next item */
286
129k
    }
287
4.78M
  }
288
129
  luaL_argerror(L, 1,
289
129
    lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
290
129
  return conv;  /* to avoid warnings */
291
130k
}
292
293
294
928
static time_t l_checktime (lua_State *L, int arg) {
295
928
  l_timet t = l_gettime(L, arg);
296
928
  luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
297
928
  return (time_t)t;
298
928
}
299
300
301
/* maximum size for an individual 'strftime' item */
302
260k
#define SIZETIMEFMT 250
303
304
305
3.38k
static int os_date (lua_State *L) {
306
3.38k
  size_t slen;
307
3.38k
  const char *s = luaL_optlstring(L, 1, "%c", &slen);
308
3.38k
  time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
309
3.38k
  const char *se = s + slen;  /* 's' end */
310
3.38k
  struct tm tmr, *stm;
311
3.38k
  if (*s == '!') {  /* UTC? */
312
324
    stm = l_gmtime(&t, &tmr);
313
324
    s++;  /* skip '!' */
314
324
  }
315
3.05k
  else
316
3.05k
    stm = l_localtime(&t, &tmr);
317
3.38k
  if (stm == NULL)  /* invalid date? */
318
0
    return luaL_error(L,
319
0
                 "date result cannot be represented in this installation");
320
3.38k
  if (strcmp(s, "*t") == 0) {
321
77
    lua_createtable(L, 0, 9);  /* 9 = number of fields */
322
77
    setallfields(L, stm);
323
77
  }
324
3.30k
  else {
325
3.30k
    char cc[4];  /* buffer for individual conversion specifiers */
326
3.30k
    luaL_Buffer b;
327
3.30k
    cc[0] = '%';
328
3.30k
    luaL_buffinit(L, &b);
329
1.20M
    while (s < se) {
330
1.20M
      if (*s != '%')  /* not a conversion specifier? */
331
1.07M
        luaL_addchar(&b, *s++);
332
130k
      else {
333
130k
        size_t reslen;
334
130k
        char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
335
130k
        s++;  /* skip '%' */
336
        /* copy specifier to 'cc' */
337
130k
        s = checkoption(L, s, ct_diff2sz(se - s), cc + 1);
338
130k
        reslen = strftime(buff, SIZETIMEFMT, cc, stm);
339
130k
        luaL_addsize(&b, reslen);
340
130k
      }
341
1.20M
    }
342
3.30k
    luaL_pushresult(&b);
343
3.30k
  }
344
3.38k
  return 1;
345
3.38k
}
346
347
348
83.2k
static int os_time (lua_State *L) {
349
83.2k
  time_t t;
350
83.2k
  if (lua_isnoneornil(L, 1))  /* called without args? */
351
36
    t = time(NULL);  /* get current time */
352
83.1k
  else {
353
83.1k
    struct tm ts;
354
83.1k
    luaL_checktype(L, 1, LUA_TTABLE);
355
83.1k
    lua_settop(L, 1);  /* make sure table is at the top */
356
83.1k
    ts.tm_year = getfield(L, "year", -1, 1900);
357
83.1k
    ts.tm_mon = getfield(L, "month", -1, 1);
358
83.1k
    ts.tm_mday = getfield(L, "day", -1, 0);
359
83.1k
    ts.tm_hour = getfield(L, "hour", 12, 0);
360
83.1k
    ts.tm_min = getfield(L, "min", 0, 0);
361
83.1k
    ts.tm_sec = getfield(L, "sec", 0, 0);
362
83.1k
    ts.tm_isdst = getboolfield(L, "isdst");
363
83.1k
    t = mktime(&ts);
364
83.1k
    setallfields(L, &ts);  /* update fields with normalized values */
365
83.1k
  }
366
83.2k
  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
83.2k
  l_pushtime(L, t);
370
83.2k
  return 1;
371
83.2k
}
372
373
374
872
static int os_difftime (lua_State *L) {
375
872
  time_t t1 = l_checktime(L, 1);
376
872
  time_t t2 = l_checktime(L, 2);
377
872
  lua_pushnumber(L, (lua_Number)difftime(t1, t2));
378
872
  return 1;
379
872
}
380
381
/* }====================================================== */
382
383
384
144k
static int os_setlocale (lua_State *L) {
385
144k
  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
386
144k
                      LC_NUMERIC, LC_TIME};
387
144k
  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
388
144k
     "numeric", "time", NULL};
389
144k
  const char *l = luaL_optstring(L, 1, NULL);
390
144k
  int op = luaL_checkoption(L, 2, "all", catnames);
391
144k
  lua_pushstring(L, setlocale(cat[op], l));
392
144k
  return 1;
393
144k
}
394
395
396
19
static int os_exit (lua_State *L) {
397
19
  int status;
398
19
  if (lua_isboolean(L, 1))
399
0
    status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
400
19
  else
401
19
    status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
402
19
  if (lua_toboolean(L, 2))
403
0
    lua_close(L);
404
19
  if (L) exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
405
19
  return 0;
406
19
}
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
29.9k
LUAMOD_API int luaopen_os (lua_State *L) {
429
29.9k
  luaL_newlib(L, syslib);
430
29.9k
  return 1;
431
29.9k
}
432