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/
31 * String manipulation functions.
33 * libcfs/libcfs/util/string.c
35 * Author: Nathan Rutman <nathan.rutman@sun.com>
45 #include <libcfs/util/string.h>
48 * Extracts tokens from strings.
50 * Looks for \a delim in string \a next, sets \a res to point to
51 * substring before the delimiter, sets \a next right after the found
54 * \retval 1 if \a res points to a string of non-whitespace characters
58 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
62 if (next->ls_str == NULL)
65 /* skip leading white spaces */
66 while (next->ls_len) {
67 if (!isspace(*next->ls_str))
73 if (next->ls_len == 0) /* whitespaces only */
76 if (*next->ls_str == delim) {
77 /* first non-writespace is the delimiter */
81 res->ls_str = next->ls_str;
82 end = memchr(next->ls_str, delim, next->ls_len);
84 /* there is no the delimeter in the string */
85 end = next->ls_str + next->ls_len;
88 next->ls_str = end + 1;
89 next->ls_len -= (end - res->ls_str + 1);
92 /* skip ending whitespaces */
93 while (--end != res->ls_str) {
98 res->ls_len = end - res->ls_str + 1;
103 * Converts string to integer.
105 * Accepts decimal and hexadecimal number recordings.
107 * \retval 1 if first \a nob chars of \a str convert to decimal or
108 * hexadecimal integer in the range [\a min, \a max]
109 * \retval 0 otherwise
112 cfs_str2num_check(char *str, int nob, unsigned *num,
113 unsigned min, unsigned max)
117 *num = strtoul(str, &endp, 0);
121 for (; endp < str + nob; endp++) {
126 return (*num >= min && *num <= max);
130 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
131 * \a src should only have a single token which can be \<number\> or \*
133 * \retval pointer to allocated range_expr and initialized
134 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
137 * \<number\> '-' \<number\> |
138 * \<number\> '-' \<number\> '/' \<number\>
139 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
140 * -ENOMEM will be returned.
143 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
144 int bracketed, struct cfs_range_expr **expr)
146 struct cfs_range_expr *re;
149 re = calloc(1, sizeof(*re));
153 if (src->ls_len == 1 && src->ls_str[0] == '*') {
160 if (cfs_str2num_check(src->ls_str, src->ls_len,
161 &re->re_lo, min, max)) {
162 /* <number> is parsed */
163 re->re_hi = re->re_lo;
168 if (!bracketed || !cfs_gettok(src, '-', &tok))
171 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
172 &re->re_lo, min, max))
176 if (cfs_str2num_check(src->ls_str, src->ls_len,
177 &re->re_hi, min, max)) {
178 /* <number> - <number> is parsed */
183 /* go to check <number> '-' <number> '/' <number> */
184 if (cfs_gettok(src, '/', &tok)) {
185 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
186 &re->re_hi, min, max))
189 /* <number> - <number> / ... */
190 if (cfs_str2num_check(src->ls_str, src->ls_len,
191 &re->re_stride, min, max)) {
192 /* <number> - <number> / <number> is parsed */
207 * Print the range expression \a re into specified \a buffer.
208 * If \a bracketed is true, expression does not need additional
211 * \retval number of characters written
214 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
224 if (expr->re_lo == expr->re_hi)
225 i = snprintf(buffer, count, "%u", expr->re_lo);
226 else if (expr->re_stride == 1)
227 i = snprintf(buffer, count, "%s%u-%u%s",
228 s, expr->re_lo, expr->re_hi, e);
230 i = snprintf(buffer, count, "%s%u-%u/%u%s",
231 s, expr->re_lo, expr->re_hi,
237 * Print a list of range expressions (\a expr_list) into specified \a buffer.
238 * If the list contains several expressions, separate them with comma
239 * and surround the list with brackets.
241 * \retval number of characters written
244 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
246 struct cfs_range_expr *expr;
253 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
257 i += scnprintf(buffer + i, count - i, "[");
259 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
261 i += scnprintf(buffer + i, count - i, ",");
262 i += cfs_range_expr_print(buffer + i, count - i, expr,
267 i += scnprintf(buffer + i, count - i, "]");
273 * Matches value (\a value) against ranges expression list \a expr_list.
275 * \retval 1 if \a value matches
276 * \retval 0 otherwise
279 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
281 struct cfs_range_expr *expr;
283 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
284 if (value >= expr->re_lo && value <= expr->re_hi &&
285 ((value - expr->re_lo) % expr->re_stride) == 0)
293 * Convert express list (\a expr_list) to an array of all matched values
295 * \retval N N is total number of all matched values
296 * \retval 0 if expression list is empty
297 * \retval < 0 for failure
300 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
302 struct cfs_range_expr *expr;
307 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
308 for (i = expr->re_lo; i <= expr->re_hi; i++) {
309 if (((i - expr->re_lo) % expr->re_stride) == 0)
314 if (count == 0) /* empty expression list */
320 val = calloc(sizeof(val[0]), count);
325 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
326 for (i = expr->re_lo; i <= expr->re_hi; i++) {
327 if (((i - expr->re_lo) % expr->re_stride) == 0)
337 cfs_expr_list_values_free(__u32 *values, int num)
339 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
340 * by OBD_FREE() if it's called by module other than libcfs & LNet,
341 * otherwise we will see fake memory leak */
346 * Frees cfs_range_expr structures of \a expr_list.
351 cfs_expr_list_free(struct cfs_expr_list *expr_list)
353 while (!list_empty(&expr_list->el_exprs)) {
354 struct cfs_range_expr *expr;
356 expr = list_entry(expr_list->el_exprs.next,
357 struct cfs_range_expr, re_link);
358 list_del(&expr->re_link);
366 * Parses \<cfs_expr_list\> token of the syntax.
368 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
369 * \retval -errno otherwise
372 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
373 struct cfs_expr_list **elpp)
375 struct cfs_expr_list *expr_list;
376 struct cfs_range_expr *expr;
380 expr_list = calloc(1, sizeof(*expr_list));
381 if (expr_list == NULL)
387 INIT_LIST_HEAD(&expr_list->el_exprs);
389 if (src.ls_str[0] == '[' &&
390 src.ls_str[src.ls_len - 1] == ']') {
395 while (src.ls_str != NULL) {
398 if (!cfs_gettok(&src, ',', &tok)) {
403 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
407 list_add_tail(&expr->re_link,
408 &expr_list->el_exprs);
411 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
413 list_add_tail(&expr->re_link,
414 &expr_list->el_exprs);
419 cfs_expr_list_free(expr_list);
427 * Frees cfs_expr_list structures of \a list.
429 * For each struct cfs_expr_list structure found on \a list it frees
430 * range_expr list attached to it and frees the cfs_expr_list itself.
435 cfs_expr_list_free_list(struct list_head *list)
437 struct cfs_expr_list *el;
439 while (!list_empty(list)) {
440 el = list_entry(list->next,
441 struct cfs_expr_list, el_link);
442 list_del(&el->el_link);
443 cfs_expr_list_free(el);
448 * cfs_abs_path() - Get the absolute path of a relative path
449 * @request_path: The relative path to be resolved
450 * @resolved_path: Set to the resolved absolute path
452 * Returns the canonicalized absolute pathname. This function is a wrapper to
453 * realpath, but will work even if the target file does not exist. All
454 * directories in the path must exist.
456 * Return: On success, 0 is returned and resolved_path points to an allocated
457 * string containing the absolute pathname. On error, errno is set
458 * appropriately, -errno is returned, and resolved_path points to NULL.
460 int cfs_abs_path(const char *request_path, char **resolved_path)
462 char buf[PATH_MAX + 1] = "";
469 path = malloc(sizeof(buf));
473 if (request_path[0] != '/') {
474 if (getcwd(path, sizeof(buf) - 1) == NULL) {
478 len = snprintf(buf, sizeof(buf), "%s/%s", path, request_path);
479 if (len >= sizeof(buf)) {
484 /* skip duplicate leading '/' */
485 len = snprintf(buf, sizeof(buf), "%s",
486 request_path + strspn(request_path, "/") - 1);
487 if (len >= sizeof(buf)) {
493 /* if filename not in root directory, call realpath for parent path */
494 ptr = strrchr(buf, '/');
497 if (path != realpath(buf, path)) {
501 /* add the filename back */
503 fmt = (path[len - 1] == '/') ? "%s" : "/%s";
504 len = snprintf(path + len, sizeof(buf) - len, fmt, ptr + 1);
505 if (len >= sizeof(buf) - len) {
510 len = snprintf(path, sizeof(buf), "%s", buf);
511 if (len >= sizeof(buf)) {
519 *resolved_path = path;
521 *resolved_path = NULL;