Merge svn changes up to r27399
[mplayer.git] / get_path.c
blobb1f119e450b1375454f82e069e19e208dd0939e9
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 MACOSX_BUNDLE
17 #include <CoreFoundation/CoreFoundation.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #endif
23 #ifdef WIN32
24 #include <windows.h>
25 #endif
27 #ifdef __OS2__
28 #define INCL_DOS
29 #include <os2.h>
30 #endif
32 char *get_path(const char *filename){
33 char *homedir;
34 char *buff;
35 #ifdef __MINGW32__
36 static char *config_dir = "/mplayer";
37 #else
38 static char *config_dir = "/.mplayer";
39 #endif
40 int len;
41 #ifdef MACOSX_BUNDLE
42 struct stat dummy;
43 CFIndex maxlen=256;
44 CFURLRef res_url_ref=NULL;
45 CFURLRef bdl_url_ref=NULL;
46 char *res_url_path = NULL;
47 char *bdl_url_path = NULL;
48 #endif
50 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
51 config_dir = "";
52 else if ((homedir = getenv("HOME")) == NULL)
53 #if defined(__MINGW32__) || defined(__CYGWIN__)
54 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
56 int i,imax=0;
57 char exedir[260];
58 GetModuleFileNameA(NULL, exedir, 260);
59 for (i=0; i< strlen(exedir); i++)
60 if (exedir[i] =='\\')
61 {exedir[i]='/'; imax=i;}
62 exedir[imax]='\0';
63 homedir = exedir;
65 #elif defined(__OS2__)
67 PPIB ppib;
68 char path[260];
70 // Get process info blocks
71 DosGetInfoBlocks(NULL, &ppib);
73 // Get full path of the executable
74 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
76 // Truncate name part including last backslash
77 *strrchr(path, '\\') = 0;
79 // Convert backslash to slash
80 _fnslashify(path);
82 homedir = path;
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 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(WIN32) && defined(CONFIG_WIN32DLL)
144 void set_path_env()
146 /*make our codec dirs available for LoadLibraryA()*/
147 char tmppath[MAX_PATH*2 + 1];
148 char win32path[MAX_PATH];
149 char realpath[MAX_PATH];
150 #ifdef __CYGWIN__
151 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
152 strcpy(tmppath,win32path);
153 #ifdef CONFIG_REALCODECS
154 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
155 sprintf(tmppath,"%s;%s",win32path,realpath);
156 #endif /*CONFIG_REALCODECS*/
157 #else /*__CYGWIN__*/
158 /* Expand to absolute path unless it's already absolute */
159 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
160 GetModuleFileNameA(NULL, win32path, MAX_PATH);
161 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
163 else strcpy(win32path,WIN32_PATH);
164 strcpy(tmppath,win32path);
165 #ifdef CONFIG_REALCODECS
166 /* Expand to absolute path unless it's already absolute */
167 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
168 GetModuleFileNameA(NULL, realpath, MAX_PATH);
169 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
171 else strcpy(realpath,REALCODEC_PATH);
172 sprintf(tmppath,"%s;%s",win32path,realpath);
173 #endif /*CONFIG_REALCODECS*/
174 #endif /*__CYGWIN__*/
175 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
176 if (!SetEnvironmentVariableA("PATH", tmppath))
177 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
179 #endif /*WIN32 && CONFIG_WIN32DLL*/