1 /* Concatenate two arbitrary file names.
2
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
4 Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <sys/cdefs.h>
20 __RCSID("$NetBSD: filenamecat.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
21
22
23 /* Written by Jim Meyering. */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 /* Specification. */
30 #include "filenamecat.h"
31
32 #include <string.h>
33
34 #include "dirname.h"
35 #include "xalloc.h"
36
37 #if ! HAVE_MEMPCPY && ! defined mempcpy
38 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
39 #endif
40
41 /* Return the longest suffix of F that is a relative file name.
42 If it has no such suffix, return the empty string. */
43
44 static char const *
longest_relative_suffix(char const * f)45 longest_relative_suffix (char const *f)
46 {
47 for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++)
48 continue;
49 return f;
50 }
51
52 /* Concatenate two file name components, DIR and ABASE, in
53 newly-allocated storage and return the result.
54 The resulting file name F is such that the commands "ls F" and "(cd
55 DIR; ls BASE)" refer to the same file, where BASE is ABASE with any
56 file system prefixes and leading separators removed.
57 Arrange for a directory separator if necessary between DIR and BASE
58 in the result, removing any redundant separators.
59 In any case, if BASE_IN_RESULT is non-NULL, set
60 *BASE_IN_RESULT to point to the copy of ABASE in the returned
61 concatenation. However, if ABASE begins with more than one slash,
62 set *BASE_IN_RESULT to point to the sole corresponding slash that
63 is copied into the result buffer.
64
65 Report an error if memory is exhausted. */
66
67 char *
file_name_concat(char const * dir,char const * abase,char ** base_in_result)68 file_name_concat (char const *dir, char const *abase, char **base_in_result)
69 {
70 char const *dirbase = base_name (dir);
71 size_t dirbaselen = base_len (dirbase);
72 size_t dirlen = dirbase - dir + dirbaselen;
73 size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1]));
74
75 char const *base = longest_relative_suffix (abase);
76 size_t baselen = strlen (base);
77
78 char *p_concat = xmalloc (dirlen + needs_separator + baselen + 1);
79 char *p;
80
81 p = mempcpy (p_concat, dir, dirlen);
82 *p = DIRECTORY_SEPARATOR;
83 p += needs_separator;
84
85 if (base_in_result)
86 *base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase);
87
88 p = mempcpy (p, base, baselen);
89 *p = '\0';
90
91 return p_concat;
92 }
93
94 #ifdef TEST_FILE_NAME_CONCAT
95 # include <stdlib.h>
96 # include <stdio.h>
97 int
main()98 main ()
99 {
100 static char const *const tests[][3] =
101 {
102 {"a", "b", "a/b"},
103 {"a/", "b", "a/b"},
104 {"a/", "/b", "a/b"},
105 {"a", "/b", "a/b"},
106
107 {"/", "b", "/b"},
108 {"/", "/b", "/b"},
109 {"/", "/", "/"},
110 {"a", "/", "a/"}, /* this might deserve a diagnostic */
111 {"/a", "/", "/a/"}, /* this might deserve a diagnostic */
112 {"a", "//b", "a/b"},
113 };
114 size_t i;
115 bool fail = false;
116 for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
117 {
118 char *base_in_result;
119 char const *const *t = tests[i];
120 char *res = file_name_concat (t[0], t[1], &base_in_result);
121 if (strcmp (res, t[2]) != 0)
122 {
123 printf ("got %s, expected %s\n", res, t[2]);
124 fail = true;
125 }
126 }
127 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
128 }
129 #endif
130