mixer: reindent/cosmetic changes
[mplayer.git] / path.c
blobb6b87d4e9b5c294a889a4ded37d89850889e524d
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/osdep.h"
50 #include "osdep/io.h"
52 char *get_path(const char *filename){
53 char *homedir;
54 char *buff;
55 #ifdef __MINGW32__
56 static char *config_dir = "/mplayer";
57 #else
58 static char *config_dir = "/.mplayer";
59 #endif
60 int len;
61 #ifdef CONFIG_MACOSX_BUNDLE
62 struct stat dummy;
63 CFIndex maxlen=256;
64 CFURLRef res_url_ref=NULL;
65 CFURLRef bdl_url_ref=NULL;
66 char *res_url_path = NULL;
67 char *bdl_url_path = NULL;
68 #endif
70 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
71 config_dir = "";
72 else if ((homedir = getenv("HOME")) == NULL)
73 #if defined(__MINGW32__) || defined(__CYGWIN__)
74 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
76 int i,imax=0;
77 char exedir[260];
78 GetModuleFileNameA(NULL, exedir, 260);
79 for (i=0; i< strlen(exedir); i++)
80 if (exedir[i] =='\\')
81 {exedir[i]='/'; imax=i;}
82 exedir[imax]='\0';
83 homedir = exedir;
85 #elif defined(__OS2__)
87 PPIB ppib;
88 char path[260];
90 // Get process info blocks
91 DosGetInfoBlocks(NULL, &ppib);
93 // Get full path of the executable
94 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
96 // Truncate name part including last backslash
97 *strrchr(path, '\\') = 0;
99 // Convert backslash to slash
100 _fnslashify(path);
102 homedir = path;
104 #else
105 return NULL;
106 #endif
107 len = strlen(homedir) + strlen(config_dir) + 1;
108 if (filename == NULL) {
109 if ((buff = malloc(len)) == NULL)
110 return NULL;
111 sprintf(buff, "%s%s", homedir, config_dir);
112 } else {
113 len += strlen(filename) + 1;
114 if ((buff = malloc(len)) == NULL)
115 return NULL;
116 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
119 #ifdef CONFIG_MACOSX_BUNDLE
120 if (stat(buff, &dummy)) {
122 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
123 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
125 if (res_url_ref&&bdl_url_ref) {
127 res_url_path=malloc(maxlen);
128 bdl_url_path=malloc(maxlen);
130 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
131 maxlen*=2;
132 res_url_path=realloc(res_url_path, maxlen);
134 CFRelease(res_url_ref);
136 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
137 maxlen*=2;
138 bdl_url_path=realloc(bdl_url_path, maxlen);
140 CFRelease(bdl_url_ref);
142 if (strcmp(res_url_path, bdl_url_path) == 0)
143 res_url_path = NULL;
146 if (res_url_path&&filename) {
147 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
148 maxlen=strlen(filename)+strlen(res_url_path)+2;
150 free(buff);
151 buff = malloc(maxlen);
152 strcpy(buff, res_url_path);
154 strcat(buff,"/");
155 strcat(buff, filename);
158 #endif
159 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
160 return buff;
163 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
164 void set_path_env(void)
166 /*make our codec dirs available for LoadLibraryA()*/
167 char win32path[MAX_PATH];
168 #ifdef __CYGWIN__
169 cygwin_conv_to_full_win32_path(BINARY_CODECS_PATH, win32path);
170 #else /*__CYGWIN__*/
171 /* Expand to absolute path unless it's already absolute */
172 if (!strstr(BINARY_CODECS_PATH,":") && BINARY_CODECS_PATH[0] != '\\') {
173 GetModuleFileNameA(NULL, win32path, MAX_PATH);
174 strcpy(strrchr(win32path, '\\') + 1, BINARY_CODECS_PATH);
176 else strcpy(win32path, BINARY_CODECS_PATH);
177 #endif /*__CYGWIN__*/
178 mp_msg(MSGT_WIN32, MSGL_V, "Setting PATH to %s\n", win32path);
179 if (!SetEnvironmentVariableA("PATH", win32path))
180 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
182 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */
184 char *codec_path = BINARY_CODECS_PATH;
186 char *mp_basename(const char *path)
188 char *s;
190 #if HAVE_DOS_PATHS
191 s = strrchr(path, '\\');
192 if (s)
193 path = s + 1;
194 s = strrchr(path, ':');
195 if (s)
196 path = s + 1;
197 #endif
198 s = strrchr(path, '/');
199 return s ? s + 1 : (char *)path;
202 struct bstr mp_dirname(const char *path)
204 struct bstr ret = {(uint8_t *)path, mp_basename(path) - path};
205 if (ret.len == 0)
206 return bstr(".");
207 return ret;
210 char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2)
212 if (p1.len == 0)
213 return bstrdup0(talloc_ctx, p2);
214 if (p2.len == 0)
215 return bstrdup0(talloc_ctx, p1);
217 #if HAVE_DOS_PATHS
218 if (p2.len >= 2 && p2.start[1] == ':'
219 || p2.start[0] == '\\' || p2.start[0] == '/')
220 #else
221 if (p2.start[0] == '/')
222 #endif
223 return bstrdup0(talloc_ctx, p2); // absolute path
225 bool have_separator;
226 int endchar1 = p1.start[p1.len - 1];
227 #if HAVE_DOS_PATHS
228 have_separator = endchar1 == '/' || endchar1 == '\\'
229 || p1.len == 2 && endchar1 == ':'; // "X:" only
230 #else
231 have_separator = endchar1 == '/';
232 #endif
234 return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1),
235 have_separator ? "" : "/", BSTR_P(p2));
238 bool mp_path_exists(const char *path)
240 struct stat st;
241 return mp_stat(path, &st) == 0;
244 bool mp_path_isdir(const char *path)
246 struct stat st;
247 return mp_stat(path, &st) == 0 && S_ISDIR(st.st_mode);