Not only comment it out but removing it
[midnight-commander.git] / src / dir.c
blobfab60419be1438716a1bee039cde7232bf8d05fd
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>
26 #include <sys/stat.h>
28 #include "global.h"
29 #include "tty.h"
30 #include "dir.h"
31 #include "wtools.h"
32 #include "treestore.h"
34 /* If true show files starting with a dot */
35 int show_dot_files = 1;
37 /* If true show files ending in ~ */
38 int show_backups = 1;
40 /* If false then directories are shown separately from files */
41 int mix_all_files = 0;
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 },
65 #ifdef HAVE_STRCOLL
67 * g_strcasecmp() doesn't work well in some locales because it relies on
68 * the locale-specific toupper(). On the other hand, strcoll() is case
69 * sensitive in the "C" and "POSIX" locales, unlike other locales.
70 * Solution: always use strcmp() for case sensitive sort. For case
71 * insensitive sort use strcoll() if it's case insensitive for ASCII and
72 * g_strcasecmp() otherwise.
74 typedef enum {
75 STRCOLL_NO,
76 STRCOLL_YES,
77 STRCOLL_TEST
78 } strcoll_status;
80 static int string_sortcomp (const char *str1, const char *str2)
82 static strcoll_status use_strcoll = STRCOLL_TEST;
84 if (case_sensitive) {
85 return strcmp (str1, str2);
88 /* Initialize use_strcoll once. */
89 if (use_strcoll == STRCOLL_TEST) {
90 /* Only use strcoll() if it considers "B" between "a" and "c". */
91 if (strcoll ("a", "B") * strcoll ("B", "c") > 0) {
92 use_strcoll = STRCOLL_YES;
93 } else {
94 use_strcoll = STRCOLL_NO;
98 if (use_strcoll == STRCOLL_NO)
99 return g_strcasecmp (str1, str2);
100 else
101 return strcoll (str1, str2);
103 #else
104 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
105 #endif
108 unsorted (const file_entry *a, const file_entry *b)
110 (void) a;
111 (void) b;
112 return 0;
116 sort_name (const file_entry *a, const file_entry *b)
118 int ad = MY_ISDIR (a);
119 int bd = MY_ISDIR (b);
121 if (ad == bd || mix_all_files)
122 return string_sortcomp (a->fname, b->fname) * reverse;
123 return bd-ad;
127 sort_ext (const file_entry *a, const file_entry *b)
129 const char *exta, *extb;
130 int r;
131 int ad = MY_ISDIR (a);
132 int bd = MY_ISDIR (b);
134 if (ad == bd || mix_all_files){
135 exta = extension (a->fname);
136 extb = extension (b->fname);
137 r = string_sortcomp (exta, extb);
138 if (r)
139 return r * reverse;
140 else
141 return sort_name (a, b);
142 } else
143 return bd-ad;
147 sort_time (const file_entry *a, const file_entry *b)
149 int ad = MY_ISDIR (a);
150 int bd = MY_ISDIR (b);
152 if (ad == bd || mix_all_files) {
153 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
154 a->st.st_mtime > b->st.st_mtime;
155 if (result != 0)
156 return result * reverse;
157 else
158 return sort_name (a, b);
160 else
161 return bd-ad;
165 sort_ctime (const file_entry *a, const file_entry *b)
167 int ad = MY_ISDIR (a);
168 int bd = MY_ISDIR (b);
170 if (ad == bd || mix_all_files) {
171 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
172 a->st.st_ctime > b->st.st_ctime;
173 if (result != 0)
174 return result * reverse;
175 else
176 return sort_name (a, b);
178 else
179 return bd-ad;
183 sort_atime (const file_entry *a, const file_entry *b)
185 int ad = MY_ISDIR (a);
186 int bd = MY_ISDIR (b);
188 if (ad == bd || mix_all_files) {
189 int result = a->st.st_atime < b->st.st_atime ? -1 :
190 a->st.st_atime > b->st.st_atime;
191 if (result != 0)
192 return result * reverse;
193 else
194 return sort_name (a, b);
196 else
197 return bd-ad;
201 sort_inode (const file_entry *a, const file_entry *b)
203 int ad = MY_ISDIR (a);
204 int bd = MY_ISDIR (b);
206 if (ad == bd || mix_all_files)
207 return (a->st.st_ino - b->st.st_ino) * reverse;
208 else
209 return bd-ad;
213 sort_size (const file_entry *a, const file_entry *b)
215 int ad = MY_ISDIR (a);
216 int bd = MY_ISDIR (b);
217 int result = 0;
219 if (ad != bd && !mix_all_files)
220 return bd - ad;
222 result = a->st.st_size < b->st.st_size ? -1 :
223 a->st.st_size > b->st.st_size;
224 if (result != 0)
225 return result * reverse;
226 else
227 return sort_name (a, b);
231 void
232 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
234 int dot_dot_found = 0;
236 if (top == 0)
237 return;
239 /* If there is an ".." entry the caller must take care to
240 ensure that it occupies the first list element. */
241 if (!strcmp (list->list [0].fname, ".."))
242 dot_dot_found = 1;
244 reverse = reverse_f ? -1 : 1;
245 case_sensitive = case_sensitive_f;
246 exec_first = exec_first_f;
247 qsort (&(list->list) [dot_dot_found],
248 top + 1 - dot_dot_found, sizeof (file_entry), sort);
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 = 0;
262 static int
263 add_dotdot_to_list (dir_list *list, int index)
265 /* Need to grow the *list? */
266 if (index == list->size) {
267 list->list = g_realloc (list->list, sizeof (file_entry) *
268 (list->size + RESIZE_STEPS));
269 if (!list->list)
270 return 0;
271 list->size += RESIZE_STEPS;
274 memset (&(list->list) [index], 0, sizeof(file_entry));
275 (list->list) [index].fnamelen = 2;
276 (list->list) [index].fname = g_strdup ("..");
277 (list->list) [index].f.link_to_dir = 0;
278 (list->list) [index].f.stale_link = 0;
279 (list->list) [index].f.dir_size_computed = 0;
280 (list->list) [index].f.marked = 0;
281 (list->list) [index].st.st_mode = 040755;
282 return 1;
285 /* Used to set up a directory list when there is no access to a directory */
287 set_zero_dir (dir_list *list)
289 return (add_dotdot_to_list (list, 0));
292 /* If you change handle_dirent then check also handle_path. */
293 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
294 static int
295 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
296 struct stat *buf1, int next_free, int *link_to_dir,
297 int *stale_link)
299 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
300 return 0;
301 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
302 return 0;
303 if (!show_dot_files && (dp->d_name[0] == '.'))
304 return 0;
305 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
306 return 0;
307 if (mc_lstat (dp->d_name, buf1) == -1) {
309 * lstat() fails - such entries should be identified by
310 * buf1->st_mode being 0.
311 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
313 memset (buf1, 0, sizeof (*buf1));
316 if (S_ISDIR (buf1->st_mode))
317 tree_store_mark_checked (dp->d_name);
319 /* A link to a file or a directory? */
320 *link_to_dir = 0;
321 *stale_link = 0;
322 if (S_ISLNK (buf1->st_mode)) {
323 struct stat buf2;
324 if (!mc_stat (dp->d_name, &buf2))
325 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
326 else
327 *stale_link = 1;
329 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
330 && !regexp_match (filter, dp->d_name, match_file))
331 return 0;
333 /* Need to grow the *list? */
334 if (next_free == list->size) {
335 list->list =
336 g_realloc (list->list,
337 sizeof (file_entry) * (list->size + RESIZE_STEPS));
338 if (!list->list)
339 return -1;
340 list->size += RESIZE_STEPS;
342 return 1;
345 /* handle_path is a simplified handle_dirent. The difference is that
346 handle_path doesn't pay attention to show_dot_files and show_backups.
347 Moreover handle_path can't be used with a filemask.
348 If you change handle_path then check also handle_dirent. */
349 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
351 handle_path (dir_list *list, const char *path,
352 struct stat *buf1, int next_free, int *link_to_dir,
353 int *stale_link)
355 if (path [0] == '.' && path [1] == 0)
356 return 0;
357 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
358 return 0;
359 if (mc_lstat (path, buf1) == -1)
360 return 0;
362 if (S_ISDIR (buf1->st_mode))
363 tree_store_mark_checked (path);
365 /* A link to a file or a directory? */
366 *link_to_dir = 0;
367 *stale_link = 0;
368 if (S_ISLNK(buf1->st_mode)){
369 struct stat buf2;
370 if (!mc_stat (path, &buf2))
371 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
372 else
373 *stale_link = 1;
376 /* Need to grow the *list? */
377 if (next_free == list->size){
378 list->list = g_realloc (list->list, sizeof (file_entry) *
379 (list->size + RESIZE_STEPS));
380 if (!list->list)
381 return -1;
382 list->size += RESIZE_STEPS;
384 return 1;
388 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
389 int case_sensitive, int exec_ff, const char *filter)
391 DIR *dirp;
392 struct dirent *dp;
393 int status, link_to_dir, stale_link;
394 int next_free = 0;
395 struct stat st;
397 /* ".." (if any) must be the first entry in the list */
398 if (set_zero_dir (list) == 0)
399 return next_free;
400 next_free++;
402 dirp = mc_opendir (path);
403 if (!dirp) {
404 message (1, MSG_ERROR, _("Cannot read directory contents"));
405 return next_free;
407 tree_store_start_check (path);
408 /* Do not add a ".." entry to the root directory */
409 if (!strcmp (path, "/"))
410 next_free--;
411 while ((dp = mc_readdir (dirp))) {
412 status =
413 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
414 &stale_link);
415 if (status == 0)
416 continue;
417 if (status == -1) {
418 tree_store_end_check ();
419 mc_closedir (dirp);
420 return next_free;
422 list->list[next_free].fnamelen = NLENGTH (dp);
423 list->list[next_free].fname = g_strdup (dp->d_name);
424 list->list[next_free].f.marked = 0;
425 list->list[next_free].f.link_to_dir = link_to_dir;
426 list->list[next_free].f.stale_link = stale_link;
427 list->list[next_free].f.dir_size_computed = 0;
428 list->list[next_free].st = st;
429 next_free++;
430 if (!(next_free % 32))
431 rotate_dash ();
434 if (next_free) {
435 do_sort (list, sort, next_free - 1, reverse, case_sensitive, exec_ff);
438 mc_closedir (dirp);
439 tree_store_end_check ();
440 return next_free;
444 link_isdir (const file_entry *file)
446 if (file->f.link_to_dir)
447 return 1;
448 else
449 return 0;
453 if_link_is_exe (const char *full_name, const file_entry *file)
455 struct stat b;
457 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
458 return is_exe (b.st_mode);
459 } else
460 return 1;
463 static dir_list dir_copy = { 0, 0 };
465 static void
466 alloc_dir_copy (int size)
468 int i;
470 if (dir_copy.size < size){
471 if (dir_copy.list){
473 for (i = 0; i < dir_copy.size; i++) {
474 g_free (dir_copy.list [i].fname);
476 g_free (dir_copy.list);
477 dir_copy.list = 0;
480 dir_copy.list = g_new (file_entry, size);
481 for (i = 0; i < size; i++)
482 dir_copy.list [i].fname = 0;
484 dir_copy.size = size;
488 /* If filter is null, then it is a match */
490 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
491 int rev, int case_sensitive, int exec_ff, const char *filter)
493 DIR *dirp;
494 struct dirent *dp;
495 int next_free = 0;
496 int i, status, link_to_dir, stale_link;
497 struct stat st;
498 int marked_cnt;
499 GHashTable *marked_files;
501 dirp = mc_opendir (path);
502 if (!dirp) {
503 message (1, MSG_ERROR, _("Cannot read directory contents"));
504 clean_dir (list, count);
505 return set_zero_dir (list);
508 tree_store_start_check (path);
509 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
510 alloc_dir_copy (list->size);
511 for (marked_cnt = i = 0; i < count; i++) {
512 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
513 dir_copy.list[i].fname = list->list[i].fname;
514 dir_copy.list[i].f.marked = list->list[i].f.marked;
515 dir_copy.list[i].f.dir_size_computed =
516 list->list[i].f.dir_size_computed;
517 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
518 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
519 if (list->list[i].f.marked) {
520 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
521 &dir_copy.list[i]);
522 marked_cnt++;
526 /* Add ".." except to the root directory. The ".." entry
527 (if any) must be the first in the list. */
528 if (strcmp (path, "/") != 0) {
529 if (set_zero_dir (list) == 0) {
530 clean_dir (list, count);
531 clean_dir (&dir_copy, count);
532 return next_free;
534 next_free++;
537 while ((dp = mc_readdir (dirp))) {
538 status =
539 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
540 &stale_link);
541 if (status == 0)
542 continue;
543 if (status == -1) {
544 mc_closedir (dirp);
545 /* Norbert (Feb 12, 1997):
546 Just in case someone finds this memory leak:
547 -1 means big trouble (at the moment no memory left),
548 I don't bother with further cleanup because if one gets to
549 this point he will have more problems than a few memory
550 leaks and because one 'clean_dir' would not be enough (and
551 because I don't want to spent the time to make it working,
552 IMHO it's not worthwhile).
553 clean_dir (&dir_copy, count);
555 tree_store_end_check ();
556 g_hash_table_destroy (marked_files);
557 return next_free;
560 list->list[next_free].f.marked = 0;
563 * If we have marked files in the copy, scan through the copy
564 * to find matching file. Decrease number of remaining marks if
565 * we copied one.
567 if (marked_cnt > 0) {
568 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
569 list->list[next_free].f.marked = 1;
570 marked_cnt--;
574 list->list[next_free].fnamelen = NLENGTH (dp);
575 list->list[next_free].fname = g_strdup (dp->d_name);
576 list->list[next_free].f.link_to_dir = link_to_dir;
577 list->list[next_free].f.stale_link = stale_link;
578 list->list[next_free].f.dir_size_computed = 0;
579 list->list[next_free].st = st;
580 next_free++;
581 if (!(next_free % 16))
582 rotate_dash ();
584 mc_closedir (dirp);
585 tree_store_end_check ();
586 g_hash_table_destroy (marked_files);
587 if (next_free) {
588 do_sort (list, sort, next_free - 1, rev, case_sensitive, exec_ff);
590 clean_dir (&dir_copy, count);
591 return next_free;