-
Notifications
You must be signed in to change notification settings - Fork 2
/
lua_syscalls.c
147 lines (127 loc) · 2.91 KB
/
lua_syscalls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "lua_syscalls.h"
#include "utils.h"
#include "lualib.h"
#include "lauxlib.h"
#include "wapr_user.h"
int lusys_change_dir (lua_State *L)
{
apr_status_t rc;
const char *path = luaL_checkstring(L, 1);
rc = lkl_sys_chdir(path);
if (0 != rc)
{
lua_pushnil (L);
lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n",
path, lfd_apr_strerror_thunsafe(rc));
return 2;
}
else
{
lua_pushboolean (L, 1);
return 1;
}
}
/*
** This function returns the current directory
** If unable to get the current directory, it returns nil
** and a string describing the error
*/
int lusys_get_dir (lua_State *L)
{
char path[255+2];
apr_status_t rc;
rc = lkl_sys_getcwd(path, 255);
printf("wrapper_sys_getcwd ret [%d], [%s]\n", rc, path);
if (rc <= 0)
{
lua_pushnil(L);
lua_pushfstring(L, "getcwd failed because [%s]\n", lfd_apr_strerror_thunsafe(-rc));
return 2;
}
else
{
lua_pushstring(L, path);
return 1;
}
}
int lusys_mkdir (lua_State *L)
{
const char *path = luaL_checkstring (L, 1);
int fail;
__kernel_mode_t oldmask = lkl_sys_umask( (mode_t)0 );
fail = lkl_sys_mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
if (fail) {
lua_pushnil (L);
lua_pushfstring (L, "%s", lfd_apr_strerror_thunsafe(fail));
return 2;
}
lkl_sys_umask (oldmask);
lua_pushboolean (L, 1);
return 1;
}
int lusys_rmdir (lua_State *L)
{
const char *path = luaL_checkstring (L, 1);
int fail;
fail = lkl_sys_rmdir (path);
if (fail) {
lua_pushnil (L);
lua_pushfstring (L, "%s", lfd_apr_strerror_thunsafe(fail));
return 2;
}
lua_pushboolean (L, 1);
return 1;
}
/*
** Set access time and modification values for file
*/
int lusys_utime (lua_State *L)
{
const char *file = luaL_checkstring (L, 1);
struct __kernel_utimbuf utb, *buf;
apr_status_t rc;
if (lua_gettop (L) == 1) /* set to current date/time */
buf = NULL;
else
{
utb.actime = (time_t)luaL_optnumber (L, 2, 0);
utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
buf = &utb;
}
rc = lkl_sys_utime (file, buf);
if (0 != rc)
{
lua_pushnil (L);
lua_pushfstring (L, "%s", strerror (- rc));
return 2;
}
lua_pushboolean (L, 1);
return 1;
}
/*
** Set access time and modification values for file
*/
int lusys_utimes (lua_State *L)
{
const char *file = luaL_checkstring (L, 1);
struct __kernel_timeval utb, *buf;
apr_status_t rc;
if (lua_gettop (L) == 1) /* set to current date/time */
buf = NULL;
else
{
utb.tv_sec = (long)luaL_optnumber (L, 2, 0);
utb.tv_usec = (long)luaL_optnumber (L, 3, 0);
buf = &utb;
}
rc = lkl_sys_utimes (file, buf);
if (0 != rc)
{
lua_pushnil (L);
lua_pushfstring (L, "%s", strerror (- rc));
return 2;
}
lua_pushboolean (L, 1);
return 1;
}