annotate lib/lua/lua-5.2.2/loslib.c @ 2222:66bd74aa9f5e

smk video draw
author Ritor1
date Tue, 18 Feb 2014 19:43:54 +0600
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: loslib.c,v 1.40 2012/10/19 15:54:02 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Standard Operating System library
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <errno.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <locale.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #include <time.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #define loslib_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #define LUA_LIB
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lauxlib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lualib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 ** list of valid conversion specifiers for the 'strftime' function
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #if !defined(LUA_STRFTIMEOPTIONS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 #if !defined(LUA_USE_POSIX)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 #else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 #define LUA_STRFTIMEOPTIONS \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 "", "E", "cCxXyY", \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 "O", "deHImMSuUVwWy" }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 ** By default, Lua uses tmpnam except when POSIX is available, where it
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 ** uses mkstemp.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 #if defined(LUA_USE_MKSTEMP)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 #include <unistd.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 #define LUA_TMPNAMBUFSIZE 32
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 #define lua_tmpnam(b,e) { \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 strcpy(b, "/tmp/lua_XXXXXX"); \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 e = mkstemp(b); \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 if (e != -1) close(e); \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 e = (e == -1); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 #elif !defined(lua_tmpnam)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 #define LUA_TMPNAMBUFSIZE L_tmpnam
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 ** By default, Lua uses gmtime/localtime, except when POSIX is available,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 ** where it uses gmtime_r/localtime_r
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 #if defined(LUA_USE_GMTIME_R)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 #define l_gmtime(t,r) gmtime_r(t,r)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 #define l_localtime(t,r) localtime_r(t,r)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 #elif !defined(l_gmtime)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 #define l_gmtime(t,r) ((void)r, gmtime(t))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 #define l_localtime(t,r) ((void)r, localtime(t))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 static int os_execute (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 const char *cmd = luaL_optstring(L, 1, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 int stat = system(cmd);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 if (cmd != NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 return luaL_execresult(L, stat);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 lua_pushboolean(L, stat); /* true if there is a shell */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 static int os_remove (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 const char *filename = luaL_checkstring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 return luaL_fileresult(L, remove(filename) == 0, filename);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 static int os_rename (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 const char *fromname = luaL_checkstring(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 const char *toname = luaL_checkstring(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 static int os_tmpname (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 char buff[LUA_TMPNAMBUFSIZE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 int err;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 lua_tmpnam(buff, err);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 if (err)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 return luaL_error(L, "unable to generate a unique filename");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 lua_pushstring(L, buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 static int os_getenv (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 static int os_clock (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 ** Time/Date operations
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 ** wday=%w+1, yday=%j, isdst=? }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 static void setfield (lua_State *L, const char *key, int value) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 lua_pushinteger(L, value);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 lua_setfield(L, -2, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 static void setboolfield (lua_State *L, const char *key, int value) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 if (value < 0) /* undefined? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 return; /* does not set field */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 lua_pushboolean(L, value);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 lua_setfield(L, -2, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 static int getboolfield (lua_State *L, const char *key) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 int res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 lua_getfield(L, -1, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 lua_pop(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 return res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 static int getfield (lua_State *L, const char *key, int d) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 int res, isnum;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 lua_getfield(L, -1, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 res = (int)lua_tointegerx(L, -1, &isnum);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 if (!isnum) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 if (d < 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 return luaL_error(L, "field " LUA_QS " missing in date table", key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 res = d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 lua_pop(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 return res;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 static const char *checkoption (lua_State *L, const char *conv, char *buff) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 static const char *const options[] = LUA_STRFTIMEOPTIONS;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 unsigned int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 buff[1] = *conv;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 buff[2] = '\0'; /* end buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 return conv + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 else if (*(conv + 1) != '\0' &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 strchr(options[i + 1], *(conv + 1)) != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 buff[2] = *(conv + 1); /* valid two-char conversion specifier */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 buff[3] = '\0'; /* end buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 return conv + 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 luaL_argerror(L, 1,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 return conv; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 static int os_date (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 const char *s = luaL_optstring(L, 1, "%c");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 struct tm tmr, *stm;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 if (*s == '!') { /* UTC? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 stm = l_gmtime(&t, &tmr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 s++; /* skip `!' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 stm = l_localtime(&t, &tmr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 if (stm == NULL) /* invalid date? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 lua_pushnil(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 else if (strcmp(s, "*t") == 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 lua_createtable(L, 0, 9); /* 9 = number of fields */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 setfield(L, "sec", stm->tm_sec);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 setfield(L, "min", stm->tm_min);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 setfield(L, "hour", stm->tm_hour);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 setfield(L, "day", stm->tm_mday);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 setfield(L, "month", stm->tm_mon+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 setfield(L, "year", stm->tm_year+1900);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 setfield(L, "wday", stm->tm_wday+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 setfield(L, "yday", stm->tm_yday+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 setboolfield(L, "isdst", stm->tm_isdst);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 char cc[4];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 luaL_Buffer b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 cc[0] = '%';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 luaL_buffinit(L, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 while (*s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 if (*s != '%') /* no conversion specifier? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 luaL_addchar(&b, *s++);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 size_t reslen;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 char buff[200]; /* should be big enough for any conversion result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 s = checkoption(L, s + 1, cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 reslen = strftime(buff, sizeof(buff), cc, stm);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 luaL_addlstring(&b, buff, reslen);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 luaL_pushresult(&b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 static int os_time (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 time_t t;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 if (lua_isnoneornil(L, 1)) /* called without args? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 t = time(NULL); /* get current time */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 struct tm ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 luaL_checktype(L, 1, LUA_TTABLE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 lua_settop(L, 1); /* make sure table is at the top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 ts.tm_sec = getfield(L, "sec", 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 ts.tm_min = getfield(L, "min", 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 ts.tm_hour = getfield(L, "hour", 12);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 ts.tm_mday = getfield(L, "day", -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 ts.tm_mon = getfield(L, "month", -1) - 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 ts.tm_year = getfield(L, "year", -1) - 1900;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 ts.tm_isdst = getboolfield(L, "isdst");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 t = mktime(&ts);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 if (t == (time_t)(-1))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 lua_pushnil(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 lua_pushnumber(L, (lua_Number)t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 static int os_difftime (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 (time_t)(luaL_optnumber(L, 2, 0))));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 static int os_setlocale (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 LC_NUMERIC, LC_TIME};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 "numeric", "time", NULL};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 const char *l = luaL_optstring(L, 1, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 int op = luaL_checkoption(L, 2, "all", catnames);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 lua_pushstring(L, setlocale(cat[op], l));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 static int os_exit (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 if (lua_isboolean(L, 1))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 status = luaL_optint(L, 1, EXIT_SUCCESS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 if (lua_toboolean(L, 2))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 lua_close(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 static const luaL_Reg syslib[] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 {"clock", os_clock},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 {"date", os_date},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 {"difftime", os_difftime},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 {"execute", os_execute},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 {"exit", os_exit},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 {"getenv", os_getenv},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 {"remove", os_remove},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 {"rename", os_rename},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 {"setlocale", os_setlocale},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 {"time", os_time},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 {"tmpname", os_tmpname},
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 {NULL, NULL}
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 LUAMOD_API int luaopen_os (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 luaL_newlib(L, syslib);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323