libcdio
[mplayer.git] / get_path.c
blob47b50da40055c70642312b6ff1efa68dc1179dd1
2 /*
3 * Get path to config dir/file.
5 * Return Values:
6 * Returns the pointer to the ALLOCATED buffer containing the
7 * zero terminated path string. This buffer has to be FREED
8 * by the caller.
11 #ifdef MACOSX_BUNDLE
12 #include <CoreFoundation/CoreFoundation.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #endif
21 char *get_path(char *filename){
22 char *homedir;
23 char *buff;
24 #if defined(__MINGW32__)
25 static char *config_dir = "/mplayer";
26 #else
27 static char *config_dir = "/.mplayer";
28 #endif
29 int len;
30 #ifdef MACOSX_BUNDLE
31 struct stat dummy;
32 CFIndex maxlen=256;
33 CFURLRef res_url_ref=NULL;
34 CFURLRef bdl_url_ref=NULL;
35 char *res_url_path = NULL;
36 char *bdl_url_path = NULL;
37 #endif
39 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
40 config_dir = "";
41 else if ((homedir = getenv("HOME")) == NULL)
42 #if defined(__MINGW32__)||defined(__CYGWIN__) /*hack to get fonts etc. loaded outside of cygwin environment*/
44 int i,imax=0;
45 char exedir[260];
46 GetModuleFileNameA(NULL, exedir, 260);
47 for(i=0; i< strlen(exedir);i++)if(exedir[i] =='\\'){exedir[i]='/';imax=i;}
48 exedir[imax]='\0';
49 homedir = exedir;
51 #else
52 return NULL;
53 #endif
54 len = strlen(homedir) + strlen(config_dir) + 1;
55 if (filename == NULL) {
56 if ((buff = (char *) malloc(len)) == NULL)
57 return NULL;
58 sprintf(buff, "%s%s", homedir, config_dir);
59 } else {
60 len += strlen(filename) + 1;
61 if ((buff = (char *) malloc(len)) == NULL)
62 return NULL;
63 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
66 #ifdef MACOSX_BUNDLE
67 if(stat(buff, &dummy)) {
69 res_url_ref=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
70 bdl_url_ref=CFBundleCopyBundleURL(CFBundleGetMainBundle());
72 if(res_url_ref&&bdl_url_ref) {
74 res_url_path=malloc(maxlen);
75 bdl_url_path=malloc(maxlen);
77 while(!CFURLGetFileSystemRepresentation(res_url_ref, true, res_url_path, maxlen)) {
78 maxlen*=2;
79 res_url_path=realloc(res_url_path, maxlen);
81 CFRelease(res_url_ref);
83 while(!CFURLGetFileSystemRepresentation(bdl_url_ref, true, bdl_url_path, maxlen)) {
84 maxlen*=2;
85 bdl_url_path=realloc(bdl_url_path, maxlen);
87 CFRelease(bdl_url_ref);
89 if( strcmp(res_url_path, bdl_url_path) == 0)
90 res_url_path = NULL;
93 if(res_url_path&&filename) {
94 if((strlen(filename)+strlen(res_url_path)+2)>maxlen) {
95 maxlen=strlen(filename)+strlen(res_url_path)+2;
97 free(buff);
98 buff = (char *) malloc(maxlen);
99 strcpy(buff, res_url_path);
101 strcat(buff,"/");
102 strcat(buff, filename);
105 #endif
106 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
107 return buff;
110 #if defined(WIN32) && defined(USE_WIN32DLL)
111 void set_path_env()
113 /*make our codec dirs available for LoadLibraryA()*/
114 char tmppath[MAX_PATH*2 + 1];
115 char win32path[MAX_PATH];
116 char realpath[MAX_PATH];
117 #ifdef __CYGWIN__
118 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
119 strcpy(tmppath,win32path);
120 #ifdef USE_REALCODECS
121 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
122 sprintf(tmppath,"%s;%s",win32path,realpath);
123 #endif /*USE_REALCODECS*/
124 #else /*__CYGWIN__*/
125 /* Expand to absolute path unless it's already absolute */
126 if(!strstr(WIN32_PATH,":") && WIN32_PATH[0] != '\\'){
127 GetModuleFileNameA(NULL, win32path, MAX_PATH);
128 strcpy(strrchr(win32path, '\\') + 1, WIN32_PATH);
130 else strcpy(win32path,WIN32_PATH);
131 strcpy(tmppath,win32path);
132 #ifdef USE_REALCODECS
133 /* Expand to absolute path unless it's already absolute */
134 if(!strstr(REALCODEC_PATH,":") && REALCODEC_PATH[0] != '\\'){
135 GetModuleFileNameA(NULL, realpath, MAX_PATH);
136 strcpy(strrchr(realpath, '\\') + 1, REALCODEC_PATH);
138 else strcpy(realpath,REALCODEC_PATH);
139 sprintf(tmppath,"%s;%s",win32path,realpath);
140 #endif /*USE_REALCODECS*/
141 #endif /*__CYGWIN__*/
142 mp_msg(MSGT_WIN32, MSGL_V,"Setting PATH to %s\n",tmppath);
143 if (!SetEnvironmentVariableA("PATH", tmppath))
144 mp_msg(MSGT_WIN32, MSGL_WARN, "Cannot set PATH!");
146 #endif /*WIN32 && USE_WIN32DLL*/