~ubuntu-branches/ubuntu/precise/grass/precise

« back to all changes in this revision

Viewing changes to lib/gis/popen.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-04-13 17:08:41 UTC
  • mfrom: (8.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110413170841-ss1t9bic0d0uq0gz
Tags: 6.4.1-1
* New upstream version.
* Now build-dep on libjpeg-dev and current libreadline6-dev.
* Removed patch swig: obsolete.
* Policy bumped to 3.9.2, without changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <unistd.h>
2
1
#include <stdio.h>
3
 
#include <signal.h>
4
 
#include <stdlib.h>
5
 
#include <sys/types.h>
6
 
 
7
 
 
8
 
#ifdef __MINGW32__
9
 
#  include <io.h>
10
 
#  include <fcntl.h>
11
 
#  include <process.h>
12
 
#else
13
 
#  include <sys/wait.h>
14
 
#  define tst(a,b)        (*mode == 'r'? (b) : (a))
15
 
#endif
16
 
 
17
2
#include <grass/gis.h>
18
3
 
19
 
 
20
 
#define READ      0
21
 
#define WRITE     1
22
 
 
23
 
static int popen_pid[50];
24
 
 
25
 
 
26
4
FILE *G_popen(const char *cmd, const char *mode)
27
5
{
28
 
 
29
 
#ifdef __MINGW32__
30
 
 
31
 
    int thepipes[2];
32
 
    FILE *rv = NULL;
33
 
 
34
 
    fflush(stdout);
35
 
    fflush(stderr);
36
 
 
37
 
    /*setvbuf ( stdout, NULL, _IONBF, 0 ); */
38
 
 
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);
43
 
    }
44
 
 
45
 
    return (rv);
46
 
 
47
 
#else /* __MINGW32__ */
48
 
 
49
 
    int p[2];
50
 
    int me, you, pid;
51
 
 
52
 
    fflush(stdout);
53
 
    fflush(stderr);
54
 
 
55
 
    if (pipe(p) < 0)
56
 
        return NULL;
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 */
61
 
        close(me);
62
 
        close(tst(0, 1));
63
 
        dup(you);
64
 
        close(you);
65
 
        execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
66
 
        _exit(1);
67
 
    }
68
 
 
69
 
    if (pid == -1)
70
 
        return NULL;
71
 
    popen_pid[me] = pid;
72
 
    close(you);
73
 
 
74
 
    return (fdopen(me, mode));
75
 
 
76
 
#endif /* __MINGW32__ */
77
 
 
 
6
    return popen(cmd, mode);
78
7
}
79
8
 
80
9
int G_pclose(FILE * ptr)
81
10
{
82
 
    RETSIGTYPE(*sigint) ();
83
 
#ifdef SIGHUP
84
 
    RETSIGTYPE(*sighup) ();
85
 
#endif
86
 
#ifdef SIGQUIT
87
 
    RETSIGTYPE(*sigquit) ();
88
 
#endif
89
 
    int f;
90
 
 
91
 
#ifndef __MINGW32__
92
 
    int r;
93
 
#endif
94
 
    int status;
95
 
 
96
 
    f = fileno(ptr);
97
 
    fclose(ptr);
98
 
 
99
 
    sigint = signal(SIGINT, SIG_IGN);
100
 
#ifdef __MINGW32__
101
 
    _cwait(&status, popen_pid[f], WAIT_CHILD);
102
 
    if (0 & status) {
103
 
        status = -1;
104
 
    }
105
 
#else
106
 
 
107
 
#ifdef SIGQUIT
108
 
    sigquit = signal(SIGQUIT, SIG_IGN);
109
 
#endif
110
 
#ifdef SIGHUP
111
 
    sighup = signal(SIGHUP, SIG_IGN);
112
 
#endif
113
 
    while ((r = wait(&status)) != popen_pid[f] && r != -1) ;
114
 
    if (r == -1)
115
 
        status = -1;
116
 
 
117
 
#endif /* __MINGW32__ */
118
 
 
119
 
    signal(SIGINT, sigint);
120
 
 
121
 
#ifdef SIGQUIT
122
 
    signal(SIGQUIT, sigquit);
123
 
#endif
124
 
#ifdef SIGHUP
125
 
    signal(SIGHUP, sighup);
126
 
#endif
127
 
 
128
 
    return (status);
 
11
    return pclose(ptr);
129
12
}