Some optimization of loops in translation functions.
[midnight-commander.git] / src / dir.c
bloba55ed57068cf3848fb50b400bc8c6300179bb18a
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 /** \file dir.c
20 * \brief Source: directory routines
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
30 #include "lib/global.h"
31 #include "lib/tty/tty.h"
32 #include "lib/search.h"
33 #include "lib/vfs/mc-vfs/vfs.h"
34 #include "lib/fs.h"
35 #include "lib/strutil.h"
37 #include "wtools.h"
38 #include "treestore.h"
39 #include "dir.h"
40 #include "setup.h" /* panels_options */
41 #include "layout.h" /* rotate_dash() */
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 /* 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 },
66 static inline int
67 key_collate (const char *t1, const char *t2)
69 int dotdot = 0;
70 int ret;
72 dotdot = (t1[0] == '.' ? 1 : 0) | ((t2[0] == '.' ? 1 : 0) << 1);
74 switch (dotdot)
76 case 0:
77 case 3:
78 ret = str_key_collate (t1, t2, case_sensitive) * reverse;
79 break;
80 case 1:
81 ret = -1; /* t1 < t2 */
82 break;
83 case 2:
84 ret = 1; /* t1 > t2 */
85 break;
86 default:
87 ret = 0; /* it must not happen */
90 return ret;
93 int
94 unsorted (file_entry *a, file_entry *b)
96 (void) a;
97 (void) b;
98 return 0;
102 sort_name (file_entry *a, file_entry *b)
104 int ad = MY_ISDIR (a);
105 int bd = MY_ISDIR (b);
107 if (ad == bd || panels_options.mix_all_files) {
108 /* create key if does not exist, key will be freed after sorting */
109 if (a->sort_key == NULL)
110 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
111 if (b->sort_key == NULL)
112 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
114 return key_collate (a->sort_key, b->sort_key);
116 return bd - ad;
120 sort_vers (file_entry *a, file_entry *b)
122 int ad = MY_ISDIR (a);
123 int bd = MY_ISDIR (b);
125 if (ad == bd || panels_options.mix_all_files) {
126 return str_verscmp(a->fname, b->fname) * reverse;
127 } else {
128 return bd - ad;
133 sort_ext (file_entry *a, file_entry *b)
135 int r;
136 int ad = MY_ISDIR (a);
137 int bd = MY_ISDIR (b);
139 if (ad == bd || panels_options.mix_all_files) {
140 if (a->second_sort_key == NULL)
141 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
142 if (b->second_sort_key == NULL)
143 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
145 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
146 if (r)
147 return r * reverse;
148 else
149 return sort_name (a, b);
150 } else
151 return bd-ad;
155 sort_time (file_entry *a, file_entry *b)
157 int ad = MY_ISDIR (a);
158 int bd = MY_ISDIR (b);
160 if (ad == bd || panels_options.mix_all_files) {
161 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
162 a->st.st_mtime > b->st.st_mtime;
163 if (result != 0)
164 return result * reverse;
165 else
166 return sort_name (a, b);
168 else
169 return bd-ad;
173 sort_ctime (file_entry *a, file_entry *b)
175 int ad = MY_ISDIR (a);
176 int bd = MY_ISDIR (b);
178 if (ad == bd || panels_options.mix_all_files) {
179 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
180 a->st.st_ctime > b->st.st_ctime;
181 if (result != 0)
182 return result * reverse;
183 else
184 return sort_name (a, b);
186 else
187 return bd-ad;
191 sort_atime (file_entry *a, file_entry *b)
193 int ad = MY_ISDIR (a);
194 int bd = MY_ISDIR (b);
196 if (ad == bd || panels_options.mix_all_files) {
197 int result = a->st.st_atime < b->st.st_atime ? -1 :
198 a->st.st_atime > b->st.st_atime;
199 if (result != 0)
200 return result * reverse;
201 else
202 return sort_name (a, b);
204 else
205 return bd-ad;
209 sort_inode (file_entry *a, file_entry *b)
211 int ad = MY_ISDIR (a);
212 int bd = MY_ISDIR (b);
214 if (ad == bd || panels_options.mix_all_files)
215 return (a->st.st_ino - b->st.st_ino) * reverse;
216 else
217 return bd-ad;
221 sort_size (file_entry *a, file_entry *b)
223 int ad = MY_ISDIR (a);
224 int bd = MY_ISDIR (b);
225 int result = 0;
227 if (ad != bd && !panels_options.mix_all_files)
228 return bd - ad;
230 result = a->st.st_size < b->st.st_size ? -1 :
231 a->st.st_size > b->st.st_size;
232 if (result != 0)
233 return result * reverse;
234 else
235 return sort_name (a, b);
238 /* clear keys, should be call after sorting is finished */
239 static void
240 clean_sort_keys (dir_list *list, int start, int count)
242 int i;
244 for (i = 0; i < count; i++){
245 str_release_key (list->list [i + start].sort_key, case_sensitive);
246 list->list [i + start].sort_key = NULL;
247 str_release_key (list->list [i + start].second_sort_key, case_sensitive);
248 list->list [i + start].second_sort_key = NULL;
253 void
254 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
256 int dot_dot_found = 0;
258 if (top == 0)
259 return;
261 /* If there is an ".." entry the caller must take care to
262 ensure that it occupies the first list element. */
263 if (!strcmp (list->list [0].fname, ".."))
264 dot_dot_found = 1;
266 reverse = reverse_f ? -1 : 1;
267 case_sensitive = case_sensitive_f;
268 exec_first = exec_first_f;
269 qsort (&(list->list) [dot_dot_found],
270 top + 1 - dot_dot_found, sizeof (file_entry), sort);
272 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
275 void
276 clean_dir (dir_list *list, int count)
278 int i;
280 for (i = 0; i < count; i++){
281 g_free (list->list [i].fname);
282 list->list [i].fname = NULL;
286 /* Used to set up a directory list when there is no access to a directory */
287 gboolean
288 set_zero_dir (dir_list *list)
290 /* Need to grow the *list? */
291 if (list->size == 0) {
292 list->list = g_try_realloc (list->list, sizeof (file_entry) *
293 (list->size + RESIZE_STEPS));
294 if (list->list == NULL)
295 return FALSE;
297 list->size += RESIZE_STEPS;
300 memset (&(list->list) [0], 0, sizeof(file_entry));
301 list->list[0].fnamelen = 2;
302 list->list[0].fname = g_strdup ("..");
303 list->list[0].f.link_to_dir = 0;
304 list->list[0].f.stale_link = 0;
305 list->list[0].f.dir_size_computed = 0;
306 list->list[0].f.marked = 0;
307 list->list[0].st.st_mode = 040755;
308 return TRUE;
311 /* If you change handle_dirent then check also handle_path. */
312 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
313 static int
314 handle_dirent (dir_list *list, const char *fltr, struct dirent *dp,
315 struct stat *buf1, int next_free, int *link_to_dir,
316 int *stale_link)
318 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
319 return 0;
320 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
321 return 0;
322 if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
323 return 0;
324 if (!panels_options.show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
325 return 0;
327 if (mc_lstat (dp->d_name, buf1) == -1) {
329 * lstat() fails - such entries should be identified by
330 * buf1->st_mode being 0.
331 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
333 memset (buf1, 0, sizeof (*buf1));
336 if (S_ISDIR (buf1->st_mode))
337 tree_store_mark_checked (dp->d_name);
339 /* A link to a file or a directory? */
340 *link_to_dir = 0;
341 *stale_link = 0;
342 if (S_ISLNK (buf1->st_mode)) {
343 struct stat buf2;
344 if (!mc_stat (dp->d_name, &buf2))
345 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
346 else
347 *stale_link = 1;
349 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
350 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
351 return 0;
353 /* Need to grow the *list? */
354 if (next_free == list->size) {
355 list->list = g_try_realloc (list->list, sizeof (file_entry) *
356 (list->size + RESIZE_STEPS));
357 if (list->list == NULL)
358 return -1;
359 list->size += RESIZE_STEPS;
361 return 1;
364 /* get info about ".." */
365 static gboolean
366 get_dotdot_dir_stat (const char *path, struct stat *st)
368 gboolean ret = FALSE;
370 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
371 char *dotdot_dir;
372 struct stat s;
374 dotdot_dir = g_strdup_printf ("%s/../", path);
375 canonicalize_pathname (dotdot_dir);
376 ret = mc_stat (dotdot_dir, &s) == 0;
377 g_free (dotdot_dir);
378 *st = s;
381 return ret;
384 /* handle_path is a simplified handle_dirent. The difference is that
385 handle_path doesn't pay attention to panels_options.show_dot_files
386 and panels_options.show_backups.
387 Moreover handle_path can't be used with a filemask.
388 If you change handle_path then check also handle_dirent. */
389 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
391 handle_path (dir_list *list, const char *path,
392 struct stat *buf1, int next_free, int *link_to_dir,
393 int *stale_link)
395 if (path [0] == '.' && path [1] == 0)
396 return 0;
397 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
398 return 0;
399 if (mc_lstat (path, buf1) == -1)
400 return 0;
402 if (S_ISDIR (buf1->st_mode))
403 tree_store_mark_checked (path);
405 /* A link to a file or a directory? */
406 *link_to_dir = 0;
407 *stale_link = 0;
408 if (S_ISLNK(buf1->st_mode)){
409 struct stat buf2;
410 if (!mc_stat (path, &buf2))
411 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
412 else
413 *stale_link = 1;
416 /* Need to grow the *list? */
417 if (next_free == list->size){
418 list->list = g_try_realloc (list->list, sizeof (file_entry) *
419 (list->size + RESIZE_STEPS));
420 if (list->list == NULL)
421 return -1;
422 list->size += RESIZE_STEPS;
424 return 1;
428 do_load_dir (const char *path, dir_list *list, sortfn *sort, int lc_reverse,
429 int lc_case_sensitive, int exec_ff, const char *fltr)
431 DIR *dirp;
432 struct dirent *dp;
433 int status, link_to_dir, stale_link;
434 int next_free = 0;
435 struct stat st;
437 /* ".." (if any) must be the first entry in the list */
438 if (!set_zero_dir (list))
439 return next_free;
441 if (get_dotdot_dir_stat (path, &st))
442 list->list[next_free].st = st;
443 next_free++;
445 dirp = mc_opendir (path);
446 if (!dirp) {
447 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
448 return next_free;
451 tree_store_start_check (path);
453 /* Do not add a ".." entry to the root directory */
454 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
455 next_free--;
457 while ((dp = mc_readdir (dirp))) {
458 status =
459 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
460 &stale_link);
461 if (status == 0)
462 continue;
463 if (status == -1) {
464 tree_store_end_check ();
465 mc_closedir (dirp);
466 return next_free;
468 list->list[next_free].fnamelen = NLENGTH (dp);
469 list->list[next_free].fname = g_strdup (dp->d_name);
470 list->list[next_free].f.marked = 0;
471 list->list[next_free].f.link_to_dir = link_to_dir;
472 list->list[next_free].f.stale_link = stale_link;
473 list->list[next_free].f.dir_size_computed = 0;
474 list->list[next_free].st = st;
475 list->list[next_free].sort_key = NULL;
476 list->list[next_free].second_sort_key = NULL;
477 next_free++;
479 if ((next_free & 31) == 0)
480 rotate_dash ();
483 if (next_free != 0)
484 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
486 mc_closedir (dirp);
487 tree_store_end_check ();
488 return next_free;
492 link_isdir (const file_entry *file)
494 if (file->f.link_to_dir)
495 return 1;
496 else
497 return 0;
501 if_link_is_exe (const char *full_name, const file_entry *file)
503 struct stat b;
505 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
506 return is_exe (b.st_mode);
507 } else
508 return 1;
511 static dir_list dir_copy = { 0, 0 };
513 static void
514 alloc_dir_copy (int size)
516 if (dir_copy.size < size) {
517 if (dir_copy.list) {
518 int i;
519 for (i = 0; i < dir_copy.size; i++)
520 g_free (dir_copy.list [i].fname);
521 g_free (dir_copy.list);
524 dir_copy.list = g_new0 (file_entry, size);
525 dir_copy.size = size;
529 /* If fltr is null, then it is a match */
531 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
532 int rev, int lc_case_sensitive, int exec_ff, const char *fltr)
534 DIR *dirp;
535 struct dirent *dp;
536 int next_free = 0;
537 int i, status, link_to_dir, stale_link;
538 struct stat st;
539 int marked_cnt;
540 GHashTable *marked_files;
542 dirp = mc_opendir (path);
543 if (!dirp) {
544 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
545 clean_dir (list, count);
546 return set_zero_dir (list) ? 1 : 0;
549 tree_store_start_check (path);
550 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
551 alloc_dir_copy (list->size);
552 for (marked_cnt = i = 0; i < count; i++) {
553 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
554 dir_copy.list[i].fname = list->list[i].fname;
555 dir_copy.list[i].f.marked = list->list[i].f.marked;
556 dir_copy.list[i].f.dir_size_computed =
557 list->list[i].f.dir_size_computed;
558 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
559 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
560 dir_copy.list[i].sort_key = NULL;
561 dir_copy.list[i].second_sort_key = NULL;
562 if (list->list[i].f.marked) {
563 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
564 &dir_copy.list[i]);
565 marked_cnt++;
569 /* Add ".." except to the root directory. The ".." entry
570 (if any) must be the first in the list. */
571 if (!((path[0] == PATH_SEP) && (path[1] == '\0'))) {
572 if (!set_zero_dir (list)) {
573 clean_dir (list, count);
574 clean_dir (&dir_copy, count);
575 return next_free;
578 if (get_dotdot_dir_stat (path, &st))
579 list->list[next_free].st = st;
581 next_free++;
584 while ((dp = mc_readdir (dirp))) {
585 status =
586 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
587 &stale_link);
588 if (status == 0)
589 continue;
590 if (status == -1) {
591 mc_closedir (dirp);
592 /* Norbert (Feb 12, 1997):
593 Just in case someone finds this memory leak:
594 -1 means big trouble (at the moment no memory left),
595 I don't bother with further cleanup because if one gets to
596 this point he will have more problems than a few memory
597 leaks and because one 'clean_dir' would not be enough (and
598 because I don't want to spent the time to make it working,
599 IMHO it's not worthwhile).
600 clean_dir (&dir_copy, count);
602 tree_store_end_check ();
603 g_hash_table_destroy (marked_files);
604 return next_free;
607 list->list[next_free].f.marked = 0;
610 * If we have marked files in the copy, scan through the copy
611 * to find matching file. Decrease number of remaining marks if
612 * we copied one.
614 if (marked_cnt > 0) {
615 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
616 list->list[next_free].f.marked = 1;
617 marked_cnt--;
621 list->list[next_free].fnamelen = NLENGTH (dp);
622 list->list[next_free].fname = g_strdup (dp->d_name);
623 list->list[next_free].f.link_to_dir = link_to_dir;
624 list->list[next_free].f.stale_link = stale_link;
625 list->list[next_free].f.dir_size_computed = 0;
626 list->list[next_free].st = st;
627 list->list[next_free].sort_key = NULL;
628 list->list[next_free].second_sort_key = NULL;
629 next_free++;
630 if (!(next_free % 16))
631 rotate_dash ();
633 mc_closedir (dirp);
634 tree_store_end_check ();
635 g_hash_table_destroy (marked_files);
636 if (next_free) {
637 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
639 clean_dir (&dir_copy, count);
640 return next_free;