1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/types.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 <utmpx.h> 46 #include "finger.h" 47 48 static void stimeprint(WHERE *); 49 50 void 51 sflag_print(void) 52 { 53 PERSON *pn; 54 WHERE *w; 55 int sflag, r, namelen; 56 char p[80]; 57 PERSON *tmp; 58 DBT data, key; 59 struct tm *lc; 60 61 if (d_first < 0) 62 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 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. 72 * If > 6 months, show year instead of time. 73 * if (-o) 74 * office location 75 * office phone 76 * else 77 * remote host 78 */ 79 #define MAXREALNAME 16 80 #define MAXHOSTNAME 17 /* in reality, hosts are never longer than 16 */ 81 (void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME, 82 "Name", " TTY Idle Login Time ", (gflag) ? "" : 83 oflag ? "Office Phone" : "Where"); 84 85 for (sflag = R_FIRST;; sflag = R_NEXT) { 86 r = (*db->seq)(db, &key, &data, sflag); 87 if (r == -1) 88 err(1, "db seq"); 89 if (r == 1) 90 break; 91 memmove(&tmp, data.data, sizeof tmp); 92 pn = tmp; 93 94 for (w = pn->whead; w != NULL; w = w->next) { 95 namelen = MAXREALNAME; 96 if (w->info == LOGGEDIN && !w->writable) 97 --namelen; /* leave space before `*' */ 98 (void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME, 99 pn->name, MAXREALNAME, namelen, 100 pn->realname ? pn->realname : ""); 101 if (!w->loginat) { 102 (void)printf(" * * No logins "); 103 goto office; 104 } 105 (void)putchar(w->info == LOGGEDIN && !w->writable ? 106 '*' : ' '); 107 if (*w->tty) 108 (void)printf("%-7.7s ", 109 (strncmp(w->tty, "tty", 3) 110 && strncmp(w->tty, "cua", 3)) 111 ? w->tty : w->tty + 3); 112 else 113 (void)printf(" "); 114 if (w->info == LOGGEDIN) { 115 stimeprint(w); 116 (void)printf(" "); 117 } else 118 (void)printf(" * "); 119 lc = localtime(&w->loginat); 120 #define SECSPERDAY 86400 121 #define DAYSPERWEEK 7 122 #define DAYSPERNYEAR 365 123 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) { 124 (void)strftime(p, sizeof(p), "%a", lc); 125 } else { 126 (void)strftime(p, sizeof(p), 127 d_first ? "%e %b" : "%b %e", lc); 128 } 129 (void)printf("%-6.6s", p); 130 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) { 131 (void)strftime(p, sizeof(p), "%Y", lc); 132 } else { 133 (void)strftime(p, sizeof(p), "%R", lc); 134 } 135 (void)printf(" %-5.5s", p); 136 office: 137 if (gflag) 138 goto no_gecos; 139 if (oflag) { 140 if (pn->office) 141 (void)printf(" %-7.7s", pn->office); 142 else if (pn->officephone) 143 (void)printf(" %-7.7s", " "); 144 if (pn->officephone) 145 (void)printf(" %-.15s", 146 prphone(pn->officephone)); 147 } else 148 (void)printf(" %.*s", MAXHOSTNAME, w->host); 149 no_gecos: 150 putchar('\n'); 151 } 152 } 153 } 154 155 static void 156 stimeprint(WHERE *w) 157 { 158 struct tm *delta; 159 160 if (w->idletime == -1) { 161 (void)printf(" "); 162 return; 163 } 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