Fix vf_tcdump's compilation
[mplayer/kovensky.git] / get_path.c
blobe2b6be6f39738ec51db3f271ff196615b2d07238
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 "get_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(_WIN32)
39 #define _WIN32_IE 0x0400
40 #include <windows.h>
41 #include <shlobj.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #if defined(__CYGWIN__)
45 #include <sys/cygwin.h>
46 #endif
47 #elif defined(__OS2__)
48 #define INCL_DOS
49 #include <os2.h>
50 #endif
52 char *get_path(const char *filename){
53 char *homedir;
54 char *buff;
55 #ifdef _WIN32
56 static char *config_dir = "/mplayer";
57 #else
58 static char *config_dir = "/.mplayer";
59 #endif
60 int len;
61 #ifdef CONFIG_MACOSX_BUNDLE
62 struct stat dummy;
63 CFIndex maxlen=256;
64 CFURLRef res_url_ref=NULL;
65 CFURLRef bdl_url_ref=NULL;
66 char *res_url_path = NULL;
67 char *bdl_url_path = NULL;
68 #endif
70 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
71 config_dir = "";
72 else if ((homedir = getenv("HOME")) == NULL)
73 #if _WIN32
74 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
76 int i,imax=0;
77 struct _stat statBuffer;
78 char exedir[4096], config_path[4096], appdata[4096];
79 if (SHGetSpecialFolderPathA(NULL, appdata, CSIDL_APPDATA, 1)) {
80 strncpy(exedir, appdata, strlen(appdata));
81 } else {
82 GetModuleFileNameA(NULL, exedir, 4096);
84 int len = strlen(exedir);
85 for (i=0; i< len; i++)
86 if (exedir[i] =='\\')
87 exedir[i]='/';
89 sprintf(config_path, "%s%s", exedir, config_dir);
90 int stat_res;
91 if ((stat_res = stat(config_path, &statBuffer)) < 0 ||
92 !(statBuffer.st_mode & S_IFDIR)) {
93 GetModuleFileNameA(NULL, exedir, 4096);
94 len = strlen(exedir);
95 for (i=0; i< len; i++)
96 if (exedir[i] =='\\')
97 {exedir[i]='/'; imax=i;}
98 exedir[imax]='\0';
101 homedir = exedir;
103 #elif defined(__OS2__)
105 PPIB ppib;
106 char path[260];
108 // Get process info blocks
109 DosGetInfoBlocks(NULL, &ppib);
111 // Get full path of the executable
112 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
114 // Truncate name part including last backslash
115 *strrchr(path, '\\') = 0;
117 // Convert backslash to slash
118 _fnslashify(path);
120 homedir = path;
122 #else
123 return NULL;
124 #endif
125 len = strlen(homedir) + strlen(config_dir) + 1;
126 if (filename == NULL) {
127 if ((buff = malloc(len)) == NULL)
128 return NULL;
129 sprintf(buff, "%s%s", homedir, config_dir);
130 } else {
131 len += strlen(filename) + 1;
132 if ((buff = malloc(len)) == NULL)
133 return NULL;
134 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
137 #ifdef CONFIG_MACOSX_BUNDLE
138 if (stat(buff, &dummy)) {
140 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
141 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
143 if (res_url_ref&&bdl_url_ref) {
145 res_url_path=malloc(maxlen);
146 bdl_url_path=malloc(maxlen);
148 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
149 maxlen*=2;
150 res_url_path=realloc(res_url_path, maxlen);
152 CFRelease(res_url_ref);
154 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
155 maxlen*=2;
156 bdl_url_path=realloc(bdl_url_path, maxlen);
158 CFRelease(bdl_url_ref);
160 if (strcmp(res_url_path, bdl_url_path) == 0)
161 res_url_path = NULL;
164 if (res_url_path&&filename) {
165 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
166 maxlen=strlen(filename)+strlen(res_url_path)+2;
168 free(buff);
169 buff = malloc(maxlen);
170 strcpy(buff, res_url_path);
172 strcat(buff,"/");
173 strcat(buff, filename);
176 #endif
177 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
178 return buff;
181 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
182 void set_path_env(void)
184 /*make our codec dirs available for LoadLibraryA()*/
185 char tmppath[MAX_PATH*2 + 1];
186 char win32path[MAX_PATH];
187 char realpath[MAX_PATH];
188 #ifdef __CYGWIN__
189 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
190 strcpy(tmppath,win32path);
191 #ifdef CONFIG_REALCODECS
192 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
193 sprintf(tmppath,"%s;%s",win32path,realpath);
194 #endif /*CONFIG_REALCODECS*/
195 #else /*__CYGWIN__*/
196 /* Expand to absolute path unless it's already absolute */
197 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
198 GetModuleFileNameA(NULL, win32path, MAX_PATH);
199 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
201 else strcpy(win32path,WIN32_PATH);
202 strcpy(tmppath,win32path);
203 #ifdef CONFIG_REALCODECS
204 /* Expand to absolute path unless it's already absolute */
205 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
206 GetModuleFileNameA(NULL, realpath, MAX_PATH);
207 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
209 else strcpy(realpath,REALCODEC_PATH);
210 sprintf(tmppath,"%s;%s",win32path,realpath);
211 #endif /*CONFIG_REALCODECS*/
212 #endif /*__CYGWIN__*/
213 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
214 if (!SetEnvironmentVariableA("PATH", tmppath))
215 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
217 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */