1999-09-09 Federico Mena Quintero <federico@redhat.com>
[midnight-commander.git] / src / dir.c
blob60b0000a130db19e7164a6c8d3caa70873112bfe
1 /* Directory routines
2 Copyright (C) 1994 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include <config.h>
19 #define DIR_H_INCLUDE_HANDLE_DIRENT
20 #include "tty.h"
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include "x.h"
27 #include "global.h"
28 #include "dir.h"
29 #include "dialog.h"
30 #include "tree.h"
31 #include "../vfs/vfs.h"
33 /* "$Id$" */
36 * If true show files starting with a dot.
38 * In GNOME we default to not showing them.
40 #ifdef HAVE_GNOME
41 int show_dot_files = 0;
42 #else
43 int show_dot_files = 1;
44 #endif
46 /* If true show files ending in ~ */
47 int show_backups = 0;
49 /* If false then directories are shown separately from files */
50 int mix_all_files = 0;
52 /* Reverse flag */
53 static int reverse = 1;
55 /* Are the files sorted case sensitively? */
56 static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
58 #define MY_ISDIR(x) ( (S_ISDIR (x->buf.st_mode) || x->f.link_to_dir) ? 1 : 0)
60 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
61 { N_("&Unsorted"), unsorted },
62 { N_("&Name"), sort_name },
63 { N_("&Extension"), sort_ext },
64 { N_("&Modify time"), sort_time },
65 { N_("&Access time"), sort_atime },
66 { N_("&Change time"), sort_ctime },
67 { N_("&Size"), sort_size },
68 { N_("&Inode"), sort_inode },
70 /* New sort orders */
71 { N_("&Type"), sort_type },
72 { N_("&Links"), sort_links },
73 { N_("N&GID"), sort_ngid },
74 { N_("N&UID"), sort_nuid },
75 { N_("&Owner"), sort_owner },
76 { N_("&Group"), sort_group }
79 #define string_sortcomp(a,b) (case_sensitive ? strcmp (a,b) : g_strcasecmp (a,b))
81 int
82 unsorted (const file_entry *a, const file_entry *b)
84 return 0;
87 int
88 sort_name (const file_entry *a, const file_entry *b)
90 int ad = MY_ISDIR (a);
91 int bd = MY_ISDIR (b);
93 if (ad == bd || mix_all_files)
94 return string_sortcomp (a->fname, b->fname) * reverse;
95 return bd-ad;
98 int
99 sort_ext (const file_entry *a, const file_entry *b)
101 char *exta, *extb;
102 int r;
103 int ad = MY_ISDIR (a);
104 int bd = MY_ISDIR (b);
106 if (ad == bd || mix_all_files){
107 exta = extension (a->fname);
108 extb = extension (b->fname);
109 r = string_sortcomp (exta, extb);
110 if (r)
111 return r * reverse;
112 else
113 return sort_name (a, b);
114 } else
115 return bd-ad;
119 sort_owner (const file_entry *a, const file_entry *b)
121 int ad = MY_ISDIR (a);
122 int bd = MY_ISDIR (b);
124 if (ad == bd || mix_all_files)
125 return string_sortcomp (get_owner (a->buf.st_uid), get_owner (a->buf.st_uid)) * reverse;
126 return bd-ad;
130 sort_group (const file_entry *a, const file_entry *b)
132 int ad = MY_ISDIR (a);
133 int bd = MY_ISDIR (b);
135 if (ad == bd || mix_all_files)
136 return string_sortcomp (get_group (a->buf.st_gid), get_group (a->buf.st_gid)) * reverse;
137 return bd-ad;
141 sort_time (const file_entry *a, const file_entry *b)
143 int ad = MY_ISDIR (a);
144 int bd = MY_ISDIR (b);
146 if (ad == bd || mix_all_files)
147 return (a->buf.st_mtime - b->buf.st_mtime) * reverse;
148 else
149 return bd-ad;
153 sort_ctime (const file_entry *a, const file_entry *b)
155 int ad = MY_ISDIR (a);
156 int bd = MY_ISDIR (b);
158 if (ad == bd || mix_all_files)
159 return (a->buf.st_ctime - b->buf.st_ctime) * reverse;
160 else
161 return bd-ad;
165 sort_atime (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 return (a->buf.st_atime - b->buf.st_atime) * reverse;
172 else
173 return bd-ad;
177 sort_inode (const file_entry *a, const file_entry *b)
179 int ad = MY_ISDIR (a);
180 int bd = MY_ISDIR (b);
182 if (ad == bd || mix_all_files)
183 return (a->buf.st_ino - b->buf.st_ino) * reverse;
184 else
185 return bd-ad;
189 sort_size (const file_entry *a, const file_entry *b)
191 int ad = MY_ISDIR (a);
192 int bd = MY_ISDIR (b);
194 if (ad == bd || mix_all_files)
195 return (b->buf.st_size - a->buf.st_size) * reverse;
196 else
197 return bd-ad;
201 sort_links (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 (b->buf.st_nlink - a->buf.st_nlink) * reverse;
208 else
209 return bd-ad;
213 sort_ngid (const file_entry *a, const file_entry *b)
215 int ad = MY_ISDIR (a);
216 int bd = MY_ISDIR (b);
218 if (ad == bd || mix_all_files)
219 return (b->buf.st_gid - a->buf.st_gid) * reverse;
220 else
221 return bd-ad;
225 sort_nuid (const file_entry *a, const file_entry *b)
227 int ad = MY_ISDIR (a);
228 int bd = MY_ISDIR (b);
230 if (ad == bd || mix_all_files)
231 return (b->buf.st_uid - a->buf.st_uid) * reverse;
232 else
233 return bd-ad;
236 inline static int
237 file_type_to_num (const file_entry *fe)
239 const struct stat *s = &fe->buf;
241 if (S_ISDIR (s->st_mode))
242 return 0;
243 if (S_ISLNK (s->st_mode)){
244 if (fe->f.link_to_dir)
245 return 1;
246 if (fe->f.stalled_link)
247 return 2;
248 else
249 return 3;
251 if (S_ISSOCK (s->st_mode))
252 return 4;
253 if (S_ISCHR (s->st_mode))
254 return 5;
255 if (S_ISBLK (s->st_mode))
256 return 6;
257 if (S_ISFIFO (s->st_mode))
258 return 7;
259 if (is_exe (s->st_mode))
260 return 8;
261 return 9;
265 sort_type (const file_entry *a, const file_entry *b)
267 int aa = file_type_to_num (a);
268 int bb = file_type_to_num (b);
270 return bb-aa;
274 void
275 do_sort (dir_list *list, sortfn *sort, int top, int reverse_f, int case_sensitive_f)
277 int i;
278 file_entry tmp_fe;
280 for (i = 0; i < top + 1; i++) { /* put ".." first in list */
281 if (!strcmp (list->list [i].fname, "..")) {
282 if (i > 0) { /* swap [i] and [0] */
283 memcpy (&tmp_fe, &(list->list [0]), sizeof (file_entry));
284 memcpy (&(list->list [0]), &(list->list [i]), sizeof (file_entry));
285 memcpy (&(list->list [i]), &tmp_fe, sizeof (file_entry));
287 break;
291 reverse = reverse_f ? -1 : 1;
292 case_sensitive = case_sensitive_f;
293 qsort (&(list->list) [1], top, sizeof (file_entry), sort);
296 void
297 clean_dir (dir_list *list, int count)
299 int i;
301 for (i = 0; i < count; i++){
302 g_free (list->list [i].fname);
303 list->list [i].fname = 0;
307 static int
308 add_dotdot_to_list (dir_list *list, int index)
310 char buffer [MC_MAXPATHLEN + MC_MAXPATHLEN];
311 char *p;
312 int i = 0;
314 /* Need to grow the *list? */
315 if (index == list->size) {
316 list->list = g_realloc (list->list, sizeof (file_entry) *
317 (list->size + RESIZE_STEPS));
318 if (!list->list)
319 return 0;
320 list->size += RESIZE_STEPS;
323 (list->list) [index].fnamelen = 2;
324 (list->list) [index].fname = g_strdup ("..");
325 (list->list) [index].f.link_to_dir = 0;
326 (list->list) [index].f.stalled_link = 0;
327 (list->list) [index].f.dir_size_computed = 0;
329 /* FIXME: We need to get the panel definition! to use file_mark */
330 (list->list) [index].f.marked = 0;
331 mc_get_current_wd (buffer, sizeof (buffer) - 1 );
332 if (buffer [strlen (buffer) - 1] == PATH_SEP)
333 buffer [strlen (buffer) - 1] = 0;
334 for (;;) {
335 strcat (buffer, PATH_SEP_STR "..");
336 p = vfs_canon (buffer);
337 if (mc_stat (p, &((list->list) [index].buf)) != -1){
338 g_free (p);
339 break;
341 i = 1;
342 if (!strcmp (p, PATH_SEP_STR)){
343 g_free (p);
344 return 1;
346 strcpy (buffer, p);
347 g_free (p);
350 /* Commented out to preserve a usable '..'. What's the purpose of this
351 * three lines? (Norbert) */
352 #if 0
353 if (i) { /* So there is bogus information on the .. directory's stat */
354 (list->list) [index].buf.st_mode &= ~0444;
356 #endif
357 return 1;
360 /* Used to set up a directory list when there is no access to a directory */
362 set_zero_dir (dir_list *list)
364 return (add_dotdot_to_list (list, 0));
367 /* If you change handle_dirent then check also handle_path. */
368 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
370 handle_dirent (dir_list *list, char *filter, struct dirent *dp,
371 struct stat *buf1, int next_free, int *link_to_dir,
372 int *stalled_link)
374 if (dp->d_name [0] == '.' && dp->d_name [1] == 0)
375 return 0;
376 if (!show_dot_files){
377 if (dp->d_name [0] == '.'){
378 if (!(dp->d_name [1] == 0))
379 if (!(dp->d_name [1] == '.' && NLENGTH (dp) == 2))
380 return 0;
383 if (!show_backups && dp->d_name [NLENGTH (dp)-1] == '~')
384 return 0;
385 if (mc_lstat (dp->d_name, buf1) == -1) {
386 message(1, _(" Error "), _("File exists but can not be stat-ed: %s %s"), dp->d_name, strerror(errno));
387 return 0;
390 if (S_ISDIR (buf1->st_mode))
391 tree_store_mark_checked (dp->d_name);
393 /* A link to a file or a directory? */
394 *link_to_dir = 0;
395 *stalled_link = 0;
396 if (S_ISLNK(buf1->st_mode)){
397 struct stat buf2;
398 if (!mc_stat (dp->d_name, &buf2))
399 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
400 else
401 *stalled_link = 1;
403 if (!(S_ISDIR(buf1->st_mode) || *link_to_dir) && filter &&
404 !regexp_match (filter, dp->d_name, match_file))
405 return 0;
407 /* Need to grow the *list? */
408 if (next_free == list->size){
409 list->list = g_realloc (list->list, sizeof (file_entry) *
410 (list->size + RESIZE_STEPS));
411 if (!list->list)
412 return -1;
413 list->size += RESIZE_STEPS;
415 return 1;
418 /* handle_path is a simplified handle_dirent. The difference is that
419 handle_path doesn't pay attention to show_dot_files and show_backups.
420 Moreover handle_path can't be used with a filemask.
421 If you change handle_path then check also handle_dirent. */
422 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
424 handle_path (dir_list *list, char *path,
425 struct stat *buf1, int next_free, int *link_to_dir,
426 int *stalled_link)
428 if (path [0] == '.' && path [1] == 0)
429 return 0;
430 if (mc_lstat (path, buf1) == -1)
431 return 0;
433 if (S_ISDIR (buf1->st_mode))
434 tree_store_mark_checked (path);
436 /* A link to a file or a directory? */
437 *link_to_dir = 0;
438 *stalled_link = 0;
439 if (S_ISLNK(buf1->st_mode)){
440 struct stat buf2;
441 if (!mc_stat (path, &buf2))
442 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
443 else
444 *stalled_link = 1;
447 /* Need to grow the *list? */
448 if (next_free == list->size){
449 list->list = g_realloc (list->list, sizeof (file_entry) *
450 (list->size + RESIZE_STEPS));
451 if (!list->list)
452 return -1;
453 list->size += RESIZE_STEPS;
455 return 1;
459 do_load_dir (dir_list *list, sortfn *sort, int reverse, int case_sensitive, char *filter)
461 DIR *dirp;
462 struct dirent *dp;
463 int status, link_to_dir, stalled_link;
464 int next_free = 0;
465 struct stat buf;
466 int dotdot_found = 0;
468 tree_store_start_check_cwd ();
470 dirp = mc_opendir (".");
471 if (!dirp){
472 tree_store_end_check ();
473 return set_zero_dir (list);
475 for (dp = mc_readdir (dirp); dp; dp = mc_readdir (dirp)){
476 status = handle_dirent (list, filter, dp, &buf, next_free, &link_to_dir,
477 &stalled_link);
478 if (status == 0)
479 continue;
480 if (status == -1){
481 tree_store_end_check ();
482 return next_free;
484 list->list [next_free].fnamelen = NLENGTH (dp);
485 list->list [next_free].fname = g_strdup (dp->d_name);
486 list->list [next_free].f.marked = 0;
487 list->list [next_free].f.link_to_dir = link_to_dir;
488 list->list [next_free].f.stalled_link = stalled_link;
489 list->list [next_free].f.dir_size_computed = 0;
490 list->list [next_free].buf = buf;
491 if (strcmp (dp->d_name, ".." ) == 0)
492 dotdot_found = 1;
493 next_free++;
494 if (!(next_free % 32))
495 rotate_dash ();
498 if (next_free) {
499 if (!dotdot_found)
500 add_dotdot_to_list (list, next_free++);
501 do_sort (list, sort, next_free-1, reverse, case_sensitive);
502 } else {
503 tree_store_end_check ();
504 return set_zero_dir (list);
507 mc_closedir (dirp);
508 tree_store_end_check ();
509 return next_free;
513 link_isdir (file_entry *file)
515 if (file->f.link_to_dir)
516 return 1;
517 else
518 return 0;
522 if_link_is_exe (char *full_name, file_entry *file)
524 struct stat b;
526 if (S_ISLNK (file->buf.st_mode)) {
527 mc_stat (full_name, &b);
528 return is_exe (b.st_mode);
529 } else
530 return 1;
533 static dir_list dir_copy = { 0, 0 };
535 static void
536 alloc_dir_copy (int size)
538 int i;
540 if (dir_copy.size < size){
541 if (dir_copy.list){
543 for (i = 0; i < dir_copy.size; i++) {
544 if (dir_copy.list [i].fname)
545 g_free (dir_copy.list [i].fname);
547 g_free (dir_copy.list);
548 dir_copy.list = 0;
551 dir_copy.list = g_new (file_entry, size);
552 for (i = 0; i < size; i++)
553 dir_copy.list [i].fname = 0;
555 dir_copy.size = size;
559 /* If filter is null, then it is a match */
561 do_reload_dir (dir_list *list, sortfn *sort, int count, int rev,
562 int case_sensitive, char *filter)
564 DIR *dirp;
565 struct dirent *dp;
566 int next_free = 0;
567 int i, found, status, link_to_dir, stalled_link;
568 struct stat buf;
569 int tmp_len; /* For optimisation */
570 int dotdot_found = 0;
572 tree_store_start_check_cwd ();
573 dirp = mc_opendir (".");
574 if (!dirp) {
575 clean_dir (list, count);
576 tree_store_end_check ();
577 return set_zero_dir (list);
580 alloc_dir_copy (list->size);
581 for (i = 0; i < count; i++){
582 dir_copy.list [i].fnamelen = list->list [i].fnamelen;
583 dir_copy.list [i].fname = list->list [i].fname;
584 dir_copy.list [i].f.marked = list->list [i].f.marked;
585 dir_copy.list [i].f.dir_size_computed = list->list [i].f.dir_size_computed;
586 dir_copy.list [i].f.link_to_dir = list->list [i].f.link_to_dir;
587 dir_copy.list [i].f.stalled_link = list->list [i].f.stalled_link;
590 for (dp = mc_readdir (dirp); dp; dp = mc_readdir (dirp)){
591 status = handle_dirent (list, filter, dp, &buf, next_free, &link_to_dir,
592 &stalled_link);
593 if (status == 0)
594 continue;
595 if (status == -1) {
596 mc_closedir (dirp);
597 /* Norbert (Feb 12, 1997):
598 Just in case someone finds this memory leak:
599 -1 means big trouble (at the moment no memory left),
600 I don't bother with further cleanup because if one gets to
601 this point he will have more problems than a few memory
602 leaks and because one 'clean_dir' would not be enough (and
603 because I don't want to spent the time to make it working,
604 IMHO it's not worthwhile).
605 clean_dir (&dir_copy, count);
607 tree_store_end_check ();
608 return next_free;
611 tmp_len = NLENGTH (dp);
612 for (found = i = 0; i < count; i++)
613 if (tmp_len == dir_copy.list [i].fnamelen
614 && !strcmp (dp->d_name, dir_copy.list [i].fname)){
615 list->list [next_free].f.marked = dir_copy.list [i].f.marked;
616 found = 1;
617 break;
620 if (!found)
621 list->list [next_free].f.marked = 0;
623 list->list [next_free].fnamelen = tmp_len;
624 list->list [next_free].fname = g_strdup (dp->d_name);
625 list->list [next_free].f.link_to_dir = link_to_dir;
626 list->list [next_free].f.stalled_link = stalled_link;
627 list->list [next_free].f.dir_size_computed = 0;
628 list->list [next_free].buf = buf;
629 if (strcmp (dp->d_name, ".." ) == 0)
630 dotdot_found = 1;
631 next_free++;
632 if (!(next_free % 16))
633 rotate_dash ();
635 mc_closedir (dirp);
636 tree_store_end_check ();
637 if (next_free) {
638 if (!dotdot_found)
639 add_dotdot_to_list (list, next_free++);
640 do_sort (list, sort, next_free-1, rev, case_sensitive);
642 else
643 next_free = set_zero_dir (list);
644 clean_dir (&dir_copy, count);
645 return next_free;
648 char *
649 sort_type_to_name (sortfn *sort_fn)
651 int i;
653 for (i = 0; i < SORT_TYPES; i++)
654 if ((sortfn *) (sort_orders [i].sort_fn) == sort_fn)
655 return _(sort_orders [i].sort_name);
657 return _("Unknown");
660 sortfn *
661 sort_name_to_type (char *sname)
663 int i;
665 for (i = 0; i < SORT_TYPES; i++)
666 if ( g_strcasecmp (sort_orders [i].sort_name, sname) == 0)
667 return (sortfn *) sort_orders [i].sort_fn;
669 /* default case */
670 return (sortfn *) sort_name;