Merge branch '1858_segfault_in_search'
[midnight-commander.git] / src / dir.c
blob74af7642b66c3fcf79519658cacf2a2b0c0e4dcd
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 "../src/tty/tty.h"
32 #include "../src/search/search.h"
34 #include "global.h"
35 #include "wtools.h"
36 #include "treestore.h"
37 #include "strutil.h"
38 #include "fs.h"
39 #include "util.h" /* canonicalize_pathname () */
40 #include "dir.h"
42 /* If true show files starting with a dot */
43 int show_dot_files = 1;
45 /* If true show files ending in ~ */
46 int show_backups = 1;
48 /* If false then directories are shown separately from files */
49 int mix_all_files = 0;
52 * If true, SI units (1000 based) will be used for
53 * larger units (kilobyte, megabyte, ...).
54 * If false binary units (1024 based) will be used.
56 int kilobyte_si = 0;
58 /* Reverse flag */
59 static int reverse = 1;
61 /* Are the files sorted case sensitively? */
62 static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
64 /* Are the exec_bit files top in list*/
65 static int exec_first = 1;
67 #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) )
69 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
70 { N_("&Unsorted"), unsorted },
71 { N_("&Name"), sort_name },
72 { N_("&Extension"), sort_ext },
73 { N_("&Modify time"), sort_time },
74 { N_("&Access time"), sort_atime },
75 { N_("C&Hange time"), sort_ctime },
76 { N_("&Size"), sort_size },
77 { N_("&Inode"), sort_inode },
81 int
82 unsorted (file_entry *a, file_entry *b)
84 (void) a;
85 (void) b;
86 return 0;
89 int
90 sort_name (file_entry *a, file_entry *b)
92 int ad = MY_ISDIR (a);
93 int bd = MY_ISDIR (b);
95 if (ad == bd || mix_all_files) {
96 /* create key if does not exist, key will be freed after sorting */
97 if (a->sort_key == NULL)
98 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
99 if (b->sort_key == NULL)
100 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
102 return str_key_collate (a->sort_key, b->sort_key, case_sensitive)
103 * reverse;
105 return bd - ad;
109 sort_ext (file_entry *a, file_entry *b)
111 int r;
112 int ad = MY_ISDIR (a);
113 int bd = MY_ISDIR (b);
115 if (ad == bd || mix_all_files){
116 if (a->second_sort_key == NULL)
117 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
118 if (b->second_sort_key == NULL)
119 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
121 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
122 if (r)
123 return r * reverse;
124 else
125 return sort_name (a, b);
126 } else
127 return bd-ad;
131 sort_time (file_entry *a, file_entry *b)
133 int ad = MY_ISDIR (a);
134 int bd = MY_ISDIR (b);
136 if (ad == bd || mix_all_files) {
137 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
138 a->st.st_mtime > b->st.st_mtime;
139 if (result != 0)
140 return result * reverse;
141 else
142 return sort_name (a, b);
144 else
145 return bd-ad;
149 sort_ctime (file_entry *a, file_entry *b)
151 int ad = MY_ISDIR (a);
152 int bd = MY_ISDIR (b);
154 if (ad == bd || mix_all_files) {
155 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
156 a->st.st_ctime > b->st.st_ctime;
157 if (result != 0)
158 return result * reverse;
159 else
160 return sort_name (a, b);
162 else
163 return bd-ad;
167 sort_atime (file_entry *a, file_entry *b)
169 int ad = MY_ISDIR (a);
170 int bd = MY_ISDIR (b);
172 if (ad == bd || mix_all_files) {
173 int result = a->st.st_atime < b->st.st_atime ? -1 :
174 a->st.st_atime > b->st.st_atime;
175 if (result != 0)
176 return result * reverse;
177 else
178 return sort_name (a, b);
180 else
181 return bd-ad;
185 sort_inode (file_entry *a, 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_ino - b->st.st_ino) * reverse;
192 else
193 return bd-ad;
197 sort_size (file_entry *a, file_entry *b)
199 int ad = MY_ISDIR (a);
200 int bd = MY_ISDIR (b);
201 int result = 0;
203 if (ad != bd && !mix_all_files)
204 return bd - ad;
206 result = a->st.st_size < b->st.st_size ? -1 :
207 a->st.st_size > b->st.st_size;
208 if (result != 0)
209 return result * reverse;
210 else
211 return sort_name (a, b);
214 /* clear keys, should be call after sorting is finished */
215 static void
216 clean_sort_keys (dir_list *list, int start, int count)
218 int i;
220 for (i = 0; i < count; i++){
221 str_release_key (list->list [i + start].sort_key, case_sensitive);
222 list->list [i + start].sort_key = NULL;
223 str_release_key (list->list [i + start].second_sort_key, case_sensitive);
224 list->list [i + start].second_sort_key = NULL;
229 void
230 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
232 int dot_dot_found = 0;
234 if (top == 0)
235 return;
237 /* If there is an ".." entry the caller must take care to
238 ensure that it occupies the first list element. */
239 if (!strcmp (list->list [0].fname, ".."))
240 dot_dot_found = 1;
242 reverse = reverse_f ? -1 : 1;
243 case_sensitive = case_sensitive_f;
244 exec_first = exec_first_f;
245 qsort (&(list->list) [dot_dot_found],
246 top + 1 - dot_dot_found, sizeof (file_entry), sort);
248 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
251 void
252 clean_dir (dir_list *list, int count)
254 int i;
256 for (i = 0; i < count; i++){
257 g_free (list->list [i].fname);
258 list->list [i].fname = NULL;
262 /* Used to set up a directory list when there is no access to a directory */
263 gboolean
264 set_zero_dir (dir_list *list)
266 /* Need to grow the *list? */
267 if (list->size == 0) {
268 list->list = g_realloc (list->list, sizeof (file_entry) *
269 (list->size + RESIZE_STEPS));
270 if (list->list == NULL)
271 return FALSE;
273 list->size += RESIZE_STEPS;
276 memset (&(list->list) [0], 0, sizeof(file_entry));
277 list->list[0].fnamelen = 2;
278 list->list[0].fname = g_strdup ("..");
279 list->list[0].f.link_to_dir = 0;
280 list->list[0].f.stale_link = 0;
281 list->list[0].f.dir_size_computed = 0;
282 list->list[0].f.marked = 0;
283 list->list[0].st.st_mode = 040755;
284 return TRUE;
287 /* If you change handle_dirent then check also handle_path. */
288 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
289 static int
290 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
291 struct stat *buf1, int next_free, int *link_to_dir,
292 int *stale_link)
294 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
295 return 0;
296 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
297 return 0;
298 if (!show_dot_files && (dp->d_name[0] == '.'))
299 return 0;
300 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
301 return 0;
302 if (mc_lstat (dp->d_name, buf1) == -1) {
304 * lstat() fails - such entries should be identified by
305 * buf1->st_mode being 0.
306 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
308 memset (buf1, 0, sizeof (*buf1));
311 if (S_ISDIR (buf1->st_mode))
312 tree_store_mark_checked (dp->d_name);
314 /* A link to a file or a directory? */
315 *link_to_dir = 0;
316 *stale_link = 0;
317 if (S_ISLNK (buf1->st_mode)) {
318 struct stat buf2;
319 if (!mc_stat (dp->d_name, &buf2))
320 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
321 else
322 *stale_link = 1;
324 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
325 && !mc_search(filter, dp->d_name, MC_SEARCH_T_GLOB) )
326 return 0;
328 /* Need to grow the *list? */
329 if (next_free == list->size) {
330 list->list =
331 g_realloc (list->list,
332 sizeof (file_entry) * (list->size + RESIZE_STEPS));
333 if (!list->list)
334 return -1;
335 list->size += RESIZE_STEPS;
337 return 1;
340 /* get info about ".." */
341 static gboolean
342 get_dotdot_dir_stat (const char *path, struct stat *st)
344 gboolean ret = FALSE;
346 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
347 char *dotdot_dir;
348 struct stat s;
350 dotdot_dir = g_strdup_printf ("%s/../", path);
351 canonicalize_pathname (dotdot_dir);
352 ret = mc_stat (dotdot_dir, &s) == 0;
353 g_free (dotdot_dir);
354 *st = s;
357 return ret;
360 /* handle_path is a simplified handle_dirent. The difference is that
361 handle_path doesn't pay attention to show_dot_files and show_backups.
362 Moreover handle_path can't be used with a filemask.
363 If you change handle_path then check also handle_dirent. */
364 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
366 handle_path (dir_list *list, const char *path,
367 struct stat *buf1, int next_free, int *link_to_dir,
368 int *stale_link)
370 if (path [0] == '.' && path [1] == 0)
371 return 0;
372 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
373 return 0;
374 if (mc_lstat (path, buf1) == -1)
375 return 0;
377 if (S_ISDIR (buf1->st_mode))
378 tree_store_mark_checked (path);
380 /* A link to a file or a directory? */
381 *link_to_dir = 0;
382 *stale_link = 0;
383 if (S_ISLNK(buf1->st_mode)){
384 struct stat buf2;
385 if (!mc_stat (path, &buf2))
386 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
387 else
388 *stale_link = 1;
391 /* Need to grow the *list? */
392 if (next_free == list->size){
393 list->list = g_realloc (list->list, sizeof (file_entry) *
394 (list->size + RESIZE_STEPS));
395 if (!list->list)
396 return -1;
397 list->size += RESIZE_STEPS;
399 return 1;
403 do_load_dir (const char *path, dir_list *list, sortfn *sort, int lc_reverse,
404 int lc_case_sensitive, int exec_ff, const char *filter)
406 DIR *dirp;
407 struct dirent *dp;
408 int status, link_to_dir, stale_link;
409 int next_free = 0;
410 struct stat st;
412 /* ".." (if any) must be the first entry in the list */
413 if (!set_zero_dir (list))
414 return next_free;
416 if (get_dotdot_dir_stat (path, &st))
417 list->list[next_free].st = st;
418 next_free++;
420 dirp = mc_opendir (path);
421 if (!dirp) {
422 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
423 return next_free;
426 tree_store_start_check (path);
428 /* Do not add a ".." entry to the root directory */
429 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
430 next_free--;
432 while ((dp = mc_readdir (dirp))) {
433 status =
434 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
435 &stale_link);
436 if (status == 0)
437 continue;
438 if (status == -1) {
439 tree_store_end_check ();
440 mc_closedir (dirp);
441 return next_free;
443 list->list[next_free].fnamelen = NLENGTH (dp);
444 list->list[next_free].fname = g_strdup (dp->d_name);
445 list->list[next_free].f.marked = 0;
446 list->list[next_free].f.link_to_dir = link_to_dir;
447 list->list[next_free].f.stale_link = stale_link;
448 list->list[next_free].f.dir_size_computed = 0;
449 list->list[next_free].st = st;
450 list->list[next_free].sort_key = NULL;
451 list->list[next_free].second_sort_key = NULL;
452 next_free++;
454 if ((next_free & 31) == 0)
455 rotate_dash ();
458 if (next_free != 0)
459 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
461 mc_closedir (dirp);
462 tree_store_end_check ();
463 return next_free;
467 link_isdir (const file_entry *file)
469 if (file->f.link_to_dir)
470 return 1;
471 else
472 return 0;
476 if_link_is_exe (const char *full_name, const file_entry *file)
478 struct stat b;
480 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
481 return is_exe (b.st_mode);
482 } else
483 return 1;
486 static dir_list dir_copy = { 0, 0 };
488 static void
489 alloc_dir_copy (int size)
491 int i;
493 if (dir_copy.size < size){
494 if (dir_copy.list){
496 for (i = 0; i < dir_copy.size; i++) {
497 g_free (dir_copy.list [i].fname);
499 g_free (dir_copy.list);
500 dir_copy.list = 0;
503 dir_copy.list = g_new (file_entry, size);
504 for (i = 0; i < size; i++) {
505 dir_copy.list [i].fname = NULL;
506 dir_copy.list [i].sort_key = NULL;
507 dir_copy.list [i].second_sort_key = NULL;
510 dir_copy.size = size;
514 /* If filter is null, then it is a match */
516 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
517 int rev, int lc_case_sensitive, int exec_ff, const char *filter)
519 DIR *dirp;
520 struct dirent *dp;
521 int next_free = 0;
522 int i, status, link_to_dir, stale_link;
523 struct stat st;
524 int marked_cnt;
525 GHashTable *marked_files;
527 dirp = mc_opendir (path);
528 if (!dirp) {
529 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
530 clean_dir (list, count);
531 return set_zero_dir (list) ? 1 : 0;
534 tree_store_start_check (path);
535 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
536 alloc_dir_copy (list->size);
537 for (marked_cnt = i = 0; i < count; i++) {
538 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
539 dir_copy.list[i].fname = list->list[i].fname;
540 dir_copy.list[i].f.marked = list->list[i].f.marked;
541 dir_copy.list[i].f.dir_size_computed =
542 list->list[i].f.dir_size_computed;
543 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
544 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
545 dir_copy.list[i].sort_key = NULL;
546 dir_copy.list[i].second_sort_key = NULL;
547 if (list->list[i].f.marked) {
548 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
549 &dir_copy.list[i]);
550 marked_cnt++;
554 /* Add ".." except to the root directory. The ".." entry
555 (if any) must be the first in the list. */
556 if (!((path[0] == PATH_SEP) && (path[1] == '\0'))) {
557 if (!set_zero_dir (list)) {
558 clean_dir (list, count);
559 clean_dir (&dir_copy, count);
560 return next_free;
563 if (get_dotdot_dir_stat (path, &st))
564 list->list[next_free].st = st;
566 next_free++;
569 while ((dp = mc_readdir (dirp))) {
570 status =
571 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
572 &stale_link);
573 if (status == 0)
574 continue;
575 if (status == -1) {
576 mc_closedir (dirp);
577 /* Norbert (Feb 12, 1997):
578 Just in case someone finds this memory leak:
579 -1 means big trouble (at the moment no memory left),
580 I don't bother with further cleanup because if one gets to
581 this point he will have more problems than a few memory
582 leaks and because one 'clean_dir' would not be enough (and
583 because I don't want to spent the time to make it working,
584 IMHO it's not worthwhile).
585 clean_dir (&dir_copy, count);
587 tree_store_end_check ();
588 g_hash_table_destroy (marked_files);
589 return next_free;
592 list->list[next_free].f.marked = 0;
595 * If we have marked files in the copy, scan through the copy
596 * to find matching file. Decrease number of remaining marks if
597 * we copied one.
599 if (marked_cnt > 0) {
600 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
601 list->list[next_free].f.marked = 1;
602 marked_cnt--;
606 list->list[next_free].fnamelen = NLENGTH (dp);
607 list->list[next_free].fname = g_strdup (dp->d_name);
608 list->list[next_free].f.link_to_dir = link_to_dir;
609 list->list[next_free].f.stale_link = stale_link;
610 list->list[next_free].f.dir_size_computed = 0;
611 list->list[next_free].st = st;
612 list->list[next_free].sort_key = NULL;
613 list->list[next_free].second_sort_key = NULL;
614 next_free++;
615 if (!(next_free % 16))
616 rotate_dash ();
618 mc_closedir (dirp);
619 tree_store_end_check ();
620 g_hash_table_destroy (marked_files);
621 if (next_free) {
622 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
624 clean_dir (&dir_copy, count);
625 return next_free;