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