1. Add a function writeall and make swrite wrap that function. Use either writeall or swrite, depending on whether we want to exit on errors or not.
2. Fix warnings when compiling with a higher optimisation level.
(CFLAGS ?= -pipe -O3 -march=native -mtune=native -freorder-blocks-and-partition)
Signed-off-by: hwangcc <hwangcc@csie.nctu.edu.tw>
* of the log. */
wrap_count = header->wrap_count;
const int len = (logbuffer + header->offset_last_wrap) - walk;
- if (write(STDOUT_FILENO, walk, len) != len)
- err(EXIT_FAILURE, "write()");
+ swrite(STDOUT_FILENO, walk, len);
walk = logbuffer + sizeof(i3_shmlog_header);
return 1;
}
static void print_till_end(void) {
check_for_wrap();
const int len = (logbuffer + header->offset_next_write) - walk;
- const int n = write(STDOUT_FILENO, walk, len);
- if (len != n)
- err(EXIT_FAILURE, "write()");
- if (n > 0) {
- walk += n;
- }
+ swrite(STDOUT_FILENO, walk, len);
+ walk += len;
}
int main(int argc, char *argv[]) {
char *link_path;
char *exe_path = get_exe_path(argv0);
sasprintf(&link_path, "%s.nagbar_cmd", script_path);
- symlink(exe_path, link_path);
+ if (symlink(exe_path, link_path) == -1) {
+ err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
+ }
char *terminal_cmd;
sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
char *message;
va_list args;
va_start(args, format);
- vasprintf(&message, format, args);
+ (void)vasprintf(&message, format, args);
struct status_block *err_block = scalloc(sizeof(struct status_block));
err_block->full_text = i3string_from_utf8("Error: ");
if (child.click_events) {
const unsigned char *output;
size_t size;
+ ssize_t n;
yajl_gen_get_buf(gen, &output, &size);
- write(child_stdin, output, size);
- write(child_stdin, "\n", 1);
+
+ n = writeall(child_stdin, output, size);
+ if (n != -1)
+ n = writeall(child_stdin, "\n", 1);
+
yajl_gen_clear(gen);
+
+ if (n == -1) {
+ child.click_events = false;
+ kill_child();
+ set_statusline_error("child_write_output failed");
+ draw_bars(false);
+ }
}
}
if (payload != NULL)
strncpy(walk, payload, len);
- uint32_t written = 0;
-
- while (to_write > 0) {
- int n = write(i3_connection->fd, buffer + written, to_write);
- if (n == -1) {
- ELOG("write() failed: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- to_write -= n;
- written += n;
- }
+ swrite(i3_connection->fd, buffer, to_write);
FREE(buffer);
*/
int sasprintf(char **strp, const char *fmt, ...);
+/**
+ * Wrapper around correct write which returns -1 (meaning that
+ * write failed) or count (meaning that all bytes were written)
+ *
+ */
+ssize_t writeall(int fd, const void *buf, size_t count);
+
+/**
+ * Safe-wrapper around writeall which exits if it returns -1 (meaning that
+ * write failed)
+ *
+ */
+ssize_t swrite(int fd, const void *buf, size_t count);
+
/**
* Build an i3String from an UTF-8 encoded string.
* Returns the newly-allocated i3String.
.size = message_size,
.type = message_type};
- size_t sent_bytes = 0;
- int n = 0;
+ if (writeall(sockfd, ((void *)&header), sizeof(i3_ipc_header_t)) == -1)
+ return -1;
- /* This first loop is basically unnecessary. No operating system has
- * buffers which cannot fit 14 bytes into them, so the write() will only be
- * called once. */
- while (sent_bytes < sizeof(i3_ipc_header_t)) {
- if ((n = write(sockfd, ((void *)&header) + sent_bytes, sizeof(i3_ipc_header_t) - sent_bytes)) == -1) {
- if (errno == EAGAIN)
- continue;
- return -1;
- }
-
- sent_bytes += n;
- }
-
- sent_bytes = 0;
-
- while (sent_bytes < message_size) {
- if ((n = write(sockfd, payload + sent_bytes, message_size - sent_bytes)) == -1) {
- if (errno == EAGAIN)
- continue;
- return -1;
- }
-
- sent_bytes += n;
- }
+ if (writeall(sockfd, payload, message_size) == -1)
+ return -1;
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <unistd.h>
#include <stdio.h>
#include <err.h>
+#include <errno.h>
#include "libi3.h"
va_end(args);
return result;
}
+
+ssize_t writeall(int fd, const void *buf, size_t count) {
+ int written = 0;
+ ssize_t n = 0;
+
+ while (written < count) {
+ n = write(fd, buf + written, count - written);
+ if (n == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return n;
+ }
+ written += n;
+ }
+
+ return written;
+}
+
+ssize_t swrite(int fd, const void *buf, size_t count) {
+ ssize_t n;
+
+ n = writeall(fd, buf, count);
+ if (n == -1)
+ err(EXIT_FAILURE, "Failed to write %d", fd);
+ else
+ return n;
+}
case BORDER_BOTTOM:
search_direction = D_DOWN;
break;
+ default:
+ assert(false);
+ break;
}
bool res = resize_find_tiling_participants(&first, &second, search_direction);
/* write the whole config file to the pipe, the script will read everything
* immediately */
- int written = 0;
- int ret;
- while (written < size) {
- if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
- warn("Could not write to pipe");
- return NULL;
- }
- written += ret;
+ if (writeall(writepipe[1], input, size) == -1) {
+ warn("Could not write to pipe");
+ return NULL;
}
close(writepipe[1]);
/* read the script’s output */
int conv_size = 65535;
char *converted = malloc(conv_size);
- int read_bytes = 0;
+ int read_bytes = 0, ret;
do {
if (read_bytes == conv_size) {
conv_size += 65535;
int cnt = 1;
while (workspace != NULL) {
FREE(json_node->name);
- asprintf(&(json_node->name), "%s_%d", base, cnt++);
+ sasprintf(&(json_node->name), "%s_%d", base, cnt++);
workspace = NULL;
TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
int stdin_pipe[2],
stdout_pipe[2];
- pipe(stdin_pipe);
- pipe(stdout_pipe);
+ if (pipe(stdin_pipe) == -1) {
+ ELOG("Failed to init stdin_pipe\n");
+ return -1;
+ }
+ if (pipe(stdout_pipe) == -1) {
+ ELOG("Failed to init stdout_pipe\n");
+ return -1;
+ }
/* close standard streams in case i3 is started from a terminal; gdb
* needs to run without controlling terminal for it to work properly in
return NULL;
}
- size_t written = 0;
- while (written < length) {
- int n = write(fd, payload + written, length - written);
- /* TODO: correct error-handling */
- if (n == -1) {
- perror("write()");
- free(filename);
- close(fd);
- return NULL;
- }
- if (n == 0) {
- DLOG("write == 0?\n");
- free(filename);
- close(fd);
- return NULL;
- }
- written += n;
- DLOG("written: %zd of %zd\n", written, length);
+ if (writeall(fd, payload, length) == -1) {
+ ELOG("Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
+ free(filename);
+ close(fd);
+ return NULL;
}
+
close(fd);
if (length > 0) {