1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)sprint.c 8.3 (Berkeley) 4/28/95 33 * $FreeBSD: src/usr.bin/finger/sprint.c,v 1.11.2.5 2002/07/03 01:14:24 des Exp $ 34 */ 35 36 #include <sys/param.h> 37 #include <sys/socket.h> 38 #include <db.h> 39 #include <err.h> 40 #include <langinfo.h> 41 #include <pwd.h> 42 #include <stdio.h> 43 #include <string.h> 44 #include <time.h> 45 #include "finger.h" 46 47 static void stimeprint(WHERE *); 48 49 void 50 sflag_print(void) 51 { 52 PERSON *pn; 53 WHERE *w; 54 int sflag, r, namelen; 55 char p[80]; 56 PERSON *tmp; 57 DBT data, key; 58 struct tm *lc; 59 60 if (d_first < 0) 61 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 62 /* 63 * short format -- 64 * login name 65 * real name 66 * terminal name (the XX of ttyXX) 67 * if terminal writeable (add an '*' to the terminal name 68 * if not) 69 * if logged in show idle time and day logged in, else 70 * show last login date and time. 71 * If > 6 months, show year instead of time. 72 * if (-o) 73 * office location 74 * office phone 75 * else 76 * remote host 77 */ 78 #define MAXREALNAME 20 79 #define MAXHOSTNAME 17 /* in reality, hosts are never longer than 16 */ 80 (void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME, 81 "Name", " TTY Idle Login Time ", (gflag) ? "" : 82 oflag ? "Office Phone" : "Where"); 83 84 for (sflag = R_FIRST;; sflag = R_NEXT) { 85 r = (*db->seq)(db, &key, &data, sflag); 86 if (r == -1) 87 err(1, "db seq"); 88 if (r == 1) 89 break; 90 memmove(&tmp, data.data, sizeof tmp); 91 pn = tmp; 92 93 for (w = pn->whead; w != NULL; w = w->next) { 94 namelen = MAXREALNAME; 95 if (w->info == LOGGEDIN && !w->writable) 96 --namelen; /* leave space before `*' */ 97 (void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME, 98 pn->name, MAXREALNAME, namelen, 99 pn->realname ? pn->realname : ""); 100 if (!w->loginat) { 101 (void)printf(" * * No logins "); 102 goto office; 103 } 104 (void)putchar(w->info == LOGGEDIN && !w->writable ? 105 '*' : ' '); 106 if (*w->tty) 107 (void)printf("%-3.3s ", 108 (strncmp(w->tty, "tty", 3) 109 && strncmp(w->tty, "cua", 3)) 110 ? w->tty : w->tty + 3); 111 else 112 (void)printf(" "); 113 if (w->info == LOGGEDIN) { 114 stimeprint(w); 115 (void)printf(" "); 116 } else 117 (void)printf(" * "); 118 lc = localtime(&w->loginat); 119 #define SECSPERDAY 86400 120 #define DAYSPERWEEK 7 121 #define DAYSPERNYEAR 365 122 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) { 123 (void)strftime(p, sizeof(p), "%a", lc); 124 } else { 125 (void)strftime(p, sizeof(p), 126 d_first ? "%e %b" : "%b %e", lc); 127 } 128 (void)printf("%-6.6s", p); 129 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) { 130 (void)strftime(p, sizeof(p), "%Y", lc); 131 } else { 132 (void)strftime(p, sizeof(p), "%R", lc); 133 } 134 (void)printf(" %-5.5s ", p); 135 office: 136 if (gflag) 137 goto no_gecos; 138 if (oflag) { 139 if (pn->office) 140 (void)printf(" %-7.7s", pn->office); 141 else if (pn->officephone) 142 (void)printf(" %-7.7s", " "); 143 if (pn->officephone) 144 (void)printf(" %-.9s", 145 prphone(pn->officephone)); 146 } else 147 (void)printf(" %.*s", MAXHOSTNAME, w->host); 148 no_gecos: 149 putchar('\n'); 150 } 151 } 152 } 153 154 static void 155 stimeprint(WHERE *w) 156 { 157 struct tm *delta; 158 159 delta = gmtime(&w->idletime); 160 if (!delta->tm_yday) 161 if (!delta->tm_hour) 162 if (!delta->tm_min) 163 (void)printf(" "); 164 else 165 (void)printf("%5d", delta->tm_min); 166 else 167 (void)printf("%2d:%02d", 168 delta->tm_hour, delta->tm_min); 169 else 170 (void)printf("%4dd", delta->tm_yday); 171 } 172