sprint.c revision 1.4 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, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)sprint.c 5.8 (Berkeley) 12/4/90";*/
39 static char rcsid[] = "$Id: sprint.c,v 1.4 1996/11/21 06:01:51 lukem Exp $";
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <tzfile.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include "finger.h"
48 #include "extern.h"
49
50 void
51 sflag_print()
52 {
53 PERSON *pn;
54 WHERE *w;
55 int cnt;
56 char *p;
57 PERSON **list;
58
59 list = sort();
60 /*
61 * short format --
62 * login name
63 * real name
64 * terminal name (the XX of ttyXX)
65 * if terminal writeable (add an '*' to the terminal name
66 * if not)
67 * if logged in show idle time and day logged in, else
68 * show last login date and time. If > 6 months,
69 * show year instead of time. If < 6 days,
70 * show day name instead of month & day.
71 * if -h given
72 * remote host
73 * else if -o given (overriding -h) (default)
74 * office location
75 * office phone
76 */
77 #define MAXREALNAME 20
78 #define MAXHOSTNAME 20
79 (void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
80 "Name", "Tty Idle Login Time",
81 (oflag) ? "Office Office Phone" : "Where");
82 for (cnt = 0; cnt < entries; ++cnt) {
83 pn = list[cnt];
84 for (w = pn->whead; w != NULL; w = w->next) {
85 (void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
86 pn->name, MAXREALNAME, MAXREALNAME,
87 pn->realname ? pn->realname : "");
88 if (!w->loginat) {
89 (void)printf(" * * No logins ");
90 goto office;
91 }
92 (void)putchar(w->info == LOGGEDIN && !w->writable ?
93 '*' : ' ');
94 if (*w->tty)
95 (void)printf("%-2.2s ",
96 w->tty[0] != 't' || w->tty[1] != 't' ||
97 w->tty[2] != 'y' ? w->tty : w->tty + 3);
98 else
99 (void)printf(" ");
100 if (w->info == LOGGEDIN) {
101 stimeprint(w);
102 (void)printf(" ");
103 } else
104 (void)printf(" * ");
105 p = ctime(&w->loginat);
106 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
107 (void)printf(" %.3s ", p);
108 else
109 (void)printf("%.6s", p + 4);
110 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
111 (void)printf(" %.4s", p + 20);
112 else
113 (void)printf(" %.5s", p + 11);
114 office:
115 putchar(' ');
116 if (oflag) {
117 if (pn->office)
118 (void)printf("%-10.10s", pn->office);
119 else if (pn->officephone)
120 (void)printf("%-10.10s", " ");
121 if (pn->officephone)
122 (void)printf(" %-.15s",
123 prphone(pn->officephone));
124 } else
125 (void)printf("%.*s", MAXHOSTNAME, w->host);
126 putchar('\n');
127 }
128 }
129 }
130
131 PERSON **
132 sort()
133 {
134 PERSON *pn, **lp;
135 PERSON **list;
136
137 if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
138 (void)fprintf(stderr, "finger: out of space.\n");
139 exit(1);
140 }
141 for (lp = list, pn = phead; pn != NULL; pn = pn->next)
142 *lp++ = pn;
143 (void)qsort(list, entries, sizeof(PERSON *), psort);
144 return(list);
145 }
146
147 int
148 psort(p, t)
149 const void *p, *t;
150 {
151 return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
152 }
153
154 void
155 stimeprint(w)
156 WHERE *w;
157 {
158 struct tm *delta;
159
160 delta = gmtime(&w->idletime);
161 if (!delta->tm_yday)
162 if (!delta->tm_hour)
163 if (!delta->tm_min)
164 (void)printf(" -");
165 else
166 (void)printf("%5d", delta->tm_min);
167 else
168 (void)printf("%2d:%02d",
169 delta->tm_hour, delta->tm_min);
170 else
171 (void)printf("%4dd", delta->tm_yday);
172 }
173