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_try_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
) {
330 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
331 (list
->size
+ RESIZE_STEPS
));
332 if (list
->list
== NULL
)
334 list
->size
+= RESIZE_STEPS
;
339 /* get info about ".." */
341 get_dotdot_dir_stat (const char *path
, struct stat
*st
)
343 gboolean ret
= FALSE
;
345 if ((path
!= NULL
) && (path
[0] != '\0') && (st
!= NULL
)) {
349 dotdot_dir
= g_strdup_printf ("%s/../", path
);
350 canonicalize_pathname (dotdot_dir
);
351 ret
= mc_stat (dotdot_dir
, &s
) == 0;
359 /* handle_path is a simplified handle_dirent. The difference is that
360 handle_path doesn't pay attention to show_dot_files and show_backups.
361 Moreover handle_path can't be used with a filemask.
362 If you change handle_path then check also handle_dirent. */
363 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
365 handle_path (dir_list
*list
, const char *path
,
366 struct stat
*buf1
, int next_free
, int *link_to_dir
,
369 if (path
[0] == '.' && path
[1] == 0)
371 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
373 if (mc_lstat (path
, buf1
) == -1)
376 if (S_ISDIR (buf1
->st_mode
))
377 tree_store_mark_checked (path
);
379 /* A link to a file or a directory? */
382 if (S_ISLNK(buf1
->st_mode
)){
384 if (!mc_stat (path
, &buf2
))
385 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
390 /* Need to grow the *list? */
391 if (next_free
== list
->size
){
392 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
393 (list
->size
+ RESIZE_STEPS
));
394 if (list
->list
== NULL
)
396 list
->size
+= RESIZE_STEPS
;
402 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int lc_reverse
,
403 int lc_case_sensitive
, int exec_ff
, const char *filter
)
407 int status
, link_to_dir
, stale_link
;
411 /* ".." (if any) must be the first entry in the list */
412 if (!set_zero_dir (list
))
415 if (get_dotdot_dir_stat (path
, &st
))
416 list
->list
[next_free
].st
= st
;
419 dirp
= mc_opendir (path
);
421 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
425 tree_store_start_check (path
);
427 /* Do not add a ".." entry to the root directory */
428 if ((path
[0] == PATH_SEP
) && (path
[1] == '\0'))
431 while ((dp
= mc_readdir (dirp
))) {
433 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
438 tree_store_end_check ();
442 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
443 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
444 list
->list
[next_free
].f
.marked
= 0;
445 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
446 list
->list
[next_free
].f
.stale_link
= stale_link
;
447 list
->list
[next_free
].f
.dir_size_computed
= 0;
448 list
->list
[next_free
].st
= st
;
449 list
->list
[next_free
].sort_key
= NULL
;
450 list
->list
[next_free
].second_sort_key
= NULL
;
453 if ((next_free
& 31) == 0)
458 do_sort (list
, sort
, next_free
- 1, lc_reverse
, lc_case_sensitive
, exec_ff
);
461 tree_store_end_check ();
466 link_isdir (const file_entry
*file
)
468 if (file
->f
.link_to_dir
)
475 if_link_is_exe (const char *full_name
, const file_entry
*file
)
479 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
480 return is_exe (b
.st_mode
);
485 static dir_list dir_copy
= { 0, 0 };
488 alloc_dir_copy (int size
)
490 if (dir_copy
.size
< size
) {
493 for (i
= 0; i
< dir_copy
.size
; i
++)
494 g_free (dir_copy
.list
[i
].fname
);
495 g_free (dir_copy
.list
);
498 dir_copy
.list
= g_new0 (file_entry
, size
);
499 dir_copy
.size
= size
;
503 /* If filter is null, then it is a match */
505 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
506 int rev
, int lc_case_sensitive
, int exec_ff
, const char *filter
)
511 int i
, status
, link_to_dir
, stale_link
;
514 GHashTable
*marked_files
;
516 dirp
= mc_opendir (path
);
518 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
519 clean_dir (list
, count
);
520 return set_zero_dir (list
) ? 1 : 0;
523 tree_store_start_check (path
);
524 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
525 alloc_dir_copy (list
->size
);
526 for (marked_cnt
= i
= 0; i
< count
; i
++) {
527 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
528 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
529 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
530 dir_copy
.list
[i
].f
.dir_size_computed
=
531 list
->list
[i
].f
.dir_size_computed
;
532 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
533 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
534 dir_copy
.list
[i
].sort_key
= NULL
;
535 dir_copy
.list
[i
].second_sort_key
= NULL
;
536 if (list
->list
[i
].f
.marked
) {
537 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
543 /* Add ".." except to the root directory. The ".." entry
544 (if any) must be the first in the list. */
545 if (!((path
[0] == PATH_SEP
) && (path
[1] == '\0'))) {
546 if (!set_zero_dir (list
)) {
547 clean_dir (list
, count
);
548 clean_dir (&dir_copy
, count
);
552 if (get_dotdot_dir_stat (path
, &st
))
553 list
->list
[next_free
].st
= st
;
558 while ((dp
= mc_readdir (dirp
))) {
560 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
566 /* Norbert (Feb 12, 1997):
567 Just in case someone finds this memory leak:
568 -1 means big trouble (at the moment no memory left),
569 I don't bother with further cleanup because if one gets to
570 this point he will have more problems than a few memory
571 leaks and because one 'clean_dir' would not be enough (and
572 because I don't want to spent the time to make it working,
573 IMHO it's not worthwhile).
574 clean_dir (&dir_copy, count);
576 tree_store_end_check ();
577 g_hash_table_destroy (marked_files
);
581 list
->list
[next_free
].f
.marked
= 0;
584 * If we have marked files in the copy, scan through the copy
585 * to find matching file. Decrease number of remaining marks if
588 if (marked_cnt
> 0) {
589 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
590 list
->list
[next_free
].f
.marked
= 1;
595 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
596 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
597 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
598 list
->list
[next_free
].f
.stale_link
= stale_link
;
599 list
->list
[next_free
].f
.dir_size_computed
= 0;
600 list
->list
[next_free
].st
= st
;
601 list
->list
[next_free
].sort_key
= NULL
;
602 list
->list
[next_free
].second_sort_key
= NULL
;
604 if (!(next_free
% 16))
608 tree_store_end_check ();
609 g_hash_table_destroy (marked_files
);
611 do_sort (list
, sort
, next_free
- 1, rev
, lc_case_sensitive
, exec_ff
);
613 clean_dir (&dir_copy
, count
);