comparison contrib/chg/util.c @ 28788:57a78a64de44

chg: add util function abortmsgerrno to print error with errno It's common to abortmsg with the errno information. Let's make a utility function for it.
author Jun Wu <quark@fb.com>
date Tue, 05 Apr 2016 17:25:39 +0100
parents ea86cdcd9b50
children ddef14468952
comparison
equal deleted inserted replaced
28787:ea86cdcd9b50 28788:57a78a64de44
5 * 5 *
6 * This software may be used and distributed according to the terms of the 6 * This software may be used and distributed according to the terms of the
7 * GNU General Public License version 2 or any later version. 7 * GNU General Public License version 2 or any later version.
8 */ 8 */
9 9
10 #include <errno.h>
10 #include <signal.h> 11 #include <signal.h>
11 #include <stdarg.h> 12 #include <stdarg.h>
12 #include <stdio.h> 13 #include <stdio.h>
13 #include <stdlib.h> 14 #include <stdlib.h>
14 #include <string.h> 15 #include <string.h>
25 if (!colorenabled) 26 if (!colorenabled)
26 return; 27 return;
27 fprintf(fp, "\033[%sm", code); 28 fprintf(fp, "\033[%sm", code);
28 } 29 }
29 30
31 static void vabortmsgerrno(int no, const char *fmt, va_list args)
32 {
33 fsetcolor(stderr, "1;31");
34 fputs("chg: abort: ", stderr);
35 vfprintf(stderr, fmt, args);
36 if (no != 0)
37 fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
38 fsetcolor(stderr, "");
39 fputc('\n', stderr);
40 exit(255);
41 }
42
30 void abortmsg(const char *fmt, ...) 43 void abortmsg(const char *fmt, ...)
31 { 44 {
32 va_list args; 45 va_list args;
33 va_start(args, fmt); 46 va_start(args, fmt);
34 fsetcolor(stderr, "1;31"); 47 vabortmsgerrno(0, fmt, args);
35 fputs("chg: abort: ", stderr);
36 vfprintf(stderr, fmt, args);
37 fsetcolor(stderr, "");
38 fputc('\n', stderr);
39 va_end(args); 48 va_end(args);
49 }
40 50
41 exit(255); 51 void abortmsgerrno(const char *fmt, ...)
52 {
53 int no = errno;
54 va_list args;
55 va_start(args, fmt);
56 vabortmsgerrno(no, fmt, args);
57 va_end(args);
42 } 58 }
43 59
44 static int debugmsgenabled = 0; 60 static int debugmsgenabled = 0;
45 61
46 void enablecolor(void) 62 void enablecolor(void)