pacemaker  1.1.16-94ff4df
Scalable High-Availability cluster resource manager
strings.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <crm_internal.h>
20 
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE
23 #endif
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <bzlib.h>
29 #include <sys/types.h>
30 
31 char *
32 crm_concat(const char *prefix, const char *suffix, char join)
33 {
34  int len = 0;
35  char *new_str = NULL;
36 
37  CRM_ASSERT(prefix != NULL);
38  CRM_ASSERT(suffix != NULL);
39  len = strlen(prefix) + strlen(suffix) + 2;
40 
41  new_str = malloc(len);
42  if(new_str) {
43  sprintf(new_str, "%s%c%s", prefix, join, suffix);
44  new_str[len - 1] = 0;
45  }
46  return new_str;
47 }
48 
49 char *
50 crm_itoa_stack(int an_int, char *buffer, size_t len)
51 {
52  if (buffer != NULL) {
53  snprintf(buffer, len, "%d", an_int);
54  }
55 
56  return buffer;
57 }
58 
59 char *
60 crm_itoa(int an_int)
61 {
62  int len = 32;
63  char *buffer = NULL;
64 
65  buffer = malloc(len + 1);
66  if (buffer != NULL) {
67  snprintf(buffer, len, "%d", an_int);
68  }
69 
70  return buffer;
71 }
72 
73 void
75 {
76  free(data);
77 }
78 
79 long long
80 crm_int_helper(const char *text, char **end_text)
81 {
82  long long result = -1;
83  char *local_end_text = NULL;
84  int saved_errno = 0;
85 
86  errno = 0;
87 
88  if (text != NULL) {
89 #ifdef ANSI_ONLY
90  if (end_text != NULL) {
91  result = strtol(text, end_text, 10);
92  } else {
93  result = strtol(text, &local_end_text, 10);
94  }
95 #else
96  if (end_text != NULL) {
97  result = strtoll(text, end_text, 10);
98  } else {
99  result = strtoll(text, &local_end_text, 10);
100  }
101 #endif
102 
103  saved_errno = errno;
104  if (errno == EINVAL) {
105  crm_err("Conversion of %s failed", text);
106  result = -1;
107 
108  } else if (errno == ERANGE) {
109  crm_err("Conversion of %s was clipped: %lld", text, result);
110 
111  } else if (errno != 0) {
112  crm_perror(LOG_ERR, "Conversion of %s failed", text);
113  }
114 
115  if (local_end_text != NULL && local_end_text[0] != '\0') {
116  crm_err("Characters left over after parsing '%s': '%s'", text, local_end_text);
117  }
118 
119  errno = saved_errno;
120  }
121  return result;
122 }
123 
124 int
125 crm_parse_int(const char *text, const char *default_text)
126 {
127  int atoi_result = -1;
128 
129  if (text != NULL) {
130  atoi_result = crm_int_helper(text, NULL);
131  if (errno == 0) {
132  return atoi_result;
133  }
134  }
135 
136  if (default_text != NULL) {
137  atoi_result = crm_int_helper(default_text, NULL);
138  if (errno == 0) {
139  return atoi_result;
140  }
141 
142  } else {
143  crm_err("No default conversion value supplied");
144  }
145 
146  return -1;
147 }
148 
149 gboolean
150 safe_str_neq(const char *a, const char *b)
151 {
152  if (a == b) {
153  return FALSE;
154 
155  } else if (a == NULL || b == NULL) {
156  return TRUE;
157 
158  } else if (strcasecmp(a, b) == 0) {
159  return FALSE;
160  }
161  return TRUE;
162 }
163 
164 gboolean
165 crm_is_true(const char *s)
166 {
167  gboolean ret = FALSE;
168 
169  if (s != NULL) {
170  crm_str_to_boolean(s, &ret);
171  }
172  return ret;
173 }
174 
175 int
176 crm_str_to_boolean(const char *s, int *ret)
177 {
178  if (s == NULL) {
179  return -1;
180 
181  } else if (strcasecmp(s, "true") == 0
182  || strcasecmp(s, "on") == 0
183  || strcasecmp(s, "yes") == 0 || strcasecmp(s, "y") == 0 || strcasecmp(s, "1") == 0) {
184  *ret = TRUE;
185  return 1;
186 
187  } else if (strcasecmp(s, "false") == 0
188  || strcasecmp(s, "off") == 0
189  || strcasecmp(s, "no") == 0 || strcasecmp(s, "n") == 0 || strcasecmp(s, "0") == 0) {
190  *ret = FALSE;
191  return 1;
192  }
193  return -1;
194 }
195 
196 char *
198 {
199  int len;
200 
201  if (str == NULL) {
202  return str;
203  }
204 
205  for (len = strlen(str) - 1; len >= 0 && str[len] == '\n'; len--) {
206  str[len] = '\0';
207  }
208 
209  return str;
210 }
211 
212 gboolean
213 crm_str_eq(const char *a, const char *b, gboolean use_case)
214 {
215  if (use_case) {
216  return g_strcmp0(a, b) == 0;
217 
218  /* TODO - Figure out which calls, if any, really need to be case independent */
219  } else if (a == b) {
220  return TRUE;
221 
222  } else if (a == NULL || b == NULL) {
223  /* shouldn't be comparing NULLs */
224  return FALSE;
225 
226  } else if (strcasecmp(a, b) == 0) {
227  return TRUE;
228  }
229  return FALSE;
230 }
231 
241 gboolean
242 crm_ends_with(const char *s, const char *match)
243 {
244  if ((s == NULL) || (match == NULL)) {
245  return FALSE;
246  } else {
247  size_t slen = strlen(s);
248  size_t mlen = strlen(match);
249 
250  return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
251  }
252 }
253 
254 /*
255  * This re-implements g_str_hash as it was prior to glib2-2.28:
256  *
257  * http://git.gnome.org/browse/glib/commit/?id=354d655ba8a54b754cb5a3efb42767327775696c
258  *
259  * Note that the new g_str_hash is presumably a *better* hash (it's actually
260  * a correct implementation of DJB's hash), but we need to preserve existing
261  * behaviour, because the hash key ultimately determines the "sort" order
262  * when iterating through GHashTables, which affects allocation of scores to
263  * clone instances when iterating through rsc->allowed_nodes. It (somehow)
264  * also appears to have some minor impact on the ordering of a few
265  * pseudo_event IDs in the transition graph.
266  */
267 guint
268 g_str_hash_traditional(gconstpointer v)
269 {
270  const signed char *p;
271  guint32 h = 0;
272 
273  for (p = v; *p != '\0'; p++)
274  h = (h << 5) - h + *p;
275 
276  return h;
277 }
278 
279 guint
280 crm_strcase_hash(gconstpointer v)
281 {
282  const signed char *p;
283  guint32 h = 0;
284 
285  for (p = v; *p != '\0'; p++)
286  h = (h << 5) - h + g_ascii_tolower(*p);
287 
288  return h;
289 }
290 
291 char *
292 add_list_element(char *list, const char *value)
293 {
294  int len = 0;
295  int last = 0;
296 
297  if (value == NULL) {
298  return list;
299  }
300  if (list) {
301  last = strlen(list);
302  }
303  len = last + 2; /* +1 space, +1 EOS */
304  len += strlen(value);
305  list = realloc_safe(list, len);
306  sprintf(list + last, " %s", value);
307  return list;
308 }
309 
310 bool
311 crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
312 {
313  int rc;
314  char *compressed = NULL;
315  char *uncompressed = strdup(data);
316  struct timespec after_t;
317  struct timespec before_t;
318 
319  if(max == 0) {
320  max = (length * 1.1) + 600; /* recomended size */
321  }
322 
323 #ifdef CLOCK_MONOTONIC
324  clock_gettime(CLOCK_MONOTONIC, &before_t);
325 #endif
326 
327  /* coverity[returned_null] Ignore */
328  compressed = malloc(max);
329 
330  *result_len = max;
331  rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length, CRM_BZ2_BLOCKS, 0,
332  CRM_BZ2_WORK);
333 
334  free(uncompressed);
335 
336  if (rc != BZ_OK) {
337  crm_err("Compression of %d bytes failed: %s (%d)", length, bz2_strerror(rc), rc);
338  free(compressed);
339  return FALSE;
340  }
341 
342 #ifdef CLOCK_MONOTONIC
343  clock_gettime(CLOCK_MONOTONIC, &after_t);
344 
345  crm_info("Compressed %d bytes into %d (ratio %d:1) in %dms",
346  length, *result_len, length / (*result_len),
347  (after_t.tv_sec - before_t.tv_sec) * 1000 + (after_t.tv_nsec -
348  before_t.tv_nsec) / 1000000);
349 #else
350  crm_info("Compressed %d bytes into %d (ratio %d:1)",
351  length, *result_len, length / (*result_len));
352 #endif
353 
354  *result = compressed;
355  return TRUE;
356 }
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
Definition: strings.c:311
long long crm_int_helper(const char *text, char **end_text)
Definition: strings.c:80
gboolean crm_ends_with(const char *s, const char *match)
Definition: strings.c:242
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:268
#define CRM_BZ2_WORK
Definition: xml.h:49
char * add_list_element(char *list, const char *value)
Definition: strings.c:292
void g_hash_destroy_str(gpointer data)
Definition: strings.c:74
char * crm_itoa_stack(int an_int, char *buffer, size_t len)
Definition: strings.c:50
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:176
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:197
gboolean crm_is_true(const char *s)
Definition: strings.c:165
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:213
int crm_parse_int(const char *text, const char *default_text)
Definition: strings.c:125
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:226
#define crm_err(fmt, args...)
Definition: logging.h:248
const char * bz2_strerror(int rc)
Definition: logging.c:1191
char * crm_itoa(int an_int)
Definition: strings.c:60
#define CRM_ASSERT(expr)
Definition: error.h:35
char data[0]
Definition: internal.h:58
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:150
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:280
char * crm_concat(const char *prefix, const char *suffix, char join)
Definition: strings.c:32
#define CRM_BZ2_BLOCKS
Definition: xml.h:48
#define crm_info(fmt, args...)
Definition: logging.h:251