4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * String manipulation functions.
38 * libcfs/libcfs/util/string.c
40 * Author: Nathan Rutman <nathan.rutman@sun.com>
48 #include <libcfs/util/string.h>
51 * According manual of strlcpy() and strlcat() the functions should return
52 * the total length of the string they tried to create. For strlcpy() that
53 * means the length of src. For strlcat() that means the initial length of
54 * dst plus the length of src. So, the function strnlen() cannot be used
55 * otherwise the return value will be wrong.
57 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
58 size_t strlcpy(char *dst, const char *src, size_t size)
60 size_t ret = strlen(src);
63 size_t len = (ret >= size) ? size - 1 : ret;
64 memcpy(dst, src, len);
71 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
72 size_t strlcat(char *dst, const char *src, size_t size)
74 size_t dsize = strlen(dst);
75 size_t len = strlen(src);
76 size_t ret = dsize + len;
82 memcpy(dst, src, len);
89 * Extracts tokens from strings.
91 * Looks for \a delim in string \a next, sets \a res to point to
92 * substring before the delimiter, sets \a next right after the found
95 * \retval 1 if \a res points to a string of non-whitespace characters
99 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
103 if (next->ls_str == NULL)
106 /* skip leading white spaces */
107 while (next->ls_len) {
108 if (!isspace(*next->ls_str))
114 if (next->ls_len == 0) /* whitespaces only */
117 if (*next->ls_str == delim) {
118 /* first non-writespace is the delimiter */
122 res->ls_str = next->ls_str;
123 end = memchr(next->ls_str, delim, next->ls_len);
125 /* there is no the delimeter in the string */
126 end = next->ls_str + next->ls_len;
129 next->ls_str = end + 1;
130 next->ls_len -= (end - res->ls_str + 1);
133 /* skip ending whitespaces */
134 while (--end != res->ls_str) {
139 res->ls_len = end - res->ls_str + 1;
144 * Converts string to integer.
146 * Accepts decimal and hexadecimal number recordings.
148 * \retval 1 if first \a nob chars of \a str convert to decimal or
149 * hexadecimal integer in the range [\a min, \a max]
150 * \retval 0 otherwise
153 cfs_str2num_check(char *str, int nob, unsigned *num,
154 unsigned min, unsigned max)
158 *num = strtoul(str, &endp, 0);
162 for (; endp < str + nob; endp++) {
167 return (*num >= min && *num <= max);
171 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
172 * \a src should only have a single token which can be \<number\> or \*
174 * \retval pointer to allocated range_expr and initialized
175 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
178 * \<number\> '-' \<number\> |
179 * \<number\> '-' \<number\> '/' \<number\>
180 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
181 * -ENOMEM will be returned.
184 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
185 int bracketed, struct cfs_range_expr **expr)
187 struct cfs_range_expr *re;
190 re = calloc(1, sizeof(*re));
194 if (src->ls_len == 1 && src->ls_str[0] == '*') {
201 if (cfs_str2num_check(src->ls_str, src->ls_len,
202 &re->re_lo, min, max)) {
203 /* <number> is parsed */
204 re->re_hi = re->re_lo;
209 if (!bracketed || !cfs_gettok(src, '-', &tok))
212 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
213 &re->re_lo, min, max))
217 if (cfs_str2num_check(src->ls_str, src->ls_len,
218 &re->re_hi, min, max)) {
219 /* <number> - <number> is parsed */
224 /* go to check <number> '-' <number> '/' <number> */
225 if (cfs_gettok(src, '/', &tok)) {
226 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
227 &re->re_hi, min, max))
230 /* <number> - <number> / ... */
231 if (cfs_str2num_check(src->ls_str, src->ls_len,
232 &re->re_stride, min, max)) {
233 /* <number> - <number> / <number> is parsed */
248 * Print the range expression \a re into specified \a buffer.
249 * If \a bracketed is true, expression does not need additional
252 * \retval number of characters written
255 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
265 if (expr->re_lo == expr->re_hi)
266 i = snprintf(buffer, count, "%u", expr->re_lo);
267 else if (expr->re_stride == 1)
268 i = snprintf(buffer, count, "%s%u-%u%s",
269 s, expr->re_lo, expr->re_hi, e);
271 i = snprintf(buffer, count, "%s%u-%u/%u%s",
272 s, expr->re_lo, expr->re_hi,
278 * Print a list of range expressions (\a expr_list) into specified \a buffer.
279 * If the list contains several expressions, separate them with comma
280 * and surround the list with brackets.
282 * \retval number of characters written
285 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
287 struct cfs_range_expr *expr;
294 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
298 i += snprintf(buffer + i, count - i, "[");
300 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
302 i += snprintf(buffer + i, count - i, ",");
303 i += cfs_range_expr_print(buffer + i, count - i, expr,
308 i += snprintf(buffer + i, count - i, "]");
314 * Matches value (\a value) against ranges expression list \a expr_list.
316 * \retval 1 if \a value matches
317 * \retval 0 otherwise
320 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
322 struct cfs_range_expr *expr;
324 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
325 if (value >= expr->re_lo && value <= expr->re_hi &&
326 ((value - expr->re_lo) % expr->re_stride) == 0)
334 * Frees cfs_range_expr structures of \a expr_list.
339 cfs_expr_list_free(struct cfs_expr_list *expr_list)
341 while (!list_empty(&expr_list->el_exprs)) {
342 struct cfs_range_expr *expr;
344 expr = list_entry(expr_list->el_exprs.next,
345 struct cfs_range_expr, re_link);
346 list_del(&expr->re_link);
354 * Parses \<cfs_expr_list\> token of the syntax.
356 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
357 * \retval -errno otherwise
360 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
361 struct cfs_expr_list **elpp)
363 struct cfs_expr_list *expr_list;
364 struct cfs_range_expr *expr;
368 expr_list = calloc(1, sizeof(*expr_list));
369 if (expr_list == NULL)
375 INIT_LIST_HEAD(&expr_list->el_exprs);
377 if (src.ls_str[0] == '[' &&
378 src.ls_str[src.ls_len - 1] == ']') {
383 while (src.ls_str != NULL) {
386 if (!cfs_gettok(&src, ',', &tok)) {
391 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
395 list_add_tail(&expr->re_link,
396 &expr_list->el_exprs);
399 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
401 list_add_tail(&expr->re_link,
402 &expr_list->el_exprs);
407 cfs_expr_list_free(expr_list);
415 * Frees cfs_expr_list structures of \a list.
417 * For each struct cfs_expr_list structure found on \a list it frees
418 * range_expr list attached to it and frees the cfs_expr_list itself.
423 cfs_expr_list_free_list(struct list_head *list)
425 struct cfs_expr_list *el;
427 while (!list_empty(list)) {
428 el = list_entry(list->next,
429 struct cfs_expr_list, el_link);
430 list_del(&el->el_link);
431 cfs_expr_list_free(el);