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