file reorg, pathnames.h, paths.h
[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
3dc87a1e 19static char sccsid[] = "@(#)lprint.c 5.4 (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();
63 char *t, *ctime();
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,
88 pn->officephone);
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,
97 pn->officephone);
98 oddfield = demi_print(tbuf, oddfield);
99 }
100 }
101 if (pn->homephone) {
102 (void)sprintf(tbuf, "%s: %s", "Home Phone", pn->homephone);
103 oddfield = demi_print(tbuf, oddfield);
104 }
105 if (oddfield)
106 putchar('\n');
107
108 /*
7619ff47 109 * long format con't: * if logged in
6b8910b8
KB
110 * terminal
111 * idle time
112 * if messages allowed
113 * where logged in from
7619ff47
EW
114 * if not logged in
115 * when last logged in
6b8910b8 116 */
7619ff47 117 /* find out longest device name for this user for formatting */
3dc87a1e 118 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
7619ff47
EW
119 if ((len = strlen(w->tty)) > maxlen)
120 maxlen = len;
121 /* find rest of entries for user */
122 for (w = pn->whead; w != NULL; w = w->next) {
123 switch (w->info) {
124 case LOGGEDIN:
6b8910b8 125 cpr = printf("On since %16.16s on %s",
7619ff47 126 ctime(&w->loginat), w->tty);
6b8910b8
KB
127 /*
128 * idle time is tough; if have one, print a comma,
129 * then spaces to pad out the device name, then the
130 * idle time. Follow with a comma if a remote login.
131 */
7619ff47 132 delta = gmtime(&w->idletime);
6b8910b8
KB
133 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
134 cpr += printf("%-*s idle ",
7619ff47 135 maxlen - strlen(w->tty) + 1, ",");
6b8910b8
KB
136 if (delta->tm_yday > 0) {
137 cpr += printf("%d day%s ",
138 delta->tm_yday,
139 delta->tm_yday == 1 ? "" : "s");
140 }
141 cpr += printf("%d:%02d",
142 delta->tm_hour, delta->tm_min);
7619ff47 143 if (*w->host) {
6b8910b8
KB
144 putchar(',');
145 ++cpr;
146 }
147 }
7619ff47 148 if (!w->writable)
6b8910b8 149 cpr += printf(" (messages off)");
7619ff47
EW
150 break;
151 case LASTLOG:
152 if (w->loginat == 0) {
153 (void)printf("Never logged in.");
154 break;
6b8910b8 155 }
7619ff47
EW
156 t = ctime(&w->loginat);
157 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
158 cpr = printf("Last login %10.10s, %4.4s on %s",
159 t, t + 20, w->tty);
160 else
161 cpr = printf("Last login %16.16s on %s",
162 t, w->tty);
163 break;
6b8910b8 164 }
7619ff47
EW
165 if (*w->host) {
166 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
6b8910b8 167 (void)printf("\n ");
7619ff47 168 (void)printf(" from %s", w->host);
6b8910b8
KB
169 }
170 putchar('\n');
171 }
6b8910b8
KB
172}
173
174demi_print(str, oddfield)
175 char *str;
176 int oddfield;
177{
178 static int lenlast;
179 int lenthis, maxlen;
180
181 lenthis = strlen(str);
182 if (oddfield) {
183 /*
184 * We left off on an odd number of fields. If we haven't
185 * crossed the midpoint of the screen, and we have room for
186 * the next field, print it on the same line; otherwise,
187 * print it on a new line.
188 *
189 * Note: we insist on having the right hand fields start
190 * no less than 5 tabs out.
191 */
192 maxlen = 5 * TAB_LEN;
193 if (maxlen < lenlast)
194 maxlen = lenlast;
195 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
196 lenthis) <= LINE_LEN) {
197 while(lenlast < (4 * TAB_LEN)) {
198 putchar('\t');
199 lenlast += TAB_LEN;
200 }
201 (void)printf("\t%s\n", str); /* force one tab */
202 } else {
203 (void)printf("\n%s", str); /* go to next line */
204 oddfield = !oddfield; /* this'll be undone below */
205 }
206 } else
207 (void)printf("%s", str);
208 oddfield = !oddfield; /* toggle odd/even marker */
209 lenlast = lenthis;
210 return(oddfield);
211}
212
213show_text(directory, file_name, header)
214 char *directory, *file_name, *header;
215{
216 register int fd, n;
217
218 (void)sprintf(tbuf, "%s/%s", directory, file_name);
219 if ((fd = open(tbuf, O_RDONLY, 0)) < 0)
220 return(0);
221 (void)printf("%s\n", header);
222 (void)fflush(stdout);
223 while ((n = read(fd, tbuf, sizeof(tbuf))) > 0)
224 if (write(1, tbuf, n) != n)
225 break;
226 (void)close(fd);
227 return(1);
228}