aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-02-23 11:54:37 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-02-23 11:54:37 +0000
commitd2172c04e6c53aedc9194d0a86a4686e5af31364 (patch)
treedb4e6a4a20d19005b30c96a9e504bb330e544cc1 /miscutils
parent33196372bed41405ff240f13fe63ca5587912ed2 (diff)
downloadbusybox-w32-d2172c04e6c53aedc9194d0a86a4686e5af31364.tar.gz
busybox-w32-d2172c04e6c53aedc9194d0a86a4686e5af31364.tar.bz2
busybox-w32-d2172c04e6c53aedc9194d0a86a4686e5af31364.zip
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.c31
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)
706/* Grab a character from input without requiring the return key. If the 706/* Grab a character from input without requiring the return key. If the
707 * character is ASCII \033, get more characters and assign certain sequences 707 * character is ASCII \033, get more characters and assign certain sequences
708 * special return codes. Note that this function works best with raw input. */ 708 * special return codes. Note that this function works best with raw input. */
709static int less_getch(void) 709static int less_getch(int pos)
710{ 710{
711 unsigned char input[16]; 711 unsigned char input[16];
712 unsigned i; 712 unsigned i;
713
713 again: 714 again:
715 less_gets_pos = pos;
714 memset(input, 0, sizeof(input)); 716 memset(input, 0, sizeof(input));
715 getch_nowait(input, sizeof(input)); 717 getch_nowait(input, sizeof(input));
718 less_gets_pos = -1;
716 719
717 /* Detect escape sequences (i.e. arrow keys) and handle 720 /* Detect escape sequences (i.e. arrow keys) and handle
718 * them accordingly */ 721 * them accordingly */
@@ -777,12 +780,17 @@ static void examine_file(void)
777 char *new_fname; 780 char *new_fname;
778 781
779 print_statusline("Examine: "); 782 print_statusline("Examine: ");
780 new_fname = less_gets(sizeof("Examine: ")-1); 783 new_fname = less_gets(sizeof("Examine: ") - 1);
781 if (!new_fname[0]) { 784 if (!new_fname[0]) {
782 free(new_fname);
783 status_print(); 785 status_print();
786 err:
787 free(new_fname);
784 return; 788 return;
785 } 789 }
790 if (access(new_fname, R_OK) != 0) {
791 print_statusline("Cannot read this file");
792 goto err;
793 }
786 free(filename); 794 free(filename);
787 filename = new_fname; 795 filename = new_fname;
788 /* files start by = argv. why we assume that argv is infinitely long?? 796 /* files start by = argv. why we assume that argv is infinitely long??
@@ -838,7 +846,7 @@ static void colon_process(void)
838 /* Clear the current line and print a prompt */ 846 /* Clear the current line and print a prompt */
839 print_statusline(" :"); 847 print_statusline(" :");
840 848
841 keypress = less_getch(); 849 keypress = less_getch(2);
842 switch (keypress) { 850 switch (keypress) {
843 case 'd': 851 case 'd':
844 remove_current_file(); 852 remove_current_file();
@@ -971,7 +979,7 @@ static void regex_process(void)
971 979
972static void number_process(int first_digit) 980static void number_process(int first_digit)
973{ 981{
974 int i = 1; 982 int i;
975 int num; 983 int num;
976 char num_input[sizeof(int)*4]; /* more than enough */ 984 char num_input[sizeof(int)*4]; /* more than enough */
977 char keypress; 985 char keypress;
@@ -983,8 +991,9 @@ static void number_process(int first_digit)
983 printf(":%c", first_digit); 991 printf(":%c", first_digit);
984 992
985 /* Receive input until a letter is given */ 993 /* Receive input until a letter is given */
994 i = 1;
986 while (i < sizeof(num_input)-1) { 995 while (i < sizeof(num_input)-1) {
987 num_input[i] = less_getch(); 996 num_input[i] = less_getch(i + 1);
988 if (!num_input[i] || !isdigit(num_input[i])) 997 if (!num_input[i] || !isdigit(num_input[i]))
989 break; 998 break;
990 bb_putchar(num_input[i]); 999 bb_putchar(num_input[i]);
@@ -1043,7 +1052,7 @@ static void flag_change(void)
1043 1052
1044 clear_line(); 1053 clear_line();
1045 bb_putchar('-'); 1054 bb_putchar('-');
1046 keypress = less_getch(); 1055 keypress = less_getch(1);
1047 1056
1048 switch (keypress) { 1057 switch (keypress) {
1049 case 'M': 1058 case 'M':
@@ -1068,7 +1077,7 @@ static void show_flag_status(void)
1068 1077
1069 clear_line(); 1078 clear_line();
1070 bb_putchar('_'); 1079 bb_putchar('_');
1071 keypress = less_getch(); 1080 keypress = less_getch(1);
1072 1081
1073 switch (keypress) { 1082 switch (keypress) {
1074 case 'M': 1083 case 'M':
@@ -1127,7 +1136,7 @@ static void add_mark(void)
1127 int letter; 1136 int letter;
1128 1137
1129 print_statusline("Mark: "); 1138 print_statusline("Mark: ");
1130 letter = less_getch(); 1139 letter = less_getch(sizeof("Mark: ") - 1);
1131 1140
1132 if (isalpha(letter)) { 1141 if (isalpha(letter)) {
1133 /* If we exceed 15 marks, start overwriting previous ones */ 1142 /* If we exceed 15 marks, start overwriting previous ones */
@@ -1148,7 +1157,7 @@ static void goto_mark(void)
1148 int i; 1157 int i;
1149 1158
1150 print_statusline("Go to mark: "); 1159 print_statusline("Go to mark: ");
1151 letter = less_getch(); 1160 letter = less_getch(sizeof("Go to mark: ") - 1);
1152 clear_line(); 1161 clear_line();
1153 1162
1154 if (isalpha(letter)) { 1163 if (isalpha(letter)) {
@@ -1385,7 +1394,7 @@ int less_main(int argc, char **argv)
1385 1394
1386 reinitialize(); 1395 reinitialize();
1387 while (1) { 1396 while (1) {
1388 keypress = less_getch(); 1397 keypress = less_getch(-1); /* -1: do not position cursor */
1389 keypress_process(keypress); 1398 keypress_process(keypress);
1390 } 1399 }
1391} 1400}