-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.c
3935 lines (3534 loc) · 131 KB
/
file.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
//----------------------------------------------------------------------------
// file.h:
//
// File operations
//
//----------------------------------------------------------------------------
#include "alloc.h"
#include "error.h"
#include "eval.h"
#include "file.h"
#include "gui.h"
#include "probe.h"
#include "resource.h"
#include "strop.h"
#include "util.h"
#include <dirent.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#ifdef AMIGA
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <dos/dosextens.h>
#include <workbench/workbench.h>
#endif
//----------------------------------------------------------------------------
// Helper functions
//----------------------------------------------------------------------------
static int h_copyfile(entry_p contxt, const char *src, const char *dst, int mode);
static int h_exists(const char *n);
static const char *h_fileonly(int id, const char *s);
static pnode_p h_filetree(int id, const char *src, const char *dst, entry_p files, entry_p fonts, entry_p choices, entry_p pattern);
static int h_makedir(entry_p contxt, const char *dst, int mode);
static int h_protect_get(entry_p contxt, const char *file, LONG *mask);
static int h_protect_set(entry_p contxt, const char *file, LONG mask);
static int h_confirm(entry_p contxt, const char *hlp, const char *msg, ...);
static int h_delete_dir(entry_p contxt, const char *dir);
static int h_delete_file(entry_p contxt, const char *file);
static int h_delete_pattern(entry_p contxt, const char *pat);
#define CF_INFOS (1 << 0)
#define CF_FONTS (1 << 1)
#define CF_NOGAUGE (1 << 2)
#define CF_NOFAIL (1 << 3)
#define CF_OKNODELETE (1 << 4)
#define CF_FORCE (1 << 5)
#define CF_ASKUSER (1 << 6)
#define CF_NOPOSITION (1 << 7)
#define CF_SILENT (1 << 8)
//----------------------------------------------------------------------------
// (expandpath <path>)
// Expands a short path to its full path equivalent
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_expandpath(entry_p contxt)
{
// We need one argument, a short path.
if(c_sane(contxt, 1))
{
// On non Amiga systems, this is a dummy.
#ifdef AMIGA
BPTR lock = (BPTR) Lock(str(CARG(1)), ACCESS_READ);
if(lock)
{
// Allocate enough to hold any valid path.
char *buf = calloc(PATH_MAX, 1);
if(buf)
{
// FIXME - error handling
NameFromLock(lock, buf, PATH_MAX);
UnLock(lock);
RSTR(buf);
}
UnLock(lock);
}
#endif
}
else
{
// Broken parser
error(PANIC);
}
// Return empty string.
REST;
}
//----------------------------------------------------------------------------
// (copyfiles (prompt..) (help..) (source..) (dest..) (newname..) (choices..)
// (all) (pattern..) (files) (infos) (confirm..) (safe) (optional
// <option> <option> ...) (delopts <option> <option> ...) (nogauge))
//
// copy files (and subdir's by default). files option say NO
// subdirectories
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_copyfiles(entry_p contxt)
{
// We need atleast one argument
if(c_sane(contxt, 1))
{
entry_p prompt = get_opt(contxt, OPT_PROMPT),
help = get_opt(contxt, OPT_HELP),
source = get_opt(contxt, OPT_SOURCE),
dest = get_opt(contxt, OPT_DEST),
newname = get_opt(contxt, OPT_NEWNAME),
choices = get_opt(contxt, OPT_CHOICES),
all = get_opt(contxt, OPT_ALL),
pattern = get_opt(contxt, OPT_PATTERN),
infos = get_opt(contxt, OPT_INFOS),
confirm = get_opt(contxt, OPT_CONFIRM),
safe = get_opt(contxt, OPT_SAFE),
nogauge = get_opt(contxt, OPT_NOGAUGE),
fonts = get_opt(contxt, OPT_FONTS),
optional = get_opt(contxt, OPT_OPTIONAL),
delopts = get_opt(contxt, OPT_DELOPTS),
files = all ? NULL : get_opt(contxt, OPT_FILES),
fail = get_opt(delopts, OPT_FAIL) ?
NULL : get_opt(optional, OPT_FAIL),
nofail = get_opt(delopts, OPT_NOFAIL) ?
NULL : get_opt(optional, OPT_NOFAIL),
oknodelete = get_opt(delopts, OPT_OKNODELETE) ?
NULL : get_opt(optional, OPT_OKNODELETE),
force = get_opt(delopts, OPT_FORCE) ?
NULL : get_opt(optional, OPT_FORCE),
askuser = get_opt(delopts, OPT_ASKUSER) ?
NULL : get_opt(optional, OPT_ASKUSER);
DNUM = 0;
// The (pattern) (choices) and (all) options
// are mutually exclusive.
if((pattern && (choices || all)) ||
(choices && (pattern || all)) ||
(all && (choices || pattern)))
{
error(contxt->id, ERR_OPTION_MUTEX,
"pattern/choices/all");
RCUR;
}
// The (fail) (nofail) and (oknodelete) options
// are mutually exclusive.
if((fail && (nofail || oknodelete)) ||
(nofail && (fail || oknodelete)) ||
(oknodelete && (nofail || fail)))
{
error(contxt->id, ERR_OPTION_MUTEX,
"fail/nofail/oknodelete");
RCUR;
}
// We need a source and a destination dir.
if(source && dest)
{
// Tree of source / destination tuples.
pnode_p tree;
const char *src = str(source),
*dst = str(dest);
// If the source is a directory, (all),
// (choices) or (pattern) must be used.
if(h_exists(src) == 2 &&
!all && !choices && !pattern)
{
error(contxt->id, ERR_MISSING_OPTION,
"all/choices/pattern");
RCUR;
}
// A non safe operation in pretend mode
// always succeeds.
if(get_numvar(contxt, "@pretend") && !safe)
{
RNUM(1);
}
// Does the destination already exist?
if(h_exists(dst) == 2)
{
// Don't trust h_exists, this string
// might be empty.
size_t dln = strlen(dst);
// If it's not a volume, prompt for
// confirmation before overwriting.
if(dln && dst[dln - 1] != ':')
{
if(!h_confirm(contxt, "", tr(S_ODIR), dst))
{
h_log(contxt, tr(S_ACPY), src, dst);
RCUR;
}
else
{
// Give permission so that overwriting
// can succeed. The proper (as in what
// the source has) permissions are set
// later.
chmod(dst, S_IRWXU);
}
}
}
// Traverse the source directory and create
// the corresponding destination strings.
tree = h_filetree
(
contxt->id, src, dst,
files,
fonts,
choices,
pattern
);
// Unless we ran out of memory when traversing
// the source directory, we now have a list of
// source -> destination tuples.
if(tree)
{
int go = 0;
pnode_p cur = tree;
if(newname)
{
// Replace file name if single file and
// the 'newname' option is set
if(cur->next && cur->type == 2 &&
cur->next->type == 1 && !cur->next->next)
{
free(cur->next->copy);
cur->next->copy = h_tackon(contxt->id, dst, str(newname));
}
}
// Find out if we need confirmation...
if(confirm)
{
// The default threshold is expert.
int level = get_numvar(contxt, "@user-level"),
th = 2;
// If the (confirm ...) option contains
// something that can be translated into
// a new threshold value...
if(confirm->children &&
confirm->children[0] &&
confirm->children[0] != end())
{
// ...then do so.
th = num(confirm->children[0]);
}
// If we are below the threshold value, or
// user input has been short-circuited by
// @yes, skip confirmation.
if(level < th ||
get_numvar(contxt, "@yes"))
{
confirm = NULL;
}
// Make sure that we have the prompt and
// help texts that we need if 'confirm'
// is set.
if(!prompt || !help)
{
cur = NULL;
error(contxt->id, ERR_MISSING_OPTION,
prompt ? "help" : "prompt");
}
}
// Initialize GUI, set up file lists, events,
// and so on.
if(cur)
{
go = gui_copyfiles_start
(
prompt ? str(prompt) : NULL,
confirm ? str(help) : NULL,
cur,
confirm != NULL
);
}
// Start copy unless user canceled / aborted.
if(go == 1)
{
// Set file copy mode.
int md = (infos ? CF_INFOS : 0) |
(fonts ? CF_FONTS : 0) |
(nogauge ? CF_NOGAUGE : 0) |
(nofail ? CF_NOFAIL : 0) |
(oknodelete ? CF_OKNODELETE : 0) |
(force ? CF_FORCE : 0) |
(askuser ? CF_ASKUSER : 0);
DNUM = 1;
// For all files / dirs in list, copy / create.
for(; cur && DNUM;
cur = cur->next)
{
LONG prm;
// Copy file / create dir / skip if zero:ed
switch(cur->type)
{
case 0:
continue;
case 1:
DNUM = h_copyfile
(
contxt,
cur->name,
cur->copy,
md
);
break;
case 2:
// Make directory and make sure the protection
// bits the new one matches the old.
if(h_makedir(contxt, cur->copy, md) &&
h_protect_get(contxt, cur->name, &prm) &&
h_protect_set(contxt, cur->copy, prm))
{
DNUM = 1;
}
else
{
DNUM = 0;
}
break;
default:
error(PANIC);
DNUM = 0;
}
}
// GUI teardown.
gui_copyfiles_end();
}
else
{
// '0' == skip.
// '-1' == halt.
if(go == -1)
{
error(HALT);
}
}
cur = tree;
// Free list of files / dirs.
while(cur)
{
tree = cur;
free(cur->name);
free(cur->copy);
cur = cur->next;
free(tree);
}
}
}
else
{
error(contxt->id, ERR_MISSING_OPTION,
!source ? "source" : "dest");
}
}
else
{
// The parser is broken
error(PANIC);
}
// We don't know if we're successsful,
// at this point, return what we have.
RCUR;
}
//----------------------------------------------------------------------------
// (copylib (prompt..) (help..) (source..) (dest..) (newname..) (infos)
// (confirm) (safe) (optional <option> <option> ...) (delopts <option>
// <option> ...) (nogauge))
//
// install a library if newer version
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_copylib(entry_p contxt)
{
// We need atleast one argument
if(c_sane(contxt, 1))
{
entry_p prompt = get_opt(contxt, OPT_PROMPT),
help = get_opt(contxt, OPT_HELP),
source = get_opt(contxt, OPT_SOURCE),
dest = get_opt(contxt, OPT_DEST),
newname = get_opt(contxt, OPT_NEWNAME),
infos = get_opt(contxt, OPT_INFOS),
confirm = get_opt(contxt, OPT_CONFIRM),
safe = get_opt(contxt, OPT_SAFE),
nogauge = get_opt(contxt, OPT_NOGAUGE),
noposition = get_opt(contxt, OPT_NOPOSITION),
optional = get_opt(contxt, OPT_OPTIONAL),
delopts = get_opt(contxt, OPT_DELOPTS),
fail = get_opt(delopts, OPT_FAIL) ?
NULL : get_opt(optional, OPT_FAIL),
nofail = get_opt(delopts, OPT_NOFAIL) ?
NULL : get_opt(optional, OPT_NOFAIL),
oknodelete = get_opt(delopts, OPT_OKNODELETE) ?
NULL : get_opt(optional, OPT_OKNODELETE),
force = get_opt(delopts, OPT_FORCE) ?
NULL : get_opt(optional, OPT_FORCE),
askuser = get_opt(delopts, OPT_ASKUSER) ?
NULL : get_opt(optional, OPT_ASKUSER);
DNUM = 0;
// The (fail) (nofail) and (oknodelete) options
// are mutually exclusive.
if((fail && (nofail || oknodelete)) ||
(nofail && (fail || oknodelete)) ||
(oknodelete && (nofail || fail)))
{
error(contxt->id, ERR_OPTION_MUTEX,
"fail/nofail/oknodelete");
RCUR;
}
// We always need a prompt and help since trying
// to overwrite new files with older ones will
// force a confirm if we're in expert mode. The
// CBM installer doesn't behave this way, but the
// Installer guide says so, and it makes sense, so
// let's do it this way until we know for sure that
// this will break existing scripts.
if(source && dest &&
prompt && help)
{
const char *s = str(source),
*d = str(dest);
// Does the source file exist?
if(h_exists(s) == 1)
{
// User level.
int level = get_numvar(contxt, "@user-level"),
// Source file version.
vs = h_getversion(contxt, s),
// Destination type.
dt = h_exists(d);
// Destination file.
char *f;
// A non safe operation in pretend mode
// always succeeds.
if(get_numvar(contxt, "@pretend") && !safe)
{
RNUM(1);
}
if(vs < 0)
{
// Could not find version string.
error(contxt->id, ERR_NO_VERSION, s);
RCUR;
}
if(dt == 1)
{
// Destination is a file, not a dir.
error(contxt->id, ERR_NOT_A_DIR, d);
RCUR;
}
// Find out if we need confirmation...
if(confirm)
{
// The default threshold is expert.
int th = 2;
// If the (confirm ...) option contains
// something that can be translated into
// a new threshold value...
if(confirm->children &&
confirm->children[0] &&
confirm->children[0] != end())
{
// ...then do so.
th = num(confirm->children[0]);
}
// If we are below the threshold value, or
// user input has been short-circuited by
// @yes, skip confirmation.
if(level < th ||
get_numvar(contxt, "@yes"))
{
confirm = NULL;
}
}
if(!dt)
{
// Directory doesn't exist, create
// it. One level deep only.
if(!mkdir(d, 0777))
{
// Log the success.
h_log(contxt, tr(S_CRTD), d);
}
else
{
// Permission problems or the dir is
// more than 1 level deeper than the
// existing path.
error(contxt->id, ERR_WRITE_DIR, d);
RCUR;
}
}
// Are we renaming the file?
if(newname)
{
// Yes, append the new name to the path.
f = h_tackon(contxt->id, d, str(newname));
}
else
{
// No, append the file part of the (possibly)
// full source path to the destination path.
f = h_tackon(contxt->id, d, h_fileonly(contxt->id, s));
}
// If we're not out of memory and destination dir
// and / or destination file is non empty, f will
// be <> 0.
if(f)
{
// Get f type info and set copy mode.
int ft = h_exists(f),
md = CF_SILENT |
(infos ? CF_INFOS : 0) |
(noposition ? CF_NOPOSITION : 0) |
(nogauge ? CF_NOGAUGE : 0) |
(nofail ? CF_NOFAIL : 0) |
(oknodelete ? CF_OKNODELETE : 0) |
(force ? CF_FORCE : 0) |
(askuser ? CF_ASKUSER : 0);
// Does it exist?
if(!ft)
{
// No such file, copy source to the
// destination directory. If needed
// get confirmation.
if(!confirm ||
h_confirm
(
contxt,
"",
"%s\n\n%s: %d.%d\n%s\n\n%s: %s",
str(prompt),
tr(S_VINS),
vs >> 16,
vs & 0xffff,
tr(S_NINS),
tr(S_DDRW),
d
))
{
DNUM = h_copyfile(contxt, s, f, md);
}
}
else
// It's a file.
if(ft == 1)
{
// Get version of existing file.
int vf = h_getversion(contxt, f);
// Did we find a version not equal to
// that of the current file?
if(vf >= 0)
{
if(vs != vf)
{
// If we ask for confirmation and get it, copy
// the file no matter what version it (and the
// existing destination file) has.
if(confirm)
{
if(h_confirm(
contxt,
"",
"%s\n\n%s: %d.%d\n%s: %d.%d\n\n%s: %s",
str(prompt),
tr(S_VINS),
vs >> 16,
vs & 0xffff,
tr(S_VCUR),
vf >> 16,
vf & 0xffff,
tr(S_DDRW),
d))
{
DNUM = h_copyfile(contxt, s, f, md);
}
}
else
{
// If the file to be copied has a higher version
// number than the existing one, overwrite.
if(vs > vf)
{
DNUM = h_copyfile(contxt, s, f, md);
}
else
{
// If the file to be copied has a lower version
// number than the existing one, and we're in
// expert mode, ask the user to confirm. If we
// get a confirmation, overwrite.
if(level == 2)
{
if(h_confirm(
contxt,
"",
"%s\n\n%s: %d.%d\n%s: %d.%d\n\n%s: %s",
str(prompt),
tr(S_VINS),
vs >> 16,
vs & 0xffff,
tr(S_VCUR),
vf >> 16,
vf & 0xffff,
tr(S_DDRW),
d))
{
DNUM = h_copyfile(contxt, s, f, md);
}
}
}
}
}
}
else
{
// Could not find version string.
error(contxt->id, ERR_NO_VERSION, f);
}
}
// It's a dir.
else
{
// Dest file exists, but is a directory.
error(contxt->id, ERR_NOT_A_FILE, f);
}
// Free memory allocated by h_tackon.
free(f);
}
}
else
{
// No file, doesn't exist or is dir.
error(contxt->id, ERR_NOT_A_FILE, s);
}
}
else
{
error
(
contxt->id, ERR_MISSING_OPTION,
!source ? "source" :
!dest ? "dest" :
!prompt ? "prompt" : "help"
);
}
}
else
{
// The parser is broken
error(PANIC);
}
// Success or failure.
RCUR;
}
//----------------------------------------------------------------------------
// (delete file (help..) (prompt..) (confirm..)
// (infos) (optional <option> <option> ...) (all)
// (delopts <option> <option> ...) (safe))
//
// delete file
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_delete(entry_p contxt)
{
// We need atleast one argument
if(c_sane(contxt, 1))
{
int wc = 0;
const char *w = str(CARG(1));
#ifdef AMIGA
wc = ParsePattern(w, get_buf(), buf_size());
#endif
// Assume failure.
DNUM = 0;
if(wc >= 0)
{
entry_p help = get_opt(CARG(2), OPT_HELP),
prompt = get_opt(CARG(2), OPT_PROMPT),
confirm = get_opt(CARG(2), OPT_CONFIRM),
safe = get_opt(CARG(2), OPT_SAFE);
// Find out if we need confirmation...
if(confirm)
{
// The default threshold is expert.
int level = get_numvar(contxt, "@user-level"),
th = 2;
// If the (confirm ...) option contains
// something that can be translated into
// a new threshold value...
if(confirm->children &&
confirm->children[0] &&
confirm->children[0] != end())
{
// ...then do so.
th = num(confirm->children[0]);
}
// If we are below the threshold value,
// don't care about getting confirmation
// from the user.
if(level < th)
{
confirm = NULL;
}
// Make sure that we have the prompt and
// help texts that we need if 'confirm'
// is set.
if(!prompt || !help)
{
error(contxt->id, ERR_MISSING_OPTION,
prompt ? "help" : "prompt");
RCUR;
}
}
// If we need confirmation and the user skips
// or aborts, return. On abort, the HALT will
// be set by h_confirm.
if(confirm &&
!h_confirm(contxt, str(help), str(prompt)))
{
RCUR;
}
// Is this a safe operation or are we not
// running in pretend mode?
if(safe || !get_numvar(contxt, "@pretend"))
{
if(wc)
{
DNUM = h_delete_pattern(contxt, w);
}
else
{
switch(h_exists(w))
{
case 2:
DNUM = h_delete_dir(contxt, w);
break;
case 1:
DNUM = h_delete_file(contxt, w);
break;
case 0:
h_log(contxt, tr(S_NSFL), w);
DNUM = 0;
}
}
}
else
{
// A non safe operation in pretend
// mode always succeeds.
DNUM = 1;
}
}
else
{
// We probably had a buffer overflow.
error(contxt->id, ERR_OVERFLOW, w);
}
}
else
{
// The parser is broken
error(PANIC);
}
RCUR;
}
//----------------------------------------------------------------------------
// (exists <filename> (noreq))
// 0 if no, 1 if file, 2 if dir
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_exists(entry_p contxt)
{
if(c_sane(contxt, 1))
{
// Non-existance.
int r = 0;
// Supress volume requester?
if(get_opt(contxt, OPT_NOREQ))
{
#ifdef AMIGA
struct Process *p = (struct Process *)
FindTask(NULL);
// Save the current window ptr.
APTR w = p->pr_WindowPtr;
// Disable auto request.
p->pr_WindowPtr = (APTR) -1L;
#endif
// Get type (file / dir / 0)
r = h_exists(str(CARG(1)));
#ifdef AMIGA
// Restore auto request.
p->pr_WindowPtr = w;
#endif
}
else
{
// Get type (file / dir / 0)
r = h_exists(str(CARG(1)));
}
RNUM(r);
}
else
{
// The parser is broken
error(PANIC);
RCUR;
}
}
//----------------------------------------------------------------------------
// (fileonly <path>)
// return file part of path (see pathonly)
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_fileonly(entry_p contxt)
{
if(c_sane(contxt, 1))
{
const char *p = str(CARG(1)),
*f = h_fileonly(contxt->id, p);
RSTR(strdup(f));
}
else
{
// The parser is broken
error(PANIC);
REST;
}
}
//----------------------------------------------------------------------------
// (foreach <dir> <pattern> <statements>)
// do for entries in directory
//
// Refer to Installer.guide 1.19 (29.4.96) 1995-96 by ESCOM AG
//----------------------------------------------------------------------------
entry_p m_foreach(entry_p contxt)
{
if(contxt || c_sane(contxt, 3))
{
// Pattern matching is not done
// on non-Amiga systems.
#ifdef AMIGA
const char *pt = str(CARG(2));
#endif
// Open dir and assume failure.
const char *dn = str(CARG(1));
DIR *dir = opendir(dn);
pnode_p top = NULL;
int err = 1;
// Permission to read?
if(dir)
{
static char cwd[PATH_MAX];
struct dirent *ent = readdir(dir);
// Save the current working directory.
if(getcwd(cwd, PATH_MAX) == cwd)
{
// Enter the directory <drawer name>
if(!chdir(dn))
{
// Allocate memory for the start node.
pnode_p cur;
top = calloc(1, sizeof(struct pnode_t));
#ifdef AMIGA
// This file info block will be used for all files
// and subdirs in the directory.
struct FileInfoBlock *fib = (struct FileInfoBlock *)
AllocDosObject(DOS_FIB, NULL);
// Use cur as a gatekeeper. If set to NULL, nothing
// will take place except clean ups.
cur = fib ? top : NULL;
#else
// No pattern matching = no fib.
cur = top;
#endif
// The dir might be empty but that's not an
// error.
err = 0;
// Iterate over all entries in dir.
while(ent && cur && !err)
{
// Name of file / dir.
const char *fn = ent->d_name;
#ifndef AMIGA
// Filter out the magic on non-Amigas.
if(strcmp(fn, ".") &&
strcmp(fn, ".."))
#endif
{
#ifdef AMIGA
// The dir is not empty, we should be able
// to go all the way here. Assume failure.
err = 1;
// Lock and get the information we need
// from the current entry
BPTR lock = (BPTR) Lock(fn, ACCESS_READ);
if(lock)
{
if(Examine(lock, fib))
{
// Please note that if we fail to
// lock and examine cur->name will
// be NULL.
cur->type = fib->fib_DirEntryType;
cur->name = strdup(fn);
// We're probably good. PANIC:s will
// be caught further down.
err = 0;