Mercurial > mplayer.hg
comparison path.c @ 34897:9f5d0eade1bd
Reduce code duplication in get_path.
Also avoid using a pointer to an on-stack array outside the
array's scope, a compiler might use its stack area for other variables
or it might optimize away all code modifying it right before it leaves
the scope, thus completely breaking the code.
author | reimar |
---|---|
date | Sat, 02 Jun 2012 17:59:01 +0000 |
parents | 139743f7e085 |
children |
comparison
equal
deleted
inserted
replaced
34896:139743f7e085 | 34897:9f5d0eade1bd |
---|---|
43 #endif | 43 #endif |
44 | 44 |
45 #include "osdep/osdep.h" | 45 #include "osdep/osdep.h" |
46 | 46 |
47 char *get_path(const char *filename){ | 47 char *get_path(const char *filename){ |
48 // temporary buffer that will be freed | |
49 char *tmp = 0; | |
48 char *homedir; | 50 char *homedir; |
49 char *buff; | 51 char *buff; |
50 #ifdef __MINGW32__ | 52 #ifdef __MINGW32__ |
51 const char *config_dir = "/mplayer"; | 53 const char *config_dir = "/mplayer"; |
52 #else | 54 #else |
63 #endif | 65 #endif |
64 | 66 |
65 if ((homedir = getenv("MPLAYER_HOME")) != NULL) | 67 if ((homedir = getenv("MPLAYER_HOME")) != NULL) |
66 config_dir = ""; | 68 config_dir = ""; |
67 else if ((homedir = getenv("HOME")) == NULL) | 69 else if ((homedir = getenv("HOME")) == NULL) |
68 #if defined(__MINGW32__) || defined(__CYGWIN__) | |
69 /* Hack to get fonts etc. loaded outside of Cygwin environment. */ | |
70 { | 70 { |
71 int i,imax=0; | 71 #if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__) |
72 char exedir[260]; | 72 return NULL; |
73 GetModuleFileNameA(NULL, exedir, 260); | 73 #else |
74 for (i=0; i< strlen(exedir); i++) | 74 int i; |
75 if (exedir[i] =='\\') | 75 char path[260]; |
76 {exedir[i]='/'; imax=i;} | 76 #ifdef __OS2__ |
77 exedir[imax]='\0'; | 77 PPIB ppib; |
78 homedir = exedir; | 78 // Get process info blocks |
79 DosGetInfoBlocks(NULL, &ppib); | |
80 // Get full path of the executable | |
81 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path); | |
82 #else | |
83 GetModuleFileNameA(NULL, path, 260); | |
84 #endif | |
85 // Extract directory part | |
86 tmp = homedir = mp_dirname(path); | |
87 // Convert backslashes to slashes | |
88 for (i = 0; homedir[i]; i++) | |
89 if (homedir[i] == '\\') homedir[i] = '/'; | |
90 // If there is a trailing slash remove it. | |
91 // If there isn't, remove the one from config dir (if homedir | |
92 // ends up e.g. c:) | |
93 if (i && homedir[i-1] == '/') homedir[i-1] = 0; | |
94 else config_dir++; | |
95 #endif | |
79 } | 96 } |
80 #elif defined(__OS2__) | |
81 { | |
82 PPIB ppib; | |
83 char path[260]; | |
84 | |
85 // Get process info blocks | |
86 DosGetInfoBlocks(NULL, &ppib); | |
87 | |
88 // Get full path of the executable | |
89 DosQueryModuleName(ppib->pib_hmte, sizeof( path ), path); | |
90 | |
91 // Truncate name part including last backslash | |
92 *strrchr(path, '\\') = 0; | |
93 | |
94 // Convert backslash to slash | |
95 _fnslashify(path); | |
96 | |
97 homedir = path; | |
98 } | |
99 #else | |
100 return NULL; | |
101 #endif | |
102 len = strlen(homedir) + strlen(config_dir) + 1; | 97 len = strlen(homedir) + strlen(config_dir) + 1; |
103 if (filename == NULL) { | 98 if (filename == NULL) { |
104 if ((buff = malloc(len)) == NULL) | 99 if ((buff = malloc(len)) == NULL) |
105 return NULL; | 100 return NULL; |
106 sprintf(buff, "%s%s", homedir, config_dir); | 101 sprintf(buff, "%s%s", homedir, config_dir); |
149 strcat(buff,"/"); | 144 strcat(buff,"/"); |
150 strcat(buff, filename); | 145 strcat(buff, filename); |
151 } | 146 } |
152 } | 147 } |
153 #endif | 148 #endif |
149 free(tmp); | |
154 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff); | 150 mp_msg(MSGT_GLOBAL,MSGL_V,"get_path('%s') -> '%s'\n",filename,buff); |
155 return buff; | 151 return buff; |
156 } | 152 } |
157 | 153 |
158 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) | 154 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL) |