1 /*
2   lsyslog.c - wrapper for the syslog function
3   Copyright (C) 1997 Uwe Ohse
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2, or (at your option)
8   any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18   02111-1307, USA.
19 
20 */
21 #include "config.h"
22 #ifdef ENABLE_SYSLOG
23 #include "zglobal.h"
24 #include <pwd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #endif
28 
29 #if __STDC__
30 #  include <stdarg.h>
31 #  define VA_START(args, lastarg) va_start(args, lastarg)
32 #  define WAYTOGO
33 #else
34 #  include <varargs.h>
35 #  define VA_START(args, lastarg) va_start(args)
36 #endif
37 
38 void
39 #ifdef WAYTOGO
lsyslog(int prio,const char * format,...)40 lsyslog(int prio, const char *format, ...)
41 #else
42 lsyslog(prio,format,va_alist)
43 	int prio;
44 	const char *format;
45 	va_dcl
46 #endif
47 {
48 #ifdef ENABLE_SYSLOG
49 	static char *username=NULL;
50 	static char uid_string[20]=""; /* i'd really hate this function to fail! */
51 	char *s=NULL;
52 	static int init_done=0;
53     va_list ap;
54 	if (!enable_syslog)
55 		return;
56 	if (!init_done) {
57 		uid_t uid;
58 		struct passwd *pwd;
59 		init_done=1;
60 		uid=getuid();
61 		pwd=getpwuid(uid);
62 		if (pwd && pwd->pw_name && *pwd->pw_name) {
63 			username=strdup(pwd->pw_name);
64 		}
65 		if (!username) {
66 			username=uid_string;
67 			sprintf(uid_string,"#%lu",(unsigned long) uid);
68 		}
69 	}
70 
71     VA_START(ap, format);
72     vasprintf(&s,format, ap);
73     va_end(ap);
74     syslog(prio,"[%s] %s",username,s);
75 	free(s);
76 #else
77 	(void) prio; /* get rid of warning */
78 	(void) format; /* get rid of warning */
79 #endif
80 }
81 
82