jdk/src/windows/demo/jvmti/hprof/hprof_md.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7999 059b8737ff55
child 10292 ed7db6a12c2a
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7999
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
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
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    32
// To ensure winsock2.h is used, it has to be included ahead of
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    33
// windows.h, which includes winsock.h by default.
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    34
#include <winsock2.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <mmsystem.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
md_getpid(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static int pid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    if ( pid >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    pid = getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
md_sleep(unsigned seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    Sleep((DWORD)seconds*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
md_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
md_connect(char *hostname, unsigned short port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    struct hostent *hentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    struct sockaddr_in s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* create a socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    fd = (int)socket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* find remote host's addr from name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if ((hentry = gethostbyname(hostname)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    (void)memset((char *)&s, 0, sizeof(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /* set remote host's addr; its already in network byte order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
           (int)sizeof(s.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* set remote host's port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    s.sin_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    s.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /* now try connecting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
md_recv(int f, char *buf, int len, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return recv(f, buf, len, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
md_shutdown(int filedes, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    return shutdown(filedes, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
md_open(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    return open(filename, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
md_open_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return open(filename, O_RDONLY|O_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
md_creat(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                             _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
md_creat_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
md_seek(int filedes, jlong pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    jlong new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    if ( pos == (jlong)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        new_pos = _lseeki64(filedes, 0L, SEEK_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        new_pos = _lseeki64(filedes, pos, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    return new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
md_close(int filedes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
   152
    (void)closesocket(filedes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
md_send(int s, const char *msg, int len, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    return send(s, msg, len, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
md_read(int filedes, void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    return read(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
md_write(int filedes, const void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    return write(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
md_get_microsecs(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    return (jlong)(timeGetTime())*(jlong)1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
#define FT2JLONG(ft) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
md_get_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static jlong fileTime_1_1_70 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    SYSTEMTIME st0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    FILETIME   ft0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (fileTime_1_1_70 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /* Initialize fileTime_1_1_70 -- the Win32 file time of midnight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * 1/1/70.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        memset(&st0, 0, sizeof(st0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        st0.wYear  = 1970;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        st0.wMonth = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        st0.wDay   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        fileTime_1_1_70 = FT2JLONG(ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    GetSystemTime(&st0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
md_get_thread_cpu_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    return md_get_timemillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
HINSTANCE hJavaInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
static int nError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
BOOL WINAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    WSADATA wsaData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        case DLL_PROCESS_ATTACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            hJavaInst = hinst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            nError = WSAStartup(MAKEWORD(2,0), &wsaData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case DLL_PROCESS_DETACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            WSACleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            hJavaInst = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
md_get_prelude_path(char *path, int path_len, char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    char libdir[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    char *lastSlash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /* This is actually in the bin directory, so move above bin for lib */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    (void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
md_vsnprintf(char *s, int n, const char *format, va_list ap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    return _vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
md_snprintf(char *s, int n, const char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    ret = md_vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
md_system_error(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
md_htons(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    return htons(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
md_htonl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    return htonl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
md_ntohs(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    return ntohs(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
md_ntohl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return ntohl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
get_last_error_string(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /* DOS error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    if (errno != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /* C runtime error that has no corresponding DOS error code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        const char *s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int         n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        s = strerror(errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (n >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            n = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        (void)strncpy(buf, s, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
/* Build a machine dependent library name out of a path and file name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    int   pnamelen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    char  c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    pnamelen = pname ? (int)strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    c = (pnamelen > 0) ? pname[pnamelen-1] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /* Quietly truncates on buffer overflow. Should be an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        *holder = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    if (pnamelen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        sprintf(holder, "%s.dll", fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    } else if (c == ':' || c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        sprintf(holder, "%s%s.dll", pname, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        sprintf(holder, "%s\\%s.dll", pname, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
md_load_library(const char * name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    void *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    result = LoadLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        /* Error message is pretty lame, try to make a better guess. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        long errcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        errcode = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (errcode == ERROR_MOD_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            get_last_error_string(err_buf, err_buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
md_unload_library(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    FreeLibrary(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
md_find_library_entry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    return GetProcAddress(handle, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
}