]> git.sur5r.net Git - i3/i3/blobdiff - libi3/ipc_send_message.c
Update copyright notices and get rid of ranges
[i3/i3] / libi3 / ipc_send_message.c
index 850fbdd239d5a796efd203d20312322ea2127664..a9ba31653e07cacc1b86ed030075eccccc509a52 100644 (file)
@@ -2,7 +2,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
  */
 #include <string.h>
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <stdint.h>
 #include <err.h>
+#include <errno.h>
 
 #include <i3/ipc.h>
 
  * Returns 0 on success.
  *
  */
-int ipc_send_message(int sockfd, uint32_t message_size,
-                     uint32_t message_type, const uint8_t *payload) {
-    int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
-    char msg[buffer_size];
-    char *walk = msg;
+int ipc_send_message(int sockfd, const uint32_t message_size,
+                     const uint32_t message_type, const uint8_t *payload) {
+    const i3_ipc_header_t header = {
+        /* We don’t use I3_IPC_MAGIC because it’s a 0-terminated C string. */
+        .magic = {'i', '3', '-', 'i', 'p', 'c'},
+        .size = message_size,
+        .type = message_type};
 
-    strncpy(walk, I3_IPC_MAGIC, buffer_size - 1);
-    walk += strlen(I3_IPC_MAGIC);
-    memcpy(walk, &message_size, sizeof(uint32_t));
-    walk += sizeof(uint32_t);
-    memcpy(walk, &message_type, sizeof(uint32_t));
-    walk += sizeof(uint32_t);
-    memcpy(walk, payload, message_size);
+    if (writeall(sockfd, ((void *)&header), sizeof(i3_ipc_header_t)) == -1)
+        return -1;
 
-    int sent_bytes = 0;
-    int bytes_to_go = buffer_size;
-    while (sent_bytes < bytes_to_go) {
-        int n = write(sockfd, msg + sent_bytes, bytes_to_go);
-        if (n == -1)
-            return -1;
-
-        sent_bytes += n;
-        bytes_to_go -= n;
-    }
+    if (writeall(sockfd, payload, message_size) == -1)
+        return -1;
 
     return 0;
 }