2 * Get path to config dir/file.
5 * Returns the pointer to the ALLOCATED buffer containing the
6 * zero terminated path string. This buffer has to be FREED
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.
34 #ifdef CONFIG_MACOSX_BUNDLE
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <sys/types.h>
39 #elif defined(__MINGW32__)
41 #elif defined(__CYGWIN__)
43 #include <sys/cygwin.h>
48 #include "osdep/osdep.h"
50 char *get_path(const char *filename
){
54 static char *config_dir
= "/mplayer";
56 static char *config_dir
= "/.mplayer";
59 #ifdef CONFIG_MACOSX_BUNDLE
62 CFURLRef res_url_ref
=NULL
;
63 CFURLRef bdl_url_ref
=NULL
;
64 char *res_url_path
= NULL
;
65 char *bdl_url_path
= NULL
;
68 if ((homedir
= getenv("MPLAYER_HOME")) != NULL
)
70 else if ((homedir
= getenv("HOME")) == NULL
)
71 #if defined(__MINGW32__) || defined(__CYGWIN__)
72 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
76 GetModuleFileNameA(NULL
, exedir
, 260);
77 for (i
=0; i
< strlen(exedir
); i
++)
79 {exedir
[i
]='/'; imax
=i
;}
83 #elif defined(__OS2__)
88 // Get process info blocks
89 DosGetInfoBlocks(NULL
, &ppib
);
91 // Get full path of the executable
92 DosQueryModuleName(ppib
->pib_hmte
, sizeof( path
), path
);
94 // Truncate name part including last backslash
95 *strrchr(path
, '\\') = 0;
97 // Convert backslash to slash
105 len
= strlen(homedir
) + strlen(config_dir
) + 1;
106 if (filename
== NULL
) {
107 if ((buff
= malloc(len
)) == NULL
)
109 sprintf(buff
, "%s%s", homedir
, config_dir
);
111 len
+= strlen(filename
) + 1;
112 if ((buff
= malloc(len
)) == NULL
)
114 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
117 #ifdef CONFIG_MACOSX_BUNDLE
118 if (stat(buff
, &dummy
)) {
120 res_url_ref
=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
121 bdl_url_ref
=CFBundleCopyBundleURL(CFBundleGetMainBundle());
123 if (res_url_ref
&&bdl_url_ref
) {
125 res_url_path
=malloc(maxlen
);
126 bdl_url_path
=malloc(maxlen
);
128 while (!CFURLGetFileSystemRepresentation(res_url_ref
, true, res_url_path
, maxlen
)) {
130 res_url_path
=realloc(res_url_path
, maxlen
);
132 CFRelease(res_url_ref
);
134 while (!CFURLGetFileSystemRepresentation(bdl_url_ref
, true, bdl_url_path
, maxlen
)) {
136 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
138 CFRelease(bdl_url_ref
);
140 if (strcmp(res_url_path
, bdl_url_path
) == 0)
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;
149 buff
= malloc(maxlen
);
150 strcpy(buff
, res_url_path
);
153 strcat(buff
, filename
);
157 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);
161 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
162 void set_path_env(void)
164 /*make our codec dirs available for LoadLibraryA()*/
165 char win32path
[MAX_PATH
];
167 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH
, win32path
);
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
);
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!");
180 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
182 char *codec_path
= BINARY_CODECS_PATH
;
184 char *mp_basename(const char *path
)
189 s
= strrchr(path
, '\\');
192 s
= strrchr(path
, ':');
196 s
= strrchr(path
, '/');
197 return s
? s
+ 1 : (char *)path
;
200 struct bstr
mp_dirname(const char *path
)
202 struct bstr ret
= {(uint8_t *)path
, mp_basename(path
) - path
};
208 char *mp_path_join(void *talloc_ctx
, struct bstr p1
, struct bstr p2
)
211 return bstrdup0(talloc_ctx
, p2
);
213 return bstrdup0(talloc_ctx
, p1
);
216 if (p2
.len
>= 2 && p2
.start
[1] == ':'
217 || p2
.start
[0] == '\\' || p2
.start
[0] == '/')
219 if (p2
.start
[0] == '/')
221 return bstrdup0(talloc_ctx
, p2
); // absolute path
224 int endchar1
= p1
.start
[p1
.len
- 1];
226 have_separator
= endchar1
== '/' || endchar1
== '\\'
227 || p1
.len
== 2 && endchar1
== ':'; // "X:" only
229 have_separator
= endchar1
== '/';
232 return talloc_asprintf(talloc_ctx
, "%.*s%s%.*s", BSTR_P(p1
),
233 have_separator
? "" : "/", BSTR_P(p2
));