X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=libcfs%2Flibcfs%2Flibcfs_string.c;h=04e1dd56dd4309b1f07e547a97f71436090227dd;hb=8789d45536081ab667865ccc14b10628c7fadbf3;hp=be26f93c37fa7ebedc46c4b12de5bd4773e69115;hpb=148a7fba3d289caf053db937c8a08f63528dada1;p=fs%2Flustre-release.git diff --git a/libcfs/libcfs/libcfs_string.c b/libcfs/libcfs/libcfs_string.c index be26f93..04e1dd5 100644 --- a/libcfs/libcfs/libcfs_string.c +++ b/libcfs/libcfs/libcfs_string.c @@ -15,11 +15,7 @@ * * You should have received a copy of the GNU General Public License * version 2 along with this program; If not, see - * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf - * - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. + * http://www.gnu.org/licenses/gpl-2.0.html * * GPL HEADER END */ @@ -27,7 +23,7 @@ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Use is subject to license terms. * - * Copyright (c) 2012, Intel Corporation. + * Copyright (c) 2012, 2015, Intel Corporation. */ /* * This file is part of Lustre, http://www.lustre.org/ @@ -42,25 +38,32 @@ #include -/* non-0 = don't match */ -int cfs_strncasecmp(const char *s1, const char *s2, size_t n) +char *cfs_strrstr(const char *haystack, const char *needle) { - if (s1 == NULL || s2 == NULL) - return 1; + char *ptr; - if (n == 0) - return 0; + if (unlikely(haystack == NULL || needle == NULL)) + return NULL; - while (n-- != 0 && tolower(*s1) == tolower(*s2)) { - if (n == 0 || *s1 == '\0' || *s2 == '\0') - break; - s1++; - s2++; - } + if (strlen(needle) == 1) + return strrchr(haystack, needle[0]); + + ptr = strstr(haystack, needle); + if (ptr != NULL) { + while (1) { + char *tmp; + + tmp = strstr(&ptr[1], needle); + if (tmp == NULL) + return ptr; + + ptr = tmp; + } + } - return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2); + return NULL; } -EXPORT_SYMBOL(cfs_strncasecmp); +EXPORT_SYMBOL(cfs_strrstr); /* Convert a text string to a bitmask */ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), @@ -102,7 +105,7 @@ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), debugstr = bit2str(i); if (debugstr != NULL && strlen(debugstr) == len && - cfs_strncasecmp(str, debugstr, len) == 0) { + strncasecmp(str, debugstr, len) == 0) { if (op == '-') newmask &= ~(1 << i); else @@ -112,7 +115,7 @@ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), } } if (!found && len == 3 && - (cfs_strncasecmp(str, "ALL", len) == 0)) { + (strncasecmp(str, "ALL", len) == 0)) { if (op == '-') newmask = minmask; else @@ -132,54 +135,6 @@ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), } EXPORT_SYMBOL(cfs_str2mask); -/* Duplicate a string in a platform-independent way */ -char *cfs_strdup(const char *str, u_int32_t flags) -{ - size_t lenz; /* length of str + zero byte */ - char *dup_str; - - lenz = strlen(str) + 1; - - dup_str = kmalloc(lenz, flags); - if (dup_str == NULL) - return NULL; - - memcpy(dup_str, str, lenz); - - return dup_str; -} -EXPORT_SYMBOL(cfs_strdup); - -/** - * cfs_{v}snprintf() return the actual size that is printed rather than - * the size that would be printed in standard functions. - */ -/* safe vsnprintf */ -int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) -{ - int i; - - LASSERT(size > 0); - i = vsnprintf(buf, size, fmt, args); - - return (i >= size ? size - 1 : i); -} -EXPORT_SYMBOL(cfs_vsnprintf); - -/* safe snprintf */ -int cfs_snprintf(char *buf, size_t size, const char *fmt, ...) -{ - va_list args; - int i; - - va_start(args, fmt); - i = cfs_vsnprintf(buf, size, fmt, args); - va_end(args); - - return i; -} -EXPORT_SYMBOL(cfs_snprintf); - /* get the first string out of @str */ char *cfs_firststr(char *str, size_t size) { @@ -213,12 +168,12 @@ cfs_trimwhite(char *str) { char *end; - while (cfs_iswhite(*str)) + while (isspace(*str)) str++; end = str + strlen(str); while (end > str) { - if (!cfs_iswhite(end[-1])) + if (!isspace(end[-1])) break; end--; } @@ -248,7 +203,7 @@ cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res) /* skip leading white spaces */ while (next->ls_len) { - if (!cfs_iswhite(*next->ls_str)) + if (!isspace(*next->ls_str)) break; next->ls_str++; next->ls_len--; @@ -275,7 +230,7 @@ cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res) /* skip ending whitespaces */ while (--end != res->ls_str) { - if (!cfs_iswhite(*end)) + if (!isspace(*end)) break; } @@ -299,13 +254,12 @@ cfs_str2num_check(char *str, int nob, unsigned *num, { char *endp; - str = cfs_trimwhite(str); - *num = strtoul(str, &endp, 0); + *num = simple_strtoul(str, &endp, 0); if (endp == str) return 0; for (; endp < str + nob; endp++) { - if (!cfs_iswhite(*endp)) + if (!isspace(*endp)) return 0; } @@ -326,7 +280,7 @@ EXPORT_SYMBOL(cfs_str2num_check); * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or * -ENOMEM will be returned. */ -int +static int cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max, int bracketed, struct cfs_range_expr **expr) { @@ -389,7 +343,73 @@ cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max, LIBCFS_FREE(re, sizeof(*re)); return -EINVAL; } -EXPORT_SYMBOL(cfs_range_expr_parse); + +/** + * Print the range expression \a re into specified \a buffer. + * If \a bracketed is true, expression does not need additional + * brackets. + * + * \retval number of characters written + */ +static int +cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr, + bool bracketed) +{ + int i; + char s[] = "["; + char e[] = "]"; + + if (bracketed) + s[0] = e[0] = '\0'; + + if (expr->re_lo == expr->re_hi) + i = scnprintf(buffer, count, "%u", expr->re_lo); + else if (expr->re_stride == 1) + i = scnprintf(buffer, count, "%s%u-%u%s", + s, expr->re_lo, expr->re_hi, e); + else + i = scnprintf(buffer, count, "%s%u-%u/%u%s", + s, expr->re_lo, expr->re_hi, + expr->re_stride, e); + return i; +} + +/** + * Print a list of range expressions (\a expr_list) into specified \a buffer. + * If the list contains several expressions, separate them with comma + * and surround the list with brackets. + * + * \retval number of characters written + */ +int +cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list) +{ + struct cfs_range_expr *expr; + int i = 0, j = 0; + int numexprs = 0; + + if (count <= 0) + return 0; + + list_for_each_entry(expr, &expr_list->el_exprs, re_link) + numexprs++; + + if (numexprs > 1) + i += scnprintf(buffer + i, count - i, "["); + + list_for_each_entry(expr, &expr_list->el_exprs, re_link) { + if (j++ != 0) + i += scnprintf(buffer + i, count - i, ","); + i += cfs_range_expr_print(buffer + i, count - i, expr, + numexprs > 1); + } + + if (numexprs > 1) + i += scnprintf(buffer + i, count - i, "]"); + + return i; +} +EXPORT_SYMBOL(cfs_expr_list_print); /** * Matches value (\a value) against ranges expression list \a expr_list. @@ -460,6 +480,16 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp) } EXPORT_SYMBOL(cfs_expr_list_values); +void +cfs_expr_list_values_free(__u32 *values, int num) +{ + /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed + * by OBD_FREE() if it's called by module other than libcfs & LNet, + * otherwise we will see fake memory leak */ + LIBCFS_FREE(values, num * sizeof(values[0])); +} +EXPORT_SYMBOL(cfs_expr_list_values_free); + /** * Frees cfs_range_expr structures of \a expr_list. * @@ -472,7 +502,7 @@ cfs_expr_list_free(struct cfs_expr_list *expr_list) struct cfs_range_expr *expr; expr = list_entry(expr_list->el_exprs.next, - struct cfs_range_expr, re_link), + struct cfs_range_expr, re_link); list_del(&expr->re_link); LIBCFS_FREE(expr, sizeof(*expr)); } @@ -481,23 +511,11 @@ cfs_expr_list_free(struct cfs_expr_list *expr_list) } EXPORT_SYMBOL(cfs_expr_list_free); -void -cfs_expr_list_print(struct cfs_expr_list *expr_list) -{ - struct cfs_range_expr *expr; - - list_for_each_entry(expr, &expr_list->el_exprs, re_link) { - CDEBUG(D_WARNING, "%d-%d/%d\n", - expr->re_lo, expr->re_hi, expr->re_stride); - } -} -EXPORT_SYMBOL(cfs_expr_list_print); - /** * Parses \ token of the syntax. * - * \retval 1 if \a str parses to \ | \ - * \retval 0 otherwise + * \retval 0 if \a str parses to \ | \ + * \retval -errno otherwise */ int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max, @@ -576,72 +594,3 @@ cfs_expr_list_free_list(struct list_head *list) } } EXPORT_SYMBOL(cfs_expr_list_free_list); - -int -cfs_ip_addr_parse(char *str, int len, struct list_head *list) -{ - struct cfs_expr_list *el; - struct cfs_lstr src; - int rc; - int i; - - src.ls_str = str; - src.ls_len = len; - i = 0; - - while (src.ls_str != NULL) { - struct cfs_lstr res; - - if (!cfs_gettok(&src, '.', &res)) { - rc = -EINVAL; - goto out; - } - - rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el); - if (rc != 0) - goto out; - - list_add_tail(&el->el_link, list); - i++; - } - - if (i == 4) - return 0; - - rc = -EINVAL; - out: - cfs_expr_list_free_list(list); - - return rc; -} -EXPORT_SYMBOL(cfs_ip_addr_parse); - -/** - * Matches address (\a addr) against address set encoded in \a list. - * - * \retval 1 if \a addr matches - * \retval 0 otherwise - */ -int -cfs_ip_addr_match(__u32 addr, struct list_head *list) -{ - struct cfs_expr_list *el; - int i = 0; - - list_for_each_entry_reverse(el, list, el_link) { - if (!cfs_expr_list_match(addr & 0xff, el)) - return 0; - addr >>= 8; - i++; - } - - return i == 4; -} -EXPORT_SYMBOL(cfs_ip_addr_match); - -void -cfs_ip_addr_free(struct list_head *list) -{ - cfs_expr_list_free_list(list); -} -EXPORT_SYMBOL(cfs_ip_addr_free);