i3bar: use safewrappers from libi3

This commit is contained in:
Michael Stapelberg 2011-10-21 19:30:46 +01:00
parent c65d13ff9f
commit d71db710dd
9 changed files with 37 additions and 72 deletions

View File

@ -34,5 +34,6 @@ struct rect_t {
#include "xcb.h" #include "xcb.h"
#include "ucs2_to_utf8.h" #include "ucs2_to_utf8.h"
#include "config.h" #include "config.h"
#include "libi3.h"
#endif #endif

View File

@ -62,7 +62,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
int n = 0; int n = 0;
int rec = 0; int rec = 0;
int buffer_len = STDIN_CHUNK_SIZE; int buffer_len = STDIN_CHUNK_SIZE;
char *buffer = malloc(buffer_len); char *buffer = smalloc(buffer_len);
buffer[0] = '\0'; buffer[0] = '\0';
while(1) { while(1) {
n = read(fd, buffer + rec, buffer_len - rec); n = read(fd, buffer + rec, buffer_len - rec);
@ -91,7 +91,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
if (rec == buffer_len) { if (rec == buffer_len) {
buffer_len += STDIN_CHUNK_SIZE; buffer_len += STDIN_CHUNK_SIZE;
buffer = realloc(buffer, buffer_len); buffer = srealloc(buffer, buffer_len);
} }
} }
if (*buffer == '\0') { if (*buffer == '\0') {
@ -169,12 +169,12 @@ void start_child(char *command) {
/* We set O_NONBLOCK because blocking is evil in event-driven software */ /* We set O_NONBLOCK because blocking is evil in event-driven software */
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
stdin_io = malloc(sizeof(ev_io)); stdin_io = smalloc(sizeof(ev_io));
ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ); ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
ev_io_start(main_loop, stdin_io); ev_io_start(main_loop, stdin_io);
/* We must cleanup, if the child unexpectedly terminates */ /* We must cleanup, if the child unexpectedly terminates */
child_sig = malloc(sizeof(ev_child)); child_sig = smalloc(sizeof(ev_child));
ev_child_init(child_sig, &child_sig_cb, child_pid, 0); ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
ev_child_start(main_loop, child_sig); ev_child_start(main_loop, child_sig);

View File

@ -35,7 +35,7 @@ static int config_map_key_cb(void *params_, const unsigned char *keyVal, unsigne
#endif #endif
FREE(cur_key); FREE(cur_key);
cur_key = malloc(sizeof(unsigned char) * (keyLen + 1)); cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(cur_key, (const char*) keyVal, keyLen); strncpy(cur_key, (const char*) keyVal, keyLen);
cur_key[keyLen] = '\0'; cur_key[keyLen] = '\0';

View File

@ -150,11 +150,7 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
/* First we only read the header, because we know its length */ /* First we only read the header, because we know its length */
uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2; uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
char *header = malloc(header_len); char *header = smalloc(header_len);
if (header == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* We first parse the fixed-length IPC-header, to know, how much data /* We first parse the fixed-length IPC-header, to know, how much data
* we have to expect */ * we have to expect */
@ -191,13 +187,7 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
/* Now that we know, what to expect, we can start read()ing the rest /* Now that we know, what to expect, we can start read()ing the rest
* of the message */ * of the message */
char *buffer = malloc(size + 1); char *buffer = smalloc(size + 1);
if (buffer == NULL) {
/* EOF received. Since i3 will restart i3bar instances as appropriate,
* we exit here. */
DLOG("EOF received, exiting...\n");
exit(EXIT_SUCCESS);
}
rec = 0; rec = 0;
while (rec < size) { while (rec < size) {
@ -243,12 +233,7 @@ int i3_send_msg(uint32_t type, const char *payload) {
/* TODO: I'm not entirely sure if this buffer really has to contain more /* TODO: I'm not entirely sure if this buffer really has to contain more
* than the pure header (why not just write() the payload from *payload?), * than the pure header (why not just write() the payload from *payload?),
* but we leave it for now */ * but we leave it for now */
char *buffer = malloc(to_write); char *buffer = smalloc(to_write);
if (buffer == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
char *walk = buffer; char *walk = buffer;
strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)); strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
@ -301,11 +286,7 @@ int init_connection(const char *socket_path) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
i3_connection = malloc(sizeof(ev_io)); i3_connection = smalloc(sizeof(ev_io));
if (i3_connection == NULL) {
ELOG("malloc() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ev_io_init(i3_connection, &got_data, sockfd, EV_READ); ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
ev_io_start(main_loop, i3_connection); ev_io_start(main_loop, i3_connection);
return 1; return 1;

View File

@ -19,7 +19,6 @@
#include <glob.h> #include <glob.h>
#include "common.h" #include "common.h"
#include "libi3.h"
/* /*
* Glob path, i.e. expand ~ * Glob path, i.e. expand ~
@ -31,11 +30,7 @@ char *expand_path(char *path) {
ELOG("glob() failed\n"); ELOG("glob() failed\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path); char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
if (result == NULL) {
ELOG("malloc() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
globfree(&globbuf); globfree(&globbuf);
return result; return result;
} }
@ -142,14 +137,9 @@ int main(int argc, char **argv) {
/* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop. /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
* We only need those watchers on the stack, so putting them on the stack saves us * We only need those watchers on the stack, so putting them on the stack saves us
* some calls to free() */ * some calls to free() */
ev_signal *sig_term = malloc(sizeof(ev_signal)); ev_signal *sig_term = smalloc(sizeof(ev_signal));
ev_signal *sig_int = malloc(sizeof(ev_signal)); ev_signal *sig_int = smalloc(sizeof(ev_signal));
ev_signal *sig_hup = malloc(sizeof(ev_signal)); ev_signal *sig_hup = smalloc(sizeof(ev_signal));
if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
ELOG("malloc() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ev_signal_init(sig_term, &sig_cb, SIGTERM); ev_signal_init(sig_term, &sig_cb, SIGTERM);
ev_signal_init(sig_int, &sig_cb, SIGINT); ev_signal_init(sig_int, &sig_cb, SIGINT);

View File

@ -115,7 +115,7 @@ static int outputs_string_cb(void *params_, const unsigned char *val, unsigned i
struct outputs_json_params *params = (struct outputs_json_params*) params_; struct outputs_json_params *params = (struct outputs_json_params*) params_;
if (!strcmp(params->cur_key, "current_workspace")) { if (!strcmp(params->cur_key, "current_workspace")) {
char *copy = malloc(sizeof(const unsigned char) * (len + 1)); char *copy = smalloc(sizeof(const unsigned char) * (len + 1));
strncpy(copy, (const char*) val, len); strncpy(copy, (const char*) val, len);
copy[len] = '\0'; copy[len] = '\0';
@ -134,7 +134,7 @@ static int outputs_string_cb(void *params_, const unsigned char *val, unsigned i
return 0; return 0;
} }
char *name = malloc(sizeof(const unsigned char) * (len + 1)); char *name = smalloc(sizeof(const unsigned char) * (len + 1));
strncpy(name, (const char*) val, len); strncpy(name, (const char*) val, len);
name[len] = '\0'; name[len] = '\0';
@ -154,16 +154,16 @@ static int outputs_start_map_cb(void *params_) {
i3_output *new_output = NULL; i3_output *new_output = NULL;
if (params->cur_key == NULL) { if (params->cur_key == NULL) {
new_output = malloc(sizeof(i3_output)); new_output = smalloc(sizeof(i3_output));
new_output->name = NULL; new_output->name = NULL;
new_output->ws = 0, new_output->ws = 0,
memset(&new_output->rect, 0, sizeof(rect)); memset(&new_output->rect, 0, sizeof(rect));
new_output->bar = XCB_NONE; new_output->bar = XCB_NONE;
new_output->workspaces = malloc(sizeof(struct ws_head)); new_output->workspaces = smalloc(sizeof(struct ws_head));
TAILQ_INIT(new_output->workspaces); TAILQ_INIT(new_output->workspaces);
new_output->trayclients = malloc(sizeof(struct tc_head)); new_output->trayclients = smalloc(sizeof(struct tc_head));
TAILQ_INIT(new_output->trayclients); TAILQ_INIT(new_output->trayclients);
params->outputs_walk = new_output; params->outputs_walk = new_output;
@ -208,7 +208,7 @@ static int outputs_map_key_cb(void *params_, const unsigned char *keyVal, unsign
struct outputs_json_params *params = (struct outputs_json_params*) params_; struct outputs_json_params *params = (struct outputs_json_params*) params_;
FREE(params->cur_key); FREE(params->cur_key);
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1)); params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char*) keyVal, keyLen); strncpy(params->cur_key, (const char*) keyVal, keyLen);
params->cur_key[keyLen] = '\0'; params->cur_key[keyLen] = '\0';
@ -235,7 +235,7 @@ yajl_callbacks outputs_callbacks = {
* *
*/ */
void init_outputs() { void init_outputs() {
outputs = malloc(sizeof(struct outputs_head)); outputs = smalloc(sizeof(struct outputs_head));
SLIST_INIT(outputs); SLIST_INIT(outputs);
} }

View File

@ -14,6 +14,8 @@
#include <err.h> #include <err.h>
#include <iconv.h> #include <iconv.h>
#include "libi3.h"
static iconv_t conversion_descriptor = 0; static iconv_t conversion_descriptor = 0;
static iconv_t conversion_descriptor2 = 0; static iconv_t conversion_descriptor2 = 0;
@ -27,9 +29,7 @@ char *convert_ucs_to_utf8(char *input) {
/* UTF-8 may consume up to 4 byte */ /* UTF-8 may consume up to 4 byte */
int buffer_size = 8; int buffer_size = 8;
char *buffer = calloc(buffer_size, 1); char *buffer = scalloc(buffer_size);
if (buffer == NULL)
err(EXIT_FAILURE, "malloc() failed\n");
size_t output_size = buffer_size; size_t output_size = buffer_size;
/* We need to use an additional pointer, because iconv() modifies it */ /* We need to use an additional pointer, because iconv() modifies it */
char *output = buffer; char *output = buffer;
@ -68,9 +68,7 @@ char *convert_utf8_to_ucs2(char *input, int *real_strlen) {
/* UCS-2 consumes exactly two bytes for each glyph */ /* UCS-2 consumes exactly two bytes for each glyph */
int buffer_size = input_size * 2; int buffer_size = input_size * 2;
char *buffer = malloc(buffer_size); char *buffer = smalloc(buffer_size);
if (buffer == NULL)
err(EXIT_FAILURE, "malloc() failed\n");
size_t output_size = buffer_size; size_t output_size = buffer_size;
/* We need to use an additional pointer, because iconv() modifies it */ /* We need to use an additional pointer, because iconv() modifies it */
char *output = buffer; char *output = buffer;

View File

@ -117,7 +117,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne
if (!strcmp(params->cur_key, "name")) { if (!strcmp(params->cur_key, "name")) {
/* Save the name */ /* Save the name */
params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1)); params->workspaces_walk->name = smalloc(sizeof(const unsigned char) * (len + 1));
strncpy(params->workspaces_walk->name, (const char*) val, len); strncpy(params->workspaces_walk->name, (const char*) val, len);
params->workspaces_walk->name[len] = '\0'; params->workspaces_walk->name[len] = '\0';
@ -141,7 +141,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne
if (!strcmp(params->cur_key, "output")) { if (!strcmp(params->cur_key, "output")) {
/* We add the ws to the TAILQ of the output, it belongs to */ /* We add the ws to the TAILQ of the output, it belongs to */
output_name = malloc(sizeof(const unsigned char) * (len + 1)); output_name = smalloc(sizeof(const unsigned char) * (len + 1));
strncpy(output_name, (const char*) val, len); strncpy(output_name, (const char*) val, len);
output_name[len] = '\0'; output_name[len] = '\0';
params->workspaces_walk->output = get_output_by_name(output_name); params->workspaces_walk->output = get_output_by_name(output_name);
@ -167,7 +167,7 @@ static int workspaces_start_map_cb(void *params_) {
i3_ws *new_workspace = NULL; i3_ws *new_workspace = NULL;
if (params->cur_key == NULL) { if (params->cur_key == NULL) {
new_workspace = malloc(sizeof(i3_ws)); new_workspace = smalloc(sizeof(i3_ws));
new_workspace->num = -1; new_workspace->num = -1;
new_workspace->name = NULL; new_workspace->name = NULL;
new_workspace->visible = 0; new_workspace->visible = 0;
@ -197,11 +197,7 @@ static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, uns
struct workspaces_json_params *params = (struct workspaces_json_params*) params_; struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
FREE(params->cur_key); FREE(params->cur_key);
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1)); params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
if (params->cur_key == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
strncpy(params->cur_key, (const char*) keyVal, keyLen); strncpy(params->cur_key, (const char*) keyVal, keyLen);
params->cur_key[keyLen] = '\0'; params->cur_key[keyLen] = '\0';

View File

@ -51,8 +51,7 @@ char *strndup(const char *str, size_t n) {
for (len = 0; len < n && str[len]; len++) for (len = 0; len < n && str[len]; len++)
continue; continue;
if ((copy = malloc(len + 1)) == NULL) copy = smalloc(len + 1);
return (NULL);
memcpy(copy, str, len); memcpy(copy, str, len);
copy[len] = '\0'; copy[len] = '\0';
return (copy); return (copy);
@ -516,7 +515,7 @@ static void handle_client_message(xcb_client_message_event_t* event) {
values); values);
/* send the XEMBED_EMBEDDED_NOTIFY message */ /* send the XEMBED_EMBEDDED_NOTIFY message */
void *event = calloc(32, 1); void *event = scalloc(32);
xcb_client_message_event_t *ev = event; xcb_client_message_event_t *ev = event;
ev->response_type = XCB_CLIENT_MESSAGE; ev->response_type = XCB_CLIENT_MESSAGE;
ev->window = client; ev->window = client;
@ -539,7 +538,7 @@ static void handle_client_message(xcb_client_message_event_t* event) {
} else { } else {
DLOG("Not mapping dock client yet\n"); DLOG("Not mapping dock client yet\n");
} }
trayclient *tc = malloc(sizeof(trayclient)); trayclient *tc = smalloc(sizeof(trayclient));
tc->win = client; tc->win = client;
tc->mapped = map_it; tc->mapped = map_it;
tc->xe_version = xe_version; tc->xe_version = xe_version;
@ -841,9 +840,9 @@ char *init_xcb_early() {
/* The various Watchers to communicate with xcb */ /* The various Watchers to communicate with xcb */
xcb_io = malloc(sizeof(ev_io)); xcb_io = smalloc(sizeof(ev_io));
xcb_prep = malloc(sizeof(ev_prepare)); xcb_prep = smalloc(sizeof(ev_prepare));
xcb_chk = malloc(sizeof(ev_check)); xcb_chk = smalloc(sizeof(ev_check));
ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ); ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
ev_prepare_init(xcb_prep, &xcb_prep_cb); ev_prepare_init(xcb_prep, &xcb_prep_cb);
@ -956,7 +955,7 @@ void init_xcb_late(char *fontname) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
xkb_io = malloc(sizeof(ev_io)); xkb_io = smalloc(sizeof(ev_io));
ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ); ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
ev_io_start(main_loop, xkb_io); ev_io_start(main_loop, xkb_io);
XFlush(xkb_dpy); XFlush(xkb_dpy);
@ -1053,7 +1052,7 @@ void init_tray() {
} }
/* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */ /* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */
void *event = calloc(32, 1); void *event = scalloc(32);
xcb_client_message_event_t *ev = event; xcb_client_message_event_t *ev = event;
ev->response_type = XCB_CLIENT_MESSAGE; ev->response_type = XCB_CLIENT_MESSAGE;
ev->window = xcb_root; ev->window = xcb_root;