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