filenamecat-lgpl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Concatenate two arbitrary file names.
  2. Copyright (C) 1996-2007, 2009-2021 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 <https://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 "basename-lgpl.h"
  20. #include "filename.h"
  21. #if ! HAVE_MEMPCPY && ! defined mempcpy
  22. # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
  23. #endif
  24. /* Concatenate two file name components, DIR and BASE, in
  25. newly-allocated storage and return the result.
  26. The resulting file name F is such that the commands "ls F" and "(cd
  27. DIR; ls ./BASE)" refer to the same file. If necessary, put
  28. a separator between DIR and BASE in the result. Typically this
  29. separator is "/", but in rare cases it might be ".".
  30. In any case, if BASE_IN_RESULT is non-NULL, set
  31. *BASE_IN_RESULT to point to the copy of BASE at the end of the
  32. returned concatenation.
  33. If malloc fails, return NULL with errno set. */
  34. char *
  35. mfile_name_concat (char const *dir, char const *base, char **base_in_result)
  36. {
  37. char const *dirbase = last_component (dir);
  38. size_t dirbaselen = base_len (dirbase);
  39. size_t dirlen = dirbase - dir + dirbaselen;
  40. size_t baselen = strlen (base);
  41. char sep = '\0';
  42. if (dirbaselen)
  43. {
  44. /* DIR is not a file system root, so separate with / if needed. */
  45. if (! ISSLASH (dir[dirlen - 1]) && ! ISSLASH (*base))
  46. sep = '/';
  47. }
  48. else if (ISSLASH (*base))
  49. {
  50. /* DIR is a file system root and BASE begins with a slash, so
  51. separate with ".". For example, if DIR is "/" and BASE is
  52. "/foo" then return "/./foo", as "//foo" would be wrong on
  53. some POSIX systems. A fancier algorithm could omit "." in
  54. some cases but is not worth the trouble. */
  55. sep = '.';
  56. }
  57. char *p_concat = malloc (dirlen + (sep != '\0') + baselen + 1);
  58. if (p_concat == NULL)
  59. return NULL;
  60. {
  61. char *p;
  62. p = mempcpy (p_concat, dir, dirlen);
  63. *p = sep;
  64. p += sep != '\0';
  65. if (base_in_result)
  66. *base_in_result = p;
  67. p = mempcpy (p, base, baselen);
  68. *p = '\0';
  69. }
  70. return p_concat;
  71. }