7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $PostgreSQL: pgsql/src/port/exec.c,v 1.14 2004/05/24 20:23:50 momjian Exp $
10
+ * $PostgreSQL: pgsql/src/port/exec.c,v 1.15 2004/05/24 22:35:37 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
28
28
29
29
#define _ (x ) gettext(x)
30
30
31
- #ifdef FRONTEND
32
- #undef pstrdup
33
- #define pstrdup (p ) strdup(p)
34
- #define pfree (p ) free(p)
35
- #endif
36
-
37
31
/* $PATH (or %PATH%) path separator */
38
32
#ifdef WIN32
39
33
#define PATHSEP ';'
@@ -185,11 +179,8 @@ validate_exec(const char *path)
185
179
int
186
180
find_my_exec (const char * argv0 , char * retpath )
187
181
{
188
- char cwd [MAXPGPATH ];
189
- char * p ;
190
- char * path ,
191
- * startp ,
192
- * endp ;
182
+ char cwd [MAXPGPATH ], test_path [MAXPGPATH ];
183
+ char * path ;
193
184
194
185
if (!getcwd (cwd , MAXPGPATH ))
195
186
cwd [0 ] = '\0' ;
@@ -205,9 +196,9 @@ find_my_exec(const char *argv0, char *retpath)
205
196
* it).
206
197
*/
207
198
/* Does argv0 have a separator? */
208
- if ((p = last_path_separator (argv0 )))
199
+ if ((path = last_path_separator (argv0 )))
209
200
{
210
- if (* ++ p == '\0' )
201
+ if (* ++ path == '\0' )
211
202
{
212
203
log_error ("argv[0] ends with a path separator \"%s\"" , argv0 );
213
204
return -1 ;
@@ -245,41 +236,41 @@ find_my_exec(const char *argv0, char *retpath)
245
236
* Second try: since no explicit path was supplied, the user must have
246
237
* been relying on PATH. We'll use the same PATH.
247
238
*/
248
- if ((p = getenv ("PATH" )) && * p )
239
+ if ((path = getenv ("PATH" )) && * path )
249
240
{
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
254
244
{
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 ;
259
249
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 );
262
258
else
263
- snprintf (retpath , MAXPGPATH , "%s/%s/%s" , cwd , startp , argv0 );
259
+ snprintf (retpath , MAXPGPATH , "%s/%s/%s" , cwd , test_path , argv0 );
264
260
265
261
canonicalize_path (retpath );
266
262
switch (validate_exec (retpath ))
267
263
{
268
264
case 0 : /* found ok */
269
265
win32_make_absolute (retpath );
270
- pfree (path );
271
266
return 0 ;
272
267
case -1 : /* wasn't even a candidate, keep looking */
273
- break ;
268
+ continue ;
274
269
case -2 : /* found but disqualified */
275
270
log_error ("could not read binary \"%s\"" , retpath );
276
- pfree (path );
277
- return -1 ;
271
+ continue ;
278
272
}
279
- if (!endp ) /* last one */
280
- break ;
281
- }
282
- pfree (path );
273
+ } while (* endp );
283
274
}
284
275
285
276
log_error ("could not find a \"%s\" to execute" , argv0 );
0 commit comments