lprint.c revision 70474
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95"; 40#else 41static const char rcsid[] = 42 "$FreeBSD: head/usr.bin/finger/lprint.c 70474 2000-12-29 11:39:25Z des $"; 43#endif 44#endif /* not lint */ 45 46#include <sys/types.h> 47#include <sys/stat.h> 48#include <sys/time.h> 49#include <fcntl.h> 50#include <time.h> 51#include <db.h> 52#include <err.h> 53#include <pwd.h> 54#include <utmp.h> 55#include <errno.h> 56#include <unistd.h> 57#include <stdio.h> 58#include <ctype.h> 59#include <string.h> 60#include <paths.h> 61#include "finger.h" 62#include "pathnames.h" 63#include "extern.h" 64 65#define LINE_LEN 80 66#define TAB_LEN 8 /* 8 spaces between tabs */ 67 68static int demi_print __P((char *, int)); 69static void lprint __P((PERSON *)); 70static void vputc __P((unsigned char)); 71 72void 73lflag_print() 74{ 75 extern int pplan; 76 register PERSON *pn; 77 register int sflag, r; 78 PERSON *tmp; 79 DBT data, key; 80 81 for (sflag = R_FIRST;; sflag = R_NEXT) { 82 r = (*db->seq)(db, &key, &data, sflag); 83 if (r == -1) 84 err(1, "db seq"); 85 if (r == 1) 86 break; 87 memmove(&tmp, data.data, sizeof tmp); 88 pn = tmp; 89 if (sflag != R_FIRST) 90 putchar('\n'); 91 lprint(pn); 92 if (!pplan) { 93 (void)show_text(pn->dir, 94 _PATH_FORWARD, "Mail forwarded to"); 95 (void)show_text(pn->dir, _PATH_PROJECT, "Project"); 96 if (!show_text(pn->dir, _PATH_PLAN, "Plan")) 97 (void)printf("No Plan.\n"); 98 (void)show_text(pn->dir, 99 _PATH_PUBLICKEY, "Public key"); 100 } 101 } 102} 103 104static void 105lprint(pn) 106 register PERSON *pn; 107{ 108 extern time_t now; 109 register struct tm *delta; 110 register WHERE *w; 111 register int cpr, len, maxlen; 112 struct tm *tp; 113 int oddfield; 114 char *tzn; 115 char t[80]; 116 117 /* 118 * long format -- 119 * login name 120 * real name 121 * home directory 122 * shell 123 * office, office phone, home phone if available 124 * mail status 125 */ 126 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 127 pn->name, pn->realname, pn->dir); 128 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 129 130 /* 131 * try and print office, office phone, and home phone on one line; 132 * if that fails, do line filling so it looks nice. 133 */ 134#define OFFICE_TAG "Office" 135#define OFFICE_PHONE_TAG "Office Phone" 136 oddfield = 0; 137 if (pn->office && pn->officephone && 138 strlen(pn->office) + strlen(pn->officephone) + 139 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 140 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s", 141 OFFICE_TAG, pn->office, prphone(pn->officephone)); 142 oddfield = demi_print(tbuf, oddfield); 143 } else { 144 if (pn->office) { 145 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 146 OFFICE_TAG, pn->office); 147 oddfield = demi_print(tbuf, oddfield); 148 } 149 if (pn->officephone) { 150 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 151 OFFICE_PHONE_TAG, prphone(pn->officephone)); 152 oddfield = demi_print(tbuf, oddfield); 153 } 154 } 155 if (pn->homephone) { 156 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", 157 prphone(pn->homephone)); 158 oddfield = demi_print(tbuf, oddfield); 159 } 160 if (oddfield) 161 putchar('\n'); 162 163 /* 164 * long format con't: 165 * if logged in 166 * terminal 167 * idle time 168 * if messages allowed 169 * where logged in from 170 * if not logged in 171 * when last logged in 172 */ 173 /* find out longest device name for this user for formatting */ 174 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 175 if ((len = strlen(w->tty)) > maxlen) 176 maxlen = len; 177 /* find rest of entries for user */ 178 for (w = pn->whead; w != NULL; w = w->next) { 179 if (w->info == LOGGEDIN) { 180 tp = localtime(&w->loginat); 181 strftime(t, sizeof(t), "%c", tp); 182 tzn = tp->tm_zone; 183 cpr = printf("On since %.16s (%s) on %s", 184 t, tzn, w->tty); 185 /* 186 * idle time is tough; if have one, print a comma, 187 * then spaces to pad out the device name, then the 188 * idle time. Follow with a comma if a remote login. 189 */ 190 delta = gmtime(&w->idletime); 191 if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 192 cpr += printf("%-*s idle ", 193 maxlen - strlen(w->tty) + 1, ","); 194 if (delta->tm_yday > 0) { 195 cpr += printf("%d day%s ", 196 delta->tm_yday, 197 delta->tm_yday == 1 ? "" : "s"); 198 } 199 cpr += printf("%d:%02d", 200 delta->tm_hour, delta->tm_min); 201 if (*w->host) { 202 putchar(','); 203 ++cpr; 204 } 205 } 206 if (!w->writable) 207 cpr += printf(" (messages off)"); 208 } else if (w->loginat == 0) { 209 cpr = printf("Never logged in."); 210 } else { 211 tp = localtime(&w->loginat); 212 strftime(t, sizeof(t), "%c", tp); 213 tzn = tp->tm_zone; 214 if (now - w->loginat > 86400 * 365 / 2) 215 cpr = 216 printf("Last login %.16s %.4s (%s) on %s", 217 t, t + 20, tzn, w->tty); 218 else 219 cpr = printf("Last login %.16s (%s) on %s", 220 t, tzn, w->tty); 221 } 222 if (*w->host) { 223 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 224 (void)printf("\n "); 225 (void)printf(" from %s", w->host); 226 } 227 putchar('\n'); 228 } 229 if (pn->mailrecv == -1) 230 printf("No Mail.\n"); 231 else if (pn->mailrecv > pn->mailread) { 232 tp = localtime(&pn->mailrecv); 233 strftime(t, sizeof(t), "%c", tp); 234 tzn = tp->tm_zone; 235 printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn); 236 tp = localtime(&pn->mailread); 237 strftime(t, sizeof(t), "%c", tp); 238 tzn = tp->tm_zone; 239 printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn); 240 } else { 241 tp = localtime(&pn->mailread); 242 strftime(t, sizeof(t), "%c", tp); 243 tzn = tp->tm_zone; 244 printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn); 245 } 246} 247 248static int 249demi_print(str, oddfield) 250 char *str; 251 int oddfield; 252{ 253 static int lenlast; 254 int lenthis, maxlen; 255 256 lenthis = strlen(str); 257 if (oddfield) { 258 /* 259 * We left off on an odd number of fields. If we haven't 260 * crossed the midpoint of the screen, and we have room for 261 * the next field, print it on the same line; otherwise, 262 * print it on a new line. 263 * 264 * Note: we insist on having the right hand fields start 265 * no less than 5 tabs out. 266 */ 267 maxlen = 5 * TAB_LEN; 268 if (maxlen < lenlast) 269 maxlen = lenlast; 270 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 271 lenthis) <= LINE_LEN) { 272 while(lenlast < (4 * TAB_LEN)) { 273 putchar('\t'); 274 lenlast += TAB_LEN; 275 } 276 (void)printf("\t%s\n", str); /* force one tab */ 277 } else { 278 (void)printf("\n%s", str); /* go to next line */ 279 oddfield = !oddfield; /* this'll be undone below */ 280 } 281 } else 282 (void)printf("%s", str); 283 oddfield = !oddfield; /* toggle odd/even marker */ 284 lenlast = lenthis; 285 return(oddfield); 286} 287 288int 289show_text(directory, file_name, header) 290 char *directory, *file_name, *header; 291{ 292 struct stat sb; 293 register FILE *fp; 294 register int ch, cnt; 295 register char *p, lastc; 296 int fd, nr; 297 298 lastc = '\0'; 299 300 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name); 301 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) || 302 sb.st_size == 0) 303 return(0); 304 305 /* If short enough, and no newlines, show it on a single line.*/ 306 if (sb.st_size <= LINE_LEN - strlen(header) - 5) { 307 nr = read(fd, tbuf, sizeof(tbuf)); 308 if (nr <= 0) { 309 (void)close(fd); 310 return(0); 311 } 312 for (p = tbuf, cnt = nr; cnt--; ++p) 313 if (*p == '\n') 314 break; 315 if (cnt <= 1) { 316 if (*header != '\0') 317 (void)printf("%s: ", header); 318 for (p = tbuf, cnt = nr; cnt--; ++p) 319 if (*p != '\r') 320 vputc(lastc = *p); 321 if (lastc != '\n') 322 (void)putchar('\n'); 323 (void)close(fd); 324 return(1); 325 } 326 else 327 (void)lseek(fd, 0L, SEEK_SET); 328 } 329 if ((fp = fdopen(fd, "r")) == NULL) 330 return(0); 331 if (*header != '\0') 332 (void)printf("%s:\n", header); 333 while ((ch = getc(fp)) != EOF) 334 if (ch != '\r') 335 vputc(lastc = ch); 336 if (lastc != '\n') 337 (void)putchar('\n'); 338 (void)fclose(fp); 339 return(1); 340} 341 342static void 343vputc(ch) 344 register unsigned char ch; 345{ 346 int meta; 347 348 if (!isprint(ch) && !isascii(ch)) { 349 (void)putchar('M'); 350 (void)putchar('-'); 351 ch = toascii(ch); 352 meta = 1; 353 } else 354 meta = 0; 355 if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) 356 (void)putchar(ch); 357 else { 358 (void)putchar('^'); 359 (void)putchar(ch == '\177' ? '?' : ch | 0100); 360 } 361} 362