File: dynamic.c

package info (click to toggle)
fontforge 0.0.20120101%2Bgit-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,756 kB
  • sloc: ansic: 556,562; sh: 236; makefile: 162; xml: 11; python: 11
file content (181 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright (C) 2005-2011 by George Williams */
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.

 * The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <basics.h>

/* Parse GNU .la "library" files. They give us the name to use with dlopen */
/* Currently only important on _Cygwin */
#if defined(__CygWin) && !defined(NODYNAMIC)
#  include <dynamic.h>
#  include <stdio.h>
#  include <string.h>
#  include <stdlib.h>

static char *LaInDir(char *dir,const char *filename,char *buffer, int buflen) {
    char *pt, *dpt;
    FILE *test;
    char dllname[1025];

    if ( dir==NULL ) {
	strncpy(buffer,filename,buflen-3);
	buffer[buflen-3] = '\0';
    } else
	snprintf(buffer,buflen-3,"%s/%s", dir,filename);
    dpt = strrchr(buffer,'/');
    if ( dpt==NULL )
	pt = strrchr(buffer,'.');
    else
	pt = strrchr(dpt+1,'.');
    if ( pt==NULL )
	strcat(buffer,".la");
    else
	strcpy(pt,".la");

    test = fopen(buffer,"r");
    if ( test==NULL )
return( NULL );
    while ( fgets(buffer,buflen,test)!=NULL ) {
	if ( strncmp(buffer,"dlname='",8)==0 ) {
	    pt = strrchr(buffer+8,'\'');
	    if ( pt!=NULL ) {
		*pt = '\0';
		fclose(test);
		if ( buffer[8]=='\0' )
return( NULL );			/* No dlopenable version */
		if ( buffer[8]=='/' )
return( buffer+8 );
		strcpy(dllname,buffer+8);
		if ( dir==NULL ) {
		    pt = strrchr(filename,'/');
		    if ( pt==NULL )
return( buffer+8 );
		    snprintf(buffer,buflen,"%.*s/%s", pt-filename, filename, dllname );
		} else
		    snprintf(buffer,buflen,"%s/%s", dir,dllname);
return( buffer );
	    }
	}
    }
    fclose(test);
return( NULL );
}

#ifdef dlopen
# undef dlopen
#endif

void *libtool_laopen(const char *filename, int flags) {
    char *ret;
    char buffer[1025];
    char dirbuf[1025];
    char *path, *pt;

    if ( filename==NULL )
return( dlopen(NULL,flags));	/* Return magic handle to main program */

    if ( strchr(filename,'/')!=NULL )
	ret = LaInDir(NULL,filename,buffer,sizeof(buffer));
    else {
	ret = NULL;
	path = getenv("LD_LIBRARY_PATH");
	if ( path!=NULL ) {
	    while ( (pt=strchr(path,':'))!=NULL ) {
		strncpy(dirbuf,path,pt-path);
		dirbuf[pt-path] = '\0';
		ret = LaInDir(dirbuf,filename,buffer,sizeof(buffer));
		if ( ret!=NULL )
	    break;
		path = pt+1;
	    }
	    if ( ret==NULL )
		ret = LaInDir(path,filename,buffer,sizeof(buffer));
	}
	/* I should search /etc/ld.so.cache here, but I can't parse it */
	if ( ret==NULL )
	    ret = LaInDir("/lib",filename,buffer,sizeof(buffer));
	if ( ret==NULL )
	    ret = LaInDir("/usr/lib",filename,buffer,sizeof(buffer));
	if ( ret==NULL )
	    ret = LaInDir("/usr/X11R6/lib",filename,buffer,sizeof(buffer));
	if ( ret==NULL )
	    ret = LaInDir("/usr/local/lib",filename,buffer,sizeof(buffer));
    }
    if ( ret!=NULL )
return( dlopen( ret,flags ));

return( dlopen(filename,flags) );	/* This will almost certainly fail, but it will provide an error for dlerror() */
}
#elif defined( __Mac )
    /* The mac now has normal dlopen routines */
#  include <dynamic.h>
#  include <stdio.h>
#  include <string.h>

void *gwwv_dlopen(char *name,int flags) {
#undef dlopen
    void *lib = dlopen(name,flags);
    char *temp;

    if (( lib!=NULL && lib!=(void *) -1) || name==NULL || *name=='/' )
return( lib );

    temp = galloc( strlen("/sw/lib/") + strlen(name) +1 );
    strcpy(temp,"/sw/lib/");
    strcat(temp,name);
    lib = dlopen(temp,flags);
    free(temp);
    if ( lib!=NULL && lib!=(void *) -1 )
return( lib );

    temp = galloc( strlen("/opt/local/lib/") + strlen(name) +1 );
    strcpy(temp,"/opt/local/lib/");
    strcat(temp,name);
    lib = dlopen(temp,flags);
    free(temp);
return( lib );
}
#elif defined( __Mac )
#  include <basics.h>
#  include <dynamic.h>
#  include <stdio.h>
#  include <string.h>

const void *gwwv_NSAddImage(char *name,uint32_t options) {
    const void *lib = NSAddImage(name,options);
    char *temp;

    if (( lib!=NULL && lib!=(void *) -1) || name==NULL || *name=='/' )
return( lib );

    temp = galloc( strlen("/sw/lib/") + strlen(name) +1 );
    strcpy(temp,"/sw/lib/");
    strcat(temp,name);
    lib = NSAddImage(temp,options);
    free(temp);
return( lib );
}
#endif