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. */
31 #include "treestore.h"
34 /* If true show files starting with a dot */
35 int show_dot_files
= 1;
37 /* If true show files ending in ~ */
40 /* If false then directories are shown separately from files */
41 int mix_all_files
= 0;
44 static int reverse
= 1;
46 /* Are the files sorted case sensitively? */
47 static int case_sensitive
= OS_SORT_CASE_SENSITIVE_DEFAULT
;
49 /* Are the exec_bit files top in list*/
50 static int exec_first
= 1;
52 #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) )
54 sort_orders_t sort_orders
[SORT_TYPES_TOTAL
] = {
55 { N_("&Unsorted"), unsorted
},
56 { N_("&Name"), sort_name
},
57 { N_("&Extension"), sort_ext
},
58 { N_("&Modify time"), sort_time
},
59 { N_("&Access time"), sort_atime
},
60 { N_("C&Hange time"), sort_ctime
},
61 { N_("&Size"), sort_size
},
62 { N_("&Inode"), sort_inode
},
67 unsorted (file_entry
*a
, file_entry
*b
)
75 sort_name (file_entry
*a
, file_entry
*b
)
77 int ad
= MY_ISDIR (a
);
78 int bd
= MY_ISDIR (b
);
80 if (ad
== bd
|| mix_all_files
) {
81 /* create key if does not exist, key will be freed after sorting */
82 if (a
->sort_key
== NULL
)
83 a
->sort_key
= str_create_key_for_filename (a
->fname
, case_sensitive
);
84 if (b
->sort_key
== NULL
)
85 b
->sort_key
= str_create_key_for_filename (b
->fname
, case_sensitive
);
87 return str_key_collate (a
->sort_key
, b
->sort_key
, case_sensitive
)
94 sort_ext (file_entry
*a
, file_entry
*b
)
97 int ad
= MY_ISDIR (a
);
98 int bd
= MY_ISDIR (b
);
100 if (ad
== bd
|| mix_all_files
){
101 if (a
->second_sort_key
== NULL
)
102 a
->second_sort_key
= str_create_key (extension (a
->fname
), case_sensitive
);
103 if (b
->second_sort_key
== NULL
)
104 b
->second_sort_key
= str_create_key (extension (b
->fname
), case_sensitive
);
106 r
= str_key_collate (a
->second_sort_key
, b
->second_sort_key
, case_sensitive
);
110 return sort_name (a
, b
);
116 sort_time (file_entry
*a
, file_entry
*b
)
118 int ad
= MY_ISDIR (a
);
119 int bd
= MY_ISDIR (b
);
121 if (ad
== bd
|| mix_all_files
) {
122 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
123 a
->st
.st_mtime
> b
->st
.st_mtime
;
125 return result
* reverse
;
127 return sort_name (a
, b
);
134 sort_ctime (file_entry
*a
, file_entry
*b
)
136 int ad
= MY_ISDIR (a
);
137 int bd
= MY_ISDIR (b
);
139 if (ad
== bd
|| mix_all_files
) {
140 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
141 a
->st
.st_ctime
> b
->st
.st_ctime
;
143 return result
* reverse
;
145 return sort_name (a
, b
);
152 sort_atime (file_entry
*a
, file_entry
*b
)
154 int ad
= MY_ISDIR (a
);
155 int bd
= MY_ISDIR (b
);
157 if (ad
== bd
|| mix_all_files
) {
158 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
159 a
->st
.st_atime
> b
->st
.st_atime
;
161 return result
* reverse
;
163 return sort_name (a
, b
);
170 sort_inode (file_entry
*a
, file_entry
*b
)
172 int ad
= MY_ISDIR (a
);
173 int bd
= MY_ISDIR (b
);
175 if (ad
== bd
|| mix_all_files
)
176 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
182 sort_size (file_entry
*a
, file_entry
*b
)
184 int ad
= MY_ISDIR (a
);
185 int bd
= MY_ISDIR (b
);
188 if (ad
!= bd
&& !mix_all_files
)
191 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
192 a
->st
.st_size
> b
->st
.st_size
;
194 return result
* reverse
;
196 return sort_name (a
, b
);
199 /* clear keys, should be call after sorting is finished */
201 clean_sort_keys (dir_list
*list
, int start
, int count
)
205 for (i
= 0; i
< count
; i
++){
206 str_release_key (list
->list
[i
+ start
].sort_key
, case_sensitive
);
207 list
->list
[i
+ start
].sort_key
= NULL
;
208 str_release_key (list
->list
[i
+ start
].second_sort_key
, case_sensitive
);
209 list
->list
[i
+ start
].second_sort_key
= NULL
;
215 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
, int exec_first_f
)
217 int dot_dot_found
= 0;
222 /* If there is an ".." entry the caller must take care to
223 ensure that it occupies the first list element. */
224 if (!strcmp (list
->list
[0].fname
, ".."))
227 reverse
= reverse_f
? -1 : 1;
228 case_sensitive
= case_sensitive_f
;
229 exec_first
= exec_first_f
;
230 qsort (&(list
->list
) [dot_dot_found
],
231 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
233 clean_sort_keys (list
, dot_dot_found
, top
+ 1 - dot_dot_found
);
237 clean_dir (dir_list
*list
, int count
)
241 for (i
= 0; i
< count
; i
++){
242 g_free (list
->list
[i
].fname
);
243 list
->list
[i
].fname
= NULL
;
248 add_dotdot_to_list (dir_list
*list
, int index
)
250 /* Need to grow the *list? */
251 if (index
== list
->size
) {
252 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
253 (list
->size
+ RESIZE_STEPS
));
256 list
->size
+= RESIZE_STEPS
;
259 memset (&(list
->list
) [index
], 0, sizeof(file_entry
));
260 (list
->list
) [index
].fnamelen
= 2;
261 (list
->list
) [index
].fname
= g_strdup ("..");
262 (list
->list
) [index
].f
.link_to_dir
= 0;
263 (list
->list
) [index
].f
.stale_link
= 0;
264 (list
->list
) [index
].f
.dir_size_computed
= 0;
265 (list
->list
) [index
].f
.marked
= 0;
266 (list
->list
) [index
].st
.st_mode
= 040755;
270 /* Used to set up a directory list when there is no access to a directory */
272 set_zero_dir (dir_list
*list
)
274 return (add_dotdot_to_list (list
, 0));
277 /* If you change handle_dirent then check also handle_path. */
278 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
280 handle_dirent (dir_list
*list
, const char *filter
, struct dirent
*dp
,
281 struct stat
*buf1
, int next_free
, int *link_to_dir
,
284 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
286 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
288 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
290 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
292 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
294 * lstat() fails - such entries should be identified by
295 * buf1->st_mode being 0.
296 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
298 memset (buf1
, 0, sizeof (*buf1
));
301 if (S_ISDIR (buf1
->st_mode
))
302 tree_store_mark_checked (dp
->d_name
);
304 /* A link to a file or a directory? */
307 if (S_ISLNK (buf1
->st_mode
)) {
309 if (!mc_stat (dp
->d_name
, &buf2
))
310 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
314 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && filter
315 && !regexp_match (filter
, dp
->d_name
, match_file
))
318 /* Need to grow the *list? */
319 if (next_free
== list
->size
) {
321 g_realloc (list
->list
,
322 sizeof (file_entry
) * (list
->size
+ RESIZE_STEPS
));
325 list
->size
+= RESIZE_STEPS
;
330 /* handle_path is a simplified handle_dirent. The difference is that
331 handle_path doesn't pay attention to show_dot_files and show_backups.
332 Moreover handle_path can't be used with a filemask.
333 If you change handle_path then check also handle_dirent. */
334 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
336 handle_path (dir_list
*list
, const char *path
,
337 struct stat
*buf1
, int next_free
, int *link_to_dir
,
340 if (path
[0] == '.' && path
[1] == 0)
342 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
344 if (mc_lstat (path
, buf1
) == -1)
347 if (S_ISDIR (buf1
->st_mode
))
348 tree_store_mark_checked (path
);
350 /* A link to a file or a directory? */
353 if (S_ISLNK(buf1
->st_mode
)){
355 if (!mc_stat (path
, &buf2
))
356 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
361 /* Need to grow the *list? */
362 if (next_free
== list
->size
){
363 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
364 (list
->size
+ RESIZE_STEPS
));
367 list
->size
+= RESIZE_STEPS
;
373 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int reverse
,
374 int case_sensitive
, int exec_ff
, const char *filter
)
378 int status
, link_to_dir
, stale_link
;
382 /* ".." (if any) must be the first entry in the list */
383 if (set_zero_dir (list
) == 0)
387 dirp
= mc_opendir (path
);
389 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
392 tree_store_start_check (path
);
393 /* Do not add a ".." entry to the root directory */
394 if (!strcmp (path
, "/"))
396 while ((dp
= mc_readdir (dirp
))) {
398 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
403 tree_store_end_check ();
407 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
408 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
409 list
->list
[next_free
].f
.marked
= 0;
410 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
411 list
->list
[next_free
].f
.stale_link
= stale_link
;
412 list
->list
[next_free
].f
.dir_size_computed
= 0;
413 list
->list
[next_free
].st
= st
;
414 list
->list
[next_free
].sort_key
= NULL
;
415 list
->list
[next_free
].second_sort_key
= NULL
;
417 if (!(next_free
% 32))
422 do_sort (list
, sort
, next_free
- 1, reverse
, case_sensitive
, exec_ff
);
426 tree_store_end_check ();
431 link_isdir (const file_entry
*file
)
433 if (file
->f
.link_to_dir
)
440 if_link_is_exe (const char *full_name
, const file_entry
*file
)
444 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
445 return is_exe (b
.st_mode
);
450 static dir_list dir_copy
= { 0, 0 };
453 alloc_dir_copy (int size
)
457 if (dir_copy
.size
< size
){
460 for (i
= 0; i
< dir_copy
.size
; i
++) {
461 g_free (dir_copy
.list
[i
].fname
);
463 g_free (dir_copy
.list
);
467 dir_copy
.list
= g_new (file_entry
, size
);
468 for (i
= 0; i
< size
; i
++) {
469 dir_copy
.list
[i
].fname
= NULL
;
470 dir_copy
.list
[i
].sort_key
= NULL
;
471 dir_copy
.list
[i
].second_sort_key
= NULL
;
474 dir_copy
.size
= size
;
478 /* If filter is null, then it is a match */
480 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
481 int rev
, int case_sensitive
, int exec_ff
, const char *filter
)
486 int i
, status
, link_to_dir
, stale_link
;
489 GHashTable
*marked_files
;
491 dirp
= mc_opendir (path
);
493 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
494 clean_dir (list
, count
);
495 return set_zero_dir (list
);
498 tree_store_start_check (path
);
499 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
500 alloc_dir_copy (list
->size
);
501 for (marked_cnt
= i
= 0; i
< count
; i
++) {
502 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
503 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
504 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
505 dir_copy
.list
[i
].f
.dir_size_computed
=
506 list
->list
[i
].f
.dir_size_computed
;
507 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
508 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
509 dir_copy
.list
[i
].sort_key
= NULL
;
510 dir_copy
.list
[i
].second_sort_key
= NULL
;
511 if (list
->list
[i
].f
.marked
) {
512 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
518 /* Add ".." except to the root directory. The ".." entry
519 (if any) must be the first in the list. */
520 if (strcmp (path
, "/") != 0) {
521 if (set_zero_dir (list
) == 0) {
522 clean_dir (list
, count
);
523 clean_dir (&dir_copy
, count
);
529 while ((dp
= mc_readdir (dirp
))) {
531 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
537 /* Norbert (Feb 12, 1997):
538 Just in case someone finds this memory leak:
539 -1 means big trouble (at the moment no memory left),
540 I don't bother with further cleanup because if one gets to
541 this point he will have more problems than a few memory
542 leaks and because one 'clean_dir' would not be enough (and
543 because I don't want to spent the time to make it working,
544 IMHO it's not worthwhile).
545 clean_dir (&dir_copy, count);
547 tree_store_end_check ();
548 g_hash_table_destroy (marked_files
);
552 list
->list
[next_free
].f
.marked
= 0;
555 * If we have marked files in the copy, scan through the copy
556 * to find matching file. Decrease number of remaining marks if
559 if (marked_cnt
> 0) {
560 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
561 list
->list
[next_free
].f
.marked
= 1;
566 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
567 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
568 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
569 list
->list
[next_free
].f
.stale_link
= stale_link
;
570 list
->list
[next_free
].f
.dir_size_computed
= 0;
571 list
->list
[next_free
].st
= st
;
572 list
->list
[next_free
].sort_key
= NULL
;
573 list
->list
[next_free
].second_sort_key
= NULL
;
575 if (!(next_free
% 16))
579 tree_store_end_check ();
580 g_hash_table_destroy (marked_files
);
582 do_sort (list
, sort
, next_free
- 1, rev
, case_sensitive
, exec_ff
);
584 clean_dir (&dir_copy
, count
);