Declare strdup only if it's not defined.
[gnulib.git] / lib / path-concat.c
1 /* path-concat.c -- concatenate two arbitrary pathnames
2    Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Jim Meyering.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifndef HAVE_MEMPCPY
25 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
26 #endif
27
28 #include <stdio.h>
29 #if HAVE_STRING_H
30 # include <string.h>
31 #endif
32 #include <sys/types.h>
33
34 char *malloc ();
35 #ifndef strdup
36 char *strdup ();
37 #endif
38
39 #ifndef DIRECTORY_SEPARATOR
40 # define DIRECTORY_SEPARATOR '/'
41 #endif
42
43 #ifndef ISSLASH
44 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
45 #endif
46
47 #include "xalloc.h"
48 #include "path-concat.h"
49
50 /* Concatenate two pathname components, DIR and BASE, in
51    newly-allocated storage and return the result.  Return 0 if out of
52    memory.  Add a slash between DIR and BASE in the result if neither
53    would contribute one.  If each would contribute at least one, elide
54    one from the end of DIR.  Otherwise, simply concatenate DIR and
55    BASE.  In any case, if BASE_IN_RESULT is non-NULL, set
56    *BASE_IN_RESULT to point to the copy of BASE in the returned
57    concatenation.
58
59    DIR may be NULL, BASE must not be.
60
61    Return NULL if memory is exhausted.  */
62
63 char *
64 path_concat (const char *dir, const char *base, char **base_in_result)
65 {
66   char *p;
67   char *p_concat;
68   size_t base_len;
69   size_t dir_len;
70
71   if (!dir)
72     {
73       p_concat = strdup (base);
74       if (base_in_result)
75         *base_in_result = p_concat;
76       return p_concat;
77     }
78
79   /* DIR is not empty. */
80   base_len = strlen (base);
81   dir_len = strlen (dir);
82
83   p_concat = malloc (dir_len + base_len + 2);
84   if (!p_concat)
85     return 0;
86
87   p = mempcpy (p_concat, dir, dir_len);
88
89   if (ISSLASH (*(p - 1)) && ISSLASH(*base))
90     --p;
91   else if (!ISSLASH (*(p - 1)) && !ISSLASH(*base))
92     *p++ = DIRECTORY_SEPARATOR;
93
94   if (base_in_result)
95     *base_in_result = p;
96
97   memcpy (p, base, base_len + 1);
98
99   return p_concat;
100 }
101
102 /* Same, but die when memory is exhausted. */
103
104 char *
105 xpath_concat (const char *dir, const char *base, char **base_in_result)
106 {
107   char *res = path_concat (dir, base, base_in_result);
108   if (res)
109     return res;
110   xalloc_die ();
111 }