| 1 |
/* basechsuffix.c: replace the last bit of a filename with something else. |
| 2 |
|
| 3 |
Written in 1995 by Karl Berry. Public domain. */ |
| 4 |
|
| 5 |
#include <w2c/config.h> |
| 6 |
#include "lib.h" |
| 7 |
|
| 8 |
/* Return the basename of NAME, with trailing characters OLD replaced by |
| 9 |
NEW. (If last characters in NAME are not OLD, just append NEW.) |
| 10 |
Since this is used to turn, e.g., foo/cmr10.300pk -> cmr10.300gf, |
| 11 |
don't assume a `.' preceding OLD or NEW. |
| 12 |
|
| 13 |
In other words, we're implementing `basename NAME OLD`NEW. */ |
| 14 |
|
| 15 |
string |
| 16 |
basenamechangesuffix (const_string name, const_string old_suffix, |
| 17 |
const_string new_suffix) |
| 18 |
{ |
| 19 |
string answer; |
| 20 |
unsigned c; |
| 21 |
const_string base = xbasename (name); |
| 22 |
unsigned base_len = strlen (base); |
| 23 |
unsigned copy_limit = base_len; |
| 24 |
unsigned old_suffix_len = strlen (old_suffix); |
| 25 |
|
| 26 |
if (old_suffix_len <= base_len) { |
| 27 |
for (c = 0; c < old_suffix_len; c++) { |
| 28 |
if (!FILECHARCASEEQ (old_suffix[old_suffix_len - c - 1], |
| 29 |
base[base_len - c - 1])) |
| 30 |
break; |
| 31 |
} |
| 32 |
if (c == old_suffix_len) { |
| 33 |
copy_limit -= old_suffix_len; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
answer = xmalloc (copy_limit + strlen (new_suffix) + 1); |
| 38 |
strncpy (answer, base, copy_limit); |
| 39 |
answer[copy_limit] = 0; |
| 40 |
strcat (answer, new_suffix); |
| 41 |
|
| 42 |
return answer; |
| 43 |
} |