Support of multiple editors and viewers.
[midnight-commander.git] / src / dir.c
blob62f40a24fc01ae051b6a9369aa0b89bdc6381a19
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 */
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 /* Are the exec_bit files top in list*/
49 static int exec_first = 1;
51 #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) )
53 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
54 { N_("&Unsorted"), unsorted },
55 { N_("&Name"), sort_name },
56 { N_("&Extension"), sort_ext },
57 { N_("&Modify time"), sort_time },
58 { N_("&Access time"), sort_atime },
59 { N_("C&Hange time"), sort_ctime },
60 { N_("&Size"), sort_size },
61 { N_("&Inode"), sort_inode },
65 int
66 unsorted (file_entry *a, file_entry *b)
68 (void) a;
69 (void) b;
70 return 0;
73 int
74 sort_name (file_entry *a, file_entry *b)
76 int ad = MY_ISDIR (a);
77 int bd = MY_ISDIR (b);
79 if (ad == bd || panels_options.mix_all_files) {
80 /* create key if does not exist, key will be freed after sorting */
81 if (a->sort_key == NULL)
82 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
83 if (b->sort_key == NULL)
84 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
86 return str_key_collate (a->sort_key, b->sort_key, case_sensitive)
87 * reverse;
89 return bd - ad;
92 int
93 sort_vers (file_entry *a, file_entry *b)
95 int ad = MY_ISDIR (a);
96 int bd = MY_ISDIR (b);
98 if (ad == bd || panels_options.mix_all_files) {
99 return str_verscmp(a->fname, b->fname) * reverse;
100 } else {
101 return bd - ad;
106 sort_ext (file_entry *a, file_entry *b)
108 int r;
109 int ad = MY_ISDIR (a);
110 int bd = MY_ISDIR (b);
112 if (ad == bd || panels_options.mix_all_files) {
113 if (a->second_sort_key == NULL)
114 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
115 if (b->second_sort_key == NULL)
116 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
118 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
119 if (r)
120 return r * reverse;
121 else
122 return sort_name (a, b);
123 } else
124 return bd-ad;
128 sort_time (file_entry *a, file_entry *b)
130 int ad = MY_ISDIR (a);
131 int bd = MY_ISDIR (b);
133 if (ad == bd || panels_options.mix_all_files) {
134 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
135 a->st.st_mtime > b->st.st_mtime;
136 if (result != 0)
137 return result * reverse;
138 else
139 return sort_name (a, b);
141 else
142 return bd-ad;
146 sort_ctime (file_entry *a, file_entry *b)
148 int ad = MY_ISDIR (a);
149 int bd = MY_ISDIR (b);
151 if (ad == bd || panels_options.mix_all_files) {
152 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
153 a->st.st_ctime > b->st.st_ctime;
154 if (result != 0)
155 return result * reverse;
156 else
157 return sort_name (a, b);
159 else
160 return bd-ad;
164 sort_atime (file_entry *a, file_entry *b)
166 int ad = MY_ISDIR (a);
167 int bd = MY_ISDIR (b);
169 if (ad == bd || panels_options.mix_all_files) {
170 int result = a->st.st_atime < b->st.st_atime ? -1 :
171 a->st.st_atime > b->st.st_atime;
172 if (result != 0)
173 return result * reverse;
174 else
175 return sort_name (a, b);
177 else
178 return bd-ad;
182 sort_inode (file_entry *a, file_entry *b)
184 int ad = MY_ISDIR (a);
185 int bd = MY_ISDIR (b);
187 if (ad == bd || panels_options.mix_all_files)
188 return (a->st.st_ino - b->st.st_ino) * reverse;
189 else
190 return bd-ad;
194 sort_size (file_entry *a, file_entry *b)
196 int ad = MY_ISDIR (a);
197 int bd = MY_ISDIR (b);
198 int result = 0;
200 if (ad != bd && !panels_options.mix_all_files)
201 return bd - ad;
203 result = a->st.st_size < b->st.st_size ? -1 :
204 a->st.st_size > b->st.st_size;
205 if (result != 0)
206 return result * reverse;
207 else
208 return sort_name (a, b);
211 /* clear keys, should be call after sorting is finished */
212 static void
213 clean_sort_keys (dir_list *list, int start, int count)
215 int i;
217 for (i = 0; i < count; i++){
218 str_release_key (list->list [i + start].sort_key, case_sensitive);
219 list->list [i + start].sort_key = NULL;
220 str_release_key (list->list [i + start].second_sort_key, case_sensitive);
221 list->list [i + start].second_sort_key = NULL;
226 void
227 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
229 int dot_dot_found = 0;
231 if (top == 0)
232 return;
234 /* If there is an ".." entry the caller must take care to
235 ensure that it occupies the first list element. */
236 if (!strcmp (list->list [0].fname, ".."))
237 dot_dot_found = 1;
239 reverse = reverse_f ? -1 : 1;
240 case_sensitive = case_sensitive_f;
241 exec_first = exec_first_f;
242 qsort (&(list->list) [dot_dot_found],
243 top + 1 - dot_dot_found, sizeof (file_entry), sort);
245 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
248 void
249 clean_dir (dir_list *list, int count)
251 int i;
253 for (i = 0; i < count; i++){
254 g_free (list->list [i].fname);
255 list->list [i].fname = NULL;
259 /* Used to set up a directory list when there is no access to a directory */
260 gboolean
261 set_zero_dir (dir_list *list)
263 /* Need to grow the *list? */
264 if (list->size == 0) {
265 list->list = g_try_realloc (list->list, sizeof (file_entry) *
266 (list->size + RESIZE_STEPS));
267 if (list->list == NULL)
268 return FALSE;
270 list->size += RESIZE_STEPS;
273 memset (&(list->list) [0], 0, sizeof(file_entry));
274 list->list[0].fnamelen = 2;
275 list->list[0].fname = g_strdup ("..");
276 list->list[0].f.link_to_dir = 0;
277 list->list[0].f.stale_link = 0;
278 list->list[0].f.dir_size_computed = 0;
279 list->list[0].f.marked = 0;
280 list->list[0].st.st_mode = 040755;
281 return TRUE;
284 /* If you change handle_dirent then check also handle_path. */
285 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
286 static int
287 handle_dirent (dir_list *list, const char *fltr, struct dirent *dp,
288 struct stat *buf1, int next_free, int *link_to_dir,
289 int *stale_link)
291 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
292 return 0;
293 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
294 return 0;
295 if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
296 return 0;
297 if (!panels_options.show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
298 return 0;
300 if (mc_lstat (dp->d_name, buf1) == -1) {
302 * lstat() fails - such entries should be identified by
303 * buf1->st_mode being 0.
304 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
306 memset (buf1, 0, sizeof (*buf1));
309 if (S_ISDIR (buf1->st_mode))
310 tree_store_mark_checked (dp->d_name);
312 /* A link to a file or a directory? */
313 *link_to_dir = 0;
314 *stale_link = 0;
315 if (S_ISLNK (buf1->st_mode)) {
316 struct stat buf2;
317 if (!mc_stat (dp->d_name, &buf2))
318 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
319 else
320 *stale_link = 1;
322 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
323 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
324 return 0;
326 /* Need to grow the *list? */
327 if (next_free == list->size) {
328 list->list = g_try_realloc (list->list, sizeof (file_entry) *
329 (list->size + RESIZE_STEPS));
330 if (list->list == NULL)
331 return -1;
332 list->size += RESIZE_STEPS;
334 return 1;
337 /* get info about ".." */
338 static gboolean
339 get_dotdot_dir_stat (const char *path, struct stat *st)
341 gboolean ret = FALSE;
343 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
344 char *dotdot_dir;
345 struct stat s;
347 dotdot_dir = g_strdup_printf ("%s/../", path);
348 canonicalize_pathname (dotdot_dir);
349 ret = mc_stat (dotdot_dir, &s) == 0;
350 g_free (dotdot_dir);
351 *st = s;
354 return ret;
357 /* handle_path is a simplified handle_dirent. The difference is that
358 handle_path doesn't pay attention to panels_options.show_dot_files
359 and panels_options.show_backups.
360 Moreover handle_path can't be used with a filemask.
361 If you change handle_path then check also handle_dirent. */
362 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
364 handle_path (dir_list *list, const char *path,
365 struct stat *buf1, int next_free, int *link_to_dir,
366 int *stale_link)
368 if (path [0] == '.' && path [1] == 0)
369 return 0;
370 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
371 return 0;
372 if (mc_lstat (path, buf1) == -1)
373 return 0;
375 if (S_ISDIR (buf1->st_mode))
376 tree_store_mark_checked (path);
378 /* A link to a file or a directory? */
379 *link_to_dir = 0;
380 *stale_link = 0;
381 if (S_ISLNK(buf1->st_mode)){
382 struct stat buf2;
383 if (!mc_stat (path, &buf2))
384 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
385 else
386 *stale_link = 1;
389 /* Need to grow the *list? */
390 if (next_free == list->size){
391 list->list = g_try_realloc (list->list, sizeof (file_entry) *
392 (list->size + RESIZE_STEPS));
393 if (list->list == NULL)
394 return -1;
395 list->size += RESIZE_STEPS;
397 return 1;
401 do_load_dir (const char *path, dir_list *list, sortfn *sort, int lc_reverse,
402 int lc_case_sensitive, int exec_ff, const char *fltr)
404 DIR *dirp;
405 struct dirent *dp;
406 int status, link_to_dir, stale_link;
407 int next_free = 0;
408 struct stat st;
410 /* ".." (if any) must be the first entry in the list */
411 if (!set_zero_dir (list))
412 return next_free;
414 if (get_dotdot_dir_stat (path, &st))
415 list->list[next_free].st = st;
416 next_free++;
418 dirp = mc_opendir (path);
419 if (!dirp) {
420 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
421 return next_free;
424 tree_store_start_check (path);
426 /* Do not add a ".." entry to the root directory */
427 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
428 next_free--;
430 while ((dp = mc_readdir (dirp))) {
431 status =
432 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
433 &stale_link);
434 if (status == 0)
435 continue;
436 if (status == -1) {
437 tree_store_end_check ();
438 mc_closedir (dirp);
439 return next_free;
441 list->list[next_free].fnamelen = NLENGTH (dp);
442 list->list[next_free].fname = g_strdup (dp->d_name);
443 list->list[next_free].f.marked = 0;
444 list->list[next_free].f.link_to_dir = link_to_dir;
445 list->list[next_free].f.stale_link = stale_link;
446 list->list[next_free].f.dir_size_computed = 0;
447 list->list[next_free].st = st;
448 list->list[next_free].sort_key = NULL;
449 list->list[next_free].second_sort_key = NULL;
450 next_free++;
452 if ((next_free & 31) == 0)
453 rotate_dash ();
456 if (next_free != 0)
457 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
459 mc_closedir (dirp);
460 tree_store_end_check ();
461 return next_free;
465 link_isdir (const file_entry *file)
467 if (file->f.link_to_dir)
468 return 1;
469 else
470 return 0;
474 if_link_is_exe (const char *full_name, const file_entry *file)
476 struct stat b;
478 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
479 return is_exe (b.st_mode);
480 } else
481 return 1;
484 static dir_list dir_copy = { 0, 0 };
486 static void
487 alloc_dir_copy (int size)
489 if (dir_copy.size < size) {
490 if (dir_copy.list) {
491 int i;
492 for (i = 0; i < dir_copy.size; i++)
493 g_free (dir_copy.list [i].fname);
494 g_free (dir_copy.list);
497 dir_copy.list = g_new0 (file_entry, size);
498 dir_copy.size = size;
502 /* If fltr is null, then it is a match */
504 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
505 int rev, int lc_case_sensitive, int exec_ff, const char *fltr)
507 DIR *dirp;
508 struct dirent *dp;
509 int next_free = 0;
510 int i, status, link_to_dir, stale_link;
511 struct stat st;
512 int marked_cnt;
513 GHashTable *marked_files;
515 dirp = mc_opendir (path);
516 if (!dirp) {
517 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
518 clean_dir (list, count);
519 return set_zero_dir (list) ? 1 : 0;
522 tree_store_start_check (path);
523 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
524 alloc_dir_copy (list->size);
525 for (marked_cnt = i = 0; i < count; i++) {
526 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
527 dir_copy.list[i].fname = list->list[i].fname;
528 dir_copy.list[i].f.marked = list->list[i].f.marked;
529 dir_copy.list[i].f.dir_size_computed =
530 list->list[i].f.dir_size_computed;
531 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
532 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
533 dir_copy.list[i].sort_key = NULL;
534 dir_copy.list[i].second_sort_key = NULL;
535 if (list->list[i].f.marked) {
536 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
537 &dir_copy.list[i]);
538 marked_cnt++;
542 /* Add ".." except to the root directory. The ".." entry
543 (if any) must be the first in the list. */
544 if (!((path[0] == PATH_SEP) && (path[1] == '\0'))) {
545 if (!set_zero_dir (list)) {
546 clean_dir (list, count);
547 clean_dir (&dir_copy, count);
548 return next_free;
551 if (get_dotdot_dir_stat (path, &st))
552 list->list[next_free].st = st;
554 next_free++;
557 while ((dp = mc_readdir (dirp))) {
558 status =
559 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
560 &stale_link);
561 if (status == 0)
562 continue;
563 if (status == -1) {
564 mc_closedir (dirp);
565 /* Norbert (Feb 12, 1997):
566 Just in case someone finds this memory leak:
567 -1 means big trouble (at the moment no memory left),
568 I don't bother with further cleanup because if one gets to
569 this point he will have more problems than a few memory
570 leaks and because one 'clean_dir' would not be enough (and
571 because I don't want to spent the time to make it working,
572 IMHO it's not worthwhile).
573 clean_dir (&dir_copy, count);
575 tree_store_end_check ();
576 g_hash_table_destroy (marked_files);
577 return next_free;
580 list->list[next_free].f.marked = 0;
583 * If we have marked files in the copy, scan through the copy
584 * to find matching file. Decrease number of remaining marks if
585 * we copied one.
587 if (marked_cnt > 0) {
588 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
589 list->list[next_free].f.marked = 1;
590 marked_cnt--;
594 list->list[next_free].fnamelen = NLENGTH (dp);
595 list->list[next_free].fname = g_strdup (dp->d_name);
596 list->list[next_free].f.link_to_dir = link_to_dir;
597 list->list[next_free].f.stale_link = stale_link;
598 list->list[next_free].f.dir_size_computed = 0;
599 list->list[next_free].st = st;
600 list->list[next_free].sort_key = NULL;
601 list->list[next_free].second_sort_key = NULL;
602 next_free++;
603 if (!(next_free % 16))
604 rotate_dash ();
606 mc_closedir (dirp);
607 tree_store_end_check ();
608 g_hash_table_destroy (marked_files);
609 if (next_free) {
610 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
612 clean_dir (&dir_copy, count);
613 return next_free;