core: audio: cut audio writes at end of timeline part
[mplayer/greg.git] / path.c
blobf97e8a6c3a788bb39e4ea7ee2f78207e86aedf95
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 "config.h"
31 #include "mp_msg.h"
32 #include "path.h"
34 #ifdef CONFIG_MACOSX_BUNDLE
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #elif defined(__MINGW32__)
40 #include <windows.h>
41 #elif defined(__CYGWIN__)
42 #include <windows.h>
43 #include <sys/cygwin.h>
44 #endif
46 #include "talloc.h"
48 #include "osdep/osdep.h"
50 char *get_path(const char *filename){
51 char *homedir;
52 char *buff;
53 #ifdef __MINGW32__
54 static char *config_dir = "/mplayer";
55 #else
56 static char *config_dir = "/.mplayer";
57 #endif
58 int len;
59 #ifdef CONFIG_MACOSX_BUNDLE
60 struct stat dummy;
61 CFIndex maxlen=256;
62 CFURLRef res_url_ref=NULL;
63 CFURLRef bdl_url_ref=NULL;
64 char *res_url_path = NULL;
65 char *bdl_url_path = NULL;
66 #endif
68 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
69 config_dir = "";
70 else if ((homedir = getenv("HOME")) == NULL)
71 #if defined(__MINGW32__) || defined(__CYGWIN__)
72 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
74 int i,imax=0;
75 char exedir[260];
76 GetModuleFileNameA(NULL, exedir, 260);
77 for (i=0; i< strlen(exedir); i++)
78 if (exedir[i] =='\\')
79 {exedir[i]='/'; imax=i;}
80 exedir[imax]='\0';
81 homedir = exedir;
83 #elif defined(__OS2__)
85 PPIB ppib;
86 char path[260];
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
98 _fnslashify(path);
100 homedir = path;
102 #else
103 return NULL;
104 #endif
105 len = strlen(homedir) + strlen(config_dir) + 1;
106 if (filename == NULL) {
107 if ((buff = malloc(len)) == NULL)
108 return NULL;
109 sprintf(buff, "%s%s", homedir, config_dir);
110 } else {
111 len += strlen(filename) + 1;
112 if ((buff = malloc(len)) == NULL)
113 return 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)) {
129 maxlen*=2;
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)) {
135 maxlen*=2;
136 bdl_url_path=realloc(bdl_url_path, maxlen);
138 CFRelease(bdl_url_ref);
140 if (strcmp(res_url_path, bdl_url_path) == 0)
141 res_url_path = NULL;
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;
148 free(buff);
149 buff = malloc(maxlen);
150 strcpy(buff, res_url_path);
152 strcat(buff,"/");
153 strcat(buff, filename);
156 #endif
157 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
158 return 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];
166 #ifdef __CYGWIN__
167 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
168 #else /*__CYGWIN__*/
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 static int needs_free = 0;
186 void set_codec_path(const char *path)
188 if (needs_free)
189 free(codec_path);
190 if (path == 0) {
191 codec_path = BINARY_CODECS_PATH;
192 needs_free = 0;
193 return;
195 codec_path = malloc(strlen(path) + 1);
196 strcpy(codec_path, path);
197 needs_free = 1;
200 char *mp_basename(const char *path)
202 char *s;
204 #if HAVE_DOS_PATHS
205 s = strrchr(path, '\\');
206 if (s)
207 path = s + 1;
208 s = strrchr(path, ':');
209 if (s)
210 path = s + 1;
211 #endif
212 s = strrchr(path, '/');
213 return s ? s + 1 : (char *)path;
216 struct bstr mp_dirname(const char *path)
218 struct bstr ret = {(uint8_t *)path, mp_basename(path) - path};
219 if (ret.len == 0)
220 return BSTR(".");
221 return ret;
224 char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
226 if (p1.len == 0)
227 return bstrdup0(talloc_ctx, p2);
228 if (p2.len == 0)
229 return bstrdup0(talloc_ctx, p1);
231 #if HAVE_DOS_PATHS
232 if (p2.len >= 2 && p2.start[1] == ':'
233 || p2.start[0] == '\\' || p2.start[0] == '/')
234 #else
235 if (p2.start[0] == '/')
236 #endif
237 return bstrdup0(talloc_ctx, p2); // absolute path
239 bool have_separator;
240 int endchar1 = p1.start[p1.len - 1];
241 #if HAVE_DOS_PATHS
242 have_separator = endchar1 == '/' || endchar1 == '\\'
243 || p1.len == 2 && endchar1 == ':'; // "X:" only
244 #else
245 have_separator = endchar1 == '/';
246 #endif
248 return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
249 have_separator ? "" : "/", BSTR_P(p2));