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