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