use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE
[gnulib.git] / lib / filenamecat-lgpl.c
1 /* Concatenate two arbitrary file names.
2
3    Copyright (C) 1996-2007, 2009-2011 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
30 #if ! HAVE_MEMPCPY && ! defined mempcpy
31 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
32 #endif
33
34 /* The attribute __pure__ was added in gcc 2.96.  */
35 #undef _GL_ATTRIBUTE_PURE
36 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
37 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
38 #else
39 # define _GL_ATTRIBUTE_PURE /* empty */
40 #endif
41
42 /* Return the longest suffix of F that is a relative file name.
43    If it has no such suffix, return the empty string.  */
44
45 static char const * _GL_ATTRIBUTE_PURE
46 longest_relative_suffix (char const *f)
47 {
48   for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++)
49     continue;
50   return f;
51 }
52
53 /* Concatenate two file name components, DIR and ABASE, in
54    newly-allocated storage and return the result.
55    The resulting file name F is such that the commands "ls F" and "(cd
56    DIR; ls BASE)" refer to the same file, where BASE is ABASE with any
57    file system prefixes and leading separators removed.
58    Arrange for a directory separator if necessary between DIR and BASE
59    in the result, removing any redundant separators.
60    In any case, if BASE_IN_RESULT is non-NULL, set
61    *BASE_IN_RESULT to point to the copy of ABASE in the returned
62    concatenation.  However, if ABASE begins with more than one slash,
63    set *BASE_IN_RESULT to point to the sole corresponding slash that
64    is copied into the result buffer.
65
66    Return NULL if malloc fails.  */
67
68 char *
69 mfile_name_concat (char const *dir, char const *abase, char **base_in_result)
70 {
71   char const *dirbase = last_component (dir);
72   size_t dirbaselen = base_len (dirbase);
73   size_t dirlen = dirbase - dir + dirbaselen;
74   size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1]));
75
76   char const *base = longest_relative_suffix (abase);
77   size_t baselen = strlen (base);
78
79   char *p_concat = malloc (dirlen + needs_separator + baselen + 1);
80   char *p;
81
82   if (p_concat == NULL)
83     return NULL;
84
85   p = mempcpy (p_concat, dir, dirlen);
86   *p = DIRECTORY_SEPARATOR;
87   p += needs_separator;
88
89   if (base_in_result)
90     *base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase);
91
92   p = mempcpy (p, base, baselen);
93   *p = '\0';
94
95   return p_concat;
96 }