#include <stdio.h>
#include <syslog.h>
#include <pwd.h>
#include <strings.h>
#include <errno.h>
#include <sys/param.h>

char *ttyname(), *malloc();
int strlen();

log (cmd, argv)	
char *cmd, *argv[];		
{			
	extern int errno;
	int uid;
	struct passwd *u_pwd;
	char wd[MAXPATHLEN];		/* 1024 */
	char *buffp;
	int i;
	int tty_iocbp;
	char *tty_id;
	char *cmd_string;
	int cmd_len = 0;

	if (argv != NULL) {
		for (i = 0; argv[i] != NULL; i++) {
			cmd_len += strlen(argv[i]) + 1;
		}
	}
	getwd(wd);
	uid = getuid();
	u_pwd = getpwuid(uid);
	endpwent();
	tty_id = ttyname(2);
	if (tty_id == NULL) tty_id = "no tty";
	cmd_len += strlen(u_pwd->pw_name) + strlen(wd) + 6;
	cmd_string = malloc(cmd_len);
	if (cmd_string == NULL) {
		perror("malloc");
		exit(1);
	}

	sprintf(cmd_string, "%s (%s; %s)", u_pwd->pw_name, tty_id, wd);
	buffp = cmd_string + strlen(cmd_string);
	while(*argv != NULL) {
		strcpy(buffp++, " ");
		strcpy(buffp, *argv);
		buffp += strlen(*argv++);
	}

	/* syslog() can't handle lines over 1024 chars long... */
	if (cmd_len > 1000)
	     cmd_string[1000] = '\0';
	syslog(LOG_INFO, "%s", cmd_string);
}
