Once again syncing after too long an absence
[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     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
148 static
149 XS(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
197 void
198 Perl_init_os_extras(void)
199
200   dTHXo;
201   char *file = __FILE__;
202   newXS("EPOC::getcwd", epoc_getcwd, file);
203 }
204
205 void
206 Perl_my_setenv(pTHX_ char *nam,char *val) {
207   setenv( nam, val, 1);
208 }