|
|
/* path-concat.c -- concatenate two arbitrary pathnames | /* path-concat.c -- concatenate two arbitrary pathnames |
| |
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free |
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free |
Software Foundation, Inc. | Software Foundation, Inc. |
| |
This program is free software; you can redistribute it and/or modify | This program is free software; you can redistribute it and/or modify |
|
Lines 54-61
longest_relative_suffix (char const *f)
|
Link Here
|
|---|
|
Arrange for a directory separator if necessary between DIR and BASE | Arrange for a directory separator if necessary between DIR and BASE |
in the result, removing any redundant separators. | in the result, removing any redundant separators. |
In any case, if BASE_IN_RESULT is non-NULL, set | In any case, if BASE_IN_RESULT is non-NULL, set |
*BASE_IN_RESULT to point to the copy of BASE in the returned |
*BASE_IN_RESULT to point to the copy of ABASE in the returned |
concatenation. |
concatenation. However, if ABASE begins with more than one slash, |
|
set *BASE_IN_RESULT to point to the sole corresponding slash that |
|
is copied into the result buffer. |
| |
Report an error if memory is exhausted. */ | Report an error if memory is exhausted. */ |
| |
|
Lines 78-87
path_concat (char const *dir, char const
|
Link Here
|
|---|
|
p += needs_separator; | p += needs_separator; |
| |
if (base_in_result) | if (base_in_result) |
*base_in_result = p; |
*base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase); |
| |
p = mempcpy (p, base, baselen); | p = mempcpy (p, base, baselen); |
*p = '\0'; | *p = '\0'; |
| |
return p_concat; | return p_concat; |
} | } |
|
|
|
#ifdef TEST_PATH_CONCAT |
|
#include <stdlib.h> |
|
#include <stdio.h> |
|
int |
|
main () |
|
{ |
|
static char const *const tests[][3] = |
|
{ |
|
{"a", "b", "a/b"}, |
|
{"a/", "b", "a/b"}, |
|
{"a/", "/b", "a/b"}, |
|
{"a", "/b", "a/b"}, |
|
|
|
{"/", "b", "/b"}, |
|
{"/", "/b", "/b"}, |
|
{"/", "/", "/"}, |
|
{"a", "/", "a/"}, /* this might deserve a diagnostic */ |
|
{"/a", "/", "/a/"}, /* this might deserve a diagnostic */ |
|
{"a", "//b", "a/b"}, |
|
}; |
|
size_t i; |
|
bool fail = false; |
|
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) |
|
{ |
|
char *base_in_result; |
|
char const *const *t = tests[i]; |
|
char *res = path_concat (t[0], t[1], &base_in_result); |
|
if (strcmp (res, t[2]) != 0) |
|
{ |
|
printf ("got %s, expected %s\n", res, t[2]); |
|
fail = true; |
|
} |
|
} |
|
exit (fail ? EXIT_FAILURE : EXIT_SUCCESS); |
|
} |
|
#endif |