1 /* $NetBSD: loslib.c,v 1.1.1.2 2012/03/15 00:08:13 alnsn Exp $ */
4 ** $Id: loslib.c,v 1.1.1.2 2012/03/15 00:08:13 alnsn Exp $
5 ** Standard Operating System library
6 ** See Copyright Notice in lua.h
25 static int os_pushresult (lua_State
*L
, int i
, const char *filename
) {
26 int en
= errno
; /* calls to Lua API may change this value */
28 lua_pushboolean(L
, 1);
33 lua_pushfstring(L
, "%s: %s", filename
, strerror(en
));
34 lua_pushinteger(L
, en
);
40 static int os_execute (lua_State
*L
) {
41 lua_pushinteger(L
, system(luaL_optstring(L
, 1, NULL
)));
46 static int os_remove (lua_State
*L
) {
47 const char *filename
= luaL_checkstring(L
, 1);
48 return os_pushresult(L
, remove(filename
) == 0, filename
);
52 static int os_rename (lua_State
*L
) {
53 const char *fromname
= luaL_checkstring(L
, 1);
54 const char *toname
= luaL_checkstring(L
, 2);
55 return os_pushresult(L
, rename(fromname
, toname
) == 0, fromname
);
59 static int os_tmpname (lua_State
*L
) {
60 char buff
[LUA_TMPNAMBUFSIZE
];
62 lua_tmpnam(buff
, err
);
64 return luaL_error(L
, "unable to generate a unique filename");
65 lua_pushstring(L
, buff
);
70 static int os_getenv (lua_State
*L
) {
71 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
76 static int os_clock (lua_State
*L
) {
77 lua_pushnumber(L
, ((lua_Number
)clock())/(lua_Number
)CLOCKS_PER_SEC
);
83 ** {======================================================
84 ** Time/Date operations
85 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
86 ** wday=%w+1, yday=%j, isdst=? }
87 ** =======================================================
90 static void setfield (lua_State
*L
, const char *key
, int value
) {
91 lua_pushinteger(L
, value
);
92 lua_setfield(L
, -2, key
);
95 static void setboolfield (lua_State
*L
, const char *key
, int value
) {
96 if (value
< 0) /* undefined? */
97 return; /* does not set field */
98 lua_pushboolean(L
, value
);
99 lua_setfield(L
, -2, key
);
102 static int getboolfield (lua_State
*L
, const char *key
) {
104 lua_getfield(L
, -1, key
);
105 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
111 static int getfield (lua_State
*L
, const char *key
, int d
) {
113 lua_getfield(L
, -1, key
);
114 if (lua_isnumber(L
, -1))
115 res
= (int)lua_tointeger(L
, -1);
118 return luaL_error(L
, "field " LUA_QS
" missing in date table", key
);
126 static int os_date (lua_State
*L
) {
127 const char *s
= luaL_optstring(L
, 1, "%c");
128 time_t t
= luaL_opt(L
, (time_t)luaL_checknumber
, 2, time(NULL
));
130 if (*s
== '!') { /* UTC? */
136 if (stm
== NULL
) /* invalid date? */
138 else if (strcmp(s
, "*t") == 0) {
139 lua_createtable(L
, 0, 9); /* 9 = number of fields */
140 setfield(L
, "sec", stm
->tm_sec
);
141 setfield(L
, "min", stm
->tm_min
);
142 setfield(L
, "hour", stm
->tm_hour
);
143 setfield(L
, "day", stm
->tm_mday
);
144 setfield(L
, "month", stm
->tm_mon
+1);
145 setfield(L
, "year", stm
->tm_year
+1900);
146 setfield(L
, "wday", stm
->tm_wday
+1);
147 setfield(L
, "yday", stm
->tm_yday
+1);
148 setboolfield(L
, "isdst", stm
->tm_isdst
);
153 cc
[0] = '%'; cc
[2] = '\0';
154 luaL_buffinit(L
, &b
);
156 if (*s
!= '%' || *(s
+ 1) == '\0') /* no conversion specifier? */
157 luaL_addchar(&b
, *s
);
160 char buff
[200]; /* should be big enough for any conversion result */
162 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
163 luaL_addlstring(&b
, buff
, reslen
);
172 static int os_time (lua_State
*L
) {
174 if (lua_isnoneornil(L
, 1)) /* called without args? */
175 t
= time(NULL
); /* get current time */
178 luaL_checktype(L
, 1, LUA_TTABLE
);
179 lua_settop(L
, 1); /* make sure table is at the top */
180 ts
.tm_sec
= getfield(L
, "sec", 0);
181 ts
.tm_min
= getfield(L
, "min", 0);
182 ts
.tm_hour
= getfield(L
, "hour", 12);
183 ts
.tm_mday
= getfield(L
, "day", -1);
184 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
185 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
186 ts
.tm_isdst
= getboolfield(L
, "isdst");
189 if (t
== (time_t)(-1))
192 lua_pushnumber(L
, (lua_Number
)t
);
197 static int os_difftime (lua_State
*L
) {
198 lua_pushnumber(L
, difftime((time_t)(luaL_checknumber(L
, 1)),
199 (time_t)(luaL_optnumber(L
, 2, 0))));
203 /* }====================================================== */
206 static int os_setlocale (lua_State
*L
) {
207 static const int cat
[] = {LC_ALL
, LC_COLLATE
, LC_CTYPE
, LC_MONETARY
,
208 LC_NUMERIC
, LC_TIME
};
209 static const char *const catnames
[] = {"all", "collate", "ctype", "monetary",
210 "numeric", "time", NULL
};
211 const char *l
= luaL_optstring(L
, 1, NULL
);
212 int op
= luaL_checkoption(L
, 2, "all", catnames
);
213 lua_pushstring(L
, setlocale(cat
[op
], l
));
218 static int os_exit (lua_State
*L
) {
219 exit(luaL_optint(L
, 1, EXIT_SUCCESS
));
222 static const luaL_Reg syslib
[] = {
225 {"difftime", os_difftime
},
226 {"execute", os_execute
},
228 {"getenv", os_getenv
},
229 {"remove", os_remove
},
230 {"rename", os_rename
},
231 {"setlocale", os_setlocale
},
233 {"tmpname", os_tmpname
},
237 /* }====================================================== */
241 LUALIB_API
int luaopen_os (lua_State
*L
) {
242 luaL_register(L
, LUA_OSLIBNAME
, syslib
);