| 1 |
/* find-suffix.c: return the stuff after a dot. |
| 2 |
|
| 3 |
Copyright 1992, 1993, 1995, 2008, 2011, 2016 Karl Berry. |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#include <kpathsea/config.h> |
| 19 |
|
| 20 |
#include <kpathsea/c-pathch.h> |
| 21 |
|
| 22 |
|
| 23 |
/* Return pointer to first character after `.' in last directory element |
| 24 |
of NAME. If the name is `foo' or `/foo.bar/baz', we have no extension. */ |
| 25 |
|
| 26 |
/* The the result of strrchr(NAME, '.'), when not NULL, is a non-const |
| 27 |
pointer into the string NAME. However, this is cheating (motivated |
| 28 |
by limitations of the C language) when the argument NAME is a |
| 29 |
const string, because in that case the (technically non-const) result |
| 30 |
from strrchr() is certainly not modifiable. |
| 31 |
|
| 32 |
We do not want to repeat this kind of cheating for find_suffix() and |
| 33 |
therefore declare find_suffix(NAME) as const. When find_suffix(NAME) |
| 34 |
is non-NULL and the argument NAME is modifiable (i.e., non-const) |
| 35 |
then NAME+(find_suffix(NAME)-NAME) is an equivalent modifiable string |
| 36 |
and the pointer arithmetic is optimized away by modern compilers. */ |
| 37 |
|
| 38 |
const_string |
| 39 |
find_suffix (const_string name) |
| 40 |
{ |
| 41 |
const_string dot_pos = strrchr (name, '.'); |
| 42 |
const_string p; |
| 43 |
|
| 44 |
if (dot_pos == NULL) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
for (p = dot_pos + 1; *p; p++) { |
| 48 |
if (IS_DIR_SEP (*p)) |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
return dot_pos + 1; |
| 53 |
} |