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