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, 2017, 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>
46 #include <libcfs/util/string.h>
49 * Extracts tokens from strings.
51 * Looks for \a delim in string \a next, sets \a res to point to
52 * substring before the delimiter, sets \a next right after the found
55 * \retval 1 if \a res points to a string of non-whitespace characters
59 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
63 if (next->ls_str == NULL)
66 /* skip leading white spaces */
67 while (next->ls_len) {
68 if (!isspace(*next->ls_str))
74 if (next->ls_len == 0) /* whitespaces only */
77 if (*next->ls_str == delim) {
78 /* first non-writespace is the delimiter */
82 res->ls_str = next->ls_str;
83 end = memchr(next->ls_str, delim, next->ls_len);
85 /* there is no the delimeter in the string */
86 end = next->ls_str + next->ls_len;
89 next->ls_str = end + 1;
90 next->ls_len -= (end - res->ls_str + 1);
93 /* skip ending whitespaces */
94 while (--end != res->ls_str) {
99 res->ls_len = end - res->ls_str + 1;
104 * Converts string to integer.
106 * Accepts decimal and hexadecimal number recordings.
108 * \retval 1 if first \a nob chars of \a str convert to decimal or
109 * hexadecimal integer in the range [\a min, \a max]
110 * \retval 0 otherwise
113 cfs_str2num_check(char *str, int nob, unsigned *num,
114 unsigned min, unsigned max)
118 *num = strtoul(str, &endp, 0);
122 for (; endp < str + nob; endp++) {
127 return (*num >= min && *num <= max);
131 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
132 * \a src should only have a single token which can be \<number\> or \*
134 * \retval pointer to allocated range_expr and initialized
135 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
138 * \<number\> '-' \<number\> |
139 * \<number\> '-' \<number\> '/' \<number\>
140 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
141 * -ENOMEM will be returned.
144 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
145 int bracketed, struct cfs_range_expr **expr)
147 struct cfs_range_expr *re;
150 re = calloc(1, sizeof(*re));
154 if (src->ls_len == 1 && src->ls_str[0] == '*') {
161 if (cfs_str2num_check(src->ls_str, src->ls_len,
162 &re->re_lo, min, max)) {
163 /* <number> is parsed */
164 re->re_hi = re->re_lo;
169 if (!bracketed || !cfs_gettok(src, '-', &tok))
172 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
173 &re->re_lo, min, max))
177 if (cfs_str2num_check(src->ls_str, src->ls_len,
178 &re->re_hi, min, max)) {
179 /* <number> - <number> is parsed */
184 /* go to check <number> '-' <number> '/' <number> */
185 if (cfs_gettok(src, '/', &tok)) {
186 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
187 &re->re_hi, min, max))
190 /* <number> - <number> / ... */
191 if (cfs_str2num_check(src->ls_str, src->ls_len,
192 &re->re_stride, min, max)) {
193 /* <number> - <number> / <number> is parsed */
208 * Print the range expression \a re into specified \a buffer.
209 * If \a bracketed is true, expression does not need additional
212 * \retval number of characters written
215 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
225 if (expr->re_lo == expr->re_hi)
226 i = snprintf(buffer, count, "%u", expr->re_lo);
227 else if (expr->re_stride == 1)
228 i = snprintf(buffer, count, "%s%u-%u%s",
229 s, expr->re_lo, expr->re_hi, e);
231 i = snprintf(buffer, count, "%s%u-%u/%u%s",
232 s, expr->re_lo, expr->re_hi,
238 * Print a list of range expressions (\a expr_list) into specified \a buffer.
239 * If the list contains several expressions, separate them with comma
240 * and surround the list with brackets.
242 * \retval number of characters written
245 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
247 struct cfs_range_expr *expr;
254 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
258 i += snprintf(buffer + i, count - i, "[");
260 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
262 i += snprintf(buffer + i, count - i, ",");
263 i += cfs_range_expr_print(buffer + i, count - i, expr,
268 i += snprintf(buffer + i, count - i, "]");
274 * Matches value (\a value) against ranges expression list \a expr_list.
276 * \retval 1 if \a value matches
277 * \retval 0 otherwise
280 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
282 struct cfs_range_expr *expr;
284 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
285 if (value >= expr->re_lo && value <= expr->re_hi &&
286 ((value - expr->re_lo) % expr->re_stride) == 0)
294 * Convert express list (\a expr_list) to an array of all matched values
296 * \retval N N is total number of all matched values
297 * \retval 0 if expression list is empty
298 * \retval < 0 for failure
301 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
303 struct cfs_range_expr *expr;
308 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
309 for (i = expr->re_lo; i <= expr->re_hi; i++) {
310 if (((i - expr->re_lo) % expr->re_stride) == 0)
315 if (count == 0) /* empty expression list */
321 val = calloc(sizeof(val[0]), count);
326 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
327 for (i = expr->re_lo; i <= expr->re_hi; i++) {
328 if (((i - expr->re_lo) % expr->re_stride) == 0)
338 cfs_expr_list_values_free(__u32 *values, int num)
340 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
341 * by OBD_FREE() if it's called by module other than libcfs & LNet,
342 * otherwise we will see fake memory leak */
347 * Frees cfs_range_expr structures of \a expr_list.
352 cfs_expr_list_free(struct cfs_expr_list *expr_list)
354 while (!list_empty(&expr_list->el_exprs)) {
355 struct cfs_range_expr *expr;
357 expr = list_entry(expr_list->el_exprs.next,
358 struct cfs_range_expr, re_link);
359 list_del(&expr->re_link);
367 * Parses \<cfs_expr_list\> token of the syntax.
369 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
370 * \retval -errno otherwise
373 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
374 struct cfs_expr_list **elpp)
376 struct cfs_expr_list *expr_list;
377 struct cfs_range_expr *expr;
381 expr_list = calloc(1, sizeof(*expr_list));
382 if (expr_list == NULL)
388 INIT_LIST_HEAD(&expr_list->el_exprs);
390 if (src.ls_str[0] == '[' &&
391 src.ls_str[src.ls_len - 1] == ']') {
396 while (src.ls_str != NULL) {
399 if (!cfs_gettok(&src, ',', &tok)) {
404 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
408 list_add_tail(&expr->re_link,
409 &expr_list->el_exprs);
412 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
414 list_add_tail(&expr->re_link,
415 &expr_list->el_exprs);
420 cfs_expr_list_free(expr_list);
428 * Frees cfs_expr_list structures of \a list.
430 * For each struct cfs_expr_list structure found on \a list it frees
431 * range_expr list attached to it and frees the cfs_expr_list itself.
436 cfs_expr_list_free_list(struct list_head *list)
438 struct cfs_expr_list *el;
440 while (!list_empty(list)) {
441 el = list_entry(list->next,
442 struct cfs_expr_list, el_link);
443 list_del(&el->el_link);
444 cfs_expr_list_free(el);
449 * cfs_abs_path() - Get the absolute path of a relative path
450 * @request_path: The relative path to be resolved
451 * @resolved_path: Set to the resolved absolute path
453 * Returns the canonicalized absolute pathname. This function is a wrapper to
454 * realpath, but will work even if the target file does not exist. All
455 * directories in the path must exist.
457 * Return: On success, 0 is returned and resolved_path points to an allocated
458 * string containing the absolute pathname. On error, errno is set
459 * appropriately, -errno is returned, and resolved_path points to NULL.
461 int cfs_abs_path(const char *request_path, char **resolved_path)
463 char buf[PATH_MAX + 1] = "";
470 path = malloc(sizeof(buf));
474 if (request_path[0] != '/') {
475 if (getcwd(path, sizeof(buf) - 1) == NULL) {
479 len = snprintf(buf, sizeof(buf), "%s/%s", path, request_path);
480 if (len >= sizeof(buf)) {
485 /* skip duplicate leading '/' */
486 len = snprintf(buf, sizeof(buf), "%s",
487 request_path + strspn(request_path, "/") - 1);
488 if (len >= sizeof(buf)) {
494 /* if filename not in root directory, call realpath for parent path */
495 ptr = strrchr(buf, '/');
498 if (path != realpath(buf, path)) {
502 /* add the filename back */
504 fmt = (path[len - 1] == '/') ? "%s" : "/%s";
505 len = snprintf(path + len, sizeof(buf) - len, fmt, ptr + 1);
506 if (len >= sizeof(buf) - len) {
511 len = snprintf(path, sizeof(buf), "%s", buf);
512 if (len >= sizeof(buf)) {
520 *resolved_path = path;
522 *resolved_path = NULL;