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
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <sys/types.h>
26 char *get_path(const char *filename
){
30 static char *config_dir
= "/mplayer";
32 static char *config_dir
= "/.mplayer";
38 CFURLRef res_url_ref
=NULL
;
39 CFURLRef bdl_url_ref
=NULL
;
40 char *res_url_path
= NULL
;
41 char *bdl_url_path
= NULL
;
44 if ((homedir
= getenv("MPLAYER_HOME")) != NULL
)
46 else if ((homedir
= getenv("HOME")) == NULL
)
47 #if defined(__MINGW32__) || defined(__CYGWIN__)
48 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
52 GetModuleFileNameA(NULL
, exedir
, 260);
53 for (i
=0; i
< strlen(exedir
); i
++)
55 {exedir
[i
]='/'; imax
=i
;}
62 len
= strlen(homedir
) + strlen(config_dir
) + 1;
63 if (filename
== NULL
) {
64 if ((buff
= malloc(len
)) == NULL
)
66 sprintf(buff
, "%s%s", homedir
, config_dir
);
68 len
+= strlen(filename
) + 1;
69 if ((buff
= malloc(len
)) == NULL
)
71 sprintf(buff
, "%s%s/%s", homedir
, config_dir
, filename
);
75 if (stat(buff
, &dummy
)) {
77 res_url_ref
=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
78 bdl_url_ref
=CFBundleCopyBundleURL(CFBundleGetMainBundle());
80 if (res_url_ref
&&bdl_url_ref
) {
82 res_url_path
=malloc(maxlen
);
83 bdl_url_path
=malloc(maxlen
);
85 while (!CFURLGetFileSystemRepresentation(res_url_ref
, true, res_url_path
, maxlen
)) {
87 res_url_path
=realloc(res_url_path
, maxlen
);
89 CFRelease(res_url_ref
);
91 while (!CFURLGetFileSystemRepresentation(bdl_url_ref
, true, bdl_url_path
, maxlen
)) {
93 bdl_url_path
=realloc(bdl_url_path
, maxlen
);
95 CFRelease(bdl_url_ref
);
97 if (strcmp(res_url_path
, bdl_url_path
) == 0)
101 if (res_url_path
&&filename
) {
102 if ((strlen(filename
)+strlen(res_url_path
)+2)>maxlen
) {
103 maxlen
=strlen(filename
)+strlen(res_url_path
)+2;
106 buff
= malloc(maxlen
);
107 strcpy(buff
, res_url_path
);
110 strcat(buff
, filename
);
114 mp_msg(MSGT_GLOBAL
,MSGL_V
,"get_path('%s') -> '%s'\n",filename
,buff
);
118 #if defined(WIN32) && defined(USE_WIN32DLL)
121 /*make our codec dirs available for LoadLibraryA()*/
122 char tmppath
[MAX_PATH
*2 + 1];
123 char win32path
[MAX_PATH
];
124 char realpath
[MAX_PATH
];
126 cygwin_conv_to_full_win32_path(WIN32_PATH
,win32path
);
127 strcpy(tmppath
,win32path
);
128 #ifdef USE_REALCODECS
129 cygwin_conv_to_full_win32_path(REALCODEC_PATH
,realpath
);
130 sprintf(tmppath
,"%s;%s",win32path
,realpath
);
131 #endif /*USE_REALCODECS*/
133 /* Expand to absolute path unless it's already absolute */
134 if (!strstr(WIN32_PATH
,":") && WIN32_PATH
[0] != '\\'){
135 GetModuleFileNameA(NULL
, win32path
, MAX_PATH
);
136 strcpy(strrchr(win32path
, '\\') + 1, WIN32_PATH
);
138 else strcpy(win32path
,WIN32_PATH
);
139 strcpy(tmppath
,win32path
);
140 #ifdef USE_REALCODECS
141 /* Expand to absolute path unless it's already absolute */
142 if (!strstr(REALCODEC_PATH
,":") && REALCODEC_PATH
[0] != '\\'){
143 GetModuleFileNameA(NULL
, realpath
, MAX_PATH
);
144 strcpy(strrchr(realpath
, '\\') + 1, REALCODEC_PATH
);
146 else strcpy(realpath
,REALCODEC_PATH
);
147 sprintf(tmppath
,"%s;%s",win32path
,realpath
);
148 #endif /*USE_REALCODECS*/
149 #endif /*__CYGWIN__*/
150 mp_msg(MSGT_WIN32
, MSGL_V
,"Setting PATH to %s\n",tmppath
);
151 if (!SetEnvironmentVariableA("PATH", tmppath
))
152 mp_msg(MSGT_WIN32
, MSGL_WARN
, "Cannot set PATH!");
154 #endif /*WIN32 && USE_WIN32DLL*/