19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 348b56c58bSMark Murray #include <sys/cdefs.h> 358b56c58bSMark Murray 368b56c58bSMark Murray __FBSDID("$FreeBSD$"); 378b56c58bSMark Murray 389b50d902SRodney W. Grimes #ifndef lint 398b56c58bSMark Murray static const char sccsid[] = "@(#)pr_time.c 8.2 (Berkeley) 4/4/94"; 4090389da9SPhilippe Charnier #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <sys/types.h> 439b50d902SRodney W. Grimes #include <sys/time.h> 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <stdio.h> 469b50d902SRodney W. Grimes #include <string.h> 47e39ee421SAndrey A. Chernov #include <wchar.h> 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include "extern.h" 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes /* 529b50d902SRodney W. Grimes * pr_attime -- 539b50d902SRodney W. Grimes * Print the time since the user logged in. 549b50d902SRodney W. Grimes */ 5534903a55SHajimu UMEMOTO int 56e8e649ccSJuli Mallett pr_attime(time_t *started, time_t *now) 579b50d902SRodney W. Grimes { 5834903a55SHajimu UMEMOTO static wchar_t buf[256]; 5965824845SDavid Nugent struct tm tp, tm; 609b50d902SRodney W. Grimes time_t diff; 61277dbce9SXin LI const wchar_t *fmt; 6234903a55SHajimu UMEMOTO int len, width, offset = 0; 639b50d902SRodney W. Grimes 6465824845SDavid Nugent tp = *localtime(started); 6565824845SDavid Nugent tm = *localtime(now); 669b50d902SRodney W. Grimes diff = *now - *started; 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes /* If more than a week, use day-month-year. */ 69656dcd43SGarrett Wollman if (diff > 86400 * 7) 7034903a55SHajimu UMEMOTO fmt = L"%d%b%y"; 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes /* If not today, use day-hour-am/pm. */ 7365824845SDavid Nugent else if (tm.tm_mday != tp.tm_mday || 7465824845SDavid Nugent tm.tm_mon != tp.tm_mon || 7565824845SDavid Nugent tm.tm_year != tp.tm_year) { 7665824845SDavid Nugent /* The line below does not take DST into consideration */ 7765824845SDavid Nugent /* else if (*now / 86400 != *started / 86400) { */ 7834903a55SHajimu UMEMOTO fmt = use_ampm ? L"%a%I%p" : L"%a%H"; 799b50d902SRodney W. Grimes } 809b50d902SRodney W. Grimes 819b50d902SRodney W. Grimes /* Default is hh:mm{am,pm}. */ 829b50d902SRodney W. Grimes else { 8334903a55SHajimu UMEMOTO fmt = use_ampm ? L"%l:%M%p" : L"%k:%M"; 849b50d902SRodney W. Grimes } 859b50d902SRodney W. Grimes 8634903a55SHajimu UMEMOTO (void)wcsftime(buf, sizeof(buf), fmt, &tp); 8734903a55SHajimu UMEMOTO len = wcslen(buf); 8834903a55SHajimu UMEMOTO width = wcswidth(buf, len); 8934903a55SHajimu UMEMOTO if (len == width) 9034903a55SHajimu UMEMOTO (void)wprintf(L"%-7.7ls", buf); 9134903a55SHajimu UMEMOTO else if (width < 7) 9234903a55SHajimu UMEMOTO (void)wprintf(L"%ls%.*s", buf, 7 - width, " "); 9334903a55SHajimu UMEMOTO else { 9434903a55SHajimu UMEMOTO (void)wprintf(L"%ls", buf); 9534903a55SHajimu UMEMOTO offset = width - 7; 9634903a55SHajimu UMEMOTO } 9734903a55SHajimu UMEMOTO return (offset); 989b50d902SRodney W. Grimes } 999b50d902SRodney W. Grimes 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * pr_idle -- 1029b50d902SRodney W. Grimes * Display the idle time. 10369b41093SPeter Wemm * Returns number of excess characters that were used for long idle time. 1049b50d902SRodney W. Grimes */ 10570498d5aSDaniel O'Callaghan int 106e8e649ccSJuli Mallett pr_idle(time_t idle) 1079b50d902SRodney W. Grimes { 1089b50d902SRodney W. Grimes /* If idle more than 36 hours, print as a number of days. */ 10989cc2fabSDima Ruban if (idle >= 36 * 3600) { 11089cc2fabSDima Ruban int days = idle / 86400; 11189cc2fabSDima Ruban (void)printf(" %dday%s ", days, days > 1 ? "s" : " " ); 11269b41093SPeter Wemm if (days >= 100) 11369b41093SPeter Wemm return (2); 11470498d5aSDaniel O'Callaghan if (days >= 10) 11570498d5aSDaniel O'Callaghan return (1); 11689cc2fabSDima Ruban } 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes /* If idle more than an hour, print as HH:MM. */ 119656dcd43SGarrett Wollman else if (idle >= 3600) 1209b50d902SRodney W. Grimes (void)printf(" %2d:%02d ", 1218109e672SAlexander Langer (int)(idle / 3600), (int)((idle % 3600) / 60)); 1229b50d902SRodney W. Grimes 123656dcd43SGarrett Wollman else if (idle / 60 == 0) 124255318a8SAndrey A. Chernov (void)printf(" - "); 125255318a8SAndrey A. Chernov 1269b50d902SRodney W. Grimes /* Else print the minutes idle. */ 1279b50d902SRodney W. Grimes else 1288109e672SAlexander Langer (void)printf(" %2d ", (int)(idle / 60)); 12970498d5aSDaniel O'Callaghan 13070498d5aSDaniel O'Callaghan return (0); /* not idle longer than 9 days */ 1319b50d902SRodney W. Grimes } 132