| 1 |
/* make-suffix.c: unconditionally add a filename suffix. |
| 2 |
|
| 3 |
Copyright 1992, 1993, 1995, 2008, 2011, 2016 Karl Berry. |
| 4 |
Copyright 2001, 2005 Olaf Weber. |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
#include <kpathsea/c-pathch.h> |
| 21 |
|
| 22 |
/* Return a new string: S suffixed with SUFFIX, regardless of what it |
| 23 |
was before. This returns a newly allocated string. */ |
| 24 |
|
| 25 |
string |
| 26 |
make_suffix (const_string s, const_string suffix) |
| 27 |
{ |
| 28 |
string new_s; |
| 29 |
const_string dot_pos = strrchr (s, '.'); |
| 30 |
const_string p; |
| 31 |
#if defined(WIN32) |
| 32 |
string q; |
| 33 |
#endif |
| 34 |
|
| 35 |
if (dot_pos) |
| 36 |
for (p = dot_pos + 1; *p; p++) { |
| 37 |
if (IS_DIR_SEP (*p)) { |
| 38 |
dot_pos = NULL; |
| 39 |
break; |
| 40 |
} |
| 41 |
#if defined(WIN32) && defined(KPSE_COMPAT_API) |
| 42 |
else if (IS_KANJI(p)) |
| 43 |
p++; |
| 44 |
#endif |
| 45 |
} |
| 46 |
|
| 47 |
if (dot_pos == NULL) |
| 48 |
new_s = concat3 (s, ".", suffix); |
| 49 |
else |
| 50 |
{ |
| 51 |
unsigned past_dot_index = dot_pos + 1 - s; |
| 52 |
|
| 53 |
new_s = (string)xmalloc (past_dot_index + strlen (suffix) + 1); |
| 54 |
strncpy (new_s, s, past_dot_index); |
| 55 |
strcpy (new_s + past_dot_index, suffix); |
| 56 |
} |
| 57 |
|
| 58 |
#if defined(WIN32) |
| 59 |
for (q = new_s; *q; q++) { |
| 60 |
if (*q == '\\') |
| 61 |
*q = '/'; |
| 62 |
#if defined(KPSE_COMPAT_API) |
| 63 |
else if (IS_KANJI(q)) |
| 64 |
q++; |
| 65 |
#endif |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
return new_s; |
| 70 |
} |