infobar

Unixy status monitor
git clone git://wolog.xyz/infobar
Log | Files | Refs | README | LICENSE

util.c (738B)


      1 /*
      2  * Copyright (C) Raymond Cole <rc@wolog.xyz>
      3  * License: GPL-3.0-or-later
      4  */
      5 #include <errno.h>
      6 #include <stdarg.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 #include "util.h"
     12 
     13 const char *progname;
     14 
     15 void
     16 vwarn(const char *fmt, va_list ap)
     17 {
     18 	int e = 0;
     19 	char c = '\0';
     20 
     21 	if (fmt[0]) {
     22 		c = fmt[strlen(fmt) - 1];
     23 		if (c == ':')
     24 			e = errno;
     25 	}
     26 	if (progname)
     27 		fprintf(stderr, "%s: ", progname);
     28 	vfprintf(stderr, fmt, ap);
     29 	if (c == ':')
     30 		fprintf(stderr, " %s", strerror(e));
     31 	fputc('\n', stderr);
     32 }
     33 
     34 void
     35 warn(const char *fmt, ...)
     36 {
     37 	va_list ap;
     38 
     39 	va_start(ap, fmt);
     40 	vwarn(fmt, ap);
     41 	va_end(ap);
     42 }
     43 
     44 void
     45 die(const char *fmt, ...)
     46 {
     47 	va_list ap;
     48 
     49 	va_start(ap, fmt);
     50 	vwarn(fmt, ap);
     51 	va_end(ap);
     52 	exit(1);
     53 }