src/loadso/dlopen/SDL_sysloadso.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1635 92947e3a18db
child 2084 b656e6f09be3
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@1361
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1361
    23
slouken@1635
    24
#ifdef SDL_LOADSO_DLOPEN
slouken@1635
    25
slouken@1361
    26
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
slouken@1361
    27
/* System dependent library loading routines                           */
slouken@1361
    28
slouken@1361
    29
#include <stdio.h>
slouken@1361
    30
#include <dlfcn.h>
slouken@1361
    31
slouken@1361
    32
#include "SDL_loadso.h"
slouken@1361
    33
slouken@1895
    34
void *
slouken@1895
    35
SDL_LoadObject(const char *sofile)
slouken@1361
    36
{
slouken@1895
    37
    void *handle = dlopen(sofile, RTLD_NOW);
slouken@1895
    38
    const char *loaderror = (char *) dlerror();
slouken@1895
    39
    if (handle == NULL) {
slouken@1895
    40
        SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@1895
    41
    }
slouken@1895
    42
    return (handle);
slouken@1361
    43
}
slouken@1361
    44
slouken@1895
    45
void *
slouken@1895
    46
SDL_LoadFunction(void *handle, const char *name)
slouken@1361
    47
{
slouken@1895
    48
    void *symbol = dlsym(handle, name);
slouken@1895
    49
    if (symbol == NULL) {
slouken@1895
    50
        size_t len = 1 + SDL_strlen(name) + 1;
slouken@1895
    51
        char *_name = SDL_stack_alloc(char, len);
slouken@1895
    52
        _name[0] = '_';
slouken@1895
    53
        SDL_strlcpy(&_name[1], name, len);
slouken@1895
    54
        symbol = dlsym(handle, name);
slouken@1895
    55
        SDL_stack_free(_name);
slouken@1895
    56
        if (symbol == NULL) {
slouken@1895
    57
            SDL_SetError("Failed loading %s: %s", name,
slouken@1895
    58
                         (const char *) dlerror());
slouken@1895
    59
        }
slouken@1895
    60
    }
slouken@1895
    61
    return (symbol);
slouken@1361
    62
}
slouken@1361
    63
slouken@1895
    64
void
slouken@1895
    65
SDL_UnloadObject(void *handle)
slouken@1361
    66
{
slouken@1895
    67
    if (handle != NULL) {
slouken@1895
    68
        dlclose(handle);
slouken@1895
    69
    }
slouken@1361
    70
}
slouken@1361
    71
slouken@1635
    72
#endif /* SDL_LOADSO_DLOPEN */
slouken@1895
    73
/* vi: set ts=4 sw=4 expandtab: */