Rework strutils for usage GString instread of self-made buffers.
[midnight-commander.git] / src / dir.c
blobe093209af1bb9f620f654aaf02b5fe411140ff95
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"
32 #include "strutil.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 },
66 int
67 unsorted (file_entry *a, file_entry *b)
69 (void) a;
70 (void) b;
71 return 0;
74 int
75 sort_name (file_entry *a, file_entry *b)
77 int ad = MY_ISDIR (a);
78 int bd = MY_ISDIR (b);
80 if (ad == bd || mix_all_files) {
81 /* create key if does not exist, key will be freed after sorting */
82 if (a->sort_key == NULL)
83 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
84 if (b->sort_key == NULL)
85 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
87 return str_key_collate (a->sort_key, b->sort_key, case_sensitive)
88 * reverse;
90 return bd - ad;
93 int
94 sort_ext (file_entry *a, file_entry *b)
96 int r;
97 int ad = MY_ISDIR (a);
98 int bd = MY_ISDIR (b);
100 if (ad == bd || mix_all_files){
101 if (a->second_sort_key == NULL)
102 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
103 if (b->second_sort_key == NULL)
104 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
106 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
107 if (r)
108 return r * reverse;
109 else
110 return sort_name (a, b);
111 } else
112 return bd-ad;
116 sort_time (file_entry *a, file_entry *b)
118 int ad = MY_ISDIR (a);
119 int bd = MY_ISDIR (b);
121 if (ad == bd || mix_all_files) {
122 int result = a->st.st_mtime < b->st.st_mtime ? -1 :
123 a->st.st_mtime > b->st.st_mtime;
124 if (result != 0)
125 return result * reverse;
126 else
127 return sort_name (a, b);
129 else
130 return bd-ad;
134 sort_ctime (file_entry *a, file_entry *b)
136 int ad = MY_ISDIR (a);
137 int bd = MY_ISDIR (b);
139 if (ad == bd || mix_all_files) {
140 int result = a->st.st_ctime < b->st.st_ctime ? -1 :
141 a->st.st_ctime > b->st.st_ctime;
142 if (result != 0)
143 return result * reverse;
144 else
145 return sort_name (a, b);
147 else
148 return bd-ad;
152 sort_atime (file_entry *a, file_entry *b)
154 int ad = MY_ISDIR (a);
155 int bd = MY_ISDIR (b);
157 if (ad == bd || mix_all_files) {
158 int result = a->st.st_atime < b->st.st_atime ? -1 :
159 a->st.st_atime > b->st.st_atime;
160 if (result != 0)
161 return result * reverse;
162 else
163 return sort_name (a, b);
165 else
166 return bd-ad;
170 sort_inode (file_entry *a, file_entry *b)
172 int ad = MY_ISDIR (a);
173 int bd = MY_ISDIR (b);
175 if (ad == bd || mix_all_files)
176 return (a->st.st_ino - b->st.st_ino) * reverse;
177 else
178 return bd-ad;
182 sort_size (file_entry *a, file_entry *b)
184 int ad = MY_ISDIR (a);
185 int bd = MY_ISDIR (b);
186 int result = 0;
188 if (ad != bd && !mix_all_files)
189 return bd - ad;
191 result = a->st.st_size < b->st.st_size ? -1 :
192 a->st.st_size > b->st.st_size;
193 if (result != 0)
194 return result * reverse;
195 else
196 return sort_name (a, b);
199 /* clear keys, should be call after sorting is finished */
200 static void
201 clean_sort_keys (dir_list *list, int start, int count)
203 int i;
205 for (i = 0; i < count; i++){
206 str_release_key (list->list [i + start].sort_key, case_sensitive);
207 list->list [i + start].sort_key = NULL;
208 str_release_key (list->list [i + start].second_sort_key, case_sensitive);
209 list->list [i + start].second_sort_key = NULL;
214 void
215 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f, int exec_first_f)
217 int dot_dot_found = 0;
219 if (top == 0)
220 return;
222 /* If there is an ".." entry the caller must take care to
223 ensure that it occupies the first list element. */
224 if (!strcmp (list->list [0].fname, ".."))
225 dot_dot_found = 1;
227 reverse = reverse_f ? -1 : 1;
228 case_sensitive = case_sensitive_f;
229 exec_first = exec_first_f;
230 qsort (&(list->list) [dot_dot_found],
231 top + 1 - dot_dot_found, sizeof (file_entry), sort);
233 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
236 void
237 clean_dir (dir_list *list, int count)
239 int i;
241 for (i = 0; i < count; i++){
242 g_free (list->list [i].fname);
243 list->list [i].fname = NULL;
247 static int
248 add_dotdot_to_list (dir_list *list, int index)
250 /* Need to grow the *list? */
251 if (index == list->size) {
252 list->list = g_realloc (list->list, sizeof (file_entry) *
253 (list->size + RESIZE_STEPS));
254 if (!list->list)
255 return 0;
256 list->size += RESIZE_STEPS;
259 memset (&(list->list) [index], 0, sizeof(file_entry));
260 (list->list) [index].fnamelen = 2;
261 (list->list) [index].fname = g_strdup ("..");
262 (list->list) [index].f.link_to_dir = 0;
263 (list->list) [index].f.stale_link = 0;
264 (list->list) [index].f.dir_size_computed = 0;
265 (list->list) [index].f.marked = 0;
266 (list->list) [index].st.st_mode = 040755;
267 return 1;
270 /* Used to set up a directory list when there is no access to a directory */
272 set_zero_dir (dir_list *list)
274 return (add_dotdot_to_list (list, 0));
277 /* If you change handle_dirent then check also handle_path. */
278 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
279 static int
280 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
281 struct stat *buf1, int next_free, int *link_to_dir,
282 int *stale_link)
284 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
285 return 0;
286 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
287 return 0;
288 if (!show_dot_files && (dp->d_name[0] == '.'))
289 return 0;
290 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
291 return 0;
292 if (mc_lstat (dp->d_name, buf1) == -1) {
294 * lstat() fails - such entries should be identified by
295 * buf1->st_mode being 0.
296 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
298 memset (buf1, 0, sizeof (*buf1));
301 if (S_ISDIR (buf1->st_mode))
302 tree_store_mark_checked (dp->d_name);
304 /* A link to a file or a directory? */
305 *link_to_dir = 0;
306 *stale_link = 0;
307 if (S_ISLNK (buf1->st_mode)) {
308 struct stat buf2;
309 if (!mc_stat (dp->d_name, &buf2))
310 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
311 else
312 *stale_link = 1;
314 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
315 && !regexp_match (filter, dp->d_name, match_file))
316 return 0;
318 /* Need to grow the *list? */
319 if (next_free == list->size) {
320 list->list =
321 g_realloc (list->list,
322 sizeof (file_entry) * (list->size + RESIZE_STEPS));
323 if (!list->list)
324 return -1;
325 list->size += RESIZE_STEPS;
327 return 1;
330 /* handle_path is a simplified handle_dirent. The difference is that
331 handle_path doesn't pay attention to show_dot_files and show_backups.
332 Moreover handle_path can't be used with a filemask.
333 If you change handle_path then check also handle_dirent. */
334 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
336 handle_path (dir_list *list, const char *path,
337 struct stat *buf1, int next_free, int *link_to_dir,
338 int *stale_link)
340 if (path [0] == '.' && path [1] == 0)
341 return 0;
342 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
343 return 0;
344 if (mc_lstat (path, buf1) == -1)
345 return 0;
347 if (S_ISDIR (buf1->st_mode))
348 tree_store_mark_checked (path);
350 /* A link to a file or a directory? */
351 *link_to_dir = 0;
352 *stale_link = 0;
353 if (S_ISLNK(buf1->st_mode)){
354 struct stat buf2;
355 if (!mc_stat (path, &buf2))
356 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
357 else
358 *stale_link = 1;
361 /* Need to grow the *list? */
362 if (next_free == list->size){
363 list->list = g_realloc (list->list, sizeof (file_entry) *
364 (list->size + RESIZE_STEPS));
365 if (!list->list)
366 return -1;
367 list->size += RESIZE_STEPS;
369 return 1;
373 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
374 int case_sensitive, int exec_ff, const char *filter)
376 DIR *dirp;
377 struct dirent *dp;
378 int status, link_to_dir, stale_link;
379 int next_free = 0;
380 struct stat st;
382 /* ".." (if any) must be the first entry in the list */
383 if (set_zero_dir (list) == 0)
384 return next_free;
385 next_free++;
387 dirp = mc_opendir (path);
388 if (!dirp) {
389 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
390 return next_free;
392 tree_store_start_check (path);
393 /* Do not add a ".." entry to the root directory */
394 if (!strcmp (path, "/"))
395 next_free--;
396 while ((dp = mc_readdir (dirp))) {
397 status =
398 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
399 &stale_link);
400 if (status == 0)
401 continue;
402 if (status == -1) {
403 tree_store_end_check ();
404 mc_closedir (dirp);
405 return next_free;
407 list->list[next_free].fnamelen = NLENGTH (dp);
408 list->list[next_free].fname = g_strdup (dp->d_name);
409 list->list[next_free].f.marked = 0;
410 list->list[next_free].f.link_to_dir = link_to_dir;
411 list->list[next_free].f.stale_link = stale_link;
412 list->list[next_free].f.dir_size_computed = 0;
413 list->list[next_free].st = st;
414 list->list[next_free].sort_key = NULL;
415 list->list[next_free].second_sort_key = NULL;
416 next_free++;
417 if (!(next_free % 32))
418 rotate_dash ();
421 if (next_free) {
422 do_sort (list, sort, next_free - 1, reverse, case_sensitive, exec_ff);
425 mc_closedir (dirp);
426 tree_store_end_check ();
427 return next_free;
431 link_isdir (const file_entry *file)
433 if (file->f.link_to_dir)
434 return 1;
435 else
436 return 0;
440 if_link_is_exe (const char *full_name, const file_entry *file)
442 struct stat b;
444 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
445 return is_exe (b.st_mode);
446 } else
447 return 1;
450 static dir_list dir_copy = { 0, 0 };
452 static void
453 alloc_dir_copy (int size)
455 int i;
457 if (dir_copy.size < size){
458 if (dir_copy.list){
460 for (i = 0; i < dir_copy.size; i++) {
461 g_free (dir_copy.list [i].fname);
463 g_free (dir_copy.list);
464 dir_copy.list = 0;
467 dir_copy.list = g_new (file_entry, size);
468 for (i = 0; i < size; i++) {
469 dir_copy.list [i].fname = NULL;
470 dir_copy.list [i].sort_key = NULL;
471 dir_copy.list [i].second_sort_key = NULL;
474 dir_copy.size = size;
478 /* If filter is null, then it is a match */
480 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
481 int rev, int case_sensitive, int exec_ff, const char *filter)
483 DIR *dirp;
484 struct dirent *dp;
485 int next_free = 0;
486 int i, status, link_to_dir, stale_link;
487 struct stat st;
488 int marked_cnt;
489 GHashTable *marked_files;
491 dirp = mc_opendir (path);
492 if (!dirp) {
493 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
494 clean_dir (list, count);
495 return set_zero_dir (list);
498 tree_store_start_check (path);
499 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
500 alloc_dir_copy (list->size);
501 for (marked_cnt = i = 0; i < count; i++) {
502 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
503 dir_copy.list[i].fname = list->list[i].fname;
504 dir_copy.list[i].f.marked = list->list[i].f.marked;
505 dir_copy.list[i].f.dir_size_computed =
506 list->list[i].f.dir_size_computed;
507 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
508 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
509 dir_copy.list[i].sort_key = NULL;
510 dir_copy.list[i].second_sort_key = NULL;
511 if (list->list[i].f.marked) {
512 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
513 &dir_copy.list[i]);
514 marked_cnt++;
518 /* Add ".." except to the root directory. The ".." entry
519 (if any) must be the first in the list. */
520 if (strcmp (path, "/") != 0) {
521 if (set_zero_dir (list) == 0) {
522 clean_dir (list, count);
523 clean_dir (&dir_copy, count);
524 return next_free;
526 next_free++;
529 while ((dp = mc_readdir (dirp))) {
530 status =
531 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
532 &stale_link);
533 if (status == 0)
534 continue;
535 if (status == -1) {
536 mc_closedir (dirp);
537 /* Norbert (Feb 12, 1997):
538 Just in case someone finds this memory leak:
539 -1 means big trouble (at the moment no memory left),
540 I don't bother with further cleanup because if one gets to
541 this point he will have more problems than a few memory
542 leaks and because one 'clean_dir' would not be enough (and
543 because I don't want to spent the time to make it working,
544 IMHO it's not worthwhile).
545 clean_dir (&dir_copy, count);
547 tree_store_end_check ();
548 g_hash_table_destroy (marked_files);
549 return next_free;
552 list->list[next_free].f.marked = 0;
555 * If we have marked files in the copy, scan through the copy
556 * to find matching file. Decrease number of remaining marks if
557 * we copied one.
559 if (marked_cnt > 0) {
560 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
561 list->list[next_free].f.marked = 1;
562 marked_cnt--;
566 list->list[next_free].fnamelen = NLENGTH (dp);
567 list->list[next_free].fname = g_strdup (dp->d_name);
568 list->list[next_free].f.link_to_dir = link_to_dir;
569 list->list[next_free].f.stale_link = stale_link;
570 list->list[next_free].f.dir_size_computed = 0;
571 list->list[next_free].st = st;
572 list->list[next_free].sort_key = NULL;
573 list->list[next_free].second_sort_key = NULL;
574 next_free++;
575 if (!(next_free % 16))
576 rotate_dash ();
578 mc_closedir (dirp);
579 tree_store_end_check ();
580 g_hash_table_destroy (marked_files);
581 if (next_free) {
582 do_sort (list, sort, next_free - 1, rev, case_sensitive, exec_ff);
584 clean_dir (&dir_copy, count);
585 return next_free;