Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a70ec1

Browse files
committed
Allow relative paths as long as the hardcoded path matches the bin path
up to the last bin directory name.
1 parent bfb77c1 commit 2a70ec1

File tree

1 file changed

+67
-81
lines changed

1 file changed

+67
-81
lines changed

src/port/path.c

Lines changed: 67 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/port/path.c,v 1.14 2004/05/25 18:18:29 momjian Exp $
11+
* $PostgreSQL: pgsql/src/port/path.c,v 1.15 2004/05/25 20:47:41 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -26,7 +26,7 @@
2626
#define ISSEP(ch) ((ch) == '/' || (ch) == '\\')
2727
#endif
2828

29-
static bool relative_path(const char *path1, const char *path2);
29+
const static char *relative_path(const char *bin_path, const char *other_path);
3030
static void trim_directory(char *path);
3131
static void trim_trailing_separator(char *path);
3232

@@ -37,6 +37,14 @@ static void trim_trailing_separator(char *path);
3737
(p)++; \
3838
}
3939

40+
/* Macro creates a relative path */
41+
#define MAKE_RELATIVE \
42+
do { \
43+
StrNCpy(path, my_exec_path, MAXPGPATH); \
44+
trim_directory(path); \
45+
trim_directory(path); \
46+
snprintf(ret_path, MAXPGPATH, "%s/%s", path, p); \
47+
} while (0)
4048

4149
/*
4250
* first_path_separator
@@ -114,14 +122,10 @@ void
114122
get_share_path(const char *my_exec_path, char *ret_path)
115123
{
116124
char path[MAXPGPATH];
125+
const char *p;
117126

118-
if (relative_path(PGBINDIR, PGSHAREDIR))
119-
{
120-
StrNCpy(path, my_exec_path, MAXPGPATH);
121-
trim_directory(path); /* trim off binary */
122-
trim_directory(path); /* trim off /bin */
123-
snprintf(ret_path, MAXPGPATH, "%s/share", path);
124-
}
127+
if ((p = relative_path(PGBINDIR, PGSHAREDIR)))
128+
MAKE_RELATIVE;
125129
else
126130
StrNCpy(ret_path, PGSHAREDIR, MAXPGPATH);
127131
}
@@ -135,14 +139,10 @@ void
135139
get_etc_path(const char *my_exec_path, char *ret_path)
136140
{
137141
char path[MAXPGPATH];
142+
const char *p;
138143

139-
if (relative_path(PGBINDIR, SYSCONFDIR))
140-
{
141-
StrNCpy(path, my_exec_path, MAXPGPATH);
142-
trim_directory(path);
143-
trim_directory(path);
144-
snprintf(ret_path, MAXPGPATH, "%s/etc", path);
145-
}
144+
if ((p = relative_path(PGBINDIR, SYSCONFDIR)))
145+
MAKE_RELATIVE;
146146
else
147147
StrNCpy(ret_path, SYSCONFDIR, MAXPGPATH);
148148
}
@@ -156,14 +156,10 @@ void
156156
get_include_path(const char *my_exec_path, char *ret_path)
157157
{
158158
char path[MAXPGPATH];
159+
const char *p;
159160

160-
if (relative_path(PGBINDIR, INCLUDEDIR))
161-
{
162-
StrNCpy(path, my_exec_path, MAXPGPATH);
163-
trim_directory(path);
164-
trim_directory(path);
165-
snprintf(ret_path, MAXPGPATH, "%s/include", path);
166-
}
161+
if ((p = relative_path(PGBINDIR, INCLUDEDIR)))
162+
MAKE_RELATIVE;
167163
else
168164
StrNCpy(ret_path, INCLUDEDIR, MAXPGPATH);
169165
}
@@ -177,14 +173,10 @@ void
177173
get_pkginclude_path(const char *my_exec_path, char *ret_path)
178174
{
179175
char path[MAXPGPATH];
176+
const char *p;
180177

181-
if (relative_path(PGBINDIR, PKGINCLUDEDIR))
182-
{
183-
StrNCpy(path, my_exec_path, MAXPGPATH);
184-
trim_directory(path);
185-
trim_directory(path);
186-
snprintf(ret_path, MAXPGPATH, "%s/include", path);
187-
}
178+
if ((p = relative_path(PGBINDIR, PKGINCLUDEDIR)))
179+
MAKE_RELATIVE;
188180
else
189181
StrNCpy(ret_path, PKGINCLUDEDIR, MAXPGPATH);
190182
}
@@ -200,14 +192,10 @@ void
200192
get_pkglib_path(const char *my_exec_path, char *ret_path)
201193
{
202194
char path[MAXPGPATH];
195+
const char *p;
203196

204-
if (relative_path(PGBINDIR, PKGLIBDIR))
205-
{
206-
StrNCpy(path, my_exec_path, MAXPGPATH);
207-
trim_directory(path);
208-
trim_directory(path);
209-
snprintf(ret_path, MAXPGPATH, "%s/lib", path);
210-
}
197+
if ((p = relative_path(PGBINDIR, PKGLIBDIR)))
198+
MAKE_RELATIVE;
211199
else
212200
StrNCpy(ret_path, PKGLIBDIR, MAXPGPATH);
213201
}
@@ -223,14 +211,10 @@ void
223211
get_locale_path(const char *my_exec_path, char *ret_path)
224212
{
225213
char path[MAXPGPATH];
214+
const char *p;
226215

227-
if (relative_path(PGBINDIR, LOCALEDIR))
228-
{
229-
StrNCpy(path, my_exec_path, MAXPGPATH);
230-
trim_directory(path);
231-
trim_directory(path);
232-
snprintf(ret_path, MAXPGPATH, "%s/share/locale", path);
233-
}
216+
if ((p = relative_path(PGBINDIR, LOCALEDIR)))
217+
MAKE_RELATIVE;
234218
else
235219
StrNCpy(ret_path, LOCALEDIR, MAXPGPATH);
236220
}
@@ -271,68 +255,71 @@ set_pglocale(const char *argv0, const char *app)
271255
*
272256
* Do the supplied paths differ only in their last component?
273257
*/
274-
static bool
275-
relative_path(const char *path1, const char *path2)
258+
static const char *
259+
relative_path(const char *bin_path, const char *other_path)
276260
{
277-
261+
const char *other_sep = other_path;
262+
278263
#ifdef WIN32
279264
/* Driver letters match? */
280-
if (isalpha(*path1) && path1[1] == ':' &&
281-
(!isalpha(*path2) || !path2[1] == ':'))
282-
return false;
283-
if ((!isalpha(*path1) || !path1[1] == ':') &&
284-
(isalpha(*path2) && path2[1] == ':'))
285-
return false;
286-
if (isalpha(*path1) && path1[1] == ':' &&
287-
isalpha(*path2) && path2[1] == ':')
265+
if (isalpha(*bin_path) && bin_path[1] == ':' &&
266+
(!isalpha(*other_path) || !other_path[1] == ':'))
267+
return NULL;
268+
if ((!isalpha(*bin_path) || !bin_path[1] == ':') &&
269+
(isalpha(*other_path) && other_path[1] == ':'))
270+
return NULL;
271+
if (isalpha(*bin_path) && bin_path[1] == ':' &&
272+
isalpha(*other_path) && other_path[1] == ':')
288273
{
289-
if (toupper(*path1) != toupper(*path2))
290-
return false;
291-
path1 += 2;
292-
path2 += 2;
274+
if (toupper(*bin_path) != toupper(*other_path))
275+
return NULL;
276+
bin_path += 2;
277+
other_path += 2;
278+
other_sep = other_path + 1; /* past separator */
293279
}
294280
#endif
295281

296282
while (1)
297283
{
298284
/* Move past adjacent slashes like //, and trailing ones */
299-
MOVE_TO_SEP_END(path1);
300-
MOVE_TO_SEP_END(path2);
285+
MOVE_TO_SEP_END(bin_path);
286+
MOVE_TO_SEP_END(other_path);
301287

302288
/* One of the paths is done? */
303-
if (!*path1 || !*path2)
289+
if (!*bin_path || !*other_path)
304290
break;
305291

306292
/* Win32 filesystem is case insensitive */
293+
if ((!ISSEP(*bin_path) || !ISSEP(*other_path)) &&
307294
#ifndef WIN32
308-
if (*path1 != *path2)
295+
*bin_path != *other_path)
309296
#else
310-
if (toupper((unsigned char) *path1) != toupper((unsigned char)*path2))
297+
toupper((unsigned char) *bin_path) != toupper((unsigned char)*other_path))
311298
#endif
312-
break;
299+
break;
313300

314-
path1++;
315-
path2++;
301+
if (ISSEP(*other_path))
302+
other_sep = other_path + 1; /* past separator */
303+
304+
bin_path++;
305+
other_path++;
316306
}
317307

318-
/* both done, identical? */
319-
if (!*path1 && !*path2)
320-
return false;
308+
/* identical? */
309+
if (!*bin_path && !*other_path)
310+
return NULL;
321311

322312
/* advance past directory name */
323-
while (!ISSEP(*path1) && *path1)
324-
path1++;
325-
while (!ISSEP(*path2) && *path2)
326-
path2++;
313+
while (!ISSEP(*bin_path) && *bin_path)
314+
bin_path++;
327315

328-
MOVE_TO_SEP_END(path1);
329-
MOVE_TO_SEP_END(path2);
316+
MOVE_TO_SEP_END(bin_path);
330317

331-
/* Are both strings done? */
332-
if (!*path1 && !*path2)
333-
return true;
318+
/* Is bin done? */
319+
if (!*bin_path)
320+
return other_path;
334321
else
335-
return false;
322+
return NULL;
336323
}
337324

338325

@@ -372,4 +359,3 @@ trim_trailing_separator(char *path)
372359
for (p--; p >= path && ISSEP(*p); p--)
373360
*p = '\0';
374361
}
375-

0 commit comments

Comments
 (0)