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. */
19 #define DIR_H_INCLUDE_HANDLE_DIRENT
31 #include "../vfs/vfs.h"
36 * If true show files starting with a dot.
38 * In GNOME we default to not showing them.
41 int show_dot_files
= 0;
43 int show_dot_files
= 1;
46 /* If true show files ending in ~ */
49 /* If false then directories are shown separately from files */
50 int mix_all_files
= 0;
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
},
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))
82 unsorted (const file_entry
*a
, const file_entry
*b
)
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
;
99 sort_ext (const file_entry
*a
, const file_entry
*b
)
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
);
113 return sort_name (a
, b
);
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
237 file_type_to_num (const file_entry
*fe
)
239 const struct stat
*s
= &fe
->buf
;
241 if (S_ISDIR (s
->st_mode
))
243 if (S_ISLNK (s
->st_mode
)){
244 if (fe
->f
.link_to_dir
)
246 if (fe
->f
.stalled_link
)
251 if (S_ISSOCK (s
->st_mode
))
253 if (S_ISCHR (s
->st_mode
))
255 if (S_ISBLK (s
->st_mode
))
257 if (S_ISFIFO (s
->st_mode
))
259 if (is_exe (s
->st_mode
))
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
);
275 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
)
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
));
291 reverse
= reverse_f
? -1 : 1;
292 case_sensitive
= case_sensitive_f
;
293 qsort (&(list
->list
) [1], top
, sizeof (file_entry
), sort
);
297 clean_dir (dir_list
*list
, int count
)
301 for (i
= 0; i
< count
; i
++){
302 g_free (list
->list
[i
].fname
);
303 list
->list
[i
].fname
= 0;
308 add_dotdot_to_list (dir_list
*list
, int index
)
310 char buffer
[MC_MAXPATHLEN
+ MC_MAXPATHLEN
];
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
));
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;
335 strcat (buffer
, PATH_SEP_STR
"..");
336 p
= vfs_canon (buffer
);
337 if (mc_stat (p
, &((list
->list
) [index
].buf
)) != -1){
342 if (!strcmp (p
, PATH_SEP_STR
)){
350 /* Commented out to preserve a usable '..'. What's the purpose of this
351 * three lines? (Norbert) */
353 if (i
) { /* So there is bogus information on the .. directory's stat */
354 (list
->list
) [index
].buf
.st_mode
&= ~0444;
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
,
374 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 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))
383 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
)-1] == '~')
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
));
390 if (S_ISDIR (buf1
->st_mode
))
391 tree_store_mark_checked (dp
->d_name
);
393 /* A link to a file or a directory? */
396 if (S_ISLNK(buf1
->st_mode
)){
398 if (!mc_stat (dp
->d_name
, &buf2
))
399 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
403 if (!(S_ISDIR(buf1
->st_mode
) || *link_to_dir
) && filter
&&
404 !regexp_match (filter
, dp
->d_name
, match_file
))
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
));
413 list
->size
+= RESIZE_STEPS
;
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
,
428 if (path
[0] == '.' && path
[1] == 0)
430 if (mc_lstat (path
, buf1
) == -1)
433 if (S_ISDIR (buf1
->st_mode
))
434 tree_store_mark_checked (path
);
436 /* A link to a file or a directory? */
439 if (S_ISLNK(buf1
->st_mode
)){
441 if (!mc_stat (path
, &buf2
))
442 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
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
));
453 list
->size
+= RESIZE_STEPS
;
459 do_load_dir (dir_list
*list
, sortfn
*sort
, int reverse
, int case_sensitive
, char *filter
)
463 int status
, link_to_dir
, stalled_link
;
466 int dotdot_found
= 0;
468 tree_store_start_check_cwd ();
470 dirp
= mc_opendir (".");
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
,
481 tree_store_end_check ();
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)
494 if (!(next_free
% 32))
500 add_dotdot_to_list (list
, next_free
++);
501 do_sort (list
, sort
, next_free
-1, reverse
, case_sensitive
);
503 tree_store_end_check ();
504 return set_zero_dir (list
);
508 tree_store_end_check ();
513 link_isdir (file_entry
*file
)
515 if (file
->f
.link_to_dir
)
522 if_link_is_exe (char *full_name
, file_entry
*file
)
526 if (S_ISLNK (file
->buf
.st_mode
)) {
527 mc_stat (full_name
, &b
);
528 return is_exe (b
.st_mode
);
533 static dir_list dir_copy
= { 0, 0 };
536 alloc_dir_copy (int size
)
540 if (dir_copy
.size
< size
){
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
);
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
)
567 int i
, found
, status
, link_to_dir
, stalled_link
;
569 int tmp_len
; /* For optimisation */
570 int dotdot_found
= 0;
572 tree_store_start_check_cwd ();
573 dirp
= mc_opendir (".");
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
,
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 ();
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
;
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)
632 if (!(next_free
% 16))
636 tree_store_end_check ();
639 add_dotdot_to_list (list
, next_free
++);
640 do_sort (list
, sort
, next_free
-1, rev
, case_sensitive
);
643 next_free
= set_zero_dir (list
);
644 clean_dir (&dir_copy
, count
);
649 sort_type_to_name (sortfn
*sort_fn
)
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
);
661 sort_name_to_type (char *sname
)
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
;
670 return (sortfn
*) sort_name
;