Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / AcessNative / libacess-native.so_src / main.c
1 /*
2  */
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 extern int      giSyscall_ClientID;
10 extern void     Request_Preinit(void);
11 extern int      acess__SysOpen(const char *Path, unsigned int flags);
12
13 #ifdef __WINDOWS__
14 int DllMain(void)
15 {
16         fprintf(stderr, "TODO: Windows libacessnative setup\n");
17         return 0;
18 }
19
20 #endif
21
22 #ifdef __linux__
23 int __attribute__ ((constructor(102))) libacessnative_init(int argc, char *argv[], char **envp);
24
25 const char *getenv_p(char **envp, const char *name)
26 {
27         size_t  namelen = strlen(name);
28         for(; *envp; envp ++)
29         {
30                 if( strncmp(*envp, name, namelen) != 0 )
31                         continue ;
32                 if( (*envp)[namelen] != '=' )
33                         continue ;
34                 return (*envp)+namelen+1;
35         }
36         return 0;
37 }
38
39 int libacessnative_init(int argc, char *argv[], char **envp)
40 {
41         Request_Preinit();
42         
43         const char *preopens = getenv_p(envp, "AN_PREOPEN");
44         printf("preopens = %s\n", preopens);
45         if( preopens )
46         {
47                 while( *preopens )
48                 {
49                         const char *splitter = strchr(preopens, ':');
50                         size_t  len;
51                         if( !splitter ) {
52                                 len = strlen(preopens);
53                         }
54                         else {
55                                 len = splitter - preopens;
56                         }
57                         char path[len+1];
58                         memcpy(path, preopens, len);
59                         path[len] = 0;
60                         int fd = acess__SysOpen(path, 6);       // WRITE,READ,no EXEC
61                         if( fd == -1 ) {
62                                 fprintf(stderr, "Unable to preopen '%s'\n", path);
63                         }
64                         
65                         if( !splitter )
66                                 break;
67                         preopens = splitter + 1;
68                 }
69         }
70
71 //      if( !getenv("ACESSNATIVE_ID")
72         
73         return 0;
74 }
75 #endif
76
77
78 void Debug(const char *format, ...)
79 {
80         va_list args;
81         printf("Debug: ");
82         va_start(args, format);
83         vfprintf(stdout, format, args);
84         va_end(args);
85         printf("\n");
86 }
87
88 void Warning(const char *format, ...)
89 {
90         va_list args;
91         printf("Warning: ");
92         va_start(args, format);
93         vfprintf(stdout, format, args);
94         va_end(args);
95         printf("\n");
96 }
97
98 void __libc_csu_fini()
99 {
100 }
101
102 void __libc_csu_init()
103 {
104 }
105

UCC git Repository :: git.ucc.asn.au