Whamcloud - gitweb
LU-3963 libcfs: Use kernel's strncasecmp and remove cfs_get_blocked_sigs
[fs/lustre-release.git] / libcfs / libcfs / libcfs_string.c
index db376b5..bb956a2 100644 (file)
 
 #include <libcfs/libcfs.h>
 
-/* 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 +109,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 +119,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
@@ -140,7 +147,7 @@ char *cfs_strdup(const char *str, u_int32_t flags)
 
         lenz = strlen(str) + 1;
 
-        dup_str = cfs_alloc(lenz, flags);
+       dup_str = kmalloc(lenz, flags);
         if (dup_str == NULL)
                 return NULL;
 
@@ -299,7 +306,6 @@ cfs_str2num_check(char *str, int nob, unsigned *num,
 {
        char    *endp;
 
-       str = cfs_trimwhite(str);
        *num = strtoul(str, &endp, 0);
        if (endp == str)
                return 0;
@@ -392,6 +398,73 @@ cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
 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 = cfs_snprintf(buffer, count, "%u", expr->re_lo);
+       else if (expr->re_stride == 1)
+               i = cfs_snprintf(buffer, count, "%s%u-%u%s",
+                                 s, expr->re_lo, expr->re_hi, e);
+       else
+               i = cfs_snprintf(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 += cfs_snprintf(buffer + i, count - i, "[");
+
+       list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
+               if (j++ != 0)
+                       i += cfs_snprintf(buffer + i, count - i, ",");
+               i += cfs_range_expr_print(buffer + i, count - i, expr,
+                                         numexprs > 1);
+       }
+
+       if (numexprs > 1)
+               i += cfs_snprintf(buffer + i, count - i, "]");
+
+       return i;
+}
+EXPORT_SYMBOL(cfs_expr_list_print);
+
+/**
  * Matches value (\a value) against ranges expression list \a expr_list.
  *
  * \retval 1 if \a value matches
@@ -402,7 +475,7 @@ cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
 {
        struct cfs_range_expr   *expr;
 
-       cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
+       list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
                if (value >= expr->re_lo && value <= expr->re_hi &&
                    ((value - expr->re_lo) % expr->re_stride) == 0)
                        return 1;
@@ -427,7 +500,7 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
        int                     count = 0;
        int                     i;
 
-       cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
+       list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
                for (i = expr->re_lo; i <= expr->re_hi; i++) {
                        if (((i - expr->re_lo) % expr->re_stride) == 0)
                                count++;
@@ -448,7 +521,7 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
                return -ENOMEM;
 
        count = 0;
-       cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
+       list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
                for (i = expr->re_lo; i <= expr->re_hi; i++) {
                        if (((i - expr->re_lo) % expr->re_stride) == 0)
                                val[count++] = i;
@@ -468,12 +541,12 @@ EXPORT_SYMBOL(cfs_expr_list_values);
 void
 cfs_expr_list_free(struct cfs_expr_list *expr_list)
 {
-       while (!cfs_list_empty(&expr_list->el_exprs)) {
+       while (!list_empty(&expr_list->el_exprs)) {
                struct cfs_range_expr *expr;
 
-               expr = cfs_list_entry(expr_list->el_exprs.next,
+               expr = list_entry(expr_list->el_exprs.next,
                                      struct cfs_range_expr, re_link),
-               cfs_list_del(&expr->re_link);
+               list_del(&expr->re_link);
                LIBCFS_FREE(expr, sizeof(*expr));
        }
 
@@ -481,23 +554,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;
-
-       cfs_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 \<cfs_expr_list\> token of the syntax.
  *
- * \retval 1 if \a str parses to \<number\> | \<expr_list\>
- * \retval 0 otherwise
+ * \retval 0 if \a str parses to \<number\> | \<expr_list\>
+ * \retval -errno otherwise
  */
 int
 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
@@ -515,7 +576,7 @@ cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
        src.ls_str = str;
        src.ls_len = len;
 
-       CFS_INIT_LIST_HEAD(&expr_list->el_exprs);
+       INIT_LIST_HEAD(&expr_list->el_exprs);
 
        if (src.ls_str[0] == '[' &&
            src.ls_str[src.ls_len - 1] == ']') {
@@ -535,13 +596,13 @@ cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
                        if (rc != 0)
                                break;
 
-                       cfs_list_add_tail(&expr->re_link,
+                       list_add_tail(&expr->re_link,
                                          &expr_list->el_exprs);
                }
        } else {
                rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
                if (rc == 0) {
-                       cfs_list_add_tail(&expr->re_link,
+                       list_add_tail(&expr->re_link,
                                          &expr_list->el_exprs);
                }
        }
@@ -564,21 +625,21 @@ EXPORT_SYMBOL(cfs_expr_list_parse);
  * \retval none
  */
 void
-cfs_expr_list_free_list(cfs_list_t *list)
+cfs_expr_list_free_list(struct list_head *list)
 {
        struct cfs_expr_list *el;
 
-       while (!cfs_list_empty(list)) {
-               el = cfs_list_entry(list->next,
+       while (!list_empty(list)) {
+               el = list_entry(list->next,
                                    struct cfs_expr_list, el_link);
-               cfs_list_del(&el->el_link);
+               list_del(&el->el_link);
                cfs_expr_list_free(el);
        }
 }
 EXPORT_SYMBOL(cfs_expr_list_free_list);
 
 int
-cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
+cfs_ip_addr_parse(char *str, int len, struct list_head *list)
 {
        struct cfs_expr_list    *el;
        struct cfs_lstr         src;
@@ -601,7 +662,7 @@ cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
                if (rc != 0)
                        goto out;
 
-               cfs_list_add_tail(&el->el_link, list);
+               list_add_tail(&el->el_link, list);
                i++;
        }
 
@@ -623,12 +684,12 @@ EXPORT_SYMBOL(cfs_ip_addr_parse);
  * \retval 0 otherwise
  */
 int
-cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
+cfs_ip_addr_match(__u32 addr, struct list_head *list)
 {
        struct cfs_expr_list *el;
        int i = 0;
 
-       cfs_list_for_each_entry_reverse(el, list, el_link) {
+       list_for_each_entry_reverse(el, list, el_link) {
                if (!cfs_expr_list_match(addr & 0xff, el))
                        return 0;
                addr >>= 8;
@@ -640,7 +701,7 @@ cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
 EXPORT_SYMBOL(cfs_ip_addr_match);
 
 void
-cfs_ip_addr_free(cfs_list_t *list)
+cfs_ip_addr_free(struct list_head *list)
 {
        cfs_expr_list_free_list(list);
 }