2 * Get path to config dir/file.
5 * Returns the pointer to the ALLOCATED buffer containing the
6 * zero terminated path string. This buffer has to be FREED
17 #include <CoreFoundation/CoreFoundation.h>
18 #include <sys/types.h>
32 char *get_path(const char *filename
){
36 static char *config_dir
= "/mplayer";
38 static char *config_dir
= "/.mplayer";
44 CFURLRef res_url_ref
=NULL
;
45 CFURLRef bdl_url_ref
=NULL
;
46 char *res_url_path
= NULL
;
47 char *bdl_url_path
= NULL
;
50 if ((homedir
= getenv("MPLAYER_HOME")) != NULL
)
52 else if ((homedir
= getenv("HOME")) == NULL
)
53 #if defined(__MINGW32__) || defined(__CYGWIN__)
54 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
58 GetModuleFileNameA(NULL
, exedir
, 260);
59 for (i
=0; i
< strlen(exedir
); i
++)
61 {exedir
[i
]='/'; imax
=i
;}
65 #elif defined(__OS2__)
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
87 len
= strlen(homedir
) + strlen(config_dir
) + 1;
88 if (filename
== NULL
) {
89 if ((buff
= malloc(len
)) == NULL
)
91 sprintf(buff
, "%s%s", homedir
, config_dir
);
93 len
+= strlen(filename
) + 1;
94 if ((buff
= malloc(len
)) == NULL
)
96 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
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
)) {
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
)) {
118 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
120 CFRelease(bdl_url_ref
);
122 if (strcmp(res_url_path
, bdl_url_path
) == 0)
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;
131 buff
= malloc(maxlen
);
132 strcpy(buff
, res_url_path
);
135 strcat(buff
, filename
);
139 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);
143 #if defined(WIN32) && defined(CONFIG_WIN32DLL)
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
];
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*/
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*/