1 /* amigaos.c uses only amigaos APIs,
2 * as opposed to amigaio.c which mixes amigaos and perl APIs */
13 #if defined(__CLIB2__)
16 #if defined(__NEWLIB__)
17 # include <amiga_platform.h>
27 #include <proto/dos.h>
28 #include <proto/exec.h>
29 #include <proto/utility.h>
33 struct UtilityIFace *IUtility = NULL;
35 /***************************************************************************/
37 struct Interface *OpenInterface(CONST_STRPTR libname, uint32 libver)
39 struct Library *base = IExec->OpenLibrary(libname, libver);
40 struct Interface *iface = IExec->GetInterface(base, "main", 1, NULL);
43 // We should probably post some kind of error message here.
45 IExec->CloseLibrary(base);
51 /***************************************************************************/
53 void CloseInterface(struct Interface *iface)
57 struct Library *base = iface->Data.LibBase;
58 IExec->DropInterface(iface);
59 IExec->CloseLibrary(base);
63 BOOL __unlink_retries = FALSE;
65 void ___makeenviron() __attribute__((constructor));
66 void ___freeenviron() __attribute__((destructor));
68 void ___openinterfaces() __attribute__((constructor));
69 void ___closeinterfaces() __attribute__((destructor));
71 void ___openinterfaces()
74 IDOS = (struct DOSIFace *)OpenInterface("dos.library", 53);
77 (struct UtilityIFace *)OpenInterface("utility.library", 53);
80 void ___closeinterfaces()
82 CloseInterface((struct Interface *)IDOS);
83 CloseInterface((struct Interface *)IUtility);
85 int VARARGS68K araddebug(UBYTE *fmt, ...);
86 int VARARGS68K adebug(UBYTE *fmt, ...);
88 #define __USE_RUNCOMMAND__
90 char **myenviron = NULL;
91 char **origenviron = NULL;
93 static void createvars(char **envp);
105 int __myrc(__attribute__((unused))char *arg)
107 struct Task *thisTask = IExec->FindTask(0);
108 struct args *myargs = (struct args *)thisTask->tc_UserData;
110 createvars(myargs->envp);
111 // adebug("%s %ld %s \n",__FUNCTION__,__LINE__,myargs->command);
112 myargs->result = IDOS->RunCommand(myargs->seglist, myargs->stack,
113 myargs->command, myargs->length);
118 BPTR seglist, int stack, char *command, int length, char **envp)
121 struct Task *thisTask = IExec->FindTask(0);
122 struct Process *proc;
124 // adebug("%s %ld %s\n",__FUNCTION__,__LINE__,command?command:"NULL");
126 myargs.seglist = seglist;
127 myargs.stack = stack;
128 myargs.command = command;
129 myargs.length = length;
133 if ((proc = IDOS->CreateNewProcTags(
134 NP_Entry, __myrc, NP_Child, TRUE, NP_Input, IDOS->Input(),
135 NP_Output, IDOS->Output(), NP_Error, IDOS->ErrorOutput(),
136 NP_CloseInput, FALSE, NP_CloseOutput, FALSE, NP_CloseError,
137 FALSE, NP_CopyVars, FALSE,
139 // NP_StackSize, ((struct Process
140 // *)myargs.parent)->pr_StackSize,
141 NP_Cli, TRUE, NP_UserData, (int)&myargs,
142 NP_NotifyOnDeathSigTask, thisTask, TAG_DONE)))
145 IExec->Wait(SIGF_CHILD);
147 return myargs.result;
150 char *mystrdup(const char *s)
155 size = strlen(s) + 1;
157 if ((result = (char *)IExec->AllocVecTags(size, TAG_DONE)))
159 memmove(result, s, size);
164 unsigned int pipenum = 0;
166 int pipe(int filedes[2])
168 char pipe_name[1024];
170 // adebug("%s %ld \n",__FUNCTION__,__LINE__);
172 sprintf(pipe_name, "/T/%x.%08lx", pipenum++, IUtility->GetUniqueID());
174 sprintf(pipe_name, "/PIPE/%x%08lx/4096/0", pipenum++,
175 IUtility->GetUniqueID());
178 /* printf("pipe: %s \n", pipe_name);*/
180 filedes[1] = open(pipe_name, O_WRONLY | O_CREAT);
181 filedes[0] = open(pipe_name, O_RDONLY);
182 if (filedes[0] == -1 || filedes[1] == -1)
184 if (filedes[0] != -1)
186 if (filedes[1] != -1)
190 /* printf("filedes %d %d\n", filedes[0],
191 * filedes[1]);fflush(stdout);*/
198 fprintf(stderr, "Can not bloody fork\n");
203 int wait(__attribute__((unused))int *status)
205 fprintf(stderr, "No wait try waitpid instead\n");
210 char *convert_path_a2u(const char *filename)
212 struct NameTranslationInfo nti;
219 __translate_amiga_to_unix_path_name(&filename, &nti);
221 return mystrdup(filename);
223 char *convert_path_u2a(const char *filename)
225 struct NameTranslationInfo nti;
232 if (strcmp(filename, "/dev/tty") == 0)
234 return mystrdup("CONSOLE:");
238 __translate_unix_to_amiga_path_name(&filename, &nti);
240 return mystrdup(filename);
243 struct SignalSemaphore environ_sema;
244 struct SignalSemaphore popen_sema;
247 void amigaos4_init_environ_sema()
249 IExec->InitSemaphore(&environ_sema);
250 IExec->InitSemaphore(&popen_sema);
253 void amigaos4_obtain_environ()
255 IExec->ObtainSemaphore(&environ_sema);
258 void amigaos4_release_environ()
260 IExec->ReleaseSemaphore(&environ_sema);
263 static void createvars(char **envp)
267 /* Set a local var to indicate to any subsequent sh that it is
269 * the top level shell and so should only inherit local amigaos
271 IDOS->SetVar("ABCSH_IMPORT_LOCAL", "TRUE", 5, GVF_LOCAL_ONLY);
273 amigaos4_obtain_environ();
277 while ((envp != NULL) && (*envp != NULL))
282 if ((len = strlen(*envp)))
284 if ((var = (char *)IExec->AllocVecTags(len + 1, AVT_ClearWithValue,0,TAG_DONE)))
288 val = strchr(var, '=');
305 amigaos4_release_environ();
317 int myexecvp(bool isperlthread, const char *filename, char *argv[])
320 //%s\n",__FUNCTION__,__LINE__,filename?filename:"NULL");
321 /* if there's a slash or a colon consider filename a path and skip
325 char *pathpart = NULL;
326 if ((strchr(filename, '/') == NULL) && (strchr(filename, ':') == NULL))
333 if (!(path = getenv("PATH")))
335 path = ".:/bin:/usr/bin:/c";
338 len = strlen(filename) + 1;
339 name = (char *)IExec->AllocVecTags(strlen(path) + len, AVT_ClearWithValue,0,AVT_Type,MEMF_SHARED,TAG_DONE);
340 pathpart = (char *)IExec->AllocVecTags(strlen(path) + 1, AVT_ClearWithValue,0,AVT_Type,MEMF_SHARED,TAG_DONE);
346 if (!(p = strchr(path, ':')))
348 p = strchr(path, '\0');
351 memcpy(pathpart, path, p - path);
352 pathpart[p - path] = '\0';
353 if (!(strlen(pathpart) == 0))
355 sprintf(name, "%s/%s", pathpart, filename);
358 sprintf(name, "%s", filename);
360 if ((stat(name, &st) == 0) && (S_ISREG(st.st_mode)))
362 /* we stated it and it's a regular file */
369 while (*p++ != '\0');
372 res = myexecve(isperlthread, filename, argv, myenviron);
376 IExec->FreeVec((APTR)name);
381 IExec->FreeVec((APTR)pathpart);
387 int myexecv(bool isperlthread, const char *path, char *argv[])
389 return myexecve(isperlthread, path, argv, myenviron);
392 int myexecl(bool isperlthread, const char *path, ...)
395 char *argv[1024]; /* 1024 enough? let's hope so! */
397 // adebug("%s %ld\n",__FUNCTION__,__LINE__);
404 argv[i] = va_arg(va, char *);
406 while (argv[i++] != NULL);
409 return myexecve(isperlthread, path, argv, myenviron);
414 fprintf(stderr, "Pause not implemented\n");
420 uint32 size_env(struct Hook *hook, __attribute__((unused))APTR userdata, struct ScanVarsMsg *message)
422 if (strlen(message->sv_GDir) <= 4)
424 hook->h_Data = (APTR)(((uint32)hook->h_Data) + 1);
429 uint32 copy_env(struct Hook *hook, __attribute__((unused))APTR userdata, struct ScanVarsMsg *message)
431 if (strlen(message->sv_GDir) <= 4)
433 char **env = (char **)hook->h_Data;
435 strlen(message->sv_Name) + 1 + message->sv_VarLen + 1 + 1;
436 char *buffer = (char *)IExec->AllocVecTags((uint32)size,AVT_ClearWithValue,0,TAG_DONE);
439 snprintf(buffer, size - 1, "%s=%s", message->sv_Name,
449 void ___makeenviron()
451 struct Hook *hook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,TAG_DONE);
458 struct DOSIFace *myIDOS =
459 (struct DOSIFace *)OpenInterface("dos.library", 53);
463 if (myIDOS->GetVar("ABCSH_IMPORT_LOCAL", varbuf, 8,
466 flags = GVF_LOCAL_ONLY;
470 flags = GVF_GLOBAL_ONLY;
473 hook->h_Entry = size_env;
476 myIDOS->ScanVars(hook, flags, 0);
477 size = ((uint32)hook->h_Data) + 1;
479 myenviron = (char **)IExec->AllocVecTags(size *
481 AVT_ClearWithValue,0,TAG_DONE);
482 origenviron = myenviron;
485 IExec->FreeSysObject(ASOT_HOOK,hook);
486 CloseInterface((struct Interface *)myIDOS);
489 hook->h_Entry = copy_env;
490 hook->h_Data = myenviron;
492 myIDOS->ScanVars(hook, flags, 0);
493 IExec->FreeSysObject(ASOT_HOOK,hook);
494 CloseInterface((struct Interface *)myIDOS);
499 void ___freeenviron()
502 /* perl might change environ, it puts it back except for ctrl-c */
503 /* so restore our own copy here */
504 struct DOSIFace *myIDOS =
505 (struct DOSIFace *)OpenInterface("dos.library", 53);
508 myenviron = origenviron;
512 for (i = myenviron; *i != NULL; i++)
516 IExec->FreeVec(myenviron);
519 CloseInterface((struct Interface *)myIDOS);
524 /* Work arround for clib2 fstat */
526 #define S_IFCHR 0x0020000
529 #define SET_FLAG(u, v) ((void)((u) |= (v)))
531 int afstat(int fd, struct stat *statb)
537 /* In the first instance pass it to fstat */
538 // adebug("fd %ld ad %ld\n",fd,amigaos_get_file(fd));
540 if ((result = fstat(fd, statb) >= 0))
543 /* Now we've got a file descriptor but we failed to stat it */
544 /* Could be a nil: or could be a std#? */
546 /* if get_default_file fails we had a dud fd so return failure */
547 #if !defined(__CLIB2__)
549 fh = amigaos_get_file(fd);
551 /* if nil: return failure*/
555 /* Now compare with our process Input() Output() etc */
556 /* if these were regular files sockets or pipes we had already
558 /* so we can guess they a character special console.... I hope */
560 struct ExamineData *data;
564 data = IDOS->ExamineObjectTags(EX_FileHandleInput, fh, TAG_END);
568 IUtility->Strlcpy(name, data->Name, sizeof(name));
570 IDOS->FreeDosObject(DOS_EXAMINEDATA, data);
573 // adebug("ad %ld '%s'\n",amigaos_get_file(fd),name);
576 if (fh == IDOS->Input())
579 SET_FLAG(mode, S_IRUSR);
580 SET_FLAG(mode, S_IRGRP);
581 SET_FLAG(mode, S_IROTH);
583 else if (fh == IDOS->Output() || fh == IDOS->ErrorOutput())
586 SET_FLAG(mode, S_IWUSR);
587 SET_FLAG(mode, S_IWGRP);
588 SET_FLAG(mode, S_IWOTH);
592 /* we got a filehandle not handle by fstat or the above */
593 /* most likely it's NIL: but lets check */
594 struct ExamineData *exd = NULL;
595 if ((exd = IDOS->ExamineObjectTags(EX_FileHandleInput, fh,
600 (20060920)) // Ugh yes I know nasty.....
604 IDOS->FreeDosObject(DOS_EXAMINEDATA, exd);
607 /* yep we got NIL: */
608 SET_FLAG(mode, S_IRUSR);
609 SET_FLAG(mode, S_IRGRP);
610 SET_FLAG(mode, S_IROTH);
611 SET_FLAG(mode, S_IWUSR);
612 SET_FLAG(mode, S_IWGRP);
613 SET_FLAG(mode, S_IWOTH);
618 "unhandled filehandle in afstat()\n");
624 memset(statb, 0, sizeof(statb));
626 statb->st_mode = mode;
632 BPTR amigaos_get_file(int fd)
634 BPTR fh = (BPTR)NULL;
635 if (!(fh = _get_osfhandle(fd)))
646 fh = IDOS->ErrorOutput();
655 /*########################################################################*/
657 #define LOCK_START 0xFFFFFFFFFFFFFFFELL
658 #define LOCK_LENGTH 1LL
660 // No wait forever option so lets wait for a loooong time.
661 #define TIMEOUT 0x7FFFFFFF
663 int amigaos_flock(int fd, int oper)
668 if (!(fh = amigaos_get_file(fd)))
678 if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
679 REC_SHARED | RECF_DOS_METHOD_ONLY,
688 if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
689 REC_EXCLUSIVE | RECF_DOS_METHOD_ONLY,
696 case LOCK_SH | LOCK_NB:
698 if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
699 REC_SHARED_IMMED | RECF_DOS_METHOD_ONLY,
710 case LOCK_EX | LOCK_NB:
712 if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH,
713 REC_EXCLUSIVE_IMMED | RECF_DOS_METHOD_ONLY,
726 if (IDOS->UnLockRecord(fh, LOCK_START, LOCK_LENGTH))