1 | |
1 | |
2 | /* |
2 | /* |
3 | * static char *rcsid_porting_c = |
3 | * static char *rcsid_porting_c = |
4 | * "$Id: porting.C,v 1.5 2006/09/10 16:00:23 root Exp $"; |
4 | * "$Id: porting.C,v 1.6 2006/09/12 23:22:32 root Exp $"; |
5 | */ |
5 | */ |
6 | |
6 | |
7 | /* |
7 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
9 | |
9 | |
… | |
… | |
178 | int fd[2]; |
178 | int fd[2]; |
179 | int pd; |
179 | int pd; |
180 | FILE *ret; |
180 | FILE *ret; |
181 | |
181 | |
182 | if (!strcmp (type, "r")) |
182 | if (!strcmp (type, "r")) |
183 | { |
|
|
184 | pd = STDOUT_FILENO; |
183 | pd = STDOUT_FILENO; |
185 | } |
|
|
186 | else if (!strcmp (type, "w")) |
184 | else if (!strcmp (type, "w")) |
187 | { |
|
|
188 | pd = STDIN_FILENO; |
185 | pd = STDIN_FILENO; |
189 | } |
|
|
190 | else |
186 | else |
191 | { |
|
|
192 | return NULL; |
187 | return NULL; |
193 | } |
188 | |
194 | if (pipe (fd) != -1) |
189 | if (pipe (fd) != -1) |
195 | { |
190 | { |
196 | switch (fork ()) |
191 | switch (fork ()) |
197 | { |
192 | { |
198 | case -1: |
193 | case -1: |
199 | close (fd[0]); |
194 | close (fd[0]); |
200 | close (fd[1]); |
195 | close (fd[1]); |
201 | break; |
196 | break; |
202 | case 0: |
197 | case 0: |
203 | close (fd[0]); |
198 | close (fd[0]); |
204 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
199 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
205 | { |
200 | { |
206 | if (fd[1] != pd) |
201 | if (fd[1] != pd) |
207 | { |
202 | { |
208 | close (fd[1]); |
203 | close (fd[1]); |
209 | } |
204 | } |
210 | execl ("/bin/sh", "sh", "-c", command, NULL); |
205 | execl ("/bin/sh", "sh", "-c", command, NULL); |
211 | close (pd); |
206 | close (pd); |
212 | } |
207 | } |
213 | exit (1); |
208 | _exit (1); |
214 | break; |
209 | break; |
215 | default: |
210 | default: |
216 | close (fd[1]); |
211 | close (fd[1]); |
217 | if (ret = fdopen (fd[0], type)) |
212 | if (ret = fdopen (fd[0], type)) |
218 | { |
213 | { |
219 | return ret; |
214 | return ret; |
220 | } |
215 | } |
221 | close (fd[0]); |
216 | close (fd[0]); |
222 | break; |
217 | break; |
223 | } |
218 | } |
224 | } |
219 | } |
225 | return NULL; |
220 | return NULL; |
226 | } |
221 | } |
227 | |
222 | |
… | |
… | |
229 | |
224 | |
230 | |
225 | |
231 | /***************************************************************************** |
226 | /***************************************************************************** |
232 | * String related function |
227 | * String related function |
233 | ****************************************************************************/ |
228 | ****************************************************************************/ |
234 | |
|
|
235 | |
|
|
236 | |
229 | |
237 | /* |
230 | /* |
238 | * A replacement of strdup(), since it's not defined at some |
231 | * A replacement of strdup(), since it's not defined at some |
239 | * unix variants. |
232 | * unix variants. |
240 | */ |
233 | */ |