13
# include <sys/wait.h>
14
# define tst(a,b) (*mode == 'r'? (b) : (a))
17
6
#include <grass/gis.h>
23
static int popen_pid[50];
26
FILE *G_popen(const char *cmd, const char *mode)
37
/*setvbuf ( stdout, NULL, _IONBF, 0 ); */
39
if (_pipe(thepipes, 256, O_BINARY) != -1) {
40
execl("cmd", "cmd", "/c", cmd, (char *)NULL);
41
close(thepipes[WRITE]);
42
rv = fdopen(thepipes[READ], mode);
47
#else /* __MINGW32__ */
57
me = tst(p[WRITE], p[READ]);
58
you = tst(p[READ], p[WRITE]);
59
if ((pid = fork()) == 0) {
60
/* me and you reverse roles in child */
65
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
74
return (fdopen(me, mode));
76
#endif /* __MINGW32__ */
80
int G_pclose(FILE * ptr)
82
RETSIGTYPE(*sigint) ();
84
RETSIGTYPE(*sighup) ();
87
RETSIGTYPE(*sigquit) ();
99
sigint = signal(SIGINT, SIG_IGN);
101
_cwait(&status, popen_pid[f], WAIT_CHILD);
108
sigquit = signal(SIGQUIT, SIG_IGN);
111
sighup = signal(SIGHUP, SIG_IGN);
113
while ((r = wait(&status)) != popen_pid[f] && r != -1) ;
117
#endif /* __MINGW32__ */
119
signal(SIGINT, sigint);
122
signal(SIGQUIT, sigquit);
125
signal(SIGHUP, sighup);
7
#include <grass/spawn.h>
12
#define pipe(fds) _pipe(fds, 4096, O_BINARY|O_NOINHERIT)
15
static FILE *do_popen(struct Popen *state, int wr,
16
const char *program, const char **args)
18
int which = wr ? 0 : 1;
19
const char *dir = wr ? "w" : "r";
27
if (pipe(pipe_fds) < 0)
30
cfd = pipe_fds[wr ? 0 : 1];
31
pfd = pipe_fds[wr ? 1 : 0];
39
state->pid = G_spawn_ex(
42
SF_REDIRECT_DESCRIPTOR, which, cfd,
43
SF_CLOSE_DESCRIPTOR, pfd,
46
if (state->pid == -1) {
54
state->fp = fdopen(pfd, dir);
59
void G_popen_clear(struct Popen *state)
65
FILE *G_popen_write(struct Popen *state, const char *program, const char **args)
67
return do_popen(state, 1, program, args);
70
FILE *G_popen_read(struct Popen *state, const char *program, const char **args)
72
return do_popen(state, 0, program, args);
75
void G_popen_close(struct Popen *state)