예제 #1
0
파일: lib.c 프로젝트: iNode/mc
void
mcview_done (mcview_t * view)
{
    /* Save current file position */
    if (mcview_remember_file_position && view->filename_vpath != NULL)
    {
        save_file_position (view->filename_vpath, -1, 0,
                            view->hex_mode ? view->hex_cursor : view->dpy_start,
                            view->saved_bookmarks);
        view->saved_bookmarks = NULL;
    }

    /* Write back the global viewer mode */
    mcview_default_hex_mode = view->hex_mode;
    mcview_default_nroff_flag = view->text_nroff_mode;
    mcview_default_magic_flag = view->magic_mode;
    mcview_global_wrap_mode = view->text_wrap_mode;

    /* Free memory used by the viewer */

    /* view->widget needs no destructor */

    vfs_path_free (view->filename_vpath);
    view->filename_vpath = NULL;
    vfs_path_free (view->workdir_vpath);
    view->workdir_vpath = NULL;
    MC_PTR_FREE (view->command);

    mcview_close_datasource (view);
    /* the growing buffer is freed with the datasource */

    coord_cache_free (view->coord_cache), view->coord_cache = NULL;

    if (view->converter == INVALID_CONV)
        view->converter = str_cnv_from_term;

    if (view->converter != str_cnv_from_term)
    {
        str_close_conv (view->converter);
        view->converter = str_cnv_from_term;
    }

    mc_search_free (view->search);
    view->search = NULL;
    g_free (view->last_search_string);
    view->last_search_string = NULL;
    mcview_nroff_seq_free (&view->search_nroff_seq);
    mcview_hexedit_free_change_list (view);

    if (mc_global.mc_run_mode == MC_RUN_VIEWER && view->dir != NULL)
    {
        /* mcviewer is the owner of file list */
        dir_list_clean (view->dir);
        g_free (view->dir->list);
        g_free (view->dir_idx);
        g_free (view->dir);
    }

    view->dir = NULL;
}
예제 #2
0
void
dir_list_load (dir_list * list, const vfs_path_t * vpath, GCompareFunc sort,
               const dir_sort_options_t * sort_op, const char *fltr)
{
    DIR *dirp;
    struct dirent *dp;
    int link_to_dir, stale_link;
    struct stat st;
    file_entry_t *fentry;
    const char *vpath_str;

    /* ".." (if any) must be the first entry in the list */
    if (!dir_list_init (list))
        return;

    fentry = &list->list[0];
    if (dir_get_dotdot_stat (vpath, &st))
        fentry->st = st;

    dirp = mc_opendir (vpath);
    if (dirp == NULL)
    {
        message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
        return;
    }

    tree_store_start_check (vpath);

    vpath_str = vfs_path_as_str (vpath);
    /* Do not add a ".." entry to the root directory */
    if (IS_PATH_SEP (vpath_str[0]) && vpath_str[1] == '\0')
        dir_list_clean (list);

    while ((dp = mc_readdir (dirp)) != NULL)
    {
        if (!handle_dirent (dp, fltr, &st, &link_to_dir, &stale_link))
            continue;

        if (!dir_list_append (list, dp->d_name, &st, link_to_dir != 0, stale_link != 0))
            goto ret;

        if ((list->len & 31) == 0)
            rotate_dash (TRUE);
    }

    dir_list_sort (list, sort, sort_op);

  ret:
    mc_closedir (dirp);
    tree_store_end_check ();
    rotate_dash (FALSE);
}
예제 #3
0
void
dir_list_reload (dir_list * list, const vfs_path_t * vpath, GCompareFunc sort,
                 const dir_sort_options_t * sort_op, const char *fltr)
{
    DIR *dirp;
    struct dirent *dp;
    int i, link_to_dir, stale_link;
    struct stat st;
    int marked_cnt;
    GHashTable *marked_files;
    const char *tmp_path;

    dirp = mc_opendir (vpath);
    if (dirp == NULL)
    {
        message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
        dir_list_clean (list);
        dir_list_init (list);
        return;
    }

    tree_store_start_check (vpath);

    marked_files = g_hash_table_new (g_str_hash, g_str_equal);
    alloc_dir_copy (list->len);
    for (marked_cnt = i = 0; i < list->len; i++)
    {
        file_entry_t *fentry, *dfentry;

        fentry = &list->list[i];
        dfentry = &dir_copy.list[i];

        dfentry->fnamelen = fentry->fnamelen;
        dfentry->fname = g_strndup (fentry->fname, fentry->fnamelen);
        dfentry->f.marked = fentry->f.marked;
        dfentry->f.dir_size_computed = fentry->f.dir_size_computed;
        dfentry->f.link_to_dir = fentry->f.link_to_dir;
        dfentry->f.stale_link = fentry->f.stale_link;
        dfentry->sort_key = NULL;
        dfentry->second_sort_key = NULL;
        if (fentry->f.marked)
        {
            g_hash_table_insert (marked_files, dfentry->fname, dfentry);
            marked_cnt++;
        }
    }

    /* save len for later dir_list_clean() */
    dir_copy.len = list->len;

    /* Add ".." except to the root directory. The ".." entry
       (if any) must be the first in the list. */
    tmp_path = vfs_path_get_by_index (vpath, 0)->path;
    if (vfs_path_elements_count (vpath) == 1 && IS_PATH_SEP (tmp_path[0]) && tmp_path[1] == '\0')
    {
        /* root directory */
        dir_list_clean (list);
    }
    else
    {
        dir_list_clean (list);
        if (!dir_list_init (list))
        {
            dir_list_clean (&dir_copy);
            return;
        }

        if (dir_get_dotdot_stat (vpath, &st))
        {
            file_entry_t *fentry;

            fentry = &list->list[0];
            fentry->st = st;
        }
    }

    while ((dp = mc_readdir (dirp)) != NULL)
    {
        file_entry_t *fentry;

        if (!handle_dirent (dp, fltr, &st, &link_to_dir, &stale_link))
            continue;

        if (!dir_list_append (list, dp->d_name, &st, link_to_dir != 0, stale_link != 0))
        {
            mc_closedir (dirp);
            /* Norbert (Feb 12, 1997):
               Just in case someone finds this memory leak:
               -1 means big trouble (at the moment no memory left),
               I don't bother with further cleanup because if one gets to
               this point he will have more problems than a few memory
               leaks and because one 'dir_list_clean' would not be enough (and
               because I don't want to spent the time to make it working,
               IMHO it's not worthwhile).
               dir_list_clean (&dir_copy);
             */
            tree_store_end_check ();
            g_hash_table_destroy (marked_files);
            return;
        }
        fentry = &list->list[list->len - 1];

        fentry->f.marked = 0;

        /*
         * If we have marked files in the copy, scan through the copy
         * to find matching file.  Decrease number of remaining marks if
         * we copied one.
         */
        if (marked_cnt > 0 && g_hash_table_lookup (marked_files, dp->d_name) != NULL)
        {
            fentry->f.marked = 1;
            marked_cnt--;
        }

        if ((list->len & 15) == 0)
            rotate_dash (TRUE);
    }
    mc_closedir (dirp);
    tree_store_end_check ();
    g_hash_table_destroy (marked_files);

    dir_list_sort (list, sort, sort_op);

    dir_list_clean (&dir_copy);
    rotate_dash (FALSE);
}