1 #ifndef _ERROR_H_ 2 #define _ERROR_H_ 3 4 #include <stdarg.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <errno.h> 9 10 #warning usage of non-standard #include <error.h> is deprecated 11 12 static unsigned int error_message_count = 0; 13 14 static inline void error(int status, int errnum, const char* format, ...) 15 { 16 /* should be fflush(stdout), but that's unspecified if stdout has been closed; 17 * stick with fflush(NULL) for simplicity (glibc checks if the fd is still valid) */ 18 fflush(NULL); 19 20 va_list ap; 21 fprintf(stderr, "%s: ", program_invocation_name); 22 va_start(ap, format); 23 vfprintf(stderr, format, ap); 24 va_end(ap); 25 if (errnum) 26 fprintf(stderr, ": %s", strerror(errnum)); 27 fprintf(stderr, "\n"); 28 error_message_count++; 29 if (status) 30 exit(status); 31 } 32 33 static int error_one_per_line = 0; 34 35 static inline void error_at_line(int status, int errnum, const char *filename, 36 unsigned int linenum, const char *format, ...) 37 { 38 va_list ap; 39 if (error_one_per_line) { 40 static const char *old_filename; 41 static int old_linenum; 42 if (linenum == old_linenum && filename == old_filename) 43 return; 44 old_filename = filename; 45 old_linenum = linenum; 46 } 47 fprintf(stderr, "%s: %s:%u: ", program_invocation_name, filename, linenum); 48 va_start(ap, format); 49 vfprintf(stderr, format, ap); 50 va_end(ap); 51 if (errnum) 52 fprintf(stderr, ": %s", strerror(errnum)); 53 fprintf(stderr, "\n"); 54 error_message_count++; 55 if (status) 56 exit(status); 57 } 58 59 60 #endif /* _ERROR_H_ */ 61