Add support for implementing LDB modules in Python.
[ira/wip.git] / source4 / lib / appweb / ejs-2.0 / mpr / UNIX / mprPlatform.c
1 /**
2  *      @file   mprPlatform.c
3  *      @brief  Cross platform routines 
4  *      @overview This module provides low level cross platform routines.
5  *      @remarks Most routines in this file are not thread-safe. It is the callers 
6  *              responsibility to perform all thread synchronization.
7  */
8
9 /*
10  *      @copy   default
11  *      
12  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
13  *      
14  *      This software is distributed under commercial and open source licenses.
15  *      You may use the GPL open source license described below or you may acquire 
16  *      a commercial license from Mbedthis Software. You agree to be fully bound 
17  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
18  *      this software for full details.
19  *      
20  *      This software is open source; you can redistribute it and/or modify it 
21  *      under the terms of the GNU General Public License as published by the 
22  *      Free Software Foundation; either version 2 of the License, or (at your 
23  *      option) any later version. See the GNU General Public License for more 
24  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
25  *      
26  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
27  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
28  *      
29  *      This GPL license does NOT permit incorporating this software into 
30  *      proprietary programs. If you are unable to comply with the GPL, you must
31  *      acquire a commercial license to use this software. Commercial licenses 
32  *      for this software and support services are available from Mbedthis 
33  *      Software at http://www.mbedthis.com 
34  *      
35  *      @end
36  */
37
38 /********************************** Includes **********************************/
39 /*
40  *      We need to use the underlying str(cpy) routines to implement our safe
41  *      alternatives
42  */
43 #if !DOXYGEN
44 #define         UNSAFE_FUNCTIONS_OK 1
45 #endif
46
47 #include        "mpr.h"
48
49 /************************************ Code ************************************/
50
51 char *mprInetToStr(char *buffer, int bufsize, const struct in_addr in)
52 {
53 #if HAVE_NTOA_R
54         inet_ntoa_r(in, buffer, bufsize);
55 #else
56         uchar   *cp;
57         /*      FUTURE -- this is not portable */
58         cp = (uchar*) ∈
59         mprSprintf(buffer, bufsize, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
60 #endif
61         return buffer;
62 }
63
64 /******************************************************************************/
65
66 void mprSetShell(MprCtx ctx, void *shell)
67 {
68 }
69
70 /******************************************************************************/
71
72 void *mprGetShell(MprCtx ctx)
73 {
74         return 0;
75 }
76
77 /******************************************************************************/
78 /*
79  *      Sleep. Period given in milliseconds.
80  */
81
82 void mprSleep(MprCtx ctx, int milliseconds)
83 {
84         struct timespec timeout;
85         int                             rc;
86
87         mprAssert(milliseconds >= 0);
88         timeout.tv_sec = milliseconds / 1000;
89         timeout.tv_nsec = (milliseconds % 1000) * 1000000;
90         do {
91                 rc = nanosleep(&timeout, 0);
92         } while (rc < 0 && errno == EINTR);
93 }
94
95 /******************************************************************************/
96 /*
97  *      Make intervening directories
98  */
99
100 int mprMakeDirPath(MprCtx ctx, const char *path)
101 {
102         char    dir[MPR_MAX_PATH], buf[MPR_MAX_PATH];
103         char    *dirSep;
104         char    *next, *tok;
105
106         dir[0] = '\0';
107         dirSep = "/\\";
108
109         if (path == 0 || *path == '\0') {
110                 return MPR_ERR_BAD_ARGS;
111         }
112
113         mprStrcpy(buf, sizeof(buf), path);
114         next = mprStrTok(buf, dirSep, &tok);
115         if (*buf == '/') {
116                 dir[0] = '/';
117         }
118         while (next != NULL) {
119                 if (strcmp(next, ".") == 0 ) {
120                         next = mprStrTok(NULL, dirSep, &tok);
121                         continue;
122                 }
123                 strcat(dir, next);
124                 if (access(dir, R_OK) != 0) {
125                         if (mkdir(dir, 0666) < 0) {
126                                 return MPR_ERR_CANT_CREATE;
127                         }
128                 }
129                 strcat(dir, "/");
130                 next = mprStrTok(NULL, dirSep, &tok);
131         }
132         return 0;
133 }
134
135 /******************************************************************************/
136 /*
137  *      Get a fully qualified file name for the given path. Return with forward
138  *      slashes always
139  */
140
141 char *mprGetFullPathName(char *buf, int buflen, const char *path)
142 {
143         if (mprStrcpy(buf, buflen, path) < 0) {
144                 mprAssert(0);
145                 return 0;
146         }
147         return buf;
148 }
149
150 /******************************************************************************/
151 /*
152  *      Replacement for gethostbyname that is multi-thread safe
153  */
154
155 struct hostent *mprGetHostByName(MprCtx ctx, const char *name)
156 {
157         MprApp                  *app;
158         struct hostent  *hp;
159         struct hostent  *ip;
160         int                             count, i;
161
162         hp = (struct hostent*) mprAlloc(ctx, sizeof(struct hostent));
163         memset(hp, 0, sizeof(struct hostent));
164
165         app = mprGetApp(ctx);
166
167         #undef gethostbyname
168
169         mprGlobalLock(app);
170         ip = gethostbyname(name);
171         mprGlobalUnlock(app);
172
173         if (ip == 0) {
174                 return 0;
175         }
176         hp->h_addrtype = ip->h_addrtype;
177         hp->h_length = ip->h_length;
178         hp->h_name = mprStrdup(hp, ip->h_name);
179         hp->h_addr_list = 0;
180         hp->h_aliases = 0;
181
182         for (count = 0; ip->h_addr_list[count] != 0; ) {
183                 count++;
184         }
185         if (count > 0) {
186                 count++;
187                 hp->h_addr_list = mprAlloc(hp, count * sizeof(char*));
188                 for (i = 0; ip->h_addr_list[i] != 0; i++) {
189                         memcpy(&hp->h_addr_list[i], &ip->h_addr_list[i], ip->h_length);
190                 }
191                 hp->h_addr_list[i] = 0;
192         }
193
194         for (count = 0; ip->h_aliases[count] != 0; ) {
195                 count++;
196         }
197         if (count > 0) {
198                 count++;
199                 hp->h_aliases = mprAlloc(hp, count * sizeof(char*));
200                 for (i = 0; ip->h_aliases[i] != 0; i++) {
201                         hp->h_aliases[i] = mprStrdup(hp, ip->h_aliases[i]);
202                 }
203                 hp->h_aliases[i] = 0;
204         }
205         return hp;
206 }
207
208 /******************************************************************************/
209
210 /*
211  * Local variables:
212  * tab-width: 4
213  * c-basic-offset: 4
214  * End:
215  * vim:tw=78
216  * vim600: sw=4 ts=4 fdm=marker
217  * vim<600: sw=4 ts=4
218  */