1*c74ee44fSkstailey /* $OpenBSD: sprint.c,v 1.3 1997/05/30 23:35:52 kstailey Exp $ */ 21258a77dSderaadt 3df930be7Sderaadt /* 4df930be7Sderaadt * Copyright (c) 1989 The Regents of the University of California. 5df930be7Sderaadt * All rights reserved. 6df930be7Sderaadt * 7df930be7Sderaadt * This code is derived from software contributed to Berkeley by 8df930be7Sderaadt * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 9df930be7Sderaadt * 10df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 11df930be7Sderaadt * modification, are permitted provided that the following conditions 12df930be7Sderaadt * are met: 13df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 14df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 15df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 16df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 17df930be7Sderaadt * documentation and/or other materials provided with the distribution. 18df930be7Sderaadt * 3. All advertising materials mentioning features or use of this software 19df930be7Sderaadt * must display the following acknowledgement: 20df930be7Sderaadt * This product includes software developed by the University of 21df930be7Sderaadt * California, Berkeley and its contributors. 22df930be7Sderaadt * 4. Neither the name of the University nor the names of its contributors 23df930be7Sderaadt * may be used to endorse or promote products derived from this software 24df930be7Sderaadt * without specific prior written permission. 25df930be7Sderaadt * 26df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df930be7Sderaadt * SUCH DAMAGE. 37df930be7Sderaadt */ 38df930be7Sderaadt 39df930be7Sderaadt #ifndef lint 40df930be7Sderaadt /*static char sccsid[] = "from: @(#)sprint.c 5.8 (Berkeley) 12/4/90";*/ 41*c74ee44fSkstailey static char rcsid[] = "$OpenBSD: sprint.c,v 1.3 1997/05/30 23:35:52 kstailey Exp $"; 42df930be7Sderaadt #endif /* not lint */ 43df930be7Sderaadt 44df930be7Sderaadt #include <sys/types.h> 45df930be7Sderaadt #include <sys/time.h> 46df930be7Sderaadt #include <tzfile.h> 47df930be7Sderaadt #include <stdio.h> 48*c74ee44fSkstailey #include <stdlib.h> 49df930be7Sderaadt #include "finger.h" 50*c74ee44fSkstailey #include "extern.h" 51df930be7Sderaadt 52*c74ee44fSkstailey void 53df930be7Sderaadt sflag_print() 54df930be7Sderaadt { 55*c74ee44fSkstailey PERSON *pn; 56*c74ee44fSkstailey WHERE *w; 57*c74ee44fSkstailey int cnt; 58*c74ee44fSkstailey char *p; 59*c74ee44fSkstailey PERSON **list; 60df930be7Sderaadt 61df930be7Sderaadt list = sort(); 62df930be7Sderaadt /* 63df930be7Sderaadt * short format -- 64df930be7Sderaadt * login name 65df930be7Sderaadt * real name 66df930be7Sderaadt * terminal name (the XX of ttyXX) 67df930be7Sderaadt * if terminal writeable (add an '*' to the terminal name 68df930be7Sderaadt * if not) 69df930be7Sderaadt * if logged in show idle time and day logged in, else 70*c74ee44fSkstailey * show last login date and time. If > 6 months, 71*c74ee44fSkstailey * show year instead of time. If < 6 days, 72*c74ee44fSkstailey * show day name instead of month & day. 73*c74ee44fSkstailey * if -h given 74*c74ee44fSkstailey * remote host 75*c74ee44fSkstailey * else if -o given (overriding -h) (default) 76df930be7Sderaadt * office location 77df930be7Sderaadt * office phone 78df930be7Sderaadt */ 79df930be7Sderaadt #define MAXREALNAME 20 80*c74ee44fSkstailey #define MAXHOSTNAME 20 81*c74ee44fSkstailey (void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME, 82*c74ee44fSkstailey "Name", "Tty Idle Login Time ", 83*c74ee44fSkstailey (oflag) ? "Office Office Phone" : "Where"); 84df930be7Sderaadt for (cnt = 0; cnt < entries; ++cnt) { 85df930be7Sderaadt pn = list[cnt]; 86df930be7Sderaadt for (w = pn->whead; w != NULL; w = w->next) { 87df930be7Sderaadt (void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE, 88df930be7Sderaadt pn->name, MAXREALNAME, MAXREALNAME, 89df930be7Sderaadt pn->realname ? pn->realname : ""); 90df930be7Sderaadt if (!w->loginat) { 91df930be7Sderaadt (void)printf(" * * No logins "); 92df930be7Sderaadt goto office; 93df930be7Sderaadt } 94df930be7Sderaadt (void)putchar(w->info == LOGGEDIN && !w->writable ? 95df930be7Sderaadt '*' : ' '); 96df930be7Sderaadt if (*w->tty) 97df930be7Sderaadt (void)printf("%-2.2s ", 98df930be7Sderaadt w->tty[0] != 't' || w->tty[1] != 't' || 99df930be7Sderaadt w->tty[2] != 'y' ? w->tty : w->tty + 3); 100df930be7Sderaadt else 101df930be7Sderaadt (void)printf(" "); 102df930be7Sderaadt if (w->info == LOGGEDIN) { 103df930be7Sderaadt stimeprint(w); 104df930be7Sderaadt (void)printf(" "); 105df930be7Sderaadt } else 106df930be7Sderaadt (void)printf(" * "); 107df930be7Sderaadt p = ctime(&w->loginat); 108*c74ee44fSkstailey if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) 109*c74ee44fSkstailey (void)printf(" %.3s", p); 110*c74ee44fSkstailey else 111df930be7Sderaadt (void)printf("%.6s", p + 4); 112df930be7Sderaadt if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) 113df930be7Sderaadt (void)printf(" %.4s ", p + 20); 114df930be7Sderaadt else 115df930be7Sderaadt (void)printf(" %.5s", p + 11); 116*c74ee44fSkstailey office: 117*c74ee44fSkstailey putchar(' '); 118*c74ee44fSkstailey if (oflag) { 119*c74ee44fSkstailey if (pn->office) 120df930be7Sderaadt (void)printf("%-10.10s", pn->office); 121df930be7Sderaadt else if (pn->officephone) 122df930be7Sderaadt (void)printf("%-10.10s", " "); 123df930be7Sderaadt if (pn->officephone) 124df930be7Sderaadt (void)printf(" %-.15s", 125df930be7Sderaadt prphone(pn->officephone)); 126*c74ee44fSkstailey } else 127*c74ee44fSkstailey (void)printf("%.*s", MAXHOSTNAME, w->host); 128df930be7Sderaadt putchar('\n'); 129df930be7Sderaadt } 130df930be7Sderaadt } 131df930be7Sderaadt } 132df930be7Sderaadt 133df930be7Sderaadt PERSON ** 134df930be7Sderaadt sort() 135df930be7Sderaadt { 136*c74ee44fSkstailey PERSON *pn, **lp; 137df930be7Sderaadt PERSON **list; 138df930be7Sderaadt 139df930be7Sderaadt if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) { 140df930be7Sderaadt (void)fprintf(stderr, "finger: out of space.\n"); 141df930be7Sderaadt exit(1); 142df930be7Sderaadt } 143df930be7Sderaadt for (lp = list, pn = phead; pn != NULL; pn = pn->next) 144df930be7Sderaadt *lp++ = pn; 145df930be7Sderaadt (void)qsort(list, entries, sizeof(PERSON *), psort); 146df930be7Sderaadt return(list); 147df930be7Sderaadt } 148df930be7Sderaadt 149*c74ee44fSkstailey int 150df930be7Sderaadt psort(p, t) 151*c74ee44fSkstailey const void *p, *t; 152df930be7Sderaadt { 153*c74ee44fSkstailey return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name)); 154df930be7Sderaadt } 155df930be7Sderaadt 156*c74ee44fSkstailey void 157df930be7Sderaadt stimeprint(w) 158df930be7Sderaadt WHERE *w; 159df930be7Sderaadt { 160*c74ee44fSkstailey struct tm *delta; 161df930be7Sderaadt 162df930be7Sderaadt delta = gmtime(&w->idletime); 163df930be7Sderaadt if (!delta->tm_yday) 164df930be7Sderaadt if (!delta->tm_hour) 165df930be7Sderaadt if (!delta->tm_min) 166df930be7Sderaadt (void)printf(" -"); 167df930be7Sderaadt else 168df930be7Sderaadt (void)printf("%5d", delta->tm_min); 169df930be7Sderaadt else 170df930be7Sderaadt (void)printf("%2d:%02d", 171df930be7Sderaadt delta->tm_hour, delta->tm_min); 172df930be7Sderaadt else 173df930be7Sderaadt (void)printf("%4dd", delta->tm_yday); 174df930be7Sderaadt } 175