rework string handling for directory listing, avoid redundant string scanning
authorFelix Fietkau <nbd@openwrt.org>
Wed, 2 Jan 2013 18:24:23 +0000 (19:24 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Wed, 2 Jan 2013 18:46:17 +0000 (19:46 +0100)
file.c

diff --git a/file.c b/file.c
index 22eb04859f9cb78146b9450aad9b0ad0fabc1e98..b46f0d671ca9e4861399ab32b3fc842e8c7852c7 100644 (file)
--- a/file.c
+++ b/file.c
@@ -462,14 +462,16 @@ static void uh_file_dirlist(struct client *cl, struct path_info *pi)
        if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
                                                 alphasort)) > 0)
        {
-               memset(filename, 0, sizeof(filename));
-               memcpy(filename, pi->phys, sizeof(filename));
-               pathptr = &filename[strlen(filename)];
+               int len;
+
+               strcpy(filename, pi->phys);
+               len = strlen(filename);
+               pathptr = filename + len;
+               len = PATH_MAX - len;
 
                /* list subdirs */
                for (i = 0; i < count; i++) {
-                       strncat(filename, files[i]->d_name,
-                                       sizeof(filename) - strlen(files[i]->d_name));
+                       snprintf(pathptr, len, "%s", files[i]->d_name);
 
                        if (!stat(filename, &s) &&
                                (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
@@ -488,8 +490,7 @@ static void uh_file_dirlist(struct client *cl, struct path_info *pi)
 
                /* list files */
                for (i = 0; i < count; i++) {
-                       strncat(filename, files[i]->d_name,
-                                       sizeof(filename) - strlen(files[i]->d_name));
+                       snprintf(pathptr, len, "%s", files[i]->d_name);
 
                        if (!stat(filename, &s) &&
                                !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))