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. */
32 #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 * g_strcasecmp() doesn't work well in some locales because it relies on
68 * the locale-specific toupper(). On the other hand, strcoll() is case
69 * sensitive in the "C" and "POSIX" locales, unlike other locales.
70 * Solution: always use strcmp() for case sensitive sort. For case
71 * insensitive sort use strcoll() if it's case insensitive for ASCII and
72 * g_strcasecmp() otherwise.
80 static int string_sortcomp (const char *str1
, const char *str2
)
82 static strcoll_status use_strcoll
= STRCOLL_TEST
;
85 return strcmp (str1
, str2
);
88 /* Initialize use_strcoll once. */
89 if (use_strcoll
== STRCOLL_TEST
) {
90 /* Only use strcoll() if it considers "B" between "a" and "c". */
91 if (strcoll ("a", "B") * strcoll ("B", "c") > 0) {
92 use_strcoll
= STRCOLL_YES
;
94 use_strcoll
= STRCOLL_NO
;
98 if (use_strcoll
== STRCOLL_NO
)
99 return g_strcasecmp (str1
, str2
);
101 return strcoll (str1
, str2
);
104 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
108 unsorted (const file_entry
*a
, const file_entry
*b
)
116 sort_name (const file_entry
*a
, const file_entry
*b
)
118 int ad
= MY_ISDIR (a
);
119 int bd
= MY_ISDIR (b
);
121 if (ad
== bd
|| mix_all_files
)
122 return string_sortcomp (a
->fname
, b
->fname
) * reverse
;
127 sort_ext (const file_entry
*a
, const file_entry
*b
)
129 const char *exta
, *extb
;
131 int ad
= MY_ISDIR (a
);
132 int bd
= MY_ISDIR (b
);
134 if (ad
== bd
|| mix_all_files
){
135 exta
= extension (a
->fname
);
136 extb
= extension (b
->fname
);
137 r
= string_sortcomp (exta
, extb
);
141 return sort_name (a
, b
);
147 sort_time (const file_entry
*a
, const file_entry
*b
)
149 int ad
= MY_ISDIR (a
);
150 int bd
= MY_ISDIR (b
);
152 if (ad
== bd
|| mix_all_files
) {
153 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
154 a
->st
.st_mtime
> b
->st
.st_mtime
;
156 return result
* reverse
;
158 return sort_name (a
, b
);
165 sort_ctime (const file_entry
*a
, const file_entry
*b
)
167 int ad
= MY_ISDIR (a
);
168 int bd
= MY_ISDIR (b
);
170 if (ad
== bd
|| mix_all_files
) {
171 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
172 a
->st
.st_ctime
> b
->st
.st_ctime
;
174 return result
* reverse
;
176 return sort_name (a
, b
);
183 sort_atime (const file_entry
*a
, const file_entry
*b
)
185 int ad
= MY_ISDIR (a
);
186 int bd
= MY_ISDIR (b
);
188 if (ad
== bd
|| mix_all_files
) {
189 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
190 a
->st
.st_atime
> b
->st
.st_atime
;
192 return result
* reverse
;
194 return sort_name (a
, b
);
201 sort_inode (const file_entry
*a
, const file_entry
*b
)
203 int ad
= MY_ISDIR (a
);
204 int bd
= MY_ISDIR (b
);
206 if (ad
== bd
|| mix_all_files
)
207 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
213 sort_size (const file_entry
*a
, const file_entry
*b
)
215 int ad
= MY_ISDIR (a
);
216 int bd
= MY_ISDIR (b
);
219 if (ad
!= bd
&& !mix_all_files
)
222 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
223 a
->st
.st_size
> b
->st
.st_size
;
225 return result
* reverse
;
227 return sort_name (a
, b
);
232 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
, int exec_first_f
)
234 int dot_dot_found
= 0;
239 /* If there is an ".." entry the caller must take care to
240 ensure that it occupies the first list element. */
241 if (!strcmp (list
->list
[0].fname
, ".."))
244 reverse
= reverse_f
? -1 : 1;
245 case_sensitive
= case_sensitive_f
;
246 exec_first
= exec_first_f
;
247 qsort (&(list
->list
) [dot_dot_found
],
248 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
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
= 0;
263 add_dotdot_to_list (dir_list
*list
, int index
)
265 /* Need to grow the *list? */
266 if (index
== list
->size
) {
267 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
268 (list
->size
+ RESIZE_STEPS
));
271 list
->size
+= RESIZE_STEPS
;
274 memset (&(list
->list
) [index
], 0, sizeof(file_entry
));
275 (list
->list
) [index
].fnamelen
= 2;
276 (list
->list
) [index
].fname
= g_strdup ("..");
277 (list
->list
) [index
].f
.link_to_dir
= 0;
278 (list
->list
) [index
].f
.stale_link
= 0;
279 (list
->list
) [index
].f
.dir_size_computed
= 0;
280 (list
->list
) [index
].f
.marked
= 0;
281 (list
->list
) [index
].st
.st_mode
= 040755;
285 /* Used to set up a directory list when there is no access to a directory */
287 set_zero_dir (dir_list
*list
)
289 return (add_dotdot_to_list (list
, 0));
292 /* If you change handle_dirent then check also handle_path. */
293 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
295 handle_dirent (dir_list
*list
, const char *filter
, struct dirent
*dp
,
296 struct stat
*buf1
, int next_free
, int *link_to_dir
,
299 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
301 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
303 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
305 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
307 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
309 * lstat() fails - such entries should be identified by
310 * buf1->st_mode being 0.
311 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
313 memset (buf1
, 0, sizeof (*buf1
));
316 if (S_ISDIR (buf1
->st_mode
))
317 tree_store_mark_checked (dp
->d_name
);
319 /* A link to a file or a directory? */
322 if (S_ISLNK (buf1
->st_mode
)) {
324 if (!mc_stat (dp
->d_name
, &buf2
))
325 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
329 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && filter
330 && !regexp_match (filter
, dp
->d_name
, match_file
))
333 /* Need to grow the *list? */
334 if (next_free
== list
->size
) {
336 g_realloc (list
->list
,
337 sizeof (file_entry
) * (list
->size
+ RESIZE_STEPS
));
340 list
->size
+= RESIZE_STEPS
;
345 /* handle_path is a simplified handle_dirent. The difference is that
346 handle_path doesn't pay attention to show_dot_files and show_backups.
347 Moreover handle_path can't be used with a filemask.
348 If you change handle_path then check also handle_dirent. */
349 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
351 handle_path (dir_list
*list
, const char *path
,
352 struct stat
*buf1
, int next_free
, int *link_to_dir
,
355 if (path
[0] == '.' && path
[1] == 0)
357 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
359 if (mc_lstat (path
, buf1
) == -1)
362 if (S_ISDIR (buf1
->st_mode
))
363 tree_store_mark_checked (path
);
365 /* A link to a file or a directory? */
368 if (S_ISLNK(buf1
->st_mode
)){
370 if (!mc_stat (path
, &buf2
))
371 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
376 /* Need to grow the *list? */
377 if (next_free
== list
->size
){
378 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
379 (list
->size
+ RESIZE_STEPS
));
382 list
->size
+= RESIZE_STEPS
;
388 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int reverse
,
389 int case_sensitive
, int exec_ff
, const char *filter
)
393 int status
, link_to_dir
, stale_link
;
397 /* ".." (if any) must be the first entry in the list */
398 if (set_zero_dir (list
) == 0)
402 dirp
= mc_opendir (path
);
404 message (1, MSG_ERROR
, _("Cannot read directory contents"));
407 tree_store_start_check (path
);
408 /* Do not add a ".." entry to the root directory */
409 if (!strcmp (path
, "/"))
411 while ((dp
= mc_readdir (dirp
))) {
413 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
418 tree_store_end_check ();
422 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
423 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
424 list
->list
[next_free
].f
.marked
= 0;
425 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
426 list
->list
[next_free
].f
.stale_link
= stale_link
;
427 list
->list
[next_free
].f
.dir_size_computed
= 0;
428 list
->list
[next_free
].st
= st
;
430 if (!(next_free
% 32))
435 do_sort (list
, sort
, next_free
- 1, reverse
, case_sensitive
, exec_ff
);
439 tree_store_end_check ();
444 link_isdir (const file_entry
*file
)
446 if (file
->f
.link_to_dir
)
453 if_link_is_exe (const char *full_name
, const file_entry
*file
)
457 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
458 return is_exe (b
.st_mode
);
463 static dir_list dir_copy
= { 0, 0 };
466 alloc_dir_copy (int size
)
470 if (dir_copy
.size
< size
){
473 for (i
= 0; i
< dir_copy
.size
; i
++) {
474 g_free (dir_copy
.list
[i
].fname
);
476 g_free (dir_copy
.list
);
480 dir_copy
.list
= g_new (file_entry
, size
);
481 for (i
= 0; i
< size
; i
++)
482 dir_copy
.list
[i
].fname
= 0;
484 dir_copy
.size
= size
;
488 /* If filter is null, then it is a match */
490 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
491 int rev
, int case_sensitive
, int exec_ff
, const char *filter
)
496 int i
, status
, link_to_dir
, stale_link
;
499 GHashTable
*marked_files
;
501 dirp
= mc_opendir (path
);
503 message (1, MSG_ERROR
, _("Cannot read directory contents"));
504 clean_dir (list
, count
);
505 return set_zero_dir (list
);
508 tree_store_start_check (path
);
509 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
510 alloc_dir_copy (list
->size
);
511 for (marked_cnt
= i
= 0; i
< count
; i
++) {
512 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
513 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
514 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
515 dir_copy
.list
[i
].f
.dir_size_computed
=
516 list
->list
[i
].f
.dir_size_computed
;
517 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
518 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
519 if (list
->list
[i
].f
.marked
) {
520 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
526 /* Add ".." except to the root directory. The ".." entry
527 (if any) must be the first in the list. */
528 if (strcmp (path
, "/") != 0) {
529 if (set_zero_dir (list
) == 0) {
530 clean_dir (list
, count
);
531 clean_dir (&dir_copy
, count
);
537 while ((dp
= mc_readdir (dirp
))) {
539 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
545 /* Norbert (Feb 12, 1997):
546 Just in case someone finds this memory leak:
547 -1 means big trouble (at the moment no memory left),
548 I don't bother with further cleanup because if one gets to
549 this point he will have more problems than a few memory
550 leaks and because one 'clean_dir' would not be enough (and
551 because I don't want to spent the time to make it working,
552 IMHO it's not worthwhile).
553 clean_dir (&dir_copy, count);
555 tree_store_end_check ();
556 g_hash_table_destroy (marked_files
);
560 list
->list
[next_free
].f
.marked
= 0;
563 * If we have marked files in the copy, scan through the copy
564 * to find matching file. Decrease number of remaining marks if
567 if (marked_cnt
> 0) {
568 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
569 list
->list
[next_free
].f
.marked
= 1;
574 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
575 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
576 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
577 list
->list
[next_free
].f
.stale_link
= stale_link
;
578 list
->list
[next_free
].f
.dir_size_computed
= 0;
579 list
->list
[next_free
].st
= st
;
581 if (!(next_free
% 16))
585 tree_store_end_check ();
586 g_hash_table_destroy (marked_files
);
588 do_sort (list
, sort
, next_free
- 1, rev
, case_sensitive
, exec_ff
);
590 clean_dir (&dir_copy
, count
);