xref: /illumos-gate/usr/src/cmd/tip/log.c (revision 6d3b960aa395b3d268922a1938aed248a213725b)
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 #include "tip.h"
13 
14 static	FILE *flog = NULL;
15 
16 /*
17  * Log file maintenance routines
18  */
19 void
20 logent(char *group, char *num, char *acu, char *message)
21 {
22 	char *user, *timestamp;
23 	struct passwd *pwd;
24 	time_t t;
25 
26 	if (flog == NULL)
27 		return;
28 #ifndef USG
29 	if (flock(fileno(flog), LOCK_EX) < 0) {
30 		perror("tip: flock");
31 		return;
32 	}
33 #endif
34 	if ((user = getlogin()) == NOSTR)
35 		if ((pwd = getpwuid(uid)) == NOPWD)
36 			user = "???";
37 		else
38 			user = pwd->pw_name;
39 	t = time(0);
40 	timestamp = ctime(&t);
41 	timestamp[24] = '\0';
42 	(void) fprintf(flog, "%s (%s) <%s, %s, %s> %s\n",
43 	    user, timestamp, group,
44 #ifdef PRISTINE
45 	    "",
46 #else
47 	    num,
48 #endif
49 	    acu, message);
50 	(void) fflush(flog);
51 #ifndef USG
52 	(void) flock(fileno(flog), LOCK_UN);
53 #endif
54 }
55 
56 void
57 loginit(void)
58 {
59 
60 #ifdef ACULOG
61 	flog = fopen(value(LOG), "a");
62 	if (flog == NULL)
63 		(void) fprintf(stderr, "tip: can't open log file %s\r\n",
64 		    value(LOG));
65 #endif
66 }
67