Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tailfix #23118

Merged
merged 2 commits into from
Jul 12, 2024
Merged

Tailfix #23118

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions libr/core/cmd_type.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,22 @@ static int cmd_tac(void *data, const char *_input) { // "tac"
}

static int cmd_tail(void *data, const char *_input) { // "tail"
char *tmp, *arg;
char *input = strdup (_input);
RCore *core = (RCore *)data;
int lines = 5;
char *tmp, *arg = strchr (input, ' ');
if (r_str_startswith (input, "ail")) {
arg = input + 3;
} else {
arg = strchr (input, ' ');
}
if (arg) {
arg = (char *)r_str_trim_head_ro (arg + 1); // contains "count filename"
arg = (char *)r_str_trim_head_ro (arg);
char *count = strchr (arg, ' ');
if (count) {
*count = 0; // split the count and file name
*count = 0; // split the count and file name
tmp = (char *)r_str_trim_head_ro (count + 1);
lines = atoi (arg);
lines = r_num_math (core->num, arg);
arg = tmp;
}
}
Expand Down
7 changes: 6 additions & 1 deletion libr/util/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,13 @@ R_API char *r_file_slurp_lines_from_bottom(const char *file, int line) {
r_return_val_if_fail (file, NULL);
int i, lines = 0;
size_t sz;
if (line < 1) {
return strdup ("");
}
char *ptr = NULL, *str = r_file_slurp (file, &sz);
// TODO: Implement context
if (str) {
r_str_trim (str);
for (i = 0; str[i]; i++) {
if (str[i] == '\n') {
lines++;
Expand All @@ -772,13 +776,14 @@ R_API char *r_file_slurp_lines_from_bottom(const char *file, int line) {
return str; // number of lines requested in more than present, return all
}
i--;
line++;
for (; str[i] && line; i--) {
if (str[i] == '\n') {
line--;
}
}
ptr = str + i;
ptr = strdup (ptr);
ptr = strdup (ptr + 2);
free (str);
}
return ptr;
Expand Down
10 changes: 4 additions & 6 deletions libr/util/syscmd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2013-2023 - pancake */
/* radare - LGPL - Copyright 2013-2024 - pancake */

#include <r_core.h>
#include <errno.h>
Expand Down Expand Up @@ -373,9 +373,8 @@ R_API char *r_syscmd_head(const char *file, int count) {
}
free (filename);
return data;
} else {
eprintf ("Usage: head 7 [file]\n");
}
R_LOG_INFO ("Usage: head 7 [file]");
return NULL;
}

Expand All @@ -388,7 +387,7 @@ R_API char *r_syscmd_tail(const char *file, int count) {
p = file;
}
}
if (p && *p) {
if (R_STR_ISNOTEMPTY (p)) {
char *filename = strdup (p);
r_str_trim (filename);
char *data = r_file_slurp_lines_from_bottom (filename, count);
Expand All @@ -397,9 +396,8 @@ R_API char *r_syscmd_tail(const char *file, int count) {
}
free (filename);
return data;
} else {
eprintf ("Usage: tail 7 [file]\n");
}
R_LOG_INFO ("Usage: tail 7 [file]");
return NULL;
}

Expand Down
Loading