diff options
| author | Anselm R. Garbe <garbeam@wmii.de> | 2006-07-11 11:10:05 +0200 |
|---|---|---|
| committer | Anselm R. Garbe <garbeam@wmii.de> | 2006-07-11 11:10:05 +0200 |
| commit | 3a69c5173cdd24959410870bec2a10a76272e034 (patch) | |
| tree | 993f2fb75b1c44213d0c95a4da8b3a18daa739fe /util.c | |
| parent | added several other stuff (diff) | |
| download | dwm-3a69c5173cdd24959410870bec2a10a76272e034.tar.gz dwm-3a69c5173cdd24959410870bec2a10a76272e034.zip | |
implemented pipe_spawn
Diffstat (limited to 'util.c')
| -rw-r--r-- | util.c | 56 |
1 files changed, 52 insertions, 4 deletions
| @@ -13,6 +13,8 @@ | |||
| 13 | 13 | ||
| 14 | #include "util.h" | 14 | #include "util.h" |
| 15 | 15 | ||
| 16 | static char *shell = NULL; | ||
| 17 | |||
| 16 | void | 18 | void |
| 17 | error(char *errstr, ...) { | 19 | error(char *errstr, ...) { |
| 18 | va_list ap; | 20 | va_list ap; |
| @@ -82,19 +84,65 @@ swap(void **p1, void **p2) | |||
| 82 | } | 84 | } |
| 83 | 85 | ||
| 84 | void | 86 | void |
| 85 | spawn(Display *dpy, const char *shell, const char *cmd) | 87 | spawn(Display *dpy, const char *cmd) |
| 86 | { | 88 | { |
| 87 | if(!cmd || !shell) | 89 | if(!shell && !(shell = getenv("SHELL"))) |
| 90 | shell = "/bin/sh"; | ||
| 91 | |||
| 92 | if(!cmd) | ||
| 88 | return; | 93 | return; |
| 89 | if(fork() == 0) { | 94 | if(fork() == 0) { |
| 90 | if(fork() == 0) { | 95 | if(fork() == 0) { |
| 96 | setsid(); | ||
| 91 | if(dpy) | 97 | if(dpy) |
| 92 | close(ConnectionNumber(dpy)); | 98 | close(ConnectionNumber(dpy)); |
| 93 | execl(shell, shell, "-c", cmd, (const char *)0); | 99 | execlp(shell, "shell", "-c", cmd, NULL); |
| 94 | fprintf(stderr, "gridwm: execl %s", shell); | 100 | fprintf(stderr, "gridwm: execvp %s", cmd); |
| 95 | perror(" failed"); | 101 | perror(" failed"); |
| 96 | } | 102 | } |
| 97 | exit (0); | 103 | exit (0); |
| 98 | } | 104 | } |
| 99 | wait(0); | 105 | wait(0); |
| 100 | } | 106 | } |
| 107 | |||
| 108 | void | ||
| 109 | pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd) | ||
| 110 | { | ||
| 111 | unsigned int l, n; | ||
| 112 | int pfd[2]; | ||
| 113 | |||
| 114 | if(!shell && !(shell = getenv("SHELL"))) | ||
| 115 | shell = "/bin/sh"; | ||
| 116 | |||
| 117 | if(!cmd) | ||
| 118 | return; | ||
| 119 | |||
| 120 | if(pipe(pfd) == -1) { | ||
| 121 | perror("pipe"); | ||
| 122 | exit(1); | ||
| 123 | } | ||
| 124 | |||
| 125 | if(fork() == 0) { | ||
| 126 | setsid(); | ||
| 127 | if(dpy) | ||
| 128 | close(ConnectionNumber(dpy)); | ||
| 129 | dup2(pfd[1], STDOUT_FILENO); | ||
| 130 | close(pfd[0]); | ||
| 131 | close(pfd[1]); | ||
| 132 | execlp(shell, "shell", "-c", cmd, NULL); | ||
| 133 | fprintf(stderr, "gridwm: execvp %s", cmd); | ||
| 134 | perror(" failed"); | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | n = 0; | ||
| 138 | close(pfd[1]); | ||
| 139 | while(l > n) { | ||
| 140 | if((l = read(pfd[0], buf + n, len - n)) < 1) | ||
| 141 | break; | ||
| 142 | n += l; | ||
| 143 | } | ||
| 144 | close(pfd[0]); | ||
| 145 | buf[n - 1] = 0; | ||
| 146 | } | ||
| 147 | wait(0); | ||
| 148 | } | ||
