80 *p1 = *p2; |
82 *p1 = *p2; |
81 *p2 = tmp; |
83 *p2 = tmp; |
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 } |