]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-path/sd-path.c
tree-wide: replace strjoin() with path_join()
[thirdparty/systemd.git] / src / libsystemd / sd-path / sd-path.c
index 419c763668ed7fa202e0e128b5cf5ff479412890..5b6bff1139a6fd3a52ca87059bd32000a0f60ed8 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include "sd-path.h"
 
@@ -76,10 +58,7 @@ static int from_home_dir(const char *envname, const char *suffix, char **buffer,
         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;
 
@@ -93,7 +72,6 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
         _cleanup_free_ char *b = NULL;
         _cleanup_free_ const char *fn = NULL;
         const char *c = NULL;
-        char line[LINE_MAX];
         size_t n;
         int r;
 
@@ -121,9 +99,16 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
          * 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))
@@ -348,6 +333,7 @@ _public_ int sd_path_home(uint64_t type, const char *suffix, char **path) {
 
         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,
@@ -385,11 +371,7 @@ _public_ int sd_path_home(uint64_t type, const char *suffix, char **path) {
         }
 
         suffix += strspn(suffix, "/");
-
-        if (endswith(ret, "/"))
-                cc = strappend(ret, suffix);
-        else
-                cc = strjoin(ret, "/", suffix);
+        cc = path_join(ret, suffix);
 
         free(buffer);
 
@@ -454,10 +436,7 @@ static int search_from_environment(
         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);
@@ -566,19 +545,31 @@ static int get_search(uint64_t type, char ***list) {
                                                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,
@@ -601,7 +592,7 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
                 l[0] = p;
                 l[1] = NULL;
 
-                *paths = l;
+                *paths = TAKE_PTR(l);
                 return 0;
         }
 
@@ -610,34 +601,24 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
                 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;
 }