vo_mga: minor code cleanup
[mplayer.git] / path.c
blob2a1d504ce373f38dd6152b51afa6b1aa51d5db08
1 /*
2 * Get path to config dir/file.
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.
9 * This file is part of MPlayer.
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.
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.
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.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "path.h"
33 #ifdef CONFIG_MACOSX_BUNDLE
34 #include <CoreFoundation/CoreFoundation.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #elif defined(__MINGW32__)
39 #include <windows.h>
40 #elif defined(__CYGWIN__)
41 #include <windows.h>
42 #include <sys/cygwin.h>
43 #endif
45 #include "osdep/osdep.h"
47 char *get_path(const char *filename){
48 char *homedir;
49 char *buff;
50 #ifdef __MINGW32__
51 static char *config_dir = "/mplayer";
52 #else
53 static char *config_dir = "/.mplayer";
54 #endif
55 int len;
56 #ifdef CONFIG_MACOSX_BUNDLE
57 struct stat dummy;
58 CFIndex maxlen=256;
59 CFURLRef res_url_ref=NULL;
60 CFURLRef bdl_url_ref=NULL;
61 char *res_url_path = NULL;
62 char *bdl_url_path = NULL;
63 #endif
65 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
66 config_dir = "";
67 else if ((homedir = getenv("HOME")) == NULL)
68 #if defined(__MINGW32__) || defined(__CYGWIN__)
69 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
71 int i,imax=0;
72 char exedir[260];
73 GetModuleFileNameA(NULL, exedir, 260);
74 for (i=0; i< strlen(exedir); i++)
75 if (exedir[i] =='\\')
76 {exedir[i]='/'; imax=i;}
77 exedir[imax]='\0';
78 homedir = exedir;
80 #elif defined(__OS2__)
82 PPIB ppib;
83 char path[260];
85 // Get process info blocks
86 DosGetInfoBlocks(NULL, &ppib);
88 // Get full path of the executable
89 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
91 // Truncate name part including last backslash
92 *strrchr(path, '\\') = 0;
94 // Convert backslash to slash
95 _fnslashify(path);
97 homedir = path;
99 #else
100 return NULL;
101 #endif
102 len = strlen(homedir) + strlen(config_dir) + 1;
103 if (filename == NULL) {
104 if ((buff = malloc(len)) == NULL)
105 return NULL;
106 sprintf(buff, "%s%s", homedir, config_dir);
107 } else {
108 len += strlen(filename) + 1;
109 if ((buff = malloc(len)) == NULL)
110 return NULL;
111 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
114 #ifdef CONFIG_MACOSX_BUNDLE
115 if (stat(buff, &dummy)) {
117 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
118 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
120 if (res_url_ref&&bdl_url_ref) {
122 res_url_path=malloc(maxlen);
123 bdl_url_path=malloc(maxlen);
125 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
126 maxlen*=2;
127 res_url_path=realloc(res_url_path, maxlen);
129 CFRelease(res_url_ref);
131 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
132 maxlen*=2;
133 bdl_url_path=realloc(bdl_url_path, maxlen);
135 CFRelease(bdl_url_ref);
137 if (strcmp(res_url_path, bdl_url_path) == 0)
138 res_url_path = NULL;
141 if (res_url_path&&filename) {
142 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
143 maxlen=strlen(filename)+strlen(res_url_path)+2;
145 free(buff);
146 buff = malloc(maxlen);
147 strcpy(buff, res_url_path);
149 strcat(buff,"/");
150 strcat(buff, filename);
153 #endif
154 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
155 return buff;
158 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
159 void set_path_env(void)
161 /*make our codec dirs available for LoadLibraryA()*/
162 char win32path[MAX_PATH];
163 #ifdef __CYGWIN__
164 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
165 #else /*__CYGWIN__*/
166 /* Expand to absolute path unless it's already absolute */
167 if (!strstr(BINARY_CODECS_PATH,":") && BINARY_CODECS_PATH[0] != '\\') {
168 GetModuleFileNameA(NULL, win32path, MAX_PATH);
169 strcpy(strrchr(win32path, '\\') + 1, BINARY_CODECS_PATH);
171 else strcpy(win32path, BINARY_CODECS_PATH);
172 #endif /*__CYGWIN__*/
173 mp_msg(MSGT_WIN32, MSGL_V, "Setting PATH to %s\n", win32path);
174 if (!SetEnvironmentVariableA("PATH", win32path))
175 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
177 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
179 char *codec_path = BINARY_CODECS_PATH;
181 static int needs_free = 0;
183 void set_codec_path(const char *path)
185 if (needs_free)
186 free(codec_path);
187 if (path == 0) {
188 codec_path = BINARY_CODECS_PATH;
189 needs_free = 0;
190 return;
192 codec_path = malloc(strlen(path) + 1);
193 strcpy(codec_path, path);
194 needs_free = 1;