Ignore svn changes up to r30479
[mplayer/kovensky.git] / get_path.c
blobec76f571911accd2d9d1a09d96ebf82081540bc9
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 "mp_msg.h"
30 #include "get_path.h"
32 #ifdef CONFIG_MACOSX_BUNDLE
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #elif defined(__MINGW32__)
38 #include <windows.h>
39 #elif defined(__CYGWIN__)
40 #include <windows.h>
41 #include <sys/cygwin.h>
42 #elif defined(__OS2__)
43 #define INCL_DOS
44 #include <os2.h>
45 #endif
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 tmppath[MAX_PATH*2 + 1];
163 char win32path[MAX_PATH];
164 char realpath[MAX_PATH];
165 #ifdef __CYGWIN__
166 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
167 strcpy(tmppath,win32path);
168 #ifdef CONFIG_REALCODECS
169 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
170 sprintf(tmppath,"%s;%s",win32path,realpath);
171 #endif /*CONFIG_REALCODECS*/
172 #else /*__CYGWIN__*/
173 /* Expand to absolute path unless it's already absolute */
174 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
175 GetModuleFileNameA(NULL, win32path, MAX_PATH);
176 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
178 else strcpy(win32path,WIN32_PATH);
179 strcpy(tmppath,win32path);
180 #ifdef CONFIG_REALCODECS
181 /* Expand to absolute path unless it's already absolute */
182 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
183 GetModuleFileNameA(NULL, realpath, MAX_PATH);
184 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
186 else strcpy(realpath,REALCODEC_PATH);
187 sprintf(tmppath,"%s;%s",win32path,realpath);
188 #endif /*CONFIG_REALCODECS*/
189 #endif /*__CYGWIN__*/
190 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
191 if (!SetEnvironmentVariableA("PATH", tmppath))
192 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
194 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */