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));
121 return a http time string from a nttime
123 static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv)
127 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
128 ejsSetErrorMsg(eid, "sys_httptime invalid arguments");
131 t = nt_time_to_unix(mprVarToNumber(argv[0]));
132 s = http_timestring(mprMemCtx(), t);
133 mpr_Return(eid, mprString(s));
142 static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
146 ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
149 ret = unlink(argv[0]);
150 mpr_Return(eid, mprCreateBoolVar(ret == 0));
155 load a file as a string
157 string = sys_file_load(filename);
159 static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
163 ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
167 s = file_load(argv[0], NULL, mprMemCtx());
168 mpr_Return(eid, mprString(s));
174 save a file from a string
176 ok = sys_file_save(filename, str);
178 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
182 ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
185 ret = file_save(argv[0], argv[1], strlen(argv[1]));
186 mpr_Return(eid, mprCreateBoolVar(ret));
192 initialise sys ejs subsystem
194 static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
196 struct MprVar obj = mprObject("sys");
198 mprSetCFunction(&obj, "interfaces", ejs_sys_interfaces);
199 mprSetCFunction(&obj, "hostname", ejs_sys_hostname);
200 mprSetCFunction(&obj, "nttime", ejs_sys_nttime);
201 mprSetCFunction(&obj, "gmtime", ejs_sys_gmtime);
202 mprSetCFunction(&obj, "ldaptime", ejs_sys_ldaptime);
203 mprSetCFunction(&obj, "httptime", ejs_sys_httptime);
204 mprSetStringCFunction(&obj, "unlink", ejs_sys_unlink);
205 mprSetStringCFunction(&obj, "file_load", ejs_sys_file_load);
206 mprSetStringCFunction(&obj, "file_save", ejs_sys_file_save);
208 mpr_Return(eid, obj);
214 setup C functions that be called from ejs
216 void smb_setup_ejs_system(void)
218 ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);