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 #define MY_ISDIR(x) ( (S_ISDIR (x->st.st_mode) || x->f.link_to_dir) ? 1 : 0)
51 sort_orders_t sort_orders
[SORT_TYPES_TOTAL
] = {
52 { N_("&Unsorted"), unsorted
},
53 { N_("&Name"), sort_name
},
54 { N_("&Extension"), sort_ext
},
55 { N_("&Modify time"), sort_time
},
56 { N_("&Access time"), sort_atime
},
57 { N_("C&Hange time"), sort_ctime
},
58 { N_("&Size"), sort_size
},
59 { N_("&Inode"), sort_inode
},
64 * g_strcasecmp() doesn't work well in some locales because it relies on
65 * the locale-specific toupper(). On the other hand, strcoll() is case
66 * sensitive in the "C" and "POSIX" locales, unlike other locales.
67 * Solution: always use strcmp() for case sensitive sort. For case
68 * insensitive sort use strcoll() if it's case insensitive for ASCII and
69 * g_strcasecmp() otherwise.
77 static int string_sortcomp (const char *str1
, const char *str2
)
79 static strcoll_status use_strcoll
= STRCOLL_TEST
;
82 return strcmp (str1
, str2
);
85 /* Initialize use_strcoll once. */
86 if (use_strcoll
== STRCOLL_TEST
) {
87 /* Only use strcoll() if it considers "B" between "a" and "c". */
88 if (strcoll ("a", "B") * strcoll ("B", "c") > 0) {
89 use_strcoll
= STRCOLL_YES
;
91 use_strcoll
= STRCOLL_NO
;
95 if (use_strcoll
== STRCOLL_NO
)
96 return g_strcasecmp (str1
, str2
);
98 return strcoll (str1
, str2
);
101 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
105 unsorted (const file_entry
*a
, const file_entry
*b
)
113 sort_name (const file_entry
*a
, const file_entry
*b
)
115 int ad
= MY_ISDIR (a
);
116 int bd
= MY_ISDIR (b
);
118 if (ad
== bd
|| mix_all_files
)
119 return string_sortcomp (a
->fname
, b
->fname
) * reverse
;
124 sort_ext (const file_entry
*a
, const file_entry
*b
)
126 const char *exta
, *extb
;
128 int ad
= MY_ISDIR (a
);
129 int bd
= MY_ISDIR (b
);
131 if (ad
== bd
|| mix_all_files
){
132 exta
= extension (a
->fname
);
133 extb
= extension (b
->fname
);
134 r
= string_sortcomp (exta
, extb
);
138 return sort_name (a
, b
);
144 sort_time (const file_entry
*a
, const file_entry
*b
)
146 int ad
= MY_ISDIR (a
);
147 int bd
= MY_ISDIR (b
);
149 if (ad
== bd
|| mix_all_files
) {
150 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
151 a
->st
.st_mtime
> b
->st
.st_mtime
;
153 return result
* reverse
;
155 return sort_name (a
, b
);
162 sort_ctime (const file_entry
*a
, const file_entry
*b
)
164 int ad
= MY_ISDIR (a
);
165 int bd
= MY_ISDIR (b
);
167 if (ad
== bd
|| mix_all_files
) {
168 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
169 a
->st
.st_ctime
> b
->st
.st_ctime
;
171 return result
* reverse
;
173 return sort_name (a
, b
);
180 sort_atime (const file_entry
*a
, const file_entry
*b
)
182 int ad
= MY_ISDIR (a
);
183 int bd
= MY_ISDIR (b
);
185 if (ad
== bd
|| mix_all_files
) {
186 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
187 a
->st
.st_atime
> b
->st
.st_atime
;
189 return result
* reverse
;
191 return sort_name (a
, b
);
198 sort_inode (const file_entry
*a
, const file_entry
*b
)
200 int ad
= MY_ISDIR (a
);
201 int bd
= MY_ISDIR (b
);
203 if (ad
== bd
|| mix_all_files
)
204 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
210 sort_size (const file_entry
*a
, const file_entry
*b
)
212 int ad
= MY_ISDIR (a
);
213 int bd
= MY_ISDIR (b
);
216 if (ad
!= bd
&& !mix_all_files
)
219 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
220 a
->st
.st_size
> b
->st
.st_size
;
222 return result
* reverse
;
224 return sort_name (a
, b
);
229 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
)
231 int dot_dot_found
= 0;
236 /* If there is an ".." entry the caller must take care to
237 ensure that it occupies the first list element. */
238 if (!strcmp (list
->list
[0].fname
, ".."))
241 reverse
= reverse_f
? -1 : 1;
242 case_sensitive
= case_sensitive_f
;
243 qsort (&(list
->list
) [dot_dot_found
],
244 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
248 clean_dir (dir_list
*list
, int count
)
252 for (i
= 0; i
< count
; i
++){
253 g_free (list
->list
[i
].fname
);
254 list
->list
[i
].fname
= 0;
259 add_dotdot_to_list (dir_list
*list
, int index
)
261 /* Need to grow the *list? */
262 if (index
== list
->size
) {
263 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
264 (list
->size
+ RESIZE_STEPS
));
267 list
->size
+= RESIZE_STEPS
;
270 memset (&(list
->list
) [index
], 0, sizeof(file_entry
));
271 (list
->list
) [index
].fnamelen
= 2;
272 (list
->list
) [index
].fname
= g_strdup ("..");
273 (list
->list
) [index
].f
.link_to_dir
= 0;
274 (list
->list
) [index
].f
.stale_link
= 0;
275 (list
->list
) [index
].f
.dir_size_computed
= 0;
276 (list
->list
) [index
].f
.marked
= 0;
277 (list
->list
) [index
].st
.st_mode
= 040755;
281 /* Used to set up a directory list when there is no access to a directory */
283 set_zero_dir (dir_list
*list
)
285 return (add_dotdot_to_list (list
, 0));
288 /* If you change handle_dirent then check also handle_path. */
289 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
291 handle_dirent (dir_list
*list
, const char *filter
, struct dirent
*dp
,
292 struct stat
*buf1
, int next_free
, int *link_to_dir
,
295 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
297 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
299 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
301 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
303 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
305 * lstat() fails - such entries should be identified by
306 * buf1->st_mode being 0.
307 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
309 memset (buf1
, 0, sizeof (*buf1
));
312 if (S_ISDIR (buf1
->st_mode
))
313 tree_store_mark_checked (dp
->d_name
);
315 /* A link to a file or a directory? */
318 if (S_ISLNK (buf1
->st_mode
)) {
320 if (!mc_stat (dp
->d_name
, &buf2
))
321 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
325 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && filter
326 && !regexp_match (filter
, dp
->d_name
, match_file
))
329 /* Need to grow the *list? */
330 if (next_free
== list
->size
) {
332 g_realloc (list
->list
,
333 sizeof (file_entry
) * (list
->size
+ RESIZE_STEPS
));
336 list
->size
+= RESIZE_STEPS
;
341 /* handle_path is a simplified handle_dirent. The difference is that
342 handle_path doesn't pay attention to show_dot_files and show_backups.
343 Moreover handle_path can't be used with a filemask.
344 If you change handle_path then check also handle_dirent. */
345 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
347 handle_path (dir_list
*list
, const char *path
,
348 struct stat
*buf1
, int next_free
, int *link_to_dir
,
351 if (path
[0] == '.' && path
[1] == 0)
353 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
355 if (mc_lstat (path
, buf1
) == -1)
358 if (S_ISDIR (buf1
->st_mode
))
359 tree_store_mark_checked (path
);
361 /* A link to a file or a directory? */
364 if (S_ISLNK(buf1
->st_mode
)){
366 if (!mc_stat (path
, &buf2
))
367 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
372 /* Need to grow the *list? */
373 if (next_free
== list
->size
){
374 list
->list
= g_realloc (list
->list
, sizeof (file_entry
) *
375 (list
->size
+ RESIZE_STEPS
));
378 list
->size
+= RESIZE_STEPS
;
384 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int reverse
,
385 int case_sensitive
, const char *filter
)
389 int status
, link_to_dir
, stale_link
;
393 /* ".." (if any) must be the first entry in the list */
394 if (set_zero_dir (list
) == 0)
398 dirp
= mc_opendir (path
);
400 message (1, MSG_ERROR
, _("Cannot read directory contents"));
403 tree_store_start_check (path
);
404 /* Do not add a ".." entry to the root directory */
405 if (!strcmp (path
, "/"))
407 while ((dp
= mc_readdir (dirp
))) {
409 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
414 tree_store_end_check ();
418 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
419 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
420 list
->list
[next_free
].f
.marked
= 0;
421 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
422 list
->list
[next_free
].f
.stale_link
= stale_link
;
423 list
->list
[next_free
].f
.dir_size_computed
= 0;
424 list
->list
[next_free
].st
= st
;
426 if (!(next_free
% 32))
431 do_sort (list
, sort
, next_free
- 1, reverse
, case_sensitive
);
435 tree_store_end_check ();
440 link_isdir (const file_entry
*file
)
442 if (file
->f
.link_to_dir
)
449 if_link_is_exe (const char *full_name
, const file_entry
*file
)
453 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
454 return is_exe (b
.st_mode
);
459 static dir_list dir_copy
= { 0, 0 };
462 alloc_dir_copy (int size
)
466 if (dir_copy
.size
< size
){
469 for (i
= 0; i
< dir_copy
.size
; i
++) {
470 g_free (dir_copy
.list
[i
].fname
);
472 g_free (dir_copy
.list
);
476 dir_copy
.list
= g_new (file_entry
, size
);
477 for (i
= 0; i
< size
; i
++)
478 dir_copy
.list
[i
].fname
= 0;
480 dir_copy
.size
= size
;
484 /* If filter is null, then it is a match */
486 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
487 int rev
, int case_sensitive
, const char *filter
)
492 int i
, status
, link_to_dir
, stale_link
;
495 GHashTable
*marked_files
;
497 dirp
= mc_opendir (path
);
499 message (1, MSG_ERROR
, _("Cannot read directory contents"));
500 clean_dir (list
, count
);
501 return set_zero_dir (list
);
504 tree_store_start_check (path
);
505 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
506 alloc_dir_copy (list
->size
);
507 for (marked_cnt
= i
= 0; i
< count
; i
++) {
508 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
509 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
510 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
511 dir_copy
.list
[i
].f
.dir_size_computed
=
512 list
->list
[i
].f
.dir_size_computed
;
513 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
514 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
515 if (list
->list
[i
].f
.marked
) {
516 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
522 /* Add ".." except to the root directory. The ".." entry
523 (if any) must be the first in the list. */
524 if (strcmp (path
, "/") != 0) {
525 if (set_zero_dir (list
) == 0) {
526 clean_dir (list
, count
);
527 clean_dir (&dir_copy
, count
);
533 while ((dp
= mc_readdir (dirp
))) {
535 handle_dirent (list
, filter
, dp
, &st
, next_free
, &link_to_dir
,
541 /* Norbert (Feb 12, 1997):
542 Just in case someone finds this memory leak:
543 -1 means big trouble (at the moment no memory left),
544 I don't bother with further cleanup because if one gets to
545 this point he will have more problems than a few memory
546 leaks and because one 'clean_dir' would not be enough (and
547 because I don't want to spent the time to make it working,
548 IMHO it's not worthwhile).
549 clean_dir (&dir_copy, count);
551 tree_store_end_check ();
552 g_hash_table_destroy (marked_files
);
556 list
->list
[next_free
].f
.marked
= 0;
559 * If we have marked files in the copy, scan through the copy
560 * to find matching file. Decrease number of remaining marks if
563 if (marked_cnt
> 0) {
564 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
565 list
->list
[next_free
].f
.marked
= 1;
570 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
571 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
572 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
573 list
->list
[next_free
].f
.stale_link
= stale_link
;
574 list
->list
[next_free
].f
.dir_size_computed
= 0;
575 list
->list
[next_free
].st
= st
;
577 if (!(next_free
% 16))
581 tree_store_end_check ();
582 g_hash_table_destroy (marked_files
);
584 do_sort (list
, sort
, next_free
- 1, rev
, case_sensitive
);
586 clean_dir (&dir_copy
, count
);