1 /* Part of publib.
2 
3    Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11 
12    2. Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the following
14       disclaimer in the documentation and/or other materials provided
15       with the distribution.
16 
17    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*
30  * errormsg.c -- print error message and optionally die
31  *
32  * Part of publib.  See man page for more information
33  * "@(#)publib-errormsg:$Id: errormsg.c,v 1.2 1999/03/24 22:29:13 liw Exp $"
34  */
35 
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <errno.h>
42 #include "publib/errormsg.h"
43 
44 static const char *progname = NULL;
45 
46 enum __liberror __liberror = __exit_on_error;
47 
__set_liberror(enum __liberror i)48 void __set_liberror(enum __liberror i) {
49 	__liberror = i;
50 }
51 
52 
53 /*
54  * Set program name; this is automatically included in the error messages.
55  * Both arguments should point at strings that have static duration, i.e.
56  * they exist until the program terminates (or at least until the last error
57  * message has been printed); this is so that it is not necessary to create
58  * a copy of the name.  (The arguments can also be NULL.)
59  *
60  * If the first argument is non-NULL, that is used, otherwise the seconds
61  * argument is used.  If both are NULL, no program name is included in the
62  * output.  The reason for having two arguments is so that the caller
63  * doesn't have to do the test, and can just call
64  *	set_progname(argv[0], "default_name");
65  * (it is valid for argv[0] to be NULL, under ISO C).
66  */
set_progname(const char * argv0,const char * def)67 void set_progname(const char *argv0, const char *def) {
68 	if (argv0 != NULL && *argv0 != '\0')
69 		progname = argv0;
70 	else if (def != NULL && *def != '\0')
71 		progname = def;
72 }
73 
74 
75 /*
76  * Return program name set by set_progname, or an empty string if not set.
77  */
get_progname(void)78 const char *get_progname(void) {
79 	return progname != NULL ? progname : "";
80 }
81 
82 
83 /*
84  * Print error message; if exitp is 1, exit, if 2, abort, after printing
85  * the message.  If eno is positive, print error corresponding strerror
86  * message; if eno is -1, use current value of errno.
87  */
errormsg(int do_after,int eno,const char * fmt,...)88 void errormsg(int do_after, int eno, const char *fmt, ...) {
89 	va_list args;
90 	size_t len;
91 	int print_newline, print_progname;
92 
93 	if (eno == -1)		/* this must be done before anything else */
94 		eno = errno;
95 
96 	assert(do_after == 0 || do_after == 1 || do_after == 2);
97 	assert(eno >= -1);	/* there is no known maximum value for errno */
98 	assert(fmt != NULL);
99 
100 	fflush(NULL);		/* output all pending output */
101 
102 	print_progname = (progname != NULL) && (*progname != '\0');
103 
104 	if (print_progname)
105 		fprintf(stderr, "%s: ", progname);
106 
107 	va_start(args, fmt);
108 	vfprintf(stderr, fmt, args);
109 	va_end(args);
110 
111 	len = strlen(fmt);
112 	print_newline = (len > 0) && (fmt[len-1] == '\n');
113 
114 	if (eno != 0) {
115 		if (print_newline) {
116 			if (print_progname)
117 				fprintf(stderr, "%s: ", progname);
118 			fprintf(stderr, "Possible explanation: ");
119 		} else {
120 			fprintf(stderr, ": ");
121 		}
122 		fprintf(stderr, "%s\n", strerror(eno));
123 	} else {
124 		if (!print_newline)
125 			fprintf(stderr, "\n"); /* make sure newline ends msg */
126 	}
127 
128 	fflush(stderr);
129 
130 	switch (do_after) {
131 	case 1:
132 		exit(EXIT_FAILURE);
133 	case 2:
134 		abort();
135 	}
136 }
137