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