diff options
| author | James Morris <james.morris@microsoft.com> | 2019-01-10 11:41:59 -0800 |
|---|---|---|
| committer | James Morris <james.morris@microsoft.com> | 2019-01-10 11:41:59 -0800 |
| commit | 49e41801b335f64610bbfd23e8f2bbaf34d46276 (patch) | |
| tree | 4fbedacd1de1bbd4054f07f93031aebcb7b7a919 /include/linux/string.h | |
| parent | b49d564344f773d8afee982153c8493e5f2eaf38 (diff) | |
| parent | bfeffd155283772bbe78c6a05dec7c0128ee500c (diff) | |
Merge tag 'v5.0-rc1' into next-general
Linux 5.0-rc1
Sync to pick up LSM stacking work (which is based on -rc1).
Diffstat (limited to 'include/linux/string.h')
| -rw-r--r-- | include/linux/string.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index 27d0482e5e05..7927b875f80c 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -456,4 +456,24 @@ static inline void memcpy_and_pad(void *dest, size_t dest_len, memcpy(dest, src, dest_len); } +/** + * str_has_prefix - Test if a string has a given prefix + * @str: The string to test + * @prefix: The string to see if @str starts with + * + * A common way to test a prefix of a string is to do: + * strncmp(str, prefix, sizeof(prefix) - 1) + * + * But this can lead to bugs due to typos, or if prefix is a pointer + * and not a constant. Instead use str_has_prefix(). + * + * Returns: 0 if @str does not start with @prefix + strlen(@prefix) if @str does start with @prefix + */ +static __always_inline size_t str_has_prefix(const char *str, const char *prefix) +{ + size_t len = strlen(prefix); + return strncmp(str, prefix, len) == 0 ? len : 0; +} + #endif /* _LINUX_STRING_H_ */ |
