7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $PostgreSQL: pgsql/src/port/exec.c,v 1.10 2004/05/19 17: 15:21 momjian Exp $
10
+ * $PostgreSQL: pgsql/src/port/exec.c,v 1.11 2004/05/20 15:35:41 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
14
14
15
15
#ifndef FRONTEND
16
16
#include "postgres.h"
17
+ #define malloc (l ) palloc(l)
18
+ #define free (p ) pfree(p)
19
+ #define strdup (p ) pstrdup(p)
17
20
#else
18
21
#include "postgres_fe.h"
19
22
#endif
@@ -178,12 +181,15 @@ validate_exec(char *path)
178
181
int
179
182
find_my_exec (const char * argv0 , char * full_path )
180
183
{
181
- char buf [MAXPGPATH + 2 ];
184
+ char cwd [MAXPGPATH ];
182
185
char * p ;
183
186
char * path ,
184
187
* startp ,
185
188
* endp ;
186
189
190
+ if (!getcwd (cwd , MAXPGPATH ))
191
+ cwd [0 ] = '\0' ;
192
+
187
193
/*
188
194
* First try: use the binary that's located in the
189
195
* same directory if it was invoked with an explicit path.
@@ -195,31 +201,42 @@ find_my_exec(const char *argv0, char *full_path)
195
201
* it).
196
202
*/
197
203
/* Does argv0 have a separator? */
198
- if (argv0 && (p = last_path_separator (argv0 )))
204
+ if ((p = last_path_separator (argv0 )))
199
205
{
200
206
if (* ++ p == '\0' )
201
207
{
202
208
log_error ("argv[0] ends with a path separator \"%s\"" , argv0 );
203
209
return -1 ;
204
210
}
205
- if (is_absolute_path (argv0 ) || !getcwd (buf , MAXPGPATH ))
206
- buf [0 ] = '\0' ;
207
- else /* path is not absolute and getcwd worked */
208
- strcat (buf , "/" );
209
- strcat (buf , argv0 );
210
- if (validate_exec (buf ) == 0 )
211
+
212
+ if (is_absolute_path (argv0 ))
213
+ StrNCpy (full_path , argv0 , MAXPGPATH );
214
+ else
215
+ snprintf (full_path , MAXPGPATH , "%s/%s" , cwd , argv0 );
216
+
217
+ canonicalize_path (full_path );
218
+ if (validate_exec (full_path ) == 0 )
211
219
{
212
- strncpy (full_path , buf , MAXPGPATH );
213
220
win32_make_absolute (full_path );
214
221
return 0 ;
215
222
}
216
223
else
217
224
{
218
- log_error ("invalid binary \"%s\"" , buf );
225
+ log_error ("invalid binary \"%s\"" , full_path );
219
226
return -1 ;
220
227
}
221
228
}
222
229
230
+ #ifdef WIN32
231
+ /* Win32 checks the current directory first for names without slashes */
232
+ if (validate_exec (argv0 ) == 0 )
233
+ {
234
+ snprintf (full_path , MAXPGPATH , "%s/%s" , cwd , argv0 );
235
+ win32_make_absolute (full_path );
236
+ return 0 ;
237
+ }
238
+ #endif
239
+
223
240
/*
224
241
* Second try: since no explicit path was supplied, the user must have
225
242
* been relying on PATH. We'll use the same PATH.
@@ -235,24 +252,23 @@ find_my_exec(const char *argv0, char *full_path)
235
252
continue ;
236
253
if (endp )
237
254
* endp = '\0' ;
238
- if ( is_absolute_path ( startp ) || ! getcwd ( buf , MAXPGPATH ))
239
- buf [ 0 ] = '\0' ;
240
- else /* path is not absolute and getcwd worked */
241
- strcat ( buf , "/" );
242
- strcat ( buf , startp );
243
- strcat ( buf , "/" );
244
- strcat ( buf , argv0 );
245
- switch (validate_exec (buf ))
255
+
256
+ if ( is_absolute_path ( startp ))
257
+ snprintf ( full_path , MAXPGPATH , "%s/%s" , startp , argv0 );
258
+ else
259
+ snprintf ( full_path , MAXPGPATH , "%s/%s/%s" , cwd , startp , argv0 );
260
+
261
+ canonicalize_path ( full_path );
262
+ switch (validate_exec (full_path ))
246
263
{
247
264
case 0 : /* found ok */
248
- strncpy (full_path , buf , MAXPGPATH );
249
265
win32_make_absolute (full_path );
250
266
free (path );
251
267
return 0 ;
252
268
case -1 : /* wasn't even a candidate, keep looking */
253
269
break ;
254
270
case -2 : /* found but disqualified */
255
- log_error ("could not read binary \"%s\"" , buf );
271
+ log_error ("could not read binary \"%s\"" , full_path );
256
272
free (path );
257
273
return -1 ;
258
274
}
@@ -270,9 +286,8 @@ find_my_exec(const char *argv0, char *full_path)
270
286
* Win32 has a native way to find the executable name, but the above
271
287
* method works too.
272
288
*/
273
- if (GetModuleFileName (NULL ,basename ,MAXPGPATH ) == 0 )
274
- ereport (FATAL ,
275
- (errmsg ("GetModuleFileName failed (%i)" ,(int )GetLastError ())));
289
+ if (GetModuleFileName (NULL ,full_path ,MAXPGPATH ) == 0 )
290
+ log_error ("GetModuleFileName failed (%i)" ,(int )GetLastError ());
276
291
#endif
277
292
}
278
293
0 commit comments