sprint.c revision 1.12
1/* $NetBSD: sprint.c,v 1.12 2002/08/05 08:04:03 tron Exp $ */ 2 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. 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#include <sys/cdefs.h> 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)sprint.c 8.3 (Berkeley) 4/28/95"; 43#else 44__RCSID("$NetBSD: sprint.c,v 1.12 2002/08/05 08:04:03 tron Exp $"); 45#endif 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/time.h> 50 51#include <time.h> 52#include <tzfile.h> 53#include <db.h> 54#include <err.h> 55#include <pwd.h> 56#include <errno.h> 57#include <utmp.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61 62#include "utmpentry.h" 63 64#include "finger.h" 65#include "extern.h" 66 67static void stimeprint __P((WHERE *)); 68 69void 70sflag_print() 71{ 72 PERSON *pn; 73 WHERE *w; 74 int sflag, r; 75 char *p; 76 PERSON *tmp; 77 DBT data, key; 78 79 if (db == NULL) 80 return; 81 82 /* 83 * short format -- 84 * login name 85 * real name 86 * terminal name (the XX of ttyXX) 87 * if terminal writeable (add an '*' to the terminal name 88 * if not) 89 * if logged in show idle time and day logged in, else 90 * show last login date and time. If > 6 months, 91 * show year instead of time. If < 6 days, 92 * show day name instead of month & day. 93 * if -h given 94 * remote host 95 * else if -o given (overriding -h) (default) 96 * office location 97 * office phone 98 */ 99#define MAXREALNAME 20 100 (void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME, 101 "Name", "Tty Idle Login Time ", (gflag) ? "" : 102 (oflag) ? "Office Office Phone" : "Where"); 103 104 for (sflag = R_FIRST;; sflag = R_NEXT) { 105 r = (*db->seq)(db, &key, &data, sflag); 106 if (r == -1) 107 err(1, "db seq"); 108 if (r == 1) 109 break; 110 memmove(&tmp, data.data, sizeof tmp); 111 pn = tmp; 112 113 for (w = pn->whead; w != NULL; w = w->next) { 114 (void)printf("%-*.*s %-*.*s ", (int)UT_NAMESIZE, 115 (int)UT_NAMESIZE, 116 pn->name, MAXREALNAME, MAXREALNAME, 117 pn->realname ? pn->realname : ""); 118 if (!w->loginat) { 119 (void)printf(" * * No logins "); 120 goto office; 121 } 122 (void)putchar(w->info == LOGGEDIN && !w->writable ? 123 '*' : ' '); 124 if (*w->tty) 125 (void)printf("%-2.2s ", 126 w->tty[0] != 't' || w->tty[1] != 't' || 127 w->tty[2] != 'y' ? w->tty : w->tty + 3); 128 else 129 (void)printf(" "); 130 if (w->info == LOGGEDIN) { 131 stimeprint(w); 132 (void)printf(" "); 133 } else 134 (void)printf(" * "); 135 p = ctime(&w->loginat); 136 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) 137 (void)printf("%.3s %-8.5s", p, p + 11); 138 else if (now - w->loginat 139 < SECSPERDAY * DAYSPERNYEAR / 2) 140 (void)printf("%.6s %-5.5s", p + 4, p + 11); 141 else 142 (void)printf("%.6s %-5.4s", p + 4, p + 20); 143office: 144 if (gflag) 145 goto no_gecos; 146 putchar(' '); 147 if (oflag) { 148 if (pn->office) 149 (void)printf("%-10.10s", pn->office); 150 else if (pn->officephone) 151 (void)printf("%-10.10s", " "); 152 if (pn->officephone) 153 (void)printf(" %-.15s", 154 prphone(pn->officephone)); 155 } else 156 (void)printf("%.*s", MAXHOSTNAMELEN, w->host); 157no_gecos: 158 putchar('\n'); 159 } 160 } 161} 162 163static void 164stimeprint(w) 165 WHERE *w; 166{ 167 struct tm *delta; 168 169 delta = gmtime(&w->idletime); 170 if (!delta->tm_yday) { 171 if (!delta->tm_hour) { 172 if (!delta->tm_min) 173 (void)printf(" -"); 174 else 175 (void)printf("%5d", delta->tm_min); 176 } else 177 (void)printf("%2d:%02d", 178 delta->tm_hour, delta->tm_min); 179 } else 180 (void)printf("%4dd", delta->tm_yday); 181} 182