21 #include <sys/types.h>
27 snprintf(buffer, len,
"%d", an_int);
44 scan_ll(
const char *text,
long long *result,
char **end_text)
46 long long local_result = -1;
47 char *local_end_text = NULL;
53 local_result = (
long long) strtol(text, &local_end_text, 10);
55 local_result = strtoll(text, &local_end_text, 10);
57 if (errno == ERANGE) {
59 crm_warn(
"Integer parsed from %s was clipped to %lld",
62 }
else if (errno != 0) {
65 crm_err(
"Could not parse integer from %s (using -1 instead): %s",
68 }
else if (local_end_text == text) {
71 crm_err(
"Could not parse integer from %s (using -1 instead): "
72 "No digits found", text);
75 if ((end_text == NULL) && (local_end_text != NULL)
76 && (local_end_text[0] !=
'\0')) {
77 crm_warn(
"Characters left over after parsing '%s': '%s'",
78 text, local_end_text);
82 if (end_text != NULL) {
83 *end_text = local_end_text;
86 *result = local_result;
107 crm_err(
"No default conversion value supplied");
112 scan_ll(text, &result, NULL);
130 if (result < INT_MIN) {
132 if (errno != ERANGE) {
133 crm_err(
"Conversion of %s was clipped: %lld", text, result);
138 }
else if (result > INT_MAX) {
140 if (errno != ERANGE) {
141 crm_err(
"Conversion of %s was clipped: %lld", text, result);
168 CRM_CHECK((table != NULL) && (key != NULL),
return EINVAL);
170 value = g_hash_table_lookup(table, key);
172 if (result != NULL) {
173 *result = default_val;
183 if ((value_ll < 0) || (value_ll > G_MAXUINT)) {
184 crm_warn(
"Could not parse non-negative integer from %s", value);
188 if (result != NULL) {
189 *result = (guint) value_ll;
195 # define NUMCHARS "0123456789."
199 # define WHITESPACE " \t\n\r\f"
213 const char *num_start = NULL;
215 long long multiplier = 1000;
216 long long divisor = 1;
219 char *end_text = NULL;
225 num_start = input + strspn(input,
WHITESPACE);
226 num_len = strspn(num_start,
NUMCHARS);
230 units = num_start + num_len;
233 if (!strncasecmp(units,
"ms", 2) || !strncasecmp(units,
"msec", 4)) {
236 }
else if (!strncasecmp(units,
"us", 2) || !strncasecmp(units,
"usec", 4)) {
239 }
else if (!strncasecmp(units,
"s", 1) || !strncasecmp(units,
"sec", 3)) {
242 }
else if (!strncasecmp(units,
"m", 1) || !strncasecmp(units,
"min", 3)) {
243 multiplier = 60 * 1000;
245 }
else if (!strncasecmp(units,
"h", 1) || !strncasecmp(units,
"hr", 2)) {
246 multiplier = 60 * 60 * 1000;
248 }
else if ((*units !=
EOS) && (*units !=
'\n') && (*units !=
'\r')) {
252 scan_ll(num_start, &msec, &end_text);
253 if (msec > (LLONG_MAX / multiplier)) {
268 }
else if (a == NULL || b == NULL) {
271 }
else if (strcasecmp(a, b) == 0) {
280 gboolean ret = FALSE;
294 }
else if (strcasecmp(s,
"true") == 0
295 || strcasecmp(s,
"on") == 0
296 || strcasecmp(s,
"yes") == 0 || strcasecmp(s,
"y") == 0 || strcasecmp(s,
"1") == 0) {
300 }
else if (strcasecmp(s,
"false") == 0
301 || strcasecmp(s,
"off") == 0
302 || strcasecmp(s,
"no") == 0 || strcasecmp(s,
"n") == 0 || strcasecmp(s,
"0") == 0) {
318 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
329 return g_strcmp0(a, b) == 0;
335 }
else if (a == NULL || b == NULL) {
339 }
else if (strcasecmp(a, b) == 0) {
361 const char *p = prefix;
375 ends_with(
const char *s,
const char *match,
bool as_extension)
377 if (pcmk__str_empty(match)) {
379 }
else if (s == NULL) {
388 s = strrchr(s, match[0]);
389 return (s == NULL)? false : !strcmp(s, match);
392 mlen = strlen(match);
394 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
412 return ends_with(s, match,
false);
439 return ends_with(s, match,
true);
458 const signed char *p;
461 for (p = v; *p !=
'\0'; p++)
462 h = (h << 5) - h + *p;
471 return crm_str_eq((
const char *) a, (
const char *) b, FALSE);
477 const signed char *p;
480 for (p = v; *p !=
'\0'; p++)
481 h = (h << 5) - h + g_ascii_tolower(*p);
487 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
489 if (key && value && user_data) {
490 g_hash_table_insert((GHashTable*)user_data, strdup(key), strdup(value));
497 GHashTable *new_table = NULL;
500 new_table = crm_str_table_new();
501 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
520 size_t len = list? strlen(list) : 0;
522 list = realloc_safe(list, len + strlen(word) + 2);
523 sprintf(list + len,
" %s", word);
542 char **result,
unsigned int *result_len)
545 char *compressed = NULL;
546 char *uncompressed = strdup(
data);
547 #ifdef CLOCK_MONOTONIC
548 struct timespec after_t;
549 struct timespec before_t;
553 max = (length * 1.01) + 601;
556 #ifdef CLOCK_MONOTONIC
557 clock_gettime(CLOCK_MONOTONIC, &before_t);
560 compressed = calloc((
size_t) max,
sizeof(
char));
564 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
568 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " bzerror=%d",
574 #ifdef CLOCK_MONOTONIC
575 clock_gettime(CLOCK_MONOTONIC, &after_t);
577 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
578 length, *result_len, length / (*result_len),
579 (after_t.tv_sec - before_t.tv_sec) * 1000 +
580 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
582 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
583 length, *result_len, length / (*result_len));
586 *result = compressed;
597 va_start(ap, format);
598 len = vasprintf (&
string, format, ap);
607 char *remainder = NULL;
614 crm_trace(
"Attempting to decode: [%s]", srcstring);
615 if (srcstring == NULL || strcmp(srcstring,
"") == 0 || strcmp(srcstring,
"-") == 0) {
622 if (*srcstring ==
'-') {
623 int rc = scan_ll(srcstring+1, end, &remainder);
632 if (scan_ll(srcstring, start, &remainder) !=
pcmk_rc_ok) {
636 if (*remainder && *remainder ==
'-') {
637 if (*(remainder+1)) {
638 char *more_remainder = NULL;
639 int rc = scan_ll(remainder+1, end, &more_remainder);
645 }
else if (*remainder && *remainder !=
'-') {
666 if (strcmp(lst->data,
"*") == 0 && lst->next == NULL) {
670 return g_list_find_custom(lst, s, (GCompareFunc) strcmp) != NULL;