util.c (1046B)
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 * Portions of the code originally belong to dwm as developed by Anselm 9 * R. Garbe et al. See LICENSE-dwm for the copyright and license details 10 * of those portions. 11 */ 12 #include <errno.h> 13 #include <stdarg.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include "util.h" 19 20 const char *progname; 21 22 void 23 vwarn(const char *fmt, va_list ap) 24 { 25 int e = 0; 26 char c = '\0'; 27 28 if (fmt[0]) { 29 c = fmt[strlen(fmt) - 1]; 30 if (c == ':') 31 e = errno; 32 } 33 if (progname) 34 fprintf(stderr, "%s: ", progname); 35 vfprintf(stderr, fmt, ap); 36 if (c == ':') 37 fprintf(stderr, " %s", strerror(e)); 38 fputc('\n', stderr); 39 } 40 41 void 42 warn(const char *fmt, ...) 43 { 44 va_list ap; 45 46 va_start(ap, fmt); 47 vwarn(fmt, ap); 48 va_end(ap); 49 } 50 51 void 52 die(const char *fmt, ...) 53 { 54 va_list ap; 55 56 va_start(ap, fmt); 57 vwarn(fmt, ap); 58 va_end(ap); 59 exit(1); 60 }