Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / filenamecat.c
1 /* Concatenate two arbitrary file names.
2
3    Copyright (C) 1996-2007 Free Software Foundation, Inc.
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
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 #include "xalloc.h"
30
31 #if ! HAVE_MEMPCPY && ! defined mempcpy
32 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
33 #endif
34
35 /* Return the longest suffix of F that is a relative file name.
36    If it has no such suffix, return the empty string.  */
37
38 static char const *
39 longest_relative_suffix (char const *f)
40 {
41   for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++)
42     continue;
43   return f;
44 }
45
46 /* Concatenate two file name components, DIR and ABASE, in
47    newly-allocated storage and return the result.
48    The resulting file name F is such that the commands "ls F" and "(cd
49    DIR; ls BASE)" refer to the same file, where BASE is ABASE with any
50    file system prefixes and leading separators removed.
51    Arrange for a directory separator if necessary between DIR and BASE
52    in the result, removing any redundant separators.
53    In any case, if BASE_IN_RESULT is non-NULL, set
54    *BASE_IN_RESULT to point to the copy of ABASE in the returned
55    concatenation.  However, if ABASE begins with more than one slash,
56    set *BASE_IN_RESULT to point to the sole corresponding slash that
57    is copied into the result buffer.
58
59    Return NULL if malloc fails.  */
60
61 char *
62 mfile_name_concat (char const *dir, char const *abase, char **base_in_result)
63 {
64   char const *dirbase = last_component (dir);
65   size_t dirbaselen = base_len (dirbase);
66   size_t dirlen = dirbase - dir + dirbaselen;
67   size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1]));
68
69   char const *base = longest_relative_suffix (abase);
70   size_t baselen = strlen (base);
71
72   char *p_concat = malloc (dirlen + needs_separator + baselen + 1);
73   char *p;
74
75   if (p_concat == NULL)
76     return NULL;
77
78   p = mempcpy (p_concat, dir, dirlen);
79   *p = DIRECTORY_SEPARATOR;
80   p += needs_separator;
81
82   if (base_in_result)
83     *base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase);
84
85   p = mempcpy (p, base, baselen);
86   *p = '\0';
87
88   return p_concat;
89 }
90
91 /* Just like mfile_name_concat, above, except, rather than
92    returning NULL upon malloc failure, here, we report the
93    "memory exhausted" condition and exit.  */
94
95 char *
96 file_name_concat (char const *dir, char const *abase, char **base_in_result)
97 {
98   char *p = mfile_name_concat (dir, abase, base_in_result);
99   if (p == NULL)
100     xalloc_die ();
101   return p;
102 }
103
104 #ifdef TEST_FILE_NAME_CONCAT
105 # include <stdlib.h>
106 # include <stdio.h>
107 int
108 main ()
109 {
110   static char const *const tests[][3] =
111     {
112       {"a", "b",   "a/b"},
113       {"a/", "b",  "a/b"},
114       {"a/", "/b", "a/b"},
115       {"a", "/b",  "a/b"},
116
117       {"/", "b",  "/b"},
118       {"/", "/b", "/b"},
119       {"/", "/",  "/"},
120       {"a", "/",  "a/"},   /* this might deserve a diagnostic */
121       {"/a", "/", "/a/"},  /* this might deserve a diagnostic */
122       {"a", "//b",  "a/b"},
123       {"", "a", "a"},  /* this might deserve a diagnostic */
124     };
125   size_t i;
126   bool fail = false;
127   for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
128     {
129       char *base_in_result;
130       char const *const *t = tests[i];
131       char *res = file_name_concat (t[0], t[1], &base_in_result);
132       if (strcmp (res, t[2]) != 0)
133         {
134           printf ("got %s, expected %s\n", res, t[2]);
135           fail = true;
136         }
137     }
138   exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
139 }
140 #endif