i3bar: use safewrappers from libi3

next
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 "ucs2_to_utf8.h"
#include "config.h"
#include "libi3.h"
#endif

View File

@ -62,7 +62,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
int n = 0;
int rec = 0;
int buffer_len = STDIN_CHUNK_SIZE;
char *buffer = malloc(buffer_len);
char *buffer = smalloc(buffer_len);
buffer[0] = '\0';
while(1) {
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) {
buffer_len += STDIN_CHUNK_SIZE;
buffer = realloc(buffer, buffer_len);
buffer = srealloc(buffer, buffer_len);
}
}
if (*buffer == '\0') {
@ -169,12 +169,12 @@ void start_child(char *command) {
/* We set O_NONBLOCK because blocking is evil in event-driven software */
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_start(main_loop, stdin_io);
/* 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_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
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);
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 */
uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
char *header = malloc(header_len);
if (header == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
char *header = smalloc(header_len);
/* We first parse the fixed-length IPC-header, to know, how much data
* 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
* of the message */
char *buffer = malloc(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);
}
char *buffer = smalloc(size + 1);
rec = 0;
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
* than the pure header (why not just write() the payload from *payload?),
* but we leave it for now */
char *buffer = malloc(to_write);
if (buffer == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
char *buffer = smalloc(to_write);
char *walk = buffer;
strncpy(buffer, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC));
@ -301,11 +286,7 @@ int init_connection(const char *socket_path) {
exit(EXIT_FAILURE);
}
i3_connection = malloc(sizeof(ev_io));
if (i3_connection == NULL) {
ELOG("malloc() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
i3_connection = smalloc(sizeof(ev_io));
ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
ev_io_start(main_loop, i3_connection);
return 1;

View File

@ -19,7 +19,6 @@
#include <glob.h>
#include "common.h"
#include "libi3.h"
/*
* Glob path, i.e. expand ~
@ -31,11 +30,7 @@ char *expand_path(char *path) {
ELOG("glob() failed\n");
exit(EXIT_FAILURE);
}
char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
if (result == NULL) {
ELOG("malloc() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
globfree(&globbuf);
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 only need those watchers on the stack, so putting them on the stack saves us
* some calls to free() */
ev_signal *sig_term = malloc(sizeof(ev_signal));
ev_signal *sig_int = malloc(sizeof(ev_signal));
ev_signal *sig_hup = malloc(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 *sig_term = smalloc(sizeof(ev_signal));
ev_signal *sig_int = smalloc(sizeof(ev_signal));
ev_signal *sig_hup = smalloc(sizeof(ev_signal));
ev_signal_init(sig_term, &sig_cb, SIGTERM);
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_;
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);
copy[len] = '\0';
@ -134,7 +134,7 @@ static int outputs_string_cb(void *params_, const unsigned char *val, unsigned i
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);
name[len] = '\0';
@ -154,16 +154,16 @@ static int outputs_start_map_cb(void *params_) {
i3_output *new_output = NULL;
if (params->cur_key == NULL) {
new_output = malloc(sizeof(i3_output));
new_output = smalloc(sizeof(i3_output));
new_output->name = NULL;
new_output->ws = 0,
memset(&new_output->rect, 0, sizeof(rect));
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);
new_output->trayclients = malloc(sizeof(struct tc_head));
new_output->trayclients = smalloc(sizeof(struct tc_head));
TAILQ_INIT(new_output->trayclients);
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_;
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);
params->cur_key[keyLen] = '\0';
@ -235,7 +235,7 @@ yajl_callbacks outputs_callbacks = {
*
*/
void init_outputs() {
outputs = malloc(sizeof(struct outputs_head));
outputs = smalloc(sizeof(struct outputs_head));
SLIST_INIT(outputs);
}

View File

@ -14,6 +14,8 @@
#include <err.h>
#include <iconv.h>
#include "libi3.h"
static iconv_t conversion_descriptor = 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 */
int buffer_size = 8;
char *buffer = calloc(buffer_size, 1);
if (buffer == NULL)
err(EXIT_FAILURE, "malloc() failed\n");
char *buffer = scalloc(buffer_size);
size_t output_size = buffer_size;
/* We need to use an additional pointer, because iconv() modifies it */
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 */
int buffer_size = input_size * 2;
char *buffer = malloc(buffer_size);
if (buffer == NULL)
err(EXIT_FAILURE, "malloc() failed\n");
char *buffer = smalloc(buffer_size);
size_t output_size = buffer_size;
/* We need to use an additional pointer, because iconv() modifies it */
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")) {
/* 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);
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")) {
/* 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);
output_name[len] = '\0';
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;
if (params->cur_key == NULL) {
new_workspace = malloc(sizeof(i3_ws));
new_workspace = smalloc(sizeof(i3_ws));
new_workspace->num = -1;
new_workspace->name = NULL;
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_;
FREE(params->cur_key);
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
if (params->cur_key == NULL) {
ELOG("Could not allocate memory: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char*) keyVal, keyLen);
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++)
continue;
if ((copy = malloc(len + 1)) == NULL)
return (NULL);
copy = smalloc(len + 1);
memcpy(copy, str, len);
copy[len] = '\0';
return (copy);
@ -516,7 +515,7 @@ static void handle_client_message(xcb_client_message_event_t* event) {
values);
/* send the XEMBED_EMBEDDED_NOTIFY message */
void *event = calloc(32, 1);
void *event = scalloc(32);
xcb_client_message_event_t *ev = event;
ev->response_type = XCB_CLIENT_MESSAGE;
ev->window = client;
@ -539,7 +538,7 @@ static void handle_client_message(xcb_client_message_event_t* event) {
} else {
DLOG("Not mapping dock client yet\n");
}
trayclient *tc = malloc(sizeof(trayclient));
trayclient *tc = smalloc(sizeof(trayclient));
tc->win = client;
tc->mapped = map_it;
tc->xe_version = xe_version;
@ -841,9 +840,9 @@ char *init_xcb_early() {
/* The various Watchers to communicate with xcb */
xcb_io = malloc(sizeof(ev_io));
xcb_prep = malloc(sizeof(ev_prepare));
xcb_chk = malloc(sizeof(ev_check));
xcb_io = smalloc(sizeof(ev_io));
xcb_prep = smalloc(sizeof(ev_prepare));
xcb_chk = smalloc(sizeof(ev_check));
ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
ev_prepare_init(xcb_prep, &xcb_prep_cb);
@ -956,7 +955,7 @@ void init_xcb_late(char *fontname) {
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_start(main_loop, xkb_io);
XFlush(xkb_dpy);
@ -1053,7 +1052,7 @@ void init_tray() {
}
/* 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;
ev->response_type = XCB_CLIENT_MESSAGE;
ev->window = xcb_root;