Home | History | Annotate | Line # | Download | only in w
pr_time.c revision 1.3
      1 /*	$NetBSD: pr_time.c,v 1.3 1996/06/07 01:39:21 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)pr_time.c	8.2 (Berkeley) 4/4/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: pr_time.c,v 1.3 1996/06/07 01:39:21 thorpej Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/time.h>
     46 
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <tzfile.h>
     50 
     51 #include "extern.h"
     52 
     53 /*
     54  * pr_attime --
     55  *	Print the time since the user logged in.
     56  *
     57  *	Note: SCCS forces the bizarre string manipulation, things like
     58  *	8.2 get replaced in the source code.
     59  */
     60 void
     61 pr_attime(started, now)
     62 	time_t *started, *now;
     63 {
     64 	static char buf[256];
     65 	struct tm *tp;
     66 	time_t diff;
     67 	char fmt[20];
     68 
     69 	tp = localtime(started);
     70 	diff = *now - *started;
     71 
     72 	/* If more than a week, use day-month-year. */
     73 	if (diff > SECSPERDAY * DAYSPERWEEK)
     74 		(void)strcpy(fmt, "%d%b%y");
     75 
     76 	/* If not today, use day-hour-am/pm. */
     77 	else if (*now / SECSPERDAY != *started / SECSPERDAY) {
     78 		(void)strcpy(fmt, __CONCAT("%a%", "I%p"));
     79 	}
     80 
     81 	/* Default is hh:mm{am,pm}. */
     82 	else {
     83 		(void)strcpy(fmt, __CONCAT("%l:%", "M%p"));
     84 	}
     85 
     86 	(void)strftime(buf, sizeof(buf), fmt, tp);
     87 	(void)printf("%s", buf);
     88 }
     89 
     90 /*
     91  * pr_idle --
     92  *	Display the idle time.
     93  */
     94 void
     95 pr_idle(idle)
     96 	time_t idle;
     97 {
     98 	int days = idle / SECSPERDAY;
     99 
    100 	/* If idle more than 36 hours, print as a number of days. */
    101 	if (idle >= 36 * SECSPERHOUR)
    102 		printf(days == 1 ? "  %dday " : " %ddays ", days);
    103 
    104 	/* If idle more than an hour, print as HH:MM. */
    105 	else if (idle >= SECSPERHOUR)
    106 		(void)printf(" %2d:%02d ",
    107 		    idle / SECSPERHOUR, (idle % SECSPERHOUR) / SECSPERMIN);
    108 
    109 	/* Else print the minutes idle. */
    110 	else
    111 		(void)printf("    %2d ", idle / SECSPERMIN);
    112 }
    113