📄 dir.c
字号:
/* Directory routines
Copyright (C) 1994 Miguel de Icaza.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <config.h>
#include "tty.h"
#include "fs.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include "x.h"
#include "mad.h"
#include "global.h"
#define DIR_H_INCLUDE_HANDLE_DIRENT
#include "dir.h"
#include "util.h"
#include "tree.h"
#include "../vfs/vfs.h"
/* "$Id: dir.c 15091 2005-05-07 21:24:31Z sedwards $" */
/* If true show files starting with a dot */
int show_dot_files = 1;
/* If true show files ending in ~ */
int show_backups = 0;
/* If false then directories are shown separately from files */
int mix_all_files = 0;
/* Reverse flag */
static int reverse = 1;
/* Are the files sorted case sensitively? */
static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
#define MY_ISDIR(x) ( (S_ISDIR (x->buf.st_mode) || x->f.link_to_dir) ? 1 : 0)
sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
{ N_("&Unsorted"), unsorted },
{ N_("&Name"), sort_name },
{ N_("&Extension"), sort_ext },
{ N_("&Modify time"), sort_time },
{ N_("&Access time"), sort_atime },
{ N_("&Change time"), sort_ctime },
{ N_("&Size"), sort_size },
{ N_("&Inode"), sort_inode },
/* New sort orders */
{ N_("&Type"), sort_type },
{ N_("&Links"), sort_links },
{ N_("N&GID"), sort_ngid },
{ N_("N&UID"), sort_nuid },
{ N_("&Owner"), sort_owner },
{ N_("&Group"), sort_group }
};
#define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : strcasecmp (a,b))
int
unsorted (const file_entry *a, const file_entry *b)
{
return 0;
}
int
sort_name (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return string_sortcomp (a->fname, b->fname) * reverse;
return bd-ad;
}
int
sort_ext (const file_entry *a, const file_entry *b)
{
char *exta, *extb;
int r;
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files){
exta = extension (a->fname);
extb = extension (b->fname);
r = string_sortcomp (exta, extb);
if (r)
return r * reverse;
else
return sort_name (a, b);
} else
return bd-ad;
}
int
sort_owner (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return string_sortcomp (get_owner (a->buf.st_uid), get_owner (a->buf.st_uid)) * reverse;
return bd-ad;
}
int
sort_group (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return string_sortcomp (get_group (a->buf.st_gid), get_group (a->buf.st_gid)) * reverse;
return bd-ad;
}
int
sort_time (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (a->buf.st_mtime - b->buf.st_mtime) * reverse;
else
return bd-ad;
}
int
sort_ctime (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (a->buf.st_ctime - b->buf.st_ctime) * reverse;
else
return bd-ad;
}
int
sort_atime (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (a->buf.st_atime - b->buf.st_atime) * reverse;
else
return bd-ad;
}
int
sort_inode (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (a->buf.st_ino - b->buf.st_ino) * reverse;
else
return bd-ad;
}
int
sort_size (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (b->buf.st_size - a->buf.st_size) * reverse;
else
return bd-ad;
}
int
sort_links (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (b->buf.st_nlink - a->buf.st_nlink) * reverse;
else
return bd-ad;
}
int
sort_ngid (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (b->buf.st_gid - a->buf.st_gid) * reverse;
else
return bd-ad;
}
int
sort_nuid (const file_entry *a, const file_entry *b)
{
int ad = MY_ISDIR (a);
int bd = MY_ISDIR (b);
if (ad == bd || mix_all_files)
return (b->buf.st_uid - a->buf.st_uid) * reverse;
else
return bd-ad;
}
inline static int
file_type_to_num (const file_entry *fe)
{
const struct stat *s = &fe->buf;
if (S_ISDIR (s->st_mode))
return 0;
if (S_ISLNK (s->st_mode)){
if (fe->f.link_to_dir)
return 1;
if (fe->f.stalled_link)
return 2;
else
return 3;
}
if (S_ISSOCK (s->st_mode))
return 4;
if (S_ISCHR (s->st_mode))
return 5;
if (S_ISBLK (s->st_mode))
return 6;
if (S_ISFIFO (s->st_mode))
return 7;
if (is_exe (s->st_mode))
return 8;
return 9;
}
int
sort_type (const file_entry *a, const file_entry *b)
{
int aa = file_type_to_num (a);
int bb = file_type_to_num (b);
return bb-aa;
}
void
do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f)
{
int i;
file_entry tmp_fe;
for (i = 0; i < top + 1; i++) { /* put ".." first in list */
if (!strcmp (list->list [i].fname, "..")) {
if (i > 0) { /* swap [i] and [0] */
memcpy (&tmp_fe, &(list->list [0]), sizeof (file_entry));
memcpy (&(list->list [0]), &(list->list [i]), sizeof (file_entry));
memcpy (&(list->list [i]), &tmp_fe, sizeof (file_entry));
}
break;
}
}
reverse = reverse_f ? -1 : 1;
case_sensitive = case_sensitive_f;
qsort (&(list->list) [1], top, sizeof (file_entry), sort);
}
void clean_dir (dir_list *list, int count)
{
int i;
for (i = 0; i < count; i++){
free (list->list [i].fname);
list->list [i].fname = 0;
if (list->list [i].cache != NULL) {
free (list->list [i].cache);
list->list [i].cache = NULL;
}
}
}
static int
add_dotdot_to_list (dir_list *list, int index)
{
char buffer [MC_MAXPATHLEN + MC_MAXPATHLEN];
char *p, *s;
int i = 0;
/* Need to grow the *list? */
if (index == list->size) {
list->list = realloc (list->list, sizeof (file_entry) *
(list->size + RESIZE_STEPS));
if (!list->list)
return 0;
list->size += RESIZE_STEPS;
}
(list->list) [index].fnamelen = 2;
(list->list) [index].fname = strdup ("..");
(list->list) [index].cache = NULL;
(list->list) [index].f.link_to_dir = 0;
(list->list) [index].f.stalled_link = 0;
/* FIXME: We need to get the panel definition! to use file_mark */
(list->list) [index].f.marked = 0;
mc_get_current_wd (buffer, sizeof (buffer) - 1 );
if (buffer [strlen (buffer) - 1] == PATH_SEP)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -