#include "utils.h"
#include "paths.h"
+#include "utils.h"
enum resolve_name_direction {
LEFT_TO_RIGHT,
char *
get_home_xcompose_file_path(void)
{
- int ret;
const char *home;
- char *path;
home = secure_getenv("HOME");
if (!home)
return NULL;
- ret = asprintf(&path, "%s/.XCompose", home);
- if (ret <0)
- return NULL;
-
- return path;
+ return asprintf_safe("%s/.XCompose", home);
}
char *
}
else {
const char *xlocaledir = get_xlocaledir_path();
- int ret = asprintf(&path, "%s/%s", xlocaledir, resolved);
+ path = asprintf_safe("%s/%s", xlocaledir, resolved);
free(resolved);
- if (ret < 0)
- return NULL;
}
return path;
{
const char *home, *xdg, *root;
char *user_path;
- int err;
int ret = 0;
home = secure_getenv("HOME");
xdg = secure_getenv("XDG_CONFIG_HOME");
if (xdg != NULL) {
- err = asprintf(&user_path, "%s/xkb", xdg);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/xkb", xdg);
+ if (user_path) {
ret |= xkb_context_include_path_append(ctx, user_path);
free(user_path);
}
} else if (home != NULL) {
/* XDG_CONFIG_HOME fallback is $HOME/.config/ */
- err = asprintf(&user_path, "%s/.config/xkb", home);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/.config/xkb", home);
+ if (user_path) {
ret |= xkb_context_include_path_append(ctx, user_path);
free(user_path);
}
}
if (home != NULL) {
- err = asprintf(&user_path, "%s/.xkb", home);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/.xkb", home);
+ if (user_path) {
ret |= xkb_context_include_path_append(ctx, user_path);
free(user_path);
}
{
const char *home, *xdg, *root;
char *user_path;
- int err;
bool ret = false;
if (ctx->context_state != CONTEXT_NEW) {
xdg = secure_getenv("XDG_CONFIG_HOME");
if (xdg != NULL) {
- err = asprintf(&user_path, "%s/xkb", xdg);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/xkb", xdg);
+ if (user_path) {
ret |= rxkb_context_include_path_append(ctx, user_path);
free(user_path);
}
} else if (home != NULL) {
/* XDG_CONFIG_HOME fallback is $HOME/.config/ */
- err = asprintf(&user_path, "%s/.config/xkb", home);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/.config/xkb", home);
+ if (user_path) {
ret |= rxkb_context_include_path_append(ctx, user_path);
free(user_path);
}
}
if (home != NULL) {
- err = asprintf(&user_path, "%s/.xkb", home);
- if (err >= 0) {
+ user_path = asprintf_safe("%s/.xkb", home);
+ if (user_path) {
ret |= rxkb_context_include_path_append(ctx, user_path);
free(user_path);
}
return rc >= 0 && (size_t)rc < sz;
}
+static inline char *
+ATTR_PRINTF(1, 0)
+vasprintf_safe(const char *fmt, va_list args)
+{
+ char *str;
+ int len;
+
+ len = vasprintf(&str, fmt, args);
+
+ if (len == -1)
+ return NULL;
+
+ return str;
+}
+
+/**
+ * A version of asprintf that returns the allocated string or NULL on error.
+ */
+static inline char *
+ATTR_PRINTF(1, 2)
+asprintf_safe(const char *fmt, ...)
+{
+ va_list args;
+ char *str;
+
+ va_start(args, fmt);
+ str = vasprintf_safe(fmt, args);
+ va_end(args);
+
+ return str;
+}
+
#endif /* UTILS_H */
char *
test_get_path(const char *path_rel)
{
- int ret;
char *path;
const char *srcdir;
if (path_rel[0] == '/')
return strdup(path_rel);
- ret = asprintf(&path, "%s/test/data%s%s", srcdir,
- path_rel[0] ? "/" : "", path_rel);
- if (ret < 0) {
+ path = asprintf_safe("%s/test/data%s%s", srcdir,
+ path_rel[0] ? "/" : "", path_rel);
+ if (!path) {
fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
return NULL;
}
test_include(struct xkb_context *ctx)
{
char *path, *table_string;
- int ret;
path = test_get_path("compose/en_US.UTF-8/Compose");
assert(path);
/* We don't have a mechanism to change the include paths like we
* have for keymaps. So we must include the full path. */
- ret = asprintf(&table_string,
- "<dead_tilde> <space> : \"foo\" X\n"
- "include \"%s\"\n"
- "<dead_tilde> <dead_tilde> : \"bar\" Y\n", path);
- assert(ret >= 0);
+ table_string = asprintf_safe("<dead_tilde> <space> : \"foo\" X\n"
+ "include \"%s\"\n"
+ "<dead_tilde> <dead_tilde> : \"bar\" Y\n", path);
+ assert(table_string);
assert(test_compose_seq_buffer(ctx, table_string,
/* No conflict. */
char *dirname;
int err;
- err = asprintf(&dirname, "%s/%s", parent, path);
- assert(err >= 0);
+ dirname = asprintf_safe("%s/%s", parent, path);
+ assert(dirname);
err = mkdir(dirname, 0777);
assert(err == 0);
int rc;
FILE *fp;
- rc = asprintf(&tmpdir, "/tmp/%s.%d.XXXXXX", ruleset, iteration++);
- assert(rc > 0);
+ tmpdir = asprintf_safe("/tmp/%s.%d.XXXXXX", ruleset, iteration++);
+ assert(tmpdir);
assert(mkdtemp(tmpdir) == tmpdir);
rc = snprintf_safe(buf, sizeof(buf), "%s/rules", tmpdir);