xref: /original-bsd/usr.bin/finger/sprint.c (revision 9a77813a)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)sprint.c	5.3 (Berkeley) 05/08/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <tzfile.h>
25 #include <stdio.h>
26 #include "finger.h"
27 
28 extern int entries;
29 
30 sflag_print()
31 {
32 	extern time_t now;
33 	register PERSON *pn;
34 	register WHERE *w;
35 	register int cnt;
36 	register char *p;
37 	PERSON **list, **sort();
38 	time_t time();
39 	char *ctime();
40 
41 	list = sort();
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        Office      Office Phone");
58 	for (cnt = 0; cnt < entries; ++cnt) {
59 		pn = list[cnt];
60 		for (w = pn->whead; w != NULL; w = w->next) {
61 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
62 			    pn->name, MAXREALNAME, MAXREALNAME,
63 			    pn->realname ? pn->realname : "");
64 			if (!w->loginat) {
65 				(void)printf("  *     *  No logins   ");
66 				goto office;
67 			}
68 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
69 			    '*' : ' ');
70 			if (*w->tty)
71 				(void)printf("%-2.2s ",
72 				    w->tty[0] != 't' || w->tty[1] != 't' ||
73 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
74 			else
75 				(void)printf("   ");
76 			if (w->info == LOGGEDIN) {
77 				stimeprint(w);
78 				(void)printf("  ");
79 			} else
80 				(void)printf("    *  ");
81 			p = ctime(&w->loginat);
82 			(void)printf("%.6s", p + 4);
83 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
84 				(void)printf("  %.4s", p + 20);
85 			else
86 				(void)printf(" %.5s", p + 11);
87 office:			if (pn->office)
88 				(void)printf(" %-11.11s", pn->office);
89 			else if (pn->officephone)
90 				(void)printf(" %-11.11s", " ");
91 			if (pn->officephone)
92 				(void)printf(" %s", pn->officephone);
93 			putchar('\n');
94 		}
95 	}
96 }
97 
98 PERSON **
99 sort()
100 {
101 	register PERSON *pn, **lp;
102 	PERSON **list;
103 	int psort();
104 	char *malloc();
105 
106 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
107 		(void)fprintf(stderr, "finger: out of space.\n");
108 		exit(1);
109 	}
110 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
111 		*lp++ = pn;
112 	(void)qsort(list, entries, sizeof(PERSON *), psort);
113 	return(list);
114 }
115 
116 psort(p, t)
117 	PERSON **p, **t;
118 {
119 	return(strcmp((*p)->name, (*t)->name));
120 }
121 
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