From 8bcd28c38647b48d7c9271bac28e35ba23f02487 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 11 Jul 2018 15:51:35 +0200 Subject: [PATCH] Implement ls -q and lsq commands to list files in quiet mode (one file per line) --- libr/util/syscmd.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/libr/util/syscmd.c b/libr/util/syscmd.c index b78abe6b86..cb125dbd40 100644 --- a/libr/util/syscmd.c +++ b/libr/util/syscmd.c @@ -72,7 +72,9 @@ static char *showfile(char *res, const int nth, const char *fpath, const char *n u_rwx = strdup ("-"); fch = isdir? 'd': '-'; #endif - if (printfmt == FMT_RAW) { + if (printfmt == 'q') { + res = r_str_appendf (res, "%s\n", nn); + } else if (printfmt == FMT_RAW) { res = r_str_appendf (res, "%c%s%s%s 1 %4d:%-4d %-10d %s\n", isdir?'d': fch, u_rwx? u_rwx: "-", @@ -85,7 +87,7 @@ static char *showfile(char *res, const int nth, const char *fpath, const char *n } res = r_str_appendf (res, "{\"name\":\"%s\",\"size\":%d,\"uid\":%d," "\"gid\":%d,\"perm\":%d,\"isdir\":%s}", - name, sz, uid, gid, perm, isdir?"true":"false"); + name, sz, uid, gid, perm, isdir? "true": "false"); } free (nn); free (u_rwx); @@ -110,6 +112,10 @@ R_API char *r_syscmd_ls(const char *input) { input = ""; path = "."; } + if (*input == 'q') { + printfmt = 'q'; + input++; + } if (r_sandbox_enable (0)) { eprintf ("Sandbox forbids listing directories\n"); return NULL; @@ -118,7 +124,15 @@ R_API char *r_syscmd_ls(const char *input) { input++; } if (*input) { - if ((!strncmp (input, "-l", 2)) || (!strncmp (input, "-j", 2))) { + if ((!strncmp (input, "-h", 2))) { + eprintf ("Usage: ls ([-l,-j,-q]) ([path]) # long, json, quiet\n"); + } else if ((!strncmp (input, "-q", 2))) { + printfmt = 'q'; + path ++; + while (*path == ' ') { + path++; + } + } else if ((!strncmp (input, "-l", 2)) || (!strncmp (input, "-j", 2))) { // mode = 'l'; if (input[2]) { printfmt = (input[2] == 'j') ? FMT_JSON : FMT_RAW; @@ -144,8 +158,8 @@ R_API char *r_syscmd_ls(const char *input) { path = (const char *)homepath; } } else if (*path == '$') { - if (!strncmp (path + 1, "home", 4) || !strncmp (path + 1, "HOME", 4)){ - homepath = r_str_home ((strlen (path)>5)? path + 6: NULL); + if (!strncmp (path + 1, "home", 4) || !strncmp (path + 1, "HOME", 4)) { + homepath = r_str_home ((strlen (path) > 5)? path + 6: NULL); if (homepath) { path = (const char *)homepath; } @@ -153,7 +167,7 @@ R_API char *r_syscmd_ls(const char *input) { } if (!r_file_is_directory (path)) { p = strrchr (path, '/'); - if (p){ + if (p) { off = p - path; d = (char *) calloc (1, off + 1); if (!d) {