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 "lib/socket/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 seconds and microseconds
63 static int ejs_sys_gettimeofday(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
65 struct timeval tv
= timeval_current();
66 struct MprVar v
= mprObject("timeval");
67 struct MprVar sec
= mprCreateIntegerVar(tv
.tv_sec
);
68 struct MprVar usec
= mprCreateIntegerVar(tv
.tv_usec
);
70 mprCreateProperty(&v
, "sec", &sec
);
71 mprCreateProperty(&v
, "usec", &usec
);
77 return current time as a 64 bit nttime value
79 static int ejs_sys_nttime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
81 struct timeval tv
= timeval_current();
82 struct MprVar v
= mprCreateNumberVar(timeval_to_nttime(&tv
));
88 return time as a 64 bit nttime value from a 32 bit time_t value
90 static int ejs_sys_unix2nttime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
94 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
95 ejsSetErrorMsg(eid
, "sys_unix2nttime invalid arguments");
98 unix_to_nt_time(&nt
, mprVarToNumber(argv
[0]));
99 v
= mprCreateNumberVar(nt
);
105 return the GMT time represented by the struct tm argument, as a time_t value
107 static int ejs_sys_gmmktime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
111 if (argc
!= 1 || !mprVarIsObject(argv
[0]->type
)) {
112 ejsSetErrorMsg(eid
, "sys_gmmktime invalid arguments");
117 #define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL))
129 mpr_Return(eid
, mprCreateIntegerVar(mktime(&tm
)));
134 return the given time as a gmtime structure
136 static int ejs_sys_gmtime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
141 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
142 ejsSetErrorMsg(eid
, "sys_gmtime invalid arguments");
145 t
= (time_t) mprVarToNumber(argv
[0]);
148 mpr_Return(eid
, mprCreateUndefinedVar());
151 ret
= mprObject("gmtime");
152 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
164 mpr_Return(eid
, ret
);
169 return the given NT time as a time_t value
171 static int ejs_sys_nttime2unix(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
175 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
176 ejsSetErrorMsg(eid
, "sys_ntgmtime invalid arguments");
179 t
= nt_time_to_unix(mprVarToNumber(argv
[0]));
180 v
= mprCreateNumberVar(t
);
186 return the given NT time as a gmtime structure
188 static int ejs_sys_ntgmtime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
193 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
194 ejsSetErrorMsg(eid
, "sys_ntgmtime invalid arguments");
197 t
= nt_time_to_unix(mprVarToNumber(argv
[0]));
200 mpr_Return(eid
, mprCreateUndefinedVar());
203 ret
= mprObject("gmtime");
204 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
216 mpr_Return(eid
, ret
);
221 return a ldap time string from a nttime
223 static int ejs_sys_ldaptime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
227 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
228 ejsSetErrorMsg(eid
, "sys_ldaptime invalid arguments");
231 t
= nt_time_to_unix(mprVarToNumber(argv
[0]));
232 s
= ldb_timestring(mprMemCtx(), t
);
233 mpr_Return(eid
, mprString(s
));
239 return a http time string from a nttime
241 static int ejs_sys_httptime(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
245 if (argc
!= 1 || !mprVarIsNumber(argv
[0]->type
)) {
246 ejsSetErrorMsg(eid
, "sys_httptime invalid arguments");
249 t
= nt_time_to_unix(mprVarToNumber(argv
[0]));
250 s
= http_timestring(mprMemCtx(), t
);
251 mpr_Return(eid
, mprString(s
));
258 ok = sys.unlink(fname);
260 static int ejs_sys_unlink(MprVarHandle eid
, int argc
, char **argv
)
264 ejsSetErrorMsg(eid
, "sys_unlink invalid arguments");
267 ret
= unlink(argv
[0]);
268 mpr_Return(eid
, mprCreateBoolVar(ret
== 0));
273 load a file as a string
275 string = sys.file_load(filename);
277 static int ejs_sys_file_load(MprVarHandle eid
, int argc
, char **argv
)
281 ejsSetErrorMsg(eid
, "sys_file_load invalid arguments");
285 s
= file_load(argv
[0], NULL
, mprMemCtx());
286 mpr_Return(eid
, mprString(s
));
292 save a file from a string
294 ok = sys.file_save(filename, str);
296 static int ejs_sys_file_save(MprVarHandle eid
, int argc
, char **argv
)
300 ejsSetErrorMsg(eid
, "sys_file_save invalid arguments");
303 ret
= file_save(argv
[0], argv
[1], strlen(argv
[1]));
304 mpr_Return(eid
, mprCreateBoolVar(ret
));
311 ok = sys.mkdir(dirname, mode);
313 static int ejs_sys_mkdir(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
318 ejsSetErrorMsg(eid
, "sys_mkdir invalid arguments, need mkdir(dirname, mode)");
321 if (!mprVarIsString(argv
[0]->type
)) {
322 ejsSetErrorMsg(eid
, "sys_mkdir dirname not a string");
325 if (!mprVarIsNumber(argv
[1]->type
)) {
326 ejsSetErrorMsg(eid
, "sys_mkdir mode not a number");
329 mprVarToString(&name
, 0, NULL
, argv
[0]);
330 ret
= mkdir(name
, mprVarToNumber(argv
[1]));
331 mpr_Return(eid
, mprCreateBoolVar(ret
== 0));
337 return fields of a stat() call
339 static struct MprVar
mpr_stat(struct stat
*st
)
342 ret
= mprObject("stat");
344 #define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n))
364 var st = sys.stat(filename);
365 returns an object containing struct stat elements
367 static int ejs_sys_stat(MprVarHandle eid
, int argc
, char **argv
)
370 /* validate arguments */
372 ejsSetErrorMsg(eid
, "sys.stat invalid arguments");
375 if (stat(argv
[0], &st
) != 0) {
376 mpr_Return(eid
, mprCreateUndefinedVar());
378 mpr_Return(eid
, mpr_stat(&st
));
385 var st = sys.lstat(filename);
386 returns an object containing struct stat elements
388 static int ejs_sys_lstat(MprVarHandle eid
, int argc
, char **argv
)
391 /* validate arguments */
393 ejsSetErrorMsg(eid
, "sys.stat invalid arguments");
396 if (lstat(argv
[0], &st
) != 0) {
397 mpr_Return(eid
, mprCreateUndefinedVar());
399 mpr_Return(eid
, mpr_stat(&st
));
407 var z = sys.bitAND(x, 0x70);
409 static int ejs_sys_bitAND(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
414 !mprVarIsNumber(argv
[0]->type
) ||
415 !mprVarIsNumber(argv
[1]->type
)) {
416 ejsSetErrorMsg(eid
, "bitand invalid arguments");
419 x
= mprToInt(argv
[0]);
420 y
= mprToInt(argv
[1]);
423 mpr_Return(eid
, mprCreateIntegerVar(z
));
430 var z = sys.bitOR(x, 0x70);
432 static int ejs_sys_bitOR(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
437 !mprVarIsNumber(argv
[0]->type
) ||
438 !mprVarIsNumber(argv
[1]->type
)) {
439 ejsSetErrorMsg(eid
, "bitand invalid arguments");
442 x
= mprToInt(argv
[0]);
443 y
= mprToInt(argv
[1]);
446 mpr_Return(eid
, mprCreateIntegerVar(z
));
451 initialise sys ejs subsystem
453 static int ejs_sys_init(MprVarHandle eid
, int argc
, struct MprVar
**argv
)
455 struct MprVar
*obj
= mprInitObject(eid
, "sys", argc
, argv
);
457 mprSetCFunction(obj
, "interfaces", ejs_sys_interfaces
);
458 mprSetCFunction(obj
, "hostname", ejs_sys_hostname
);
459 mprSetCFunction(obj
, "nttime", ejs_sys_nttime
);
460 mprSetCFunction(obj
, "gettimeofday", ejs_sys_gettimeofday
);
461 mprSetCFunction(obj
, "unix2nttime", ejs_sys_unix2nttime
);
462 mprSetCFunction(obj
, "gmmktime", ejs_sys_gmmktime
);
463 mprSetCFunction(obj
, "gmtime", ejs_sys_gmtime
);
464 mprSetCFunction(obj
, "nttime2unix", ejs_sys_nttime2unix
);
465 mprSetCFunction(obj
, "ntgmtime", ejs_sys_ntgmtime
);
466 mprSetCFunction(obj
, "ldaptime", ejs_sys_ldaptime
);
467 mprSetCFunction(obj
, "httptime", ejs_sys_httptime
);
468 mprSetCFunction(obj
, "mkdir", ejs_sys_mkdir
);
469 mprSetStringCFunction(obj
, "unlink", ejs_sys_unlink
);
470 mprSetStringCFunction(obj
, "file_load", ejs_sys_file_load
);
471 mprSetStringCFunction(obj
, "file_save", ejs_sys_file_save
);
472 mprSetStringCFunction(obj
, "stat", ejs_sys_stat
);
473 mprSetStringCFunction(obj
, "lstat", ejs_sys_lstat
);
474 mprSetCFunction(obj
, "bitAND", ejs_sys_bitAND
);
475 mprSetCFunction(obj
, "bitOR", ejs_sys_bitOR
);
482 setup C functions that be called from ejs
484 NTSTATUS
smb_setup_ejs_system(void)
486 ejsDefineCFunction(-1, "sys_init", ejs_sys_init
, NULL
, MPR_VAR_SCRIPT_HANDLE
);