filenamecat-lgpl.c 2.8 KB

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