77 *
88 *
99 * IDENTIFICATION
10- * $PostgreSQL: pgsql/src/port/exec.c,v 1.14 2004/05/2420:23:50 momjian Exp $
10+ * $PostgreSQL: pgsql/src/port/exec.c,v 1.15 2004/05/2422:35:37 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
2828
2929#define _ (x ) gettext(x)
3030
31- #ifdef FRONTEND
32- #undef pstrdup
33- #define pstrdup (p )strdup(p)
34- #define pfree (p )free(p)
35- #endif
36-
3731/* $PATH (or %PATH%) path separator */
3832#ifdef WIN32
3933#define PATHSEP ';'
@@ -185,11 +179,8 @@ validate_exec(const char *path)
185179int
186180find_my_exec (const char * argv0 ,char * retpath )
187181{
188- char cwd [MAXPGPATH ];
189- char * p ;
190- char * path ,
191- * startp ,
192- * endp ;
182+ char cwd [MAXPGPATH ],test_path [MAXPGPATH ];
183+ char * path ;
193184
194185if (!getcwd (cwd ,MAXPGPATH ))
195186cwd [0 ]= '\0' ;
@@ -205,9 +196,9 @@ find_my_exec(const char *argv0, char *retpath)
205196 * it).
206197 */
207198/* Does argv0 have a separator? */
208- if ((p = last_path_separator (argv0 )))
199+ if ((path = last_path_separator (argv0 )))
209200{
210- if (* ++ p == '\0' )
201+ if (* ++ path == '\0' )
211202{
212203log_error ("argv[0] ends with a path separator \"%s\"" ,argv0 );
213204return -1 ;
@@ -245,41 +236,41 @@ find_my_exec(const char *argv0, char *retpath)
245236 * Second try: since no explicit path was supplied, the user must have
246237 * been relying on PATH. We'll use the same PATH.
247238 */
248- if ((p = getenv ("PATH" ))&& * p )
239+ if ((path = getenv ("PATH" ))&& * path )
249240{
250- path = pstrdup (p );/* make a modifiable copy */
251- for (startp = path ,endp = strchr (path ,PATHSEP );
252- startp && * startp ;
253- startp = endp + 1 ,endp = strchr (startp ,PATHSEP ))
241+ char * startp = NULL ,* endp = NULL ;
242+
243+ do
254244{
255- if (startp == endp ) /* it's a "::" */
256- continue ;
257- if ( endp )
258- * endp = '\0' ;
245+ if (! startp )
246+ startp = path ;
247+ else
248+ startp = endp + 1 ;
259249
260- if (is_absolute_path (startp ))
261- snprintf (retpath ,MAXPGPATH ,"%s/%s" ,startp ,argv0 );
250+ endp = strchr (startp ,PATHSEP );
251+ if (!endp )
252+ endp = startp + strlen (startp );/* point to end */
253+
254+ StrNCpy (test_path ,startp ,Min (endp - startp + 1 ,MAXPGPATH ));
255+
256+ if (is_absolute_path (test_path ))
257+ snprintf (retpath ,MAXPGPATH ,"%s/%s" ,test_path ,argv0 );
262258else
263- snprintf (retpath ,MAXPGPATH ,"%s/%s/%s" ,cwd ,startp ,argv0 );
259+ snprintf (retpath ,MAXPGPATH ,"%s/%s/%s" ,cwd ,test_path ,argv0 );
264260
265261canonicalize_path (retpath );
266262switch (validate_exec (retpath ))
267263{
268264case 0 :/* found ok */
269265win32_make_absolute (retpath );
270- pfree (path );
271266return 0 ;
272267case -1 :/* wasn't even a candidate, keep looking */
273- break ;
268+ continue ;
274269case -2 :/* found but disqualified */
275270log_error ("could not read binary \"%s\"" ,retpath );
276- pfree (path );
277- return -1 ;
271+ continue ;
278272}
279- if (!endp )/* last one */
280- break ;
281- }
282- pfree (path );
273+ }while (* endp );
283274}
284275
285276log_error ("could not find a \"%s\" to execute" ,argv0 );