Update base version to 4.6.1.
[midnight-commander.git] / src / dir.c
blob5f87c3987a6bdd0afa3dce173b4753941f9e6480
1 /* Directory routines
2 Copyright (C) 1994 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include <config.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <sys/stat.h>
27 #include "global.h"
28 #include "tty.h"
29 #include "dir.h"
30 #include "wtools.h"
31 #include "treestore.h"
33 /* If true show files starting with a dot */
34 int show_dot_files = 1;
36 /* If true show files ending in ~ */
37 int show_backups = 1;
39 /* If false then directories are shown separately from files */
40 int mix_all_files = 0;
42 /* Reverse flag */
43 static int reverse = 1;
45 /* Are the files sorted case sensitively? */
46 static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
48 #define MY_ISDIR(x) ( (S_ISDIR (x->st.st_mode) || x->f.link_to_dir) ? 1 : 0)
50 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
51 { N_("&Unsorted"), unsorted },
52 { N_("&Name"), sort_name },
53 { N_("&Extension"), sort_ext },
54 { N_("&Modify time"), sort_time },
55 { N_("&Access time"), sort_atime },
56 { N_("&Change time"), sort_ctime },
57 { N_("&Size"), sort_size },
58 { N_("&Inode"), sort_inode },
60 /* New sort orders */
61 { N_("&Type"), sort_type },
62 { N_("&Links"), sort_links },
63 { N_("N&GID"), sort_ngid },
64 { N_("N&UID"), sort_nuid },
65 { N_("&Owner"), sort_owner },
66 { N_("&Group"), sort_group }
69 #ifdef HAVE_STRCOLL
71 * g_strcasecmp() doesn't work well in some locales because it relies on
72 * the locale-specific toupper(). On the other hand, strcoll() is case
73 * sensitive in the "C" and "POSIX" locales, unlike other locales.
74 * Solution: always use strcmp() for case sensitive sort. For case
75 * insensitive sort use strcoll() if it's case insensitive for ASCII and
76 * g_strcasecmp() otherwise.
78 typedef enum {
79 STRCOLL_NO,
80 STRCOLL_YES,
81 STRCOLL_TEST
82 } strcoll_status;
84 static int string_sortcomp (const char *str1, const char *str2)
86 static strcoll_status use_strcoll = STRCOLL_TEST;
88 if (case_sensitive) {
89 return strcmp (str1, str2);
92 /* Initialize use_strcoll once. */
93 if (use_strcoll == STRCOLL_TEST) {
94 /* Only use strcoll() if it considers "B" between "a" and "c". */
95 if (strcoll ("a", "B") * strcoll ("B", "c") > 0) {
96 use_strcoll = STRCOLL_YES;
97 } else {
98 use_strcoll = STRCOLL_NO;
102 if (use_strcoll == STRCOLL_NO)
103 return g_strcasecmp (str1, str2);
104 else
105 return strcoll (str1, str2);
107 #else
108 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
109 #endif
112 unsorted (const file_entry *a, const file_entry *b)
114 (void) a;
115 (void) b;
116 return 0;
120 sort_name (const file_entry *a, const file_entry *b)
122 int ad = MY_ISDIR (a);
123 int bd = MY_ISDIR (b);
125 if (ad == bd || mix_all_files)
126 return string_sortcomp (a->fname, b->fname) * reverse;
127 return bd-ad;
131 sort_ext (const file_entry *a, const file_entry *b)
133 const char *exta, *extb;
134 int r;
135 int ad = MY_ISDIR (a);
136 int bd = MY_ISDIR (b);
138 if (ad == bd || mix_all_files){
139 exta = extension (a->fname);
140 extb = extension (b->fname);
141 r = string_sortcomp (exta, extb);
142 if (r)
143 return r * reverse;
144 else
145 return sort_name (a, b);
146 } else
147 return bd-ad;
151 sort_owner (const file_entry *a, const file_entry *b)
153 int ad = MY_ISDIR (a);
154 int bd = MY_ISDIR (b);
156 if (ad == bd || mix_all_files)
157 return string_sortcomp (get_owner (a->st.st_uid), get_owner (a->st.st_uid)) * reverse;
158 return bd-ad;
162 sort_group (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 return string_sortcomp (get_group (a->st.st_gid), get_group (a->st.st_gid)) * reverse;
169 return bd-ad;
173 sort_time (const file_entry *a, const file_entry *b)
175 int ad = MY_ISDIR (a);
176 int bd = MY_ISDIR (b);
178 if (ad == bd || mix_all_files)
179 return (a->st.st_mtime - b->st.st_mtime) * reverse;
180 else
181 return bd-ad;
185 sort_ctime (const file_entry *a, const 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_ctime - b->st.st_ctime) * reverse;
192 else
193 return bd-ad;
197 sort_atime (const file_entry *a, const file_entry *b)
199 int ad = MY_ISDIR (a);
200 int bd = MY_ISDIR (b);
202 if (ad == bd || mix_all_files)
203 return (a->st.st_atime - b->st.st_atime) * reverse;
204 else
205 return bd-ad;
209 sort_inode (const file_entry *a, const file_entry *b)
211 int ad = MY_ISDIR (a);
212 int bd = MY_ISDIR (b);
214 if (ad == bd || mix_all_files)
215 return (a->st.st_ino - b->st.st_ino) * reverse;
216 else
217 return bd-ad;
221 sort_size (const file_entry *a, const file_entry *b)
223 int ad = MY_ISDIR (a);
224 int bd = MY_ISDIR (b);
226 if (ad != bd && !mix_all_files)
227 return bd - ad;
229 return (2 * (b->st.st_size > a->st.st_size) - 1) * reverse;
233 sort_links (const file_entry *a, const file_entry *b)
235 int ad = MY_ISDIR (a);
236 int bd = MY_ISDIR (b);
238 if (ad == bd || mix_all_files)
239 return (b->st.st_nlink - a->st.st_nlink) * reverse;
240 else
241 return bd-ad;
245 sort_ngid (const file_entry *a, const file_entry *b)
247 int ad = MY_ISDIR (a);
248 int bd = MY_ISDIR (b);
250 if (ad == bd || mix_all_files)
251 return (b->st.st_gid - a->st.st_gid) * reverse;
252 else
253 return bd-ad;
257 sort_nuid (const file_entry *a, const file_entry *b)
259 int ad = MY_ISDIR (a);
260 int bd = MY_ISDIR (b);
262 if (ad == bd || mix_all_files)
263 return (b->st.st_uid - a->st.st_uid) * reverse;
264 else
265 return bd-ad;
268 inline static int
269 file_type_to_num (const file_entry *fe)
271 const struct stat *s = &fe->st;
273 if (S_ISDIR (s->st_mode))
274 return 0;
275 if (S_ISLNK (s->st_mode)){
276 if (fe->f.link_to_dir)
277 return 1;
278 if (fe->f.stale_link)
279 return 2;
280 else
281 return 3;
283 if (S_ISSOCK (s->st_mode))
284 return 4;
285 if (S_ISCHR (s->st_mode))
286 return 5;
287 if (S_ISBLK (s->st_mode))
288 return 6;
289 if (S_ISFIFO (s->st_mode))
290 return 7;
291 if (S_ISNAM (s->st_mode)) /* Special named files will be shown as block devices */
292 return 6;
293 if (is_exe (s->st_mode))
294 return 8;
295 return 9;
299 sort_type (const file_entry *a, const file_entry *b)
301 int aa = file_type_to_num (a);
302 int bb = file_type_to_num (b);
304 return bb-aa;
308 void
309 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f)
311 int i;
312 int dot_dot_found = 0;
313 file_entry tmp_fe;
315 for (i = 0; i < top + 1; i++) { /* put ".." first in list */
316 if (!strcmp (list->list [i].fname, "..")) {
317 dot_dot_found = 1;
318 if (i > 0) { /* swap [i] and [0] */
319 memcpy (&tmp_fe, &(list->list [0]), sizeof (file_entry));
320 memcpy (&(list->list [0]), &(list->list [i]), sizeof (file_entry));
321 memcpy (&(list->list [i]), &tmp_fe, sizeof (file_entry));
323 break;
327 reverse = reverse_f ? -1 : 1;
328 case_sensitive = case_sensitive_f;
329 qsort (&(list->list) [dot_dot_found],
330 top + 1 - dot_dot_found, sizeof (file_entry), sort);
333 void
334 clean_dir (dir_list *list, int count)
336 int i;
338 for (i = 0; i < count; i++){
339 g_free (list->list [i].fname);
340 list->list [i].fname = 0;
344 static int
345 add_dotdot_to_list (dir_list *list, int index)
347 /* Need to grow the *list? */
348 if (index == list->size) {
349 list->list = g_realloc (list->list, sizeof (file_entry) *
350 (list->size + RESIZE_STEPS));
351 if (!list->list)
352 return 0;
353 list->size += RESIZE_STEPS;
356 memset (&(list->list) [index], 0, sizeof(file_entry));
357 (list->list) [index].fnamelen = 2;
358 (list->list) [index].fname = g_strdup ("..");
359 (list->list) [index].f.link_to_dir = 0;
360 (list->list) [index].f.stale_link = 0;
361 (list->list) [index].f.dir_size_computed = 0;
362 (list->list) [index].f.marked = 0;
363 (list->list) [index].st.st_mode = 040755;
364 return 1;
367 /* Used to set up a directory list when there is no access to a directory */
369 set_zero_dir (dir_list *list)
371 return (add_dotdot_to_list (list, 0));
374 /* If you change handle_dirent then check also handle_path. */
375 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
376 static int
377 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
378 struct stat *buf1, int next_free, int *link_to_dir,
379 int *stale_link)
381 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
382 return 0;
383 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
384 return 0;
385 if (!show_dot_files && (dp->d_name[0] == '.'))
386 return 0;
387 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
388 return 0;
389 if (mc_lstat (dp->d_name, buf1) == -1) {
391 * lstat() fails - such entries should be identified by
392 * buf1->st_mode being 0.
393 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
395 memset (buf1, 0, sizeof (*buf1));
398 if (S_ISDIR (buf1->st_mode))
399 tree_store_mark_checked (dp->d_name);
401 /* A link to a file or a directory? */
402 *link_to_dir = 0;
403 *stale_link = 0;
404 if (S_ISLNK (buf1->st_mode)) {
405 struct stat buf2;
406 if (!mc_stat (dp->d_name, &buf2))
407 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
408 else
409 *stale_link = 1;
411 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
412 && !regexp_match (filter, dp->d_name, match_file))
413 return 0;
415 /* Need to grow the *list? */
416 if (next_free == list->size) {
417 list->list =
418 g_realloc (list->list,
419 sizeof (file_entry) * (list->size + RESIZE_STEPS));
420 if (!list->list)
421 return -1;
422 list->size += RESIZE_STEPS;
424 return 1;
427 /* handle_path is a simplified handle_dirent. The difference is that
428 handle_path doesn't pay attention to show_dot_files and show_backups.
429 Moreover handle_path can't be used with a filemask.
430 If you change handle_path then check also handle_dirent. */
431 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
433 handle_path (dir_list *list, const char *path,
434 struct stat *buf1, int next_free, int *link_to_dir,
435 int *stale_link)
437 if (path [0] == '.' && path [1] == 0)
438 return 0;
439 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
440 return 0;
441 if (mc_lstat (path, buf1) == -1)
442 return 0;
444 if (S_ISDIR (buf1->st_mode))
445 tree_store_mark_checked (path);
447 /* A link to a file or a directory? */
448 *link_to_dir = 0;
449 *stale_link = 0;
450 if (S_ISLNK(buf1->st_mode)){
451 struct stat buf2;
452 if (!mc_stat (path, &buf2))
453 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
454 else
455 *stale_link = 1;
458 /* Need to grow the *list? */
459 if (next_free == list->size){
460 list->list = g_realloc (list->list, sizeof (file_entry) *
461 (list->size + RESIZE_STEPS));
462 if (!list->list)
463 return -1;
464 list->size += RESIZE_STEPS;
466 return 1;
470 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
471 int case_sensitive, const char *filter)
473 DIR *dirp;
474 struct dirent *dp;
475 int status, link_to_dir, stale_link;
476 int next_free = 0;
477 struct stat st;
479 dirp = mc_opendir (path);
480 if (!dirp) {
481 message (1, MSG_ERROR, _("Cannot read directory contents"));
482 return set_zero_dir (list);
484 tree_store_start_check (path);
485 while ((dp = mc_readdir (dirp))) {
486 status =
487 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
488 &stale_link);
489 if (status == 0)
490 continue;
491 if (status == -1) {
492 tree_store_end_check ();
493 mc_closedir (dirp);
494 return next_free;
496 list->list[next_free].fnamelen = NLENGTH (dp);
497 list->list[next_free].fname = g_strdup (dp->d_name);
498 list->list[next_free].f.marked = 0;
499 list->list[next_free].f.link_to_dir = link_to_dir;
500 list->list[next_free].f.stale_link = stale_link;
501 list->list[next_free].f.dir_size_computed = 0;
502 list->list[next_free].st = st;
503 next_free++;
504 if (!(next_free % 32))
505 rotate_dash ();
508 if (next_free) {
509 /* Add ".." except the root directory */
510 if (strcmp (path, "/") != 0)
511 add_dotdot_to_list (list, next_free++);
512 do_sort (list, sort, next_free - 1, reverse, case_sensitive);
513 } else {
514 next_free = set_zero_dir (list);
517 mc_closedir (dirp);
518 tree_store_end_check ();
519 return next_free;
523 link_isdir (const file_entry *file)
525 if (file->f.link_to_dir)
526 return 1;
527 else
528 return 0;
532 if_link_is_exe (const char *full_name, const file_entry *file)
534 struct stat b;
536 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
537 return is_exe (b.st_mode);
538 } else
539 return 1;
542 static dir_list dir_copy = { 0, 0 };
544 static void
545 alloc_dir_copy (int size)
547 int i;
549 if (dir_copy.size < size){
550 if (dir_copy.list){
552 for (i = 0; i < dir_copy.size; i++) {
553 g_free (dir_copy.list [i].fname);
555 g_free (dir_copy.list);
556 dir_copy.list = 0;
559 dir_copy.list = g_new (file_entry, size);
560 for (i = 0; i < size; i++)
561 dir_copy.list [i].fname = 0;
563 dir_copy.size = size;
567 /* If filter is null, then it is a match */
569 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
570 int rev, int case_sensitive, const char *filter)
572 DIR *dirp;
573 struct dirent *dp;
574 int next_free = 0;
575 int i, status, link_to_dir, stale_link;
576 struct stat st;
577 int marked_cnt;
578 GHashTable *marked_files;
580 dirp = mc_opendir (path);
581 if (!dirp) {
582 message (1, MSG_ERROR, _("Cannot read directory contents"));
583 clean_dir (list, count);
584 return set_zero_dir (list);
587 tree_store_start_check (path);
588 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
589 alloc_dir_copy (list->size);
590 for (marked_cnt = i = 0; i < count; i++) {
591 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
592 dir_copy.list[i].fname = list->list[i].fname;
593 dir_copy.list[i].f.marked = list->list[i].f.marked;
594 dir_copy.list[i].f.dir_size_computed =
595 list->list[i].f.dir_size_computed;
596 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
597 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
598 if (list->list[i].f.marked) {
599 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
600 &dir_copy.list[i]);
601 marked_cnt++;
605 while ((dp = mc_readdir (dirp))) {
606 status =
607 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
608 &stale_link);
609 if (status == 0)
610 continue;
611 if (status == -1) {
612 mc_closedir (dirp);
613 /* Norbert (Feb 12, 1997):
614 Just in case someone finds this memory leak:
615 -1 means big trouble (at the moment no memory left),
616 I don't bother with further cleanup because if one gets to
617 this point he will have more problems than a few memory
618 leaks and because one 'clean_dir' would not be enough (and
619 because I don't want to spent the time to make it working,
620 IMHO it's not worthwhile).
621 clean_dir (&dir_copy, count);
623 tree_store_end_check ();
624 g_hash_table_destroy (marked_files);
625 return next_free;
628 list->list[next_free].f.marked = 0;
631 * If we have marked files in the copy, scan through the copy
632 * to find matching file. Decrease number of remaining marks if
633 * we copied one.
635 if (marked_cnt > 0) {
636 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
637 list->list[next_free].f.marked = 1;
638 marked_cnt--;
642 list->list[next_free].fnamelen = NLENGTH (dp);
643 list->list[next_free].fname = g_strdup (dp->d_name);
644 list->list[next_free].f.link_to_dir = link_to_dir;
645 list->list[next_free].f.stale_link = stale_link;
646 list->list[next_free].f.dir_size_computed = 0;
647 list->list[next_free].st = st;
648 next_free++;
649 if (!(next_free % 16))
650 rotate_dash ();
652 mc_closedir (dirp);
653 tree_store_end_check ();
654 g_hash_table_destroy (marked_files);
655 if (next_free) {
656 /* Add ".." except the root directory */
657 if (strcmp (path, "/") != 0)
658 add_dotdot_to_list (list, next_free++);
659 do_sort (list, sort, next_free - 1, rev, case_sensitive);
660 } else
661 next_free = set_zero_dir (list);
662 clean_dir (&dir_copy, count);
663 return next_free;