Motr  M0
string.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
24 #include "lib/trace.h"
25 
26 #include "lib/types.h" /* m0_bcount_t */
27 #include "lib/memory.h" /* M0_ALLOC_ARR */
28 #include "lib/misc.h" /* ARRAY_SIZE */
29 #include "lib/finject.h" /* M0_FI_ENABLED */
30 #include "lib/string.h"
31 
32 const char *m0_bcount_with_suffix(char *buf, size_t size, m0_bcount_t c)
33 {
34  static const char *suffix[] = {
35  "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
36  };
37  int i;
38 
39  for (i = 0; i < ARRAY_SIZE(suffix) - 1 && c >= 1024; ++i, c /= 1024)
40  ;
41  snprintf(buf, size, "%3" PRId64 " %s", c, suffix[i]);
42  return buf;
43 }
44 
45 M0_INTERNAL void m0_strings_free(const char **arr)
46 {
47  if (arr != NULL) {
48  const char **p;
49  for (p = arr; *p != NULL; ++p)
50  m0_free((void *)*p);
51  m0_free0(&arr);
52  }
53 }
54 
55 M0_INTERNAL const char **m0_strings_dup(const char **src)
56 {
57  int i;
58  int n;
59  char **dest;
60 
61  for (n = 0; src[n] != NULL; ++n)
62  ; /* counting */
63 
64  M0_ALLOC_ARR(dest, n + 1);
65  if (dest == NULL)
66  return NULL;
67 
68  for (i = 0; i < n; ++i) {
69  if (!M0_FI_ENABLED("strdup_failed")) {
70  dest[i] = m0_strdup(src[i]);
71  }
72 
73  if (dest[i] == NULL) {
74  m0_strings_free((const char **)dest);
75  return NULL;
76  }
77  }
78  dest[n] = NULL; /* end of list */
79 
80  return (const char **)dest;
81 }
82 
83 M0_INTERNAL char *
84 m0_vsnprintf(char *buf, size_t buflen, const char *format, ...)
85 {
86  va_list ap;
87  int n;
88 
89  va_start(ap, format);
90  n = vsnprintf(buf, buflen, format, ap);
91  va_end(ap);
92  M0_ASSERT(n > 0);
93  if ((size_t)n >= buflen)
94  M0_LOG(M0_WARN, "Output was truncated");
95  return buf;
96 }
97 
98 M0_INTERNAL bool m0_startswith(const char *prefix, const char *str)
99 {
100  size_t len_pre = strlen(prefix);
101 
102  return len_pre <= strlen(str) && strncmp(prefix, str, len_pre) == 0;
103 }
104 
105 #undef M0_TRACE_SUBSYSTEM
106 
107 /*
108  * Local variables:
109  * c-indentation-style: "K&R"
110  * c-basic-offset: 8
111  * tab-width: 8
112  * fill-column: 80
113  * scroll-step: 1
114  * End:
115  */
116 /*
117  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
118  */
static struct m0_addb2_philter p
Definition: consumer.c:40
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
M0_INTERNAL const char ** m0_strings_dup(const char **src)
Definition: string.c:55
#define m0_strdup(s)
Definition: string.h:43
#define NULL
Definition: misc.h:38
#define M0_LOG(level,...)
Definition: trace.h:167
static struct m0_uint128 prefix
Definition: extmap.c:45
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL bool m0_startswith(const char *prefix, const char *str)
Definition: string.c:98
static int void * buf
Definition: dir.c:1019
Definition: sock.c:887
int i
Definition: dir.c:1033
const char * m0_bcount_with_suffix(char *buf, size_t size, m0_bcount_t c)
Definition: string.c:32
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
static struct m0_addb2_callback c
Definition: consumer.c:41
#define PRId64
Definition: types.h:57
format
Definition: hist.py:128
uint64_t n
Definition: fops.h:107
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
m0_bcount_t size
Definition: di.c:39
void m0_free(void *data)
Definition: memory.c:146
struct m0_pdclust_src_addr src
Definition: fd.c:108
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: trace.h:478
M0_INTERNAL char * m0_vsnprintf(char *buf, size_t buflen, const char *format,...)
Definition: string.c:84