77 *
88 *
99 * 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 $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1414
1515#ifndef FRONTEND
1616#include "postgres.h"
17+ #define malloc (l )palloc(l)
18+ #define free (p )pfree(p)
19+ #define strdup (p )pstrdup(p)
1720#else
1821#include "postgres_fe.h"
1922#endif
@@ -178,12 +181,15 @@ validate_exec(char *path)
178181int
179182find_my_exec (const char * argv0 ,char * full_path )
180183{
181- char buf [MAXPGPATH + 2 ];
184+ char cwd [MAXPGPATH ];
182185char * p ;
183186char * path ,
184187* startp ,
185188* endp ;
186189
190+ if (!getcwd (cwd ,MAXPGPATH ))
191+ cwd [0 ]= '\0' ;
192+
187193/*
188194 * First try: use the binary that's located in the
189195 * same directory if it was invoked with an explicit path.
@@ -195,31 +201,42 @@ find_my_exec(const char *argv0, char *full_path)
195201 * it).
196202 */
197203/* Does argv0 have a separator? */
198- if (argv0 && (p = last_path_separator (argv0 )))
204+ if ((p = last_path_separator (argv0 )))
199205{
200206if (* ++ p == '\0' )
201207{
202208log_error ("argv[0] ends with a path separator \"%s\"" ,argv0 );
203209return -1 ;
204210}
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 )
211219{
212- strncpy (full_path ,buf ,MAXPGPATH );
213220win32_make_absolute (full_path );
214221return 0 ;
215222}
216223else
217224{
218- log_error ("invalid binary \"%s\"" ,buf );
225+ log_error ("invalid binary \"%s\"" ,full_path );
219226return -1 ;
220227}
221228}
222229
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+
223240/*
224241 * Second try: since no explicit path was supplied, the user must have
225242 * been relying on PATH. We'll use the same PATH.
@@ -235,24 +252,23 @@ find_my_exec(const char *argv0, char *full_path)
235252continue ;
236253if (endp )
237254* 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 ))
246263{
247264case 0 :/* found ok */
248- strncpy (full_path ,buf ,MAXPGPATH );
249265win32_make_absolute (full_path );
250266free (path );
251267return 0 ;
252268case -1 :/* wasn't even a candidate, keep looking */
253269break ;
254270case -2 :/* found but disqualified */
255- log_error ("could not read binary \"%s\"" ,buf );
271+ log_error ("could not read binary \"%s\"" ,full_path );
256272free (path );
257273return -1 ;
258274}
@@ -270,9 +286,8 @@ find_my_exec(const char *argv0, char *full_path)
270286 *Win32 has a native way to find the executable name, but the above
271287 *method works too.
272288 */
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 ());
276291#endif
277292}
278293