summaryrefslogtreecommitdiff
path: root/libreofficekit/source/shim.c
blob: ac272e8d8c04c4939070e9012f04dd0ccea92107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifdef LINUX

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sal/types.h>
#include <LibreOfficeKit/LibreOfficeKit.h>

#include <dlfcn.h>
#ifdef AIX
#  include <sys/ldr.h>
#endif

#define TARGET_LIB        "lib" "sofficeapp" ".so"
#define TARGET_MERGED_LIB "lib" "libmergedlo" ".so"

typedef LibreOfficeKit *(HookFunction)( const char *install_path);

SAL_DLLPUBLIC_EXPORT LibreOfficeKit *lok_init( const char *install_path )
{
    char *imp_lib;
    size_t partial_length;
    void *dlhandle;
    HookFunction *pSym;

    if (!install_path)
        return NULL;

    // allocate large enough buffer
    partial_length = strlen(install_path);
    imp_lib = (char *) malloc(partial_length + sizeof(TARGET_LIB) + sizeof(TARGET_MERGED_LIB) + 2);
    if (!imp_lib)
    {
        fprintf( stderr, "failed to open library : not enough memory\n");
        return NULL;
    }

    strcpy(imp_lib, install_path);

    imp_lib[partial_length++] = '/';
    strcpy(imp_lib + partial_length, TARGET_LIB);

    dlhandle = dlopen(imp_lib, RTLD_LAZY);
    if (!dlhandle)
    {
        strcpy(imp_lib + partial_length, TARGET_MERGED_LIB);

        dlhandle = dlopen(imp_lib, RTLD_LAZY);
        if (!dlhandle)
        {
            fprintf(stderr, "failed to open library '%s' or '%s' in '%s/'\n", TARGET_LIB, TARGET_MERGED_LIB, install_path);
            free(imp_lib);
            return NULL;
        }
    }

    pSym = (HookFunction *) dlsym( dlhandle, "libreofficekit_hook" );
    if (!pSym)
    {
        fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib );
        dlclose( dlhandle );
        free( imp_lib );
        return NULL;
    }

    free( imp_lib );
    return pSym( install_path );
}

#endif // not LINUX => port me !

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */