Document [string map]
[jimtcl.git] / jim-win32compat.c
blob9eb8e7fb596760895af88d36d0417e21da189ef5
1 #include "jim.h"
2 #include "jimautoconf.h"
4 #if defined(HAVE_DLOPEN_COMPAT)
5 void *dlopen(const char *path, int mode)
7 JIM_NOTUSED(mode);
9 return (void *)LoadLibraryA(path);
12 int dlclose(void *handle)
14 FreeLibrary((HANDLE)handle);
15 return 0;
18 void *dlsym(void *handle, const char *symbol)
20 return GetProcAddress((HMODULE)handle, symbol);
23 char *dlerror(void)
25 static char msg[121];
26 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
27 LANG_NEUTRAL, msg, sizeof(msg) - 1, NULL);
28 return msg;
30 #endif
32 #ifdef _MSC_VER
33 /* POSIX gettimeofday() compatibility for WIN32 */
34 int gettimeofday(struct timeval *tv, void *unused)
36 struct _timeb tb;
38 _ftime(&tb);
39 tv->tv_sec = tb.time;
40 tv->tv_usec = tb.millitm * 1000;
42 return 0;
45 /* Posix dirent.h compatiblity layer for WIN32.
46 * Copyright Kevlin Henney, 1997, 2003. All rights reserved.
47 * Copyright Salvatore Sanfilippo ,2005.
49 * Permission to use, copy, modify, and distribute this software and its
50 * documentation for any purpose is hereby granted without fee, provided
51 * that this copyright and permissions notice appear in all copies and
52 * derivatives.
54 * This software is supplied "as is" without express or implied warranty.
55 * This software was modified by Salvatore Sanfilippo for the Jim Interpreter.
58 DIR *opendir(const char *name)
60 DIR *dir = 0;
62 if (name && name[0]) {
63 size_t base_length = strlen(name);
64 const char *all = /* search pattern must end with suitable wildcard */
65 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
67 if ((dir = (DIR *) Jim_Alloc(sizeof *dir)) != 0 &&
68 (dir->name = (char *)Jim_Alloc(base_length + strlen(all) + 1)) != 0) {
69 strcat(strcpy(dir->name, name), all);
71 if ((dir->handle = (long)_findfirst(dir->name, &dir->info)) != -1)
72 dir->result.d_name = 0;
73 else { /* rollback */
74 Jim_Free(dir->name);
75 Jim_Free(dir);
76 dir = 0;
79 else { /* rollback */
80 Jim_Free(dir);
81 dir = 0;
82 errno = ENOMEM;
85 else {
86 errno = EINVAL;
88 return dir;
91 int closedir(DIR * dir)
93 int result = -1;
95 if (dir) {
96 if (dir->handle != -1)
97 result = _findclose(dir->handle);
98 Jim_Free(dir->name);
99 Jim_Free(dir);
101 if (result == -1) /* map all errors to EBADF */
102 errno = EBADF;
103 return result;
106 struct dirent *readdir(DIR * dir)
108 struct dirent *result = 0;
110 if (dir && dir->handle != -1) {
111 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
112 result = &dir->result;
113 result->d_name = dir->info.name;
116 else {
117 errno = EBADF;
119 return result;
121 #endif