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