xref: /original-bsd/usr.bin/tip/log.c (revision f1dcf51e)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)log.c	5.1 (Berkeley) 04/30/85";
9 #endif not lint
10 
11 #include "tip.h"
12 
13 static	FILE *flog = NULL;
14 
15 /*
16  * Log file maintenance routines
17  */
18 
19 logent(group, num, acu, message)
20 	char *group, *num, *acu, *message;
21 {
22 	char *user, *timestamp;
23 	struct passwd *pwd;
24 	long t;
25 
26 	if (flog == NULL)
27 		return;
28 	if (flock(fileno(flog), LOCK_EX) < 0) {
29 		perror("tip: flock");
30 		return;
31 	}
32 	if ((user = getlogin()) == NOSTR)
33 		if ((pwd = getpwuid(getuid())) == NOPWD)
34 			user = "???";
35 		else
36 			user = pwd->pw_name;
37 	t = time(0);
38 	timestamp = ctime(&t);
39 	timestamp[24] = '\0';
40 	fprintf(flog, "%s (%s) <%s, %s, %s> %s\n",
41 		user, timestamp, group,
42 #ifdef PRISTINE
43 		"",
44 #else
45 		num,
46 #endif
47 		acu, message);
48 	fflush(flog);
49 	(void) flock(fileno(flog), LOCK_UN);
50 }
51 
52 loginit()
53 {
54 
55 #ifdef ACULOG
56 	flog = fopen(value(LOG), "a");
57 	if (flog == NULL)
58 		fprintf(stderr, "can't open log file\r\n");
59 #endif
60 }
61