This commit is contained in:
Álvaro Felipe Melchor 2015-04-16 13:11:21 +02:00 committed by pancake
parent ec1ba93468
commit 775a780b43

View File

@ -85,11 +85,14 @@ static void showfile(const int nth, const char *fpath, const char *name, int pri
// TODO: Move into r_util .. r_print maybe? r_cons dep is anoying
R_API void r_core_syscmd_ls(const char *input) {
const char *path = ".";
char *p, *d = NULL;
char *pattern = NULL;
int printfmt = 0;
RListIter *iter;
RList *files;
char *name;
char *dir;
int off;
if (r_sandbox_enable (0)) {
eprintf ("Sandbox forbids listing directories\n");
return;
@ -104,6 +107,20 @@ R_API void r_core_syscmd_ls(const char *input) {
}
} else path = input+2;
}
if (!r_file_is_directory (path)){
p = strrchr(path, '/');
if (p){
off = p - path;
d = (char *) calloc (1, off);
if (!d) return;
memcpy (d, path, off);
path = (const char *)d;
pattern = strdup (p+1);
}else {
pattern = strdup (path);
path = ".";
}
} else pattern = strdup ("*");
if (r_file_is_regular (path)) {
showfile (0, path, path, printfmt);
return;
@ -119,12 +136,16 @@ R_API void r_core_syscmd_ls(const char *input) {
r_list_foreach (files, iter, name) {
char *n = r_str_concat (strdup (dir), name);
if (!n) break;
if (*n) showfile (nth, n, name, printfmt);
if (r_str_glob(name, pattern)){
if (*n) showfile (nth, n, name, printfmt);
nth++;
}
free (n);
nth++;
}
if (printfmt == FMT_JSON) r_cons_printf ("]");
free (dir);
if (d) free (d);
free (pattern);
r_list_free (files);
}