Home | History | Annotate | Line # | Download | only in finger
sprint.c revision 1.7
      1  1.7      mrg /*	$NetBSD: sprint.c,v 1.7 1997/09/09 02:41:10 mrg Exp $	*/
      2  1.5      tls 
      3  1.1      cgd /*
      4  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      5  1.1      cgd  * 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.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  */
     38  1.1      cgd 
     39  1.1      cgd #ifndef lint
     40  1.2  mycroft /*static char sccsid[] = "from: @(#)sprint.c	5.8 (Berkeley) 12/4/90";*/
     41  1.7      mrg static char rcsid[] = "$NetBSD: sprint.c,v 1.7 1997/09/09 02:41:10 mrg Exp $";
     42  1.1      cgd #endif /* not lint */
     43  1.1      cgd 
     44  1.1      cgd #include <sys/types.h>
     45  1.1      cgd #include <sys/time.h>
     46  1.1      cgd #include <tzfile.h>
     47  1.1      cgd #include <stdio.h>
     48  1.4    lukem #include <stdlib.h>
     49  1.1      cgd #include "finger.h"
     50  1.4    lukem #include "extern.h"
     51  1.1      cgd 
     52  1.4    lukem void
     53  1.1      cgd sflag_print()
     54  1.1      cgd {
     55  1.4    lukem 	PERSON *pn;
     56  1.4    lukem 	WHERE *w;
     57  1.4    lukem 	int cnt;
     58  1.4    lukem 	char *p;
     59  1.4    lukem 	PERSON **list;
     60  1.1      cgd 
     61  1.1      cgd 	list = sort();
     62  1.1      cgd 	/*
     63  1.1      cgd 	 * short format --
     64  1.1      cgd 	 *	login name
     65  1.1      cgd 	 *	real name
     66  1.1      cgd 	 *	terminal name (the XX of ttyXX)
     67  1.1      cgd 	 *	if terminal writeable (add an '*' to the terminal name
     68  1.1      cgd 	 *		if not)
     69  1.1      cgd 	 *	if logged in show idle time and day logged in, else
     70  1.4    lukem 	 *		show last login date and time.  If > 6 months,
     71  1.4    lukem 	 *		show year instead of time.  If < 6 days,
     72  1.4    lukem 	 *		show day name instead of month & day.
     73  1.4    lukem 	 *	if -h given
     74  1.4    lukem 	 *		remote host
     75  1.4    lukem 	 *	else if -o given (overriding -h) (default)
     76  1.4    lukem 	 *		office location
     77  1.4    lukem 	 *		office phone
     78  1.1      cgd 	 */
     79  1.1      cgd #define	MAXREALNAME	20
     80  1.4    lukem #define	MAXHOSTNAME	20
     81  1.6  mycroft 	(void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
     82  1.7      mrg 	    "Name", "Tty  Idle  Login Time  ", (gflag) ? "" :
     83  1.4    lukem 	    (oflag) ? "Office     Office Phone" : "Where");
     84  1.1      cgd 	for (cnt = 0; cnt < entries; ++cnt) {
     85  1.1      cgd 		pn = list[cnt];
     86  1.1      cgd 		for (w = pn->whead; w != NULL; w = w->next) {
     87  1.1      cgd 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
     88  1.1      cgd 			    pn->name, MAXREALNAME, MAXREALNAME,
     89  1.1      cgd 			    pn->realname ? pn->realname : "");
     90  1.1      cgd 			if (!w->loginat) {
     91  1.1      cgd 				(void)printf("  *     *  No logins   ");
     92  1.1      cgd 				goto office;
     93  1.1      cgd 			}
     94  1.1      cgd 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
     95  1.1      cgd 			    '*' : ' ');
     96  1.1      cgd 			if (*w->tty)
     97  1.1      cgd 				(void)printf("%-2.2s ",
     98  1.1      cgd 				    w->tty[0] != 't' || w->tty[1] != 't' ||
     99  1.1      cgd 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
    100  1.1      cgd 			else
    101  1.1      cgd 				(void)printf("   ");
    102  1.1      cgd 			if (w->info == LOGGEDIN) {
    103  1.1      cgd 				stimeprint(w);
    104  1.1      cgd 				(void)printf("  ");
    105  1.1      cgd 			} else
    106  1.1      cgd 				(void)printf("    *  ");
    107  1.1      cgd 			p = ctime(&w->loginat);
    108  1.4    lukem 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
    109  1.6  mycroft 				(void)printf("   %.3s", p);
    110  1.4    lukem 			else
    111  1.4    lukem 				(void)printf("%.6s", p + 4);
    112  1.1      cgd 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
    113  1.6  mycroft 				(void)printf(" %.4s ", p + 20);
    114  1.1      cgd 			else
    115  1.1      cgd 				(void)printf(" %.5s", p + 11);
    116  1.4    lukem office:
    117  1.7      mrg 			if (gflag)
    118  1.7      mrg 				goto no_gecos;
    119  1.4    lukem 			putchar(' ');
    120  1.4    lukem 			if (oflag) {
    121  1.4    lukem 				if (pn->office)
    122  1.4    lukem 					(void)printf("%-10.10s", pn->office);
    123  1.4    lukem 				else if (pn->officephone)
    124  1.4    lukem 					(void)printf("%-10.10s", " ");
    125  1.4    lukem 				if (pn->officephone)
    126  1.4    lukem 					(void)printf(" %-.15s",
    127  1.4    lukem 						    prphone(pn->officephone));
    128  1.4    lukem 			} else
    129  1.4    lukem 				(void)printf("%.*s", MAXHOSTNAME, w->host);
    130  1.7      mrg no_gecos:
    131  1.1      cgd 			putchar('\n');
    132  1.1      cgd 		}
    133  1.1      cgd 	}
    134  1.1      cgd }
    135  1.1      cgd 
    136  1.1      cgd PERSON **
    137  1.1      cgd sort()
    138  1.1      cgd {
    139  1.4    lukem 	PERSON *pn, **lp;
    140  1.1      cgd 	PERSON **list;
    141  1.1      cgd 
    142  1.1      cgd 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
    143  1.1      cgd 		(void)fprintf(stderr, "finger: out of space.\n");
    144  1.1      cgd 		exit(1);
    145  1.1      cgd 	}
    146  1.1      cgd 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
    147  1.1      cgd 		*lp++ = pn;
    148  1.1      cgd 	(void)qsort(list, entries, sizeof(PERSON *), psort);
    149  1.1      cgd 	return(list);
    150  1.1      cgd }
    151  1.1      cgd 
    152  1.4    lukem int
    153  1.1      cgd psort(p, t)
    154  1.4    lukem 	const void *p, *t;
    155  1.1      cgd {
    156  1.4    lukem 	return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
    157  1.1      cgd }
    158  1.1      cgd 
    159  1.4    lukem void
    160  1.1      cgd stimeprint(w)
    161  1.1      cgd 	WHERE *w;
    162  1.1      cgd {
    163  1.4    lukem 	struct tm *delta;
    164  1.1      cgd 
    165  1.1      cgd 	delta = gmtime(&w->idletime);
    166  1.1      cgd 	if (!delta->tm_yday)
    167  1.1      cgd 		if (!delta->tm_hour)
    168  1.1      cgd 			if (!delta->tm_min)
    169  1.3  mycroft 				(void)printf("    -");
    170  1.1      cgd 			else
    171  1.1      cgd 				(void)printf("%5d", delta->tm_min);
    172  1.1      cgd 		else
    173  1.1      cgd 			(void)printf("%2d:%02d",
    174  1.1      cgd 			    delta->tm_hour, delta->tm_min);
    175  1.1      cgd 	else
    176  1.1      cgd 		(void)printf("%4dd", delta->tm_yday);
    177  1.1      cgd }
    178