summaryrefslogtreecommitdiff
path: root/amigaos4/amigaos.c
blob: f79943079f63cd8d86339ceeead43ba67192b367 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/* amigaos.c uses only amigaos APIs,
 * as opposed to amigaio.c which mixes amigaos and perl APIs */

#include <string.h>

#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(__CLIB2__)
#  include <dos.h>
#endif
#if defined(__NEWLIB__)
#  include <amiga_platform.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#undef WORD
#define WORD int16

#include <dos/dos.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/utility.h>

#include "amigaos.h"

struct UtilityIFace *IUtility = NULL;

/***************************************************************************/

struct Interface *OpenInterface(CONST_STRPTR libname, uint32 libver)
{
        struct Library *base = IExec->OpenLibrary(libname, libver);
        struct Interface *iface = IExec->GetInterface(base, "main", 1, NULL);
        if (iface == NULL)
        {
                // We should probably post some kind of error message here.

                IExec->CloseLibrary(base);
        }

        return iface;
}

/***************************************************************************/

void CloseInterface(struct Interface *iface)
{
        if (iface != NULL)
        {
                struct Library *base = iface->Data.LibBase;
                IExec->DropInterface(iface);
                IExec->CloseLibrary(base);
        }
}

BOOL __unlink_retries = FALSE;

void ___makeenviron() __attribute__((constructor));
void ___freeenviron() __attribute__((destructor));

void ___openinterfaces() __attribute__((constructor));
void ___closeinterfaces() __attribute__((destructor));

void ___openinterfaces()
{
        if (!IDOS)
                IDOS = (struct DOSIFace *)OpenInterface("dos.library", 53);
        if (!IUtility)
                IUtility =
                    (struct UtilityIFace *)OpenInterface("utility.library", 53);
}

void ___closeinterfaces()
{
        CloseInterface((struct Interface *)IDOS);
        CloseInterface((struct Interface *)IUtility);
}
int VARARGS68K araddebug(UBYTE *fmt, ...);
int VARARGS68K adebug(UBYTE *fmt, ...);

#define __USE_RUNCOMMAND__

char **myenviron = NULL;
char **origenviron = NULL;

static void createvars(char **envp);

struct args
{
        BPTR seglist;
        int stack;
        char *command;
        int length;
        int result;
        char **envp;
};

int __myrc(char *arg)
{
        struct Task *thisTask = IExec->FindTask(0);
        struct args *myargs = (struct args *)thisTask->tc_UserData;
        if (myargs->envp)
                createvars(myargs->envp);
        // adebug("%s %ld %s \n",__FUNCTION__,__LINE__,myargs->command);
        myargs->result = IDOS->RunCommand(myargs->seglist, myargs->stack,
                                          myargs->command, myargs->length);
        return 0;
}

int32 myruncommand(
    BPTR seglist, int stack, char *command, int length, char **envp)
{
        struct args myargs;
        struct Task *thisTask = IExec->FindTask(0);
        struct Process *proc;

        // adebug("%s %ld  %s\n",__FUNCTION__,__LINE__,command?command:"NULL");

        myargs.seglist = seglist;
        myargs.stack = stack;
        myargs.command = command;
        myargs.length = length;
        myargs.result = -1;
        myargs.envp = envp;

        if ((proc = IDOS->CreateNewProcTags(
                 NP_Entry, __myrc, NP_Child, TRUE, NP_Input, IDOS->Input(),
                 NP_Output, IDOS->Output(), NP_Error, IDOS->ErrorOutput(),
                 NP_CloseInput, FALSE, NP_CloseOutput, FALSE, NP_CloseError,
                 FALSE, NP_CopyVars, FALSE,

                 //           NP_StackSize,           ((struct Process
                 //           *)myargs.parent)->pr_StackSize,
                 NP_Cli, TRUE, NP_UserData, (int)&myargs,
                 NP_NotifyOnDeathSigTask, thisTask, TAG_DONE)))

        {
                IExec->Wait(SIGF_CHILD);
        }
        return myargs.result;
}

char *mystrdup(const char *s)
{
        char *result = NULL;
        size_t size;

        size = strlen(s) + 1;

        if ((result = (char *)IExec->AllocVec(size, MEMF_ANY)))
        {
                memmove(result, s, size);
        }
        return result;
}

static int pipenum = 0;

int pipe(int filedes[2])
{
        char pipe_name[1024];

//   adebug("%s %ld \n",__FUNCTION__,__LINE__);
#ifdef USE_TEMPFILES
        sprintf(pipe_name, "/T/%x.%08lx", pipenum++, IUtility->GetUniqueID());
#else
        sprintf(pipe_name, "/PIPE/%x%08lx/4096/0", pipenum++,
                IUtility->GetUniqueID());
#endif

        /*      printf("pipe: %s \n", pipe_name);*/

        filedes[1] = open(pipe_name, O_WRONLY | O_CREAT);
        filedes[0] = open(pipe_name, O_RDONLY);
        if (filedes[0] == -1 || filedes[1] == -1)
        {
                if (filedes[0] != -1)
                        close(filedes[0]);
                if (filedes[1] != -1)
                        close(filedes[1]);
                return -1;
        }
        /*      printf("filedes %d %d\n", filedes[0],
         * filedes[1]);fflush(stdout);*/

        return 0;
}

int fork(void)
{
        fprintf(stderr, "Can not bloody fork\n");
        errno = ENOMEM;
        return -1;
}

int wait(int *status)
{
        fprintf(stderr, "No wait try waitpid instead\n");
        errno = ECHILD;
        return -1;
}

char *convert_path_a2u(const char *filename)
{
        struct NameTranslationInfo nti;

        if (!filename)
        {
                return 0;
        }

        __translate_amiga_to_unix_path_name(&filename, &nti);

        return mystrdup(filename);
}
char *convert_path_u2a(const char *filename)
{
        struct NameTranslationInfo nti;

        if (!filename)
        {
                return 0;
        }

        if (strcmp(filename, "/dev/tty") == 0)
        {
                return mystrdup("CONSOLE:");
                ;
        }

        __translate_unix_to_amiga_path_name(&filename, &nti);

        return mystrdup(filename);
}

static struct SignalSemaphore environ_sema;

void amigaos4_init_environ_sema() { IExec->InitSemaphore(&environ_sema); }

void amigaos4_obtain_environ() { IExec->ObtainSemaphore(&environ_sema); }

void amigaos4_release_environ() { IExec->ReleaseSemaphore(&environ_sema); }

static void createvars(char **envp)
{
        if (envp)
        {
                /* Set a local var to indicate to any subsequent sh that it is
                * not
                * the top level shell and so should only inherit local amigaos
                * vars */
                IDOS->SetVar("ABCSH_IMPORT_LOCAL", "TRUE", 5, GVF_LOCAL_ONLY);

                amigaos4_obtain_environ();

                envp = myenviron;

                while ((envp != NULL) && (*envp != NULL))
                {
                        int len;
                        char *var;
                        char *val;
                        if ((len = strlen(*envp)))
                        {
                                if ((var = (char *)IExec->AllocVec(
                                         len + 1, MEMF_ANY | MEMF_CLEAR)))
                                {
                                        strcpy(var, *envp);

                                        val = strchr(var, '=');
                                        if (val)
                                        {
                                                *val++ = '\0';
                                                if (*val)
                                                {
                                                        IDOS->SetVar(
                                                            var, val,
                                                            strlen(val) + 1,
                                                            GVF_LOCAL_ONLY);
                                                }
                                        }
                                        IExec->FreeVec(var);
                                }
                        }
                        envp++;
                }
                amigaos4_release_environ();
        }
}

static BOOL contains_whitespace(char *string)
{

        if (string)
        {

                if (strchr(string, ' '))
                        return TRUE;
                if (strchr(string, '\t'))
                        return TRUE;
                if (strchr(string, '\n'))
                        return TRUE;
                if (strchr(string, 0xA0))
                        return TRUE;
                if (strchr(string, '"'))
                        return TRUE;
        }
        return FALSE;
}

static int no_of_escapes(char *string)
{
        int cnt = 0;
        char *p;
        for (p = string; p < string + strlen(string); p++)
        {
                if (*p == '"')
                        cnt++;
                if (*p == '*')
                        cnt++;
                if (*p == '\n')
                        cnt++;
                if (*p == '\t')
                        cnt++;
        }
        return cnt;
}

struct command_data
{
        STRPTR args;
        BPTR seglist;
        struct Task *parent;
};

int myexecvp(bool isperlthread, const char *filename, char *argv[])
{
        //	adebug("%s %ld
        //%s\n",__FUNCTION__,__LINE__,filename?filename:"NULL");
        /* if there's a slash or a colon consider filename a path and skip
         * search */
        int res;
        if ((strchr(filename, '/') == NULL) && (strchr(filename, ':') == NULL))
        {
                char *path;
                char *name;
                char *pathpart;
                char *p;
                size_t len;
                struct stat st;

                if (!(path = getenv("PATH")))
                {
                        path = ".:/bin:/usr/bin:/c";
                }

                len = strlen(filename) + 1;
                name = (char *)alloca(strlen(path) + len);
                pathpart = (char *)alloca(strlen(path) + 1);
                p = path;
                do
                {
                        path = p;

                        if (!(p = strchr(path, ':')))
                        {
                                p = strchr(path, '\0');
                        }

                        memcpy(pathpart, path, p - path);
                        pathpart[p - path] = '\0';
                        if (!(strlen(pathpart) == 0))
                        {
                                sprintf(name, "%s/%s", pathpart, filename);
                        }
                        else
                                sprintf(name, "%s", filename);

                        if ((stat(name, &st) == 0) && (S_ISREG(st.st_mode)))
                        {
                                /* we stated it and it's a regular file */
                                /* let's boogie! */
                                filename = name;
                                break;
                        }

                } while (*p++ != '\0');
        }
        res = myexecve(isperlthread, filename, argv, myenviron);
        return res;
}

int myexecv(bool isperlthread, const char *path, char *argv[])
{
        return myexecve(isperlthread, path, argv, myenviron);
}

int myexecl(bool isperlthread, const char *path, ...)
{
        va_list va;
        char *argv[1024]; /* 1024 enough? let's hope so! */
        int i = 0;
        // adebug("%s %ld\n",__FUNCTION__,__LINE__);

        va_start(va, path);
        i = 0;

        do
        {
                argv[i] = va_arg(va, char *);
        } while (argv[i++] != NULL);

        va_end(va);
        return myexecve(isperlthread, path, argv, myenviron);
}

#if 0

int myexecve(const char *filename, char *argv[], char *envp[])
{
        FILE *fh;
        char buffer[1000];
        int size = 0;
        char **cur;
        char *interpreter = 0;
        char *interpreter_args = 0;
        char *full = 0;
        char *filename_conv = 0;
        char *interpreter_conv = 0;
        //        char *tmp = 0;
        char *fname;
        //        int tmpint;
        //        struct Task *thisTask = IExec->FindTask(0);
        int result = -1;

        StdioStore store;

		dTHX;
		if(aTHX) // I hope this is NULL when not on a interpreteer thread nor to level.
		{
			/* Save away our stdio */
	        amigaos_stdio_save(aTHX_ & store);
		}

        // adebug("%s %ld %s\n",__FUNCTION__,__LINE__,filename?filename:"NULL");

        /* Calculate the size of filename and all args, including spaces and
         * quotes */
        size = 0; // strlen(filename) + 1;
        for (cur = (char **)argv /* +1 */; *cur; cur++)
        {
                size +=
                    strlen(*cur) + 1 +
                    (contains_whitespace(*cur) ? (2 + no_of_escapes(*cur)) : 0);
        }
        /* Check if it's a script file */

        fh = fopen(filename, "r");
        if (fh)
        {
                if (fgetc(fh) == '#' && fgetc(fh) == '!')
                {
                        char *p;
                        char *q;
                        fgets(buffer, 999, fh);
                        p = buffer;
                        while (*p == ' ' || *p == '\t')
                                p++;
                        if (buffer[strlen(buffer) - 1] == '\n')
                                buffer[strlen(buffer) - 1] = '\0';
                        if ((q = strchr(p, ' ')))
                        {
                                *q++ = '\0';
                                if (*q != '\0')
                                {
                                        interpreter_args = mystrdup(q);
                                }
                        }
                        else
                                interpreter_args = mystrdup("");

                        interpreter = mystrdup(p);
                        size += strlen(interpreter) + 1;
                        size += strlen(interpreter_args) + 1;
                }

                fclose(fh);
        }
        else
        {
                /* We couldn't open this why not? */
                if (errno == ENOENT)
                {
                        /* file didn't exist! */
						goto out;
                }
        }

        /* Allocate the command line */
        filename_conv = convert_path_u2a(filename);

        if (filename_conv)
                size += strlen(filename_conv);
        size += 1;
        full = (char *)IExec->AllocVec(size + 10, MEMF_ANY | MEMF_CLEAR);
        if (full)
        {
                if (interpreter)
                {
                        interpreter_conv = convert_path_u2a(interpreter);
#if !defined(__USE_RUNCOMMAND__)
#warning(using system!)
                        sprintf(full, "%s %s %s ", interpreter_conv,
                                interpreter_args, filename_conv);
#else
                        sprintf(full, "%s %s ", interpreter_args,
                                filename_conv);
#endif
                        IExec->FreeVec(interpreter);
                        IExec->FreeVec(interpreter_args);

                        if (filename_conv)
                                IExec->FreeVec(filename_conv);
                        fname = mystrdup(interpreter_conv);

                        if (interpreter_conv)
                                IExec->FreeVec(interpreter_conv);
                }
                else
                {
#ifndef __USE_RUNCOMMAND__
                        sprintf(full, "%s ", filename_conv);
#else
                        sprintf(full, "");
#endif
                        fname = mystrdup(filename_conv);
                        if (filename_conv)
                                IExec->FreeVec(filename_conv);
                }

                for (cur = (char **)(argv + 1); *cur != 0; cur++)
                {
                        if (contains_whitespace(*cur))
                        {
                                int esc = no_of_escapes(*cur);

                                if (esc > 0)
                                {
                                        char *buff = IExec->AllocVec(
                                            strlen(*cur) + 4 + esc,
                                            MEMF_ANY | MEMF_CLEAR);
                                        char *p = *cur;
                                        char *q = buff;

                                        *q++ = '"';
                                        while (*p != '\0')
                                        {

                                                if (*p == '\n')
                                                {
                                                        *q++ = '*';
                                                        *q++ = 'N';
                                                        p++;
                                                        continue;
                                                }
                                                else if (*p == '"')
                                                {
                                                        *q++ = '*';
                                                        *q++ = '"';
                                                        p++;
                                                        continue;
                                                }
                                                else if (*p == '*')
                                                {
                                                        *q++ = '*';
                                                }
                                                *q++ = *p++;
                                        }
                                        *q++ = '"';
                                        *q++ = ' ';
                                        *q = '\0';
                                        strcat(full, buff);
                                        IExec->FreeVec(buff);
                                }
                                else
                                {
                                        strcat(full, "\"");
                                        strcat(full, *cur);
                                        strcat(full, "\" ");
                                }
                        }
                        else
                        {
                                strcat(full, *cur);
                                strcat(full, " ");
                        }
                }
                strcat(full, "\n");

//            if(envp)
//                 createvars(envp);

#ifndef __USE_RUNCOMMAND__
                result = IDOS->SystemTags(
                    full, SYS_UserShell, TRUE, NP_StackSize,
                    ((struct Process *)thisTask)->pr_StackSize, SYS_Input,
                    ((struct Process *)thisTask)->pr_CIS, SYS_Output,
                    ((struct Process *)thisTask)->pr_COS, SYS_Error,
                    ((struct Process *)thisTask)->pr_CES, TAG_DONE);
#else

                if (fname)
                {
                        BPTR seglist = IDOS->LoadSeg(fname);
                        if (seglist)
                        {
                                /* check if we have an executable! */
                                struct PseudoSegList *ps = NULL;
                                if (!IDOS->GetSegListInfoTags(
                                        seglist, GSLI_Native, &ps, TAG_DONE))
                                {
                                        IDOS->GetSegListInfoTags(
                                            seglist, GSLI_68KPS, &ps, TAG_DONE);
                                }
                                if (ps != NULL)
                                {
                                        //                    adebug("%s %ld %s
                                        //                    %s\n",__FUNCTION__,__LINE__,fname,full);
                                        IDOS->SetCliProgramName(fname);
                                        //                        result=RunCommand(seglist,8*1024,full,strlen(full));
                                        //                        result=myruncommand(seglist,8*1024,full,strlen(full),envp);
                                        result = myruncommand(seglist, 8 * 1024,
                                                              full, -1, envp);
                                        errno = 0;
                                }
                                else
                                {
                                        errno = ENOEXEC;
                                }
                                IDOS->UnLoadSeg(seglist);
                        }
                        else
                        {
                                errno = ENOEXEC;
                        }
                        IExec->FreeVec(fname);
                }

#endif /* USE_RUNCOMMAND */

                IExec->FreeVec(full);
                if (errno == ENOEXEC)
                {
					result = -1;
                }
                goto out;
        }

        if (interpreter)
                IExec->FreeVec(interpreter);
        if (filename_conv)
                IExec->FreeVec(filename_conv);

        errno = ENOMEM;

out:

    amigaos_stdio_restore(aTHX_ &store);
    STATUS_NATIVE_CHILD_SET(result);
    PL_exit_flags |= PERL_EXIT_EXPECTED;
    if (result != -1) my_exit(result);

        return(result);
}

#endif

int pause(void)
{
        fprintf(stderr, "Pause not implemented\n");

        errno = EINTR;
        return -1;
}

uint32 size_env(struct Hook *hook, APTR userdata, struct ScanVarsMsg *message)
{
        if (strlen(message->sv_GDir) <= 4)
        {
                hook->h_Data = (APTR)(((uint32)hook->h_Data) + 1);
        }
        return 0;
}

uint32 copy_env(struct Hook *hook, APTR userdata, struct ScanVarsMsg *message)
{
        if (strlen(message->sv_GDir) <= 4)
        {
                char **env = (char **)hook->h_Data;
                uint32 size =
                    strlen(message->sv_Name) + 1 + message->sv_VarLen + 1 + 1;
                char *buffer = (char *)IExec->AllocVec((uint32)size,
                                                       MEMF_ANY | MEMF_CLEAR);

                snprintf(buffer, size - 1, "%s=%s", message->sv_Name,
                         message->sv_Var);

                *env = buffer;
                env++;
                hook->h_Data = env;
        }
        return 0;
}

void ___makeenviron()
{
        struct Hook hook;

        char varbuf[8];
        uint32 flags = 0;

        struct DOSIFace *myIDOS =
            (struct DOSIFace *)OpenInterface("dos.library", 53);
        if (myIDOS)
        {
                if (myIDOS->GetVar("ABCSH_IMPORT_LOCAL", varbuf, 8,
                                   GVF_LOCAL_ONLY) > 0)
                {
                        flags = GVF_LOCAL_ONLY;
                }
                else
                {
                        flags = GVF_GLOBAL_ONLY;
                }

                hook.h_Entry = size_env;
                hook.h_Data = 0;

                myIDOS->ScanVars(&hook, flags, 0);
                hook.h_Data = (APTR)(((uint32)hook.h_Data) + 1);

                myenviron = (char **)IExec->AllocVec((uint32)hook.h_Data *
                                                         sizeof(char **),
                                                     MEMF_ANY | MEMF_CLEAR);
                origenviron = myenviron;
                if (!myenviron)
                {
                        return;
                }
                hook.h_Entry = copy_env;
                hook.h_Data = myenviron;

                myIDOS->ScanVars(&hook, flags, 0);
                CloseInterface((struct Interface *)myIDOS);
        }
}

void ___freeenviron()
{
        char **i;
        /* perl might change environ, it puts it back except for ctrl-c */
        /* so restore our own copy here */
        struct DOSIFace *myIDOS =
            (struct DOSIFace *)OpenInterface("dos.library", 53);
        if (myIDOS)
        {
                myenviron = origenviron;

                if (myenviron)
                {
                        for (i = myenviron; *i != NULL; i++)
                        {
                                IExec->FreeVec(*i);
                        }
                        IExec->FreeVec(myenviron);
                        myenviron = NULL;
                }
                CloseInterface((struct Interface *)myIDOS);
        }
}

/* reimplementation of popen, clib2's doesn't do all we want */

static BOOL is_final_quote_character(const char *str)
{
        BOOL result;

        result = (BOOL)(str[0] == '\"' && (str[1] == '\0' || isspace(str[1])));

        return (result);
}

static BOOL is_final_squote_character(const char *str)
{
        BOOL result;

        result = (BOOL)(str[0] == '\'' && (str[1] == '\0' || isspace(str[1])));

        return (result);
}

int popen_child()
{
        struct Task *thisTask = IExec->FindTask(0);

        char *command = (char *)thisTask->tc_UserData;
        size_t len;
        char *str;
        int argc;
        int number_of_arguments;
        char *argv[4];

        argv[0] = "sh";
        argv[1] = "-c";
        argv[2] = command ? command : NULL;
        argv[3] = NULL;

        // adebug("%s %ld  %s\n",__FUNCTION__,__LINE__,command?command:"NULL");

        /* We need to give this to sh via execvp, execvp expects filename,
         * argv[]
         */

        myexecvp(FALSE, argv[0], argv);
        if (command)
                IExec->FreeVec(command);

        IExec->Forbid();
        return 0;
}

FILE *amigaos_popen(const char *cmd, const char *mode)
{
        FILE *result = NULL;
        char pipe_name[50];
        char unix_pipe[50];
        char ami_pipe[50];
        char *cmd_copy;
        BPTR input = 0;
        BPTR output = 0;
        struct Process *proc = NULL;
        struct Task *thisTask = IExec->FindTask(0);

        /* First we need to check the mode
         * We can only have unidirectional pipes
         */
        //    adebug("%s %ld cmd %s mode %s \n",__FUNCTION__,__LINE__,cmd,
        //    mode);

        switch (mode[0])
        {
        case 'r':
        case 'w':
                break;

        default:

                errno = EINVAL;
                return result;
        }

        /* Make a unique pipe name
         * we need a unix one and an amigaos version (of the same pipe!)
         * as were linking with libunix.
         */

        sprintf(pipe_name, "%x%08lx/4096/0", pipenum++,
                IUtility->GetUniqueID());
        sprintf(unix_pipe, "/PIPE/%s", pipe_name);
        sprintf(ami_pipe, "PIPE:%s", pipe_name);

        /* Now we open the AmigaOs Filehandles That we wil pass to our
         * Sub process
         */

        if (mode[0] == 'r')
        {
                /* A read mode pipe: Output from pipe input from NIL:*/
                input = IDOS->Open("NIL:", MODE_NEWFILE);
                if (input != 0)
                {
                        output = IDOS->Open(ami_pipe, MODE_NEWFILE);
                }
        }
        else
        {

                input = IDOS->Open(ami_pipe, MODE_NEWFILE);
                if (input != 0)
                {
                        output = IDOS->Open("NIL:", MODE_NEWFILE);
                }
        }
        if ((input == 0) || (output == 0))
        {
                /* Ouch stream opening failed */
                /* Close and bail */
                if (input)
                        IDOS->Close(input);
                if (output)
                        IDOS->Close(output);
                return result;
        }

        /* We have our streams now start our new process
         * We're using a new process so that execve can modify the environment
         * with messing things up for the shell that launched perl
         * Copy cmd before we launch the subprocess as perl seems to waste
         * no time in overwriting it! The subprocess will free the copy.
         */

        if ((cmd_copy = mystrdup(cmd)))
        {
                // adebug("%s %ld
                // %s\n",__FUNCTION__,__LINE__,cmd_copy?cmd_copy:"NULL");
                proc = IDOS->CreateNewProcTags(
                    NP_Entry, popen_child, NP_Child, TRUE, NP_StackSize,
                    ((struct Process *)thisTask)->pr_StackSize, NP_Input, input,
                    NP_Output, output, NP_Error, IDOS->ErrorOutput(),
                    NP_CloseError, FALSE, NP_Cli, TRUE, NP_Name,
                    "Perl: popen process", NP_UserData, (int)cmd_copy,
                    TAG_DONE);
        }
        if (!proc)
        {
                /* New Process Failed to start
                 * Close and bail out
                 */
                if (input)
                        IDOS->Close(input);
                if (output)
                        IDOS->Close(output);
                if (cmd_copy)
                        IExec->FreeVec(cmd_copy);
        }

        /* Our new process is running and will close it streams etc
         * once its done. All we need to is open the pipe via stdio
         */

        return fopen(unix_pipe, mode);
}

/* Work arround for clib2 fstat */
#ifndef S_IFCHR
#define S_IFCHR 0x0020000
#endif

#define SET_FLAG(u, v) ((void)((u) |= (v)))

int afstat(int fd, struct stat *statb)
{
        int result;
        BPTR fh;
        int mode;
        BOOL input;
        /* In the first instance pass it to fstat */
        // adebug("fd %ld ad %ld\n",fd,amigaos_get_file(fd));

        if ((result = fstat(fd, statb) >= 0))
                return result;

/* Now we've got a file descriptor but we failed to stat it */
/* Could be a nil: or could be a std#? */

/* if get_default_file fails we had a dud fd so return failure */
#if !defined(__CLIB2__)

        fh = amigaos_get_file(fd);

        /* if nil: return failure*/
        if (fh == 0)
                return -1;

        /* Now compare with our process Input() Output() etc */
        /* if these were regular files sockets or pipes we had already
         * succeeded */
        /* so we can guess they a character special console.... I hope */

        struct ExamineData *data;
        char name[120];
        name[0] = '\0';

        data = IDOS->ExamineObjectTags(EX_FileHandleInput, fh, TAG_END);
        if (data != NULL)
        {

                IUtility->Strlcpy(name, data->Name, sizeof(name));

                IDOS->FreeDosObject(DOS_EXAMINEDATA, data);
        }

        // adebug("ad %ld '%s'\n",amigaos_get_file(fd),name);
        mode = S_IFCHR;

        if (fh == IDOS->Input())
        {
                input = TRUE;
                SET_FLAG(mode, S_IRUSR);
                SET_FLAG(mode, S_IRGRP);
                SET_FLAG(mode, S_IROTH);
        }
        else if (fh == IDOS->Output() || fh == IDOS->ErrorOutput())
        {
                input = FALSE;
                SET_FLAG(mode, S_IWUSR);
                SET_FLAG(mode, S_IWGRP);
                SET_FLAG(mode, S_IWOTH);
        }
        else
        {
                /* we got a filehandle not handle by fstat or the above */
                /* most likely it's NIL: but lets check */
                struct ExamineData *exd = NULL;
                if ((exd = IDOS->ExamineObjectTags(EX_FileHandleInput, fh,
                                                   TAG_DONE)))
                {
                        BOOL isnil = FALSE;
                        if (exd->Type ==
                            (20060920)) // Ugh yes I know nasty.....
                        {
                                isnil = TRUE;
                        }
                        IDOS->FreeDosObject(DOS_EXAMINEDATA, exd);
                        if (isnil)
                        {
                                /* yep we got NIL: */
                                SET_FLAG(mode, S_IRUSR);
                                SET_FLAG(mode, S_IRGRP);
                                SET_FLAG(mode, S_IROTH);
                                SET_FLAG(mode, S_IWUSR);
                                SET_FLAG(mode, S_IWGRP);
                                SET_FLAG(mode, S_IWOTH);
                        }
                        else
                        {
                                IExec->DebugPrintF(
                                    "unhandled filehandle in afstat()\n");
                                return -1;
                        }
                }
        }

        memset(statb, 0, sizeof(statb));

        statb->st_mode = mode;

#endif
        return 0;
}

BPTR amigaos_get_file(int fd)
{
        BPTR fh = (BPTR)NULL;
        if (!(fh = _get_osfhandle(fd)))
        {
                switch (fd)
                {
                case 0:
                        fh = IDOS->Input();
                        break;
                case 1:
                        fh = IDOS->Output();
                        break;
                case 2:
                        fh = IDOS->ErrorOutput();
                        break;
                default:
                        break;
                }
        }
        return fh;
}

/*########################################################################*/

#define LOCK_START 0xFFFFFFFFFFFFFFFELL
#define LOCK_LENGTH 1LL

// No wait forever option so lets wait for a loooong time.
#define TIMEOUT 0x7FFFFFFF

int amigaos_flock(int fd, int oper)
{
        BPTR fh;
        int32 success = -1;

        if (!(fh = amigaos_get_file(fd)))
        {
                errno = EBADF;
                return -1;
        }

        switch (oper)
        {
        case LOCK_SH:
        {
                if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
                                     REC_SHARED | RECF_DOS_METHOD_ONLY,
                                     TIMEOUT))
                {
                        success = 0;
                }
                break;
        }
        case LOCK_EX:
        {
                if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
                                     REC_EXCLUSIVE | RECF_DOS_METHOD_ONLY,
                                     TIMEOUT))
                {
                        success = 0;
                }
                break;
        }
        case LOCK_SH | LOCK_NB:
        {
                if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
                                     REC_SHARED_IMMED | RECF_DOS_METHOD_ONLY,
                                     TIMEOUT))
                {
                        success = 0;
                }
                else
                {
                        errno = EWOULDBLOCK;
                }
                break;
        }
        case LOCK_EX | LOCK_NB:
        {
                if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
                                     REC_EXCLUSIVE_IMMED | RECF_DOS_METHOD_ONLY,
                                     TIMEOUT))
                {
                        success = 0;
                }
                else
                {
                        errno = EWOULDBLOCK;
                }
                break;
        }
        case LOCK_UN:
        {
                if (IDOS->UnLockRecord(fh, LOCK_START, LOCK_LENGTH))
                {
                        success = 0;
                }
                break;
        }
        default:
        {
                errno = EINVAL;
                return -1;
        }
        }
        return success;
}