]> git.sur5r.net Git - i3/i3/blob - src/util.c
Environment filtering is not needed. Instead, open applications through SHELL, double...
[i3/i3] / src / util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <sys/wait.h>
6
7 #include "i3.h"
8
9 /*
10  * Starts the given application by passing it through a shell. We use double fork
11  * to avoid zombie processes. As the started application’s parent exits (immediately),
12  * the application is reparented to init (process-id 1), which correctly handles
13  * childs, so we don’t have to do it :-).
14  *
15  * The shell is determined by looking for the SHELL environment variable. If it
16  * does not exist, /bin/sh is used.
17  *
18  */
19 void start_application(const char *command) {
20         if (fork() == 0) {
21                 /* Child process */
22                 if (fork() == 0) {
23                         /* Stores the path of the shell */
24                         static const char *shell = NULL;
25
26                         if (shell == NULL)
27                                 if ((shell = getenv("SHELL")) == NULL)
28                                         shell = "/bin/sh";
29
30                         /* This is the child */
31                         execl(shell, shell, "-c", command, NULL);
32                         /* not reached */
33                 }
34                 exit(0);
35         }
36         wait(0);
37 }
38
39 /*
40  * Checks a generic cookie for errors and quits with the given message if there
41  * was an error.
42  *
43  */
44 void check_error(xcb_connection_t *connection, xcb_void_cookie_t cookie, char *err_message) {
45         xcb_generic_error_t *error = xcb_request_check(connection, cookie);
46         if (error != NULL) {
47                 fprintf(stderr, "ERROR: %s : %d\n", err_message , error->error_code);
48                 xcb_disconnect(connection);
49                 exit(-1);
50         }
51 }