-
Notifications
You must be signed in to change notification settings - Fork 4
/
dir.c
3796 lines (2934 loc) · 79.9 KB
/
dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Teradesk. Copyright (c) 1993 - 2002 W. Klaren,
* 2002 - 2003 H. Robbers,
* 2003 - 2017 Dj. Vukovic
*
* This file is part of Teradesk.
*
* Teradesk is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Teradesk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Teradesk; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
*/
#include <library.h>
#include <xdialog.h>
#include <xscncode.h>
#include "resource.h"
#include "desk.h"
#include "stringf.h" /* get rid of stream io */
#include "xfilesys.h"
#include "font.h"
#include "config.h"
#include "window.h"
#include "copy.h"
#include "dir.h"
#include "error.h"
#include "events.h"
#include "file.h"
#include "lists.h"
#include "slider.h"
#include "filetype.h"
#include "icon.h"
#include "screen.h"
#include "showinfo.h"
#include "icontype.h"
#include "open.h"
#include "va.h"
#include "prgtype.h"
#define TOFFSET 2
#define MAXLENGTH 128 /* Maximum length of a filename to display in a window. HR: 128 */
#define MINLENGTH 12 /* Minimum length of a filename to display in a window. */
#define DRAGLENGTH 16 /* Length of a name rectangle drawn while dragging items (was 12 earlier) */
/* Beware that SIZELEN must be in accordance with DISP_KBMB in RESOURCE.H */
#define SIZELEN 8 /* 1 + length of field for file size, i.e. for max. 9999999 ~10 MB, otherwise add sufix K or M */
XDFONT dir_font;
WINFO dirwindows[MAXWINDOWS];
GRECT dmax;
bool clearline = TRUE;
static _WORD dir_find(WINDOW *w, _WORD x, _WORD y);
static bool dir_state(WINDOW *w, _WORD item);
static ITMTYPE dir_itmtype(WINDOW *w, _WORD item);
static ITMTYPE dir_tgttype(WINDOW *w, _WORD item);
static const char *dir_itmname(WINDOW *w, _WORD item);
static char *dir_fullname(WINDOW *w, _WORD item);
static _WORD dir_attrib(WINDOW *w, _WORD item, _WORD mode, XATTR *attrib);
static bool dir_islink(WINDOW *w, _WORD item);
static bool dir_open(WINDOW *w, _WORD item, _WORD kstate);
static bool dir_copy(WINDOW *dw, _WORD dobject, WINDOW *sw, _WORD n, _WORD *list, ICND *icns, _WORD x, _WORD y,
_WORD kstate);
static void dir_select(WINDOW *w, _WORD selected, _WORD mode, bool draw);
static void dir_rselect(WINDOW *w, _WORD x, _WORD y);
static ICND *dir_xlist(WINDOW *w, _WORD *nselected, _WORD *nvisible, _WORD **sel_list, _WORD mx, _WORD my);
static _WORD *dir_list(WINDOW *w, _WORD *n);
static void dir_set_update(WINDOW *w, wd_upd_type type, const char *fname1, const char *fname2);
static void dir_do_update(WINDOW *w);
static void dir_showfirst(DIR_WINDOW *dw, _WORD i);
static char *sizestr(char *tstr, long size);
static ITMFUNC itm_func = {
dir_find, /* itm_find */
dir_state, /* itm_state */
dir_itmtype, /* itm_type */
dir_tgttype, /* target item type */
dir_itmname, /* itm_name */
dir_fullname, /* itm_fullname */
dir_attrib, /* itm_attrib */
dir_islink, /* itm_islink */
dir_open, /* itm_open */
dir_copy, /* itm_copy */
dir_select, /* itm_select */
dir_rselect, /* itm_rselect */
dir_xlist, /* itm_xlist */
dir_list, /* itm_list */
wd_path, /* wd_path */
dir_set_update, /* wd_set_update */
dir_do_update, /* wd_do_update */
dir_seticons /* wd_seticons */
};
ITMFUNC *dir_func = &itm_func; /* needed for wd_do_dirs() */
/********************************************************************
* *
* Funkties voor het sorteren van directories. *
* *
********************************************************************/
/*
* Sort by visibility
*/
static int _s_visible(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
if (e1->visible && !e2->visible)
return -1;
if (e2->visible && !e1->visible)
return 1;
return 0;
}
/*
* Sort filename strings case sensitive or insensitive,
* depending on the flag in options.sort
*/
static int strfncmp(const char *s1, const char *s2)
{
#if _MINT_
if (options.sort & WD_NOCASE)
{
return stricmp(s1, s2);
}
#endif
return strcmp(s1, s2);
}
/*
* Sort folders before files.
* Link target type has to be taken into account in order
* to sort links to folders properly among folders
*/
static int _s_folder(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
bool e1dir;
bool e2dir;
#if _MINT_
e1dir = ((e1->attrib.attrib & FA_DIR) || (e1->tgt_type == ITM_FOLDER));
e2dir = ((e2->attrib.attrib & FA_DIR) || (e2->tgt_type == ITM_FOLDER));
#else
e1dir = (e1->attrib.attrib & FA_DIR);
e2dir = (e2->attrib.attrib & FA_DIR);
#endif
if ((h = _s_visible(ee1, ee2)) != 0)
return h;
if (e1dir && (!e2dir || (strfncmp(e1->name, prevdir) == 0)))
return -1;
if (e2dir && (!e1dir || (strfncmp(e2->name, prevdir) == 0)))
return 1;
return 0;
}
/*
* Function for sorting a directory by filename.
* This also sorts folders before files
*/
static int sortname(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
if ((h = _s_folder(ee1, ee2)) != 0)
return h;
return strfncmp(e1->name, e2->name);
}
/*
* Function for sorting a directory by filename extension.
* This also sorts folders before files
*/
static int sortext(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
char *ext1;
char *ext2;
if ((h = _s_folder(ee1, ee2)) != 0)
return h;
ext1 = strchr(e1->name, '.');
ext2 = strchr(e2->name, '.');
if ((ext1 != NULL) || (ext2 != NULL))
{
if (ext1 == NULL)
return -1;
if (ext2 == NULL)
return 1;
if ((h = strfncmp(ext1, ext2)) != 0)
return h;
}
return strfncmp(e1->name, e2->name);
}
/*
* Function for sorting a directory by file size.
* This also sorts folders before files
*/
static int sortlength(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
if ((h = _s_folder(ee1, ee2)) != 0)
return h;
if (e1->attrib.size > e2->attrib.size)
return 1;
if (e2->attrib.size > e1->attrib.size)
return -1;
return strfncmp(e1->name, e2->name);
}
/*
* Function for sorting a directory by object date & time.
* This also sorts folders before files
*/
static int sortdate(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
if ((h = _s_folder(ee1, ee2)) != 0)
return h;
if (e1->attrib.mdate < e2->attrib.mdate)
return 1;
if (e2->attrib.mdate < e1->attrib.mdate)
return -1;
if (e1->attrib.mtime < e2->attrib.mtime)
return 1;
if (e2->attrib.mtime < e1->attrib.mtime)
return -1;
return strfncmp(e1->name, e2->name);
}
/*
* Function for no-sorting :) of directory
*/
static int unsorted(const void *ee1, const void *ee2)
{
const NDTA *e1 = *(const NDTA *const *) ee1;
const NDTA *e2 = *(const NDTA *const *) ee2;
int h;
if ((h = _s_visible(ee1, ee2)) != 0)
return h;
if (e1->index > e2->index)
return 1;
return -1;
}
/*
* Function for reversing the order of sorted items.
* This is to be applied -after- a sorting of any kind if reverse is wanted.
*/
static void revord(NDTA **buffer, _WORD n)
{
NDTA **b1 = &buffer[0], **b2 = &buffer[n - 1], *s;
_WORD i, k = n / 2;
for (i = 0; i < k; i++)
{
s = *b1;
*b1++ = *b2;
*b2-- = s;
}
}
/*
* Function for sorting of a directory by desired key (no drawing done here)
* Function indices in the array of pointers are WD_SORT_NAME, WD_SORT_EXT,
* WD_SORT_DATE, WD_SORT_LENGTH, WD_NOSORT. This is shorter than the earlier
* version that used a switch.
*/
static void sort_directory(DIR_WINDOW *w)
{
static int (*const sortproc[5]) (const void *, const void *) = { sortname, sortext, sortdate, sortlength, unsorted };
/* Perform the actual sorting (in fact pointers only are sorted) */
qsort(w->buffer, w->nfiles, sizeof(NDTA *), sortproc[options.sort & 0x000F]);
/* Reverse the order, if so selected */
if (options.sort & WD_REVSORT)
revord(w->buffer, w->nvisible);
}
/*
* Sort a directory by desired key, then update the window
*/
void dir_sort(WINDOW *w)
{
DIR_WINDOW *dw = (DIR_WINDOW *)w;
sort_directory(dw);
wd_type_draw((TYP_WINDOW *) w, FALSE);
}
/********************************************************************
* *
* Funkties voor het laden van directories in een window. *
* *
********************************************************************/
/*
* Detect whether a file is to be considered s executable.
* First it is examined whether its filetype is in the list
* of executable filetypes. Then, it is examined whether execute
* rights are set (this is done only for names without extensions,
* i.e. those that do not contain a period).
* Parameter 'name' is the name proper, not a fullname.
*/
bool dir_isexec(const char *name, XATTR *attr)
{
return (((attr->st_mode & S_IFMT) == S_IFREG) && (prg_isprogram(name)
#if _MINT_
|| (mint && (strchr(name, '.') == NULL)
&& ((attr->st_mode & EXEC_MODE) != 0))
#endif
));
}
/*
* Zet de inforegel van een window. Als n = 0 is dit het aantal
* bytes en files, als n ongelijk is aan 0 is dit de tekst
* 'n items selected'
*/
void dir_info(DIR_WINDOW *w)
{
_WORD l, n, m;
long total;
LSUM bytes;
#if _MINT_
LNAME mask = { 0 };
#else
SNAME mask = { 0 };
#endif
char *p;
if (autoloc)
{
strcpy(mask, " (");
strcat(mask, automask);
strcat(mask, ")");
}
if (w->nselected > 0)
{
m = MSITEMS;
bytes = w->selbytes;
n = w->nselected;
} else
{
m = MITEMS;
bytes = w->visbytes;
n = w->nvisible;
}
size_sum(&total, &bytes);
p = fmt_size(total, &l);
sprintf(w->info, get_freestring(m), mask, p, n, get_freestring((n == 1) ? MISINGUL : MIPLURAL));
wind_set_str(w->xw_handle, WF_INFO, w->info);
}
/*
* Release the buffer(s) occupied by directory data
*/
static void dir_free_buffer(DIR_WINDOW *w)
{
NDTA **b = w->buffer;
if (b)
{
long i;
for (i = 0; i < w->nfiles; i++)
free(b[i]); /* free NDTA + name */
free(b); /* free pointer array */
w->buffer = NULL;
}
}
/*
* Set directory items as visible (or otherwise),
* depending on selected attributes and filemask
*/
static void set_visible(DIR_WINDOW *w)
{
if (w->buffer != NULL)
{
long i;
LSUM v;
NDTA *b;
_WORD oa = options.attribs, n = 0;
v.bytes = 0;
v.kbytes = 0;
for (i = 0; i < w->nfiles; i++)
{
b = w->buffer[i];
b->selected = FALSE;
b->newstate = FALSE;
/* Note: by adding the line of code
* options "Show subfolders" and "show parent"
* became independent. This is new behaviour as of V4.08 */
if (((oa & FA_HIDDEN) != 0 /* permit hidden */
|| (b->attrib.attrib & FA_HIDDEN) == 0 /* or item is not hidden */
) && ((oa & FA_SYSTEM) != 0 /* permit system */
|| (b->attrib.attrib & FA_SYSTEM) == 0 /* or item is not system */
) && ((oa & FA_DIR) != 0 /* permit subdirectory */
|| (b->attrib.attrib & FA_DIR) == 0 /* or item is not subdirectory */
|| strcmp(b->name, prevdir) == 0 /*** or item is parent dir ***/
) && ((oa & FA_PARDIR) != 0 /* permit parent dir */
|| strcmp(b->name, prevdir) != 0 /* or item is not parent dir */
) && ((b->attrib.mode & S_IFMT) == S_IFDIR /* permit directory */
|| cmp_wildcard(b->name, w->fspec) /* or mask match */
))
{
if ((b->attrib.mode & S_IFMT) != S_IFDIR)
add_size(&v, b->attrib.size);
b->visible = TRUE;
n++;
} else
{
b->visible = FALSE;
}
}
w->nvisible = n;
w->visbytes = v;
}
}
/*
* Transform data from extended attributes
*/
static void xattr_to_fattr(XATTR *xattr, FATTR *fattr)
{
fattr->mode = xattr->st_mode;
fattr->size = xattr->st_size;
fattr->mtime = dos_mtime(xattr);
fattr->mdate = dos_mdate(xattr);
fattr->attrib = xattr->st_attr;
#if _MINT_
fattr->gid = xattr->st_gid;
fattr->uid = xattr->st_uid;
#endif
}
/*
* Funktie voor het copieren van een dta naar de buffer van een window.
* Parameters fulln and tgt are used only with links
*/
#if _MINT_
static _WORD copy_DTA(NDTA **dest, char *fulln, char *name, XATTR *src, XATTR *tgt, _WORD iindex, bool link) /* link */
#else
static _WORD copy_DTA(NDTA **dest, char *name, XATTR *src, _WORD iindex, bool link) /* link */
#endif
{
NDTA *new;
long l = strlen(name);
new = malloc(sizeof(NDTA) + l + 1); /* Allocate space for NDTA and name together */
if (new)
{
new->index = iindex;
new->link = link; /* handle links */
#if _MINT_
if (link)
{
char *tgtname = x_fllink(fulln);
new->item_type = ITM_FILE;
if ((tgt->st_mode & S_IFMT) == S_IFDIR)
{
new->tgt_type = (strcmp(fn_get_name(tgtname), prevdir) == 0) ? ITM_PREVDIR : ITM_FOLDER;
} else
{
/* Note: this may considerably slow down opening of windows */
if (x_netob(tgtname))
new->tgt_type = ITM_NETOB;
else if (dir_isexec(fn_get_name(tgtname), tgt))
new->tgt_type = ITM_PROGRAM;
else
new->tgt_type = ITM_FILE;
}
free(tgtname);
} else
#endif
{
if ((src->st_mode & S_IFMT) == S_IFDIR)
{
new->item_type = (strcmp(name, prevdir) == 0) ? ITM_PREVDIR : ITM_FOLDER;
} else
{
/* Note: This slows down opening of windows ! */
if (dir_isexec(name, src))
new->item_type = ITM_PROGRAM;
else
new->item_type = ITM_FILE;
}
new->tgt_type = new->item_type;
}
/* Convert file attributes */
xattr_to_fattr(src, &new->attrib);
/*
* Determine which icon is to be used for files in this window.
* Note: if there are many window icons assigned, this routine
* slows down window opening dramatically (because of the wildcard
* matching), therefore a modification below to do this only
* in icon mode (for a fast machine the "if" could be disabled...)
*/
if (options.mode)
new->icon = icnt_geticon(name, new->item_type, new->tgt_type);
new->name = new->alname; /* the pointer makes it possible to use NDTA in local name space (auto) */
strcpy(new->alname, name);
*dest = new;
return (_WORD) l;
}
*dest = NULL;
return ENOMEM;
}
/*
* Funktie voor het inlezen van een directory. Het resultaat is
* ongelijk nul als er een fout is opgetreden. De directory wordt
* gesorteerd.
*/
static _WORD read_dir(DIR_WINDOW *w)
{
XDIR *dir;
long bufsiz;
long memused = 0;
long n = 0;
LSUM length;
_WORD error = 0;
_WORD maxl = 0;
#if _MINT_
XATTR tgtattr;
#endif
XATTR attr;
length.kbytes = 0;
length.bytes = 0;
dir_free_buffer(w); /* clear and release old buffer, if it exists */
/* HR we only prepare a pointer array :-) */
bufsiz = (long) options.max_dir; /* that's how many pointers we allocate */
w->buffer = malloc(bufsiz * sizeof(NDTA *));
if (w->buffer != NULL)
{
/* Open the directory. This allocates space for "dir" */
dir = x_opendir(w->path, &error);
/* If successful, read directory entries in a loop */
if (dir)
{
#if _MINT_
w->fs_type = dir->type; /* Need to know the filesystem type elsewhere. */
#endif
while (error == 0)
{
char *name;
error = (_WORD) x_xreaddir(dir, &name, sizeof(VLNAME), &attr);
if (error == 0)
{
bool link = false;
char *fulln = NULL;
/*
* Note: number of items in a directory must currently
* be limited to max int16 value, because routines for
* selection of items return numbers of items as
* short integers!
*/
if (n >= SHRT_MAX)
{
alert_iprint(MDIRTBIG);
break;
}
/* Bufer space may have to be increased... */
if (n == bufsiz) /* don't exceed allocated amount */
{
/* Allocate a 50% bigger buffer, then copy contents */
void *newb;
/* HR 120803 expand pointer array mildly exponentially */
bufsiz = 3 * bufsiz / 2;
newb = malloc(bufsiz * sizeof(NDTA *));
if (newb)
{
/*
* Copy to new buffer, free old, point to new;
* this code is smaller than using realloc()
* but at execution may suffer from memory
* fragmentation
*/
memcpy(newb, w->buffer, n * sizeof(NDTA *));
free(w->buffer);
w->buffer = newb;
} else
{
/* Can't allocate so much memory */
alert_iprint(MDIRTBIG);
break;
}
}
/* n = bufsiz ? */
#if _MINT_
/* Handle links */
if ((attr.st_mode & S_IFMT) == S_IFLNK)
{
/*
* Why get target? So that items get sorted as they should be:
* links to folders will be sorted among folders, etc.
* Getting the attributes for network objects will
* fail, but it doesn't matter.
*/
fulln = fn_make_path(dir->path, name);
x_attr(0, w->fs_type, fulln, &tgtattr); /* follow the link to show attributes */
link = true;
} else
{
tgtattr = attr;
}
#endif
if (strcmp(".", name) != 0)
{
/*
* An area is allocated here for NDTA + name
* then the area is filled with data.
* This routine returns name length in 'error'.
*/
#if _MINT_
error = copy_DTA(w->buffer + n, fulln, name, &attr, &tgtattr, (_WORD) n, link);
#else
error = copy_DTA(w->buffer + n, name, &attr, (_WORD) n, link);
#endif
if (error >= 0)
{
add_size(&length, attr.st_size);
memused += error;
if (error > maxl)
maxl = error;
error = 0;
n++;
}
}
free(fulln);
}
}
x_closedir(dir);
}
if (error == ENMFILES || error == ENOENT)
error = 0;
} else
{
error = ENOMEM; /* Can't allocate poiners for the new directory */
}
/* Number of files, total size, maximum name length */
w->nfiles = (_WORD) n;
w->usedbytes = length;
w->namelength = maxl;
/* Is everything OK ? */
if (error != 0)
{
/* An error has occured, free memory used */
dir_free_buffer(w);
w->nfiles = 0;
w->nvisible = 0;
w->usedbytes.kbytes = 0;
w->usedbytes.bytes = 0;
w->visbytes.kbytes = 0;
w->visbytes.bytes = 0;
} else
{
/* Sort directory */
set_visible(w);
sort_directory(w);
}
return error;
}
/*
* Set information to be displayed in window title and info line.
* Also set window sliders
*/
static void dir_setinfo(DIR_WINDOW *w)
{
w->nselected = 0;
w->selbytes.kbytes = 0;
w->selbytes.bytes = 0;
w->refresh = FALSE;
calc_rc((TYP_WINDOW *) w, &(w->xw_work));
dir_info(w);
wd_type_title((TYP_WINDOW *) w);
}
/*
* Read a directory into a window buffer.
* Beware: this routine sets the mouse first to busy and
* then to arrow shape!
*/
static _WORD dir_readandset(DIR_WINDOW *w)
{
_WORD error;
hourglass_mouse();
error = read_dir(w);
if (error == 0)
dir_setinfo(w); /* line length is calculated here */
arrow_mouse();
return error;
}
/*
* Aux. function for the two routines further below. This routine
* also resets the fulled flags if the number of items or the length
* of the longest name has changed.
* Use wd_type_draw() after this function!
*/
void dir_reread(DIR_WINDOW *w)
{
_WORD error, oldn = w->nvisible, oldl = w->llength;
error = dir_readandset(w);
xform_error(error);
if (w->nvisible != oldn || w->llength != oldl)
wd_type_nofull((WINDOW *) w);
}
/*
* Unconditional update of a directory window
*/
void dir_refresh_wd(DIR_WINDOW *w)
{
dir_reread(w);
wd_type_draw((TYP_WINDOW *) w, TRUE);
wd_reset((WINDOW *) w);
}
/*
* Unconditionally update all directory windows.
* Don't reset flags
*/
void dir_refresh_all(void)
{
WINDOW *w = xw_first();
while (w)
{
if (w->xw_type == DIR_WIND)
{
dir_reread((DIR_WINDOW *) w);
wd_type_draw((TYP_WINDOW *) w, TRUE);
}
w = xw_next(w);
}
}
/*
* Remove a trailing backslash from a path,
* except if the path is that of a root directory
*/
void dir_trim_slash(char *path)
{
char *b = strrchr(path, '\\');
if (b && b[1] == '\0' && !isroot(path))
*b = '\0';
}
/*
* Refresh a directory window given by path, or else top that window
* If required directory is found, return TRUE
* action: DO_PATH_TOP = top, DO_PATH_UPDATE = update
*/
bool dir_do_path(char *path, _WORD action)
{
WINDOW *w;
const char *wpath;
w = xw_first();
while (w)
{
if (xw_type(w) == DIR_WIND && (wpath = wd_path(w)) != NULL && path && (strcmp(path, wpath) == 0))
{
if (action == DO_PATH_TOP)
wd_type_topped(w);
else
dir_refresh_wd((DIR_WINDOW *) w);
return TRUE;
}
w = xw_next(w);
}
return FALSE;
}
/*
* Funktie om te controleren of het pad van file fname gelijk is
* aan path.
*/
static bool cmp_path(const char *path, const char *fname)
{
const char *h;
long l;
if ((h = strrchr(fname, '\\')) == NULL)
return FALSE;
l = h - fname;
if (isroot(path))
l++;
if ((long)strlen(path) != l)
return FALSE;