Blame lib/filenamecat-lgpl.c

Packit 709fb3
/* Concatenate two arbitrary file names.
Packit 709fb3
Packit 709fb3
   Copyright (C) 1996-2007, 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Jim Meyering.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include "filenamecat.h"
Packit 709fb3
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
#include "dirname.h"
Packit 709fb3
Packit 709fb3
#if ! HAVE_MEMPCPY && ! defined mempcpy
Packit 709fb3
# define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* Return the longest suffix of F that is a relative file name.
Packit 709fb3
   If it has no such suffix, return the empty string.  */
Packit 709fb3
Packit 709fb3
static char const * _GL_ATTRIBUTE_PURE
Packit 709fb3
longest_relative_suffix (char const *f)
Packit 709fb3
{
Packit 709fb3
  for (f += FILE_SYSTEM_PREFIX_LEN (f); ISSLASH (*f); f++)
Packit 709fb3
    continue;
Packit 709fb3
  return f;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Concatenate two file name components, DIR and ABASE, in
Packit 709fb3
   newly-allocated storage and return the result.
Packit 709fb3
   The resulting file name F is such that the commands "ls F" and "(cd
Packit 709fb3
   DIR; ls BASE)" refer to the same file, where BASE is ABASE with any
Packit 709fb3
   file system prefixes and leading separators removed.
Packit 709fb3
   Arrange for a directory separator if necessary between DIR and BASE
Packit 709fb3
   in the result, removing any redundant separators.
Packit 709fb3
   In any case, if BASE_IN_RESULT is non-NULL, set
Packit 709fb3
   *BASE_IN_RESULT to point to the copy of ABASE in the returned
Packit 709fb3
   concatenation.  However, if ABASE begins with more than one slash,
Packit 709fb3
   set *BASE_IN_RESULT to point to the sole corresponding slash that
Packit 709fb3
   is copied into the result buffer.
Packit 709fb3
Packit 709fb3
   Return NULL if malloc fails.  */
Packit 709fb3
Packit 709fb3
char *
Packit 709fb3
mfile_name_concat (char const *dir, char const *abase, char **base_in_result)
Packit 709fb3
{
Packit 709fb3
  char const *dirbase = last_component (dir);
Packit 709fb3
  size_t dirbaselen = base_len (dirbase);
Packit 709fb3
  size_t dirlen = dirbase - dir + dirbaselen;
Packit 709fb3
  size_t needs_separator = (dirbaselen && ! ISSLASH (dirbase[dirbaselen - 1]));
Packit 709fb3
Packit 709fb3
  char const *base = longest_relative_suffix (abase);
Packit 709fb3
  size_t baselen = strlen (base);
Packit 709fb3
Packit 709fb3
  char *p_concat = malloc (dirlen + needs_separator + baselen + 1);
Packit 709fb3
  char *p;
Packit 709fb3
Packit 709fb3
  if (p_concat == NULL)
Packit 709fb3
    return NULL;
Packit 709fb3
Packit 709fb3
  p = mempcpy (p_concat, dir, dirlen);
Packit 709fb3
  *p = DIRECTORY_SEPARATOR;
Packit 709fb3
  p += needs_separator;
Packit 709fb3
Packit 709fb3
  if (base_in_result)
Packit 709fb3
    *base_in_result = p - IS_ABSOLUTE_FILE_NAME (abase);
Packit 709fb3
Packit 709fb3
  p = mempcpy (p, base, baselen);
Packit 709fb3
  *p = '\0';
Packit 709fb3
Packit 709fb3
  return p_concat;
Packit 709fb3
}