diff --git a/libr/anal/p/anal_s390_gnu.c b/libr/anal/p/anal_s390_gnu.c index 2db5f9863d..8c63c59c50 100644 --- a/libr/anal/p/anal_s390_gnu.c +++ b/libr/anal/p/anal_s390_gnu.c @@ -66,12 +66,13 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn op->size = print_insn_s390 ((bfd_vma)Offset, &disasm_obj); if (op->size < 1) { op->mnemonic = strdup ("invalid"); + op->type = R_ANAL_OP_TYPE_ILL; op->size = 2; } else { op->mnemonic = r_strbuf_drain (buf_global); buf_global = NULL; } - r_buf_free (buf_global); + r_strbuf_free (buf_global); buf_global = NULL; return op->size; } diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 83a8005971..84fd845055 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -5441,6 +5441,42 @@ static int cmd_print(void *data, const char *input) { /* hijack here for now, idk how to more cleanly integrate it */ return cmd_pdu (core, input + 2); } + if (!strncmp (input, "ushd", 4)) { // "pushd" + bool halp = true; + const char *arg = strchr (input, ' '); + if (arg) { + arg = r_str_trim_head_ro (arg + 1); + if (*arg) { + halp = false; + if (r_syscmd_pushd (arg)) { + r_core_return_code (core, 0); + } else { + r_core_return_code (core, 1); + } + } + } + if (halp) { + eprintf ("Usage: pushd [dir]\n"); + r_core_return_code (core, 1); + } + return 0; + } + if (!strncmp (input, "opd", 3)) { // "popd" + bool all = strstr (input, "-a"); + bool halp = strstr (input, "-h"); + if (halp) { + eprintf ("Usage: popd [-a]\n"); + r_core_return_code (core, 1); + } else { + if (all) { + r_syscmd_popalld (); + } else { + r_syscmd_popd (); + } + r_core_return_code (core, 0); + } + return 0; + } r_print_init_rowoffsets (core->print); off = UT64_MAX; @@ -6827,7 +6863,7 @@ static int cmd_print(void *data, const char *input) { } break; case 'o': // "po" - cmd_print_op(core, input); + cmd_print_op (core, input); break; case 'x': // "px" if (input[1] == '-' && input[2] == '-') { diff --git a/libr/include/r_util/r_sys.h b/libr/include/r_util/r_sys.h index eff40a69a8..85dc7f587c 100644 --- a/libr/include/r_util/r_sys.h +++ b/libr/include/r_util/r_sys.h @@ -139,6 +139,9 @@ R_API bool r_sys_tts(const char *txt, bool bg); /* syscmd */ R_API char *r_syscmd_ls(const char *input, int w); R_API char *r_syscmd_cat(const char *file); +R_API bool r_syscmd_pushd(const char *dir); +R_API bool r_syscmd_popd(void); +R_API void r_syscmd_popalld(void); R_API bool r_syscmd_mkdir(const char *dir); R_API char *r_syscmd_mktemp(const char *dir); R_API bool r_syscmd_mv(const char *input); diff --git a/libr/main/rabin2.c b/libr/main/rabin2.c index 4207ca0a73..799955d4c9 100644 --- a/libr/main/rabin2.c +++ b/libr/main/rabin2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2021 - pancake */ +/* radare - LGPL - Copyright 2009-2022 - pancake */ #include #include @@ -1217,6 +1217,7 @@ R_API int r_main_rabin2(int argc, const char **argv) { pj_free (pj); r_cons_flush (); r_core_fini (&core); + r_syscmd_popalld (); return 0; } diff --git a/libr/util/syscmd.c b/libr/util/syscmd.c index 000aae43a9..bdf3826870 100644 --- a/libr/util/syscmd.c +++ b/libr/util/syscmd.c @@ -9,6 +9,8 @@ #define FMT_QUIET 'q' #define FMT_EMOJI 'e' +R_TH_LOCAL RList *dirstack = NULL; + static char *showfile(char *res, const int nth, const char *fpath, const char *name, int printfmt, bool needs_newline) { #if __UNIX__ struct stat sb; @@ -572,6 +574,48 @@ R_API bool r_syscmd_mkdir(const char *dir) { return true; } +R_API bool r_syscmd_pushd(const char *input) { + if (!dirstack) { + dirstack = r_list_newf (free); + } + char *cwd = r_sys_getdir (); + if (!cwd) { + eprintf ("Where am I?\n"); + return false; + } + bool suc = r_sys_chdir (input); + if (suc) { + r_list_push (dirstack, cwd); + } else { + eprintf ("Cannot chdir\n"); + } + return suc; +} + +R_API bool r_syscmd_popd(void) { + if (!dirstack) { + return false; + } + char *d = r_list_pop (dirstack); + if (d) { + r_sys_chdir (d); + eprintf ("%s\n", d); + free (d); + } + if (r_list_empty (dirstack)) { + r_list_free (dirstack); + dirstack = NULL; + return false; + } + return true; +} + +R_API void r_syscmd_popalld(void) { + while (r_syscmd_popd ()) { + // wait for it + } +} + R_API bool r_syscmd_mv(const char *input) { if (strlen (input) < 3) { eprintf ("Usage: mv src dst\n"); diff --git a/libr/util/unum.c b/libr/util/unum.c index acf6feaea9..5bf29e99a2 100644 --- a/libr/util/unum.c +++ b/libr/util/unum.c @@ -787,14 +787,12 @@ R_API ut64 r_num_tail_base(RNum *num, ut64 addr, ut64 off) { R_API ut64 r_num_tail(RNum *num, ut64 addr, const char *hex) { ut64 mask = 0LL; ut64 n = 0; - char *p; - int i; while (*hex && (*hex == ' ' || *hex == '.')) { hex++; } - i = strlen (hex) * 4; - p = malloc (strlen (hex) + 10); + int i = strlen (hex) * 4; + char *p = malloc (strlen (hex) + 10); if (p) { strcpy (p, "0x"); strcpy (p + 2, hex); diff --git a/test/db/cmd/posixshell b/test/db/cmd/posixshell index 612b7a3e5f..3771784158 100644 --- a/test/db/cmd/posixshell +++ b/test/db/cmd/posixshell @@ -1,3 +1,24 @@ +NAME=pushd;popd +FILE=- +CMDS=<