xref: /original-bsd/usr.bin/finger/sprint.c (revision a1c2194a)
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.9 (Berkeley) 07/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 <pwd.h>
20 #include <utmp.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "finger.h"
25 
26 static int	  psort __P((const void *, const void *));
27 static PERSON	**sort __P((void));
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 cnt;
37 	register char *p;
38 	PERSON **list;
39 
40 	list = sort();
41 	/*
42 	 * short format --
43 	 *	login name
44 	 *	real name
45 	 *	terminal name (the XX of ttyXX)
46 	 *	if terminal writeable (add an '*' to the terminal name
47 	 *		if not)
48 	 *	if logged in show idle time and day logged in, else
49 	 *		show last login date and time.  If > 6 moths,
50 	 *		show year instead of time.
51 	 *	office location
52 	 *	office phone
53 	 */
54 #define	MAXREALNAME	20
55 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
56 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
57 	for (cnt = 0; cnt < entries; ++cnt) {
58 		pn = list[cnt];
59 		for (w = pn->whead; w != NULL; w = w->next) {
60 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
61 			    pn->name, MAXREALNAME, MAXREALNAME,
62 			    pn->realname ? pn->realname : "");
63 			if (!w->loginat) {
64 				(void)printf("  *     *  No logins   ");
65 				goto office;
66 			}
67 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
68 			    '*' : ' ');
69 			if (*w->tty)
70 				(void)printf("%-2.2s ",
71 				    w->tty[0] != 't' || w->tty[1] != 't' ||
72 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
73 			else
74 				(void)printf("   ");
75 			if (w->info == LOGGEDIN) {
76 				stimeprint(w);
77 				(void)printf("  ");
78 			} else
79 				(void)printf("    *  ");
80 			p = ctime(&w->loginat);
81 			(void)printf("%.6s", p + 4);
82 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
83 				(void)printf("  %.4s", p + 20);
84 			else
85 				(void)printf(" %.5s", p + 11);
86 office:			if (pn->office)
87 				(void)printf(" %-10.10s", pn->office);
88 			else if (pn->officephone)
89 				(void)printf(" %-10.10s", " ");
90 			if (pn->officephone)
91 				(void)printf(" %-.15s",
92 				    prphone(pn->officephone));
93 			putchar('\n');
94 		}
95 	}
96 }
97 
98 static PERSON **
99 sort()
100 {
101 	register PERSON *pn, **lp;
102 	PERSON **list;
103 
104 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
105 		(void)fprintf(stderr, "finger: out of space.\n");
106 		exit(1);
107 	}
108 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
109 		*lp++ = pn;
110 	(void)qsort(list, entries, sizeof(PERSON *), psort);
111 	return(list);
112 }
113 
114 
115 psort(a, b)
116 	const void *a, *b;
117 {
118 	return(strcmp(((PERSON *)a)->name, ((PERSON *)b)->name));
119 }
120 
121 static void
122 stimeprint(w)
123 	WHERE *w;
124 {
125 	register struct tm *delta;
126 
127 	delta = gmtime(&w->idletime);
128 	if (!delta->tm_yday)
129 		if (!delta->tm_hour)
130 			if (!delta->tm_min)
131 				(void)printf("     ");
132 			else
133 				(void)printf("%5d", delta->tm_min);
134 		else
135 			(void)printf("%2d:%02d",
136 			    delta->tm_hour, delta->tm_min);
137 	else
138 		(void)printf("%4dd", delta->tm_yday);
139 }
140