Debian Codegraph @

unxfuncs.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21 * _XOPEN_SOURCE required for crypt()
22 * but it stuffs up SCO and QNX-RTO :-(
23 */
24 #if !defined(_SCO_ELF) && !defined(_SCO_COFF) && !defined(__QNXNTO__)
25 # ifndef _XOPEN_SOURCE
26 # define _XOPEN_SOURCE
27 # endif
28 # ifndef _XOPEN_SOURCE
29 # define _GNU_SOURCE
30 # endif
31 #endif
32
33 #ifndef _GNU_SOURCE
34 /* At least Linux needs _GNU_SOURCE and _XOPEN_SOURCE together to allow
35 * a warning free compile.
36 */
37 # define _GNU_SOURCE
38 #endif
39
40 #define HAVE_FORK
41 #if defined(__WATCOMC__) || defined(_MSC_VER) || (defined(__IBMC__) && defined(WIN32)) || defined(__SASC) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(DOS) || defined(__WINS__) || defined(__EPOC32__) ||defined(__LCC__)
42 # undef HAVE_FORK
43 #endif
44 #if defined(__WATCOMC__) && defined(__QNX__)
45 # define HAVE_FORK
46 #endif
47
48 #include "rexx.h"
49 #include "utsname.h"
50 #include <stdio.h>
51 #include <string.h>
52
53 #ifdef HAVE_ASSERT_H
54 # include <assert.h>
55 #endif
56
57 #include <errno.h>
58
59 #ifdef HAVE_CRYPT_H
60 # include <crypt.h>
61 #endif
62
63 #if !defined(MAC)
64 # include <sys/types.h>
65 # ifdef HAVE_UNISTD_H
66 # include <unistd.h>
67 # endif
68 # if defined(WIN32) && defined(__IBMC__)
69 # include <process.h>
70 # include <direct.h>
71 # elif defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(__LCC__)
72 # if !defined(__QNX__)
73 # include <process.h>
74 # include <direct.h>
75 # endif
76 # endif
77 #endif
78
79 streng *unx_popen( tsd_t *TSD, cparamboxptr parms )
80 {
81 streng *string=NULL, *result=NULL ;
82 streng *cptr=NULL ;
83 int length=0, lines=0, hl ;
84 int save_internal_queues_option;
85
86 if ( TSD->restricted )
87 exiterror( ERR_RESTRICTED, 1, "POPEN" ) ;
88
89 checkparam( parms, 1, 2 , "POPEN" ) ;
90 string = (parms->value) ;
91
92 /*
93 * Because this sort of redirection is only valid with internal
94 * queues, we need to make regina think we are using internal
95 * queues for this function.
96 */
97 save_internal_queues_option = get_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES ) ;
98 set_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES, 1 ) ;
99
100 cptr = Str_makeTSD( length=Str_len(string) + 6 ) ;
101 cptr = Str_catTSD( cptr, string ) ;
102 cptr = Str_catstrTSD( cptr, ">LIFO" ) ;
103
104 if (parms->next && parms->next->value)
105 {
106 lines = lines_in_stack( TSD, NULL );
107 if ( lines < 0 )
108 lines = 0;
109 }
110
111 result = perform( TSD, cptr, TSD->currlevel->environment, TSD->currentnode, NULL ) ;
112 Free_stringTSD( cptr ) ;
113
114 if (parms->next && parms->next->value)
115 {
116 streng *varname=NULL, *varstem=NULL ;
117 int stemlen=0 ;
118 char *eptr=NULL ;
119 streng *tmpptr=NULL ;
120
121 varstem = parms->next->value ;
122 varname = Str_makeTSD( (stemlen=varstem->len) + 8 ) ;
123
124 memcpy( varname->value, varstem->value, stemlen ) ;
125 mem_upper( varname->value, stemlen );
126 eptr = varname->value + stemlen ;
127
128 if (*(eptr-1)!='.')
129 {
130 *((eptr++)-1) = '.' ;
131 stemlen++ ;
132 }
133
134 hl = lines_in_stack( TSD, NULL );
135 lines = ( ( hl < 0 ) ? 0 : hl ) - lines ;
136 *eptr = '0' ;
137 varname->len = stemlen+1 ;
138 tmpptr = int_to_streng( TSD, lines ) ;
139 setvalue( TSD, varname, tmpptr, -1 ) ;
140 for (; lines>0; lines--)
141 {
142 tmpptr = popline( TSD, NULL, NULL, 0 ) ;
143 sprintf(eptr, "%d", lines ) ;
144 varname->len = strlen( varname->value ) ;
145 setvalue( TSD, varname, tmpptr, -1 ) ;
146 }
147 Free_stringTSD( varname ); /* bja */
148 }
149 set_options_flag( TSD->currlevel, EXT_INTERNAL_QUEUES, save_internal_queues_option ) ;
150
151 return result ;
152
153 /* Free_stringTSD( cptr ) ;
154 sprintf( (result=Str_makeTSD(SMALLSTR))->value, "%d", rcode ) ;
155 result->len = Str_len(result) ;
156 return result ; */
157 }
158
159
160 streng *unx_getpid( tsd_t *TSD, cparamboxptr parms )
161 {
162 checkparam( parms, 0, 0 , "GETPID" ) ;
163 return int_to_streng( TSD, getpid() ) ;
164 }
165
166 streng *unx_gettid( tsd_t *TSD, cparamboxptr parms )
167 {
168 checkparam( parms, 0, 0 , "GETTID" ) ;
169 return rx64u_to_streng( TSD, (rx_64u)TSD->thread_id ) ;
170 }
171
172
173 streng *unx_eof( tsd_t *TSD, cparamboxptr parms )
174 {
175 checkparam( parms, 0, 0 , "EOF" ) ;
176 /* sprintf(ptr=MallocTSD(SMALLSTR),"%d",eof_on_input()) ; */
177 return( nullstringptr() ) ;
178 }
179
180
181 streng *unx_uname( tsd_t *TSD, cparamboxptr parms )
182 {
183 char option=' ' ;
184 char *cptr=NULL ;
185 int length=0 ;
186 streng *result=NULL ;
187 struct regina_utsname utsbox ;
188
189 checkparam( parms, 0, 1 , "UNAME" ) ;
190 if (parms->value)
191 option = getoptionchar( TSD, parms->value, "UNAME", 1, "ASMNRV", "" ) ;
192 else
193 option = 'A' ;
194
195 if (TSD->OS->uname_exec( &utsbox ) <0)
196 exiterror( ERR_SYSTEM_FAILURE, 1, strerror( errno ) ) ;
197
198 switch( option )
199 {
200 case 'A':
201 result = Str_makeTSD( 5+sizeof(struct regina_utsname)) ;
202 result->len = sprintf( result->value, "%s %s %s %s %s", utsbox.sysname,
203 utsbox.nodename, utsbox.release, utsbox.version,
204 utsbox.machine ) ;
205 assert( result->len <= result->max ) ;
206 return result ;
207
208 case 'S': cptr = utsbox.sysname ; break ;
209 case 'N': cptr = utsbox.nodename ; break ;
210 case 'R': cptr = utsbox.release ; break ;
211 case 'V': cptr = utsbox.version ; break ;
212 case 'M': cptr = utsbox.machine ; break ;
213 default:
214 assert( 0 ) ;
215 }
216
217 length = strlen(cptr);
218 result = Str_makeTSD( length+1 ) ;
219 memcpy( result->value, cptr, length ) ;
220 result->len = length ;
221 assert( result->len <= result->max ) ;
222 return result ;
223 }
224
225
226 streng *unx_fork( tsd_t *TSD, cparamboxptr parms )
227 {
228 int i=1 ;
229
230 checkparam( parms, 0, 0 , "FORK" ) ;
231 #if defined(HAVE_FORK)
232 i = fork() ;
233 #endif /* MH 10-06-96 */
234 return int_to_streng( TSD, i ) ;
235 }
236
237
238 streng *unx_unixerror( tsd_t *TSD, cparamboxptr parms )
239 {
240 const char *errtxt=NULL ;
241 int errnum=0 ; /* change name from errno to not conflist with global errno */
242
243 checkparam( parms, 1, 1 , "UNIXERROR" ) ;
244 errnum = atozpos( TSD, parms->value, "UNIXERROR", 1 ) ;
245 errtxt = strerror(errnum) ;
246 return Str_creTSD( errtxt ) ;
247 }
248
249
250 streng *unx_chdir( tsd_t *TSD, cparamboxptr parms )
251 {
252 char *path;
253 int rc=0;
254 int ok=HOOK_GO_ON ;
255
256 checkparam( parms, 1, 1 , "CD" ) ;
257 /*
258 * Check if we have a registered system exit
259 */
260 if (TSD->systeminfo->hooks & HOOK_MASK(HOOK_SETCWD))
261 ok = hookup_output( TSD, HOOK_SETCWD, parms->value ) ;
262
263 if (ok==HOOK_GO_ON)
264 {
265 path = str_of( TSD, parms->value ) ;
266 rc = chdir( path ) ;
267 FreeTSD( path ) ;
268 }
269 return int_to_streng( TSD, rc!=0 ) ;
270 }
271
272
273 streng *unx_getenv( tsd_t *TSD, cparamboxptr parms )
274 {
275 streng *retval=NULL ;
276 char *output=NULL ;
277 char *path ;
278 int ok=HOOK_GO_ON ;
279
280 checkparam( parms, 1, 1 , "GETENV" ) ;
281
282 if (TSD->systeminfo->hooks & HOOK_MASK(HOOK_GETENV))
283 ok = hookup_input_output( TSD, HOOK_GETENV, parms->value, &retval ) ;
284
285 if (ok==HOOK_GO_ON)
286 {
287 path = str_of( TSD, parms->value ) ;
288 output = mygetenv( TSD, path, NULL, 0 ) ;
289 FreeTSD( path ) ;
290 if ( output )
291 {
292 retval = Str_creTSD( output ) ;
293 FreeTSD( output );
294 }
295 else
296 retval = nullstringptr() ;
297 }
298 return retval ;
299 }
300
301
302 streng *unx_putenv( tsd_t *TSD, cparamboxptr parms )
303 {
304 streng *name,*env;
305 streng *value=NULL;
306 streng *retval=NULL ;
307 int ok=HOOK_GO_ON,i ;
308
309 checkparam( parms, 1, 1 , "PUTENV" ) ;
310 name = Str_dupstrTSD( parms->value );
311 /*
312 * Argument is of form ENV=[value]
313 */
314 for ( i = 0; i < Str_len(name); i++ )
315 {
316 if ( name->value[i] == '=' )
317 {
318 name->value[i] = '\0';
319 Str_len(name) = i;
320 value = Str_creTSD( (name->value)+i+1 ) ;
321 value->value[Str_len(value)] = '\0';
322 break;
323 }
324 }
325
326 if (TSD->systeminfo->hooks & HOOK_MASK(HOOK_SETENV))
327 ok = hookup_output2( TSD, HOOK_SETENV, name, value );
328
329 if (ok==HOOK_GO_ON)
330 {
331 env = Str_creTSD( "ENVIRONMENT" ) ;
332 retval = ext_pool_value( TSD, name, value, env );
333 Free_stringTSD( env );
334 }
335 Free_stringTSD( name );
336 if ( value )
337 Free_stringTSD( value );
338 if ( !retval )
339 retval = nullstringptr() ;
340 return retval ;
341 }
342
343
344 streng *unx_crypt( tsd_t *TSD, cparamboxptr parms )
345 {
346 streng *retval=NULL ;
347 #ifdef HAVE_CRYPT
348 char *output=NULL ;
349 char *key, *salt ;
350 #endif
351 int ch2,i;
352
353 checkparam( parms, 2, 2 , "CRYPT" ) ;
354 for ( i = 0; i < Str_len( parms->next->value ); i++ )
355 {
356 ch2 = (int)*(parms->next->value->value+i);
357 if ((ch2 >= (int)'A' && ch2 <= (int)'Z')
358 || (ch2 >= (int)'a' && ch2 <= (int)'z')
359 || (ch2 >= (int)'0' && ch2 <= (int)'9')
360 || (ch2 == (int)'.' || ch2 == (int)'/'))
361 {
362 }
363 else
364 {
365 char tmp[2];
366 tmp[0] = (char)ch2;
367 tmp[1] = '\0';
368 exiterror( ERR_INCORRECT_CALL, 914, "CRYPT", 2, "A-Za-z0-9./", tmp ) ;
369 }
370 }
371 #ifdef HAVE_CRYPT
372 key = str_of( TSD, parms->value );
373 salt = str_of( TSD, parms->next->value );
374 output = crypt( key, salt ) ;
375 FreeTSD( salt ) ;
376 FreeTSD( key ) ;
377 if (output)
378 retval = Str_creTSD(output) ;
379 else
380 retval = nullstringptr() ;
381 #else
382 retval = Str_dup( parms->value );
383 #endif
384 return retval ;
385 }
386