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