Commit | Line | Data |
---|---|---|
6ec2e0f5 SDJ |
1 | /* Concatenate two arbitrary file names. |
2 | ||
5e8754f9 | 3 | Copyright (C) 1996-2007, 2009-2016 Free Software Foundation, Inc. |
6ec2e0f5 SDJ |
4 | |
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
5e8754f9 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6ec2e0f5 SDJ |
17 | |
18 | /* Written by Jim Meyering. */ | |
19 | ||
20 | #include <config.h> | |
21 | ||
22 | /* Specification. */ | |
23 | #include "filenamecat.h" | |
24 | ||
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
28 | #include "dirname.h" | |
29 | ||
30 | #if ! HAVE_MEMPCPY && ! defined mempcpy | |
31 | # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) | |
32 | #endif | |
33 | ||
5e8754f9 SDJ |
34 | /* Return the longest suffix of F that is a relative file name. |
35 | If it has no such suffix, return the empty string. */ | |
36 | ||
37 | static char const * _GL_ATTRIBUTE_PURE | |
38 | longest_relative_suffix (char const *f) | |
39 | { | |
40 | for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++) | |
41 | continue; | |
42 | return f; | |
43 | } | |
44 | ||
45 | /* Concatenate two file name components, DIR and ABASE, in | |
6ec2e0f5 SDJ |
46 | newly-allocated storage and return the result. |
47 | The resulting file name F is such that the commands "ls F" and "(cd | |
5e8754f9 SDJ |
48 | DIR; ls BASE)" refer to the same file, where BASE is ABASE with any |
49 | file system prefixes and leading separators removed. | |
50 | Arrange for a directory separator if necessary between DIR and BASE | |
51 | in the result, removing any redundant separators. | |
6ec2e0f5 | 52 | In any case, if BASE_IN_RESULT is non-NULL, set |
5e8754f9 SDJ |
53 | *BASE_IN_RESULT to point to the copy of ABASE in the returned |
54 | concatenation. However, if ABASE begins with more than one slash, | |
55 | set *BASE_IN_RESULT to point to the sole corresponding slash that | |
56 | is copied into the result buffer. | |
6ec2e0f5 SDJ |
57 | |
58 | Return NULL if malloc fails. */ | |
59 | ||
60 | char * | |
5e8754f9 | 61 | mfile_name_concat (char const *dir, char const *abase, char **base_in_result) |
6ec2e0f5 SDJ |
62 | { |
63 | char const *dirbase = last_component (dir); | |
64 | size_t dirbaselen = base_len (dirbase); | |
65 | size_t dirlen = dirbase - dir + dirbaselen; | |
5e8754f9 SDJ |
66 | size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1])); |
67 | ||
68 | char const *base = longest_relative_suffix (abase); | |
6ec2e0f5 | 69 | size_t baselen = strlen (base); |
5e8754f9 SDJ |
70 | |
71 | char *p_concat = malloc (dirlen + needs_separator + baselen + 1); | |
6ec2e0f5 SDJ |
72 | char *p; |
73 | ||
74 | if (p_concat == NULL) | |
75 | return NULL; | |
76 | ||
77 | p = mempcpy (p_concat, dir, dirlen); | |
5e8754f9 SDJ |
78 | *p = DIRECTORY_SEPARATOR; |
79 | p += needs_separator; | |
6ec2e0f5 SDJ |
80 | |
81 | if (base_in_result) | |
5e8754f9 | 82 | *base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase); |
6ec2e0f5 SDJ |
83 | |
84 | p = mempcpy (p, base, baselen); | |
85 | *p = '\0'; | |
86 | ||
87 | return p_concat; | |
88 | } |