Ticket #1711: i18n: context and cleanup in file prompt strings
[midnight-commander.git] / src / dir.c
blobb4b448fa4cca093d4aedc2948d825543b6f9192e
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 "global.h"
31 #include "../src/tty/tty.h"
32 #include "dir.h"
33 #include "wtools.h"
34 #include "treestore.h"
35 #include "strutil.h"
36 #include "fs.h"
37 #include "../src/search/search.h"
39 /* If true show files starting with a dot */
40 int show_dot_files = 1;
42 /* If true show files ending in ~ */
43 int show_backups = 1;
45 /* If false then directories are shown separately from files */
46 int mix_all_files = 0;
49 * If true, SI units (1000 based) will be used for
50 * larger units (kilobyte, megabyte, ...).
51 * If false binary units (1024 based) will be used.
53 int kilobyte_si = 0;
55 /* Reverse flag */
56 static int reverse = 1;
58 /* Are the files sorted case sensitively? */
59 static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
61 /* Are the exec_bit files top in list*/
62 static int exec_first = 1;
64 #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) )
66 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
67 { N_("&Unsorted"), unsorted },
68 { N_("&Name"), sort_name },
69 { N_("&Extension"), sort_ext },
70 { N_("&Modify time"), sort_time },
71 { N_("&Access time"), sort_atime },
72 { N_("C&Hange time"), sort_ctime },
73 { N_("&Size"), sort_size },
74 { N_("&Inode"), sort_inode },
78 int
79 unsorted (file_entry *a, file_entry *b)
81 (void) a;
82 (void) b;
83 return 0;
86 int
87 sort_name (file_entry *a, file_entry *b)
89 int ad = MY_ISDIR (a);
90 int bd = MY_ISDIR (b);
92 if (ad == bd || mix_all_files) {
93 /* create key if does not exist, key will be freed after sorting */
94 if (a->sort_key == NULL)
95 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
96 if (b->sort_key == NULL)
97 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
99 return str_key_collate (a->sort_key, b->sort_key, case_sensitive)
100 * reverse;
102 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 || 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 || 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 || 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 || 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 || 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 && !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 static int
260 add_dotdot_to_list (dir_list *list, int index)
262 /* Need to grow the *list? */
263 if (index == list->size) {
264 list->list = g_realloc (list->list, sizeof (file_entry) *
265 (list->size + RESIZE_STEPS));
266 if (!list->list)
267 return 0;
268 list->size += RESIZE_STEPS;
271 memset (&(list->list) [index], 0, sizeof(file_entry));
272 (list->list) [index].fnamelen = 2;
273 (list->list) [index].fname = g_strdup ("..");
274 (list->list) [index].f.link_to_dir = 0;
275 (list->list) [index].f.stale_link = 0;
276 (list->list) [index].f.dir_size_computed = 0;
277 (list->list) [index].f.marked = 0;
278 (list->list) [index].st.st_mode = 040755;
279 return 1;
282 /* Used to set up a directory list when there is no access to a directory */
284 set_zero_dir (dir_list *list)
286 return (add_dotdot_to_list (list, 0));
289 /* If you change handle_dirent then check also handle_path. */
290 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
291 static int
292 handle_dirent (dir_list *list, const char *filter, struct dirent *dp,
293 struct stat *buf1, int next_free, int *link_to_dir,
294 int *stale_link)
296 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
297 return 0;
298 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
299 return 0;
300 if (!show_dot_files && (dp->d_name[0] == '.'))
301 return 0;
302 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
303 return 0;
304 if (mc_lstat (dp->d_name, buf1) == -1) {
306 * lstat() fails - such entries should be identified by
307 * buf1->st_mode being 0.
308 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
310 memset (buf1, 0, sizeof (*buf1));
313 if (S_ISDIR (buf1->st_mode))
314 tree_store_mark_checked (dp->d_name);
316 /* A link to a file or a directory? */
317 *link_to_dir = 0;
318 *stale_link = 0;
319 if (S_ISLNK (buf1->st_mode)) {
320 struct stat buf2;
321 if (!mc_stat (dp->d_name, &buf2))
322 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
323 else
324 *stale_link = 1;
326 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && filter
327 && !mc_search(filter, dp->d_name, MC_SEARCH_T_GLOB) )
328 return 0;
330 /* Need to grow the *list? */
331 if (next_free == list->size) {
332 list->list =
333 g_realloc (list->list,
334 sizeof (file_entry) * (list->size + RESIZE_STEPS));
335 if (!list->list)
336 return -1;
337 list->size += RESIZE_STEPS;
339 return 1;
342 /* handle_path is a simplified handle_dirent. The difference is that
343 handle_path doesn't pay attention to show_dot_files and show_backups.
344 Moreover handle_path can't be used with a filemask.
345 If you change handle_path then check also handle_dirent. */
346 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
348 handle_path (dir_list *list, const char *path,
349 struct stat *buf1, int next_free, int *link_to_dir,
350 int *stale_link)
352 if (path [0] == '.' && path [1] == 0)
353 return 0;
354 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
355 return 0;
356 if (mc_lstat (path, buf1) == -1)
357 return 0;
359 if (S_ISDIR (buf1->st_mode))
360 tree_store_mark_checked (path);
362 /* A link to a file or a directory? */
363 *link_to_dir = 0;
364 *stale_link = 0;
365 if (S_ISLNK(buf1->st_mode)){
366 struct stat buf2;
367 if (!mc_stat (path, &buf2))
368 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
369 else
370 *stale_link = 1;
373 /* Need to grow the *list? */
374 if (next_free == list->size){
375 list->list = g_realloc (list->list, sizeof (file_entry) *
376 (list->size + RESIZE_STEPS));
377 if (!list->list)
378 return -1;
379 list->size += RESIZE_STEPS;
381 return 1;
385 do_load_dir (const char *path, dir_list *list, sortfn *sort, int reverse,
386 int case_sensitive, int exec_ff, const char *filter)
388 DIR *dirp;
389 struct dirent *dp;
390 int status, link_to_dir, stale_link;
391 int next_free = 0;
392 struct stat st;
394 /* ".." (if any) must be the first entry in the list */
395 if (set_zero_dir (list) == 0)
396 return next_free;
397 next_free++;
399 dirp = mc_opendir (path);
400 if (!dirp) {
401 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
402 return next_free;
404 tree_store_start_check (path);
405 /* Do not add a ".." entry to the root directory */
406 if (!strcmp (path, "/"))
407 next_free--;
408 while ((dp = mc_readdir (dirp))) {
409 status =
410 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
411 &stale_link);
412 if (status == 0)
413 continue;
414 if (status == -1) {
415 tree_store_end_check ();
416 mc_closedir (dirp);
417 return next_free;
419 list->list[next_free].fnamelen = NLENGTH (dp);
420 list->list[next_free].fname = g_strdup (dp->d_name);
421 list->list[next_free].f.marked = 0;
422 list->list[next_free].f.link_to_dir = link_to_dir;
423 list->list[next_free].f.stale_link = stale_link;
424 list->list[next_free].f.dir_size_computed = 0;
425 list->list[next_free].st = st;
426 list->list[next_free].sort_key = NULL;
427 list->list[next_free].second_sort_key = NULL;
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 = NULL;
482 dir_copy.list [i].sort_key = NULL;
483 dir_copy.list [i].second_sort_key = NULL;
486 dir_copy.size = size;
490 /* If filter is null, then it is a match */
492 do_reload_dir (const char *path, dir_list *list, sortfn *sort, int count,
493 int rev, int case_sensitive, int exec_ff, const char *filter)
495 DIR *dirp;
496 struct dirent *dp;
497 int next_free = 0;
498 int i, status, link_to_dir, stale_link;
499 struct stat st;
500 int marked_cnt;
501 GHashTable *marked_files;
503 dirp = mc_opendir (path);
504 if (!dirp) {
505 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
506 clean_dir (list, count);
507 return set_zero_dir (list);
510 tree_store_start_check (path);
511 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
512 alloc_dir_copy (list->size);
513 for (marked_cnt = i = 0; i < count; i++) {
514 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
515 dir_copy.list[i].fname = list->list[i].fname;
516 dir_copy.list[i].f.marked = list->list[i].f.marked;
517 dir_copy.list[i].f.dir_size_computed =
518 list->list[i].f.dir_size_computed;
519 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
520 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
521 dir_copy.list[i].sort_key = NULL;
522 dir_copy.list[i].second_sort_key = NULL;
523 if (list->list[i].f.marked) {
524 g_hash_table_insert (marked_files, dir_copy.list[i].fname,
525 &dir_copy.list[i]);
526 marked_cnt++;
530 /* Add ".." except to the root directory. The ".." entry
531 (if any) must be the first in the list. */
532 if (strcmp (path, "/") != 0) {
533 if (set_zero_dir (list) == 0) {
534 clean_dir (list, count);
535 clean_dir (&dir_copy, count);
536 return next_free;
538 next_free++;
541 while ((dp = mc_readdir (dirp))) {
542 status =
543 handle_dirent (list, filter, dp, &st, next_free, &link_to_dir,
544 &stale_link);
545 if (status == 0)
546 continue;
547 if (status == -1) {
548 mc_closedir (dirp);
549 /* Norbert (Feb 12, 1997):
550 Just in case someone finds this memory leak:
551 -1 means big trouble (at the moment no memory left),
552 I don't bother with further cleanup because if one gets to
553 this point he will have more problems than a few memory
554 leaks and because one 'clean_dir' would not be enough (and
555 because I don't want to spent the time to make it working,
556 IMHO it's not worthwhile).
557 clean_dir (&dir_copy, count);
559 tree_store_end_check ();
560 g_hash_table_destroy (marked_files);
561 return next_free;
564 list->list[next_free].f.marked = 0;
567 * If we have marked files in the copy, scan through the copy
568 * to find matching file. Decrease number of remaining marks if
569 * we copied one.
571 if (marked_cnt > 0) {
572 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
573 list->list[next_free].f.marked = 1;
574 marked_cnt--;
578 list->list[next_free].fnamelen = NLENGTH (dp);
579 list->list[next_free].fname = g_strdup (dp->d_name);
580 list->list[next_free].f.link_to_dir = link_to_dir;
581 list->list[next_free].f.stale_link = stale_link;
582 list->list[next_free].f.dir_size_computed = 0;
583 list->list[next_free].st = st;
584 list->list[next_free].sort_key = NULL;
585 list->list[next_free].second_sort_key = NULL;
586 next_free++;
587 if (!(next_free % 16))
588 rotate_dash ();
590 mc_closedir (dirp);
591 tree_store_end_check ();
592 g_hash_table_destroy (marked_files);
593 if (next_free) {
594 do_sort (list, sort, next_free - 1, rev, case_sensitive, exec_ff);
596 clean_dir (&dir_copy, count);
597 return next_free;