chiark / gitweb /
use more efficient string copying
[elogind.git] / udev / lib / libudev-enumerate.c
index abc89d5cab98b3b8d6a05039d526ad112dd3e747..c236a1c1b6d859dad8d4fccf5fc1d5a5da12a3ba 100644 (file)
@@ -3,18 +3,10 @@
  *
  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * This library 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.
  */
 
 #include <stdio.h>
@@ -269,20 +261,17 @@ static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
 {
        struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
        char path[UTIL_PATH_SIZE];
+       size_t l;
+       char *s;
        DIR *dir;
        struct dirent *dent;
 
-       util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
-       util_strlcat(path, "/", sizeof(path));
-       util_strlcat(path, basedir, sizeof(path));
-       if (subdir1 != NULL) {
-               util_strlcat(path, "/", sizeof(path));
-               util_strlcat(path, subdir1, sizeof(path));
-       }
-       if (subdir2 != NULL) {
-               util_strlcat(path, "/", sizeof(path));
-               util_strlcat(path, subdir2, sizeof(path));
-       }
+       s = path;
+       l = util_strpcpyl(&s, sizeof(path), udev_get_sys_path(udev), "/", basedir, NULL);
+       if (subdir1 != NULL)
+               l = util_strpcpyl(&s, l, "/", subdir1, NULL);
+       if (subdir2 != NULL)
+               l = util_strpcpyl(&s, l, "/", subdir2, NULL);
        dir = opendir(path);
        if (dir == NULL)
                return -1;
@@ -293,17 +282,15 @@ static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
 
                if (dent->d_name[0] == '.')
                        continue;
-               util_strlcpy(syspath, path, sizeof(syspath));
-               util_strlcat(syspath, "/", sizeof(syspath));
-               util_strlcat(syspath, dent->d_name, sizeof(syspath));
+               util_strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
                if (lstat(syspath, &statbuf) != 0)
                        continue;
                if (S_ISREG(statbuf.st_mode))
                        continue;
                if (S_ISLNK(statbuf.st_mode))
                        util_resolve_sys_link(udev, syspath, sizeof(syspath));
-               util_strlcpy(filename, syspath, sizeof(filename));
-               util_strlcat(filename, "/uevent", sizeof(filename));
+
+               util_strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
                if (stat(filename, &statbuf) != 0)
                        continue;
                if (!match_sysattr(udev_enumerate, syspath))
@@ -342,9 +329,7 @@ static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir,
        DIR *dir;
        struct dirent *dent;
 
-       util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
-       util_strlcat(path, "/", sizeof(path));
-       util_strlcat(path, basedir, sizeof(path));
+       util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), "/", basedir, NULL);
        dir = opendir(path);
        if (dir == NULL)
                return -1;
@@ -436,8 +421,7 @@ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
 
        if (udev_enumerate == NULL)
                return -EINVAL;
-       util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
-       util_strlcat(base, "/subsystem", sizeof(base));
+       util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
        if (stat(base, &statbuf) == 0) {
                /* we have /subsystem/, forget all the old stuff */
                dbg(udev, "searching '/subsystem/*/devices/*' dir\n");
@@ -448,8 +432,7 @@ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
                dbg(udev, "searching '/class/*' dir\n");
                scan_dir(udev_enumerate, "class", NULL, NULL);
                /* if block isn't a class, scan /block/ */
-               util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
-               util_strlcat(base, "/class/block", sizeof(base));
+               util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/class/block", NULL);
                if (stat(base, &statbuf) != 0) {
                        if (match_subsystem(udev_enumerate, "block")) {
                                dbg(udev, "searching '/block/*' dir\n");
@@ -479,8 +462,7 @@ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
 
        if (udev_enumerate == NULL)
                return -EINVAL;
-       util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
-       util_strlcat(base, "/subsystem", sizeof(base));
+       util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
        if (stat(base, &statbuf) == 0)
                subsysdir = "subsystem";
        else