Updated ffmpeg
[mplayer/kovensky.git] / get_path.c
blobf903fcfe29c8e89f56aa5ba3bda068d228895273
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.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "mp_msg.h"
14 #include "get_path.h"
16 #ifdef CONFIG_MACOSX_BUNDLE
17 #include <CoreFoundation/CoreFoundation.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #elif defined(__MINGW32__)
22 #include <windows.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #elif defined(__CYGWIN__)
26 #include <windows.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/cygwin.h>
30 #elif defined(__OS2__)
31 #define INCL_DOS
32 #include <os2.h>
33 #endif
35 char *get_path(const char *filename){
36 char *homedir;
37 char *buff;
38 #ifdef _WIN32
39 static char *config_dir = "/mplayer";
40 #else
41 static char *config_dir = "/.mplayer";
42 #endif
43 int len;
44 #ifdef CONFIG_MACOSX_BUNDLE
45 struct stat dummy;
46 CFIndex maxlen=256;
47 CFURLRef res_url_ref=NULL;
48 CFURLRef bdl_url_ref=NULL;
49 char *res_url_path = NULL;
50 char *bdl_url_path = NULL;
51 #endif
53 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
54 config_dir = "";
55 else if ((homedir = getenv("HOME")) == NULL)
56 #if _WIN32
57 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
59 int i,imax=0;
60 struct _stat statBuffer;
61 char exedir[MAX_PATH];
62 char config_path[MAX_PATH];
63 char *appdata = getenv("appdata");
64 if (appdata && strcmp(appdata, "")) {
65 strncpy(exedir, appdata, MAX_PATH);
66 exedir[MAX_PATH-1] = '\0';
67 } else {
68 GetModuleFileNameA(NULL, exedir, MAX_PATH);
70 for (i=0; i< strlen(exedir); i++)
71 if (exedir[i] =='\\')
72 exedir[i]='/';
74 sprintf(config_path, "%s%s", exedir, config_dir);
75 int stat_res;
76 if ((stat_res = stat(config_path, &statBuffer)) < 0 ||
77 !(statBuffer.st_mode & S_IFDIR)) {
78 GetModuleFileNameA(NULL, exedir, MAX_PATH);
79 for (i=0; i< strlen(exedir); i++)
80 if (exedir[i] =='\\')
81 {exedir[i]='/'; imax=i;}
82 exedir[imax]='\0';
85 homedir = exedir;
87 #elif defined(__OS2__)
89 PPIB ppib;
90 char path[260];
92 // Get process info blocks
93 DosGetInfoBlocks(NULL, &ppib);
95 // Get full path of the executable
96 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
98 // Truncate name part including last backslash
99 *strrchr(path, '\\') = 0;
101 // Convert backslash to slash
102 _fnslashify(path);
104 homedir = path;
106 #else
107 return NULL;
108 #endif
109 len = strlen(homedir) + strlen(config_dir) + 1;
110 if (filename == NULL) {
111 if ((buff = malloc(len)) == NULL)
112 return NULL;
113 sprintf(buff, "%s%s", homedir, config_dir);
114 } else {
115 len += strlen(filename) + 1;
116 if ((buff = malloc(len)) == NULL)
117 return NULL;
118 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
121 #ifdef CONFIG_MACOSX_BUNDLE
122 if (stat(buff, &dummy)) {
124 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
125 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
127 if (res_url_ref&&bdl_url_ref) {
129 res_url_path=malloc(maxlen);
130 bdl_url_path=malloc(maxlen);
132 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
133 maxlen*=2;
134 res_url_path=realloc(res_url_path, maxlen);
136 CFRelease(res_url_ref);
138 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
139 maxlen*=2;
140 bdl_url_path=realloc(bdl_url_path, maxlen);
142 CFRelease(bdl_url_ref);
144 if (strcmp(res_url_path, bdl_url_path) == 0)
145 res_url_path = NULL;
148 if (res_url_path&&filename) {
149 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
150 maxlen=strlen(filename)+strlen(res_url_path)+2;
152 free(buff);
153 buff = malloc(maxlen);
154 strcpy(buff, res_url_path);
156 strcat(buff,"/");
157 strcat(buff, filename);
160 #endif
161 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
162 return buff;
165 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
166 void set_path_env(void)
168 /*make our codec dirs available for LoadLibraryA()*/
169 char tmppath[MAX_PATH*2 + 1];
170 char win32path[MAX_PATH];
171 char realpath[MAX_PATH];
172 #ifdef __CYGWIN__
173 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
174 strcpy(tmppath,win32path);
175 #ifdef CONFIG_REALCODECS
176 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
177 sprintf(tmppath,"%s;%s",win32path,realpath);
178 #endif /*CONFIG_REALCODECS*/
179 #else /*__CYGWIN__*/
180 /* Expand to absolute path unless it's already absolute */
181 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
182 GetModuleFileNameA(NULL, win32path, MAX_PATH);
183 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
185 else strcpy(win32path,WIN32_PATH);
186 strcpy(tmppath,win32path);
187 #ifdef CONFIG_REALCODECS
188 /* Expand to absolute path unless it's already absolute */
189 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
190 GetModuleFileNameA(NULL, realpath, MAX_PATH);
191 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
193 else strcpy(realpath,REALCODEC_PATH);
194 sprintf(tmppath,"%s;%s",win32path,realpath);
195 #endif /*CONFIG_REALCODECS*/
196 #endif /*__CYGWIN__*/
197 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
198 if (!SetEnvironmentVariableA("PATH", tmppath))
199 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
201 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */