jdk/src/windows/demo/jvmti/hprof/hprof_md.c
author jcoomes
Sun, 07 Jun 2009 22:08:24 -0700
changeset 2997 69cd0f7ac5d6
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6814552: par compact - some compilers fail to optimize bitmap code Reviewed-by: tonyp, iveresov, jmasa, ysr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <mmsystem.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
md_getpid(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int pid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if ( pid >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    pid = getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
md_sleep(unsigned seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    Sleep((DWORD)seconds*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
md_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
md_connect(char *hostname, unsigned short port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    struct hostent *hentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    struct sockaddr_in s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* create a socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    fd = (int)socket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* find remote host's addr from name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if ((hentry = gethostbyname(hostname)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    (void)memset((char *)&s, 0, sizeof(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* set remote host's addr; its already in network byte order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           (int)sizeof(s.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* set remote host's port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    s.sin_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    s.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* now try connecting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
md_recv(int f, char *buf, int len, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    return recv(f, buf, len, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
md_shutdown(int filedes, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return shutdown(filedes, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
md_open(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return open(filename, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
md_open_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    return open(filename, O_RDONLY|O_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
md_creat(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                             _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
md_creat_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
md_seek(int filedes, jlong pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    jlong new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if ( pos == (jlong)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        new_pos = _lseeki64(filedes, 0L, SEEK_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        new_pos = _lseeki64(filedes, pos, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    return new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
md_close(int filedes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    (void)close(filedes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
md_send(int s, const char *msg, int len, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return send(s, msg, len, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
md_read(int filedes, void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    return read(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
md_write(int filedes, const void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    return write(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
md_get_microsecs(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return (jlong)(timeGetTime())*(jlong)1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
#define FT2JLONG(ft) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
md_get_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static jlong fileTime_1_1_70 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    SYSTEMTIME st0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    FILETIME   ft0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    if (fileTime_1_1_70 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /* Initialize fileTime_1_1_70 -- the Win32 file time of midnight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * 1/1/70.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        memset(&st0, 0, sizeof(st0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        st0.wYear  = 1970;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        st0.wMonth = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        st0.wDay   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        fileTime_1_1_70 = FT2JLONG(ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    GetSystemTime(&st0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
md_get_thread_cpu_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    return md_get_timemillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
HINSTANCE hJavaInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
static int nError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
BOOL WINAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    WSADATA wsaData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        case DLL_PROCESS_ATTACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            hJavaInst = hinst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            nError = WSAStartup(MAKEWORD(2,0), &wsaData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        case DLL_PROCESS_DETACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            WSACleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            hJavaInst = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
md_get_prelude_path(char *path, int path_len, char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    char libdir[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    char *lastSlash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /* This is actually in the bin directory, so move above bin for lib */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    (void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
md_vsnprintf(char *s, int n, const char *format, va_list ap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    return _vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
md_snprintf(char *s, int n, const char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    ret = md_vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
md_system_error(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
md_htons(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    return htons(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
md_htonl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    return htonl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
md_ntohs(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    return ntohs(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
md_ntohl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    return ntohl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
get_last_error_string(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /* DOS error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (errno != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /* C runtime error that has no corresponding DOS error code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        const char *s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int         n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        s = strerror(errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (n >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            n = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        (void)strncpy(buf, s, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
/* Build a machine dependent library name out of a path and file name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    int   pnamelen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    char  c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    pnamelen = pname ? (int)strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    c = (pnamelen > 0) ? pname[pnamelen-1] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /* Quietly truncates on buffer overflow. Should be an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        *holder = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (pnamelen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        sprintf(holder, "%s.dll", fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    } else if (c == ':' || c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        sprintf(holder, "%s%s.dll", pname, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        sprintf(holder, "%s\\%s.dll", pname, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
md_load_library(const char * name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    void *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    result = LoadLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /* Error message is pretty lame, try to make a better guess. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        long errcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        errcode = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (errcode == ERROR_MOD_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            get_last_error_string(err_buf, err_buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
md_unload_library(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    FreeLibrary(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
md_find_library_entry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    return GetProcAddress(handle, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
}