changed includes from ../mhl/ to <mhl/...>
[midnight-commander.git] / src / dir.c
blob6992f4078085eb9a7d660105f9ca3709603a991b
1 /* Directory routines
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. */
19 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <sys/stat.h>
28 #include "global.h"
29 #include "tty.h"
30 #include "dir.h"
31 #include "wtools.h"
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 ~ */
38 int show_backups = 1;
40 /* If false then directories are shown separately from files */
41 int mix_all_files = 0;
43 /* Reverse flag */
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 },
62 #ifdef HAVE_STRCOLL
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.
71 typedef enum {
72 STRCOLL_NO,
73 STRCOLL_YES,
74 STRCOLL_TEST
75 } strcoll_status;
77 static int string_sortcomp (const char *str1, const char *str2)
79 static strcoll_status use_strcoll = STRCOLL_TEST;
81 if (case_sensitive) {
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;
90 } else {
91 use_strcoll = STRCOLL_NO;
95 if (use_strcoll == STRCOLL_NO)
96 return g_strcasecmp (str1, str2);
97 else
98 return strcoll (str1, str2);
100 #else
101 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
102 #endif
105 unsorted (const file_entry *a, const file_entry *b)
107 (void) a;
108 (void) b;
109 return 0;
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;
120 return bd-ad;
124 sort_ext (const file_entry *a, const file_entry *b)
126 const char *exta, *extb;
127 int r;
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);
135 if (r)
136 return r * reverse;
137 else
138 return sort_name (a, b);
139 } else
140 return bd-ad;
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;
152 if (result != 0)
153 return result * reverse;
154 else
155 return sort_name (a, b);
157 else
158 return bd-ad;
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;
170 if (result != 0)
171 return result * reverse;
172 else
173 return sort_name (a, b);
175 else
176 return bd-ad;
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;
188 if (result != 0)
189 return result * reverse;
190 else
191 return sort_name (a, b);
193 else
194 return bd-ad;
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;
205 else
206 return bd-ad;
210 sort_size (const file_entry *a, const file_entry *b)
212 int ad = MY_ISDIR (a);
213 int bd = MY_ISDIR (b);
214 int result = 0;
216 if (ad != bd && !mix_all_files)
217 return bd - ad;
219 result = a->st.st_size < b->st.st_size ? -1 :
220 a->st.st_size > b->st.st_size;
221 if (result != 0)
222 return result * reverse;
223 else
224 return sort_name (a, b);
228 void
229 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f)
231 int dot_dot_found = 0;
233 if (top == 0)
234 return;
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, ".."))
239 dot_dot_found = 1;
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);
247 void
248 clean_dir (dir_list *list, int count)
250 int i;
252 for (i = 0; i < count; i++){
253 g_free (list->list [i].fname);
254 list->list [i].fname = 0;
258 static int
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));
265 if (!list->list)
266 return 0;
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;
278 return 1;
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 */
290 static int
291 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
292 struct stat *buf1, int next_free, int *link_to_dir,
293 int *stale_link)
295 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
296 return 0;
297 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
298 return 0;
299 if (!show_dot_files && (dp->d_name[0] == '.'))
300 return 0;
301 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
302 return 0;
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? */
316 *link_to_dir = 0;
317 *stale_link = 0;
318 if (S_ISLNK (buf1->st_mode)) {
319 struct stat buf2;
320 if (!mc_stat (dp->d_name, &buf2))
321 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
322 else
323 *stale_link = 1;
325 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
326 && !regexp_match (filter, dp->d_name, match_file))
327 return 0;
329 /* Need to grow the *list? */
330 if (next_free == list->size) {
331 list->list =
332 g_realloc (list->list,
333 sizeof (file_entry) * (list->size + RESIZE_STEPS));
334 if (!list->list)
335 return -1;
336 list->size += RESIZE_STEPS;
338 return 1;
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,
349 int *stale_link)
351 if (path [0] == '.' && path [1] == 0)
352 return 0;
353 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
354 return 0;
355 if (mc_lstat (path, buf1) == -1)
356 return 0;
358 if (S_ISDIR (buf1->st_mode))
359 tree_store_mark_checked (path);
361 /* A link to a file or a directory? */
362 *link_to_dir = 0;
363 *stale_link = 0;
364 if (S_ISLNK(buf1->st_mode)){
365 struct stat buf2;
366 if (!mc_stat (path, &buf2))
367 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
368 else
369 *stale_link = 1;
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));
376 if (!list->list)
377 return -1;
378 list->size += RESIZE_STEPS;
380 return 1;
384 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
385 int case_sensitive, const char *filter)
387 DIR *dirp;
388 struct dirent *dp;
389 int status, link_to_dir, stale_link;
390 int next_free = 0;
391 struct stat st;
393 /* ".." (if any) must be the first entry in the list */
394 if (set_zero_dir (list) == 0)
395 return next_free;
396 next_free++;
398 dirp = mc_opendir (path);
399 if (!dirp) {
400 message (1, MSG_ERROR, _("Cannot read directory contents"));
401 return next_free;
403 tree_store_start_check (path);
404 /* Do not add a ".." entry to the root directory */
405 if (!strcmp (path, "/"))
406 next_free--;
407 while ((dp = mc_readdir (dirp))) {
408 status =
409 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
410 &stale_link);
411 if (status == 0)
412 continue;
413 if (status == -1) {
414 tree_store_end_check ();
415 mc_closedir (dirp);
416 return next_free;
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;
425 next_free++;
426 if (!(next_free % 32))
427 rotate_dash ();
430 if (next_free) {
431 do_sort (list, sort, next_free - 1, reverse, case_sensitive);
434 mc_closedir (dirp);
435 tree_store_end_check ();
436 return next_free;
440 link_isdir (const file_entry *file)
442 if (file->f.link_to_dir)
443 return 1;
444 else
445 return 0;
449 if_link_is_exe (const char *full_name, const file_entry *file)
451 struct stat b;
453 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
454 return is_exe (b.st_mode);
455 } else
456 return 1;
459 static dir_list dir_copy = { 0, 0 };
461 static void
462 alloc_dir_copy (int size)
464 int i;
466 if (dir_copy.size < size){
467 if (dir_copy.list){
469 for (i = 0; i < dir_copy.size; i++) {
470 g_free (dir_copy.list [i].fname);
472 g_free (dir_copy.list);
473 dir_copy.list = 0;
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)
489 DIR *dirp;
490 struct dirent *dp;
491 int next_free = 0;
492 int i, status, link_to_dir, stale_link;
493 struct stat st;
494 int marked_cnt;
495 GHashTable *marked_files;
497 dirp = mc_opendir (path);
498 if (!dirp) {
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,
517 &dir_copy.list[i]);
518 marked_cnt++;
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);
528 return next_free;
530 next_free++;
533 while ((dp = mc_readdir (dirp))) {
534 status =
535 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
536 &stale_link);
537 if (status == 0)
538 continue;
539 if (status == -1) {
540 mc_closedir (dirp);
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);
553 return next_free;
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
561 * we copied one.
563 if (marked_cnt > 0) {
564 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
565 list->list[next_free].f.marked = 1;
566 marked_cnt--;
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;
576 next_free++;
577 if (!(next_free % 16))
578 rotate_dash ();
580 mc_closedir (dirp);
581 tree_store_end_check ();
582 g_hash_table_destroy (marked_files);
583 if (next_free) {
584 do_sort (list, sort, next_free - 1, rev, case_sensitive);
586 clean_dir (&dir_copy, count);
587 return next_free;