util.c
changeset 6 e0cefb3981c8
parent 5 e5018cae273f
child 9 d567f430a81d
equal deleted inserted replaced
5:e5018cae273f 6:e0cefb3981c8
    10 #include <sys/types.h>
    10 #include <sys/types.h>
    11 #include <sys/wait.h>
    11 #include <sys/wait.h>
    12 #include <unistd.h>
    12 #include <unistd.h>
    13 
    13 
    14 #include "util.h"
    14 #include "util.h"
       
    15 
       
    16 static char *shell = NULL;
    15 
    17 
    16 void
    18 void
    17 error(char *errstr, ...) {
    19 error(char *errstr, ...) {
    18 	va_list ap;
    20 	va_list ap;
    19 	va_start(ap, errstr);
    21 	va_start(ap, errstr);
    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 }