Commit Diff


commit - f7cc10d779f671cfc3ac8d06a30c6f44daf41265
commit + 787957df3c63e9a734a66d1059bfa0b01e2748fd
blob - a6b088a6ca740ad6618e2faadc3302e9bf0aa1c0
blob + c10fca2ba7bf5071888a1b9ecfc7ce86b1855d2f
--- bxwm.c
+++ bxwm.c
@@ -4,9 +4,9 @@
  * See LICENSE.md and README.md for details.
  */
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <errno.h>
 #include <signal.h>
 #include <string.h>
@@ -16,8 +16,8 @@
 #include <unistd.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
-#include <X11/cursorfont.h>
 #include <X11/Xatom.h> 
+#include <X11/cursorfont.h>
 
 #include "config.h"
 
@@ -59,6 +59,7 @@ static unsigned int num_or = 0;
 
 /* Forward Declarations */
 /* Utilities */
+static void die(const char *fmt, ...);
 static void logmsg(const char *fmt, ...);
 static void spawn(const char *cmd);
 static int xerror(Display *dpy, XErrorEvent *ee);
@@ -98,7 +99,7 @@ main(int argc, char *argv[])
 
     XEvent ev;
 
-    if (!(dpy = XOpenDisplay(NULL))) exit(1);
+    die("bxwm: cannot open display");
     root = DefaultRootWindow(dpy);
 
     server_fd = socket_init();
@@ -151,6 +152,16 @@ main(int argc, char *argv[])
     return 0;
 }
 
+static void
+die(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+    cleanup(1);
+}
+
 /* Uncomment to enable debug logging */
 /* #define DEBUG */
 
@@ -185,13 +196,10 @@ xerror(Display *dpy, XErrorEvent *ee) {
 int
 socket_init(void) {
     struct sockaddr_un addr;
-    int server_fd;
-    char socket_path[256];
 
     char *home = getenv("HOME");
     if (home == NULL) {
-        fprintf(stderr, "bxwm: HOME not set\n");
-        exit(1);
+        die("bxwm: HOME not set");
     }
     snprintf(socket_path, sizeof(socket_path), "%s%s", home, SOCKET_SUFFIX);
 
@@ -199,8 +207,7 @@ socket_init(void) {
 
     server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (server_fd < 0) {
-        perror("bxwm: socket");
-        exit(1);
+        die("bxwm: bind: %s", strerror(errno));
     }
 
     memset(&addr, 0, sizeof(addr));
@@ -208,16 +215,11 @@ socket_init(void) {
     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
 
     if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-        perror("bxwm: bind");
-        close(server_fd);
-        exit(1);
+        die("bxwm: bind: %s", strerror(errno));
     }
 
     if (listen(server_fd, SOMAXCONN) < 0) {
-        perror("bxwm: listen");
-        close(server_fd);
-        unlink(socket_path);
-        exit(1);
+        die("bxwm: listen: %s", strerror(errno));
     }
 
     return server_fd;
@@ -241,21 +243,18 @@ setup(void) {
     XSetErrorHandler(xerror);
 
     if (XGetSelectionOwner(dpy, XInternAtom(dpy, "WM_S0", False))) {
-        fprintf(stderr, "bxwm: another window manager is running\n");
-        cleanup(1);
+        die("bxwm: another window manager is running");
     }
 
     if (!XParseColor(dpy, cmap, BORDER_FOCUSED, &color) ||
         !XAllocColor(dpy, cmap, &color)) {
-        fprintf(stderr, "bxwm: cannot allocate focus color '%s'\n", BORDER_FOCUSED);
-        cleanup(1);
+        die("bxwm: cannot allocate focus color '%s'", BORDER_UNFOCUSED);
     }
     focus_pixel = color.pixel;
 
     if (!XParseColor(dpy, cmap, BORDER_UNFOCUSED, &color) ||
         !XAllocColor(dpy, cmap, &color)) {
-        fprintf(stderr, "bxwm: cannot allocate unfocus color '%s'\n", BORDER_UNFOCUSED);
-        cleanup(1);
+        die("bxwm: cannot allocate unfocus color '%s'", BORDER_UNFOCUSED);
     }
     unfocus_pixel = color.pixel;
 
@@ -456,8 +455,7 @@ manage(Window w) {
 
     clients = realloc(clients, ++num_clients * sizeof(Client));
     if (!clients) {
-        fprintf(stderr, "bxwm: realloc failed\n");
-        cleanup(1);
+        die("bxwm: realloc failed");
     }
     c = &clients[num_clients - 1];
     c->win = w;