jimregexp: rename local regex functions
[jimtcl.git] / jim-win32compat.c
blob94bd831785b6c741136c899097ccc46fd71cfd63
1 #include "jim.h"
2 #include "jimautoconf.h"
4 #if defined(_WIN32) || defined(WIN32)
5 #ifndef STRICT
6 #define STRICT
7 #endif
8 #define WIN32_LEAN_AND_MEAN
9 #include <windows.h>
11 #if defined(HAVE_DLOPEN_COMPAT)
12 void *dlopen(const char *path, int mode)
14 JIM_NOTUSED(mode);
16 return (void *)LoadLibraryA(path);
19 int dlclose(void *handle)
21 FreeLibrary((HANDLE)handle);
22 return 0;
25 void *dlsym(void *handle, const char *symbol)
27 return GetProcAddress((HMODULE)handle, symbol);
30 char *dlerror(void)
32 static char msg[121];
33 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
34 LANG_NEUTRAL, msg, sizeof(msg) - 1, NULL);
35 return msg;
37 #endif
39 #ifdef _MSC_VER
41 #include <sys/timeb.h>
43 /* POSIX gettimeofday() compatibility for WIN32 */
44 int gettimeofday(struct timeval *tv, void *unused)
46 struct _timeb tb;
48 _ftime(&tb);
49 tv->tv_sec = tb.time;
50 tv->tv_usec = tb.millitm * 1000;
52 return 0;
55 /* Posix dirent.h compatiblity layer for WIN32.
56 * Copyright Kevlin Henney, 1997, 2003. All rights reserved.
57 * Copyright Salvatore Sanfilippo ,2005.
59 * Permission to use, copy, modify, and distribute this software and its
60 * documentation for any purpose is hereby granted without fee, provided
61 * that this copyright and permissions notice appear in all copies and
62 * derivatives.
64 * This software is supplied "as is" without express or implied warranty.
65 * This software was modified by Salvatore Sanfilippo for the Jim Interpreter.
68 DIR *opendir(const char *name)
70 DIR *dir = 0;
72 if (name && name[0]) {
73 size_t base_length = strlen(name);
74 const char *all = /* search pattern must end with suitable wildcard */
75 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
77 if ((dir = (DIR *) Jim_Alloc(sizeof *dir)) != 0 &&
78 (dir->name = (char *)Jim_Alloc(base_length + strlen(all) + 1)) != 0) {
79 strcat(strcpy(dir->name, name), all);
81 if ((dir->handle = (long)_findfirst(dir->name, &dir->info)) != -1)
82 dir->result.d_name = 0;
83 else { /* rollback */
84 Jim_Free(dir->name);
85 Jim_Free(dir);
86 dir = 0;
89 else { /* rollback */
90 Jim_Free(dir);
91 dir = 0;
92 errno = ENOMEM;
95 else {
96 errno = EINVAL;
98 return dir;
101 int closedir(DIR * dir)
103 int result = -1;
105 if (dir) {
106 if (dir->handle != -1)
107 result = _findclose(dir->handle);
108 Jim_Free(dir->name);
109 Jim_Free(dir);
111 if (result == -1) /* map all errors to EBADF */
112 errno = EBADF;
113 return result;
116 struct dirent *readdir(DIR * dir)
118 struct dirent *result = 0;
120 if (dir && dir->handle != -1) {
121 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
122 result = &dir->result;
123 result->d_name = dir->info.name;
126 else {
127 errno = EBADF;
129 return result;
131 #endif
132 #endif