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