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