configure: --enable-debug: don't change other compiler flags
[mplayer.git] / path.c
blob785c48eec9d2152c1f50bb6495e99c6d3aa1724a
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 <stdbool.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include "config.h"
34 #include "mp_msg.h"
35 #include "path.h"
37 #ifdef CONFIG_MACOSX_BUNDLE
38 #include <CoreFoundation/CoreFoundation.h>
39 #include <unistd.h>
40 #elif defined(__MINGW32__)
41 #include <windows.h>
42 #elif defined(__CYGWIN__)
43 #include <windows.h>
44 #include <sys/cygwin.h>
45 #endif
47 #include "talloc.h"
49 #include "osdep/io.h"
51 char *get_path(const char *filename){
52 char *homedir;
53 char *buff;
54 #ifdef __MINGW32__
55 static char *config_dir = "/mplayer";
56 #else
57 static char *config_dir = "/.mplayer";
58 #endif
59 int len;
60 #ifdef CONFIG_MACOSX_BUNDLE
61 struct stat dummy;
62 CFIndex maxlen=256;
63 CFURLRef res_url_ref=NULL;
64 CFURLRef bdl_url_ref=NULL;
65 char *res_url_path = NULL;
66 char *bdl_url_path = NULL;
67 #endif
69 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
70 config_dir = "";
71 else if ((homedir = getenv("HOME")) == NULL)
72 #if defined(__MINGW32__) || defined(__CYGWIN__)
73 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
75 int i,imax=0;
76 char exedir[260];
77 GetModuleFileNameA(NULL, exedir, 260);
78 for (i=0; i< strlen(exedir); i++)
79 if (exedir[i] =='\\')
80 {exedir[i]='/'; imax=i;}
81 exedir[imax]='\0';
82 homedir = exedir;
84 #else
85 return NULL;
86 #endif
87 len = strlen(homedir) + strlen(config_dir) + 1;
88 if (filename == NULL) {
89 if ((buff = malloc(len)) == NULL)
90 return NULL;
91 sprintf(buff, "%s%s", homedir, config_dir);
92 } else {
93 len += strlen(filename) + 1;
94 if ((buff = malloc(len)) == NULL)
95 return 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)) {
111 maxlen*=2;
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)) {
117 maxlen*=2;
118 bdl_url_path=realloc(bdl_url_path, maxlen);
120 CFRelease(bdl_url_ref);
122 if (strcmp(res_url_path, bdl_url_path) == 0)
123 res_url_path = NULL;
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;
130 free(buff);
131 buff = malloc(maxlen);
132 strcpy(buff, res_url_path);
134 strcat(buff,"/");
135 strcat(buff, filename);
138 #endif
139 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
140 return 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];
148 #ifdef __CYGWIN__
149 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
150 #else /*__CYGWIN__*/
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)
168 char *s;
170 #if HAVE_DOS_PATHS
171 s = strrchr(path, '\\');
172 if (s)
173 path = s + 1;
174 s = strrchr(path, ':');
175 if (s)
176 path = s + 1;
177 #endif
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};
185 if (ret.len == 0)
186 return bstr(".");
187 return ret;
190 char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
192 if (p1.len == 0)
193 return bstrdup0(talloc_ctx, p2);
194 if (p2.len == 0)
195 return bstrdup0(talloc_ctx, p1);
197 #if HAVE_DOS_PATHS
198 if (p2.len >= 2 && p2.start[1] == ':'
199 || p2.start[0] == '\\' || p2.start[0] == '/')
200 #else
201 if (p2.start[0] == '/')
202 #endif
203 return bstrdup0(talloc_ctx, p2); // absolute path
205 bool have_separator;
206 int endchar1 = p1.start[p1.len - 1];
207 #if HAVE_DOS_PATHS
208 have_separator = endchar1 == '/' || endchar1 == '\\'
209 || p1.len == 2 && endchar1 == ':'; // "X:" only
210 #else
211 have_separator = endchar1 == '/';
212 #endif
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)
220 struct stat st;
221 return mp_stat(path, &st) == 0;
224 bool mp_path_isdir(const char *path)
226 struct stat st;
227 return mp_stat(path, &st) == 0 && S_ISDIR(st.st_mode);