Add a safe wrapper for write and fix some warnings

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>
This commit is contained in:
hwangcc
2015-03-24 20:57:06 +08:00
parent 822cd3bf1b
commit 42515308e7
12 changed files with 90 additions and 80 deletions

View File

@ -46,6 +46,9 @@ static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press
case BORDER_BOTTOM:
search_direction = D_DOWN;
break;
default:
assert(false);
break;
}
bool res = resize_find_tiling_participants(&first, &second, search_direction);

View File

@ -778,14 +778,9 @@ static char *migrate_config(char *input, off_t size) {
/* 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]);
@ -795,7 +790,7 @@ static char *migrate_config(char *input, off_t size) {
/* read the scripts 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;

View File

@ -105,7 +105,7 @@ static int json_end_map(void *ctx) {
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));

View File

@ -70,8 +70,14 @@ static int backtrace(void) {
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

View File

@ -265,25 +265,13 @@ char *store_restart_layout(void) {
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) {