2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 * \brief Source: directory routines
30 #include "../src/tty/tty.h"
32 #include "../src/search/search.h"
36 #include "treestore.h"
39 #include "util.h" /* canonicalize_pathname () */
42 /* If true show files starting with a dot */
43 int show_dot_files
= 1;
45 /* If true show files ending in ~ */
48 /* If false then directories are shown separately from files */
49 int mix_all_files
= 0;
52 * If true, SI units (1000 based) will be used for
53 * larger units (kilobyte, megabyte, ...).
54 * If false binary units (1024 based) will be used.
59 static int reverse
= 1;
61 /* Are the files sorted case sensitively? */
62 static int case_sensitive
= OS_SORT_CASE_SENSITIVE_DEFAULT
;
64 /* Are the exec_bit files top in list*/
65 static int exec_first
= 1;
67 #define MY_ISDIR(x) ( (is_exe (x->st.st_mode) && !(S_ISDIR (x->st.st_mode) || x->f.link_to_dir) && (exec_first == 1)) ? 1 : ( (S_ISDIR (x->st.st_mode) || x->f.link_to_dir) ? 2 : 0) )
69 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
70 { N_("&Unsorted"), unsorted },
71 { N_("&Name"), sort_name },
72 { N_("&Extension"), sort_ext },
73 { N_("&Modify time"), sort_time },
74 { N_("&Access time"), sort_atime },
75 { N_("C&Hange time"), sort_ctime },
76 { N_("&Size"), sort_size },
77 { N_("&Inode"), sort_inode },
82 unsorted (file_entry
*a
, file_entry
*b
)
90 sort_name (file_entry
*a
, file_entry
*b
)
92 int ad
= MY_ISDIR (a
);
93 int bd
= MY_ISDIR (b
);
95 if (ad
== bd
|| mix_all_files
) {
96 /* create key if does not exist, key will be freed after sorting */
97 if (a
->sort_key
== NULL
)
98 a
->sort_key
= str_create_key_for_filename (a
->fname
, case_sensitive
);
99 if (b
->sort_key
== NULL
)
100 b
->sort_key
= str_create_key_for_filename (b
->fname
, case_sensitive
);
102 return str_key_collate (a
->sort_key
, b
->sort_key
, case_sensitive
)
109 sort_ext (file_entry
*a
, file_entry
*b
)
112 int ad
= MY_ISDIR (a
);
113 int bd
= MY_ISDIR (b
);
115 if (ad
== bd
|| mix_all_files
){
116 if (a
->second_sort_key
== NULL
)
117 a
->second_sort_key
= str_create_key (extension (a
->fname
), case_sensitive
);
118 if (b
->second_sort_key
== NULL
)
119 b
->second_sort_key
= str_create_key (extension (b
->fname
), case_sensitive
);
121 r
= str_key_collate (a
->second_sort_key
, b
->second_sort_key
, case_sensitive
);
125 return sort_name (a
, b
);
131 sort_time (file_entry
*a
, file_entry
*b
)
133 int ad
= MY_ISDIR (a
);
134 int bd
= MY_ISDIR (b
);
136 if (ad
== bd
|| mix_all_files
) {
137 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
138 a
->st
.st_mtime
> b
->st
.st_mtime
;
140 return result
* reverse
;
142 return sort_name (a
, b
);
149 sort_ctime (file_entry
*a
, file_entry
*b
)
151 int ad
= MY_ISDIR (a
);
152 int bd
= MY_ISDIR (b
);
154 if (ad
== bd
|| mix_all_files
) {
155 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
156 a
->st
.st_ctime
> b
->st
.st_ctime
;
158 return result
* reverse
;
160 return sort_name (a
, b
);
167 sort_atime (file_entry
*a
, file_entry
*b
)
169 int ad
= MY_ISDIR (a
);
170 int bd
= MY_ISDIR (b
);
172 if (ad
== bd
|| mix_all_files
) {
173 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
174 a
->st
.st_atime
> b
->st
.st_atime
;
176 return result
* reverse
;
178 return sort_name (a
, b
);
185 sort_inode (file_entry
*a
, file_entry
*b
)
187 int ad
= MY_ISDIR (a
);
188 int bd
= MY_ISDIR (b
);
190 if (ad
== bd
|| mix_all_files
)
191 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
197 sort_size (file_entry
*a
, file_entry
*b
)
199 int ad
= MY_ISDIR (a
);
200 int bd
= MY_ISDIR (b
);
203 if (ad
!= bd
&& !mix_all_files
)
206 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
207 a
->st
.st_size
> b
->st
.st_size
;
209 return result
* reverse
;
211 return sort_name (a
, b
);
214 /* clear keys, should be call after sorting is finished */
216 clean_sort_keys (dir_list
*list
, int start
, int count
)
220 for (i
= 0; i
< count
; i
++){
221 str_release_key (list
->list
[i
+ start
].sort_key
, case_sensitive
);
222 list
->list
[i
+ start
].sort_key
= NULL
;
223 str_release_key (list
->list
[i
+ start
].second_sort_key
, case_sensitive
);
224 list
->list
[i
+ start
].second_sort_key
= NULL
;
230 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
, int exec_first_f
)
232 int dot_dot_found
= 0;
237 /* If there is an ".." entry the caller must take care to
238 ensure that it occupies the first list element. */
239 if (!strcmp (list
->list
[0].fname
, ".."))
242 reverse
= reverse_f
? -1 : 1;
243 case_sensitive
= case_sensitive_f
;
244 exec_first
= exec_first_f
;
245 qsort (&(list
->list
) [dot_dot_found
],
246 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
248 clean_sort_keys (list
, dot_dot_found
, top
+ 1 - dot_dot_found
);
252 clean_dir (dir_list
*list
, int count
)
256 for (i
= 0; i
< count
; i
++){
257 g_free (list
->list
[i
].fname
);
258 list
->list
[i
].fname
= NULL
;
262 /* Used to set up a directory list when there is no access to a directory */
264 set_zero_dir (dir_list
*list
)
266 /* Need to grow the *list? */
267 if (list
->size
== 0) {
268 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
269 (list
->size
+ RESIZE_STEPS
));
270 if (list
->list
== NULL
)
273 list
->size
+= RESIZE_STEPS
;
276 memset (&(list
->list
) [0], 0, sizeof(file_entry
));
277 list
->list
[0].fnamelen
= 2;
278 list
->list
[0].fname
= g_strdup ("..");
279 list
->list
[0].f
.link_to_dir
= 0;
280 list
->list
[0].f
.stale_link
= 0;
281 list
->list
[0].f
.dir_size_computed
= 0;
282 list
->list
[0].f
.marked
= 0;
283 list
->list
[0].st
.st_mode
= 040755;
287 /* If you change handle_dirent then check also handle_path. */
288 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
290 handle_dirent (dir_list
*list
, const char *filter
, struct dirent
*dp
,
291 struct stat
*buf1
, int next_free
, int *link_to_dir
,
294 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
296 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
298 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
300 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
302 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
304 * lstat() fails - such entries should be identified by
305 * buf1->st_mode being 0.
306 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
308 memset (buf1
, 0, sizeof (*buf1
));
311 if (S_ISDIR (buf1
->st_mode
))
312 tree_store_mark_checked (dp
->d_name
);
314 /* A link to a file or a directory? */
317 if (S_ISLNK (buf1
->st_mode
)) {
319 if (!mc_stat (dp
->d_name
, &buf2
))
320 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
324 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && filter
325 && !mc_search(filter
, dp
->d_name
, MC_SEARCH_T_GLOB
) )
328 /* Need to grow the *list? */
329 if (next_free
== list
->size
) {
331 g_realloc (list
->list
,
332 sizeof (file_entry
) * (list
->size
+ RESIZE_STEPS
));
335 list
->size
+= RESIZE_STEPS
;
340 /* get info about ".." */
342 get_dotdot_dir_stat (const char *path
, struct stat
*st
)
344 gboolean ret
= FALSE
;
346 if ((path
!= NULL
) && (path
[0] != '\0') && (st
!= NULL
)) {
350 dotdot_dir
= g_strdup_printf ("%s/../", path
);
351 canonicalize_pathname (dotdot_dir
);
352 ret
= mc_stat (dotdot_dir
, &s
) == 0;
360 /* handle_path is a simplified handle_dirent. The difference is that
361 handle_path doesn't pay attention to show_dot_files and show_backups.
362 Moreover handle_path can't be used with a filemask.
363 If you change handle_path then check also handle_dirent. */
364 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
366 handle_path (dir_list
*list
, const char *path
,
367 struct stat
*buf1
, int next_free
, int *link_to_dir
,
370 if (path
[0] == '.' && path
[1] == 0)
372 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
374 if (mc_lstat (path
, buf1
) == -1)
377 if (S_ISDIR (buf1
->st_mode
))
378 tree_store_mark_checked (path
);
380 /* A link to a file or a directory? */
383 if (S_ISLNK(buf1
->st_mode
)){
385 if (!mc_stat (path
, &buf2
))
386 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
391 /* Need to grow the *list? */
392 if (next_free
== list
->size
){
393 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
394 (list
->size
+ RESIZE_STEPS
));
397 list
->size
+= RESIZE_STEPS
;
403 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int lc_reverse
,
404 int lc_case_sensitive
, int exec_ff
, const char *filter
)
408 int status
, link_to_dir
, stale_link
;
412 /* ".." (if any) must be the first entry in the list */
413 if (!set_zero_dir (list
))
416 if (get_dotdot_dir_stat (path
, &st
))
417 list
->list
[next_free
].st
= st
;
420 dirp
= mc_opendir (path
);
422 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
426 tree_store_start_check (path
);
428 /* Do not add a ".." entry to the root directory */
429 if ((path
[0] == PATH_SEP
) && (path
[1] == '\0'))
432 while ((dp
= mc_readdir (dirp
))) {
434 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
439 tree_store_end_check ();
443 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
444 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
445 list
->list
[next_free
].f
.marked
= 0;
446 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
447 list
->list
[next_free
].f
.stale_link
= stale_link
;
448 list
->list
[next_free
].f
.dir_size_computed
= 0;
449 list
->list
[next_free
].st
= st
;
450 list
->list
[next_free
].sort_key
= NULL
;
451 list
->list
[next_free
].second_sort_key
= NULL
;
454 if ((next_free
& 31) == 0)
459 do_sort (list
, sort
, next_free
- 1, lc_reverse
, lc_case_sensitive
, exec_ff
);
462 tree_store_end_check ();
467 link_isdir (const file_entry
*file
)
469 if (file
->f
.link_to_dir
)
476 if_link_is_exe (const char *full_name
, const file_entry
*file
)
480 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
481 return is_exe (b
.st_mode
);
486 static dir_list dir_copy
= { 0, 0 };
489 alloc_dir_copy (int size
)
493 if (dir_copy
.size
< size
){
496 for (i
= 0; i
< dir_copy
.size
; i
++) {
497 g_free (dir_copy
.list
[i
].fname
);
499 g_free (dir_copy
.list
);
503 dir_copy
.list
= g_new (file_entry
, size
);
504 for (i
= 0; i
< size
; i
++) {
505 dir_copy
.list
[i
].fname
= NULL
;
506 dir_copy
.list
[i
].sort_key
= NULL
;
507 dir_copy
.list
[i
].second_sort_key
= NULL
;
510 dir_copy
.size
= size
;
514 /* If filter is null, then it is a match */
516 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
517 int rev
, int lc_case_sensitive
, int exec_ff
, const char *filter
)
522 int i
, status
, link_to_dir
, stale_link
;
525 GHashTable
*marked_files
;
527 dirp
= mc_opendir (path
);
529 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
530 clean_dir (list
, count
);
531 return set_zero_dir (list
) ? 1 : 0;
534 tree_store_start_check (path
);
535 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
536 alloc_dir_copy (list
->size
);
537 for (marked_cnt
= i
= 0; i
< count
; i
++) {
538 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
539 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
540 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
541 dir_copy
.list
[i
].f
.dir_size_computed
=
542 list
->list
[i
].f
.dir_size_computed
;
543 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
544 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
545 dir_copy
.list
[i
].sort_key
= NULL
;
546 dir_copy
.list
[i
].second_sort_key
= NULL
;
547 if (list
->list
[i
].f
.marked
) {
548 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
554 /* Add ".." except to the root directory. The ".." entry
555 (if any) must be the first in the list. */
556 if (!((path
[0] == PATH_SEP
) && (path
[1] == '\0'))) {
557 if (!set_zero_dir (list
)) {
558 clean_dir (list
, count
);
559 clean_dir (&dir_copy
, count
);
563 if (get_dotdot_dir_stat (path
, &st
))
564 list
->list
[next_free
].st
= st
;
569 while ((dp
= mc_readdir (dirp
))) {
571 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
577 /* Norbert (Feb 12, 1997):
578 Just in case someone finds this memory leak:
579 -1 means big trouble (at the moment no memory left),
580 I don't bother with further cleanup because if one gets to
581 this point he will have more problems than a few memory
582 leaks and because one 'clean_dir' would not be enough (and
583 because I don't want to spent the time to make it working,
584 IMHO it's not worthwhile).
585 clean_dir (&dir_copy, count);
587 tree_store_end_check ();
588 g_hash_table_destroy (marked_files
);
592 list
->list
[next_free
].f
.marked
= 0;
595 * If we have marked files in the copy, scan through the copy
596 * to find matching file. Decrease number of remaining marks if
599 if (marked_cnt
> 0) {
600 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
601 list
->list
[next_free
].f
.marked
= 1;
606 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
607 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
608 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
609 list
->list
[next_free
].f
.stale_link
= stale_link
;
610 list
->list
[next_free
].f
.dir_size_computed
= 0;
611 list
->list
[next_free
].st
= st
;
612 list
->list
[next_free
].sort_key
= NULL
;
613 list
->list
[next_free
].second_sort_key
= NULL
;
615 if (!(next_free
% 16))
619 tree_store_end_check ();
620 g_hash_table_destroy (marked_files
);
622 do_sort (list
, sort
, next_free
- 1, rev
, lc_case_sensitive
, exec_ff
);
624 clean_dir (&dir_copy
, count
);