~ubuntu-branches/ubuntu/precise/mplayer2/precise-proposed

1.1.2 by Reinhard Tartler
Import upstream version 2.0-134-g84d8671
1
/*
2
 * Get path to config dir/file.
3
 *
4
 * Return Values:
5
 *   Returns the pointer to the ALLOCATED buffer containing the
6
 *   zero terminated path string. This buffer has to be FREED
7
 *   by the caller.
8
 *
9
 * This file is part of MPlayer.
10
 *
11
 * MPlayer is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * MPlayer is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
 */
25
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <stdbool.h>
30
#include "config.h"
31
#include "mp_msg.h"
32
#include "path.h"
33
34
#ifdef CONFIG_MACOSX_BUNDLE
35
#include <CoreFoundation/CoreFoundation.h>
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
#include <unistd.h>
39
#elif defined(__MINGW32__)
40
#include <windows.h>
41
#elif defined(__CYGWIN__)
42
#include <windows.h>
43
#include <sys/cygwin.h>
44
#endif
45
46
#include "talloc.h"
47
48
#include "osdep/osdep.h"
49
50
char *get_path(const char *filename){
51
	char *homedir;
52
	char *buff;
53
#ifdef __MINGW32__
54
	static char *config_dir = "/mplayer";
55
#else
56
	static char *config_dir = "/.mplayer";
57
#endif
58
	int len;
59
#ifdef CONFIG_MACOSX_BUNDLE
60
	struct stat dummy;
61
	CFIndex maxlen=256;
62
	CFURLRef res_url_ref=NULL;
63
	CFURLRef bdl_url_ref=NULL;
64
	char *res_url_path = NULL;
65
	char *bdl_url_path = NULL;
66
#endif
67
68
	if ((homedir = getenv("MPLAYER_HOME")) != NULL)
69
		config_dir = "";
70
	else if ((homedir = getenv("HOME")) == NULL)
71
#if defined(__MINGW32__) || defined(__CYGWIN__)
72
	/* Hack to get fonts etc. loaded outside of Cygwin environment. */
73
	{
74
		int i,imax=0;
75
		char exedir[260];
76
		GetModuleFileNameA(NULL, exedir, 260);
77
		for (i=0; i< strlen(exedir); i++)
78
			if (exedir[i] =='\\')
79
				{exedir[i]='/'; imax=i;}
80
		exedir[imax]='\0';
81
		homedir = exedir;
82
	}
83
#elif defined(__OS2__)
84
    {
85
        PPIB ppib;
86
        char path[260];
87
88
        // Get process info blocks
89
        DosGetInfoBlocks(NULL, &ppib);
90
91
        // Get full path of the executable
92
        DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
93
94
        // Truncate name part including last backslash
95
        *strrchr(path, '\\') = 0;
96
97
        // Convert backslash to slash
98
        _fnslashify(path);
99
100
        homedir = path;
101
    }
102
#else
103
	return NULL;
104
#endif
105
	len = strlen(homedir) + strlen(config_dir) + 1;
106
	if (filename == NULL) {
107
		if ((buff = malloc(len)) == NULL)
108
			return NULL;
109
		sprintf(buff, "%s%s", homedir, config_dir);
110
	} else {
111
		len += strlen(filename) + 1;
112
		if ((buff = malloc(len)) == NULL)
113
			return NULL;
114
		sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
115
	}
116
117
#ifdef CONFIG_MACOSX_BUNDLE
118
	if (stat(buff, &dummy)) {
119
120
		res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
121
		bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
122
123
		if (res_url_ref&&bdl_url_ref) {
124
125
			res_url_path=malloc(maxlen);
126
			bdl_url_path=malloc(maxlen);
127
128
			while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
129
				maxlen*=2;
130
				res_url_path=realloc(res_url_path, maxlen);
131
			}
132
			CFRelease(res_url_ref);
133
134
			while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
135
				maxlen*=2;
136
				bdl_url_path=realloc(bdl_url_path, maxlen);
137
			}
138
			CFRelease(bdl_url_ref);
139
140
			if (strcmp(res_url_path, bdl_url_path) == 0)
141
				res_url_path = NULL;
142
		}
143
144
		if (res_url_path&&filename) {
145
			if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
146
				maxlen=strlen(filename)+strlen(res_url_path)+2;
147
			}
148
			free(buff);
149
			buff = malloc(maxlen);
150
			strcpy(buff, res_url_path);
151
152
			strcat(buff,"/");
153
			strcat(buff, filename);
154
		}
155
	}
156
#endif
157
	mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
158
	return buff;
159
}
160
161
#if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
162
void set_path_env(void)
163
{
164
	/*make our codec dirs available for LoadLibraryA()*/
165
	char win32path[MAX_PATH];
166
#ifdef __CYGWIN__
167
	cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
168
#else /*__CYGWIN__*/
169
	/* Expand to absolute path unless it's already absolute */
170
	if (!strstr(BINARY_CODECS_PATH,":") && BINARY_CODECS_PATH[0] != '\\') {
171
		GetModuleFileNameA(NULL, win32path, MAX_PATH);
172
		strcpy(strrchr(win32path, '\\') + 1, BINARY_CODECS_PATH);
173
	}
174
	else strcpy(win32path, BINARY_CODECS_PATH);
175
#endif /*__CYGWIN__*/
176
	mp_msg(MSGT_WIN32, MSGL_V, "Setting PATH to %s\n", win32path);
177
	if (!SetEnvironmentVariableA("PATH", win32path))
178
		mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
179
}
180
#endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
181
182
char *codec_path = BINARY_CODECS_PATH;
183
184
char *mp_basename(const char *path)
185
{
186
    char *s;
187
188
#if HAVE_DOS_PATHS
189
    s = strrchr(path, '\\');
190
    if (s)
191
        path = s + 1;
192
    s = strrchr(path, ':');
193
    if (s)
194
        path = s + 1;
195
#endif
196
    s = strrchr(path, '/');
197
    return s ? s + 1 : (char *)path;
198
}
199
200
struct bstr mp_dirname(const char *path)
201
{
202
    struct bstr ret = {(uint8_t *)path, mp_basename(path) - path};
203
    if (ret.len == 0)
1.1.3 by Reinhard Tartler
Import upstream version 2.0-426-gc32b3ed
204
        return bstr(".");
1.1.2 by Reinhard Tartler
Import upstream version 2.0-134-g84d8671
205
    return ret;
206
}
207
208
char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
209
{
210
    if (p1.len == 0)
211
        return bstrdup0(talloc_ctx, p2);
212
    if (p2.len == 0)
213
        return bstrdup0(talloc_ctx, p1);
214
215
#if HAVE_DOS_PATHS
216
    if (p2.len >= 2 && p2.start[1] == ':'
217
        || p2.start[0] == '\\' || p2.start[0] == '/')
218
#else
219
    if (p2.start[0] == '/')
220
#endif
221
        return bstrdup0(talloc_ctx, p2);   // absolute path
222
223
    bool have_separator;
224
    int endchar1 = p1.start[p1.len - 1];
225
#if HAVE_DOS_PATHS
226
    have_separator = endchar1 == '/' || endchar1 == '\\'
227
        || p1.len == 2 && endchar1 == ':';     // "X:" only
228
#else
229
    have_separator = endchar1 == '/';
230
#endif
231
232
    return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
233
                           have_separator ? "" : "/", BSTR_P(p2));
234
}