1*bd0fe39dSmillert /* $OpenBSD: sprint.c,v 1.14 2014/10/17 20:19:15 millert 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. 18f75387cbSmillert * 3. Neither the name of the University nor the names of its contributors 19df930be7Sderaadt * may be used to endorse or promote products derived from this software 20df930be7Sderaadt * without specific prior written permission. 21df930be7Sderaadt * 22df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df930be7Sderaadt * SUCH DAMAGE. 33df930be7Sderaadt */ 34df930be7Sderaadt 35df930be7Sderaadt #include <sys/types.h> 36df930be7Sderaadt #include <sys/time.h> 37df930be7Sderaadt #include <tzfile.h> 38df930be7Sderaadt #include <stdio.h> 39c74ee44fSkstailey #include <stdlib.h> 4070ef01f6Sdavid #include <string.h> 4124b00d2bSmickey #include <err.h> 42df930be7Sderaadt #include "finger.h" 43c74ee44fSkstailey #include "extern.h" 44df930be7Sderaadt 45c74ee44fSkstailey void 461837a5caSderaadt sflag_print(void) 47df930be7Sderaadt { 48c74ee44fSkstailey PERSON *pn; 49c74ee44fSkstailey WHERE *w; 50c74ee44fSkstailey int cnt; 51c74ee44fSkstailey char *p; 52c74ee44fSkstailey PERSON **list; 53df930be7Sderaadt 54df930be7Sderaadt list = sort(); 55df930be7Sderaadt /* 56df930be7Sderaadt * short format -- 57df930be7Sderaadt * login name 58df930be7Sderaadt * real name 59df930be7Sderaadt * terminal name (the XX of ttyXX) 60df930be7Sderaadt * if terminal writeable (add an '*' to the terminal name 61df930be7Sderaadt * if not) 62df930be7Sderaadt * if logged in show idle time and day logged in, else 63c74ee44fSkstailey * show last login date and time. If > 6 months, 64c74ee44fSkstailey * show year instead of time. If < 6 days, 65c74ee44fSkstailey * show day name instead of month & day. 66c74ee44fSkstailey * if -h given 67c74ee44fSkstailey * remote host 68c74ee44fSkstailey * else if -o given (overriding -h) (default) 69df930be7Sderaadt * office location 70df930be7Sderaadt * office phone 71df930be7Sderaadt */ 7235c79e0bSderaadt #define NAME_WIDTH 8 73df930be7Sderaadt #define MAXREALNAME 20 74c74ee44fSkstailey #define MAXHOSTNAME 20 7535c79e0bSderaadt (void)printf("%-*.*s %-*s %s %s\n", 7635c79e0bSderaadt NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME, 77c74ee44fSkstailey "Name", "Tty Idle Login Time ", 78c74ee44fSkstailey (oflag) ? "Office Office Phone" : "Where"); 79df930be7Sderaadt for (cnt = 0; cnt < entries; ++cnt) { 80df930be7Sderaadt pn = list[cnt]; 81df930be7Sderaadt for (w = pn->whead; w != NULL; w = w->next) { 8235c79e0bSderaadt (void)printf("%-*.*s %-*.*s ", 83*bd0fe39dSmillert NAME_WIDTH, UT_NAMESIZE, pn->name, 8435c79e0bSderaadt MAXREALNAME, MAXREALNAME, 85*bd0fe39dSmillert pn->realname ? pn->realname : ""); 86df930be7Sderaadt if (!w->loginat) { 87df930be7Sderaadt (void)printf(" * * No logins "); 88df930be7Sderaadt goto office; 89df930be7Sderaadt } 90df930be7Sderaadt (void)putchar(w->info == LOGGEDIN && !w->writable ? 91df930be7Sderaadt '*' : ' '); 92df930be7Sderaadt if (*w->tty) 93df930be7Sderaadt (void)printf("%-2.2s ", 94df930be7Sderaadt w->tty[0] != 't' || w->tty[1] != 't' || 95df930be7Sderaadt w->tty[2] != 'y' ? w->tty : w->tty + 3); 96df930be7Sderaadt else 97df930be7Sderaadt (void)printf(" "); 98df930be7Sderaadt if (w->info == LOGGEDIN) { 99df930be7Sderaadt stimeprint(w); 100df930be7Sderaadt (void)printf(" "); 101df930be7Sderaadt } else 102df930be7Sderaadt (void)printf(" * "); 103df930be7Sderaadt p = ctime(&w->loginat); 104c74ee44fSkstailey if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) 105c74ee44fSkstailey (void)printf(" %.3s", p); 106c74ee44fSkstailey else 107df930be7Sderaadt (void)printf("%.6s", p + 4); 108df930be7Sderaadt if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) 109df930be7Sderaadt (void)printf(" %.4s ", p + 20); 110df930be7Sderaadt else 111df930be7Sderaadt (void)printf(" %.5s", p + 11); 112c74ee44fSkstailey office: 113c74ee44fSkstailey putchar(' '); 114c74ee44fSkstailey if (oflag) { 115c74ee44fSkstailey if (pn->office) 116*bd0fe39dSmillert (void)printf("%-10.10s", pn->office); 117df930be7Sderaadt else if (pn->officephone) 118df930be7Sderaadt (void)printf("%-10.10s", " "); 119df930be7Sderaadt if (pn->officephone) 120df930be7Sderaadt (void)printf(" %-.15s", 121*bd0fe39dSmillert prphone(pn->officephone)); 122c74ee44fSkstailey } else 123c74ee44fSkstailey (void)printf("%.*s", MAXHOSTNAME, w->host); 124df930be7Sderaadt putchar('\n'); 125df930be7Sderaadt } 126df930be7Sderaadt } 127df930be7Sderaadt } 128df930be7Sderaadt 129df930be7Sderaadt PERSON ** 1301837a5caSderaadt sort(void) 131df930be7Sderaadt { 132c74ee44fSkstailey PERSON *pn, **lp; 133df930be7Sderaadt PERSON **list; 134df930be7Sderaadt 1351ed98fdfSderaadt if (!(list = (PERSON **)calloc((u_int)entries, sizeof(PERSON *)))) 13624b00d2bSmickey err(1, "malloc"); 137df930be7Sderaadt for (lp = list, pn = phead; pn != NULL; pn = pn->next) 138df930be7Sderaadt *lp++ = pn; 139df930be7Sderaadt (void)qsort(list, entries, sizeof(PERSON *), psort); 140df930be7Sderaadt return (list); 141df930be7Sderaadt } 142df930be7Sderaadt 143c74ee44fSkstailey int 1441837a5caSderaadt psort(const void *p, const void *t) 145df930be7Sderaadt { 146c74ee44fSkstailey return (strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name)); 147df930be7Sderaadt } 148df930be7Sderaadt 149c74ee44fSkstailey void 1501837a5caSderaadt stimeprint(WHERE *w) 151df930be7Sderaadt { 152c74ee44fSkstailey struct tm *delta; 153df930be7Sderaadt 154df930be7Sderaadt delta = gmtime(&w->idletime); 155523b9b38Stedu if (!delta->tm_yday) { 156523b9b38Stedu if (!delta->tm_hour) { 157df930be7Sderaadt if (!delta->tm_min) 158df930be7Sderaadt (void)printf(" -"); 159df930be7Sderaadt else 160df930be7Sderaadt (void)printf("%5d", delta->tm_min); 161523b9b38Stedu } else { 162df930be7Sderaadt (void)printf("%2d:%02d", 163df930be7Sderaadt delta->tm_hour, delta->tm_min); 164523b9b38Stedu } 165523b9b38Stedu } else 166df930be7Sderaadt (void)printf("%4dd", delta->tm_yday); 167df930be7Sderaadt } 168