diff --git a/src/ChangeLog b/src/ChangeLog index 7becad45d..e907e91e4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2004-10-25 Andrew V. Samoilov + + * cmd.c (select_unselect_cmd): Collect repeated code from + (un)?select_cmd here. Fix rare off-by-one and memory leak if + empty pattern passed. + + (select_cmd): Use select_unselect_cmd(). + (unselect_cmd): Likewise. + 2004-10-23 Roland Illig * complete.c (environ): Don't declare environ on Cygwin. diff --git a/src/cmd.c b/src/cmd.c index 1be5b0671..df897d54a 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -471,95 +471,66 @@ void reverse_selection_cmd (void) } } -void select_cmd (void) +static void +select_unselect_cmd (const char *title, int cmd) { char *reg_exp, *reg_exp_t; int i; int c; int dirflag = 0; - reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : "."); + reg_exp = input_dialog (title, "", easy_patterns ? "*" : "."); if (!reg_exp) return; - + if (!*reg_exp) { + g_free (reg_exp); + return; + } + reg_exp_t = reg_exp; /* Check if they specified a directory */ - if (*reg_exp_t == PATH_SEP){ - dirflag = 1; - reg_exp_t++; + if (*reg_exp_t == PATH_SEP) { + dirflag = 1; + reg_exp_t++; } - if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){ - dirflag = 1; - reg_exp_t [strlen(reg_exp_t) - 1] = 0; + if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) { + dirflag = 1; + reg_exp_t[strlen (reg_exp_t) - 1] = 0; } - for (i = 0; i < current_panel->count; i++){ - if (!strcmp (current_panel->dir.list [i].fname, "..")) - continue; - if (S_ISDIR (current_panel->dir.list [i].st.st_mode)){ + for (i = 0; i < current_panel->count; i++) { + if (!strcmp (current_panel->dir.list[i].fname, "..")) + continue; + if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) { if (!dirflag) - continue; - } else { - if (dirflag) - continue; + continue; + } else { + if (dirflag) + continue; } - c = regexp_match (reg_exp_t, current_panel->dir.list [i].fname, match_file); - if (c == -1){ + c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname, + match_file); + if (c == -1) { message (1, MSG_ERROR, _(" Malformed regular expression ")); g_free (reg_exp); return; } - if (c){ - do_file_mark (current_panel, i, 1); + if (c) { + do_file_mark (current_panel, i, cmd); } } g_free (reg_exp); } +void select_cmd (void) +{ + select_unselect_cmd (_(" Select "), 1); +} + void unselect_cmd (void) { - char *reg_exp, *reg_exp_t; - int i; - int c; - int dirflag = 0; - - reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : "."); - if (!reg_exp) - return; - - reg_exp_t = reg_exp; - - /* Check if they specified directory matching */ - if (*reg_exp_t == PATH_SEP){ - dirflag = 1; - reg_exp_t ++; - } - if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){ - dirflag = 1; - reg_exp_t [strlen(reg_exp_t) - 1] = 0; - } - for (i = 0; i < current_panel->count; i++){ - if (!strcmp (current_panel->dir.list [i].fname, "..")) - continue; - if (S_ISDIR (current_panel->dir.list [i].st.st_mode)){ - if (!dirflag) - continue; - } else { - if (dirflag) - continue; - } - c = regexp_match (reg_exp_t, current_panel->dir.list [i].fname, match_file); - if (c == -1){ - message (1, MSG_ERROR, _(" Malformed regular expression ")); - g_free (reg_exp); - return; - } - if (c){ - do_file_mark (current_panel, i, 0); - } - } - g_free (reg_exp); + select_unselect_cmd (_(" Unselect "), 0); } /* Check if the file exists */