print out certain types of phone numbers in US formats
[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
f156ea40 19static char sccsid[] = "@(#)lprint.c 5.5 (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>
28#include "finger.h"
29#include "pathnames.h"
30
31#define LINE_LEN 80
32#define TAB_LEN 8 /* 8 spaces between tabs */
33#define _PATH_PLAN ".plan"
34#define _PATH_PROJECT ".project"
35
36lflag_print()
37{
6b8910b8
KB
38 extern int pplan;
39 register PERSON *pn;
40
7619ff47 41 for (pn = phead;;) {
6b8910b8
KB
42 lprint(pn);
43 if (!pplan) {
7619ff47 44 (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
6b8910b8
KB
45 if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
46 (void)printf("No Plan.\n");
47 }
48 if (!(pn = pn->next))
49 break;
50 putchar('\n');
51 }
52}
53
54lprint(pn)
55 register PERSON *pn;
56{
57 extern time_t now;
58 register struct tm *delta;
7619ff47 59 register WHERE *w;
6b8910b8
KB
60 register int cpr, len, maxlen;
61 int oddfield;
62 time_t time();
f156ea40 63 char *t, *ctime(), *prphone();
6b8910b8
KB
64
65 /*
66 * long format --
67 * login name
68 * real name
69 * home directory
70 * shell
71 * office, office phone, home phone if available
72 */
73 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
74 pn->name, pn->realname, pn->dir);
75 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
76
77 /*
78 * try and print office, office phone, and home phone on one line;
79 * if that fails, do line filling so it looks nice.
80 */
81#define OFFICE_TAG "Office"
82#define OFFICE_PHONE_TAG "Office Phone"
83 oddfield = 0;
84 if (pn->office && pn->officephone &&
85 strlen(pn->office) + strlen(pn->officephone) +
86 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
87 (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
f156ea40 88 prphone(pn->officephone));
6b8910b8
KB
89 oddfield = demi_print(tbuf, oddfield);
90 } else {
91 if (pn->office) {
92 (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
93 oddfield = demi_print(tbuf, oddfield);
94 }
95 if (pn->officephone) {
96 (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
f156ea40 97 prphone(pn->officephone));
6b8910b8
KB
98 oddfield = demi_print(tbuf, oddfield);
99 }
100 }
101 if (pn->homephone) {
f156ea40
KB
102 (void)sprintf(tbuf, "%s: %s", "Home Phone",
103 prphone(pn->homephone));
6b8910b8
KB
104 oddfield = demi_print(tbuf, oddfield);
105 }
106 if (oddfield)
107 putchar('\n');
108
109 /*
7619ff47 110 * long format con't: * if logged in
6b8910b8
KB
111 * terminal
112 * idle time
113 * if messages allowed
114 * where logged in from
7619ff47
EW
115 * if not logged in
116 * when last logged in
6b8910b8 117 */
7619ff47 118 /* find out longest device name for this user for formatting */
3dc87a1e 119 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
7619ff47
EW
120 if ((len = strlen(w->tty)) > maxlen)
121 maxlen = len;
122 /* find rest of entries for user */
123 for (w = pn->whead; w != NULL; w = w->next) {
124 switch (w->info) {
125 case LOGGEDIN:
6b8910b8 126 cpr = printf("On since %16.16s on %s",
7619ff47 127 ctime(&w->loginat), w->tty);
6b8910b8
KB
128 /*
129 * idle time is tough; if have one, print a comma,
130 * then spaces to pad out the device name, then the
131 * idle time. Follow with a comma if a remote login.
132 */
7619ff47 133 delta = gmtime(&w->idletime);
6b8910b8
KB
134 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
135 cpr += printf("%-*s idle ",
7619ff47 136 maxlen - strlen(w->tty) + 1, ",");
6b8910b8
KB
137 if (delta->tm_yday > 0) {
138 cpr += printf("%d day%s ",
139 delta->tm_yday,
140 delta->tm_yday == 1 ? "" : "s");
141 }
142 cpr += printf("%d:%02d",
143 delta->tm_hour, delta->tm_min);
7619ff47 144 if (*w->host) {
6b8910b8
KB
145 putchar(',');
146 ++cpr;
147 }
148 }
7619ff47 149 if (!w->writable)
6b8910b8 150 cpr += printf(" (messages off)");
7619ff47
EW
151 break;
152 case LASTLOG:
153 if (w->loginat == 0) {
154 (void)printf("Never logged in.");
155 break;
6b8910b8 156 }
7619ff47
EW
157 t = ctime(&w->loginat);
158 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
159 cpr = printf("Last login %10.10s, %4.4s on %s",
160 t, t + 20, w->tty);
161 else
162 cpr = printf("Last login %16.16s on %s",
163 t, w->tty);
164 break;
6b8910b8 165 }
7619ff47
EW
166 if (*w->host) {
167 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
6b8910b8 168 (void)printf("\n ");
7619ff47 169 (void)printf(" from %s", w->host);
6b8910b8
KB
170 }
171 putchar('\n');
172 }
6b8910b8
KB
173}
174
175demi_print(str, oddfield)
176 char *str;
177 int oddfield;
178{
179 static int lenlast;
180 int lenthis, maxlen;
181
182 lenthis = strlen(str);
183 if (oddfield) {
184 /*
185 * We left off on an odd number of fields. If we haven't
186 * crossed the midpoint of the screen, and we have room for
187 * the next field, print it on the same line; otherwise,
188 * print it on a new line.
189 *
190 * Note: we insist on having the right hand fields start
191 * no less than 5 tabs out.
192 */
193 maxlen = 5 * TAB_LEN;
194 if (maxlen < lenlast)
195 maxlen = lenlast;
196 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
197 lenthis) <= LINE_LEN) {
198 while(lenlast < (4 * TAB_LEN)) {
199 putchar('\t');
200 lenlast += TAB_LEN;
201 }
202 (void)printf("\t%s\n", str); /* force one tab */
203 } else {
204 (void)printf("\n%s", str); /* go to next line */
205 oddfield = !oddfield; /* this'll be undone below */
206 }
207 } else
208 (void)printf("%s", str);
209 oddfield = !oddfield; /* toggle odd/even marker */
210 lenlast = lenthis;
211 return(oddfield);
212}
213
214show_text(directory, file_name, header)
215 char *directory, *file_name, *header;
216{
217 register int fd, n;
218
219 (void)sprintf(tbuf, "%s/%s", directory, file_name);
220 if ((fd = open(tbuf, O_RDONLY, 0)) < 0)
221 return(0);
222 (void)printf("%s\n", header);
223 (void)fflush(stdout);
224 while ((n = read(fd, tbuf, sizeof(tbuf))) > 0)
225 if (write(1, tbuf, n) != n)
226 break;
227 (void)close(fd);
228 return(1);
229}