xref: /original-bsd/usr.sbin/sendmail/src/err.c (revision a304ca22)
1 # include "sendmail.h"
2 # ifdef LOG
3 # include <syslog.h>
4 # endif LOG
5 
6 SCCSID(@(#)err.c	3.19		03/06/82);
7 
8 /*
9 **  SYSERR -- Print error message.
10 **
11 **	Prints an error message via printf to the diagnostic
12 **	output.  If LOG is defined, it logs it also.
13 **
14 **	Parameters:
15 **		f -- the format string
16 **		a, b, c, d, e -- parameters
17 **
18 **	Returns:
19 **		none
20 **
21 **	Side Effects:
22 **		increments Errors.
23 **		sets ExitStat.
24 */
25 
26 # ifdef lint
27 int	sys_nerr;
28 char	*sys_errlist[];
29 # endif lint
30 
31 /*VARARGS1*/
32 syserr(fmt, a, b, c, d, e)
33 	char *fmt;
34 {
35 	static char errbuf[2*BUFSIZ];
36 	extern char Arpa_Syserr[];
37 
38 	/* format the error message */
39 	fmtmsg(errbuf, (char *) NULL, Arpa_Syserr, fmt, a, b, c, d, e);
40 
41 	/* output error message to transcript */
42 	fprintf(Xscript, "%s\n", errbuf);
43 
44 	/* output error message to output channel if appropriate */
45 	if (!HoldErrs)
46 	{
47 		if (ArpaMode)
48 			fprintf(OutChannel, "%s\r\n", errbuf);
49 		else
50 			fprintf(OutChannel, "sendmail: %s\n", &errbuf[4]);
51 		(void) fflush(OutChannel);
52 	}
53 
54 	Errors++;
55 	FatalErrors = TRUE;
56 
57 	/* determine exit status if not already set */
58 	if (ExitStat == EX_OK)
59 	{
60 		if (errno == 0)
61 			ExitStat = EX_SOFTWARE;
62 		else
63 			ExitStat = EX_OSERR;
64 	}
65 
66 # ifdef LOG
67 	syslog(LOG_ERR, "%s->%s: %s", From.q_paddr, To, &errbuf[4]);
68 # endif LOG
69 	errno = 0;
70 }
71 /*
72 **  USRERR -- Signal user error.
73 **
74 **	This is much like syserr except it is for user errors.
75 **
76 **	Parameters:
77 **		fmt, a, b, c, d -- printf strings
78 **
79 **	Returns:
80 **		none
81 **
82 **	Side Effects:
83 **		increments Errors.
84 */
85 
86 /*VARARGS1*/
87 usrerr(fmt, a, b, c, d, e)
88 	char *fmt;
89 {
90 	extern char SuprErrs;
91 	extern char Arpa_Usrerr[];
92 
93 	if (SuprErrs)
94 		return;
95 	Errors++;
96 	FatalErrors = TRUE;
97 
98 	message(Arpa_Usrerr, fmt, a, b, c, d, e);
99 }
100 /*
101 **  MESSAGE -- print message (not necessarily an error)
102 **
103 **	Parameters:
104 **		num -- the default ARPANET error number (in ascii)
105 **		msg -- the message (printf fmt) -- if it begins
106 **			with a digit, this number overrides num.
107 **		a, b, c, d, e -- printf arguments
108 **
109 **	Returns:
110 **		none
111 **
112 **	Side Effects:
113 **		none.
114 */
115 
116 /*VARARGS2*/
117 message(num, msg, a, b, c, d, e)
118 	register char *num;
119 	register char *msg;
120 {
121 	char errbuf[2*BUFSIZ];
122 
123 	errno = 0;
124 	fmtmsg(errbuf, To, num, msg, a, b, c, d, e);
125 
126 	/* output to transcript */
127 	fprintf(Xscript, "%s\n", errbuf);
128 
129 	/* output to channel if appropriate */
130 	if (!HoldErrs)
131 	{
132 		if (ArpaMode)
133 			fprintf(OutChannel, "%s\r\n", errbuf);
134 		else
135 			fprintf(OutChannel, "%s\n", &errbuf[4]);
136 		(void) fflush(OutChannel);
137 	}
138 }
139 /*
140 **  FMTMSG -- format a message into buffer.
141 **
142 **	Parameters:
143 **		eb -- error buffer to get result.
144 **		to -- the recipient tag for this message.
145 **		num -- arpanet error number.
146 **		fmt -- format of string.
147 **		a, b, c, d, e -- arguments.
148 **
149 **	Returns:
150 **		none.
151 **
152 **	Side Effects:
153 **		none.
154 */
155 
156 /*VARARGS4*/
157 static
158 fmtmsg(eb, to, num, fmt, a, b, c, d, e)
159 	register char *eb;
160 	char *to;
161 	char *num;
162 	char *fmt;
163 {
164 	char del;
165 
166 	/* output the reply code */
167 	if (isdigit(*fmt))
168 	{
169 		num = fmt;
170 		fmt += 4;
171 	}
172 	if (num[3] == '-')
173 		del = '-';
174 	else
175 		del = ' ';
176 	(void) sprintf(eb, "%3.3s%c", num, del);
177 	eb += 4;
178 
179 	/* output the "to" person */
180 	if (to != NULL && to[0] != '\0')
181 	{
182 		(void) sprintf(eb, "%s... ", to);
183 		while (*eb != '\0')
184 			*eb++ &= 0177;
185 	}
186 
187 	/* output the message */
188 	(void) sprintf(eb, fmt, a, b, c, d, e);
189 	while (*eb != '\0')
190 		*eb++ &= 0177;
191 
192 	/* output the error code, if any */
193 	if (errno != 0)
194 	{
195 		extern int sys_nerr;
196 		extern char *sys_errlist[];
197 		if (errno < sys_nerr && errno > 0)
198 			(void) sprintf(eb, ": %s", sys_errlist[errno]);
199 		else
200 			(void) sprintf(eb, ": error %d", errno);
201 		eb += strlen(eb);
202 	}
203 }
204