xref: /illumos-gate/usr/src/cmd/tip/log.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
5*8d489c7aSmuffin 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
11*8d489c7aSmuffin 
127c478bd9Sstevel@tonic-gate #include "tip.h"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate static	FILE *flog = NULL;
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate  * Log file maintenance routines
187c478bd9Sstevel@tonic-gate  */
19*8d489c7aSmuffin void
logent(char * group,char * num,char * acu,char * message)20*8d489c7aSmuffin logent(char *group, char *num, char *acu, char *message)
217c478bd9Sstevel@tonic-gate {
227c478bd9Sstevel@tonic-gate 	char *user, *timestamp;
237c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
247c478bd9Sstevel@tonic-gate 	time_t t;
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 	if (flog == NULL)
277c478bd9Sstevel@tonic-gate 		return;
287c478bd9Sstevel@tonic-gate #ifndef USG
297c478bd9Sstevel@tonic-gate 	if (flock(fileno(flog), LOCK_EX) < 0) {
307c478bd9Sstevel@tonic-gate 		perror("tip: flock");
317c478bd9Sstevel@tonic-gate 		return;
327c478bd9Sstevel@tonic-gate 	}
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 	if ((user = getlogin()) == NOSTR)
357c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == NOPWD)
367c478bd9Sstevel@tonic-gate 			user = "???";
377c478bd9Sstevel@tonic-gate 		else
387c478bd9Sstevel@tonic-gate 			user = pwd->pw_name;
397c478bd9Sstevel@tonic-gate 	t = time(0);
407c478bd9Sstevel@tonic-gate 	timestamp = ctime(&t);
417c478bd9Sstevel@tonic-gate 	timestamp[24] = '\0';
42*8d489c7aSmuffin 	(void) fprintf(flog, "%s (%s) <%s, %s, %s> %s\n",
437c478bd9Sstevel@tonic-gate 	    user, timestamp, group,
447c478bd9Sstevel@tonic-gate #ifdef PRISTINE
457c478bd9Sstevel@tonic-gate 	    "",
467c478bd9Sstevel@tonic-gate #else
477c478bd9Sstevel@tonic-gate 	    num,
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 	    acu, message);
50*8d489c7aSmuffin 	(void) fflush(flog);
517c478bd9Sstevel@tonic-gate #ifndef USG
527c478bd9Sstevel@tonic-gate 	(void) flock(fileno(flog), LOCK_UN);
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
56*8d489c7aSmuffin void
loginit(void)57*8d489c7aSmuffin loginit(void)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #ifdef ACULOG
617c478bd9Sstevel@tonic-gate 	flog = fopen(value(LOG), "a");
627c478bd9Sstevel@tonic-gate 	if (flog == NULL)
63*8d489c7aSmuffin 		(void) fprintf(stderr, "tip: can't open log file %s\r\n",
64*8d489c7aSmuffin 		    value(LOG));
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate }
67