summaryrefslogtreecommitdiffstats
path: root/plugins/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/utils.c')
-rw-r--r--plugins/utils.c67
1 files changed, 47 insertions, 20 deletions
diff --git a/plugins/utils.c b/plugins/utils.c
index 4537390..8827e31 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -358,7 +358,7 @@ strscpy (char *dest, const char *src)
358 if (src == NULL) 358 if (src == NULL)
359 return NULL; 359 return NULL;
360 360
361 asprintf (&dest, "%s", src); 361 xasprintf (&dest, "%s", src);
362 362
363 return dest; 363 return dest;
364} 364}
@@ -513,6 +513,33 @@ strpcat (char *dest, const char *src, const char *str)
513 return dest; 513 return dest;
514} 514}
515 515
516
517/******************************************************************************
518 *
519 * asprintf, but die on failure
520 *
521 ******************************************************************************/
522
523int
524xvasprintf (char **strp, const char *fmt, va_list ap)
525{
526 int result = vasprintf (strp, fmt, ap);
527 if (result == -1 || *strp == NULL)
528 die (STATE_UNKNOWN, _("failed malloc in xvasprintf\n"));
529 return result;
530}
531
532int
533xasprintf (char **strp, const char *fmt, ...)
534{
535 va_list ap;
536 int result;
537 va_start (ap, fmt);
538 result = xvasprintf (strp, fmt, ap);
539 va_end (ap);
540 return result;
541}
542
516/****************************************************************************** 543/******************************************************************************
517 * 544 *
518 * Print perfdata in a standard format 545 * Print perfdata in a standard format
@@ -534,25 +561,25 @@ char *perfdata (const char *label,
534 char *data = NULL; 561 char *data = NULL;
535 562
536 if (strpbrk (label, "'= ")) 563 if (strpbrk (label, "'= "))
537 asprintf (&data, "'%s'=%ld%s;", label, val, uom); 564 xasprintf (&data, "'%s'=%ld%s;", label, val, uom);
538 else 565 else
539 asprintf (&data, "%s=%ld%s;", label, val, uom); 566 xasprintf (&data, "%s=%ld%s;", label, val, uom);
540 567
541 if (warnp) 568 if (warnp)
542 asprintf (&data, "%s%ld;", data, warn); 569 xasprintf (&data, "%s%ld;", data, warn);
543 else 570 else
544 asprintf (&data, "%s;", data); 571 xasprintf (&data, "%s;", data);
545 572
546 if (critp) 573 if (critp)
547 asprintf (&data, "%s%ld;", data, crit); 574 xasprintf (&data, "%s%ld;", data, crit);
548 else 575 else
549 asprintf (&data, "%s;", data); 576 xasprintf (&data, "%s;", data);
550 577
551 if (minp) 578 if (minp)
552 asprintf (&data, "%s%ld", data, minv); 579 xasprintf (&data, "%s%ld", data, minv);
553 580
554 if (maxp) 581 if (maxp)
555 asprintf (&data, "%s;%ld", data, maxv); 582 xasprintf (&data, "%s;%ld", data, maxv);
556 583
557 return data; 584 return data;
558} 585}
@@ -573,29 +600,29 @@ char *fperfdata (const char *label,
573 char *data = NULL; 600 char *data = NULL;
574 601
575 if (strpbrk (label, "'= ")) 602 if (strpbrk (label, "'= "))
576 asprintf (&data, "'%s'=", label); 603 xasprintf (&data, "'%s'=", label);
577 else 604 else
578 asprintf (&data, "%s=", label); 605 xasprintf (&data, "%s=", label);
579 606
580 asprintf (&data, "%s%f", data, val); 607 xasprintf (&data, "%s%f", data, val);
581 asprintf (&data, "%s%s;", data, uom); 608 xasprintf (&data, "%s%s;", data, uom);
582 609
583 if (warnp) 610 if (warnp)
584 asprintf (&data, "%s%f", data, warn); 611 xasprintf (&data, "%s%f", data, warn);
585 612
586 asprintf (&data, "%s;", data); 613 xasprintf (&data, "%s;", data);
587 614
588 if (critp) 615 if (critp)
589 asprintf (&data, "%s%f", data, crit); 616 xasprintf (&data, "%s%f", data, crit);
590 617
591 asprintf (&data, "%s;", data); 618 xasprintf (&data, "%s;", data);
592 619
593 if (minp) 620 if (minp)
594 asprintf (&data, "%s%f", data, minv); 621 xasprintf (&data, "%s%f", data, minv);
595 622
596 if (maxp) { 623 if (maxp) {
597 asprintf (&data, "%s;", data); 624 xasprintf (&data, "%s;", data);
598 asprintf (&data, "%s%f", data, maxv); 625 xasprintf (&data, "%s%f", data, maxv);
599 } 626 }
600 627
601 return data; 628 return data;