This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Once again syncing after too long an absence
[perl5.git] / epoc / epoc.c
CommitLineData
ae2d1787
OF
1/*
2 * Copyright (c) 1999 Olaf Flebbe o.flebbe@gmx.de
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Artistic License, as specified in the README file.
6 *
7 */
4d2c4e07
OF
8
9#include <stdlib.h>
ae2d1787
OF
10#include <string.h>
11#include <stdio.h>
12#include <sys/unistd.h>
13
ae2d1787
OF
14void
15Perl_epoc_init(int *argcp, char ***argvp) {
16 int i;
17 int truecount=0;
18 char **lastcp = (*argvp);
19 char *ptr;
20 for (i=0; i< *argcp; i++) {
21 if ((*argvp)[i]) {
22 if (*((*argvp)[i]) == '<') {
23 if (strlen((*argvp)[i]) > 1) {
24 ptr =((*argvp)[i])+1;
25 } else {
26 i++;
27 ptr = ((*argvp)[i]);
28 }
29 freopen( ptr, "r", stdin);
30 } else if (*((*argvp)[i]) == '>') {
31 if (strlen((*argvp)[i]) > 1) {
32 ptr =((*argvp)[i])+1;
33 } else {
34 i++;
35 ptr = ((*argvp)[i]);
36 }
37 freopen( ptr, "w", stdout);
38 } else if ((*((*argvp)[i]) == '2') && (*(((*argvp)[i])+1) == '>')) {
39 if (strcmp( (*argvp)[i], "2>&1") == 0) {
40 dup2( fileno( stdout), fileno( stderr));
41 } else {
42 if (strlen((*argvp)[i]) > 2) {
43 ptr =((*argvp)[i])+2;
44 } else {
45 i++;
46 ptr = ((*argvp)[i]);
47 }
48 freopen( ptr, "w", stderr);
49 }
50 } else {
51 *lastcp++ = (*argvp)[i];
52 truecount++;
53 }
54 }
55 }
56 *argcp=truecount;
57
4d2c4e07 58
4d2c4e07
OF
59}
60
22d4bb9c 61
4d2c4e07
OF
62#ifdef __MARM__
63/* Symbian forgot to include __fixunsdfi into the MARM euser.lib */
64/* This is from libgcc2.c , gcc-2.7.2.3 */
65
66typedef unsigned int UQItype __attribute__ ((mode (QI)));
67typedef int SItype __attribute__ ((mode (SI)));
68typedef unsigned int USItype __attribute__ ((mode (SI)));
69typedef int DItype __attribute__ ((mode (DI)));
70typedef unsigned int UDItype __attribute__ ((mode (DI)));
71
72typedef float SFtype __attribute__ ((mode (SF)));
73typedef float DFtype __attribute__ ((mode (DF)));
74
75
76
77extern DItype __fixunssfdi (SFtype a);
78extern DItype __fixunsdfdi (DFtype a);
79
80
81USItype
82__fixunsdfsi (a)
83 DFtype a;
84{
85 if (a >= - (DFtype) (- 2147483647L -1) )
86 return (SItype) (a + (- 2147483647L -1) ) - (- 2147483647L -1) ;
87 return (SItype) a;
88}
89
22d4bb9c
CB
90#endif
91
146174a9
CB
92#include "EXTERN.h"
93#include "perl.h"
94#include "XSUB.h"
95
96int
97do_aspawn( pTHX_ SV *really,SV **mark,SV **sp) {
98 return do_spawn( really, mark, sp);
99}
100
101int
102do_spawn (pTHX_ SV *really,SV **mark,SV **sp)
103{
146174a9
CB
104 int rc;
105 char **a,*cmd,**ptr, *cmdline, **argv, *p2;
106 STRLEN n_a;
107 size_t len = 0;
108
109 if (sp<=mark)
110 return -1;
111
112 a=argv=ptr=(char**) malloc ((sp-mark+3)*sizeof (char*));
113
114 while (++mark <= sp) {
115 if (*mark)
116 *a = SvPVx(*mark, n_a);
117 else
118 *a = "";
119 len += strlen( *a) + 1;
120 a++;
121 }
122 *a = Nullch;
123
124 if (!(really && *(cmd = SvPV(really, n_a)))) {
125 cmd = argv[0];
126 argv++;
127 }
128
129 cmdline = (char * ) malloc( len + 1);
130 cmdline[ 0] = '\0';
131 while (*argv != NULL) {
132 strcat( cmdline, *argv++);
133 strcat( cmdline, " ");
134 }
135
136 for (p2=cmd; *p2 != '\0'; p2++) {
137 /* Change / to \ */
138 if ( *p2 == '/')
139 *p2 = '\\';
140 }
141 rc = epoc_spawn( cmd, cmdline);
142 free( ptr);
143 free( cmdline);
144
145 return rc;
146}
147
22d4bb9c
CB
148static
149XS(epoc_getcwd) /* more or less stolen from win32.c */
150{
151 dXSARGS;
152 /* Make the host for current directory */
153 char *buffer;
154 int buflen = 256;
155
156 char *ptr;
157 buffer = (char *) malloc( buflen);
158 if (buffer == NULL) {
159 XSRETURN_UNDEF;
160 }
161 while ((NULL == ( ptr = getcwd( buffer, buflen))) && (errno == ERANGE)) {
162 buflen *= 2;
163 if (NULL == realloc( buffer, buflen)) {
164 XSRETURN_UNDEF;
165 }
166
167 }
168
169 /*
170 * If ptr != Nullch
171 * then it worked, set PV valid,
172 * else return 'undef'
173 */
174
175 if (ptr) {
176 SV *sv = sv_newmortal();
177 char *tptr;
178
179 for (tptr = ptr; *tptr != '\0'; tptr++) {
180 if (*tptr == '\\') {
181 *tptr = '/';
182 }
183 }
184 sv_setpv(sv, ptr);
185 free( buffer);
186
187 EXTEND(SP,1);
188 SvPOK_on(sv);
189 ST(0) = sv;
190 XSRETURN(1);
191 }
192 free( buffer);
193 XSRETURN_UNDEF;
194}
195
196
197void
198Perl_init_os_extras(void)
199{
200 dTHXo;
201 char *file = __FILE__;
202 newXS("EPOC::getcwd", epoc_getcwd, file);
203}
204
205void
206Perl_my_setenv(pTHX_ char *nam,char *val) {
207 setenv( nam, val, 1);
208}