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