xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/gnulib-lib/concatpath.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Construct a full pathname from a directory and a filename.
2*946379e7Schristos    Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
3*946379e7Schristos 
4*946379e7Schristos    This program is free software; you can redistribute it and/or modify it
5*946379e7Schristos    under the terms of the GNU General Public License as published by the
6*946379e7Schristos    Free Software Foundation; either version 2, or (at your option) any
7*946379e7Schristos    later version.
8*946379e7Schristos 
9*946379e7Schristos    This program is distributed in the hope that it will be useful,
10*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*946379e7Schristos    GNU General Public License for more details.
13*946379e7Schristos 
14*946379e7Schristos    You should have received a copy of the GNU General Public License
15*946379e7Schristos    along with this program; if not, write to the Free Software
16*946379e7Schristos    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17*946379e7Schristos    USA.  */
18*946379e7Schristos 
19*946379e7Schristos /* Written by Bruno Haible <haible@clisp.cons.org>.  */
20*946379e7Schristos 
21*946379e7Schristos #include <config.h>
22*946379e7Schristos 
23*946379e7Schristos /* Specification.  */
24*946379e7Schristos #include "pathname.h"
25*946379e7Schristos 
26*946379e7Schristos #include <string.h>
27*946379e7Schristos 
28*946379e7Schristos #include "xalloc.h"
29*946379e7Schristos #include "stpcpy.h"
30*946379e7Schristos 
31*946379e7Schristos /* Concatenate a directory pathname, a relative pathname and an optional
32*946379e7Schristos    suffix.  The directory may end with the directory separator.  The second
33*946379e7Schristos    argument may not start with the directory separator (it is relative).
34*946379e7Schristos    Return a freshly allocated pathname.  */
35*946379e7Schristos char *
concatenated_pathname(const char * directory,const char * filename,const char * suffix)36*946379e7Schristos concatenated_pathname (const char *directory, const char *filename,
37*946379e7Schristos 		       const char *suffix)
38*946379e7Schristos {
39*946379e7Schristos   char *result;
40*946379e7Schristos   char *p;
41*946379e7Schristos 
42*946379e7Schristos   if (strcmp (directory, ".") == 0)
43*946379e7Schristos     {
44*946379e7Schristos       /* No need to prepend the directory.  */
45*946379e7Schristos       result = (char *) xmalloc (strlen (filename)
46*946379e7Schristos 				 + (suffix != NULL ? strlen (suffix) : 0)
47*946379e7Schristos 				 + 1);
48*946379e7Schristos       p = result;
49*946379e7Schristos     }
50*946379e7Schristos   else
51*946379e7Schristos     {
52*946379e7Schristos       size_t directory_len = strlen (directory);
53*946379e7Schristos       int need_slash =
54*946379e7Schristos 	(directory_len > FILE_SYSTEM_PREFIX_LEN (directory)
55*946379e7Schristos 	 && !ISSLASH (directory[directory_len - 1]));
56*946379e7Schristos       result = (char *) xmalloc (directory_len + need_slash
57*946379e7Schristos 				 + strlen (filename)
58*946379e7Schristos 				 + (suffix != NULL ? strlen (suffix) : 0)
59*946379e7Schristos 				 + 1);
60*946379e7Schristos       memcpy (result, directory, directory_len);
61*946379e7Schristos       p = result + directory_len;
62*946379e7Schristos       if (need_slash)
63*946379e7Schristos 	*p++ = '/';
64*946379e7Schristos     }
65*946379e7Schristos   p = stpcpy (p, filename);
66*946379e7Schristos   if (suffix != NULL)
67*946379e7Schristos     stpcpy (p, suffix);
68*946379e7Schristos   return result;
69*946379e7Schristos }
70