xref: /illumos-gate/usr/src/cmd/tip/log.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from UCB 4.6 6/25/83 */
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #include "tip.h"
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate static	FILE *flog = NULL;
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate /*
17*7c478bd9Sstevel@tonic-gate  * Log file maintenance routines
18*7c478bd9Sstevel@tonic-gate  */
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate logent(group, num, acu, message)
21*7c478bd9Sstevel@tonic-gate 	char *group, *num, *acu, *message;
22*7c478bd9Sstevel@tonic-gate {
23*7c478bd9Sstevel@tonic-gate 	char *user, *timestamp;
24*7c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
25*7c478bd9Sstevel@tonic-gate 	time_t t;
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate 	if (flog == NULL)
28*7c478bd9Sstevel@tonic-gate 		return;
29*7c478bd9Sstevel@tonic-gate #ifndef USG
30*7c478bd9Sstevel@tonic-gate 	if (flock(fileno(flog), LOCK_EX) < 0) {
31*7c478bd9Sstevel@tonic-gate 		perror("tip: flock");
32*7c478bd9Sstevel@tonic-gate 		return;
33*7c478bd9Sstevel@tonic-gate 	}
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 	if ((user = getlogin()) == NOSTR)
36*7c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == NOPWD)
37*7c478bd9Sstevel@tonic-gate 			user = "???";
38*7c478bd9Sstevel@tonic-gate 		else
39*7c478bd9Sstevel@tonic-gate 			user = pwd->pw_name;
40*7c478bd9Sstevel@tonic-gate 	t = time(0);
41*7c478bd9Sstevel@tonic-gate 	timestamp = ctime(&t);
42*7c478bd9Sstevel@tonic-gate 	timestamp[24] = '\0';
43*7c478bd9Sstevel@tonic-gate 	fprintf(flog, "%s (%s) <%s, %s, %s> %s\n",
44*7c478bd9Sstevel@tonic-gate 		user, timestamp, group,
45*7c478bd9Sstevel@tonic-gate #ifdef PRISTINE
46*7c478bd9Sstevel@tonic-gate 		"",
47*7c478bd9Sstevel@tonic-gate #else
48*7c478bd9Sstevel@tonic-gate 		num,
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 		acu, message);
51*7c478bd9Sstevel@tonic-gate 	fflush(flog);
52*7c478bd9Sstevel@tonic-gate #ifndef USG
53*7c478bd9Sstevel@tonic-gate 	(void) flock(fileno(flog), LOCK_UN);
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate loginit()
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #ifdef ACULOG
61*7c478bd9Sstevel@tonic-gate 	flog = fopen(value(LOG), "a");
62*7c478bd9Sstevel@tonic-gate 	if (flog == NULL)
63*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "tip: can't open log file %s\r\n", value(LOG));
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate }
66