SYN SYN
[p5sagit/p5-mst-13.2.git] / epoc / epoc.c
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  */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <sys/unistd.h>
13
14 void
15 Perl_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       
58
59 }
60
61
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
66 typedef unsigned int UQItype    __attribute__ ((mode (QI)));
67 typedef          int SItype     __attribute__ ((mode (SI)));
68 typedef unsigned int USItype    __attribute__ ((mode (SI)));
69 typedef          int DItype     __attribute__ ((mode (DI)));
70 typedef unsigned int UDItype    __attribute__ ((mode (DI)));
71
72 typedef         float SFtype    __attribute__ ((mode (SF)));
73 typedef         float DFtype    __attribute__ ((mode (DF)));
74
75
76
77 extern DItype __fixunssfdi (SFtype a);
78 extern DItype __fixunsdfdi (DFtype a);
79
80
81 USItype
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
90 #endif
91
92 #include "EXTERN.h"
93 #include "perl.h"
94 #include "XSUB.h"
95
96 int 
97 do_aspawn( pTHX_ SV *really,SV **mark,SV **sp) {
98   return do_spawn( really, mark, sp);
99 }
100
101 int
102 do_spawn (pTHX_ SV *really,SV **mark,SV **sp)
103 {
104     dTHR;
105     int  rc;
106     char **a,*cmd,**ptr, *cmdline, **argv, *p2; 
107     STRLEN n_a;
108     size_t len = 0;
109
110     if (sp<=mark)
111       return -1;
112     
113     a=argv=ptr=(char**) malloc ((sp-mark+3)*sizeof (char*));
114     
115     while (++mark <= sp) {
116       if (*mark)
117         *a = SvPVx(*mark, n_a);
118       else
119         *a = "";
120       len += strlen( *a) + 1;
121       a++;
122     }
123     *a = Nullch;
124
125     if (!(really && *(cmd = SvPV(really, n_a)))) {
126       cmd = argv[0];
127       argv++;
128     }
129       
130     cmdline = (char * ) malloc( len + 1);
131     cmdline[ 0] = '\0';
132     while (*argv != NULL) {
133       strcat( cmdline, *argv++);
134       strcat( cmdline, " ");
135     }
136
137     for (p2=cmd; *p2 != '\0'; p2++) {
138       /* Change / to \ */
139       if ( *p2 == '/') 
140         *p2 = '\\';
141     }
142     rc = epoc_spawn( cmd, cmdline);
143     free( ptr);
144     free( cmdline);
145     
146     return rc;
147 }
148
149 static
150 XS(epoc_getcwd)   /* more or less stolen from win32.c */
151 {
152     dXSARGS;
153     /* Make the host for current directory */
154     char *buffer; 
155     int buflen = 256;
156
157     char *ptr;
158     buffer = (char *) malloc( buflen);
159     if (buffer == NULL) {
160       XSRETURN_UNDEF;
161     }
162     while ((NULL == ( ptr = getcwd( buffer, buflen))) && (errno == ERANGE)) {
163       buflen *= 2;
164       if (NULL == realloc( buffer, buflen)) {
165          XSRETURN_UNDEF;
166       }
167       
168     }
169
170     /* 
171      * If ptr != Nullch 
172      *   then it worked, set PV valid, 
173      *   else return 'undef' 
174      */
175
176     if (ptr) {
177         SV *sv = sv_newmortal();
178         char *tptr;
179
180         for (tptr = ptr; *tptr != '\0'; tptr++) {
181           if (*tptr == '\\') {
182             *tptr = '/';
183           }
184         }
185         sv_setpv(sv, ptr);
186         free( buffer);
187
188         EXTEND(SP,1);
189         SvPOK_on(sv);
190         ST(0) = sv;
191         XSRETURN(1);
192     }
193     free( buffer);
194     XSRETURN_UNDEF;
195 }
196   
197
198 void
199 Perl_init_os_extras(void)
200
201   dTHXo;
202   char *file = __FILE__;
203   newXS("EPOC::getcwd", epoc_getcwd, file);
204 }
205
206 void
207 Perl_my_setenv(pTHX_ char *nam,char *val) {
208   setenv( nam, val, 1);
209 }