Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / support / pop / RCS / syslog.c,v
1 head    1.9;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.9
9 date    93.08.25.17.23.42;      author jromine; state Exp;
10 branches;
11 next    1.8;
12
13 1.8
14 date    92.12.15.00.20.22;      author jromine; state Exp;
15 branches;
16 next    1.7;
17
18 1.7
19 date    92.11.19.00.24.36;      author jromine; state Exp;
20 branches;
21 next    1.6;
22
23 1.6
24 date    92.11.13.00.04.36;      author jromine; state Exp;
25 branches;
26 next    1.5;
27
28 1.5
29 date    90.04.05.15.34.53;      author sources; state Exp;
30 branches;
31 next    1.4;
32
33 1.4
34 date    90.04.05.14.54.30;      author sources; state Exp;
35 branches;
36 next    1.3;
37
38 1.3
39 date    90.02.05.14.11.20;      author sources; state Exp;
40 branches;
41 next    1.2;
42
43 1.2
44 date    90.02.01.14.14.35;      author sources; state Exp;
45 branches;
46 next    1.1;
47
48 1.1
49 date    90.02.01.14.09.03;      author sources; state Exp;
50 branches;
51 next    ;
52
53
54 desc
55 @@
56
57
58 1.9
59 log
60 @off_t fixes for BSD44
61 @
62 text
63 @#if !defined (BSD43) && !defined(hpux)
64 #ifndef lint
65 static char SccsId[] =  "@@(#)syslog.c  4.1 (Berkeley) 5/27/83";
66 #endif
67 #ifndef lint
68 static char ident[] = "@@(#)$Id: syslog.c,v 1.8 1992/12/15 00:20:22 jromine Exp jromine $";
69 #endif  /* lint */
70
71 /*
72  * SYSLOG -- print message on log file
73  *
74  * This routine looks a lot like printf, except that it
75  * outputs to the log file instead of the standard output.
76  * Also, it prints the module name in front of lines,
77  * and has some other formatting types (or will sometime).
78  * Also, it adds a newline on the end of messages.
79  *
80  * The output of this routine is intended to be read by
81  * /etc/syslog, which will add timestamps.
82  */
83 #include <sys/types.h>
84 #include <sys/socket.h>
85 #include <netinet/in.h>
86
87 #include "syslog.h"
88 #include <netdb.h>
89
90 #define MAXLINE 1024            /* max message size */
91 #define BUFSLOP 20              /* space to allow for "extra stuff" */
92 #ifndef NULL
93 #define NULL    0               /* manifest */
94 #endif
95
96 #define LOG_COOLIT      LOG_LOCAL0      /* local syslog code */
97 #define LOG_DGRAM       LOG_LOCAL1      /* idem */
98
99 #ifndef LOG_HOST
100 #define LOG_HOST        "localhost"     /* host where syslogd is running */
101 #endif  /* LOG_HOST */
102
103 int     LogFile = -1;           /* fd for log */
104 int     LogStat = 0;            /* status bits, set by initlog */
105 char    *LogTag = (char *)NULL; /* string to tag the entry with */
106 int     LogMask = LOG_DEBUG;    /* lowest priority to be logged */
107
108 struct sockaddr_in SyslogAddr;
109 static char *SyslogHost = LOG_HOST;
110
111 extern  int errno;
112 #ifndef BSD44
113 extern  int     sys_nerr;
114 extern  char *sys_errlist[];
115 #endif
116
117 syslog(pri, fmt, p0, p1, p2, p3, p4)
118         int pri;
119         char *fmt;
120 {
121         char buf[MAXLINE+BUFSLOP], outline[MAXLINE + 1];
122         register char *b, *f;
123
124         if (LogFile < 0)
125                 openlog(0, 0);
126         /* see if we should just throw out this message */
127         if (pri > LogMask)
128                 return;
129         for (b = buf, f = fmt; f && *f; b = buf) {
130                 register char c;
131
132                 if (pri > 0 && (LogStat & LOG_COOLIT) == 0) {
133                         sprintf(b, "<%d>", pri);
134                         b += strlen(b);
135                 }
136                 if (LogStat & LOG_PID) {
137                         sprintf(b, "%d ", getpid());
138                         b += strlen(b);
139                 }
140                 if (LogTag) {
141                         sprintf(b, "%s: ", LogTag);
142                         b += strlen(b);
143                 }
144                 while ((c = *f++) != '\0' && c != '\n' && b < buf + MAXLINE) {
145                         if (c != '%') {
146                                 *b++ = c;
147                                 continue;
148                         }
149                         c = *f++;
150                         if (c != 'm') {
151 #ifndef notdef
152                                 *b++ = '%', *b++ = c, *b++ = '\0';
153 #else
154                                 *b++ = '%', *b++ = c;
155 #endif
156                                 continue;
157                         }
158                         if ((unsigned)errno > sys_nerr)
159                                 sprintf(b, "error %d", errno);
160                         else
161                                 strcat(b, sys_errlist[errno]);
162                         b += strlen(b);
163                 }
164                 if (c == '\0')
165                         f--;
166                 *b++ = '\n', *b = '\0';
167                 sprintf(outline, buf, p0, p1, p2, p3, p4);
168                 errno = 0;
169                 if (LogStat & LOG_DGRAM)
170                         (void) sendto(LogFile, outline, strlen(outline), 0,
171                                    &SyslogAddr, sizeof SyslogAddr);
172                 else
173                         (void) write(LogFile, outline, strlen(outline));
174                 if (errno)
175                         perror("syslog: sendto");
176         }
177 }
178
179 /*
180  * OPENLOG -- open system log
181  */
182 openlog(ident, logstat)
183         char *ident;
184         int logstat;
185 {
186         struct servent *sp;
187         struct hostent *hp;
188
189         LogTag = ident;
190         LogStat = logstat;
191         if (LogFile >= 0)
192                 return;
193         sp = getservbyname("syslog", "udp");
194         hp = gethostbyname(SyslogHost);
195         if (sp == NULL || hp == NULL)
196                 goto bad;
197         LogFile = socket(AF_INET, SOCK_DGRAM, 0);
198         if (LogFile < 0) {
199                 perror("syslog: socket");
200                 goto bad;
201         }
202         bzero(&SyslogAddr, sizeof SyslogAddr);
203         SyslogAddr.sin_family = hp->h_addrtype;
204         bcopy(hp->h_addr, (char *)&SyslogAddr.sin_addr, hp->h_length);
205         SyslogAddr.sin_port = sp->s_port;
206         LogStat |= LOG_DGRAM;
207         return;
208 bad:
209         LogStat |= LOG_COOLIT;
210         LogStat &= ~LOG_DGRAM;
211         LogMask = LOG_CRIT;
212         LogFile = open("/dev/console", 1);
213         if (LogFile < 0) {
214                 perror("syslog: /dev/console");
215                 LogFile = 2;
216         }
217 }
218
219 /*
220  * CLOSELOG -- close the system log
221  */
222 closelog()
223 {
224
225         (void) close(LogFile);
226         LogFile = -1;
227 }
228 #endif  /* not BSD43 */
229 @
230
231
232 1.8
233 log
234 @endif sugar
235 @
236 text
237 @d6 1
238 a6 1
239 static char ident[] = "@@(#)$Id: syslog.c,v 1.7 1992/11/19 00:24:36 jromine Exp jromine $";
240 d49 3
241 a51 1
242 extern  int errno, sys_nerr;
243 d53 1
244 @
245
246
247 1.7
248 log
249 @define NULL only if not defined
250 @
251 text
252 @d6 2
253 a7 2
254 static char ident[] = "@@(#)$Id: syslog.c,v 1.6 1992/11/13 00:04:36 jromine Exp jromine $";
255 #endif  lint
256 d39 1
257 a39 1
258 #endif  LOG_HOST
259 d163 1
260 a163 1
261 #endif  not BSD43
262 @
263
264
265 1.6
266 log
267 @typo
268 @
269 text
270 @d6 1
271 a6 1
272 static char ident[] = "@@(#)$Id: syslog.c,v 1.5 1990/04/05 15:34:53 sources Exp jromine $";
273 d30 1
274 d32 1
275 d43 1
276 a43 1
277 char    *LogTag = NULL;         /* string to tag the entry with */
278 @
279
280
281 1.5
282 log
283 @add ID
284 @
285 text
286 @d6 1
287 a6 1
288 static char ident[] = "@@(#)$Id:$";
289 d84 1
290 a84 1
291 #ifndef UCI
292 d86 1
293 a86 1
294 #else   UCI
295 d88 1
296 a88 1
297 #endif  UCI
298 @
299
300
301 1.4
302 log
303 @add ID
304 @
305 text
306 @d6 1
307 a6 1
308 static char ident[] = "@@(#)$Id:";
309 @
310
311
312 1.3
313 log
314 @SYS5 patches
315 @
316 text
317 @d5 3
318 @
319
320
321 1.2
322 log
323 @HPUX systems evidently have syslog now
324 @
325 text
326 @d22 1
327 a22 1
328 #include <syslog.h>
329 d28 7
330 @
331
332
333 1.1
334 log
335 @Initial revision
336 @
337 text
338 @d1 1
339 a1 1
340 #ifndef BSD43
341 @