exclude sccs changes to remove contrib line from copyright
[unix-history] / usr / src / usr.bin / finger / lprint.c
CommitLineData
6b8910b8
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
6b8910b8
KB
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
16c61828 19static char sccsid[] = "@(#)lprint.c 5.9 (Berkeley) %G%";
6b8910b8
KB
20#endif /* not lint */
21
22#include <sys/types.h>
23#include <sys/file.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <tzfile.h>
27#include <stdio.h>
d65ceee1 28#include <ctype.h>
6b8910b8
KB
29#include "finger.h"
30#include "pathnames.h"
31
32#define LINE_LEN 80
33#define TAB_LEN 8 /* 8 spaces between tabs */
34#define _PATH_PLAN ".plan"
35#define _PATH_PROJECT ".project"
36
37lflag_print()
38{
6b8910b8
KB
39 extern int pplan;
40 register PERSON *pn;
41
7619ff47 42 for (pn = phead;;) {
6b8910b8
KB
43 lprint(pn);
44 if (!pplan) {
7619ff47 45 (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
6b8910b8
KB
46 if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
47 (void)printf("No Plan.\n");
48 }
49 if (!(pn = pn->next))
50 break;
51 putchar('\n');
52 }
53}
54
55lprint(pn)
56 register PERSON *pn;
57{
58 extern time_t now;
59 register struct tm *delta;
7619ff47 60 register WHERE *w;
6b8910b8
KB
61 register int cpr, len, maxlen;
62 int oddfield;
63 time_t time();
f156ea40 64 char *t, *ctime(), *prphone();
6b8910b8
KB
65
66 /*
67 * long format --
68 * login name
69 * real name
70 * home directory
71 * shell
72 * office, office phone, home phone if available
73 */
74 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
75 pn->name, pn->realname, pn->dir);
76 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
77
78 /*
79 * try and print office, office phone, and home phone on one line;
80 * if that fails, do line filling so it looks nice.
81 */
82#define OFFICE_TAG "Office"
83#define OFFICE_PHONE_TAG "Office Phone"
84 oddfield = 0;
85 if (pn->office && pn->officephone &&
86 strlen(pn->office) + strlen(pn->officephone) +
87 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
88 (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
f156ea40 89 prphone(pn->officephone));
6b8910b8
KB
90 oddfield = demi_print(tbuf, oddfield);
91 } else {
92 if (pn->office) {
93 (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
94 oddfield = demi_print(tbuf, oddfield);
95 }
96 if (pn->officephone) {
97 (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
f156ea40 98 prphone(pn->officephone));
6b8910b8
KB
99 oddfield = demi_print(tbuf, oddfield);
100 }
101 }
102 if (pn->homephone) {
f156ea40
KB
103 (void)sprintf(tbuf, "%s: %s", "Home Phone",
104 prphone(pn->homephone));
6b8910b8
KB
105 oddfield = demi_print(tbuf, oddfield);
106 }
107 if (oddfield)
108 putchar('\n');
109
110 /*
7619ff47 111 * long format con't: * if logged in
6b8910b8
KB
112 * terminal
113 * idle time
114 * if messages allowed
115 * where logged in from
7619ff47
EW
116 * if not logged in
117 * when last logged in
6b8910b8 118 */
7619ff47 119 /* find out longest device name for this user for formatting */
3dc87a1e 120 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
7619ff47
EW
121 if ((len = strlen(w->tty)) > maxlen)
122 maxlen = len;
123 /* find rest of entries for user */
124 for (w = pn->whead; w != NULL; w = w->next) {
125 switch (w->info) {
126 case LOGGEDIN:
6b8910b8 127 cpr = printf("On since %16.16s on %s",
7619ff47 128 ctime(&w->loginat), w->tty);
6b8910b8
KB
129 /*
130 * idle time is tough; if have one, print a comma,
131 * then spaces to pad out the device name, then the
132 * idle time. Follow with a comma if a remote login.
133 */
7619ff47 134 delta = gmtime(&w->idletime);
6b8910b8
KB
135 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
136 cpr += printf("%-*s idle ",
7619ff47 137 maxlen - strlen(w->tty) + 1, ",");
6b8910b8
KB
138 if (delta->tm_yday > 0) {
139 cpr += printf("%d day%s ",
140 delta->tm_yday,
141 delta->tm_yday == 1 ? "" : "s");
142 }
143 cpr += printf("%d:%02d",
144 delta->tm_hour, delta->tm_min);
7619ff47 145 if (*w->host) {
6b8910b8
KB
146 putchar(',');
147 ++cpr;
148 }
149 }
7619ff47 150 if (!w->writable)
6b8910b8 151 cpr += printf(" (messages off)");
7619ff47
EW
152 break;
153 case LASTLOG:
154 if (w->loginat == 0) {
155 (void)printf("Never logged in.");
156 break;
6b8910b8 157 }
7619ff47
EW
158 t = ctime(&w->loginat);
159 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
160 cpr = printf("Last login %10.10s, %4.4s on %s",
161 t, t + 20, w->tty);
162 else
163 cpr = printf("Last login %16.16s on %s",
164 t, w->tty);
165 break;
6b8910b8 166 }
7619ff47
EW
167 if (*w->host) {
168 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
6b8910b8 169 (void)printf("\n ");
7619ff47 170 (void)printf(" from %s", w->host);
6b8910b8
KB
171 }
172 putchar('\n');
173 }
6b8910b8
KB
174}
175
176demi_print(str, oddfield)
177 char *str;
178 int oddfield;
179{
180 static int lenlast;
181 int lenthis, maxlen;
182
183 lenthis = strlen(str);
184 if (oddfield) {
185 /*
186 * We left off on an odd number of fields. If we haven't
187 * crossed the midpoint of the screen, and we have room for
188 * the next field, print it on the same line; otherwise,
189 * print it on a new line.
190 *
191 * Note: we insist on having the right hand fields start
192 * no less than 5 tabs out.
193 */
194 maxlen = 5 * TAB_LEN;
195 if (maxlen < lenlast)
196 maxlen = lenlast;
197 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
198 lenthis) <= LINE_LEN) {
199 while(lenlast < (4 * TAB_LEN)) {
200 putchar('\t');
201 lenlast += TAB_LEN;
202 }
203 (void)printf("\t%s\n", str); /* force one tab */
204 } else {
205 (void)printf("\n%s", str); /* go to next line */
206 oddfield = !oddfield; /* this'll be undone below */
207 }
208 } else
209 (void)printf("%s", str);
210 oddfield = !oddfield; /* toggle odd/even marker */
211 lenlast = lenthis;
212 return(oddfield);
213}
214
215show_text(directory, file_name, header)
216 char *directory, *file_name, *header;
217{
980f4f50
EW
218 register int ch, lastc;
219 register FILE *fp;
6b8910b8
KB
220
221 (void)sprintf(tbuf, "%s/%s", directory, file_name);
980f4f50 222 if ((fp = fopen(tbuf, "r")) == NULL)
6b8910b8
KB
223 return(0);
224 (void)printf("%s\n", header);
980f4f50
EW
225 while ((ch = getc(fp)) != EOF)
226 vputc(lastc = ch);
227 if (lastc != '\n')
d65ceee1 228 (void)putchar('\n');
980f4f50 229 (void)fclose(fp);
6b8910b8
KB
230 return(1);
231}
d65ceee1
KB
232
233vputc(ch)
234 register int ch;
235{
236 int meta;
237
238 if (!isascii(ch)) {
239 (void)putchar('M');
240 (void)putchar('-');
241 ch = toascii(ch);
242 meta = 1;
243 } else
244 meta = 0;
d5df533e 245 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
d65ceee1
KB
246 (void)putchar(ch);
247 else {
248 (void)putchar('^');
249 (void)putchar(ch == '\177' ? '?' : ch | 0100);
250 }
251}