On i386-darwin, some POSIX conformant versions of functions have a
[AROS.git] / arch / all-unix / filesys / emul_handler / emul_host_unix.c
blobc36055f2efd958a221b5456bb829b01cd30109ee
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "unix_hints.h"
8 #ifdef HOST_LONG_ALIGNED
9 #pragma pack(4)
10 #endif
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <sys/types.h>
18 #pragma pack()
20 /* This prevents redefinition of struct timeval */
21 #define _AROS_TYPES_TIMEVAL_S_H_
23 #include <aros/debug.h>
24 #include <aros/symbolsets.h>
25 #include <dos/dosasl.h>
26 #include <utility/date.h>
27 #include <proto/dos.h>
28 #include <proto/exec.h>
29 #include <proto/hostlib.h>
30 #include <proto/oop.h>
31 #include <proto/utility.h>
33 #include "emul_intern.h"
34 #include "emul_unix.h"
36 #define NO_CASE_SENSITIVITY
38 #ifdef DEBUG_INTERFACE
39 #define DUMP_INTERFACE \
40 { \
41 int i; \
42 APTR *iface = (APTR *)emulbase->pdata.SysIFace; \
44 for (i = 0; libcSymbols[i]; i++) \
45 bug("%s\t\t0x%p\n", libcSymbols[i], iface[i]); \
47 #else
48 #define DUMP_INTERFACE
49 #endif
51 static const char *libcSymbols[] =
53 "open" UNIX2003_SUFFIX,
54 "close" UNIX2003_SUFFIX,
55 "closedir" UNIX2003_SUFFIX,
56 "opendir" UNIX2003_SUFFIX,
57 "readdir" INODE64_SUFFIX,
58 "rewinddir" UNIX2003_SUFFIX,
59 "read" UNIX2003_SUFFIX,
60 "write" UNIX2003_SUFFIX,
61 "lseek",
62 "ftruncate",
63 "mkdir",
64 "rmdir",
65 "unlink",
66 "link",
67 "symlink",
68 "readlink",
69 "rename",
70 "chmod" UNIX2003_SUFFIX,
71 "isatty",
72 "statfs",
73 "utime",
74 "localtime",
75 "mktime" UNIX2003_SUFFIX,
76 "getcwd",
77 "getenv",
78 "poll" UNIX2003_SUFFIX,
79 #ifdef HOST_OS_linux
80 "__xstat",
81 "__lxstat",
82 #else
83 "stat" INODE64_SUFFIX,
84 "lstat" INODE64_SUFFIX,
85 #endif
86 #ifndef HOST_OS_android
87 "seekdir" UNIX2003_SUFFIX,
88 "telldir" UNIX2003_SUFFIX,
89 "getpwent",
90 "endpwent",
91 #endif
92 NULL
95 /*********************************************************************************************/
97 static inline struct filehandle *CreateStdHandle(int fd)
99 struct filehandle *fh;
101 fh = AllocMem(sizeof(struct filehandle), MEMF_PUBLIC|MEMF_CLEAR);
102 if (fh)
104 fh->type = FHD_FILE|FHD_STDIO;
105 fh->fd = (void *)(IPTR)fd;
108 return fh;
111 static int host_startup(struct emulbase *emulbase)
113 ULONG r = 0;
115 OOPBase = OpenLibrary("oop.library", 0);
116 if (!OOPBase)
117 return FALSE;
119 UtilityBase = OpenLibrary("utility.library", 0);
120 D(bug("[EmulHandler] UtilityBase = %p\n", UtilityBase));
121 if (!UtilityBase)
122 return FALSE;
124 emulbase->pdata.em_UnixIOBase = (struct UnixIOBase *)OpenLibrary("unixio.hidd", 43);
125 if (!emulbase->pdata.em_UnixIOBase)
126 return FALSE;
128 emulbase->pdata.unixio = OOP_NewObject(NULL, CLID_Hidd_UnixIO, NULL);
129 if (!emulbase->pdata.unixio)
130 return FALSE;
132 emulbase->pdata.SysIFace = (struct LibCInterface *)HostLib_GetInterface(emulbase->pdata.em_UnixIOBase->uio_LibcHandle, libcSymbols, &r);
133 if (!emulbase->pdata.SysIFace)
135 D(bug("[EmulHandler] Unable to get host-side library interface!\n"));
136 CloseLibrary(UtilityBase);
137 return FALSE;
140 D(bug("[EmulHandler] %lu unresolved symbols\n", r));
141 DUMP_INTERFACE
142 if (r)
144 CloseLibrary(UtilityBase);
145 return FALSE;
148 /* Cache errno pointer for faster access */
149 emulbase->pdata.errnoPtr = emulbase->pdata.em_UnixIOBase->uio_ErrnoPtr;
151 /* Create handles for emergency console */
152 emulbase->eb_stdin = CreateStdHandle(STDIN_FILENO);
153 emulbase->eb_stdout = CreateStdHandle(STDOUT_FILENO);
154 emulbase->eb_stderr = CreateStdHandle(STDERR_FILENO);
156 return TRUE;
159 ADD2INITLIB(host_startup, 0);
161 static int host_cleanup(struct emulbase *emulbase)
163 D(bug("[EmulHandler] Expunge\n"));
165 if (emulbase->pdata.SysIFace)
166 HostLib_DropInterface((APTR *)emulbase->pdata.SysIFace);
168 /* UnixIO v42 object is a singletone, we don't need to dispose it */
170 CloseLibrary(OOPBase);
171 CloseLibrary(&emulbase->pdata.em_UnixIOBase->uio_Library);
172 CloseLibrary(UtilityBase);
174 return TRUE;
177 ADD2EXPUNGELIB(host_cleanup, 0);