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