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