Removed calls to strerror() function from own libsamba implementation.
[midnight-commander.git] / src / dir.c
blob0be1b9da4846164c4f9430f4372d50d18e3a5c60
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_try_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 = g_try_realloc (list->list, sizeof (file_entry) *
331 (list->size + RESIZE_STEPS));
332 if (list->list == NULL)
333 return -1;
334 list->size += RESIZE_STEPS;
336 return 1;
339 /* get info about ".." */
340 static gboolean
341 get_dotdot_dir_stat (const char *path, struct stat *st)
343 gboolean ret = FALSE;
345 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
346 char *dotdot_dir;
347 struct stat s;
349 dotdot_dir = g_strdup_printf ("%s/../", path);
350 canonicalize_pathname (dotdot_dir);
351 ret = mc_stat (dotdot_dir, &s) == 0;
352 g_free (dotdot_dir);
353 *st = s;
356 return ret;
359 /* handle_path is a simplified handle_dirent. The difference is that
360 handle_path doesn't pay attention to show_dot_files and show_backups.
361 Moreover handle_path can't be used with a filemask.
362 If you change handle_path then check also handle_dirent. */
363 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
365 handle_path (dir_list *list, const char *path,
366 struct stat *buf1, int next_free, int *link_to_dir,
367 int *stale_link)
369 if (path [0] == '.' && path [1] == 0)
370 return 0;
371 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
372 return 0;
373 if (mc_lstat (path, buf1) == -1)
374 return 0;
376 if (S_ISDIR (buf1->st_mode))
377 tree_store_mark_checked (path);
379 /* A link to a file or a directory? */
380 *link_to_dir = 0;
381 *stale_link = 0;
382 if (S_ISLNK(buf1->st_mode)){
383 struct stat buf2;
384 if (!mc_stat (path, &buf2))
385 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
386 else
387 *stale_link = 1;
390 /* Need to grow the *list? */
391 if (next_free == list->size){
392 list->list = g_try_realloc (list->list, sizeof (file_entry) *
393 (list->size + RESIZE_STEPS));
394 if (list->list == NULL)
395 return -1;
396 list->size += RESIZE_STEPS;
398 return 1;
402 do_load_dir (const char *path, dir_list *list, sortfn *sort, int lc_reverse,
403 int lc_case_sensitive, int exec_ff, const char *filter)
405 DIR *dirp;
406 struct dirent *dp;
407 int status, link_to_dir, stale_link;
408 int next_free = 0;
409 struct stat st;
411 /* ".." (if any) must be the first entry in the list */
412 if (!set_zero_dir (list))
413 return next_free;
415 if (get_dotdot_dir_stat (path, &st))
416 list->list[next_free].st = st;
417 next_free++;
419 dirp = mc_opendir (path);
420 if (!dirp) {
421 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
422 return next_free;
425 tree_store_start_check (path);
427 /* Do not add a ".." entry to the root directory */
428 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
429 next_free--;
431 while ((dp = mc_readdir (dirp))) {
432 status =
433 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
434 &stale_link);
435 if (status == 0)
436 continue;
437 if (status == -1) {
438 tree_store_end_check ();
439 mc_closedir (dirp);
440 return next_free;
442 list->list[next_free].fnamelen = NLENGTH (dp);
443 list->list[next_free].fname = g_strdup (dp->d_name);
444 list->list[next_free].f.marked = 0;
445 list->list[next_free].f.link_to_dir = link_to_dir;
446 list->list[next_free].f.stale_link = stale_link;
447 list->list[next_free].f.dir_size_computed = 0;
448 list->list[next_free].st = st;
449 list->list[next_free].sort_key = NULL;
450 list->list[next_free].second_sort_key = NULL;
451 next_free++;
453 if ((next_free & 31) == 0)
454 rotate_dash ();
457 if (next_free != 0)
458 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
460 mc_closedir (dirp);
461 tree_store_end_check ();
462 return next_free;
466 link_isdir (const file_entry *file)
468 if (file->f.link_to_dir)
469 return 1;
470 else
471 return 0;
475 if_link_is_exe (const char *full_name, const file_entry *file)
477 struct stat b;
479 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
480 return is_exe (b.st_mode);
481 } else
482 return 1;
485 static dir_list dir_copy = { 0, 0 };
487 static void
488 alloc_dir_copy (int size)
490 if (dir_copy.size < size) {
491 if (dir_copy.list) {
492 int i;
493 for (i = 0; i < dir_copy.size; i++)
494 g_free (dir_copy.list [i].fname);
495 g_free (dir_copy.list);
498 dir_copy.list = g_new0 (file_entry, size);
499 dir_copy.size = size;
503 /* If filter is null, then it is a match */
505 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
506 int rev, int lc_case_sensitive, int exec_ff, const char *filter)
508 DIR *dirp;
509 struct dirent *dp;
510 int next_free = 0;
511 int i, status, link_to_dir, stale_link;
512 struct stat st;
513 int marked_cnt;
514 GHashTable *marked_files;
516 dirp = mc_opendir (path);
517 if (!dirp) {
518 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
519 clean_dir (list, count);
520 return set_zero_dir (list) ? 1 : 0;
523 tree_store_start_check (path);
524 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
525 alloc_dir_copy (list->size);
526 for (marked_cnt = i = 0; i < count; i++) {
527 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
528 dir_copy.list[i].fname = list->list[i].fname;
529 dir_copy.list[i].f.marked = list->list[i].f.marked;
530 dir_copy.list[i].f.dir_size_computed =
531 list->list[i].f.dir_size_computed;
532 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
533 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
534 dir_copy.list[i].sort_key = NULL;
535 dir_copy.list[i].second_sort_key = NULL;
536 if (list->list[i].f.marked) {
537 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
538 &dir_copy.list[i]);
539 marked_cnt++;
543 /* Add ".." except to the root directory. The ".." entry
544 (if any) must be the first in the list. */
545 if (!((path[0] == PATH_SEP) && (path[1] == '\0'))) {
546 if (!set_zero_dir (list)) {
547 clean_dir (list, count);
548 clean_dir (&dir_copy, count);
549 return next_free;
552 if (get_dotdot_dir_stat (path, &st))
553 list->list[next_free].st = st;
555 next_free++;
558 while ((dp = mc_readdir (dirp))) {
559 status =
560 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
561 &stale_link);
562 if (status == 0)
563 continue;
564 if (status == -1) {
565 mc_closedir (dirp);
566 /* Norbert (Feb 12, 1997):
567 Just in case someone finds this memory leak:
568 -1 means big trouble (at the moment no memory left),
569 I don't bother with further cleanup because if one gets to
570 this point he will have more problems than a few memory
571 leaks and because one 'clean_dir' would not be enough (and
572 because I don't want to spent the time to make it working,
573 IMHO it's not worthwhile).
574 clean_dir (&dir_copy, count);
576 tree_store_end_check ();
577 g_hash_table_destroy (marked_files);
578 return next_free;
581 list->list[next_free].f.marked = 0;
584 * If we have marked files in the copy, scan through the copy
585 * to find matching file. Decrease number of remaining marks if
586 * we copied one.
588 if (marked_cnt > 0) {
589 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
590 list->list[next_free].f.marked = 1;
591 marked_cnt--;
595 list->list[next_free].fnamelen = NLENGTH (dp);
596 list->list[next_free].fname = g_strdup (dp->d_name);
597 list->list[next_free].f.link_to_dir = link_to_dir;
598 list->list[next_free].f.stale_link = stale_link;
599 list->list[next_free].f.dir_size_computed = 0;
600 list->list[next_free].st = st;
601 list->list[next_free].sort_key = NULL;
602 list->list[next_free].second_sort_key = NULL;
603 next_free++;
604 if (!(next_free % 16))
605 rotate_dash ();
607 mc_closedir (dirp);
608 tree_store_end_check ();
609 g_hash_table_destroy (marked_files);
610 if (next_free) {
611 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
613 clean_dir (&dir_copy, count);
614 return next_free;