1 | /* Construct a full pathname from a directory and a filename. |
---|
2 | Copyright (C) 2001 Free Software Foundation, Inc. |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify it |
---|
5 | under the terms of the GNU General Public License as published by the |
---|
6 | Free Software Foundation; either version 2, or (at your option) any |
---|
7 | 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 |
---|
16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
17 | USA. */ |
---|
18 | |
---|
19 | /* Written by Bruno Haible <haible@clisp.cons.org>. */ |
---|
20 | |
---|
21 | #ifdef HAVE_CONFIG_H |
---|
22 | # include <config.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include "system.h" |
---|
26 | |
---|
27 | /* Concatenate a directory pathname, a relative pathname and an optional |
---|
28 | suffix. The directory may end with the directory separator. The second |
---|
29 | argument may not start with the directory separator (it is relative). |
---|
30 | Return a freshly allocated pathname. */ |
---|
31 | char * |
---|
32 | concatenated_pathname (directory, filename, suffix) |
---|
33 | const char *directory; |
---|
34 | const char *filename; |
---|
35 | const char *suffix; |
---|
36 | { |
---|
37 | char *result; |
---|
38 | char *p; |
---|
39 | |
---|
40 | if (strcmp (directory, ".") == 0) |
---|
41 | { |
---|
42 | /* No need to prepend the directory. */ |
---|
43 | result = (char *) xmalloc (strlen (filename) |
---|
44 | + (suffix != NULL ? strlen (suffix) : 0) |
---|
45 | + 1); |
---|
46 | p = result; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | size_t directory_len = strlen (directory); |
---|
51 | int need_slash = |
---|
52 | (directory_len > FILESYSTEM_PREFIX_LEN (directory) |
---|
53 | && !ISSLASH (directory[directory_len - 1])); |
---|
54 | result = (char *) xmalloc (directory_len + need_slash |
---|
55 | + strlen (filename) |
---|
56 | + (suffix != NULL ? strlen (suffix) : 0) |
---|
57 | + 1); |
---|
58 | memcpy (result, directory, directory_len); |
---|
59 | p = result + directory_len; |
---|
60 | if (need_slash) |
---|
61 | *p++ = '/'; |
---|
62 | } |
---|
63 | p = stpcpy (p, filename); |
---|
64 | if (suffix != NULL) |
---|
65 | stpcpy (p, suffix); |
---|
66 | return result; |
---|
67 | } |
---|