Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix unitialized 'i', fix closedir() not being called, add filter_file…

…_list
  • Loading branch information...
commit dbd51804769677c7304bd6115bec54c94531bf8a 1 parent 557b6a7
@pkrumins pkrumins authored
Showing with 21 additions and 4 deletions.
  1. +21 −4 src/utils.cpp
View
25 src/utils.cpp
@@ -14,7 +14,7 @@ find_files(const char *path)
char **files = NULL;
DIR *dp;
struct dirent *dirp;
- int i;
+ int i = 0;
if ((dp = opendir(path)) == NULL) {
return NULL;
@@ -38,17 +38,16 @@ find_files(const char *path)
if (!files[i]) {
int j;
for (j=0;j<i;j++) free(files[j]);
+ files = NULL;
goto err_1;
}
strcpy(files[i], dirp->d_name);
i++;
}
- return files;
-
err_1:
closedir(dp);
- return NULL;
+ return files;
}
void
@@ -70,6 +69,24 @@ file_list_length(char **file_list)
return i;
}
+char **
+filter_file_list(char **file_list, int (*f)(const char *file))
+{
+ char **p = file_list;
+ int nfiles = 0;
+ while (*p)
+ if (f(*p++)) nfiles++;
+ char **files = (char **)malloc(sizeof(char *)*(nfiles+1));
+ if (!files)
+ return NULL;
+ p = file_list;
+ int i = 0;
+ while (*p)
+ if (f(*p))
+ files[i++] = *p++;
+ return files;
+}
+
int
file_size(const char *path)
{
Please sign in to comment.
Something went wrong with that request. Please try again.