diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-23 14:54:37 +0300 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-23 14:54:37 +0300 |
commit | d2172c04e6c53aedc9194d0a86a4686e5af31364 (patch) | |
tree | db4e6a4a20d19005b30c96a9e504bb330e544cc1 /miscutils | |
parent | 33196372bed41405ff240f13fe63ca5587912ed2 (diff) |
less: make it a bit more resistant against statusline corruption.
less: "examine" command will not bomb out on bad file name now
less_main 1663 1694 +31
examine_file 87 114 +27
less_getch 138 160 +22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 80/0) Total: 80 bytes
text data bss dec hex filename
798368 740 7484 806592 c4ec0 busybox_old
798470 740 7484 806694 c4f26 busybox_unstripped
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/less.c | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 4229dfe15..246cc6e9b 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -706,13 +706,16 @@ static ssize_t getch_nowait(char* input, int sz) /* Grab a character from input without requiring the return key. If the * character is ASCII \033, get more characters and assign certain sequences * special return codes. Note that this function works best with raw input. */ -static int less_getch(void) +static int less_getch(int pos) { unsigned char input[16]; unsigned i; + again: + less_gets_pos = pos; memset(input, 0, sizeof(input)); getch_nowait(input, sizeof(input)); + less_gets_pos = -1; /* Detect escape sequences (i.e. arrow keys) and handle * them accordingly */ @@ -777,12 +780,17 @@ static void examine_file(void) char *new_fname; print_statusline("Examine: "); - new_fname = less_gets(sizeof("Examine: ")-1); + new_fname = less_gets(sizeof("Examine: ") - 1); if (!new_fname[0]) { - free(new_fname); status_print(); + err: + free(new_fname); return; } + if (access(new_fname, R_OK) != 0) { + print_statusline("Cannot read this file"); + goto err; + } free(filename); filename = new_fname; /* files start by = argv. why we assume that argv is infinitely long?? @@ -838,7 +846,7 @@ static void colon_process(void) /* Clear the current line and print a prompt */ print_statusline(" :"); - keypress = less_getch(); + keypress = less_getch(2); switch (keypress) { case 'd': remove_current_file(); @@ -971,7 +979,7 @@ static void regex_process(void) static void number_process(int first_digit) { - int i = 1; + int i; int num; char num_input[sizeof(int)*4]; /* more than enough */ char keypress; @@ -983,8 +991,9 @@ static void number_process(int first_digit) printf(":%c", first_digit); /* Receive input until a letter is given */ + i = 1; while (i < sizeof(num_input)-1) { - num_input[i] = less_getch(); + num_input[i] = less_getch(i + 1); if (!num_input[i] || !isdigit(num_input[i])) break; bb_putchar(num_input[i]); @@ -1043,7 +1052,7 @@ static void flag_change(void) clear_line(); bb_putchar('-'); - keypress = less_getch(); + keypress = less_getch(1); switch (keypress) { case 'M': @@ -1068,7 +1077,7 @@ static void show_flag_status(void) clear_line(); bb_putchar('_'); - keypress = less_getch(); + keypress = less_getch(1); switch (keypress) { case 'M': @@ -1127,7 +1136,7 @@ static void add_mark(void) int letter; print_statusline("Mark: "); - letter = less_getch(); + letter = less_getch(sizeof("Mark: ") - 1); if (isalpha(letter)) { /* If we exceed 15 marks, start overwriting previous ones */ @@ -1148,7 +1157,7 @@ static void goto_mark(void) int i; print_statusline("Go to mark: "); - letter = less_getch(); + letter = less_getch(sizeof("Go to mark: ") - 1); clear_line(); if (isalpha(letter)) { @@ -1385,7 +1394,7 @@ int less_main(int argc, char **argv) reinitialize(); while (1) { - keypress = less_getch(); + keypress = less_getch(-1); /* -1: do not position cursor */ keypress_process(keypress); } } |