osdep.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. osdep.c -- O/S dependant code
  3. Copyright (c) All Rights Reserved. See details at the end of the file.
  4. */
  5. /*********************************** Includes *********************************/
  6. #include "goahead.h"
  7. /*********************************** Defines **********************************/
  8. #if ME_WIN_LIKE
  9. static HINSTANCE appInstance;
  10. PUBLIC void syslog(int priority, char *fmt, ...);
  11. #endif
  12. /************************************* Code ***********************************/
  13. PUBLIC int websOsOpen()
  14. {
  15. #if SOLARIS
  16. openlog(ME_NAME, LOG_LOCAL0);
  17. #elif ME_UNIX_LIKE
  18. openlog(ME_NAME, 0, LOG_LOCAL0);
  19. #endif
  20. #if WINDOWS || VXWORKS || TIDSP
  21. rand();
  22. #else
  23. random();
  24. #endif
  25. return 0;
  26. }
  27. PUBLIC void websOsClose()
  28. {
  29. #if ME_UNIX_LIKE
  30. closelog();
  31. #endif
  32. }
  33. PUBLIC char *websTempFile(char *dir, char *prefix)
  34. {
  35. static int count = 0;
  36. char sep;
  37. sep = '/';
  38. if (!dir || *dir == '\0') {
  39. #if WINCE
  40. dir = "/Temp";
  41. sep = '\\';
  42. #elif ME_WIN_LIKE
  43. dir = getenv("TEMP");
  44. sep = '\\';
  45. #elif VXWORKS
  46. dir = ".";
  47. #else
  48. dir = "/tmp";
  49. #endif
  50. }
  51. if (!prefix) {
  52. prefix = "tmp";
  53. }
  54. return sfmt("%s%c%s-%d.tmp", dir, sep, prefix, count++);
  55. }
  56. #if VXWORKS
  57. /*
  58. Get absolute path. In VxWorks, functions like chdir, ioctl for mkdir and ioctl for rmdir, require an absolute path.
  59. This function will take the path argument and convert it to an absolute path. It is the caller's responsibility to
  60. deallocate the returned string.
  61. */
  62. static char *getAbsolutePath(char *path)
  63. {
  64. #if _WRS_VXWORKS_MAJOR >= 6
  65. const char *tail;
  66. #else
  67. char *tail;
  68. #endif
  69. char *dev;
  70. /*
  71. Determine if path is relative or absolute. If relative, prepend the current working directory to the name.
  72. Otherwise, use it. Note the getcwd call below must not be getcwd or else we go into an infinite loop
  73. */
  74. if (iosDevFind(path, &tail) != NULL && path != tail) {
  75. return sclone(path);
  76. }
  77. dev = walloc(ME_GOAHEAD_LIMIT_FILENAME);
  78. #if ME_ROM
  79. dev[0] = '\0';
  80. #else
  81. getcwd(dev, ME_GOAHEAD_LIMIT_FILENAME);
  82. #endif
  83. strcat(dev, "/");
  84. strcat(dev, path);
  85. return dev;
  86. }
  87. PUBLIC int vxchdir(char *dirname)
  88. {
  89. char *path;
  90. int rc;
  91. path = getAbsolutePath(dirname);
  92. #undef chdir
  93. rc = chdir(path);
  94. wfree(path);
  95. return rc;
  96. }
  97. #endif
  98. #if ECOS
  99. PUBLIC int send(int s, const void *buf, size_t len, int flags)
  100. {
  101. return write(s, buf, len);
  102. }
  103. PUBLIC int recv(int s, void *buf, size_t len, int flags)
  104. {
  105. return read(s, buf, len);
  106. }
  107. #endif
  108. #if ME_WIN_LIKE
  109. PUBLIC void websSetInst(HINSTANCE inst)
  110. {
  111. appInstance = inst;
  112. }
  113. HINSTANCE websGetInst()
  114. {
  115. return appInstance;
  116. }
  117. PUBLIC void syslog(int priority, char *fmt, ...)
  118. {
  119. va_list args;
  120. HKEY hkey;
  121. void *event;
  122. long errorType;
  123. ulong exists;
  124. char *buf, logName[ME_GOAHEAD_LIMIT_STRING], *lines[9], *cp, *value;
  125. int type;
  126. static int once = 0;
  127. va_start(args, fmt);
  128. buf = sfmtv(fmt, args);
  129. va_end(args);
  130. cp = &buf[slen(buf) - 1];
  131. while (*cp == '\n' && cp > buf) {
  132. *cp-- = '\0';
  133. }
  134. type = EVENTLOG_ERROR_TYPE;
  135. lines[0] = buf;
  136. lines[1] = 0;
  137. lines[2] = lines[3] = lines[4] = lines[5] = 0;
  138. lines[6] = lines[7] = lines[8] = 0;
  139. if (once == 0) {
  140. /* Initialize the registry */
  141. once = 1;
  142. hkey = 0;
  143. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, logName, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, &exists) ==
  144. ERROR_SUCCESS) {
  145. value = "%SystemRoot%\\System32\\netmsg.dll";
  146. if (RegSetValueEx(hkey, "EventMessageFile", 0, REG_EXPAND_SZ,
  147. (uchar*) value, (int) slen(value) + 1) != ERROR_SUCCESS) {
  148. RegCloseKey(hkey);
  149. wfree(buf);
  150. return;
  151. }
  152. errorType = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
  153. if (RegSetValueEx(hkey, "TypesSupported", 0, REG_DWORD, (uchar*) &errorType, sizeof(DWORD)) !=
  154. ERROR_SUCCESS) {
  155. RegCloseKey(hkey);
  156. wfree(buf);
  157. return;
  158. }
  159. RegCloseKey(hkey);
  160. }
  161. }
  162. event = RegisterEventSource(0, ME_NAME);
  163. if (event) {
  164. ReportEvent(event, EVENTLOG_ERROR_TYPE, 0, 3299, NULL, sizeof(lines) / sizeof(char*), 0, (LPCSTR*) lines, 0);
  165. DeregisterEventSource(event);
  166. }
  167. wfree(buf);
  168. }
  169. PUBLIC void sleep(int secs)
  170. {
  171. Sleep(secs / 1000);
  172. }
  173. #endif
  174. /*
  175. "basename" returns a pointer to the last component of a pathname LINUX, LynxOS and Mac OS X have their own basename
  176. */
  177. #if !ME_UNIX_LIKE
  178. PUBLIC char *basename(char *name)
  179. {
  180. char *cp;
  181. #if ME_WIN_LIKE
  182. if (((cp = strrchr(name, '\\')) == NULL) && ((cp = strrchr(name, '/')) == NULL)) {
  183. return name;
  184. #else
  185. if ((cp = strrchr(name, '/')) == NULL) {
  186. return name;
  187. #endif
  188. } else if (*(cp + 1) == '\0' && cp == name) {
  189. return name;
  190. } else if (*(cp + 1) == '\0' && cp != name) {
  191. return "";
  192. } else {
  193. return ++cp;
  194. }
  195. }
  196. #endif
  197. #if TIDSP
  198. static char _inet_result[16];
  199. char *inet_ntoa(struct in_addr addr)
  200. {
  201. uchar *bytes;
  202. bytes = (uchar*) &addr;
  203. sprintf(_inet_result, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
  204. return _inet_result;
  205. }
  206. struct hostent* gethostbyname(char *name)
  207. {
  208. static char buffer[ME_MAX_PATH];
  209. if(!DNSGetHostByName(name, buffer, ME_MAX_PATH)) {
  210. return 0;
  211. }
  212. return (struct hostent*) buffer;
  213. }
  214. ulong hostGetByName(char *name)
  215. {
  216. struct _hostent *ent;
  217. ent = gethostbyname(name);
  218. return ent->h_addr[0];
  219. }
  220. int gethostname(char *host, int bufSize)
  221. {
  222. return DNSGetHostname(host, bufSize);
  223. }
  224. int closesocket(SOCKET s)
  225. {
  226. return fdClose(s);
  227. }
  228. int select(int maxfds, fd_set *readFds, fd_set *writeFds, fd_set *exceptFds, struct timeval *timeVal)
  229. {
  230. return fdSelect(maxfds, readFds, writeFds, exceptFds, timeVal);
  231. }
  232. #endif /* TIDSP */
  233. /*
  234. Copyright (c) Embedthis Software. All Rights Reserved.
  235. This software is distributed under commercial and open source licenses.
  236. You may use the Embedthis GoAhead open source license or you may acquire
  237. a commercial license from Embedthis Software. You agree to be fully bound
  238. by the terms of either license. Consult the LICENSE.md distributed with
  239. this software for full details and other copyrights.
  240. */