sprint.c revision 202191
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 3787628Sdwmalone#if 0 3887628Sdwmalone#ifndef lint 3987628Sdwmalonestatic char sccsid[] = "@(#)sprint.c 8.3 (Berkeley) 4/28/95"; 4087628Sdwmalone#endif 4187628Sdwmalone#endif 4287628Sdwmalone 4387229Smarkm#include <sys/cdefs.h> 4487229Smarkm__FBSDID("$FreeBSD: head/usr.bin/finger/sprint.c 202191 2010-01-13 17:50:58Z ed $"); 4587229Smarkm 46201140Sed#include <sys/param.h> 47102944Sdwmalone#include <sys/types.h> 48102944Sdwmalone#include <sys/socket.h> 491590Srgrimes#include <db.h> 5023693Speter#include <err.h> 5174586Sache#include <langinfo.h> 521590Srgrimes#include <pwd.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <string.h> 5572109Scharnier#include <time.h> 56202191Sed#include <utmpx.h> 571590Srgrimes#include "finger.h" 581590Srgrimes 5992920Simpstatic void stimeprint(WHERE *); 601590Srgrimes 611590Srgrimesvoid 62102944Sdwmalonesflag_print(void) 631590Srgrimes{ 6487229Smarkm PERSON *pn; 6587229Smarkm WHERE *w; 6687229Smarkm int sflag, r, namelen; 679993Sache char p[80]; 6823693Speter PERSON *tmp; 691590Srgrimes DBT data, key; 7074586Sache struct tm *lc; 711590Srgrimes 7274586Sache if (d_first < 0) 7374586Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 741590Srgrimes /* 751590Srgrimes * short format -- 761590Srgrimes * login name 771590Srgrimes * real name 781590Srgrimes * terminal name (the XX of ttyXX) 791590Srgrimes * if terminal writeable (add an '*' to the terminal name 801590Srgrimes * if not) 811590Srgrimes * if logged in show idle time and day logged in, else 822537Spst * show last login date and time. 832537Spst * If > 6 months, show year instead of time. 842537Spst * if (-o) 852537Spst * office location 862537Spst * office phone 872537Spst * else 882537Spst * remote host 891590Srgrimes */ 90156282Srwatson#define MAXREALNAME 16 9120157Sache#define MAXHOSTNAME 17 /* in reality, hosts are never longer than 16 */ 92201140Sed (void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME, 93156282Srwatson "Name", " TTY Idle Login Time ", (gflag) ? "" : 9474586Sache oflag ? "Office Phone" : "Where"); 951590Srgrimes 961590Srgrimes for (sflag = R_FIRST;; sflag = R_NEXT) { 971590Srgrimes r = (*db->seq)(db, &key, &data, sflag); 981590Srgrimes if (r == -1) 9923693Speter err(1, "db seq"); 1001590Srgrimes if (r == 1) 1011590Srgrimes break; 10223693Speter memmove(&tmp, data.data, sizeof tmp); 10323693Speter pn = tmp; 1041590Srgrimes 1051590Srgrimes for (w = pn->whead; w != NULL; w = w->next) { 10611386Speter namelen = MAXREALNAME; 10711386Speter if (w->info == LOGGEDIN && !w->writable) 10811386Speter --namelen; /* leave space before `*' */ 109201140Sed (void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME, 11011386Speter pn->name, MAXREALNAME, namelen, 11111386Speter pn->realname ? pn->realname : ""); 1121590Srgrimes if (!w->loginat) { 11363007Sasmodai (void)printf(" * * No logins "); 1141590Srgrimes goto office; 1151590Srgrimes } 1161590Srgrimes (void)putchar(w->info == LOGGEDIN && !w->writable ? 1171590Srgrimes '*' : ' '); 1181590Srgrimes if (*w->tty) 119156282Srwatson (void)printf("%-7.7s ", 1207371Sjoerg (strncmp(w->tty, "tty", 3) 1217371Sjoerg && strncmp(w->tty, "cua", 3)) 1227371Sjoerg ? w->tty : w->tty + 3); 1231590Srgrimes else 124156282Srwatson (void)printf(" "); 1251590Srgrimes if (w->info == LOGGEDIN) { 1261590Srgrimes stimeprint(w); 1271590Srgrimes (void)printf(" "); 1281590Srgrimes } else 1291590Srgrimes (void)printf(" * "); 13074586Sache lc = localtime(&w->loginat); 1319987Swollman#define SECSPERDAY 86400 1329987Swollman#define DAYSPERWEEK 7 1339987Swollman#define DAYSPERNYEAR 365 13474586Sache if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) { 13574586Sache (void)strftime(p, sizeof(p), "%a", lc); 13674586Sache } else { 13774586Sache (void)strftime(p, sizeof(p), 13874586Sache d_first ? "%e %b" : "%b %e", lc); 13974586Sache } 14074586Sache (void)printf("%-6.6s", p); 14174586Sache if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) { 14274586Sache (void)strftime(p, sizeof(p), "%Y", lc); 14374586Sache } else { 14474586Sache (void)strftime(p, sizeof(p), "%R", lc); 14574586Sache } 14674586Sache (void)printf(" %-5.5s", p); 14799249Sminioffice: 14899249Smini if (gflag) 14999249Smini goto no_gecos; 15099249Smini if (oflag) { 1512537Spst if (pn->office) 15220157Sache (void)printf(" %-7.7s", pn->office); 15320157Sache else if (pn->officephone) 15420157Sache (void)printf(" %-7.7s", " "); 15520157Sache if (pn->officephone) 15620157Sache (void)printf(" %-.9s", 15720157Sache prphone(pn->officephone)); 1582537Spst } else 1592537Spst (void)printf(" %.*s", MAXHOSTNAME, w->host); 16099249Sminino_gecos: 1611590Srgrimes putchar('\n'); 1621590Srgrimes } 1631590Srgrimes } 1641590Srgrimes} 1651590Srgrimes 1661590Srgrimesstatic void 167102944Sdwmalonestimeprint(WHERE *w) 1681590Srgrimes{ 16987229Smarkm struct tm *delta; 1701590Srgrimes 171112987Srwatson if (w->idletime == -1) { 172112987Srwatson (void)printf(" "); 173112987Srwatson return; 174112987Srwatson } 175112987Srwatson 1761590Srgrimes delta = gmtime(&w->idletime); 1771590Srgrimes if (!delta->tm_yday) 1781590Srgrimes if (!delta->tm_hour) 1791590Srgrimes if (!delta->tm_min) 1801590Srgrimes (void)printf(" "); 1811590Srgrimes else 1821590Srgrimes (void)printf("%5d", delta->tm_min); 1831590Srgrimes else 1841590Srgrimes (void)printf("%2d:%02d", 1851590Srgrimes delta->tm_hour, delta->tm_min); 1861590Srgrimes else 1871590Srgrimes (void)printf("%4dd", delta->tm_yday); 1881590Srgrimes} 189