util: change endswith() to return a pointer to the suffix

This commit is contained in:
Lennart Poettering 2012-10-19 04:52:51 +02:00
parent 857a493d55
commit 8c7c140fca
2 changed files with 8 additions and 5 deletions

View File

@ -195,7 +195,7 @@ struct timeval *timeval_store(struct timeval *tv, usec_t u) {
return tv;
}
bool endswith(const char *s, const char *postfix) {
char* endswith(const char *s, const char *postfix) {
size_t sl, pl;
assert(s);
@ -205,12 +205,15 @@ bool endswith(const char *s, const char *postfix) {
pl = strlen(postfix);
if (pl == 0)
return true;
return (char*) s + sl;
if (sl < pl)
return false;
return NULL;
return memcmp(s + sl - pl, postfix, pl) == 0;
if (memcmp(s + sl - pl, postfix, pl) != 0)
return NULL;
return (char*) s + sl - pl;
}
bool startswith(const char *s, const char *prefix) {

View File

@ -141,7 +141,7 @@ static inline bool isempty(const char *p) {
return !p || !p[0];
}
bool endswith(const char *s, const char *postfix);
char *endswith(const char *s, const char *postfix);
bool startswith(const char *s, const char *prefix);
bool startswith_no_case(const char *s, const char *prefix);