Commit | Line | Data |
---|---|---|
6ec2e0f5 SDJ |
1 | /* Concatenate two arbitrary file names. |
2 | ||
9c9d63b1 | 3 | Copyright (C) 1996-2007, 2009-2021 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 | |
c0c3707f | 16 | along with this program. If not, see <https://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 | ||
9c9d63b1 PM |
28 | #include "basename-lgpl.h" |
29 | #include "filename.h" | |
6ec2e0f5 SDJ |
30 | |
31 | #if ! HAVE_MEMPCPY && ! defined mempcpy | |
32 | # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) | |
33 | #endif | |
34 | ||
c0c3707f | 35 | /* Concatenate two file name components, DIR and BASE, in |
6ec2e0f5 SDJ |
36 | newly-allocated storage and return the result. |
37 | The resulting file name F is such that the commands "ls F" and "(cd | |
c0c3707f CB |
38 | DIR; ls ./BASE)" refer to the same file. If necessary, put |
39 | a separator between DIR and BASE in the result. Typically this | |
40 | separator is "/", but in rare cases it might be ".". | |
6ec2e0f5 | 41 | In any case, if BASE_IN_RESULT is non-NULL, set |
c0c3707f CB |
42 | *BASE_IN_RESULT to point to the copy of BASE at the end of the |
43 | returned concatenation. | |
6ec2e0f5 | 44 | |
698be2d8 | 45 | If malloc fails, return NULL with errno set. */ |
6ec2e0f5 SDJ |
46 | |
47 | char * | |
c0c3707f | 48 | mfile_name_concat (char const *dir, char const *base, char **base_in_result) |
6ec2e0f5 SDJ |
49 | { |
50 | char const *dirbase = last_component (dir); | |
51 | size_t dirbaselen = base_len (dirbase); | |
52 | size_t dirlen = dirbase - dir + dirbaselen; | |
6ec2e0f5 | 53 | size_t baselen = strlen (base); |
c0c3707f CB |
54 | char sep = '\0'; |
55 | if (dirbaselen) | |
56 | { | |
57 | /* DIR is not a file system root, so separate with / if needed. */ | |
58 | if (! ISSLASH (dir[dirlen - 1]) && ! ISSLASH (*base)) | |
59 | sep = '/'; | |
60 | } | |
61 | else if (ISSLASH (*base)) | |
62 | { | |
63 | /* DIR is a file system root and BASE begins with a slash, so | |
64 | separate with ".". For example, if DIR is "/" and BASE is | |
65 | "/foo" then return "/./foo", as "//foo" would be wrong on | |
66 | some POSIX systems. A fancier algorithm could omit "." in | |
67 | some cases but is not worth the trouble. */ | |
68 | sep = '.'; | |
69 | } | |
70 | ||
71 | char *p_concat = malloc (dirlen + (sep != '\0') + baselen + 1); | |
6ec2e0f5 SDJ |
72 | if (p_concat == NULL) |
73 | return NULL; | |
74 | ||
698be2d8 CB |
75 | { |
76 | char *p; | |
77 | ||
78 | p = mempcpy (p_concat, dir, dirlen); | |
79 | *p = sep; | |
80 | p += sep != '\0'; | |
6ec2e0f5 | 81 | |
698be2d8 CB |
82 | if (base_in_result) |
83 | *base_in_result = p; | |
6ec2e0f5 | 84 | |
698be2d8 CB |
85 | p = mempcpy (p, base, baselen); |
86 | *p = '\0'; | |
87 | } | |
6ec2e0f5 SDJ |
88 | |
89 | return p_concat; | |
90 | } |