]> git.lizzy.rs Git - bspwm.git/blobdiff - bspc.c
Raise focused floating window via pointer -g focus
[bspwm.git] / bspc.c
diff --git a/bspc.c b/bspc.c
index 25ece9cdfe8544ae5736e9f0113f370f1cf7344b..c5e1697e689528ea9aaa843fe84810aadd4be03b 100644 (file)
--- a/bspc.c
+++ b/bspc.c
@@ -1,52 +1,62 @@
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include "helpers.h"
 #include "common.h"
 
 int main(int argc, char *argv[])
 {
-    int sock_fd, nbr, i;
+    int sock_fd;
     struct sockaddr_un sock_address;
-    char socket_path[BUFSIZ];
     char msg[BUFSIZ];
     char rsp[BUFSIZ];
 
     if (argc < 2)
-        return -1;
+        err("No arguments given.\n");
 
-    char *sp = getenv(SOCKET_ENV_VAR);
+    char *sock_path = getenv(SOCKET_ENV_VAR);
+    if (sock_path != NULL && sizeof(sock_address.sun_path) <= strlen(sock_path))
+        err("The socket path can't fit into the socket address.\n");
 
-    strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
+    sock_address.sun_family = AF_UNIX;
+    strncpy(sock_address.sun_path, (sock_path == NULL ? DEFAULT_SOCKET_PATH : sock_path), sizeof(sock_address.sun_path));
+    sock_address.sun_path[sizeof(sock_address.sun_path) - 1] = 0;
 
-    msg[0] = '\0';
+    argc--, argv++;
+    int msg_len = 0;
 
-    int max = sizeof(msg);
-    for (i = 1; max > 0 && i < argc; i++) {
-        strncat(msg, argv[i], max);
-        max -= strlen(argv[i]);
-        if (i < (argc - 1)) {
-            strncat(msg, TOKEN_SEP, max);
-            max -= strlen(TOKEN_SEP);
-        }
+    for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, msg_len = offset, rem -= n, argc--, argv++) {
+        n = snprintf(msg + offset, rem, "%s%c", *argv, 0);
+        msg_len += n;
     }
 
-    sock_address.sun_family = AF_UNIX;
-    strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
+    if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+        err("Failed to create the socket.\n");
 
-    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
-    connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
+    if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
+        err("Failed to connect to the socket.\n");
 
-    send(sock_fd, msg, strlen(msg), 0);
+    if (send(sock_fd, msg, msg_len, 0) == -1)
+        err("Failed to send the data.\n");
 
-    if ((nbr = recv(sock_fd, rsp, sizeof(rsp), 0)) > 0) {
-        rsp[nbr] = '\0';
-        if (strcmp(rsp, EMPTY_RESPONSE) != 0)
-            printf("%s", rsp);
+    int ret = EXIT_SUCCESS;
+
+    int n = recv(sock_fd, rsp, sizeof(rsp), 0);
+    if (n == -1) {
+        err("Failed to get the response.\n");
+    } else if (n > 0) {
+        if (n == 1 && rsp[0] == MESSAGE_FAILURE) {
+            ret = EXIT_FAILURE;
+        } else {
+            rsp[n] = '\0';
+            printf("%s\n", rsp);
+        }
     }
 
-    close(sock_fd);
-    return 0;
+    if (sock_fd)
+        close(sock_fd);
+
+    return ret;
 }