Lines 1-6
Link Here
|
1 |
/* path-concat.c -- concatenate two arbitrary pathnames |
1 |
/* path-concat.c -- concatenate two arbitrary pathnames |
2 |
|
2 |
|
3 |
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free |
3 |
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free |
4 |
Software Foundation, Inc. |
4 |
Software Foundation, Inc. |
5 |
|
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
6 |
This program is free software; you can redistribute it and/or modify |
Lines 54-61
longest_relative_suffix (char const *f)
Link Here
|
54 |
Arrange for a directory separator if necessary between DIR and BASE |
54 |
Arrange for a directory separator if necessary between DIR and BASE |
55 |
in the result, removing any redundant separators. |
55 |
in the result, removing any redundant separators. |
56 |
In any case, if BASE_IN_RESULT is non-NULL, set |
56 |
In any case, if BASE_IN_RESULT is non-NULL, set |
57 |
*BASE_IN_RESULT to point to the copy of BASE in the returned |
57 |
*BASE_IN_RESULT to point to the copy of ABASE in the returned |
58 |
concatenation. |
58 |
concatenation. However, if ABASE begins with more than one slash, |
|
|
59 |
set *BASE_IN_RESULT to point to the sole corresponding slash that |
60 |
is copied into the result buffer. |
59 |
|
61 |
|
60 |
Report an error if memory is exhausted. */ |
62 |
Report an error if memory is exhausted. */ |
61 |
|
63 |
|
Lines 78-87
path_concat (char const *dir, char const
Link Here
|
78 |
p += needs_separator; |
80 |
p += needs_separator; |
79 |
|
81 |
|
80 |
if (base_in_result) |
82 |
if (base_in_result) |
81 |
*base_in_result = p; |
83 |
*base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase); |
82 |
|
84 |
|
83 |
p = mempcpy (p, base, baselen); |
85 |
p = mempcpy (p, base, baselen); |
84 |
*p = '\0'; |
86 |
*p = '\0'; |
85 |
|
87 |
|
86 |
return p_concat; |
88 |
return p_concat; |
87 |
} |
89 |
} |
|
|
90 |
|
91 |
#ifdef TEST_PATH_CONCAT |
92 |
#include <stdlib.h> |
93 |
#include <stdio.h> |
94 |
int |
95 |
main () |
96 |
{ |
97 |
static char const *const tests[][3] = |
98 |
{ |
99 |
{"a", "b", "a/b"}, |
100 |
{"a/", "b", "a/b"}, |
101 |
{"a/", "/b", "a/b"}, |
102 |
{"a", "/b", "a/b"}, |
103 |
|
104 |
{"/", "b", "/b"}, |
105 |
{"/", "/b", "/b"}, |
106 |
{"/", "/", "/"}, |
107 |
{"a", "/", "a/"}, /* this might deserve a diagnostic */ |
108 |
{"/a", "/", "/a/"}, /* this might deserve a diagnostic */ |
109 |
{"a", "//b", "a/b"}, |
110 |
}; |
111 |
size_t i; |
112 |
bool fail = false; |
113 |
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) |
114 |
{ |
115 |
char *base_in_result; |
116 |
char const *const *t = tests[i]; |
117 |
char *res = path_concat (t[0], t[1], &base_in_result); |
118 |
if (strcmp (res, t[2]) != 0) |
119 |
{ |
120 |
printf ("got %s, expected %s\n", res, t[2]); |
121 |
fail = true; |
122 |
} |
123 |
} |
124 |
exit (fail ? EXIT_FAILURE : EXIT_SUCCESS); |
125 |
} |
126 |
#endif |