Annotation of src/usr.bin/finger/sprint.c, Revision 1.10
1.10 ! tedu 1: /* $OpenBSD: sprint.c,v 1.9 2003/07/10 00:06:50 david 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.10 ! tedu 37: static const char rcsid[] = "$OpenBSD: sprint.c,v 1.9 2003/07/10 00:06:50 david 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.9 david 45: #include <string.h>
1.5 mickey 46: #include <err.h>
1.1 deraadt 47: #include "finger.h"
1.3 kstailey 48: #include "extern.h"
1.1 deraadt 49:
1.3 kstailey 50: void
1.8 deraadt 51: sflag_print(void)
1.1 deraadt 52: {
1.3 kstailey 53: PERSON *pn;
54: WHERE *w;
55: int cnt;
56: char *p;
57: PERSON **list;
1.1 deraadt 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
1.3 kstailey 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
1.1 deraadt 76: */
1.6 deraadt 77: #define NAME_WIDTH 8
1.1 deraadt 78: #define MAXREALNAME 20
1.3 kstailey 79: #define MAXHOSTNAME 20
1.6 deraadt 80: (void)printf("%-*.*s %-*s %s %s\n",
81: NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME,
1.3 kstailey 82: "Name", "Tty Idle Login Time ",
83: (oflag) ? "Office Office Phone" : "Where");
1.1 deraadt 84: for (cnt = 0; cnt < entries; ++cnt) {
85: pn = list[cnt];
86: for (w = pn->whead; w != NULL; w = w->next) {
1.6 deraadt 87: (void)printf("%-*.*s %-*.*s ",
88: NAME_WIDTH, UT_NAMESIZE, vs(pn->name),
89: MAXREALNAME, MAXREALNAME,
1.4 kstailey 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 **
1.8 deraadt 136: sort(void)
1.1 deraadt 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);
1.10 ! tedu 146: return (list);
1.1 deraadt 147: }
148:
1.3 kstailey 149: int
1.8 deraadt 150: psort(const void *p, const void *t)
1.1 deraadt 151: {
1.10 ! tedu 152: return (strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
1.1 deraadt 153: }
154:
1.3 kstailey 155: void
1.8 deraadt 156: stimeprint(WHERE *w)
1.1 deraadt 157: {
1.3 kstailey 158: struct tm *delta;
1.1 deraadt 159:
160: delta = gmtime(&w->idletime);
1.10 ! tedu 161: if (!delta->tm_yday) {
! 162: if (!delta->tm_hour) {
1.1 deraadt 163: if (!delta->tm_min)
164: (void)printf(" -");
165: else
166: (void)printf("%5d", delta->tm_min);
1.10 ! tedu 167: } else {
1.1 deraadt 168: (void)printf("%2d:%02d",
169: delta->tm_hour, delta->tm_min);
1.10 ! tedu 170: }
! 171: } else
1.1 deraadt 172: (void)printf("%4dd", delta->tm_yday);
173: }