8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
26
26
#define ISSEP (ch ) ((ch) == '/' || (ch) == '\\')
27
27
#endif
28
28
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 );
30
30
static void trim_directory (char * path );
31
31
static void trim_trailing_separator (char * path );
32
32
@@ -37,6 +37,14 @@ static void trim_trailing_separator(char *path);
37
37
(p)++; \
38
38
}
39
39
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)
40
48
41
49
/*
42
50
* first_path_separator
@@ -114,14 +122,10 @@ void
114
122
get_share_path (const char * my_exec_path , char * ret_path )
115
123
{
116
124
char path [MAXPGPATH ];
125
+ const char * p ;
117
126
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 ;
125
129
else
126
130
StrNCpy (ret_path , PGSHAREDIR , MAXPGPATH );
127
131
}
@@ -135,14 +139,10 @@ void
135
139
get_etc_path (const char * my_exec_path , char * ret_path )
136
140
{
137
141
char path [MAXPGPATH ];
142
+ const char * p ;
138
143
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 ;
146
146
else
147
147
StrNCpy (ret_path , SYSCONFDIR , MAXPGPATH );
148
148
}
@@ -156,14 +156,10 @@ void
156
156
get_include_path (const char * my_exec_path , char * ret_path )
157
157
{
158
158
char path [MAXPGPATH ];
159
+ const char * p ;
159
160
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 ;
167
163
else
168
164
StrNCpy (ret_path , INCLUDEDIR , MAXPGPATH );
169
165
}
@@ -177,14 +173,10 @@ void
177
173
get_pkginclude_path (const char * my_exec_path , char * ret_path )
178
174
{
179
175
char path [MAXPGPATH ];
176
+ const char * p ;
180
177
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 ;
188
180
else
189
181
StrNCpy (ret_path , PKGINCLUDEDIR , MAXPGPATH );
190
182
}
@@ -200,14 +192,10 @@ void
200
192
get_pkglib_path (const char * my_exec_path , char * ret_path )
201
193
{
202
194
char path [MAXPGPATH ];
195
+ const char * p ;
203
196
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 ;
211
199
else
212
200
StrNCpy (ret_path , PKGLIBDIR , MAXPGPATH );
213
201
}
@@ -223,14 +211,10 @@ void
223
211
get_locale_path (const char * my_exec_path , char * ret_path )
224
212
{
225
213
char path [MAXPGPATH ];
214
+ const char * p ;
226
215
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 ;
234
218
else
235
219
StrNCpy (ret_path , LOCALEDIR , MAXPGPATH );
236
220
}
@@ -271,68 +255,71 @@ set_pglocale(const char *argv0, const char *app)
271
255
*
272
256
* Do the supplied paths differ only in their last component?
273
257
*/
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 )
276
260
{
277
-
261
+ const char * other_sep = other_path ;
262
+
278
263
#ifdef WIN32
279
264
/* 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 ] == ':' )
288
273
{
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 */
293
279
}
294
280
#endif
295
281
296
282
while (1 )
297
283
{
298
284
/* 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 );
301
287
302
288
/* One of the paths is done? */
303
- if (!* path1 || !* path2 )
289
+ if (!* bin_path || !* other_path )
304
290
break ;
305
291
306
292
/* Win32 filesystem is case insensitive */
293
+ if ((!ISSEP (* bin_path ) || !ISSEP (* other_path )) &&
307
294
#ifndef WIN32
308
- if ( * path1 != * path2 )
295
+ * bin_path != * other_path )
309
296
#else
310
- if ( toupper ((unsigned char ) * path1 ) != toupper ((unsigned char )* path2 ))
297
+ toupper ((unsigned char ) * bin_path ) != toupper ((unsigned char )* other_path ))
311
298
#endif
312
- break ;
299
+ break ;
313
300
314
- path1 ++ ;
315
- path2 ++ ;
301
+ if (ISSEP (* other_path ))
302
+ other_sep = other_path + 1 ; /* past separator */
303
+
304
+ bin_path ++ ;
305
+ other_path ++ ;
316
306
}
317
307
318
- /* both done, identical? */
319
- if (!* path1 && !* path2 )
320
- return false ;
308
+ /* identical? */
309
+ if (!* bin_path && !* other_path )
310
+ return NULL ;
321
311
322
312
/* 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 ++ ;
327
315
328
- MOVE_TO_SEP_END (path1 );
329
- MOVE_TO_SEP_END (path2 );
316
+ MOVE_TO_SEP_END (bin_path );
330
317
331
- /* Are both strings done? */
332
- if (!* path1 && ! * path2 )
333
- return true ;
318
+ /* Is bin done? */
319
+ if (!* bin_path )
320
+ return other_path ;
334
321
else
335
- return false ;
322
+ return NULL ;
336
323
}
337
324
338
325
@@ -372,4 +359,3 @@ trim_trailing_separator(char *path)
372
359
for (p -- ; p >= path && ISSEP (* p ); p -- )
373
360
* p = '\0' ;
374
361
}
375
-
0 commit comments