1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2012, 2017, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
13 * Generic string manipulation functions.
15 * Author: Nathan Rutman <nathan.rutman@sun.com>
18 #ifndef __LIBCFS_UTIL_STRING_H__
19 #define __LIBCFS_UTIL_STRING_H__
24 #include <linux/types.h>
25 #include <linux/lnet/lnet-types.h>
26 #include <libcfs/util/list.h>
29 int vscnprintf(char *buf, size_t bufsz, const char *format, va_list args)
36 ret = vsnprintf(buf, bufsz, format, args);
37 return (bufsz > ret) ? ret : bufsz - 1;
40 /* __printf from linux kernel */
42 #define __printf(a, b) __attribute__((__format__(printf, a, b)))
46 static inline int scnprintf(char *buf, size_t bufsz, const char *format, ...)
51 va_start(args, format);
52 ret = vscnprintf(buf, bufsz, format, args);
62 void (*nf_addr2str)(__u32 addr, char *str, size_t size);
63 void (*nf_addr2str_size)(const __be32 *addr, size_t asize, char *str,
65 int (*nf_str2addr)(const char *str, int nob, __u32 *addr);
66 int (*nf_str2addr_size)(const char *str, int nob,
67 __be32 *addr, size_t *asize);
68 int (*nf_parse_addrlist)(char *str, int len,
69 struct list_head *list);
70 int (*nf_print_addrlist)(char *buffer, int count,
71 struct list_head *list);
72 int (*nf_match_addr)(__u32 addr, struct list_head *list);
73 int (*nf_match_netmask)(const __be32 *addr, size_t asize,
74 const __be32 *netmask,
75 const __be32 *netaddr);
76 int (*nf_min_max)(struct list_head *nidlist, __u32 *min_nid,
78 int (*nf_expand_addrrange)(struct list_head *addrranges,
79 __u32 *addrs, int max_addrs);
83 * Structure to represent NULL-less strings.
91 * Structure to represent \<range_expr\> token of the syntax.
93 struct cfs_range_expr {
95 * Link to cfs_expr_list::el_exprs.
97 struct list_head re_link;
103 struct cfs_expr_list {
104 struct list_head el_link;
105 struct list_head el_exprs;
108 int cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp);
109 int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
110 int cfs_str2num_check(char *str, int nob, unsigned *num,
111 unsigned min, unsigned max);
112 int cfs_expr2str(struct list_head *list, char *str, size_t size);
113 int cfs_expr_list_print(char *buffer, int count,
114 struct cfs_expr_list *expr_list);
115 int cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
116 struct cfs_expr_list **elpp);
117 void cfs_expr_list_free(struct cfs_expr_list *expr_list);
118 void cfs_expr_list_free_list(struct list_head *list);
119 int cfs_ip_addr_parse(char *str, int len, struct list_head *list);
120 int cfs_ip_addr_range_gen(__u32 *ip_list, int count,
121 struct list_head *ip_addr_expr);
122 int cfs_ip_addr_match(__u32 addr, struct list_head *list);
123 int libcfs_ip_in_netmask(const __be32 *addr, size_t asize,
124 const __be32 *netmask,
125 const __be32 *netaddr);
126 int cfs_expand_nidlist(struct list_head *nidlist, lnet_nid_t *lnet_nidlist,
128 int cfs_parse_nid_parts(char *str, struct list_head *addr,
129 struct list_head *net_num, __u32 *net_type);
130 int cfs_abs_path(const char *request_path, char **resolved_path);
131 char *strim(char *s);