Subject: bin/2906: w always prints current tiem as login time
To: None <gnats-bugs@gnats.netbsd.org>
From: Lennart Augustsson <augustss@cs.chalmers.se>
List: netbsd-bugs
Date: 10/29/1996 21:34:14
>Number:         2906
>Category:       bin
>Synopsis:       w always prints current tiem as login time
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Oct 29 12:50:00 1996
>Last-Modified:
>Originator:     Lennart Augustsson
>Organization:
Chalmers, CS dept.
>Release:        NetBSD-current 961029
>Environment:
System: NetBSD calvin.cs.chalmers.se 1.2B NetBSD 1.2B (CALVIN) #2: Sun Oct 27 18:23:57 MET 1996 augustss@calvin.cs.chalmers.se:/usr/src/sys/arch/i386/compile/CALVIN i386


>Description:
	w always prints the current time instead of the login time
	for users.
	The reason is that localtime is used twice without saving the
	value that it returns a pointer to.  That value is stored in
	a static buffer.
>How-To-Repeat:
	Just try w.
>Fix:
	Apply the patch below.
	It also fixes the problem that time_t values were subtracted
	without using difftime.
	[ We could probably get rid of the SCCS bizarreness also, but
	  I didn't bother. ]

*** /usr/src/usr.bin/w/pr_time.c.orig	Tue Oct 29 21:20:45 1996
--- /usr/src/usr.bin/w/pr_time.c	Tue Oct 29 21:26:27 1996
***************
*** 62,81 ****
  	time_t *started, *now;
  {
  	static char buf[256];
! 	struct tm *tp, *tpnow;
! 	time_t diff;
  	char fmt[20];
  
! 	tp = localtime(started);
! 	tpnow = localtime(now);
! 	diff = *now - *started;
  
  	/* If more than a week, use day-month-year. */
  	if (diff > SECSPERDAY * DAYSPERWEEK)
  		(void)strcpy(fmt, "%d%b%y");
  
  	/* If not today, use day-hour-am/pm. */
! 	else if (tp->tm_yday != tpnow->tm_yday) {
  		(void)strcpy(fmt, __CONCAT("%a%", "I%p"));
  	}
  
--- 62,81 ----
  	time_t *started, *now;
  {
  	static char buf[256];
! 	struct tm tstarted, tnow;
! 	long diff;
  	char fmt[20];
  
! 	tstarted = *localtime(started);
! 	tnow = *localtime(now);
! 	diff = difftime(*now, *started);
  
  	/* If more than a week, use day-month-year. */
  	if (diff > SECSPERDAY * DAYSPERWEEK)
  		(void)strcpy(fmt, "%d%b%y");
  
  	/* If not today, use day-hour-am/pm. */
! 	else if (tstarted.tm_yday != tnow.tm_yday) {
  		(void)strcpy(fmt, __CONCAT("%a%", "I%p"));
  	}
  
***************
*** 84,90 ****
  		(void)strcpy(fmt, __CONCAT("%l:%", "M%p"));
  	}
  
! 	(void)strftime(buf, sizeof(buf), fmt, tp);
  	(void)printf("%s", buf);
  }
  
--- 84,90 ----
  		(void)strcpy(fmt, __CONCAT("%l:%", "M%p"));
  	}
  
! 	(void)strftime(buf, sizeof(buf), fmt, &tstarted);
  	(void)printf("%s", buf);
  }
  
>Audit-Trail:
>Unformatted: