sprint.c revision 11386
1245450Sganbold/* 2266337Sian * Copyright (c) 1989, 1993 3245450Sganbold * The Regents of the University of California. All rights reserved. 4245450Sganbold * 5245450Sganbold * This code is derived from software contributed to Berkeley by 6245450Sganbold * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7245450Sganbold * 8245450Sganbold * Redistribution and use in source and binary forms, with or without 9245450Sganbold * modification, are permitted provided that the following conditions 10245450Sganbold * are met: 11245450Sganbold * 1. Redistributions of source code must retain the above copyright 12245450Sganbold * notice, this list of conditions and the following disclaimer. 13245450Sganbold * 2. Redistributions in binary form must reproduce the above copyright 14245450Sganbold * notice, this list of conditions and the following disclaimer in the 15245450Sganbold * documentation and/or other materials provided with the distribution. 16245450Sganbold * 3. All advertising materials mentioning features or use of this software 17245454Sganbold * must display the following acknowledgement: 18245450Sganbold * This product includes software developed by the University of 19245450Sganbold * California, Berkeley and its contributors. 20245450Sganbold * 4. Neither the name of the University nor the names of its contributors 21245450Sganbold * may be used to endorse or promote products derived from this software 22245450Sganbold * without specific prior written permission. 23245450Sganbold * 24245450Sganbold * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25245450Sganbold * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26245450Sganbold * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27245450Sganbold * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28245450Sganbold * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29245450Sganbold * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30245450Sganbold * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31245450Sganbold * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32245450Sganbold * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33245450Sganbold * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34245450Sganbold * SUCH DAMAGE. 35245450Sganbold */ 36245450Sganbold 37245450Sganbold#ifndef lint 38245450Sganboldstatic char sccsid[] = "@(#)sprint.c 8.1 (Berkeley) 6/6/93"; 39245450Sganbold#endif /* not lint */ 40245450Sganbold 41245450Sganbold#include <sys/types.h> 42245450Sganbold#include <sys/time.h> 43245450Sganbold#include <time.h> 44245450Sganbold#include <db.h> 45245450Sganbold#include <pwd.h> 46245450Sganbold#include <errno.h> 47245450Sganbold#include <utmp.h> 48245450Sganbold#include <stdio.h> 49245450Sganbold#include <stdlib.h> 50254056Sganbold#include <string.h> 51254056Sganbold#include "finger.h" 52254056Sganbold 53254056Sganboldstatic void stimeprint __P((WHERE *)); 54254056Sganbold 55254056Sganboldvoid 56254056Sganboldsflag_print() 57245450Sganbold{ 58245450Sganbold extern time_t now; 59254056Sganbold extern int oflag; 60245450Sganbold register PERSON *pn; 61245450Sganbold register WHERE *w; 62245450Sganbold register int sflag, r, namelen; 63245450Sganbold char p[80]; 64245450Sganbold DBT data, key; 65245450Sganbold 66245450Sganbold /* 67245450Sganbold * short format -- 68245450Sganbold * login name 69245450Sganbold * real name 70 * terminal name (the XX of ttyXX) 71 * if terminal writeable (add an '*' to the terminal name 72 * if not) 73 * if logged in show idle time and day logged in, else 74 * show last login date and time. 75 * If > 6 months, show year instead of time. 76 * if (-o) 77 * office location 78 * office phone 79 * else 80 * remote host 81 */ 82#define MAXREALNAME 20 83#define MAXHOSTNAME 20 /* in reality, hosts are never longer than 16 */ 84 (void)printf("%-*s %-*s%s %s\n", UT_NAMESIZE, "Login", MAXREALNAME, 85 "Name", " TTY Idle Login Time", 86 oflag ? " Office Office Phone" : " Where"); 87 88 for (sflag = R_FIRST;; sflag = R_NEXT) { 89 r = (*db->seq)(db, &key, &data, sflag); 90 if (r == -1) 91 err("db seq: %s", strerror(errno)); 92 if (r == 1) 93 break; 94 pn = *(PERSON **)data.data; 95 96 for (w = pn->whead; w != NULL; w = w->next) { 97 namelen = MAXREALNAME; 98 if (w->info == LOGGEDIN && !w->writable) 99 --namelen; /* leave space before `*' */ 100 (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, 101 pn->name, MAXREALNAME, namelen, 102 pn->realname ? pn->realname : ""); 103 if (!w->loginat) { 104 (void)printf(" * * No logins "); 105 goto office; 106 } 107 (void)putchar(w->info == LOGGEDIN && !w->writable ? 108 '*' : ' '); 109 if (*w->tty) 110 (void)printf("%-3.3s ", 111 (strncmp(w->tty, "tty", 3) 112 && strncmp(w->tty, "cua", 3)) 113 ? w->tty : w->tty + 3); 114 else 115 (void)printf(" "); 116 if (w->info == LOGGEDIN) { 117 stimeprint(w); 118 (void)printf(" "); 119 } else 120 (void)printf(" * "); 121 strftime(p, sizeof(p), "%c", localtime(&w->loginat)); 122#define SECSPERDAY 86400 123#define DAYSPERWEEK 7 124#define DAYSPERNYEAR 365 125 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) 126 (void)printf("%.3s ", p); 127 else 128 (void)printf("%.6s", p + 4); 129 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) 130 (void)printf(" %.4s", p + 20); 131 else 132 (void)printf(" %.5s", p + 11); 133office: if (oflag) { 134 if (pn->office) 135 (void)printf(" %-10.10s", pn->office); 136 else if (pn->officephone) 137 (void)printf(" %-10.10s", " "); 138 if (pn->officephone) 139 (void)printf(" %-.15s", 140 prphone(pn->officephone)); 141 } else 142 (void)printf(" %.*s", MAXHOSTNAME, w->host); 143 putchar('\n'); 144 } 145 } 146} 147 148static void 149stimeprint(w) 150 WHERE *w; 151{ 152 register struct tm *delta; 153 154 delta = gmtime(&w->idletime); 155 if (!delta->tm_yday) 156 if (!delta->tm_hour) 157 if (!delta->tm_min) 158 (void)printf(" "); 159 else 160 (void)printf("%5d", delta->tm_min); 161 else 162 (void)printf("%2d:%02d", 163 delta->tm_hour, delta->tm_min); 164 else 165 (void)printf("%4dd", delta->tm_yday); 166} 167