]> git.feebdaed.xyz Git - 0xmirror/radare2.git/commitdiff
Fix column width in 'ls' ##shell
authorpancake <pancake@nowsecure.com>
Thu, 11 Dec 2025 10:43:42 +0000 (11:43 +0100)
committerGitHub <noreply@github.com>
Thu, 11 Dec 2025 10:43:42 +0000 (11:43 +0100)
libr/bp/bp.c
libr/bp/bp_traptrace.c
libr/util/sys.c
libr/util/syscmd.c
test/db/cmd/cmd_fs_apfs
test/db/cmd/posixshell
test/db/cmd/shell
test/db/io/http

index 3373fad8d8fd088eed001ef1b07301b5ed7fde43..ab0d1ae317113014c6e9289fd0138fe7aff80037 100644 (file)
@@ -142,7 +142,7 @@ R_API void r_bp_enable_all(RBreakpoint *bp, int set) {
 
 R_API int r_bp_stepy_continuation(RBreakpoint *bp) {
        R_RETURN_VAL_IF_FAIL (bp, 0);
-       // TODO: implement
+       /* Return current step continuation mode */
        return bp->stepcont;
 }
 
index cecb965e77a4d5ffffd7c46a7c62d8a09a53049e..02d1a1440da0ea47f3b6ca11de600aff0e5ede4c 100644 (file)
@@ -99,7 +99,6 @@ R_API int r_bp_traptrace_add(RBreakpoint *bp, ut64 from, ut64 to) {
                free (trap);
                return false;
        }
-       // TODO: check return value
        int rd = bp->iob.read_at (bp->iob.io, from, buf, (int)len);
        if (rd != (int)len) {
                free (buf);
@@ -170,7 +169,6 @@ R_API bool r_bp_traptrace_at(RBreakpoint *bp, ut64 from, int len) {
        RListIter *iter;
        RBreakpointTrace *trace;
        r_list_foreach (bp->traces, iter, trace) {
-       // TODO: do we really need len?
                if (from>=trace->addr && from+len<=trace->addr_end) {
                        delta = (int) (from-trace->addr);
                        if (R_BIT_CHK (trace->bits, delta)) {
index f53e356c076036fc02d449325e48971c82d98422..c49691f20726c556282ff5b7dfb17c46dec338a7 100644 (file)
@@ -1601,8 +1601,6 @@ R_API void r_sys_info_free(RSysInfo *si) {
        }
 }
 
-// R2_590 r_sys_endian_tostring() // endian == R_SYS_ENDIAN_BIG "big" .. R_ARCH_CONFIG_IS_BIG_ENDIAN (core->rasm->config)? "big": "little"
-
 R_API bool r_sys_endian_is(int endian, int test) {
        return (endian & test) == test;
 }
index d8b1bf80ae056c68bb3fa30cb53d20e7eddd1996..c7c1d3e0f87fcc435603d6db27fc0bd1700b3494 100644 (file)
 
 static R_TH_LOCAL RList *dirstack = NULL;
 
-static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, const char *name, int printfmt, bool needs_newline, int column_width) {
+static void showfile(RStrBuf *sb, PJ *pj, const int nth, const char *fpath, const char *name, int printfmt, bool needs_newline, int column_width) {
 #if R2__UNIX__
-       struct stat sb;
+       struct stat st;
 #endif
        const char *n = fpath;
-       char *nn, *u_rwx = NULL;
+       char *u_rwx = NULL;
        int sz = r_file_size (n);
-       int perm, uid = 0, gid = 0;
+       int uid = 0, gid = 0;
        int fch = '-';
        if (r_str_startswith (fpath, "./")) {
                fpath = fpath + 2;
        }
        const bool isdir = r_file_is_directory (n);
-       if (isdir) {
-               nn = r_str_newf ("%s/", fpath);
-       } else {
-               nn = strdup (fpath);
-       }
-       if (!*nn) {
-               free (nn);
-               return;
-       }
-       perm = isdir? 0755: 0644;
+       char *nn = r_str_newf ("%s%s", fpath, isdir? "/": "");
+       int perm = isdir? 0755: 0644;
        if (!printfmt) {
-               r_strbuf_appendf (buf, "%-*s%s", column_width, nn, needs_newline? "\n": "  ");
+               if (needs_newline) {
+                       r_strbuf_appendf (sb, "%s\n", nn);
+               } else {
+                       r_strbuf_appendf (sb, "%-*s%s", column_width, nn, "  ");
+               }
                free (nn);
                return;
        }
@@ -43,17 +39,17 @@ static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, con
        // TODO: Implement more real info in ls -l
        // TODO: handle suid
 #if R2__UNIX__
-       if (lstat (n, &sb) != -1) {
-               ut32 ifmt = sb.st_mode & S_IFMT;
-               uid = sb.st_uid;
-               gid = sb.st_gid;
-               perm = sb.st_mode & 0777;
+       if (lstat (n, &st) != -1) {
+               ut32 ifmt = st.st_mode & S_IFMT;
+               uid = st.st_uid;
+               gid = st.st_gid;
+               perm = st.st_mode & 0777;
                if (! (u_rwx = strdup (r_str_rwx_i (perm >> 6)))) {
                        free (nn);
                        return;
                }
-               if (sb.st_mode & S_ISUID) {
-                       u_rwx[2] = (sb.st_mode & S_IXUSR)? 's': 'S';
+               if (st.st_mode & S_ISUID) {
+                       u_rwx[2] = (st.st_mode & S_IXUSR)? 's': 'S';
                }
                if (isdir) {
                        fch = 'd';
@@ -63,10 +59,8 @@ static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, con
                        case S_IFBLK: fch = 'b'; break;
                        case S_IFLNK: fch = 'l'; break;
                        case S_IFIFO: fch = 'p'; break;
-#ifndef __wasi__
-#ifdef S_IFSOCK
+#if !defined(__wasi__) && defined(S_IFSOCK)
                        case S_IFSOCK: fch = 's'; break;
-#endif
 #endif
                        }
                }
@@ -76,7 +70,7 @@ static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, con
        fch = isdir? 'd': '-';
 #endif
        if (printfmt == FMT_QUIET) {
-               r_strbuf_appendf (buf, "%s\n", nn);
+               r_strbuf_appendf (sb, "%s\n", nn);
        } else if (printfmt == FMT_EMOJI) {
                const char *eDIR = "📁";
                const char *eIMG = "🌅";
@@ -112,21 +106,19 @@ static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, con
                } else if (r_str_casestr (nn, ".jpg") || r_str_casestr (nn, ".png") || r_str_casestr (nn, ".gif") || r_str_casestr (nn, ".jpeg") || r_str_casestr (nn, ".svg")) {
                        icon = eIMG;
 #if R2__UNIX__
-               } else if ((sb.st_mode & S_IFMT) == S_IFLNK) {
-                       const char *eLNK = "📎";
-                       icon = eLNK;
-               } else if (sb.st_mode & S_ISUID) {
-                       const char *eUID = "🔼";
-                       icon = eUID;
+               } else if ((st.st_mode & S_IFMT) == S_IFLNK) {
+                       icon = "📎";
+               } else if (st.st_mode & S_ISUID) {
+                       icon = "🔼";
                } else if (perm & 1) {
                        icon = eEXE;
 #endif
                } else if (*nn == '.') {
                        icon = eHID;
                }
-               r_strbuf_appendf (buf, "%s %s\n", icon, nn);
+               r_strbuf_appendf (sb, "%s %s\n", icon, nn);
        } else if (printfmt == FMT_RAW) {
-               r_strbuf_appendf (buf, "%c%s%s%s  1 %4d:%-4d  %-10d  %s\n",
+               r_strbuf_appendf (sb, "%c%s%s%s  1 %4d:%-4d  %-10d  %s\n",
                        isdir? 'd': fch,
                        r_str_get_fail (u_rwx, "-"),
                        r_str_rwx_i ((perm >> 3) & 7),
@@ -151,8 +143,6 @@ static void showfile(RStrBuf *buf, PJ *pj, const int nth, const char *fpath, con
        free (u_rwx);
 }
 
-
-
 typedef struct {
        char *path;
        char *pattern;
@@ -199,17 +189,27 @@ static PathInfo *resolve_path_info(const char *input) {
                        free (pi);
                        return NULL;
                }
-               if (r_str_startswith (input, "-e")) {
-                       printfmt = FMT_EMOJI;
-                       path = r_str_trim_head_ro (input + 2);
-               } else if (r_str_startswith (input, "-q")) {
-                       printfmt = FMT_QUIET;
-                       path = r_str_trim_head_ro (input + 2);
-               } else if (r_str_startswith (input, "-l") || r_str_startswith (input, "-j")) {
-                       printfmt = (input[1] == 'j')? FMT_JSON: FMT_RAW;
-                       path = r_str_trim_head_ro (input + 2);
-                       if (!*path) {
-                               path = ".";
+               if (*input == '-') {
+                       switch (input[1]) {
+                       case 'e':
+                               printfmt = FMT_EMOJI;
+                               path = r_str_trim_head_ro (input + 2);
+                               break;
+                       case 'q':
+                               printfmt = FMT_QUIET;
+                               path = r_str_trim_head_ro (input + 2);
+                               break;
+                       case 'l':
+                       case 'j':
+                               printfmt = (input[1] == 'j')? FMT_JSON: FMT_RAW;
+                               path = r_str_trim_head_ro (input + 2);
+                               if (!*path) {
+                                       path = ".";
+                               }
+                               break;
+                       default:
+                               R_LOG_ERROR ("Unknown flag %s", input);
+                               break;
                        }
                } else {
                        path = input;
@@ -268,13 +268,12 @@ static void free_path_info(PathInfo *pi) {
 }
 
 R_API char *r_syscmd_ls(const char *input, int cons_width) {
-       RStrBuf *sb = r_strbuf_new ("");
        PJ *pj = NULL;
        PathInfo *pi = resolve_path_info (input);
        if (!pi) {
-               r_strbuf_free (sb);
                return NULL;
        }
+       RStrBuf *sb = r_strbuf_new ("");
        if (pi->printfmt == FMT_JSON) {
                pj = pj_new ();
                pj_a (pj);
@@ -313,7 +312,7 @@ R_API char *r_syscmd_ls(const char *input, int cons_width) {
        }
 
        char *dir = r_str_newf ("%s%s", pi->path, pi->path[strlen(pi->path) - 1] == '/' ? "" : "/");
-       const char *display_dir = (!strncmp (dir, "./", 2))? dir + 2: dir;
+       const char *display_dir = r_str_startswith (dir, "./")? dir + 2: dir;
        int max_len = strlen (display_dir) + max_name_len + 1;
        int column_width = max_len + 2;
        if (column_width > cons_width / 2) {
@@ -323,42 +322,36 @@ R_API char *r_syscmd_ls(const char *input, int cons_width) {
                column_width = 12;
        }
        int nth = 0;
-       bool needs_newline = false;
        int linelen = 0;
        r_list_foreach (files, iter, name) {
                char *n = r_str_newf ("%s%s", dir, name);
-               if (!n) {
-                       break;
-               }
                if (r_str_glob (name, pi->pattern)) {
                        if (*n) {
                                bool isdir = r_file_is_directory (n);
                                const char *display_path = r_str_startswith (n, "./")? n + 2: n;
                                int display_len = strlen (display_path) + (isdir? 1: 0);
                                int add = R_MAX (column_width, display_len) + 2;
-                               if (linelen + add > cons_width) {
-                                       if (!pj) {
-                                               if (r_strbuf_length (sb) == 0 || r_strbuf_get (sb)[r_strbuf_length (sb) - 1] != '\n') {
-                                                       r_strbuf_append (sb, "\n");
-                                               }
-                                               linelen = 0;
-                                       }
+                               bool needs_newline = (linelen + add > cons_width);
+                               showfile (sb, pj, nth, n, name, pi->printfmt, needs_newline, column_width);
+                               if (needs_newline) {
+                                       linelen = 0;
+                               } else {
+                                       linelen += add;
                                }
-                               showfile (sb, pj, nth, n, name, pi->printfmt, false, column_width);
-                               linelen += add;
                        }
                        nth++;
                }
                free (n);
        }
-       if (!pj && r_strbuf_length (sb) > 0 && r_strbuf_get (sb)[r_strbuf_length (sb) - 1] != '\n') {
-               r_strbuf_append (sb, "\n");
-       }
        char *res;
        if (pj) {
                pj_end (pj);
                res = pj_drain (pj);
        } else {
+               /// AITODO this is awful
+               if (r_strbuf_length (sb) > 0 && r_strbuf_get (sb)[r_strbuf_length (sb) - 1] != '\n') {
+                       r_strbuf_append (sb, "\n");
+               }
                res = r_strbuf_drain (sb);
        }
        free (dir);
index 722e03a873ade305965ccd978d6d1294911495e9..e2507b4ae83870fff55dacbaf3d63cf6ad0a089d 100644 (file)
@@ -6,10 +6,10 @@ echo --
 !!rafs2 -t apfs -l /bin bins/fs/apfs.img
 EOF
 EXPECT=<<EOF
+d          0  .fseventsd
 r         70  README
 d          0  bin
 r         12  hello.txt
-d          0  .fseventsd
 --
 r     154624  ls
 EOF
@@ -30,10 +30,10 @@ Block Size: 4096 bytes
 Volume Name: untitled
 Purpose: Apple's modern filesystem for macOS, iOS, etc.
 ---
+d .fseventsd
 r README
 d bin
 r hello.txt
-d .fseventsd
 ---
 r ls
 EOF
index 12328168a0a07acb29e925671b9fbaf39737c390..91d719e0e0e1bc5fa533b7ffbee59093132a7b8e 100644 (file)
@@ -67,8 +67,7 @@ rm /tmp/foo/bar
 rm /tmp/foo
 EOF
 EXPECT=<<EOF
-/tmp/foo/bar/foo/./  /tmp/foo/bar/foo/../  /tmp/foo/bar/foo/fooz
-EOF
-EXPECT_ERR=<<EOF
+/tmp/foo/bar/foo/./       /tmp/foo/bar/foo/../      /tmp/foo/bar/foo/fooz
 EOF
+EXPECT_ERR=
 RUN
index ed7d8637a04a0d36cf6266b47c7b1ee2dcadd3a9..1836a56c581af8d77a14875bd461746746608817 100644 (file)
@@ -17,7 +17,7 @@ ls .foo.touch
 rm .foo.touch
 EOF
 EXPECT=<<EOF
-        .foo.touch  
+.foo.touch        
 EOF
 RUN
 
index 6016497de88c8d15c29e202caf32384b866e53a9..75ed222f1c0d43d66e4b8adb2233656a29062003 100644 (file)
@@ -1,4 +1,5 @@
 NAME=r2 http://example.org
+BROKEN=1
 FILE=http://example.org
 CMDS=<<EOF
 r