Update cc_open

This commit is contained in:
Moritz Lipp 2011-05-26 14:58:10 +02:00
parent b4f834d462
commit ec836f3334

View File

@ -18,11 +18,14 @@ cc_open(girara_session_t* session, char* input)
girara_completion_t* completion = girara_completion_init(); girara_completion_t* completion = girara_completion_init();
girara_completion_group_t* group = girara_completion_group_create(session, NULL); girara_completion_group_t* group = girara_completion_group_create(session, NULL);
gchar* path = NULL;
gchar* current_path = NULL;
if (completion == NULL || group == NULL) { if (completion == NULL || group == NULL) {
goto error_free; goto error_free;
} }
gchar* path = girara_fix_path(input); path = girara_fix_path(input);
if (path == NULL) { if (path == NULL) {
goto error_free; goto error_free;
} }
@ -52,8 +55,8 @@ cc_open(girara_session_t* session, char* input)
} }
/* Append a slash if the given argument is a directory */ /* Append a slash if the given argument is a directory */
if ((g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) && bool is_dir = (path[strlen(path) - 1] == '/') ? true : false;
(path[strlen(path) - 1] != '/')) { if ((g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) && !is_dir) {
char* tmp_path = g_strdup_printf("%s/", path); char* tmp_path = g_strdup_printf("%s/", path);
if (tmp_path == NULL) { if (tmp_path == NULL) {
goto error_free; goto error_free;
@ -61,10 +64,16 @@ cc_open(girara_session_t* session, char* input)
g_free(path); g_free(path);
path = tmp_path; path = tmp_path;
is_dir = true;
} }
gchar* current_path = (path[strlen(path)-1] == '/') ? path : dirname(path); /* get current path */
gchar* current_file = (path[strlen(path)-1] == '/') ? "" : basename(path); char* tmp = g_strdup(path);
current_path = is_dir ? g_strdup(tmp) : g_strdup(dirname(tmp));
g_free(tmp);
/* get current file */
gchar* current_file = is_dir ? "" : basename(path);
int current_file_length = strlen(current_file); int current_file_length = strlen(current_file);
/* read directory */ /* read directory */
@ -84,7 +93,13 @@ cc_open(girara_session_t* session, char* input)
goto error_free; goto error_free;
} }
char* full_path = g_strdup_printf("%s%s", current_path, e_name); if ((current_file_length > e_length) || strncmp(current_file, e_name,
current_file_length)) {
g_free(e_name);
continue;
}
char* full_path = g_strdup_printf("%s%s%s", current_path, is_dir ? "" : "/", e_name);
if (full_path == NULL) { if (full_path == NULL) {
g_free(e_name); g_free(e_name);
goto error_free; goto error_free;
@ -121,6 +136,7 @@ error_free:
girara_completion_group_free(group); girara_completion_group_free(group);
} }
g_free(current_path);
g_free(path); g_free(path);
return NULL; return NULL;