1 /* $OpenBSD: pr_time.c,v 1.9 1998/01/16 17:50:42 millert 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 = "$OpenBSD: pr_time.c,v 1.9 1998/01/16 17:50:42 millert 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 int today; 69 70 today = localtime(now)->tm_yday; 71 tp = localtime(started); 72 diff = *now - *started; 73 74 /* If more than a week, use day-month-year. */ 75 if (diff > SECSPERDAY * DAYSPERWEEK) 76 (void)strcpy(fmt, "%d%b%y"); 77 78 /* If not today, use day-hour-am/pm. */ 79 else if (tp->tm_yday != today ) { 80 (void)strcpy(fmt, __CONCAT("%a%", "I%p")); 81 } 82 83 /* Default is hh:mm{am,pm}. */ 84 else { 85 (void)strcpy(fmt, __CONCAT("%l:%", "M%p")); 86 } 87 88 (void)strftime(buf, sizeof(buf) -1, fmt, tp); 89 buf[sizeof buf - 1] = '\0'; 90 (void)printf("%s", buf); 91 } 92 93 /* 94 * pr_idle -- 95 * Display the idle time. 96 */ 97 void 98 pr_idle(idle) 99 time_t idle; 100 { 101 int days = idle / SECSPERDAY; 102 103 /* If idle more than 36 hours, print as a number of days. */ 104 if (idle >= 36 * SECSPERHOUR) { 105 if (days == 1) 106 printf(" %dday ", days); 107 else if (days < 10) 108 printf(" %ddays ", days); 109 else 110 printf("%ddays ", days); 111 } 112 113 /* If idle more than an hour, print as HH:MM. */ 114 else if (idle >= SECSPERHOUR) 115 (void)printf(" %2d:%02d ", 116 idle / SECSPERHOUR, (idle % SECSPERHOUR) / SECSPERMIN); 117 118 /* Else print the minutes idle. */ 119 else 120 (void)printf(" %2d ", idle / SECSPERMIN); 121 } 122