Coverage Report

Created: 2025-08-09 06:54

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