-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcdisk.c
2205 lines (2036 loc) · 47.8 KB
/
gcdisk.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
/*
* gc3 - Gram's Commander v3.3: A configureable file manager
*
* YAGH! (tm) (Yet Another Gramish Hack)
*
* (c) 1994 by Graham Wheeler. All Rights Reserved.
*
* DISCLAIMER: The author is not responsible for any loss, damage,
* riots, earthquakes, catastrophes, marriages, annulments,
* etc that arise from the use of this program. Sorry...
*
* This software may be freely copied and distributed, provided it is
* distributed with all files intact and unmodified. You may use this
* software for a 30 day trial period, after which you should register
* your copy with me. I have spent a large number of weekends and nights
* trying to make a file manager for UNIX (and DOS) which combines ease
* of use and power, and complements the power of UNIX to process the
* contents of files with the power to *select* the files to be processed.
* By registering, you will encourage me to keep improving gc3, instead
* of seeking paying work which benefits only a few.
*
* Full details of how to register, and what you will receive if
* you do, are contained in the file LEGAL.DOC. Of course, if you
* don't register, I can't do much about it, but in that case
* may an obscure bug in gc3 erase all of your files!
*
* Enjoy using GC!
*
* See INSTALL.DOC for instructions on how to install GC3
*
* Version History
* ---------------
* v1.0 Nov 1992 Initial release
* v2.0 Nov 1992 Largely rewritten to be table-driven
* v2.1 Dec 1992 Support for non-ANSI C compilers contributed
* by George Sipe
* Support for old ioctls TCGETA and TCSETAW for
* those with no POSIX-style tcgetattr and tcsetattr.
* v2.2 Dec 1992 Termio ioctls removed.Hopefully much more
* portable. Command mode removed. A few bugs
* fixed. Placeholder replacement now done in
* a preprocessing phase before parsing commands.
* v2.3 Jan 1993 Split into a number of source files
* Basic port to MS-DOS
* v3.0 Feb/March '93 New script language; large parts rewritten
* v3.1 March '93 Ported back to UNIX and cleaned up
* v3.2b July '93 Many bugs fixed and features added
* v3.2 Oct '93 Viewer added, .gc3rul file handling
* added, bugs fixed, local variables and
* parameters, tree view and container handling.
* v3.3 April '94 Fixed lots of bugs. Large parts of compiler
* and interpreter are now table-driven. Changed
* screen repainting, and added support for colour
* and mono terminals. Added script-driven menus
* and forms, and hypertext help browser.
* Made windows moveable and resizeable.
* Made command handling in the script much more
* sophisticated with multiple prefix/suffixes.
* Improved command line history facility. Added
* repeat counts to most commands.
*/
#include "gcport.h"
#include "gc3.h"
#include "gcops.h"
#define GC_CURSES
#include "gclib.h"
/* Name of file holding old paths */
long freeSpace[2], usedSpace[2];
fInfo_t *fInfo[2]; /* Directory info for each panel */
char paths[2][MAXPATHNAME];
/* CONTAINER FILE RULES */
/* string fields */
#define R_NAME 0
#define R_TYPE 1
#define R_LIST 2
#define R_ADD 3
#define R_EXTRACT 4
#define R_DELETE 5
/* single digit fields */
#define R_FIELDS 0
#define R_ITEM 1
#define R_SIZE 2
/* field and offset pair fields */
#define R_YEAR 0
#define R_MONTH 1
#define R_DAY 2
#define R_HOUR 3
#define R_MIN 4
#define R_SEC 5
#define MAX_RULES 8
typedef struct
{
char *sf[R_DELETE + 1];
char sif[R_SIZE + 1];
char dif[R_SEC + 1][2];
} rule_t;
static rule_t Rules[MAX_RULES];
static int numRules = 0;
static char *oldSelNames[MAXARGS] =
{NULL};
static int totfiles[2] =
{0, 0};
/******************************************************************
FILE LEAK DEBUGGING
*******************************************************************/
#ifdef FILE_DEBUG
#include <fcntl.h>
static int fileOwners[40] =
{0};
int fileDebugID = 1;
#if __STDC__
FILE *gc_fopen(char *n, char *m, char *fn, int ln, int ID)
#else
FILE *gc_fopen(n, m, fn, ln, ID)
char *n, *m, *fn;
int ln, ID;
#endif
{
FILE *rtn = fopen(n, m);
if (rtn)
{
fprintf(stderr, "%2d (%s) fopened by %s line %d (ID %d)\n",
fileno(rtn), n, fn, ln, ID);
fileOwners[fileno(rtn)] = ID;
}
return rtn;
}
#if __STDC__
int gc_fclose(FILE * f, char *fn, int ln)
#else
int gc_fclose(f, fn, ln)
FILE *f;
char *fn;
int ln;
#endif
{
if (f)
{
fprintf(stderr, "%2d fclosed by %s line %d, ID %d\n",
fileno(f), fn, ln, fileOwners[fileno(f)]);
fileOwners[fileno(f)] = 0;
}
else
fprintf(stderr, " Illegal fclose(NULL) by %s line %d\n", fn, ln);
return f ? fclose(f) : 0;
}
#if __STDC__
int gc_open(char *n, int m, int a, char *fn, int ln, int ID)
#else
int gc_open(n, m, a, fn, ln, iD)
char *n, *fn, int m, a, int ln, ID;
#endif
{
int rtn = open(n, m, a);
if (rtn >= 0)
{
fprintf(stderr, "%2d (%s) opened by %s line %d, ID %d\n",
rtn, n, fn, ln, ID);
fileOwners[rtn] = ID;
}
return rtn;
}
#if __STDC__
int gc_close(int h, char *fn, int ln)
#else
int gc_close(h, fn, ln)
int h, ln;
char *fn;
#endif
{
if (h >= 0)
{
fprintf(stderr, "%2d closed by %s line %d (ID %d)\n",
h, fn, ln, fileOwners[h]);
fileOwners[h] = 0;
}
else
fprintf(stderr, " Illegal fclose(%d) by %s line %d\n",
h, fn, ln);
return (h >= 0) ? close(h) : 0;
}
#if __STDC__
int gc_dup(int h, char *fn, int ln, int ID)
#else
int gc_dup(h, fn, ln, ID)
int h, ln, ID;
char *fn;
#endif
{
int rtn = -1;
if (h >= 0)
rtn = dup(h);
if (rtn >= 0)
{
fprintf(stderr, "%2d duplicated to %d by %s line %d, old ID %d, new ID %d\n",
h, rtn, fn, ln, fileOwners[h], ID);
fileOwners[rtn] = ID;
}
else
fprintf(stderr, " dup(%d) by %s line %d failed! (old ID %d)\n",
h, fn, ln, fileOwners[h]);
return rtn;
}
#endif
#if __STDC__
void ShowOpenFiles(void)
#else
void ShowOpenFiles()
#endif
{
#ifdef FILE_DEBUG
int i;
for (i = 0; i < 40; i++)
if (fileOwners[i])
fprintf(stderr, "%d still open, ID %d\n", i, fileOwners[i]);
#endif
}
/***************************************************************
BUILD PERMISSIONS STRING FROM FILE MODE
****************************************************************/
#ifndef __MSDOS__
#if __STDC__
char *getPerms(mode_t tmd)
#else
char *getPerms(tmd)
mode_t tmd;
#endif
{
static char perms[14];
int i = 0;
perms[i++] = (tmd & S_ISUID) ? 's' : '-';
perms[i++] = (tmd & S_ISGID) ? 'S' : '-';
#ifdef S_ISVTX
perms[i++] = (tmd & S_ISVTX) ? 't' : '-';
#endif
#ifdef S_IRUSR
perms[i++] = (tmd & S_IRUSR) ? 'r' : '-';
perms[i++] = (tmd & S_IWUSR) ? 'w' : '-';
perms[i++] = (tmd & S_IXUSR) ? 'x' : '-';
perms[i++] = (tmd & S_IRGRP) ? 'r' : '-';
perms[i++] = (tmd & S_IWGRP) ? 'w' : '-';
perms[i++] = (tmd & S_IXGRP) ? 'x' : '-';
perms[i++] = (tmd & S_IROTH) ? 'r' : '-';
perms[i++] = (tmd & S_IWOTH) ? 'w' : '-';
perms[i++] = (tmd & S_IXOTH) ? 'x' : '-';
#else /* None-POSIX; owners permissions only */
perms[i++] = (tmd & S_IREAD) ? 'r' : '-';
perms[i++] = (tmd & S_IWRITE) ? 'w' : '-';
perms[i++] = (tmd & S_IEXEC) ? 'x' : '-';
#endif
perms[i++] = 0;
return perms;
}
#endif
/*****************************************************************
CHECK IF FILE IS OF A CERTAIN TYPE(S)
*******************************************************************/
#if __STDC__
int checkType(mode_t m, short f)
#else
int checkType(m, f)
mode_t m;
short f;
#endif
{
int rtn = 0;
if (m & 128)
{
}
#ifndef __MSDOS__
if (S_ISLNK(m) && (f & 64))
rtn = 1;
#endif
if (S_ISCHR(m) && (f & 4))
rtn = 1;
if (S_ISBLK(m) && (f & 8))
rtn = 1;
if (S_ISFIFO(m) && (f & 2))
rtn = 1;
if (S_ISDIR(m) && (f & 1))
rtn = 1;
if (S_ISREG(m))
{
if (f & 32)
#ifdef S_IXUSR
if ((m & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)
#else
if ((m & (S_IEXEC)) != 0)
#endif
rtn = 1;
if (f & 16)
rtn = 1;
}
return rtn;
}
/*********************************************************
DIRECTORY FILTERING
**********************************************************/
#if __STDC__
void refilter(int L, char *oldname)
#else
void refilter(L, oldname)
int L;
char *oldname;
#endif
{
int j = 0;
if (!isListWindow(L))
return;
if (filtering[L])
{
int i;
(*(compileRegexp)) (filters[L]);
for (i = 0; i < totfiles[L]; i++)
{
register char *n = fInfo[L][i].name;
if (IsDirectory(L,i) || ((*(matchRegexp)) (n)) == 1)
fIndex[L][j++] = i;
else
nselect(L, i, 0);
}
}
else for ( ; j < totfiles[L]; j++)
fIndex[L][j] = j;
numfiles[L] = j;
sortList(L, oldname);
normalise(L);
}
/**********************************************************
FIND A CONTAINER FILE RULE FOR A FILE
***********************************************************/
#if __STDC__
static int findRule(char *path)
#else
static int findRule(path)
char *path;
#endif
{
int rule, first = 1;
int b, p;
char *name = STRRCHR(path, PATH_SEP); /* get the basename in `name' */
if (name == NULL)
name = path;
if (numRules <= 0)
return -1;
b = GRABBUF();
p = GRABBUF();
for (rule = 0; rule < numRules; rule++)
{
/* If the name rule is present and fails, continue */
if (Rules[rule].sf[R_NAME])
if (!doesMatch(Rules[rule].sf[R_NAME], name, 0))
continue;
/* if there is a type rule, we run `file' if it hasn't
been done yet, and then apply the rule, continuing if
it fails */
if (Rules[rule].sf[R_TYPE])
{
if (first)
{
sprintf(BUF[b], "file %s", path);
runCommand(BUF[b], 1);
first = 0;
}
if (!doesMatch(Rules[rule].sf[R_TYPE], valspace[VAR_LINEBUFF], 0))
continue;
}
/* Both rules passed! exit the loop */
break;
}
if (rule >= numRules)
rule = -1; /* exited loop without finding a rule */
FREEBUF(p);
FREEBUF(b);
return rule;
}
/****************************************************************
SPLIT A PATH AND IDENTIFY THE FILE TYPE
****************************************************************/
/*
* This routine, given a path, splits the path into
* a part that corresponds to something that exists, and something
* (if anything) that doesn't. On entry `exist' and `nonexist'
* should point to a buffer big enough to hold the full absolute
* pathname + 2 bytes. The returned existing part is a normalised,
* absolute path.
*
* The return value is a file type code for the exist part, one of:
*
* n - existing file is a container; rule number n
* -3 - existing file is a directory
* -2 - existing file is a device or pipe
* -1 - other
*/
#if __STDC__
int splitPath(char *path, char *exist, char *nonexist)
#else
int splitPath(path, exist, nonexist)
char *path, *exist, *nonexist;
#endif
{
char *p;
strcpy(nonexist, path); /* Use nonexist as workspace */
fixPath(nonexist, exist);
*nonexist = '\0';
for (;;)
{
/* does it exist? */
if (access(exist, 0) == 0)
{
mode_t m = getFileMode(exist);
if (checkType(m, 1))
return -3;
else if (checkType(m, 2 | 4 | 8))
return -2;
else
return findRule(exist);
}
/* didn't exist - shunt up to parent */
p = STRRCHR(exist, PATH_SEP);
assert(p);
strcpy(nonexist, makePath(p + 1, nonexist));
#ifdef __MSDOS__
if (p <= (exist + 2))
#else
if (p <= exist)
#endif
{
*++p = '\0';
return -3; /* root directory */
}
*p = '\0';
}
}
/**********************************************************
ROUTINES TO GET INFO ABOUT FILES
***********************************************************/
#if __STDC__
static struct stat *getFileInfo(char *pathname)
#else
static struct stat *getFileInfo(pathname)
char *pathname;
#endif
{
static struct stat st;
#ifdef NO_SYMLINKS
if (stat(pathname, &st))
#else
int fl = testOption(VAR_FOLLOWLINKS);
if ((fl && stat(pathname, &st) != 0) ||
(!fl && lstat(pathname, &st) != 0))
#endif
{
int buf = GRABBUF();
sprintf(BUF[buf], "Cannot stat %s!", pathname);
showMsg(BUF[buf]);
FREEBUF(buf);
st.st_size = 0;
st.st_mode = 0;
st.st_uid = 0;
st.st_gid = 0;
st.st_mtime = 0;
st.st_atime = 0;
}
return &st;
}
#if __STDC__
off_t getFileSize(char *pathname)
#else
off_t getFileSize(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_size;
}
#if __STDC__
mode_t getFileMode(char *pathname)
#else
mode_t getFileMode(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_mode;
}
#if __STDC__
gid_t getFileGroup(char *pathname)
#else
gid_t getFileGroup(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_gid;
}
#if __STDC__
uid_t getFileUser(char *pathname)
#else
uid_t getFileUser(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_uid;
}
#if __STDC__
time_t getFileModTime(char *pathname)
#else
time_t getFileModTime(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_mtime;
}
#if __STDC__
time_t getFileAccTime(char *pathname)
#else
time_t getFileAccTime(pathname)
char *pathname;
#endif
{
return getFileInfo(pathname)->st_atime;
}
/***************************************************************
DATE FIXER NEEDED FOR BORLAND C
****************************************************************/
#ifdef __MSDOS__
static time_t normaliseDate(unsigned yr, unsigned mon, unsigned day,
unsigned hr, unsigned mins, unsigned sec)
{
struct date dt;
struct time tm;
tm.ti_hour = hr;
tm.ti_min = mins;
tm.ti_sec = sec;
dt.da_year = (yr < 100) ? (1900 + yr) : yr;
dt.da_mon = mon;
dt.da_day = day;
/* Borland's dostounix crashes on bad dates, so we do a reality check */
if (dt.da_year < 1980)
dt.da_year = 1980;
if (dt.da_year > 1999)
dt.da_year = 1999;
if (dt.da_mon < 1)
dt.da_mon = 1;
if (dt.da_mon > 12)
dt.da_mon = 12;
if (dt.da_day > 31)
dt.da_day = 31;
if (dt.da_day < 1)
dt.da_day = 1;
if (tm.ti_hour > 23)
tm.ti_min = 23;
if (tm.ti_min > 59)
tm.ti_min = 59;
if (tm.ti_sec > 59)
tm.ti_sec = 59;
return (time_t) dostounix(&dt, &tm);
}
#endif
/************************************************************
ADD AN ENTRY TO THE DIRECTORY LIST
*************************************************************/
#if __STDC__
#ifdef __MSDOS__
static int addEntry(int n, int i, char *fname,
int dpth, int dirOnly, struct ffblk * fdat)
#else
static int addEntry(char *pathname, int n, int i, char *fname,
int dpth, int dirOnly)
#endif
#else
static int addEntry(pathname, n, i, fname, dpth, dirOnly)
char *pathname, *fname;
int n, i, dpth, dirOnly;
#endif
{
fInfo_t *f = &fInfo[n][i];
#ifndef __MSDOS__
int fl = testOption(VAR_FOLLOWLINKS);
struct stat *st = getFileInfo(pathname);
{
register mode_t m = st->st_mode;
if (!fl && !dirOnly && S_ISLNK(m))
f->typID = 'l';
else if (S_ISDIR(m))
f->typID = PATH_SEP;
else if (dirOnly)
return 0;
else if (S_ISCHR(m))
f->typID = 'c';
else if (S_ISBLK(m))
f->typID = 'b';
else if (S_ISFIFO(m))
f->typID = 'p';
#ifdef S_IXUSR
else if (m & (S_IXUSR | S_IXGRP | S_IXOTH))
f->typID = '*';
#else
else if (m & (S_IEXEC))
f->typID = '*';
#endif
else if (fl && S_ISLNK(m))
f->typID = 'l';
else
f->typID = ' ';
}
f->size = st->st_size;
f->mode = st->st_mode;
f->uid = st->st_uid;
f->gid = st->st_gid;
f->modtime = st->st_mtime;
f->acctime = st->st_atime;
#else
/* Handle special case of "." for recovery... */
if (strcmp(fname, ".") == 0)
{
struct stat *st = getFileInfo(".");
f->typID = PATH_SEP;
f->size = st->st_size;
f->mode = st->st_mode;
f->modtime = st->st_mtime;
f->acctime = st->st_atime;
}
else
{
struct time tm;
struct date dt;
/* re-use info in fdat ffblk for efficiency */
if (fdat->ff_attrib & FA_DIREC)
f->typID = PATH_SEP;
else if (dirOnly)
return 0;
else if (fdat->ff_attrib & FA_SYSTEM)
f->typID = 's';
else if (fdat->ff_attrib & FA_HIDDEN)
f->typID = 'h';
else if (fdat->ff_attrib & FA_RDONLY)
f->typID = 'r';
/* else if (m & (S_IEXEC)) f->typID = '*';*/
else
f->typID = ' ';
f->size = fdat->ff_fsize;
f->mode = fdat->ff_attrib;
f->uid = f->gid = 0;
f->acctime = f->modtime = normaliseDate(
1980 + ((fdat->ff_fdate >> 9) & 0x3f), /* years */
(fdat->ff_fdate >> 5) & 0xf, /* months */
fdat->ff_fdate & 0x1f, /* days */
(fdat->ff_ftime >> 11) & 0x1f, /* hours */
(fdat->ff_ftime >> 5) & 0x3f, /* mins */
(fdat->ff_ftime & 0x1f) * 2l); /* secs */
}
#endif
if (f->name)
free(f->name);
if ((f->name = calloc(strlen(fname) + 1, sizeof(char))) != NULL)
strcpy(f->name, fname);
f->flag = 0;
f->depth = dpth;
f->parent = -1;
fIndex[n][i] = i;
return 1;
}
/*******************************************************************
SAVE AND RESTORE FILE SELECTIONS BETWEEN WINDOW RESCANS
********************************************************************/
#if __STDC__
static int saveSelected(int n)
#else
static int saveSelected(n)
int n;
#endif
{
int i, j;
for (i = j = 0; i < totfiles[n]; i++)
{
if (fInfo[n][i].flag & F_SELECTED)
{
oldSelNames[j++] = fInfo[n][i].name;
fInfo[n][i].name = NULL;
}
}
return j;
}
#if __STDC__
static void reselect(int n, int i, int *selected)
#else
static void reselect(n, i, selected)
int n, i, *selected;
#endif
{
int k;
for (k = 0; k < (*selected); k++)
{
if (strcmp(oldSelNames[k], fInfo[n][i].name) == 0)
{
fInfo[n][i].flag = F_SELECTED;
selCnt[n]++;
selSize[n] += fInfo[n][i].size;
(*selected)--;
free(oldSelNames[k]);
oldSelNames[k] = oldSelNames[*selected];
oldSelNames[*selected] = NULL;
break;
}
}
}
/*************************************************************
BUILD THE FULL FULL PATH OF A TREE VIEW ENTRY
**************************************************************/
#ifdef __MSDOS__
static struct ffblk dStk[20];
#else
static void *dStk[20];
#endif
#define MAXTREE 20
#if __STDC__
void getTreePath(int n, char *buf)
#else
void getTreePath(n, buf)
int n;
char *buf;
#endif
{
int f = fIndex[n][highlight[n]];
int stk[MAXTREE], i, Len = strlen(paths[n]);
strcpy(buf, paths[n]);
if (Len && buf[Len - 1] == PATH_SEP)
buf[Len - 1] = '\0';
for (i = 0; i < MAXTREE; i++)
{
stk[i] = f;
if (fInfo[n][f].parent < 0)
break;
else
f = fInfo[n][f].parent;
}
for (;;)
{
strcat(buf, PATH_SEP_STR);
strcat(buf, fInfo[n][stk[i]].name);
if (i-- <= 0)
break;
}
}
/********************************************************
DIRECTORY TREE VIEWING
*********************************************************/
static int treeDepth = 0;
#if __STDC__
static void readTree(int n, char *path, int *cnt, int parent, int depth)
#else
static void readTree(n, path, cnt, parent, depth)
int n, depth, *cnt, parent;
char *path;
#endif
{
int buf;
char *fname;
if (depth >= treeDepth || *cnt >= MAXARGS)
{
if (*cnt >= MAXARGS)
showMsg("Too many directories - some will be dropped");
return;
}
#ifdef __MSDOS__
if (initDir(path) == NULL)
#else
if ((dStk[depth] = initDir(path)) == NULL)
#endif
{
showMsg("initDir failed!");
return; /* Should produce an error */
}
if ((buf = GRABBUF()) == 0)
return;
while ((*cnt) < MAXARGS &&
#ifdef __MSDOS__
(fname = getDirEntry(testOption(VAR_SHOWHIDDEN), &dStk[depth])) != NULL)
#else
(fname = getDirEntry(dStk[depth], testOption(VAR_SHOWHIDDEN))) != NULL)
#endif
{
if (strcmp(fname, ".") == 0)
continue;
if (strcmp(fname, "..") == 0)
continue;
strcpy(BUF[buf], path);
#ifdef __MSDOS__
if (BUF[buf][strlen(BUF[buf]) - 1] != '\\')
strcat(BUF[buf], "\\");
strcat(BUF[buf], fname);
if (addEntry(n, (*cnt), fname, depth, 1, &dStk[depth]))
#else
if (BUF[buf][strlen(BUF[buf]) - 1] != '/')
strcat(BUF[buf], "/");
strcat(BUF[buf], fname);
if (addEntry(BUF[buf], n, (*cnt), fname, depth, 1))
#endif
{
fInfo[n][*cnt].parent = parent;
*cnt += 1;
if ((*cnt) >= MAXARGS)
break;
readTree(n, BUF[buf], cnt, (*cnt) - 1, depth + 1);
}
}
#ifndef __MSDOS__
closeDir((void *) dStk[depth]);
#endif
FREEBUF(buf);
}
/*****************************************************
DIRECTORY CHANGING
******************************************************/
#ifdef __MSDOS__
void mychdir(char *d)
{
if (d[1] == ':')
{
(void) _chdrive(toupper(d[0]) - 'A' + 1);
d += 2;
}
(void) chdir(d);
}
#endif
#if __STDC__
void cd2old(void)
#else
void cd2old()
#endif
{
CHDIR(paths[l]);
}
#if __STDC__
void doCD(char *d)
#else
void doCD(d)
char *d;
#endif /* __STDC__ */
{
char *p = NULL;
int ex = GRABBUF(), ne = GRABBUF();
int rtn = 0, typ = splitPath(d, BUF[ex], BUF[ne]);
if (BUF[ne][0])
goto end; /* d doesn't exist - fail */
if (typ >= 0) /* container */
{
strcpy(BUF[ne], (p = STRRCHR(BUF[ex], PATH_SEP)) + 1); /* container name */
#ifdef __MSDOS__
if (p > (BUF[ex] + 2))
#else
if (p > BUF[ex] + 2)
#endif
*p = '\0';
else
*(p + 1) = '\0';
}
else if (typ != -3)
goto end; /* not a dir */
#ifdef __MSDOS__
{
/* Change the drive if necessary */
unsigned drv, ndrv;
drv = BUF[ex][0] - 'A';
if (drv != getdisk())
{
_dos_setdrive(drv + 1, &ndrv);
if (_dos_getdrive(&ndrv), ndrv != (drv + 1))
rtn = -1;
}
}
if (rtn == 0)
rtn = chdir(BUF[ex] + 2);
#else
if (rtn == 0)
rtn = chdir(BUF[ex]);
#endif
if (rtn == 0)
{
char *path = (typ >= 0) ? makePath(BUF[ex], BUF[ne]) : BUF[ex];
if (strcmp(paths[l], path))
{
strcpy(paths[l], path);
if (winState[l] == W_LIST)
winInvalid[l] = 2;
}
}
else
{
#ifndef NO_STRERROR
sprintf(BUF[ne], "Failed to change to directory %s - %s", BUF[ex],
(char *) strerror(errno));
#else
sprintf(BUF[ne], "Failed to change to directory %s", BUF[ex]);
#endif
showMsg(BUF[ne]);
}
end:
FREEBUF(ne);
FREEBUF(ex);
}
/******************************************************************
SPLIT THE FIELDS OF A CONTAINER CONTENTS LISTING
******************************************************************/
#define MAX_FIELDS 16
static char *fieldBufs[MAX_FIELDS];
#if __STDC__
static int splitFields(char *b)
#else
static int splitFields(b)
char *b;
#endif
{
int i = 0;
if (b == NULL)
return 0;
while (i < MAX_FIELDS)