~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to lib/gis/popen.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

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
2
#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
 
3
 
 
4
#include <unistd.h>
16
5
 
17
6
#include <grass/gis.h>
18
 
 
19
 
 
20
 
#define READ      0
21
 
#define WRITE     1
22
 
 
23
 
static int popen_pid[50];
24
 
 
25
 
 
26
 
FILE *G_popen(const char *cmd, const char *mode)
27
 
{
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
 
 
78
 
}
79
 
 
80
 
int G_pclose(FILE * ptr)
81
 
{
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);
 
7
#include <grass/spawn.h>
 
8
 
 
9
#ifdef __MINGW32__
 
10
#include <io.h>
 
11
#include <fcntl.h>
 
12
#define pipe(fds) _pipe(fds, 4096, O_BINARY|O_NOINHERIT)
 
13
#endif
 
14
 
 
15
static FILE *do_popen(struct Popen *state, int wr,
 
16
                      const char *program, const char **args)
 
17
{
 
18
    int which = wr ? 0 : 1;
 
19
    const char *dir = wr ? "w" : "r";
 
20
    int pfd, cfd;
 
21
    int pipe_fds[2];
 
22
    const char *argv[2];
 
23
 
 
24
    state->fp = NULL;
 
25
    state->pid = -1;
 
26
 
 
27
    if (pipe(pipe_fds) < 0)
 
28
        return NULL;
 
29
 
 
30
    cfd = pipe_fds[wr ? 0 : 1];
 
31
    pfd = pipe_fds[wr ? 1 : 0];
 
32
 
 
33
    if (!args) {
 
34
        argv[0] = program;
 
35
        argv[1] = NULL;
 
36
        args = argv;
 
37
    }
 
38
 
 
39
    state->pid = G_spawn_ex(
 
40
        program,
 
41
        SF_ARGVEC, args,
 
42
        SF_REDIRECT_DESCRIPTOR, which, cfd,
 
43
        SF_CLOSE_DESCRIPTOR, pfd,
 
44
        SF_BACKGROUND, NULL);
 
45
 
 
46
    if (state->pid == -1) {
 
47
        close(pipe_fds[0]);
 
48
        close(pipe_fds[1]);
 
49
        return NULL;
 
50
    }
 
51
 
 
52
    close(cfd);
 
53
 
 
54
    state->fp = fdopen(pfd, dir);
 
55
 
 
56
    return state->fp;
 
57
}
 
58
 
 
59
void G_popen_clear(struct Popen *state)
 
60
{
 
61
    state->fp = NULL;
 
62
    state->pid = -1;
 
63
}
 
64
 
 
65
FILE *G_popen_write(struct Popen *state, const char *program, const char **args)
 
66
{
 
67
    return do_popen(state, 1, program, args);
 
68
}
 
69
 
 
70
FILE *G_popen_read(struct Popen *state, const char *program, const char **args)
 
71
{
 
72
    return do_popen(state, 0, program, args);
 
73
}
 
74
 
 
75
void G_popen_close(struct Popen *state)
 
76
{
 
77
    if (state->fp)
 
78
        fclose(state->fp);
 
79
 
 
80
    if (state->pid != -1)
 
81
        G_wait(state->pid);
129
82
}