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.
30 #include <sys/types.h>
37 #ifdef CONFIG_MACOSX_BUNDLE
38 #include <CoreFoundation/CoreFoundation.h>
40 #elif defined(__MINGW32__)
42 #elif defined(__CYGWIN__)
44 #include <sys/cygwin.h>
51 char *get_path(const char *filename
){
55 static char *config_dir
= "/mplayer";
57 static char *config_dir
= "/.mplayer";
59 #if defined(__MINGW32__) || defined(__CYGWIN__)
63 #ifdef CONFIG_MACOSX_BUNDLE
66 CFURLRef res_url_ref
=NULL
;
67 CFURLRef bdl_url_ref
=NULL
;
68 char *res_url_path
= NULL
;
69 char *bdl_url_path
= NULL
;
72 if ((homedir
= getenv("MPLAYER_HOME")) != NULL
)
74 else if ((homedir
= getenv("HOME")) == NULL
)
75 #if defined(__MINGW32__) || defined(__CYGWIN__)
76 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
79 len
= (int)GetModuleFileNameA(NULL
, exedir
, 260);
80 for (i
=0; i
< len
; i
++)
82 {exedir
[i
]='/'; imax
=i
;}
89 len
= strlen(homedir
) + strlen(config_dir
) + 1;
90 if (filename
== NULL
) {
91 if ((buff
= malloc(len
)) == NULL
)
93 sprintf(buff
, "%s%s", homedir
, config_dir
);
95 len
+= strlen(filename
) + 1;
96 if ((buff
= malloc(len
)) == NULL
)
98 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
101 #ifdef CONFIG_MACOSX_BUNDLE
102 if (stat(buff
, &dummy
)) {
104 res_url_ref
=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
105 bdl_url_ref
=CFBundleCopyBundleURL(CFBundleGetMainBundle());
107 if (res_url_ref
&&bdl_url_ref
) {
109 res_url_path
=malloc(maxlen
);
110 bdl_url_path
=malloc(maxlen
);
112 while (!CFURLGetFileSystemRepresentation(res_url_ref
, true, res_url_path
, maxlen
)) {
114 res_url_path
=realloc(res_url_path
, maxlen
);
116 CFRelease(res_url_ref
);
118 while (!CFURLGetFileSystemRepresentation(bdl_url_ref
, true, bdl_url_path
, maxlen
)) {
120 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
122 CFRelease(bdl_url_ref
);
124 if (strcmp(res_url_path
, bdl_url_path
) == 0)
128 if (res_url_path
&&filename
) {
129 if ((strlen(filename
)+strlen(res_url_path
)+2)>maxlen
) {
130 maxlen
=strlen(filename
)+strlen(res_url_path
)+2;
133 buff
= malloc(maxlen
);
134 strcpy(buff
, res_url_path
);
137 strcat(buff
, filename
);
141 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);
145 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
146 void set_path_env(void)
148 /*make our codec dirs available for LoadLibraryA()*/
149 char win32path
[MAX_PATH
];
151 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH
, win32path
);
153 /* Expand to absolute path unless it's already absolute */
154 if (!strstr(BINARY_CODECS_PATH
,":") && BINARY_CODECS_PATH
[0] != '\\') {
155 GetModuleFileNameA(NULL
, win32path
, MAX_PATH
);
156 strcpy(strrchr(win32path
, '\\') + 1, BINARY_CODECS_PATH
);
158 else strcpy(win32path
, BINARY_CODECS_PATH
);
159 #endif /*__CYGWIN__*/
160 mp_msg(MSGT_WIN32
, MSGL_V
, "Setting PATH to %s\n", win32path
);
161 if (!SetEnvironmentVariableA("PATH", win32path
))
162 mp_msg(MSGT_WIN32
, MSGL_WARN
, "Cannot set PATH!");
164 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
166 char *codec_path
= BINARY_CODECS_PATH
;
168 char *mp_basename(const char *path
)
173 s
= strrchr(path
, '\\');
176 s
= strrchr(path
, ':');
180 s
= strrchr(path
, '/');
181 return s
? s
+ 1 : (char *)path
;
184 struct bstr
mp_dirname(const char *path
)
186 struct bstr ret
= {(uint8_t *)path
, mp_basename(path
) - path
};
192 char *mp_path_join(void *talloc_ctx
, struct bstr p1
, struct bstr p2
)
195 return bstrdup0(talloc_ctx
, p2
);
197 return bstrdup0(talloc_ctx
, p1
);
200 if (p2
.len
>= 2 && p2
.start
[1] == ':'
201 || p2
.start
[0] == '\\' || p2
.start
[0] == '/')
203 if (p2
.start
[0] == '/')
205 return bstrdup0(talloc_ctx
, p2
); // absolute path
208 int endchar1
= p1
.start
[p1
.len
- 1];
210 have_separator
= endchar1
== '/' || endchar1
== '\\'
211 || p1
.len
== 2 && endchar1
== ':'; // "X:" only
213 have_separator
= endchar1
== '/';
216 return talloc_asprintf(talloc_ctx
, "%.*s%s%.*s", BSTR_P(p1
),
217 have_separator
? "" : "/", BSTR_P(p2
));
220 bool mp_path_exists(const char *path
)
223 return mp_stat(path
, &st
) == 0;
226 bool mp_path_isdir(const char *path
)
229 return mp_stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
);