sync with en/mplayer.1 r29731
[mplayer/glamo.git] / get_path.c
blob6ef074b86951b12545103d32a5c26a2a5dde1654
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 #elif defined(__CYGWIN__)
24 #include <windows.h>
25 #include <sys/cygwin.h>
26 #elif defined(__OS2__)
27 #define INCL_DOS
28 #include <os2.h>
29 #endif
31 char *get_path(const char *filename){
32 char *homedir;
33 char *buff;
34 #ifdef __MINGW32__
35 static char *config_dir = "/mplayer";
36 #else
37 static char *config_dir = "/.mplayer";
38 #endif
39 int len;
40 #ifdef CONFIG_MACOSX_BUNDLE
41 struct stat dummy;
42 CFIndex maxlen=256;
43 CFURLRef res_url_ref=NULL;
44 CFURLRef bdl_url_ref=NULL;
45 char *res_url_path = NULL;
46 char *bdl_url_path = NULL;
47 #endif
49 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
50 config_dir = "";
51 else if ((homedir = getenv("HOME")) == NULL)
52 #if defined(__MINGW32__) || defined(__CYGWIN__)
53 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
55 int i,imax=0;
56 char exedir[260];
57 GetModuleFileNameA(NULL, exedir, 260);
58 for (i=0; i< strlen(exedir); i++)
59 if (exedir[i] =='\\')
60 {exedir[i]='/'; imax=i;}
61 exedir[imax]='\0';
62 homedir = exedir;
64 #elif defined(__OS2__)
66 PPIB ppib;
67 char path[260];
69 // Get process info blocks
70 DosGetInfoBlocks(NULL, &ppib);
72 // Get full path of the executable
73 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path);
75 // Truncate name part including last backslash
76 *strrchr(path, '\\') = 0;
78 // Convert backslash to slash
79 _fnslashify(path);
81 homedir = path;
83 #else
84 return NULL;
85 #endif
86 len = strlen(homedir) + strlen(config_dir) + 1;
87 if (filename == NULL) {
88 if ((buff = malloc(len)) == NULL)
89 return NULL;
90 sprintf(buff, "%s%s", homedir, config_dir);
91 } else {
92 len += strlen(filename) + 1;
93 if ((buff = malloc(len)) == NULL)
94 return NULL;
95 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
98 #ifdef CONFIG_MACOSX_BUNDLE
99 if (stat(buff, &dummy)) {
101 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
102 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
104 if (res_url_ref&&bdl_url_ref) {
106 res_url_path=malloc(maxlen);
107 bdl_url_path=malloc(maxlen);
109 while (!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
110 maxlen*=2;
111 res_url_path=realloc(res_url_path, maxlen);
113 CFRelease(res_url_ref);
115 while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
116 maxlen*=2;
117 bdl_url_path=realloc(bdl_url_path, maxlen);
119 CFRelease(bdl_url_ref);
121 if (strcmp(res_url_path, bdl_url_path) == 0)
122 res_url_path = NULL;
125 if (res_url_path&&filename) {
126 if ((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
127 maxlen=strlen(filename)+strlen(res_url_path)+2;
129 free(buff);
130 buff = malloc(maxlen);
131 strcpy(buff, res_url_path);
133 strcat(buff,"/");
134 strcat(buff, filename);
137 #endif
138 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
139 return buff;
142 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
143 void set_path_env(void)
145 /*make our codec dirs available for LoadLibraryA()*/
146 char tmppath[MAX_PATH*2 + 1];
147 char win32path[MAX_PATH];
148 char realpath[MAX_PATH];
149 #ifdef __CYGWIN__
150 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
151 strcpy(tmppath,win32path);
152 #ifdef CONFIG_REALCODECS
153 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
154 sprintf(tmppath,"%s;%s",win32path,realpath);
155 #endif /*CONFIG_REALCODECS*/
156 #else /*__CYGWIN__*/
157 /* Expand to absolute path unless it's already absolute */
158 if (!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
159 GetModuleFileNameA(NULL, win32path, MAX_PATH);
160 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
162 else strcpy(win32path,WIN32_PATH);
163 strcpy(tmppath,win32path);
164 #ifdef CONFIG_REALCODECS
165 /* Expand to absolute path unless it's already absolute */
166 if (!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
167 GetModuleFileNameA(NULL, realpath, MAX_PATH);
168 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
170 else strcpy(realpath,REALCODEC_PATH);
171 sprintf(tmppath,"%s;%s",win32path,realpath);
172 #endif /*CONFIG_REALCODECS*/
173 #endif /*__CYGWIN__*/
174 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
175 if (!SetEnvironmentVariableA("PATH", tmppath))
176 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
178 #endif /* (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) */