$OpenBSD: patch-libi3_ipc_send_message_c,v 1.1 2013/01/10 08:20:52 dcoppa Exp $

From f5b7bfb12ef74ddbf250e5076bbfaafd0027474c Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Wed, 09 Jan 2013 17:11:03 +0000
Subject: Bugfix: fix IPC messages writes with low buffer sizes (Thanks jasper, dcoppa)

This fixes a problem where i3bar would exit due to malformed IPC
messages when switching between workspaces with some windows opened.

--- libi3/ipc_send_message.c.orig	Wed Dec 12 00:08:17 2012
+++ libi3/ipc_send_message.c	Thu Jan 10 09:07:13 2013
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <stdint.h>
 #include <err.h>
+#include <errno.h>
 
 #include <i3/ipc.h>
 
@@ -38,14 +39,15 @@ int ipc_send_message(int sockfd, uint32_t message_size
     memcpy(walk, payload, message_size);
 
     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)
+    while (sent_bytes < buffer_size) {
+        int n = write(sockfd, msg + sent_bytes, buffer_size - sent_bytes);
+        if (n == -1) {
+            if (errno == EAGAIN)
+                continue;
             return -1;
+        }
 
         sent_bytes += n;
-        bytes_to_go -= n;
     }
 
     return 0;
