2 Unix SMB/CIFS implementation.
4 provide access to system functions
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "system/time.h"
28 #include "system/network.h"
29 #include "netif/netif.h"
32 return the list of configured network interfaces
34 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
36 int i, count = iface_count();
37 struct MprVar ret = mprArray("interfaces");
38 for (i=0;i<count;i++) {
39 mprAddArray(&ret, i, mprString(iface_n_ip(i)));
46 return the hostname from gethostname()
48 static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
51 if (gethostname(name, sizeof(name)-1) == -1) {
52 ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
55 mpr_Return(eid, mprString(name));
61 return current time as a 64 bit nttime value
63 static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
65 struct timeval tv = timeval_current();
66 struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
72 return time as a 64 bit nttime value from a 32 bit time_t value
74 static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
78 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
79 ejsSetErrorMsg(eid, "sys_unix2nttime invalid arguments");
82 unix_to_nt_time(&nt, mprVarToNumber(argv[0]));
83 v = mprCreateNumberVar(nt);
89 return the given time as a gmtime structure
91 static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
96 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
97 ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
100 t = nt_time_to_unix(mprVarToNumber(argv[0]));
103 mpr_Return(eid, mprCreateUndefinedVar());
106 ret = mprObject("gmtime");
107 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
118 mpr_Return(eid, ret);
123 return a ldap time string from a nttime
125 static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
129 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
130 ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
133 t = nt_time_to_unix(mprVarToNumber(argv[0]));
134 s = ldb_timestring(mprMemCtx(), t);
135 mpr_Return(eid, mprString(s));
141 return a http time string from a nttime
143 static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv)
147 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
148 ejsSetErrorMsg(eid, "sys_httptime invalid arguments");
151 t = nt_time_to_unix(mprVarToNumber(argv[0]));
152 s = http_timestring(mprMemCtx(), t);
153 mpr_Return(eid, mprString(s));
160 ok = sys.unlink(fname);
162 static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
166 ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
169 ret = unlink(argv[0]);
170 mpr_Return(eid, mprCreateBoolVar(ret == 0));
175 load a file as a string
177 string = sys.file_load(filename);
179 static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
183 ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
187 s = file_load(argv[0], NULL, mprMemCtx());
188 mpr_Return(eid, mprString(s));
194 save a file from a string
196 ok = sys.file_save(filename, str);
198 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
202 ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
205 ret = file_save(argv[0], argv[1], strlen(argv[1]));
206 mpr_Return(eid, mprCreateBoolVar(ret));
212 return fields of a stat() call
214 static struct MprVar mpr_stat(struct stat *st)
217 ret = mprObject("stat");
219 #define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n))
239 var st = sys.stat(filename);
240 returns an object containing struct stat elements
242 static int ejs_sys_stat(MprVarHandle eid, int argc, char **argv)
245 /* validate arguments */
247 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
250 if (stat(argv[0], &st) != 0) {
251 mpr_Return(eid, mprCreateUndefinedVar());
253 mpr_Return(eid, mpr_stat(&st));
260 var st = sys.lstat(filename);
261 returns an object containing struct stat elements
263 static int ejs_sys_lstat(MprVarHandle eid, int argc, char **argv)
266 /* validate arguments */
268 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
271 if (lstat(argv[0], &st) != 0) {
272 mpr_Return(eid, mprCreateUndefinedVar());
274 mpr_Return(eid, mpr_stat(&st));
282 var z = sys.bitAND(x, 0x70);
284 static int ejs_sys_bitAND(MprVarHandle eid, int argc, struct MprVar **argv)
289 !mprVarIsNumber(argv[0]->type) ||
290 !mprVarIsNumber(argv[1]->type)) {
291 ejsSetErrorMsg(eid, "bitand invalid arguments");
294 x = mprToInt(argv[0]);
295 y = mprToInt(argv[1]);
298 mpr_Return(eid, mprCreateIntegerVar(z));
305 var z = sys.bitOR(x, 0x70);
307 static int ejs_sys_bitOR(MprVarHandle eid, int argc, struct MprVar **argv)
312 !mprVarIsNumber(argv[0]->type) ||
313 !mprVarIsNumber(argv[1]->type)) {
314 ejsSetErrorMsg(eid, "bitand invalid arguments");
317 x = mprToInt(argv[0]);
318 y = mprToInt(argv[1]);
321 mpr_Return(eid, mprCreateIntegerVar(z));
326 initialise sys ejs subsystem
328 static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
330 struct MprVar *obj = mprInitObject(eid, "sys", argc, argv);
332 mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
333 mprSetCFunction(obj, "hostname", ejs_sys_hostname);
334 mprSetCFunction(obj, "nttime", ejs_sys_nttime);
335 mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
336 mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
337 mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
338 mprSetCFunction(obj, "httptime", ejs_sys_httptime);
339 mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
340 mprSetStringCFunction(obj, "file_load", ejs_sys_file_load);
341 mprSetStringCFunction(obj, "file_save", ejs_sys_file_save);
342 mprSetStringCFunction(obj, "stat", ejs_sys_stat);
343 mprSetStringCFunction(obj, "lstat", ejs_sys_lstat);
344 mprSetCFunction(obj, "bitAND", ejs_sys_bitAND);
345 mprSetCFunction(obj, "bitOR", ejs_sys_bitOR);
352 setup C functions that be called from ejs
354 void smb_setup_ejs_system(void)
356 ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);