remove useless casts
[mplayer/glamo.git] / get_path.c
blobeb889cc01f3f91b0905baedfd638de4e284c816f
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"
15 #ifdef MACOSX_BUNDLE
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #endif
22 #ifdef WIN32
23 #include <windows.h>
24 #endif
26 char *get_path(const char *filename){
27 char *homedir;
28 char *buff;
29 #ifdef __MINGW32__
30 static char *config_dir = "/mplayer";
31 #else
32 static char *config_dir = "/.mplayer";
33 #endif
34 int len;
35 #ifdef MACOSX_BUNDLE
36 struct stat dummy;
37 CFIndex maxlen=256;
38 CFURLRef res_url_ref=NULL;
39 CFURLRef bdl_url_ref=NULL;
40 char *res_url_path = NULL;
41 char *bdl_url_path = NULL;
42 #endif
44 if ((homedir = getenv("MPLAYER_HOME")) != NULL)
45 config_dir = "";
46 else if ((homedir = getenv("HOME")) == NULL)
47 #if defined(__MINGW32__) || defined(__CYGWIN__)
48 /* Hack to get fonts etc. loaded outside of Cygwin environment. */
50 int i,imax=0;
51 char exedir[260];
52 GetModuleFileNameA(NULL, exedir, 260);
53 for (i=0; i< strlen(exedir); i++)
54 if (exedir[i] =='\\')
55 {exedir[i]='/'; imax=i;}
56 exedir[imax]='\0';
57 homedir = exedir;
59 #else
60 return NULL;
61 #endif
62 len = strlen(homedir) + strlen(config_dir) + 1;
63 if (filename == NULL) {
64 if ((buff = malloc(len)) == NULL)
65 return NULL;
66 sprintf(buff, "%s%s", homedir, config_dir);
67 } else {
68 len += strlen(filename) + 1;
69 if ((buff = malloc(len)) == NULL)
70 return NULL;
71 sprintf(buff, "%s%s/%s", homedir, config_dir, filename);
74 #ifdef MACOSX_BUNDLE
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)) {
86 maxlen*=2;
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)) {
92 maxlen*=2;
93 bdl_url_path=realloc(bdl_url_path, maxlen);
95 CFRelease(bdl_url_ref);
97 if (strcmp(res_url_path, bdl_url_path) == 0)
98 res_url_path = NULL;
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;
105 free(buff);
106 buff = malloc(maxlen);
107 strcpy(buff, res_url_path);
109 strcat(buff,"/");
110 strcat(buff, filename);
113 #endif
114 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff);
115 return buff;
118 #if defined(WIN32) && defined(USE_WIN32DLL)
119 void set_path_env()
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];
125 #ifdef __CYGWIN__
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*/
132 #else /*__CYGWIN__*/
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*/