util.c
changeset 14 5c078b66347b
parent 11 ea9c08ec4b48
child 15 00d4d52b231f
equal deleted inserted replaced
13:5cc5e55a132d 14:5c078b66347b
    11 #include <sys/wait.h>
    11 #include <sys/wait.h>
    12 #include <unistd.h>
    12 #include <unistd.h>
    13 #include <X11/Xatom.h>
    13 #include <X11/Xatom.h>
    14 
    14 
    15 #include "util.h"
    15 #include "util.h"
    16 
       
    17 static char *shell = NULL;
       
    18 
    16 
    19 void
    17 void
    20 error(char *errstr, ...) {
    18 error(char *errstr, ...) {
    21 	va_list ap;
    19 	va_list ap;
    22 	va_start(ap, errstr);
    20 	va_start(ap, errstr);
    83 	*p1 = *p2;
    81 	*p1 = *p2;
    84 	*p2 = tmp;
    82 	*p2 = tmp;
    85 }
    83 }
    86 
    84 
    87 void
    85 void
    88 spawn(Display *dpy, const char *cmd)
    86 spawn(Display *dpy, char *argv[])
    89 {
    87 {
    90 	if(!shell && !(shell = getenv("SHELL")))
    88 	if(!argv || !argv[0])
    91 		shell = "/bin/sh";
       
    92 
       
    93 	if(!cmd)
       
    94 		return;
    89 		return;
    95 	if(fork() == 0) {
    90 	if(fork() == 0) {
    96 		if(fork() == 0) {
    91 		if(fork() == 0) {
    97 			if(dpy)
    92 			if(dpy)
    98 				close(ConnectionNumber(dpy));
    93 				close(ConnectionNumber(dpy));
    99 			setsid();
    94 			setsid();
   100 			fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd);
    95 			execvp(argv[0], argv);
   101 			execlp(shell, shell, "-c", cmd, NULL);
    96 			fprintf(stderr, "gridwm: execvp %s", argv[0]);
   102 			fprintf(stderr, "gridwm: execlp %s", cmd);
       
   103 			perror(" failed");
    97 			perror(" failed");
   104 		}
    98 		}
   105 		exit (0);
    99 		exit (0);
   106 	}
   100 	}
   107 	wait(0);
   101 	wait(0);
   108 }
   102 }
   109 
   103 
   110 void
   104 void
   111 pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
   105 pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
   112 {
   106 {
   113 	unsigned int l, n;
   107 	unsigned int l, n;
   114 	int pfd[2];
   108 	int pfd[2];
   115 
   109 
   116 	if(!shell && !(shell = getenv("SHELL")))
   110 	if(!argv || !argv[0])
   117 		shell = "/bin/sh";
       
   118 
       
   119 	if(!cmd)
       
   120 		return;
   111 		return;
   121 
   112 
   122 	if(pipe(pfd) == -1) {
   113 	if(pipe(pfd) == -1) {
   123 		perror("pipe");
   114 		perror("pipe");
   124 		exit(1);
   115 		exit(1);
   129 			close(ConnectionNumber(dpy));
   120 			close(ConnectionNumber(dpy));
   130 		setsid();
   121 		setsid();
   131 		dup2(pfd[1], STDOUT_FILENO);
   122 		dup2(pfd[1], STDOUT_FILENO);
   132 		close(pfd[0]);
   123 		close(pfd[0]);
   133 		close(pfd[1]);
   124 		close(pfd[1]);
   134 		execlp(shell, shell, "-c", cmd, NULL);
   125 		execvp(argv[0], argv);
   135 		fprintf(stderr, "gridwm: execlp %s", cmd);
   126 		fprintf(stderr, "gridwm: execvp %s", argv[0]);
   136 		perror(" failed");
   127 		perror(" failed");
   137 	}
   128 	}
   138 	else {
   129 	else {
   139 		n = 0;
   130 		n = 0;
   140 		close(pfd[1]);
   131 		close(pfd[1]);