/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-***/
#include "sd-path.h"
if (r < 0)
return r;
- if (endswith(h, "/"))
- cc = strappend(h, suffix);
- else
- cc = strjoin(h, "/", suffix);
+ cc = path_join(h, suffix);
if (!cc)
return -ENOMEM;
_cleanup_free_ char *b = NULL;
_cleanup_free_ const char *fn = NULL;
const char *c = NULL;
- char line[LINE_MAX];
size_t n;
int r;
* xdg-user-dirs does upstream */
n = strlen(field);
- FOREACH_LINE(line, f, return -errno) {
+ for (;;) {
+ _cleanup_free_ char *line = NULL;
char *l, *p, *e;
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
l = strstrip(line);
if (!strneq(l, field, n))
if (IN_SET(type,
SD_PATH_SEARCH_BINARIES,
+ SD_PATH_SEARCH_BINARIES_DEFAULT,
SD_PATH_SEARCH_LIBRARY_PRIVATE,
SD_PATH_SEARCH_LIBRARY_ARCH,
SD_PATH_SEARCH_SHARED,
}
suffix += strspn(suffix, "/");
-
- if (endswith(ret, "/"))
- cc = strappend(ret, suffix);
- else
- cc = strjoin(ret, "/", suffix);
+ cc = path_join(ret, suffix);
free(buffer);
if (!h && home_suffix) {
e = secure_getenv("HOME");
if (e && path_is_absolute(e)) {
- if (endswith(e, "/"))
- h = strappend(e, home_suffix);
- else
- h = strjoin(e, "/", home_suffix);
+ h = path_join(e, home_suffix);
if (!h) {
strv_free(l);
false,
"/etc",
NULL);
- }
+
+ case SD_PATH_SEARCH_BINARIES_DEFAULT: {
+ char **t;
+
+ t = strv_split_nulstr(DEFAULT_PATH_NULSTR);
+ if (!t)
+ return -ENOMEM;
+
+ *list = t;
+ return 0;
+ }}
return -EOPNOTSUPP;
}
_public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
- char **l, **i, **j, **n;
+ char **i, **j;
+ _cleanup_strv_free_ char **l = NULL, **n = NULL;
int r;
assert_return(paths, -EINVAL);
if (!IN_SET(type,
SD_PATH_SEARCH_BINARIES,
+ SD_PATH_SEARCH_BINARIES_DEFAULT,
SD_PATH_SEARCH_LIBRARY_PRIVATE,
SD_PATH_SEARCH_LIBRARY_ARCH,
SD_PATH_SEARCH_SHARED,
l[0] = p;
l[1] = NULL;
- *paths = l;
+ *paths = TAKE_PTR(l);
return 0;
}
return r;
if (!suffix) {
- *paths = l;
+ *paths = TAKE_PTR(l);
return 0;
}
n = new(char*, strv_length(l)+1);
- if (!n) {
- strv_free(l);
+ if (!n)
return -ENOMEM;
- }
j = n;
STRV_FOREACH(i, l) {
-
- if (endswith(*i, "/"))
- *j = strappend(*i, suffix);
- else
- *j = strjoin(*i, "/", suffix);
-
- if (!*j) {
- strv_free(l);
- strv_free(n);
+ *j = path_join(*i, suffix);
+ if (!*j)
return -ENOMEM;
- }
j++;
}
*j = NULL;
- *paths = n;
+ *paths = TAKE_PTR(n);
return 0;
}