21 #include <sys/types.h>
27 snprintf(buffer, len,
"%d", an_int);
36 long long result = -1;
37 char *local_end_text = NULL;
44 if (end_text != NULL) {
45 result = strtol(text, end_text, 10);
47 result = strtol(text, &local_end_text, 10);
50 if (end_text != NULL) {
51 result = strtoll(text, end_text, 10);
53 result = strtoll(text, &local_end_text, 10);
58 if (errno == EINVAL) {
59 crm_err(
"Conversion of %s failed", text);
62 }
else if (errno == ERANGE) {
63 crm_err(
"Conversion of %s was clipped: %lld", text, result);
65 }
else if (errno != 0) {
66 crm_perror(LOG_ERR,
"Conversion of %s failed", text);
69 if (local_end_text != NULL && local_end_text[0] !=
'\0') {
70 crm_err(
"Characters left over after parsing '%s': '%s'", text, local_end_text);
92 crm_err(
"No default conversion value supplied");
114 if (result < INT_MIN) {
116 if (errno != ERANGE) {
117 crm_err(
"Conversion of %s was clipped: %lld", text, result);
122 }
else if (result > INT_MAX) {
124 if (errno != ERANGE) {
125 crm_err(
"Conversion of %s was clipped: %lld", text, result);
148 if ((ms < 0) || (ms > G_MAXUINT)) {
151 return errno? 0 : (guint) ms;
162 }
else if (a == NULL || b == NULL) {
165 }
else if (strcasecmp(a, b) == 0) {
174 gboolean ret = FALSE;
188 }
else if (strcasecmp(s,
"true") == 0
189 || strcasecmp(s,
"on") == 0
190 || strcasecmp(s,
"yes") == 0 || strcasecmp(s,
"y") == 0 || strcasecmp(s,
"1") == 0) {
194 }
else if (strcasecmp(s,
"false") == 0
195 || strcasecmp(s,
"off") == 0
196 || strcasecmp(s,
"no") == 0 || strcasecmp(s,
"n") == 0 || strcasecmp(s,
"0") == 0) {
212 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
223 return g_strcmp0(a, b) == 0;
229 }
else if (a == NULL || b == NULL) {
233 }
else if (strcasecmp(a, b) == 0) {
239 static inline const char * null2emptystr(
const char *);
240 static inline const char *
241 null2emptystr(
const char *input)
243 return (input == NULL) ?
"" : input;
262 const char *p = prefix;
275 static inline int crm_ends_with_internal(
const char *,
const char *, gboolean);
277 crm_ends_with_internal(
const char *s,
const char *match, gboolean as_extension)
279 if ((s == NULL) || (match == NULL)) {
287 return !strcmp(null2emptystr(strrchr(s, match[0])), match);
289 if ((mlen = strlen(match)) == 0)
292 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
311 return crm_ends_with_internal(s, match, FALSE);
340 return crm_ends_with_internal(s, match, TRUE);
359 const signed char *p;
362 for (p = v; *p !=
'\0'; p++)
363 h = (h << 5) - h + *p;
372 return crm_str_eq((
const char *) a, (
const char *) b, FALSE);
378 const signed char *p;
381 for (p = v; *p !=
'\0'; p++)
382 h = (h << 5) - h + g_ascii_tolower(*p);
388 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
390 if (key && value && user_data) {
391 g_hash_table_insert((GHashTable*)user_data, strdup(key), strdup(value));
398 GHashTable *new_table = NULL;
401 new_table = crm_str_table_new();
402 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
420 len += strlen(value);
421 list = realloc_safe(list, len);
422 sprintf(list + last,
" %s", value);
430 char *compressed = NULL;
431 char *uncompressed = strdup(data);
432 #ifdef CLOCK_MONOTONIC
433 struct timespec after_t;
434 struct timespec before_t;
438 max = (length * 1.1) + 600;
441 #ifdef CLOCK_MONOTONIC
442 clock_gettime(CLOCK_MONOTONIC, &before_t);
445 compressed = calloc(max,
sizeof(
char));
449 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
CRM_BZ2_BLOCKS, 0,
455 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " bzerror=%d",
461 #ifdef CLOCK_MONOTONIC
462 clock_gettime(CLOCK_MONOTONIC, &after_t);
464 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
465 length, *result_len, length / (*result_len),
466 difftime (after_t.tv_sec, before_t.tv_sec) * 1000 +
467 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
469 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
470 length, *result_len, length / (*result_len));
473 *result = compressed;
498 return strcasecmp(a, b);
508 va_start(ap, format);
509 len = vasprintf (&
string, format, ap);
const char * bz2_strerror(int rc)
gboolean safe_str_neq(const char *a, const char *b)
guint g_str_hash_traditional(gconstpointer v)
long long crm_int_helper(const char *text, char **end_text)
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
char * add_list_element(char *list, const char *value)
gint crm_alpha_sort(gconstpointer a, gconstpointer b)
Compare two strings alphabetically (case-insensitive)
#define crm_trace(fmt, args...)
guint crm_strcase_hash(gconstpointer v)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
char * crm_itoa_stack(int an_int, char *buf, size_t len)
gboolean crm_ends_with_ext(const char *s, const char *match)
gboolean crm_ends_with(const char *s, const char *match)
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
int crm_str_to_boolean(const char *s, int *ret)
guint crm_parse_ms(const char *text)
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
#define crm_perror(level, fmt, args...)
Log a system error message.
#define crm_err(fmt, args...)
GHashTable * crm_str_table_dup(GHashTable *old_table)
gboolean crm_is_true(const char *s)
char * crm_strip_trailing_newline(char *str)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)