Whamcloud - gitweb
1b6e819be5e9cb8c7d61ae3da457dd0f6c331c2a
[fs/lustre-release.git] / libcfs / include / libcfs / util / string.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/libcfs_string.h
33  *
34  * Generic string manipulation functions.
35  *
36  * Author: Nathan Rutman <nathan.rutman@sun.com>
37  */
38
39 #ifndef __LIBCFS_UTIL_STRING_H__
40 #define __LIBCFS_UTIL_STRING_H__
41
42 #include <stddef.h>
43 #include <stdarg.h>
44
45 #include <linux/types.h>
46 #include <libcfs/util/list.h>
47
48 static inline
49 int vscnprintf(char *buf, size_t bufsz, const char *format, va_list args)
50 {
51         int ret;
52
53         if (!bufsz)
54                 return 0;
55
56         ret = vsnprintf(buf, bufsz, format, args);
57         return (bufsz > ret) ? ret : bufsz - 1;
58 }
59
60 /* __printf from linux kernel */
61 #ifndef __printf
62 #define __printf(a, b)          __attribute__((__format__(printf, a, b)))
63 #endif
64
65 __printf(3, 4)
66 static inline int scnprintf(char *buf, size_t bufsz, const char *format, ...)
67 {
68         int ret;
69         va_list args;
70
71         va_start(args, format);
72         ret = vscnprintf(buf, bufsz, format, args);
73         va_end(args);
74
75         return ret;
76 }
77
78
79 /**
80  * Structure to represent NULL-less strings.
81  */
82 struct cfs_lstr {
83         char            *ls_str;
84         int             ls_len;
85 };
86
87 /*
88  * Structure to represent \<range_expr\> token of the syntax.
89  */
90 struct cfs_range_expr {
91         /*
92          * Link to cfs_expr_list::el_exprs.
93          */
94         struct list_head        re_link;
95         __u32                   re_lo;
96         __u32                   re_hi;
97         __u32                   re_stride;
98 };
99
100 struct cfs_expr_list {
101         struct list_head        el_link;
102         struct list_head        el_exprs;
103 };
104
105 int cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp);
106 int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
107 int cfs_str2num_check(char *str, int nob, unsigned *num,
108                       unsigned min, unsigned max);
109 int cfs_expr2str(struct list_head *list, char *str, size_t size);
110 int cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list);
111 int cfs_expr_list_print(char *buffer, int count,
112                         struct cfs_expr_list *expr_list);
113 int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
114                         struct cfs_expr_list **elpp);
115 void cfs_expr_list_free(struct cfs_expr_list *expr_list);
116 void cfs_expr_list_free_list(struct list_head *list);
117 int cfs_ip_addr_parse(char *str, int len, struct list_head *list);
118 int cfs_ip_addr_range_gen(__u32 *ip_list, int count,
119                           struct list_head *ip_addr_expr);
120 int cfs_ip_addr_match(__u32 addr, struct list_head *list);
121 int cfs_abs_path(const char *request_path, char **resolved_path);
122
123 #endif