xref: /original-bsd/usr.bin/finger/sprint.c (revision 65d10654)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 04/28/95";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <tzfile.h>
19 #include <db.h>
20 #include <err.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <utmp.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "finger.h"
28 
29 static void	  stimeprint __P((WHERE *));
30 
31 void
32 sflag_print()
33 {
34 	extern time_t now;
35 	register PERSON *pn;
36 	register WHERE *w;
37 	register int sflag, r;
38 	register char *p;
39 	PERSON *tmp;
40 	DBT data, key;
41 
42 	/*
43 	 * short format --
44 	 *	login name
45 	 *	real name
46 	 *	terminal name (the XX of ttyXX)
47 	 *	if terminal writeable (add an '*' to the terminal name
48 	 *		if not)
49 	 *	if logged in show idle time and day logged in, else
50 	 *		show last login date and time.  If > 6 moths,
51 	 *		show year instead of time.
52 	 *	office location
53 	 *	office phone
54 	 */
55 #define	MAXREALNAME	20
56 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
57 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
58 
59 	for (sflag = R_FIRST;; sflag = R_NEXT) {
60 		r = (*db->seq)(db, &key, &data, sflag);
61 		if (r == -1)
62 			err(1, "db seq");
63 		if (r == 1)
64 			break;
65 		memmove(&tmp, data.data, sizeof tmp);
66 		pn = tmp;
67 
68 		for (w = pn->whead; w != NULL; w = w->next) {
69 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
70 			    pn->name, MAXREALNAME, MAXREALNAME,
71 			    pn->realname ? pn->realname : "");
72 			if (!w->loginat) {
73 				(void)printf("  *     *  No logins   ");
74 				goto office;
75 			}
76 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
77 			    '*' : ' ');
78 			if (*w->tty)
79 				(void)printf("%-2.2s ",
80 				    w->tty[0] != 't' || w->tty[1] != 't' ||
81 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
82 			else
83 				(void)printf("   ");
84 			if (w->info == LOGGEDIN) {
85 				stimeprint(w);
86 				(void)printf("  ");
87 			} else
88 				(void)printf("    *  ");
89 			p = ctime(&w->loginat);
90 			(void)printf("%.6s", p + 4);
91 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
92 				(void)printf("  %.4s", p + 20);
93 			else
94 				(void)printf(" %.5s", p + 11);
95 office:			if (pn->office)
96 				(void)printf(" %-10.10s", pn->office);
97 			else if (pn->officephone)
98 				(void)printf(" %-10.10s", " ");
99 			if (pn->officephone)
100 				(void)printf(" %-.15s",
101 				    prphone(pn->officephone));
102 			putchar('\n');
103 		}
104 	}
105 }
106 
107 static void
108 stimeprint(w)
109 	WHERE *w;
110 {
111 	register struct tm *delta;
112 
113 	delta = gmtime(&w->idletime);
114 	if (!delta->tm_yday)
115 		if (!delta->tm_hour)
116 			if (!delta->tm_min)
117 				(void)printf("     ");
118 			else
119 				(void)printf("%5d", delta->tm_min);
120 		else
121 			(void)printf("%2d:%02d",
122 			    delta->tm_hour, delta->tm_min);
123 	else
124 		(void)printf("%4dd", delta->tm_yday);
125 }
126