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 "system/time.h"
29 return the list of configured network interfaces
31 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
33 int i, count = iface_count();
34 struct MprVar ret = mprObject("interfaces");
35 for (i=0;i<count;i++) {
36 mprAddArray(&ret, i, mprString(iface_n_ip(i)));
43 return the hostname from gethostname()
45 static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
48 if (gethostname(name, sizeof(name)-1) == -1) {
49 ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
52 mpr_Return(eid, mprString(name));
58 return current time as a 64 bit nttime value
60 static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
62 struct timeval tv = timeval_current();
63 struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
69 return the given time as a gmtime structure
71 static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
76 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
77 ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
80 t = nt_time_to_unix(mprVarToNumber(argv[0]));
83 mpr_Return(eid, mprCreateUndefinedVar());
86 ret = mprObject("gmtime");
87 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
103 return a ldap time string from a nttime
105 static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
109 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
110 ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
113 t = nt_time_to_unix(mprVarToNumber(argv[0]));
114 s = ldap_timestring(mprMemCtx(), t);
115 mpr_Return(eid, mprString(s));
124 static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
128 ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
131 ret = unlink(argv[0]);
132 mpr_Return(eid, mprCreateBoolVar(ret == 0));
137 load a file as a string
139 string = sys_file_load(filename);
141 static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
145 ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
149 s = file_load(argv[0], NULL, mprMemCtx());
150 mpr_Return(eid, mprString(s));
156 save a file from a string
158 ok = sys_file_save(filename, str);
160 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
164 ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
167 ret = file_save(argv[0], argv[1], strlen(argv[1]));
168 mpr_Return(eid, mprCreateBoolVar(ret));
174 setup C functions that be called from ejs
176 void smb_setup_ejs_system(void)
178 ejsDefineCFunction(-1, "sys_interfaces", ejs_sys_interfaces, NULL, MPR_VAR_SCRIPT_HANDLE);
179 ejsDefineCFunction(-1, "sys_hostname", ejs_sys_hostname, NULL, MPR_VAR_SCRIPT_HANDLE);
180 ejsDefineCFunction(-1, "sys_nttime", ejs_sys_nttime, NULL, MPR_VAR_SCRIPT_HANDLE);
181 ejsDefineCFunction(-1, "sys_gmtime", ejs_sys_gmtime, NULL, MPR_VAR_SCRIPT_HANDLE);
182 ejsDefineCFunction(-1, "sys_ldaptime", ejs_sys_ldaptime, NULL, MPR_VAR_SCRIPT_HANDLE);
183 ejsDefineStringCFunction(-1, "sys_unlink", ejs_sys_unlink, NULL, MPR_VAR_SCRIPT_HANDLE);
184 ejsDefineStringCFunction(-1, "sys_file_load", ejs_sys_file_load, NULL, MPR_VAR_SCRIPT_HANDLE);
185 ejsDefineStringCFunction(-1, "sys_file_save", ejs_sys_file_save, NULL, MPR_VAR_SCRIPT_HANDLE);