introduce sasprintf() in libi3, use it everywhere

This commit is contained in:
Michael Stapelberg
2011-10-23 13:16:56 +01:00
parent 14abafb3c8
commit 9d15a00ba8
18 changed files with 68 additions and 66 deletions

View File

@ -40,7 +40,7 @@ void run_assignments(i3Window *window) {
if (current->type == A_COMMAND) {
DLOG("execute command %s\n", current->dest.command);
char *full_command;
asprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
char *json_result = parse_cmd(full_command);
FREE(full_command);
FREE(json_result);

View File

@ -280,10 +280,8 @@ static void start_configerror_nagbar(const char *config_path) {
if (configerror_pid == 0) {
char *editaction,
*pageraction;
if (asprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path) == -1)
exit(1);
if (asprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename) == -1)
exit(1);
sasprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path);
sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
char *argv[] = {
NULL, /* will be replaced by the executable path */
"-t",
@ -938,7 +936,7 @@ word_or_number:
WORD
| NUMBER
{
asprintf(&$$, "%d", $1);
sasprintf(&$$, "%d", $1);
}
;
@ -1345,7 +1343,7 @@ workspace:
} else {
char *ws_name = NULL;
if ($5 == NULL) {
asprintf(&ws_name, "%d", ws_num);
sasprintf(&ws_name, "%d", ws_num);
} else {
ws_name = $5;
}
@ -1422,20 +1420,14 @@ assign:
if ((separator = strchr(criteria, '/')) != NULL) {
*(separator++) = '\0';
char *pattern;
if (asprintf(&pattern, "(?i)%s", separator) == -1) {
ELOG("asprintf failed\n");
break;
}
sasprintf(&pattern, "(?i)%s", separator);
match->title = regex_new(pattern);
free(pattern);
printf(" title = %s\n", separator);
}
if (*criteria != '\0') {
char *pattern;
if (asprintf(&pattern, "(?i)%s", criteria) == -1) {
ELOG("asprintf failed\n");
break;
}
sasprintf(&pattern, "(?i)%s", criteria);
match->class = regex_new(pattern);
free(pattern);
printf(" class = %s\n", criteria);

View File

@ -90,8 +90,8 @@ char *parse_cmd(const char *new) {
context->filename = "cmd";
if (cmdyyparse() != 0) {
fprintf(stderr, "Could not parse command\n");
asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
context->compact_error, context->first_column);
sasprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
context->compact_error, context->first_column);
FREE(context->line_copy);
FREE(context->compact_error);
free(context);
@ -433,8 +433,8 @@ focus:
ELOG("You have to specify which window/container should be focused.\n");
ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
"specify which window/container should be focused\"}");
sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
"specify which window/container should be focused\"}");
break;
}
@ -623,7 +623,7 @@ open:
printf("opening new container\n");
Con *con = tree_open_con(NULL, NULL);
con_focus(con);
asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
tree_render();
}

View File

@ -196,8 +196,7 @@ static char *get_config_path(const char *override_configpath) {
xdg_config_home = "~/.config";
xdg_config_home = resolve_tilde(xdg_config_home);
if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
die("asprintf() failed");
sasprintf(&config_path, "%s/i3/config", xdg_config_home);
free(xdg_config_home);
if (path_exists(config_path))
@ -217,8 +216,7 @@ static char *get_config_path(const char *override_configpath) {
char *tok = strtok(buf, ":");
while (tok != NULL) {
tok = resolve_tilde(tok);
if (asprintf(&config_path, "%s/i3/config", tok) == -1)
die("asprintf() failed");
sasprintf(&config_path, "%s/i3/config", tok);
free(tok);
if (path_exists(config_path)) {
free(buf);

View File

@ -90,7 +90,7 @@ void floating_enable(Con *con, bool automatic) {
}
char *name;
asprintf(&name, "[i3 con] floatingcon around %p", con);
sasprintf(&name, "[i3 con] floatingcon around %p", con);
x_set_name(nc, name);
free(name);

View File

@ -117,7 +117,7 @@ static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
LOG("sticky_group of this container is %s\n", json_node->sticky_group);
} else if (strcasecmp(last_key, "orientation") == 0) {
char *buf = NULL;
asprintf(&buf, "%.*s", (int)len, val);
sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "none") == 0)
json_node->orientation = NO_ORIENTATION;
else if (strcasecmp(buf, "horizontal") == 0)
@ -128,7 +128,7 @@ static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
free(buf);
} else if (strcasecmp(last_key, "border") == 0) {
char *buf = NULL;
asprintf(&buf, "%.*s", (int)len, val);
sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "none") == 0)
json_node->border_style = BS_NONE;
else if (strcasecmp(buf, "1pixel") == 0)
@ -139,7 +139,7 @@ static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
free(buf);
} else if (strcasecmp(last_key, "layout") == 0) {
char *buf = NULL;
asprintf(&buf, "%.*s", (int)len, val);
sasprintf(&buf, "%.*s", (int)len, val);
if (strcasecmp(buf, "default") == 0)
json_node->layout = L_DEFAULT;
else if (strcasecmp(buf, "stacked") == 0)
@ -154,7 +154,7 @@ static int json_string(void *ctx, const unsigned char *val, unsigned int len) {
free(buf);
} else if (strcasecmp(last_key, "mark") == 0) {
char *buf = NULL;
asprintf(&buf, "%.*s", (int)len, val);
sasprintf(&buf, "%.*s", (int)len, val);
json_node->mark = buf;
}
}

View File

@ -339,8 +339,7 @@ int main(int argc, char *argv[]) {
payload = sstrdup(argv[optind]);
} else {
char *both;
if (asprintf(&both, "%s %s", payload, argv[optind]) == -1)
err(EXIT_FAILURE, "asprintf");
sasprintf(&both, "%s %s", payload, argv[optind]);
free(payload);
payload = both;
}
@ -647,8 +646,8 @@ int main(int argc, char *argv[]) {
Barconfig *barconfig;
TAILQ_FOREACH(barconfig, &barconfigs, configs) {
char *command = NULL;
asprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
barconfig->id, current_socketpath);
sasprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
barconfig->id, current_socketpath);
LOG("Starting bar process: %s\n", command);
start_application(command);
free(command);

View File

@ -265,7 +265,7 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki
nc->border_width = geom->border_width;
char *name;
asprintf(&name, "[i3 con] container around %p", cwindow);
sasprintf(&name, "[i3 con] container around %p", cwindow);
x_set_name(nc, name);
free(name);

View File

@ -243,7 +243,7 @@ void output_init_con(Output *output) {
output->con = con;
char *name;
asprintf(&name, "[i3 con] output %s", con->name);
sasprintf(&name, "[i3 con] output %s", con->name);
x_set_name(con, name);
FREE(name);
@ -267,7 +267,7 @@ void output_init_con(Output *output) {
FREE(topdock->name);
topdock->name = sstrdup("topdock");
asprintf(&name, "[i3 con] top dockarea %s", con->name);
sasprintf(&name, "[i3 con] top dockarea %s", con->name);
x_set_name(topdock, name);
FREE(name);
DLOG("attaching\n");
@ -281,7 +281,7 @@ void output_init_con(Output *output) {
FREE(content->name);
content->name = sstrdup("content");
asprintf(&name, "[i3 con] content %s", con->name);
sasprintf(&name, "[i3 con] content %s", con->name);
x_set_name(content, name);
FREE(name);
con_attach(content, con, false);
@ -301,7 +301,7 @@ void output_init_con(Output *output) {
FREE(bottomdock->name);
bottomdock->name = sstrdup("bottomdock");
asprintf(&name, "[i3 con] bottom dockarea %s", con->name);
sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
x_set_name(bottomdock, name);
FREE(name);
DLOG("attaching\n");
@ -464,7 +464,7 @@ void init_ws_for_output(Output *output, Con *content) {
c++;
FREE(ws->name);
asprintf(&(ws->name), "%d", c);
sasprintf(&(ws->name), "%d", c);
current = NULL;
TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
@ -477,7 +477,7 @@ void init_ws_for_output(Output *output, Con *content) {
}
con_attach(ws, content, false);
asprintf(&name, "[i3 con] workspace %s", ws->name);
sasprintf(&name, "[i3 con] workspace %s", ws->name);
x_set_name(ws, name);
free(name);
@ -562,7 +562,7 @@ static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
new->id = id;
new->primary = (primary && primary->output == id);
FREE(new->name);
asprintf(&new->name, "%.*s",
sasprintf(&new->name, "%.*s",
xcb_randr_get_output_info_name_length(output),
xcb_randr_get_output_info_name(output));

View File

@ -85,7 +85,7 @@ void exec_i3_utility(char *name, char *argv[]) {
* argv[0]s dirname */
char *pathbuf = strdup(start_argv[0]);
char *dir = dirname(pathbuf);
asprintf(&migratepath, "%s/%s", dir, name);
sasprintf(&migratepath, "%s/%s", dir, name);
argv[0] = migratepath;
execvp(migratepath, argv);
@ -97,7 +97,7 @@ void exec_i3_utility(char *name, char *argv[]) {
exit(1);
}
dir = dirname(buffer);
asprintf(&migratepath, "%s/%s", dir, name);
sasprintf(&migratepath, "%s/%s", dir, name);
argv[0] = migratepath;
execvp(migratepath, argv);
#endif
@ -237,16 +237,10 @@ char *get_process_filename(const char *prefix) {
if (dir == NULL) {
struct passwd *pw = getpwuid(getuid());
const char *username = pw ? pw->pw_name : "unknown";
if (asprintf(&dir, "/tmp/i3-%s", username) == -1) {
perror("asprintf()");
return NULL;
}
sasprintf(&dir, "/tmp/i3-%s", username);
} else {
char *tmp;
if (asprintf(&tmp, "%s/i3", dir) == -1) {
perror("asprintf()");
return NULL;
}
sasprintf(&tmp, "%s/i3", dir);
dir = tmp;
}
if (!path_exists(dir)) {
@ -256,11 +250,7 @@ char *get_process_filename(const char *prefix) {
}
}
char *filename;
if (asprintf(&filename, "%s/%s.%d", dir, prefix, getpid()) == -1) {
perror("asprintf()");
filename = NULL;
}
sasprintf(&filename, "%s/%s.%d", dir, prefix, getpid());
free(dir);
return filename;
}

View File

@ -46,7 +46,7 @@ Con *workspace_get(const char *num, bool *created) {
* will handle CT_WORKSPACEs differently */
workspace = con_new(NULL, NULL);
char *name;
asprintf(&name, "[i3 con] workspace %s", num);
sasprintf(&name, "[i3 con] workspace %s", num);
x_set_name(workspace, name);
free(name);
workspace->type = CT_WORKSPACE;

View File

@ -56,7 +56,7 @@ static void query_screens(xcb_connection_t *conn) {
s->rect.height = min(s->rect.height, screen_info[screen].height);
} else {
s = scalloc(sizeof(Output));
asprintf(&(s->name), "xinerama-%d", num_screens);
sasprintf(&(s->name), "xinerama-%d", num_screens);
DLOG("Created new Xinerama screen %s (%p)\n", s->name, s);
s->active = true;
s->rect.x = screen_info[screen].x_org;