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";
60 #ifdef CONFIG_MACOSX_BUNDLE
63 CFURLRef res_url_ref
=NULL
;
64 CFURLRef bdl_url_ref
=NULL
;
65 char *res_url_path
= NULL
;
66 char *bdl_url_path
= NULL
;
69 if ((homedir
= getenv("MPLAYER_HOME")) != NULL
)
71 else if ((homedir
= getenv("HOME")) == NULL
)
72 #if defined(__MINGW32__) || defined(__CYGWIN__)
73 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
77 GetModuleFileNameA(NULL
, exedir
, 260);
78 for (i
=0; i
< strlen(exedir
); i
++)
80 {exedir
[i
]='/'; imax
=i
;}
87 len
= strlen(homedir
) + strlen(config_dir
) + 1;
88 if (filename
== NULL
) {
89 if ((buff
= malloc(len
)) == NULL
)
91 sprintf(buff
, "%s%s", homedir
, config_dir
);
93 len
+= strlen(filename
) + 1;
94 if ((buff
= malloc(len
)) == NULL
)
96 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
99 #ifdef CONFIG_MACOSX_BUNDLE
100 if (stat(buff
, &dummy
)) {
102 res_url_ref
=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
103 bdl_url_ref
=CFBundleCopyBundleURL(CFBundleGetMainBundle());
105 if (res_url_ref
&&bdl_url_ref
) {
107 res_url_path
=malloc(maxlen
);
108 bdl_url_path
=malloc(maxlen
);
110 while (!CFURLGetFileSystemRepresentation(res_url_ref
, true, res_url_path
, maxlen
)) {
112 res_url_path
=realloc(res_url_path
, maxlen
);
114 CFRelease(res_url_ref
);
116 while (!CFURLGetFileSystemRepresentation(bdl_url_ref
, true, bdl_url_path
, maxlen
)) {
118 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
120 CFRelease(bdl_url_ref
);
122 if (strcmp(res_url_path
, bdl_url_path
) == 0)
126 if (res_url_path
&&filename
) {
127 if ((strlen(filename
)+strlen(res_url_path
)+2)>maxlen
) {
128 maxlen
=strlen(filename
)+strlen(res_url_path
)+2;
131 buff
= malloc(maxlen
);
132 strcpy(buff
, res_url_path
);
135 strcat(buff
, filename
);
139 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);
143 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
144 void set_path_env(void)
146 /*make our codec dirs available for LoadLibraryA()*/
147 char win32path
[MAX_PATH
];
149 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH
, win32path
);
151 /* Expand to absolute path unless it's already absolute */
152 if (!strstr(BINARY_CODECS_PATH
,":") && BINARY_CODECS_PATH
[0] != '\\') {
153 GetModuleFileNameA(NULL
, win32path
, MAX_PATH
);
154 strcpy(strrchr(win32path
, '\\') + 1, BINARY_CODECS_PATH
);
156 else strcpy(win32path
, BINARY_CODECS_PATH
);
157 #endif /*__CYGWIN__*/
158 mp_msg(MSGT_WIN32
, MSGL_V
, "Setting PATH to %s\n", win32path
);
159 if (!SetEnvironmentVariableA("PATH", win32path
))
160 mp_msg(MSGT_WIN32
, MSGL_WARN
, "Cannot set PATH!");
162 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
164 char *codec_path
= BINARY_CODECS_PATH
;
166 char *mp_basename(const char *path
)
171 s
= strrchr(path
, '\\');
174 s
= strrchr(path
, ':');
178 s
= strrchr(path
, '/');
179 return s
? s
+ 1 : (char *)path
;
182 struct bstr
mp_dirname(const char *path
)
184 struct bstr ret
= {(uint8_t *)path
, mp_basename(path
) - path
};
190 char *mp_path_join(void *talloc_ctx
, struct bstr p1
, struct bstr p2
)
193 return bstrdup0(talloc_ctx
, p2
);
195 return bstrdup0(talloc_ctx
, p1
);
198 if (p2
.len
>= 2 && p2
.start
[1] == ':'
199 || p2
.start
[0] == '\\' || p2
.start
[0] == '/')
201 if (p2
.start
[0] == '/')
203 return bstrdup0(talloc_ctx
, p2
); // absolute path
206 int endchar1
= p1
.start
[p1
.len
- 1];
208 have_separator
= endchar1
== '/' || endchar1
== '\\'
209 || p1
.len
== 2 && endchar1
== ':'; // "X:" only
211 have_separator
= endchar1
== '/';
214 return talloc_asprintf(talloc_ctx
, "%.*s%s%.*s", BSTR_P(p1
),
215 have_separator
? "" : "/", BSTR_P(p2
));
218 bool mp_path_exists(const char *path
)
221 return mp_stat(path
, &st
) == 0;
224 bool mp_path_isdir(const char *path
)
227 return mp_stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
);