Motr  M0
string.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #pragma once
24 
25 #ifndef __MOTR_LIB_STRING_H__
26 #define __MOTR_LIB_STRING_H__
27 
28 /*
29  * Define standard string manipulation functions (strcat, strlen, strcmp, &c.)
30  * together with sprintf(3) and snprintf(3).
31  * Also pick up support for strtoul(3) and variants, and ctype macros.
32  */
33 
34 #define m0_streq(a, b) (strcmp((a), (b)) == 0)
35 #define m0_strcaseeq(a, b) (strcasecmp((a), (b)) == 0)
36 
37 #ifndef __KERNEL__
38 # include <ctype.h>
39 # include <stdio.h>
40 # include <stdlib.h>
41 # include <string.h>
42 
43 #define m0_strdup(s) strdup((s))
44 #define m0_asprintf(s, fmt, ...) \
45  ({ \
46  int __nr; \
47  char **__s = (s); \
48  __nr = asprintf(__s, (fmt), __VA_ARGS__); \
49  if (__nr <= 0) \
50  *__s = NULL; \
51  })
52 
53 #else
54 # include <linux/ctype.h>
55 # include <linux/kernel.h>
56 # include <linux/string.h>
57 
58 #define m0_strdup(s) kstrdup((s), GFP_KERNEL)
59 #define m0_asprintf(s, fmt, ...) \
60  ({ *(s) = kasprintf(GFP_ATOMIC, (fmt), __VA_ARGS__); })
61 
62 static inline char *strerror(int errnum)
63 {
64  return "strerror() is not supported in kernel";
65 }
66 #endif /* __KERNEL__ */
67 
68 #include "lib/types.h"
69 
70 struct m0_fop_str {
71  uint32_t s_len;
72  uint8_t *s_buf;
73 } M0_XCA_SEQUENCE M0_XCA_DOMAIN(rpc);
74 
80 const char *m0_bcount_with_suffix(char *buf, size_t size, m0_bcount_t c);
81 
82 M0_INTERNAL void m0_strings_free(const char **arr);
83 
84 M0_INTERNAL const char **m0_strings_dup(const char **src);
85 
86 M0_INTERNAL char *
87 m0_vsnprintf(char *buf, size_t buflen, const char *format, ...)
88  __attribute__((format (printf, 3, 4)));
89 
91 M0_INTERNAL bool m0_startswith(const char *prefix, const char *str);
92 
93 #endif /* __MOTR_LIB_STRING_H__ */
94 
95 /*
96  * Local variables:
97  * c-indentation-style: "K&R"
98  * c-basic-offset: 8
99  * tab-width: 8
100  * fill-column: 80
101  * scroll-step: 1
102  * End:
103  */
const char * m0_bcount_with_suffix(char *buf, size_t size, m0_bcount_t c)
Definition: string.c:32
static struct m0_uint128 prefix
Definition: extmap.c:45
enum m0_md_lustre_logrec_type __attribute__
Definition: balloc.c:2745
uint64_t m0_bcount_t
Definition: types.h:77
Definition: sock.c:887
uint8_t * s_buf
Definition: string.h:72
static struct m0_addb2_callback c
Definition: consumer.c:41
format
Definition: hist.py:128
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
m0_bcount_t size
Definition: di.c:39
M0_INTERNAL char M0_INTERNAL bool m0_startswith(const char *prefix, const char *str)
Definition: string.c:98
struct m0_fop_str M0_XCA_DOMAIN(rpc)
uint32_t s_len
Definition: string.h:71
M0_INTERNAL const char ** m0_strings_dup(const char **src)
Definition: string.c:55
M0_INTERNAL char * m0_vsnprintf(char *buf, size_t buflen, const char *format,...) __attribute__((format(printf
struct m0_pdclust_src_addr src
Definition: fd.c:108