r9813: Conver testsuite for samba3sam module to EJS
[kai/samba.git] / source / scripting / ejs / smbcalls_sys.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide access to system functions
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "system/time.h"
27
28 /*
29   return the list of configured network interfaces
30 */
31 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
32 {
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)));
37         }
38         mpr_Return(eid, ret);
39         return 0;       
40 }
41
42 /*
43   return the hostname from gethostname()
44 */
45 static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
46 {
47         char name[200];
48         if (gethostname(name, sizeof(name)-1) == -1) {
49                 ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
50                 return -1;
51         }
52         mpr_Return(eid, mprString(name));
53         return 0;       
54 }
55
56
57 /*
58   return current time as a 64 bit nttime value
59 */
60 static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
61 {
62         struct timeval tv = timeval_current();
63         struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
64         mpr_Return(eid, v);
65         return 0;
66 }
67
68 /*
69   return the given time as a gmtime structure
70 */
71 static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
72 {
73         time_t t;
74         struct MprVar ret;
75         struct tm *tm;
76         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
77                 ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
78                 return -1;
79         }
80         t = nt_time_to_unix(mprVarToNumber(argv[0]));
81         tm = gmtime(&t);
82         if (tm == NULL) {
83                 mpr_Return(eid, mprCreateUndefinedVar());
84                 return 0;
85         }
86         ret = mprObject("gmtime");
87 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
88         TM_EL(tm_sec);
89         TM_EL(tm_min);
90         TM_EL(tm_hour);
91         TM_EL(tm_mday);
92         TM_EL(tm_mon);
93         TM_EL(tm_year);
94         TM_EL(tm_wday);
95         TM_EL(tm_yday);
96         TM_EL(tm_isdst);
97
98         mpr_Return(eid, ret);
99         return 0;
100 }
101
102 /*
103   return a ldap time string from a nttime
104 */
105 static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
106 {
107         char *s;
108         time_t t;
109         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
110                 ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
111                 return -1;
112         }
113         t = nt_time_to_unix(mprVarToNumber(argv[0]));
114         s = ldap_timestring(mprMemCtx(), t);
115         mpr_Return(eid, mprString(s));
116         talloc_free(s);
117         return 0;
118 }
119
120 /*
121   return a http time string from a nttime
122 */
123 static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv)
124 {
125         char *s;
126         time_t t;
127         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
128                 ejsSetErrorMsg(eid, "sys_httptime invalid arguments");
129                 return -1;
130         }
131         t = nt_time_to_unix(mprVarToNumber(argv[0]));
132         s = http_timestring(mprMemCtx(), t);
133         mpr_Return(eid, mprString(s));
134         talloc_free(s);
135         return 0;
136 }
137
138 /*
139   unlink a file
140    ok = sys.unlink(fname);
141 */
142 static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
143 {
144         int ret;
145         if (argc != 1) {
146                 ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
147                 return -1;
148         }
149         ret = unlink(argv[0]);
150         mpr_Return(eid, mprCreateBoolVar(ret == 0));
151         return 0;
152 }
153
154 /*
155   load a file as a string
156   usage:
157      string = sys.file_load(filename);
158 */
159 static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
160 {
161         char *s;
162         if (argc != 1) {
163                 ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
164                 return -1;
165         }
166
167         s = file_load(argv[0], NULL, mprMemCtx());
168         mpr_Return(eid, mprString(s));
169         talloc_free(s);
170         return 0;
171 }
172
173 /*
174   save a file from a string
175   usage:
176      ok = sys.file_save(filename, str);
177 */
178 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
179 {
180         BOOL ret;
181         if (argc != 2) {
182                 ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
183                 return -1;
184         }
185         ret = file_save(argv[0], argv[1], strlen(argv[1]));
186         mpr_Return(eid, mprCreateBoolVar(ret));
187         return 0;
188 }
189
190
191 /*
192   return fields of a stat() call
193 */
194 static struct MprVar mpr_stat(struct stat *st)
195 {
196         struct MprVar ret;
197         ret = mprObject("stat");
198
199 #define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n))
200         ST_EL(st_dev);
201         ST_EL(st_ino);
202         ST_EL(st_mode);
203         ST_EL(st_nlink);
204         ST_EL(st_uid);
205         ST_EL(st_gid);
206         ST_EL(st_rdev);
207         ST_EL(st_size);
208         ST_EL(st_blksize);
209         ST_EL(st_blocks);
210         ST_EL(st_atime);
211         ST_EL(st_mtime);
212         ST_EL(st_ctime);
213
214         return ret;
215 }
216
217 /*
218   usage:
219       var st = sys.stat(filename);
220   returns an object containing struct stat elements
221 */
222 static int ejs_sys_stat(MprVarHandle eid, int argc, char **argv)
223 {
224         struct stat st;
225         /* validate arguments */
226         if (argc != 1) {
227                 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
228                 return -1;
229         }
230         if (stat(argv[0], &st) != 0) {
231                 mpr_Return(eid, mprCreateUndefinedVar());
232         } else {
233                 mpr_Return(eid, mpr_stat(&st));
234         }
235         return 0;
236 }
237
238 /*
239   usage:
240       var st = sys.lstat(filename);
241   returns an object containing struct stat elements
242 */
243 static int ejs_sys_lstat(MprVarHandle eid, int argc, char **argv)
244 {
245         struct stat st;
246         /* validate arguments */
247         if (argc != 1) {
248                 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
249                 return -1;
250         }
251         if (lstat(argv[0], &st) != 0) {
252                 mpr_Return(eid, mprCreateUndefinedVar());
253         } else {
254                 mpr_Return(eid, mpr_stat(&st));
255         }
256         return 0;
257 }
258
259
260 /*
261   initialise sys ejs subsystem
262 */
263 static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
264 {
265         struct MprVar *obj = mprInitObject(eid, "sys", argc, argv);
266
267         mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
268         mprSetCFunction(obj, "hostname", ejs_sys_hostname);
269         mprSetCFunction(obj, "nttime", ejs_sys_nttime);
270         mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
271         mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
272         mprSetCFunction(obj, "httptime", ejs_sys_httptime);
273         mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
274         mprSetStringCFunction(obj, "file_load", ejs_sys_file_load);
275         mprSetStringCFunction(obj, "file_save", ejs_sys_file_save);
276         mprSetStringCFunction(obj, "stat", ejs_sys_stat);
277         mprSetStringCFunction(obj, "lstat", ejs_sys_lstat);
278
279         return 0;
280 }
281
282
283 /*
284   setup C functions that be called from ejs
285 */
286 void smb_setup_ejs_system(void)
287 {
288         ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);
289 }