util.c (874B)
1 /* 2 * Copyright (C) Raymond Cole <rc@wolog.xyz> 3 * License: GPL-3.0-or-later 4 * 5 * Portions of the code are derived from suckless dwm. See file 6 * LICENSE-dwm for copyright and license details of those portions. 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 }