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