infobar

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

util.c (875B)


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