Completely removed MHL stuff
[midnight-commander.git] / src / dir.c
blobb87cd70619d9c99683011c414f08d492e99da18a
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>
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 /* 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 },
64 #ifdef HAVE_STRCOLL
66 * g_strcasecmp() doesn't work well in some locales because it relies on
67 * the locale-specific toupper(). On the other hand, strcoll() is case
68 * sensitive in the "C" and "POSIX" locales, unlike other locales.
69 * Solution: always use strcmp() for case sensitive sort. For case
70 * insensitive sort use strcoll() if it's case insensitive for ASCII and
71 * g_strcasecmp() otherwise.
73 typedef enum {
74 STRCOLL_NO,
75 STRCOLL_YES,
76 STRCOLL_TEST
77 } strcoll_status;
79 static int string_sortcomp (const char *str1, const char *str2)
81 static strcoll_status use_strcoll = STRCOLL_TEST;
83 if (case_sensitive) {
84 return strcmp (str1, str2);
87 /* Initialize use_strcoll once. */
88 if (use_strcoll == STRCOLL_TEST) {
89 /* Only use strcoll() if it considers "B" between "a" and "c". */
90 if (strcoll ("a", "B") * strcoll ("B", "c") > 0) {
91 use_strcoll = STRCOLL_YES;
92 } else {
93 use_strcoll = STRCOLL_NO;
97 if (use_strcoll == STRCOLL_NO)
98 return g_strcasecmp (str1, str2);
99 else
100 return strcoll (str1, str2);
102 #else
103 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
104 #endif
107 unsorted (const file_entry *a, const file_entry *b)
109 (void) a;
110 (void) b;
111 return 0;
115 sort_name (const file_entry *a, const file_entry *b)
117 int ad = MY_ISDIR (a);
118 int bd = MY_ISDIR (b);
120 if (ad == bd || mix_all_files)
121 return string_sortcomp (a->fname, b->fname) * reverse;
122 return bd-ad;
126 sort_ext (const file_entry *a, const file_entry *b)
128 const char *exta, *extb;
129 int r;
130 int ad = MY_ISDIR (a);
131 int bd = MY_ISDIR (b);
133 if (ad == bd || mix_all_files){
134 exta = extension (a->fname);
135 extb = extension (b->fname);
136 r = string_sortcomp (exta, extb);
137 if (r)
138 return r * reverse;
139 else
140 return sort_name (a, b);
141 } else
142 return bd-ad;
146 sort_time (const file_entry *a, const file_entry *b)
148 int ad = MY_ISDIR (a);
149 int bd = MY_ISDIR (b);
151 if (ad == bd || mix_all_files) {
152 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
153 a->st.st_mtime > b->st.st_mtime;
154 if (result != 0)
155 return result * reverse;
156 else
157 return sort_name (a, b);
159 else
160 return bd-ad;
164 sort_ctime (const file_entry *a, const file_entry *b)
166 int ad = MY_ISDIR (a);
167 int bd = MY_ISDIR (b);
169 if (ad == bd || mix_all_files) {
170 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
171 a->st.st_ctime > b->st.st_ctime;
172 if (result != 0)
173 return result * reverse;
174 else
175 return sort_name (a, b);
177 else
178 return bd-ad;
182 sort_atime (const file_entry *a, const file_entry *b)
184 int ad = MY_ISDIR (a);
185 int bd = MY_ISDIR (b);
187 if (ad == bd || mix_all_files) {
188 int result = a->st.st_atime < b->st.st_atime ? -1 :
189 a->st.st_atime > b->st.st_atime;
190 if (result != 0)
191 return result * reverse;
192 else
193 return sort_name (a, b);
195 else
196 return bd-ad;
200 sort_inode (const file_entry *a, const file_entry *b)
202 int ad = MY_ISDIR (a);
203 int bd = MY_ISDIR (b);
205 if (ad == bd || mix_all_files)
206 return (a->st.st_ino - b->st.st_ino) * reverse;
207 else
208 return bd-ad;
212 sort_size (const file_entry *a, const file_entry *b)
214 int ad = MY_ISDIR (a);
215 int bd = MY_ISDIR (b);
216 int result = 0;
218 if (ad != bd && !mix_all_files)
219 return bd - ad;
221 result = a->st.st_size < b->st.st_size ? -1 :
222 a->st.st_size > b->st.st_size;
223 if (result != 0)
224 return result * reverse;
225 else
226 return sort_name (a, b);
230 void
231 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
233 int dot_dot_found = 0;
235 if (top == 0)
236 return;
238 /* If there is an ".." entry the caller must take care to
239 ensure that it occupies the first list element. */
240 if (!strcmp (list->list [0].fname, ".."))
241 dot_dot_found = 1;
243 reverse = reverse_f ? -1 : 1;
244 case_sensitive = case_sensitive_f;
245 exec_first = exec_first_f;
246 qsort (&(list->list) [dot_dot_found],
247 top + 1 - dot_dot_found, sizeof (file_entry), sort);
250 void
251 clean_dir (dir_list *list, int count)
253 int i;
255 for (i = 0; i < count; i++){
256 g_free (list->list [i].fname);
257 list->list [i].fname = 0;
261 static int
262 add_dotdot_to_list (dir_list *list, int index)
264 /* Need to grow the *list? */
265 if (index == list->size) {
266 list->list = g_realloc (list->list, sizeof (file_entry) *
267 (list->size + RESIZE_STEPS));
268 if (!list->list)
269 return 0;
270 list->size += RESIZE_STEPS;
273 memset (&(list->list) [index], 0, sizeof(file_entry));
274 (list->list) [index].fnamelen = 2;
275 (list->list) [index].fname = g_strdup ("..");
276 (list->list) [index].f.link_to_dir = 0;
277 (list->list) [index].f.stale_link = 0;
278 (list->list) [index].f.dir_size_computed = 0;
279 (list->list) [index].f.marked = 0;
280 (list->list) [index].st.st_mode = 040755;
281 return 1;
284 /* Used to set up a directory list when there is no access to a directory */
286 set_zero_dir (dir_list *list)
288 return (add_dotdot_to_list (list, 0));
291 /* If you change handle_dirent then check also handle_path. */
292 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
293 static int
294 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
295 struct stat *buf1, int next_free, int *link_to_dir,
296 int *stale_link)
298 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
299 return 0;
300 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
301 return 0;
302 if (!show_dot_files && (dp->d_name[0] == '.'))
303 return 0;
304 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
305 return 0;
306 if (mc_lstat (dp->d_name, buf1) == -1) {
308 * lstat() fails - such entries should be identified by
309 * buf1->st_mode being 0.
310 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
312 memset (buf1, 0, sizeof (*buf1));
315 if (S_ISDIR (buf1->st_mode))
316 tree_store_mark_checked (dp->d_name);
318 /* A link to a file or a directory? */
319 *link_to_dir = 0;
320 *stale_link = 0;
321 if (S_ISLNK (buf1->st_mode)) {
322 struct stat buf2;
323 if (!mc_stat (dp->d_name, &buf2))
324 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
325 else
326 *stale_link = 1;
328 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
329 && !regexp_match (filter, dp->d_name, match_file))
330 return 0;
332 /* Need to grow the *list? */
333 if (next_free == list->size) {
334 list->list =
335 g_realloc (list->list,
336 sizeof (file_entry) * (list->size + RESIZE_STEPS));
337 if (!list->list)
338 return -1;
339 list->size += RESIZE_STEPS;
341 return 1;
344 /* handle_path is a simplified handle_dirent. The difference is that
345 handle_path doesn't pay attention to show_dot_files and show_backups.
346 Moreover handle_path can't be used with a filemask.
347 If you change handle_path then check also handle_dirent. */
348 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
350 handle_path (dir_list *list, const char *path,
351 struct stat *buf1, int next_free, int *link_to_dir,
352 int *stale_link)
354 if (path [0] == '.' && path [1] == 0)
355 return 0;
356 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
357 return 0;
358 if (mc_lstat (path, buf1) == -1)
359 return 0;
361 if (S_ISDIR (buf1->st_mode))
362 tree_store_mark_checked (path);
364 /* A link to a file or a directory? */
365 *link_to_dir = 0;
366 *stale_link = 0;
367 if (S_ISLNK(buf1->st_mode)){
368 struct stat buf2;
369 if (!mc_stat (path, &buf2))
370 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
371 else
372 *stale_link = 1;
375 /* Need to grow the *list? */
376 if (next_free == list->size){
377 list->list = g_realloc (list->list, sizeof (file_entry) *
378 (list->size + RESIZE_STEPS));
379 if (!list->list)
380 return -1;
381 list->size += RESIZE_STEPS;
383 return 1;
387 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
388 int case_sensitive, int exec_ff, const char *filter)
390 DIR *dirp;
391 struct dirent *dp;
392 int status, link_to_dir, stale_link;
393 int next_free = 0;
394 struct stat st;
396 /* ".." (if any) must be the first entry in the list */
397 if (set_zero_dir (list) == 0)
398 return next_free;
399 next_free++;
401 dirp = mc_opendir (path);
402 if (!dirp) {
403 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
404 return next_free;
406 tree_store_start_check (path);
407 /* Do not add a ".." entry to the root directory */
408 if (!strcmp (path, "/"))
409 next_free--;
410 while ((dp = mc_readdir (dirp))) {
411 status =
412 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
413 &stale_link);
414 if (status == 0)
415 continue;
416 if (status == -1) {
417 tree_store_end_check ();
418 mc_closedir (dirp);
419 return next_free;
421 list->list[next_free].fnamelen = NLENGTH (dp);
422 list->list[next_free].fname = g_strdup (dp->d_name);
423 list->list[next_free].f.marked = 0;
424 list->list[next_free].f.link_to_dir = link_to_dir;
425 list->list[next_free].f.stale_link = stale_link;
426 list->list[next_free].f.dir_size_computed = 0;
427 list->list[next_free].st = st;
428 next_free++;
429 if (!(next_free % 32))
430 rotate_dash ();
433 if (next_free) {
434 do_sort (list, sort, next_free - 1, reverse, case_sensitive, exec_ff);
437 mc_closedir (dirp);
438 tree_store_end_check ();
439 return next_free;
443 link_isdir (const file_entry *file)
445 if (file->f.link_to_dir)
446 return 1;
447 else
448 return 0;
452 if_link_is_exe (const char *full_name, const file_entry *file)
454 struct stat b;
456 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
457 return is_exe (b.st_mode);
458 } else
459 return 1;
462 static dir_list dir_copy = { 0, 0 };
464 static void
465 alloc_dir_copy (int size)
467 int i;
469 if (dir_copy.size < size){
470 if (dir_copy.list){
472 for (i = 0; i < dir_copy.size; i++) {
473 g_free (dir_copy.list [i].fname);
475 g_free (dir_copy.list);
476 dir_copy.list = 0;
479 dir_copy.list = g_new (file_entry, size);
480 for (i = 0; i < size; i++)
481 dir_copy.list [i].fname = 0;
483 dir_copy.size = size;
487 /* If filter is null, then it is a match */
489 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
490 int rev, int case_sensitive, int exec_ff, const char *filter)
492 DIR *dirp;
493 struct dirent *dp;
494 int next_free = 0;
495 int i, status, link_to_dir, stale_link;
496 struct stat st;
497 int marked_cnt;
498 GHashTable *marked_files;
500 dirp = mc_opendir (path);
501 if (!dirp) {
502 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
503 clean_dir (list, count);
504 return set_zero_dir (list);
507 tree_store_start_check (path);
508 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
509 alloc_dir_copy (list->size);
510 for (marked_cnt = i = 0; i < count; i++) {
511 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
512 dir_copy.list[i].fname = list->list[i].fname;
513 dir_copy.list[i].f.marked = list->list[i].f.marked;
514 dir_copy.list[i].f.dir_size_computed =
515 list->list[i].f.dir_size_computed;
516 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
517 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
518 if (list->list[i].f.marked) {
519 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
520 &dir_copy.list[i]);
521 marked_cnt++;
525 /* Add ".." except to the root directory. The ".." entry
526 (if any) must be the first in the list. */
527 if (strcmp (path, "/") != 0) {
528 if (set_zero_dir (list) == 0) {
529 clean_dir (list, count);
530 clean_dir (&dir_copy, count);
531 return next_free;
533 next_free++;
536 while ((dp = mc_readdir (dirp))) {
537 status =
538 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
539 &stale_link);
540 if (status == 0)
541 continue;
542 if (status == -1) {
543 mc_closedir (dirp);
544 /* Norbert (Feb 12, 1997):
545 Just in case someone finds this memory leak:
546 -1 means big trouble (at the moment no memory left),
547 I don't bother with further cleanup because if one gets to
548 this point he will have more problems than a few memory
549 leaks and because one 'clean_dir' would not be enough (and
550 because I don't want to spent the time to make it working,
551 IMHO it's not worthwhile).
552 clean_dir (&dir_copy, count);
554 tree_store_end_check ();
555 g_hash_table_destroy (marked_files);
556 return next_free;
559 list->list[next_free].f.marked = 0;
562 * If we have marked files in the copy, scan through the copy
563 * to find matching file. Decrease number of remaining marks if
564 * we copied one.
566 if (marked_cnt > 0) {
567 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
568 list->list[next_free].f.marked = 1;
569 marked_cnt--;
573 list->list[next_free].fnamelen = NLENGTH (dp);
574 list->list[next_free].fname = g_strdup (dp->d_name);
575 list->list[next_free].f.link_to_dir = link_to_dir;
576 list->list[next_free].f.stale_link = stale_link;
577 list->list[next_free].f.dir_size_computed = 0;
578 list->list[next_free].st = st;
579 next_free++;
580 if (!(next_free % 16))
581 rotate_dash ();
583 mc_closedir (dirp);
584 tree_store_end_check ();
585 g_hash_table_destroy (marked_files);
586 if (next_free) {
587 do_sort (list, sort, next_free - 1, rev, case_sensitive, exec_ff);
589 clean_dir (&dir_copy, count);
590 return next_free;