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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * String manipulation functions.
34 * libcfs/libcfs/util/string.c
36 * Author: Nathan Rutman <nathan.rutman@sun.com>
44 #include <libcfs/util/string.h>
47 * According manual of strlcpy() and strlcat() the functions should return
48 * the total length of the string they tried to create. For strlcpy() that
49 * means the length of src. For strlcat() that means the initial length of
50 * dst plus the length of src. So, the function strnlen() cannot be used
51 * otherwise the return value will be wrong.
53 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
54 size_t strlcpy(char *dst, const char *src, size_t size)
56 size_t ret = strlen(src);
59 size_t len = (ret >= size) ? size - 1 : ret;
60 memcpy(dst, src, len);
67 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
68 size_t strlcat(char *dst, const char *src, size_t size)
70 size_t dsize = strlen(dst);
71 size_t len = strlen(src);
72 size_t ret = dsize + len;
78 memcpy(dst, src, len);
85 * Extracts tokens from strings.
87 * Looks for \a delim in string \a next, sets \a res to point to
88 * substring before the delimiter, sets \a next right after the found
91 * \retval 1 if \a res points to a string of non-whitespace characters
95 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
99 if (next->ls_str == NULL)
102 /* skip leading white spaces */
103 while (next->ls_len) {
104 if (!isspace(*next->ls_str))
110 if (next->ls_len == 0) /* whitespaces only */
113 if (*next->ls_str == delim) {
114 /* first non-writespace is the delimiter */
118 res->ls_str = next->ls_str;
119 end = memchr(next->ls_str, delim, next->ls_len);
121 /* there is no the delimeter in the string */
122 end = next->ls_str + next->ls_len;
125 next->ls_str = end + 1;
126 next->ls_len -= (end - res->ls_str + 1);
129 /* skip ending whitespaces */
130 while (--end != res->ls_str) {
135 res->ls_len = end - res->ls_str + 1;
140 * Converts string to integer.
142 * Accepts decimal and hexadecimal number recordings.
144 * \retval 1 if first \a nob chars of \a str convert to decimal or
145 * hexadecimal integer in the range [\a min, \a max]
146 * \retval 0 otherwise
149 cfs_str2num_check(char *str, int nob, unsigned *num,
150 unsigned min, unsigned max)
154 *num = strtoul(str, &endp, 0);
158 for (; endp < str + nob; endp++) {
163 return (*num >= min && *num <= max);
167 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
168 * \a src should only have a single token which can be \<number\> or \*
170 * \retval pointer to allocated range_expr and initialized
171 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
174 * \<number\> '-' \<number\> |
175 * \<number\> '-' \<number\> '/' \<number\>
176 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
177 * -ENOMEM will be returned.
180 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
181 int bracketed, struct cfs_range_expr **expr)
183 struct cfs_range_expr *re;
186 re = calloc(1, sizeof(*re));
190 if (src->ls_len == 1 && src->ls_str[0] == '*') {
197 if (cfs_str2num_check(src->ls_str, src->ls_len,
198 &re->re_lo, min, max)) {
199 /* <number> is parsed */
200 re->re_hi = re->re_lo;
205 if (!bracketed || !cfs_gettok(src, '-', &tok))
208 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
209 &re->re_lo, min, max))
213 if (cfs_str2num_check(src->ls_str, src->ls_len,
214 &re->re_hi, min, max)) {
215 /* <number> - <number> is parsed */
220 /* go to check <number> '-' <number> '/' <number> */
221 if (cfs_gettok(src, '/', &tok)) {
222 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
223 &re->re_hi, min, max))
226 /* <number> - <number> / ... */
227 if (cfs_str2num_check(src->ls_str, src->ls_len,
228 &re->re_stride, min, max)) {
229 /* <number> - <number> / <number> is parsed */
244 * Print the range expression \a re into specified \a buffer.
245 * If \a bracketed is true, expression does not need additional
248 * \retval number of characters written
251 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
261 if (expr->re_lo == expr->re_hi)
262 i = snprintf(buffer, count, "%u", expr->re_lo);
263 else if (expr->re_stride == 1)
264 i = snprintf(buffer, count, "%s%u-%u%s",
265 s, expr->re_lo, expr->re_hi, e);
267 i = snprintf(buffer, count, "%s%u-%u/%u%s",
268 s, expr->re_lo, expr->re_hi,
274 * Print a list of range expressions (\a expr_list) into specified \a buffer.
275 * If the list contains several expressions, separate them with comma
276 * and surround the list with brackets.
278 * \retval number of characters written
281 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
283 struct cfs_range_expr *expr;
290 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
294 i += snprintf(buffer + i, count - i, "[");
296 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
298 i += snprintf(buffer + i, count - i, ",");
299 i += cfs_range_expr_print(buffer + i, count - i, expr,
304 i += snprintf(buffer + i, count - i, "]");
310 * Matches value (\a value) against ranges expression list \a expr_list.
312 * \retval 1 if \a value matches
313 * \retval 0 otherwise
316 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
318 struct cfs_range_expr *expr;
320 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
321 if (value >= expr->re_lo && value <= expr->re_hi &&
322 ((value - expr->re_lo) % expr->re_stride) == 0)
330 * Frees cfs_range_expr structures of \a expr_list.
335 cfs_expr_list_free(struct cfs_expr_list *expr_list)
337 while (!list_empty(&expr_list->el_exprs)) {
338 struct cfs_range_expr *expr;
340 expr = list_entry(expr_list->el_exprs.next,
341 struct cfs_range_expr, re_link);
342 list_del(&expr->re_link);
350 * Parses \<cfs_expr_list\> token of the syntax.
352 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
353 * \retval -errno otherwise
356 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
357 struct cfs_expr_list **elpp)
359 struct cfs_expr_list *expr_list;
360 struct cfs_range_expr *expr;
364 expr_list = calloc(1, sizeof(*expr_list));
365 if (expr_list == NULL)
371 INIT_LIST_HEAD(&expr_list->el_exprs);
373 if (src.ls_str[0] == '[' &&
374 src.ls_str[src.ls_len - 1] == ']') {
379 while (src.ls_str != NULL) {
382 if (!cfs_gettok(&src, ',', &tok)) {
387 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
391 list_add_tail(&expr->re_link,
392 &expr_list->el_exprs);
395 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
397 list_add_tail(&expr->re_link,
398 &expr_list->el_exprs);
403 cfs_expr_list_free(expr_list);
411 * Frees cfs_expr_list structures of \a list.
413 * For each struct cfs_expr_list structure found on \a list it frees
414 * range_expr list attached to it and frees the cfs_expr_list itself.
419 cfs_expr_list_free_list(struct list_head *list)
421 struct cfs_expr_list *el;
423 while (!list_empty(list)) {
424 el = list_entry(list->next,
425 struct cfs_expr_list, el_link);
426 list_del(&el->el_link);
427 cfs_expr_list_free(el);