xref: /original-bsd/usr.bin/finger/sprint.c (revision b3f911e0)
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  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 static char sccsid[] = "@(#)sprint.c	5.6 (Berkeley) 02/07/90";
23 #endif /* not lint */
24 
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <tzfile.h>
28 #include <stdio.h>
29 #include "finger.h"
30 
31 extern int entries;
32 
33 sflag_print()
34 {
35 	extern time_t now;
36 	register PERSON *pn;
37 	register WHERE *w;
38 	register int cnt;
39 	register char *p;
40 	PERSON **list, **sort();
41 	time_t time();
42 	char *ctime(), *prphone();
43 
44 	list = sort();
45 	/*
46 	 * short format --
47 	 *	login name
48 	 *	real name
49 	 *	terminal name (the XX of ttyXX)
50 	 *	if terminal writeable (add an '*' to the terminal name
51 	 *		if not)
52 	 *	if logged in show idle time and day logged in, else
53 	 *		show last login date and time.  If > 6 moths,
54 	 *		show year instead of time.
55 	 *	office location
56 	 *	office phone
57 	 */
58 #define	MAXREALNAME	20
59 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
60 	    "Name", "Tty  Idle  Login        Office     Office Phone");
61 	for (cnt = 0; cnt < entries; ++cnt) {
62 		pn = list[cnt];
63 		for (w = pn->whead; w != NULL; w = w->next) {
64 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
65 			    pn->name, MAXREALNAME, MAXREALNAME,
66 			    pn->realname ? pn->realname : "");
67 			if (!w->loginat) {
68 				(void)printf("  *     *  No logins   ");
69 				goto office;
70 			}
71 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
72 			    '*' : ' ');
73 			if (*w->tty)
74 				(void)printf("%-2.2s ",
75 				    w->tty[0] != 't' || w->tty[1] != 't' ||
76 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
77 			else
78 				(void)printf("   ");
79 			if (w->info == LOGGEDIN) {
80 				stimeprint(w);
81 				(void)printf("  ");
82 			} else
83 				(void)printf("    *  ");
84 			p = ctime(&w->loginat);
85 			(void)printf("%.6s", p + 4);
86 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
87 				(void)printf("  %.4s", p + 20);
88 			else
89 				(void)printf(" %.5s", p + 11);
90 office:			if (pn->office)
91 				(void)printf(" %-10.10s", pn->office);
92 			else if (pn->officephone)
93 				(void)printf(" %-10.10s", " ");
94 			if (pn->officephone)
95 				(void)printf(" %-.15s",
96 				    prphone(pn->officephone));
97 			putchar('\n');
98 		}
99 	}
100 }
101 
102 PERSON **
103 sort()
104 {
105 	register PERSON *pn, **lp;
106 	PERSON **list;
107 	int psort();
108 	char *malloc();
109 
110 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
111 		(void)fprintf(stderr, "finger: out of space.\n");
112 		exit(1);
113 	}
114 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
115 		*lp++ = pn;
116 	(void)qsort(list, entries, sizeof(PERSON *), psort);
117 	return(list);
118 }
119 
120 psort(p, t)
121 	PERSON **p, **t;
122 {
123 	return(strcmp((*p)->name, (*t)->name));
124 }
125 
126 stimeprint(w)
127 	WHERE *w;
128 {
129 	register struct tm *delta;
130 
131 	delta = gmtime(&w->idletime);
132 	if (!delta->tm_yday)
133 		if (!delta->tm_hour)
134 			if (!delta->tm_min)
135 				(void)printf("     ");
136 			else
137 				(void)printf("%5d", delta->tm_min);
138 		else
139 			(void)printf("%2d:%02d",
140 			    delta->tm_hour, delta->tm_min);
141 	else
142 		(void)printf("%4dd", delta->tm_yday);
143 }
144